Advertisement
Guest User

Untitled

a guest
Jun 1st, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.69 KB | None | 0 0
  1. ko.validation.init( { decorateInputElement: true, errorElementClass: 'has-error' } );
  2.  
  3. var viewModel = function() {
  4. var self = this;
  5.  
  6. this.authToken = ko.observable(false);
  7. this.user = ko.observable(false);
  8. this.fundName = ko.observable();
  9. this.fundId = ko.observable();
  10. self.fundCode = ko.observable(false);
  11. this.selectedPack = ko.observable(new packModel());
  12. this.selectedFund = ko.observable(false);
  13. this.fundList = ko.observableArray([]);
  14. this.searchActive = ko.observable(false);
  15. this.downloadActive = ko.observable(false);
  16. this.searchPerformed = ko.observable(false);
  17. this.isFundSelected = ko.observable(false);
  18. this.isGenerating = ko.observable(false);
  19. this.orderRequest = ko.observable();
  20. this.orderResponse = ko.observable();
  21. this.orderId = ko.observable();
  22. this.documentDate = ko.observable();
  23. this.documentPayload = ko.observableArray([]);
  24. this.documentPending = ko.observable(0);
  25. this.documentRequests = ko.observable(0);
  26. this.documentResults = ko.observable(0);
  27. this.documentSuccess = ko.observable(0);
  28. this.documentErrors = ko.observable(0);
  29. this.orderList = ko.observable(new completedOrderModel());
  30. this.orderListPending = ko.observable(true);
  31. this.documentPacks = ko.observableArray([]);
  32. var today = new Date().toISOString().substr(0, 10);
  33.  
  34. this.documentPercentage = ko.computed(function() {
  35. return Math.ceil(percentage = self.documentResults() / self.documentRequests() * 100);
  36. });
  37.  
  38. this.endpoint = ko.computed(function() {
  39. if (self.fundCode() === false) return false;
  40. else return '/api/docgen/fund/' + self.fundCode();
  41. });
  42.  
  43. this.order = ko.computed(function() {
  44. return ko.observable({ order: self.orderId(), product: self.selectedPack.productId });
  45. });
  46.  
  47. this.authDetails = ko.observable({
  48. password: 'Xj68B7dNGNDDGxg',
  49. username: 'springdoks_headless@heffron.com.au',
  50. client_id: '5_2vc4nignqc84cggcs0wc8kkk40oks8owcgk008okw0gs8480kw',
  51. client_secret: '3uceloicaiwwww0swc040kso4c8k8o84s0ocs0oc0wcs8s8o4s',
  52. grant_type: 'password'
  53. });
  54.  
  55. var dateTime = new Date();
  56. dateTime = moment(dateTime).format("YYYY-MM-DD");
  57. console.log('new date ' + dateTime);
  58.  
  59. this.initPacks = function() {
  60. self.documentPacks.push
  61. (
  62. new packModel('NAB - Fund Establishment Pack', '6', [
  63. new documentModel('NAB FSU Cover Letter', 'coverletter', 'NAB_FSU_ADMIN_Cover_Letter.docx', null, false, [], self),
  64. new documentModel('Financial Services Guide', 'fsg', 'FSG.docx', null, false, [
  65. new documentDataModel(this.documentDate() ? this.documentDate() : dateTime, 'productId', 6, null, null)
  66. ], self),
  67. new documentModel('Consent To Act', 'consenttoact', 'Consent_to_act.docx', 632, true, [], self),
  68. new documentModel('Trustee Declarations', 'trusteedeclaration', 'Trustee_Declaration.pdf', 634, true, [], self),
  69. new documentModel('Fund Establishment Minute', 'minute', 'FSU_Minute.docx', 564, true, [
  70. new documentDataModel('Does this minute require a meeting notice?', 'meetingNoticeRequired', false, [ { optionVar: 'Yes', valueVar: true }, { optionVar: 'No', valueVar: false } ], 'radio')
  71. ], self),
  72. new documentModel('Trust Deed', 'fsudeed', null, 631, true, [
  73. new documentDataModel(null, 'templateNameCorporate', 'FSU_Deed_Corporate.docx', null, null),
  74. new documentDataModel(null, 'templateNameIndividual', 'FSU_Deed_Individual.docx', null, null),
  75. new documentDataModel(null, 'deedVersion', '3.01', null, null),
  76. ], self),
  77. new documentModel('SMSF Rules version 3.01', 'static', 'SMSF_Rules_Version_3.01.pdf', null, null, null, self),
  78. new documentModel('Product Disclosure Statement', 'pds', 'PDS.docx', 108, true, [], self),
  79. new documentModel('Member Application Minute', 'minute', 'MemberApplication_Minute.docx', 568, true, [
  80. new documentDataModel(null, 'meetingNoticeRequired', false, null, null)
  81. ], self),
  82. new documentModel('Member Application', 'memberapplications', 'Member_Application.docx', 502, true, [], self),
  83. new documentModel('Service Agreement', 'serviceagreement', 'NAB_FSU_ServiceAgreement.docx', 100, [true], null, self),
  84. new documentModel('Minute Appointing Heffron as Administrator', 'minute', 'HeffronAppointmentLPOA_Minute.docx', 566, true, [
  85. new documentDataModel('Does this minute require a meeting notice?', 'meetingNoticeRequired', false, [ { optionVar: 'Yes', valueVar: true }, { optionVar: 'No', valueVar: false } ], 'radio')
  86. ], self),
  87. new documentModel('Limited Power of Attorney', 'lpoa', 'LPOA.docx', 262, [true], null, self),
  88. new documentModel('Nab 100 Point Identification Check', 'static', 'NAB_100point_Identification.PDF', null, null, null, self),
  89. new documentModel('Nab Withholding Tax Statement', 'static', 'NAB_Withholding_Tax_Statement.PDF', null, null, null, self)
  90. ],
  91. [
  92. { 'title': 'Cover Letter Address', 'text': 'IF DIRECT... Fund Contact Profile -> Legal Documents, IF REFERRED... Referrer Person -> Business Postal.' },
  93. { 'title': 'PDS & Direct Debit Request Address (and phone if PDS)', 'text': 'Fund Contact Profile -> Legal Documents.' },
  94. { 'title': 'For any member/trustee Addresses', 'text': 'Person\'s Residential Address.' },
  95. { 'title': 'Corporate Trustee Company', 'text': 'Registered Office Address' },
  96. { 'title': 'ATO Direct Debit Address', 'text': 'Heffron\'s Postal Address' }
  97. ], self)
  98. );
  99.  
  100. self.documentPacks.push(
  101. new packModel('Corporate Trustee Establishment', '7', [
  102. new documentModel('Corporate Trustee Cover Letter', 'coverletter', null, null, false, [
  103. new documentDataModel('Is Heffron the registered address for this corporation?', 'templateName', null, [ { optionVar: 'Yes', valueVar: 'CORP_TRUSTEE_Heffron_RegAddr_Cover_Letter.docx' }, { optionVar: 'No', valueVar: 'CORP_TRUSTEE_Heffron_NOT_RegAddr_Cover_Letter.docx' } ], 'radio')
  104. ], self),
  105. ],
  106. [
  107. { 'title': 'Cover Letter Address', 'text': 'IF DIRECT... Fund Contact Profile -> Legal Documents, IF REFERRED... Referrer Person -> Business Postal' }
  108. ], self)
  109. );
  110.  
  111. self.documentPacks.push(
  112. new packModel('NAB - Welcome Pack', '6', [
  113. new documentModel('Welcome Pack Cover Letter', 'coverletter', 'NAB_FSU_ADMIN_Cover_Letter.docx', null, false, [], self),
  114. new documentModel('Information regarding Death Benefits', 'static', 'Information_Concerning_Death_Benefits.pdf', null, null, null, self),
  115. new documentModel('Notice of Preferred Beneficiaries (for each member)', 'dbn', 'Notice_of_Preferred_Beneficiaries.docx', 302, [true], null, self),
  116. new documentModel('Investment Strategy Minute', 'minute', 'InvestmentStrategy_Minute.docx', 565, [true], null, self),
  117. new documentModel('Investment Strategy (blank)', 'investmentstrategy', 'InvestmentStrategy_Blank.docx', 105, [true], null, self),
  118. new documentModel('ATO Standard Choice Form)', 'standardchoice', 'Superannuation_standard_choice_form.pdf', 369, [true], null, self),
  119. new documentModel('Direct Debit Request)', 'directdebitrequest', 'Direct_Debit_Request', 261, [true], null, self),
  120. ],
  121. [
  122. { 'title': 'Cover Letter Address', 'text': 'IF DIRECT... Fund Contact Profile -> Legal Documents, IF REFERRED... Referrer Person -> Business Postal' }
  123. ], self)
  124. );
  125. }
  126.  
  127. // This is how I do an AJAX POST against HELiX - to Authenticate
  128. $.post("/api/oauth/v2/token", self.authDetails(), function(data) {
  129. self.authToken(data);
  130. }).done( function() {
  131. $.ajax({
  132. url: "/api/helix/user/authenticated",
  133. headers: {
  134. 'Content-Type': 'application/json',
  135. 'Authorization': 'Bearer ' + self.authToken().access_token
  136. },
  137. success: function(response) {
  138. // If the response comes back, add the JSON array to my fundList variable.
  139. if (response.data) {
  140. self.user(response.data);
  141. }
  142. }
  143. });
  144. }).done( function() {
  145. self.orderListPending(true);
  146. self.fetchOrders();
  147. });
  148.  
  149. this.newOrder = function() {
  150. self.orderList([]);
  151. self.initPacks();
  152. }
  153.  
  154. this.fetchFunds = function() {
  155. self.searchActive(true);
  156. self.fundList.removeAll();
  157.  
  158. // This is how I do an AJAX GET against HELiX - to Search for Funds
  159. $.ajax({
  160. url: "/api/helix/fund/search?offset=0&limit=5&filters[name]=" + self.fundName(),
  161. headers: {
  162. 'Content-Type': 'application/json',
  163. 'Authorization': 'Bearer ' + self.authToken().access_token
  164. },
  165. success: function(response) {
  166. self.searchPerformed(true);
  167. self.searchActive(false);
  168.  
  169. // If the response comes back, add the JSON array to my fundList variable.
  170. if (response) {
  171. self.fundList(response.data);
  172. }
  173. }
  174. });
  175. }
  176.  
  177. this.fetchOrders = function() {
  178. $.ajax({
  179. url: "/api/helix/order?filters&limit=20&order_by[createdDate]=DESC&serialize=fundOrders",
  180. headers: {
  181. 'Content-Type': 'application/json',
  182. 'Authorization': 'Bearer ' + self.authToken().access_token
  183. },
  184. success: function(response) {
  185. // If the response comes back, add the JSON array to my fundList variable.
  186. if (response.data) {
  187. self.orderList(response.data);
  188. }
  189. },
  190. complete: function() {
  191. self.orderListPending(false);
  192. }
  193. });
  194. }
  195.  
  196. this.selectFund = function(data) {
  197. self.orderResponse(false);
  198. self.selectedFund(data);
  199. self.fundList([]);
  200. self.fundName(data.name);
  201. self.fundId(data.id);
  202. self.fundCode(data.fundCode);
  203. self.createOrder(new orderModel(1, self.user().id));
  204. }
  205.  
  206. this.createOrder = function (order) {
  207. self.orderRequest(order);
  208. self.orderRequest().addFund(self.fundId());
  209.  
  210. $.ajax({
  211. url: "/api/helix/order",
  212. headers: {
  213. 'Content-Type': 'application/json',
  214. 'Authorization': 'Bearer ' + self.authToken().access_token
  215. },
  216. type: 'POST',
  217. contentType: 'application/json',
  218. data: ko.toJSON(self.orderRequest()),
  219. success: function(response) {
  220. if (response.data) {
  221. self.orderResponse(response.data);
  222. self.orderId(self.orderResponse().id);
  223. }
  224. },
  225. async: false
  226. });
  227. };
  228.  
  229. this.updateOrder = function () {
  230. $.ajax({
  231. url: "/api/helix/order/" + self.orderId() + "/product",
  232. headers: {
  233. 'Content-Type': 'application/json',
  234. 'Authorization': 'Bearer ' + self.authToken().access_token
  235. },
  236. type: 'POST',
  237. contentType: 'application/json',
  238. data: ko.toJSON( { product: self.selectedPack().productId } ),
  239. async: false
  240. });
  241. };
  242.  
  243. this.generateDocuments = function() {
  244. self.updateOrder();
  245. self.isGenerating(true);
  246.  
  247. $.each(self.selectedPack().documents(), function(key, doc) {
  248. if (doc.generateDocument() === true) {
  249. self.documentPayload.push(doc);
  250. self.documentPending(self.documentPending() + 1);
  251. self.documentRequests(self.documentRequests() + 1);
  252. }
  253. });
  254.  
  255. $.each(self.documentPayload(), function(key, doc) {
  256. if (doc.isGenerated() === false && doc.isError() === false) self.generateDocument(key, doc);
  257. });
  258.  
  259. self.documentPacks.remove(self.selectedPack());
  260. }
  261.  
  262. this.generateDocument = function(key, doc) {
  263. $.ajax({
  264. beforeSend: function(xhr) {
  265. doc.isGenerating(true);
  266. doc.isGenerated(false);
  267. xhr.setRequestHeader ("Authorization", "Basic " + btoa(self.authDetails().username + ":" + self.authDetails().password));
  268. },
  269. url: doc.url() + '&docNumber=' + (key + 1),
  270. type: 'GET',
  271. contentType: 'application/json',
  272. data: true,
  273. timeout: 300000,
  274. success: function(response) {
  275. doc.isGenerated(true);
  276. self.documentSuccess(self.documentSuccess() + 1);
  277. },
  278. error: function(response) {
  279. doc.isError(true);
  280. self.documentErrors(self.documentErrors() + 1);
  281. },
  282. complete: function(response) {
  283. doc.isGenerating(false);
  284. doc.documentResponse(response);
  285. self.documentResults(self.documentResults() + 1);
  286. self.documentPending(self.documentPending() - 1);
  287. },
  288. async: true
  289. });
  290. }
  291.  
  292. this.addAnotherProduct = function() {
  293. self.selectedPack();
  294. self.documentResults(0);
  295. self.documentSuccess(0);
  296. self.documentErrors(0);
  297. self.documentPending(0);
  298. self.documentRequests(0);
  299. self.isGenerating(false);
  300. };
  301.  
  302. this.completeOrder = function() {
  303. self.orderListPending(true);
  304. self.fetchOrders();
  305. self.fundName('');
  306. self.fundId('');
  307. self.selectedPack('');
  308. self.selectedFund(false);
  309. self.fundList([]);
  310. self.searchActive(false);
  311. self.searchPerformed(false);
  312. self.isFundSelected(false);
  313. self.isGenerating(false);
  314. self.orderRequest('');
  315. self.orderResponse('');
  316. self.documentDate('');
  317. self.documentPayload([]);
  318. self.documentResults(0);
  319. self.documentSuccess(0);
  320. self.documentErrors(0);
  321. self.documentPending(0);
  322. self.documentRequests(0);
  323. }
  324. };
  325.  
  326. var orderModel = function(status, user, product) {
  327. self = this;
  328.  
  329. this.status = ko.observable(status);
  330. this.user = ko.observable(user);
  331. this.product = ko.observable(product);
  332. this.funds = ko.observableArray([]);
  333.  
  334. this.addFund = function(fundId) {
  335. self.funds().push( { fund: fundId } );
  336. }
  337. }
  338.  
  339. var packModel = function(name, productId, documents, instructions, parent) {
  340. self = this;
  341.  
  342. this.name = name;
  343. this.productId = productId;
  344. this.documents = ko.observableArray(documents);
  345. this.instructions = ko.observableArray(instructions);
  346. }
  347.  
  348. var documentModel = function(name, endpoint, filename, docTypeId, barcode, data, parent) {
  349. self = this;
  350.  
  351. this.name = ko.observable(name);
  352. this.endpointName = ko.observable(endpoint);
  353. this.templateName = ko.observable(filename);
  354. this.docTypeId = ko.observable(docTypeId);
  355. this.barcode = ko.observable(barcode);
  356. this.orderId = ko.observable(false);
  357. this.documentDate = ko.observable(false);
  358. this.generateDocument = ko.observable(true);
  359. this.documentResponse = ko.observable(false);
  360. this.isGenerating = ko.observable(false);
  361. this.isError = ko.observable(false);
  362. this.isGenerated = ko.observable(false);
  363.  
  364. parent.orderId.subscribe(function(orderId) {
  365. this.orderId(orderId);
  366. }.bind(this));
  367.  
  368. parent.documentDate.subscribe(function(documentDate) {
  369. this.documentDate(documentDate);
  370. }.bind(this));
  371.  
  372. this.data = ko.observableArray(data);
  373.  
  374. this.url = ko.computed(function() {
  375. if (parent.endpoint() === false || this.orderId === false) return false;
  376. else {
  377.  
  378. // Our default baseline URL
  379. url = parent.endpoint() + '/' + this.endpointName() + '?orderId=' + this.orderId();
  380.  
  381. // Our optional additional parameters
  382. if (this.templateName()) url = url + '&templateName=' + this.templateName();
  383. if (this.docTypeId()) url = url + '&docTypeId=' + this.docTypeId();
  384. if (this.barcode()) url = url + '&generateBarcode=true';
  385. if (this.documentDate() !== false) url = url + '&documentDate=' + this.documentDate();
  386.  
  387. // Our dynamically generated optional parameters
  388. if (this.data() !== null) {
  389. for (var i = 0, len = this.data().length; i < len; i++) {
  390. url = url + '&' + this.data()[i].variable() + '=' + this.data()[i].value();
  391. }
  392. }
  393. return url;
  394. }
  395. }, self);
  396. }
  397.  
  398. var documentDataModel = function(description, variable, value, options, type) {
  399. self = this;
  400.  
  401. this.description = ko.observable(description);
  402. this.variable = ko.observable(variable);
  403. this.value = ko.observable(value);
  404. this.options = ko.observableArray(options);
  405. this.type = ko.observable(type);
  406. }
  407.  
  408. var completedOrderModel = ko.mapping.fromJS(SPRiNGDOKS);
  409.  
  410. // This is telling KnockoutJS to load & fire up our viewModel
  411. var SPRiNGDOKS = new viewModel();
  412. ko.applyBindings(SPRiNGDOKS);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement