Advertisement
nkarmi

Untitled

Aug 29th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.12 KB | None | 0 0
  1. (function () {
  2. 'use strict';
  3. angular
  4. .module('calculatorsApp')
  5. .controller('CalculatorsCtrl', CalculatorsCtrl);
  6.  
  7. CalculatorsCtrl.$inject = ['$scope', '$state', '$uibModalInstance', '$q', 'calcMethodService', 'categoryService', 'filterFilter', 'notesService', 'errorHandler'];
  8.  
  9. function CalculatorsCtrl($scope, $state, $uibModalInstance, $q, calcMethodService, categoryService, filterFilter, notesService, errorHandler) {
  10. var vm = this;
  11. vm.sParams = $state.transition ? $state.transition.targetState().params() : $state.params;
  12. vm.dp = vm.sParams.dp;
  13.  
  14. vm.submit = submit;
  15. vm.cancel = cancel;
  16. vm.selectCalculator = selectCalculator;
  17. vm.result = {};
  18. var statePrefix;
  19. init();
  20.  
  21. function init() {
  22. statePrefix = $state.current.name.split('.')[0] == 'delegation' ? 'delegation.' : '';
  23. loadCalcMethods();
  24. }
  25.  
  26. /**
  27. * Populate the calculated methods drop down and set the selected method
  28. */
  29. function loadCalcMethods() {
  30. var promise = calcMethodService.getCalcMethods(vm.sParams.delegationId);
  31. promise.then(function (data) {
  32. vm.calcMethods = data;
  33. if (vm.dp.CalcMethodId) { //Set the selected method based on the pervious user selection
  34. vm.selectedMethod = filterFilter(vm.calcMethods, { id: vm.dp.CalcMethodId })[0];
  35. }
  36. else if (vm.dp.PreselectedCalcMethod){ //If no calc method used then select the previous year calc method as default
  37. vm.selectedMethod = filterFilter(vm.calcMethods, { id: vm.dp.PreselectedCalcMethod })[0];
  38. }
  39. else { //If no calc method used then select the first one as default
  40. vm.selectedMethod = !!vm.calcMethods ? vm.calcMethods[0] : {};
  41. }
  42. vm.selectCalculator(vm.selectedMethod);
  43. })
  44. .catch(function (err) {
  45. errorHandler.handle('Failed to load calculation method list: ', err);
  46. });
  47. }
  48.  
  49. function submit(calcResult) {
  50. if (calcResult.type === 'paste') {
  51. vm.save.paste(calcResult)
  52. $uibModalInstance.close({ data: calcResult });
  53. $state.go(statePrefix + 'collect', null, { reload: true }); //reload the parent state when closing the modal
  54. } else {
  55. vm.save.math(calcResult)
  56. .then(function () {
  57. console.log("submitted", calcResult);
  58. $uibModalInstance.close({ data: calcResult });
  59. $state.go(statePrefix + 'collect', null, { reload: true }); //reload the parent state when closing the modal
  60. });
  61. }
  62. }
  63.  
  64. function cancel() {
  65. $uibModalInstance.dismiss('cancel');
  66. $state.go(statePrefix + 'collect'); //back to the parent state when closing the modal without reloading
  67. }
  68.  
  69. $scope.$on('modal.closing', function (event, reason, closed) {
  70. //console.log(event);
  71. console.log('Reason:', reason);
  72. $state.go(statePrefix + 'collect'); //back to the parent state when closing the modal without reloading
  73. });
  74.  
  75. function selectCalculator(method) {
  76. if (!method) return;
  77. //console.log('Calculator:', method);
  78. switch (method.id) { //This is a temporary allocation of calculators. Need to formalize as configuration rather than code changes
  79. case 11:
  80. $state.go(statePrefix + 'collect.calculator.paste');
  81. break;
  82. default:
  83. $state.go(statePrefix + 'collect.calculator.math');
  84. };
  85. }
  86.  
  87. vm.save = {
  88. math: function (result) {
  89. vm.dp.ActualValue = result.totalValue;
  90. vm.dp.CalcMethodId = vm.selectedMethod.id;
  91. vm.dp.CalcComment = result.comment ? result.comment : '';
  92. var dpList = [];
  93. dpList.push(vm.dp);
  94. //return categoryService.putDP(vm.sParams.responseFormat, vm.dp, vm.sParams.assetId, vm.sParams.month, vm.sParams.year);
  95. return categoryService.saveDps(vm.sParams.month, vm.sParams.year, vm.sParams.assetId, dpList, vm.sParams.delegationId)
  96. .then(function (pl) {
  97. //console.log('Saved', result);
  98.  
  99. var note = {
  100. dpId: vm.dp.DPId,
  101. assetId: vm.sParams.assetId,
  102. month: vm.sParams.month,
  103. year: vm.sParams.year,
  104. commentTxt: vm.dp.CalcComment,
  105. delegationId: vm.sParams.delegationId
  106. };
  107.  
  108. return result.comment ? notesService.addNote(note) : $q.when(false);
  109. })
  110. .then(function (response) {
  111. console.log(response ? 'Note successfully added' : 'There was no note');
  112. //$uibModalInstance.close({ data: calcResult });
  113. })
  114. .catch(function (err) {
  115. console.log('Calculator save failed', err.data);
  116. });
  117. },
  118. paste: function (result) {
  119. console.log('Paste: ', result);
  120. return categoryService.saveDps(vm.sParams.month, vm.sParams.year, vm.sParams.assetId, result.dpList, vm.sParams.delegationId)
  121. .then(function (data) {
  122. console.log('saved information for a month range:', data);
  123. })
  124. .catch(function (error) {
  125. errorHandler.handle('Failed to update data', error);
  126. })
  127. }
  128. };
  129.  
  130. }
  131.  
  132. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement