account.controller.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. (function(){
  2. 'use strict';
  3. angular
  4. .module('cloudbudget')
  5. .filter('category', CategoryFilter)
  6. .filter('sub_category', SubcategoryFilter)
  7. .controller('AccountController', AccountController);
  8. function CategoryFilter() {
  9. return function(input, categories) {
  10. if( !input ) {
  11. return '';
  12. }
  13. var category = categories.filter(function(elt, idx) {
  14. return elt._id === input;
  15. });
  16. if( category.length > 0 ) {
  17. return category[0].label;
  18. }
  19. return '';
  20. };
  21. }
  22. function SubcategoryFilter() {
  23. return function(input, category_id, categories) {
  24. if( !input || !category_id) {
  25. return '';
  26. }
  27. var category = categories.filter(function(elt, idx) {
  28. return elt._id === category_id;
  29. })[0];
  30. if( !category ) {
  31. return '';
  32. }
  33. var res = category.sub_categories.filter( function(elt, idx) {
  34. return elt._id === input;
  35. });
  36. if( res.length === 1 ) {
  37. return res[0].label;
  38. } else {
  39. return '';
  40. }
  41. };
  42. }
  43. AccountController.$inject = ['$scope', '$location', '$routeParams', 'FlashService', 'AccountService'];
  44. function AccountController($scope, $location, $routeParams, FlashService, AccountService) {
  45. var vm = this;
  46. $scope.calendar = {
  47. opened: {},
  48. dateFormat: 'dd/MM/yyyy',
  49. dateOptions: {},
  50. open: function($event, which) {
  51. $event.preventDefault();
  52. $event.stopPropagation();
  53. $scope.calendar.opened[which] = true;
  54. }
  55. };
  56. vm.dataLoading = false;
  57. vm.entries = [];
  58. vm.balance = undefined;
  59. vm.categories = [];
  60. vm.sub_categories = [];
  61. vm.account = undefined;
  62. vm.create = create;
  63. vm.drop = drop;
  64. vm.edit = edit;
  65. vm.updateSubCategory = updateSubCategory;
  66. vm.updateSubCategoryEditForm = updateSubCategoryEditForm;
  67. vm.disabledSubCategories = false;
  68. vm.edit_sub_categories = [];
  69. (function init() {
  70. vm.dataLoading = true;
  71. AccountService.details($routeParams.account_id)
  72. .then(function(response) {
  73. if( response.success ) {
  74. vm.account = response.account;
  75. vm.categories = angular.copy(vm.account.categories);
  76. vm.categories.unshift({_id: '', label: ''});
  77. } else {
  78. FlashService.error(response.message);
  79. }
  80. vm.dataLoading = false;
  81. });
  82. AccountService.list($routeParams.account_id)
  83. .then(function(response) {
  84. if( response.success ) {
  85. vm.balance = response.data.balance;
  86. vm.entries = response.data.entries;
  87. } else {
  88. FlashService.error(response.message);
  89. }
  90. });
  91. })();
  92. function create() {
  93. vm.dataLoading = true;
  94. AccountService.create(vm.account, vm.entry)
  95. .then( function(response) {
  96. if( response.success) {
  97. vm.balance = response.data.balance;
  98. vm.entries = response.data.entries;
  99. } else {
  100. FlashService.error(response.message);
  101. }
  102. vm.dataLoading = false;
  103. });
  104. vm.entry = angular.copy({});
  105. $scope.form.$setPristine();
  106. };
  107. function drop(entry) {
  108. vm.dataLoading = true;
  109. AccountService.drop(vm.account, entry)
  110. .then(function(response) {
  111. if( response.success ) {
  112. vm.balance = response.data.balance;
  113. vm.entries = response.data.entries;
  114. } else {
  115. FlashService.error( response.message );
  116. }
  117. vm.dataLoading = false;
  118. });
  119. };
  120. function edit(altered, origin) {
  121. vm.dataLoading = true;
  122. return AccountService.edit(vm.account, origin._id, altered)
  123. .then( function(response) {
  124. vm.dataLoading = false;
  125. if( response.success ) {
  126. vm.balance = response.data.balance;
  127. var index = vm.entries.map(function (item) {
  128. return item._id;
  129. }).indexOf(origin._id);
  130. vm.entries[index] = response.data.entries[index];
  131. } else {
  132. var index = vm.entries.map(function (item) {
  133. return item._id;
  134. }).indexOf(origin._id);
  135. vm.entries[index] = origin;
  136. FlashService.error( response.message );
  137. return false;
  138. }
  139. })
  140. };
  141. function updateSubCategory() {
  142. vm.sub_categories = getSubCategories(vm.entry.category);
  143. };
  144. function updateSubCategoryEditForm(category_id) {
  145. vm.edit_sub_categories = getSubCategories(category_id);
  146. vm.disabledSubCategories = !vm.edit_sub_categories || vm.edit_sub_categories.length === 0;
  147. };
  148. function getSubCategories(category_id) {
  149. var categories = vm.categories.filter(function(elt, idx) {
  150. return elt._id === category_id;
  151. });
  152. if( categories.length === 0 ) {
  153. return [];
  154. } else {
  155. return categories[0].sub_categories;
  156. }
  157. }
  158. }
  159. })();