Advertisement
Guest User

Untitled

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