account.controller.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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) {
  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) {
  28. return elt._id === category_id;
  29. })[0];
  30. if( !category ) {
  31. return '';
  32. }
  33. var res = category.sub_categories.filter( function(elt) {
  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', '$rootScope', '$routeParams', 'FlashService', 'AccountsService', 'AccountService'];
  44. function AccountController($scope, $rootScope, $routeParams, FlashService, AccountsService, 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.accounts = undefined;
  58. vm.account = undefined;
  59. vm.entries = [];
  60. vm.balance = undefined;
  61. vm.categories = [];
  62. vm.sub_categories = [];
  63. vm.create = create;
  64. vm.drop = drop;
  65. vm.edit = edit;
  66. vm.updateSubCategory = updateSubCategory;
  67. vm.updateSubCategoryEditForm = updateSubCategoryEditForm;
  68. vm.disabledSubCategories = false;
  69. vm.edit_sub_categories = [];
  70. $rootScope.current_account = $routeParams.account_id;
  71. (function init() {
  72. vm.dataLoading = true;
  73. AccountService.details($routeParams.account_id)
  74. .then(function(response) {
  75. if( response.success ) {
  76. vm.account = response.account;
  77. vm.categories = angular.copy(vm.account.categories);
  78. vm.categories.unshift({_id: '', label: ''});
  79. } else {
  80. FlashService.error(response.message);
  81. }
  82. vm.dataLoading = false;
  83. });
  84. AccountService.list($routeParams.account_id)
  85. .then(function(response) {
  86. if( response.success ) {
  87. vm.balance = response.data.balance;
  88. vm.entries = response.data.entries;
  89. } else {
  90. FlashService.error(response.message);
  91. }
  92. });
  93. AccountsService.list()
  94. .then(function(response) {
  95. if( response.success ) {
  96. $rootScope.accounts = response.accounts
  97. } else {
  98. FlashService.error(response.message);
  99. }
  100. });
  101. })();
  102. function create() {
  103. vm.dataLoading = true;
  104. AccountService.create(vm.account, vm.entry)
  105. .then( function(response) {
  106. if( response.success) {
  107. vm.balance = response.data.balance;
  108. vm.entries = response.data.entries;
  109. } else {
  110. FlashService.error(response.message);
  111. }
  112. vm.dataLoading = false;
  113. });
  114. vm.entry = angular.copy({});
  115. $scope.form.$setPristine();
  116. };
  117. function drop(entry) {
  118. vm.dataLoading = true;
  119. AccountService.drop(vm.account, entry)
  120. .then(function(response) {
  121. if( response.success ) {
  122. vm.balance = response.data.balance;
  123. vm.entries = response.data.entries;
  124. } else {
  125. FlashService.error( response.message );
  126. }
  127. vm.dataLoading = false;
  128. });
  129. };
  130. function edit(altered, origin) {
  131. vm.dataLoading = true;
  132. return AccountService.edit(vm.account, origin._id, altered)
  133. .then( function(response) {
  134. vm.dataLoading = false;
  135. if( response.success ) {
  136. vm.balance = response.data.balance;
  137. var index = vm.entries.map(function (item) {
  138. return item._id;
  139. }).indexOf(origin._id);
  140. vm.entries[index] = response.data.entries[index];
  141. } else {
  142. var index = vm.entries.map(function (item) {
  143. return item._id;
  144. }).indexOf(origin._id);
  145. vm.entries[index] = origin;
  146. FlashService.error( response.message );
  147. return false;
  148. }
  149. })
  150. };
  151. function updateSubCategory() {
  152. vm.sub_categories = getSubCategories(vm.entry.category);
  153. };
  154. function updateSubCategoryEditForm(category_id) {
  155. vm.edit_sub_categories = getSubCategories(category_id);
  156. vm.disabledSubCategories = !vm.edit_sub_categories || vm.edit_sub_categories.length === 0;
  157. };
  158. function getSubCategories(category_id) {
  159. var categories = vm.categories.filter(function(elt, idx) {
  160. return elt._id === category_id;
  161. });
  162. if( categories.length === 0 ) {
  163. return [];
  164. } else {
  165. return categories[0].sub_categories;
  166. }
  167. }
  168. }
  169. })();