opportunity.controller.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. angular.module('odoo')
  2. /**
  3. *
  4. */
  5. .controller('OpportunitiesController', function (
  6. $scope,
  7. $ionicPopup,
  8. $ionicFilterBar,
  9. $ionicActionSheet,
  10. deviceFactory,
  11. crmStagesDataFactory,
  12. opportunitiesDataFactory
  13. ) {
  14. $scope.loading = false;
  15. $scope.selectedIndex = -1;
  16. $scope.search = null;
  17. $scope.stages = [];
  18. $scope.opportunities = [];
  19. $scope.groupedOpportunities = [];
  20. $scope.stage = {
  21. previous: null,
  22. current: null,
  23. next: null
  24. };
  25. $scope.stageToMove = {};
  26. /**
  27. *
  28. */
  29. $scope.$on("$ionicSlides.sliderInitialized", function (event, data) {
  30. $scope.slider = data.slider;
  31. });
  32. /**
  33. *
  34. */
  35. $scope.$on("$ionicSlides.slideChangeStart", function (event, data) {
  36. $scope.loading = true;
  37. });
  38. /**
  39. *
  40. */
  41. $scope.$on("$ionicSlides.slideChangeEnd", function (event, data) {
  42. $scope.loading = false;
  43. $scope.stageChanged(data.slider.activeIndex);
  44. });
  45. /**
  46. *
  47. */
  48. $scope.$on("$ionicView.enter", function () {
  49. $scope.initialize();
  50. });
  51. /**
  52. *
  53. */
  54. $scope.initialize = function () {
  55. $scope.loading = true;
  56. $scope.getStages(function (stages) {
  57. $scope.slider.updateLoop();
  58. $scope.getOpportunities(function (opportunities) {
  59. $scope.loading = false;
  60. $scope.groupOpportunities();
  61. $scope.$apply();
  62. }, function (err) {
  63. $scope.loading = false;
  64. });
  65. }, function (err) {
  66. $scope.loading = false;
  67. deviceFactory.toast('No se ha podido cargar las oportunidades');
  68. });
  69. }
  70. /**
  71. *
  72. */
  73. $scope.getStages = function (success, error) {
  74. crmStagesDataFactory.sync(function (stages) {
  75. $scope.stages = stages;
  76. $scope.stageChanged(0);
  77. success(stages);
  78. }, function (syncErr) {
  79. crmStagesDataFactory.getAll(function (stages) {
  80. $scope.stages = stages;
  81. $scope.stageChanged(0);
  82. success(stages);
  83. }, function (getAllErr) {
  84. error(getAllErr);
  85. });
  86. });
  87. }
  88. /**
  89. *
  90. */
  91. $scope.getOpportunities = function (success, error) {
  92. opportunitiesDataFactory.sync(function (opportunities) {
  93. $scope.opportunities = opportunities;
  94. success(opportunities);
  95. }, function (syncErr) {
  96. opportunitiesDataFactory.getAll(function (opportunities) {
  97. $scope.opportunities = opportunities;
  98. success(opportunities);
  99. }, function (getAllErr) {
  100. error(getAllErr);
  101. });
  102. });
  103. }
  104. /**
  105. *
  106. */
  107. $scope.groupOpportunities = function () {
  108. $scope.groupedOpportunities = $scope.opportunities.filter(function (item) {
  109. return item.stage_id == $scope.stage.current.remote_id;
  110. });
  111. }
  112. /**
  113. *
  114. */
  115. $scope.toggleSearch = function () {
  116. $scope.search = $ionicFilterBar.show({
  117. items: $scope.groupedOpportunities,
  118. update: function (filtered, text) {
  119. $scope.groupedOpportunities = filtered;
  120. }
  121. });
  122. }
  123. /**
  124. * Change the state
  125. */
  126. $scope.changeStage = function (index) {
  127. switch (index) {
  128. case 0:
  129. $scope.slider.slidePrev();
  130. break;
  131. case 1:
  132. $scope.slider.slideTo(0);
  133. break;
  134. case 2:
  135. $scope.slider.slideNext();
  136. break;
  137. }
  138. }
  139. /**
  140. * Change stage name on title
  141. */
  142. $scope.stageChanged = function (index) {
  143. if (!$scope.stages.length) {
  144. return;
  145. }
  146. $scope.stage.previous = index - 1 >= 0 ? $scope.stages[index - 1] : $scope.stages[index];
  147. $scope.stage.current = $scope.stages[index];
  148. $scope.stage.next = index + 1 <= $scope.stages.length ? $scope.stages[index + 1] : $scope.stages[index];
  149. $scope.groupOpportunities();
  150. if (!$scope.$$phase) {
  151. $scope.$apply();
  152. }
  153. }
  154. /**
  155. * Show availables stages to move
  156. */
  157. $scope.showStagesToMove = function () {
  158. $ionicPopup.show({
  159. scope: $scope,
  160. title: 'Mover a',
  161. templateUrl: 'templates/sales/crm-stages.html',
  162. buttons: [
  163. {
  164. text: 'Mover',
  165. type: 'button-positive',
  166. onTap: function (e) {
  167. // console.log(e);
  168. $scope.moveOpportunity();
  169. }
  170. },
  171. {
  172. text: 'Cancelar',
  173. type: 'button-default',
  174. onTap: function (e) {
  175. // e.preventDefault();
  176. }
  177. }
  178. ]
  179. });
  180. }
  181. /**
  182. *
  183. */
  184. $scope.selectStageToMove = function (index) {
  185. $scope.stageToMove = $scope.stages[index];
  186. }
  187. /**
  188. * Move opportunity to stage
  189. */
  190. $scope.moveOpportunity = function () {
  191. console.log($scope.stageToMove);
  192. deviceFactory.toast('Se movió la oportunidad a ' + $scope.stageToMove.name);
  193. }
  194. /**
  195. * Open the actionsheet action options
  196. */
  197. $scope.openOptions = function (index) {
  198. deviceFactory.vibrate();
  199. $scope.selectedIndex = index;
  200. console.log($scope.opportunities[index]);
  201. $ionicActionSheet.show({
  202. titleText: 'Acciones',
  203. buttons: [
  204. {
  205. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  206. },
  207. {
  208. text: '<i class="icon ion-forward positive"></i> Mover a'
  209. }
  210. ],
  211. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  212. cancel: function () {
  213. $scope.customer = {};
  214. $scope.selectedIndex = -1;
  215. },
  216. buttonClicked: function (index) {
  217. $scope.selectedIndex = -1;
  218. switch (index) {
  219. case 0:
  220. $scope.show();
  221. break;
  222. case 1:
  223. $scope.showStagesToMove();
  224. break;
  225. default:
  226. $scope.show();
  227. }
  228. return true;
  229. },
  230. destructiveButtonClicked: function () {
  231. $scope.selectedIndex = -1;
  232. $scope.delete();
  233. return true;
  234. }
  235. });
  236. }
  237. });