opportunity.controller.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. angular.module('odoo')
  2. /**
  3. *
  4. */
  5. .controller('OpportunitiesController', function (
  6. $scope,
  7. $ionicActionSheet,
  8. $ionicPopup,
  9. crmStagesDataFactory,
  10. opportunitiesDataFactory,
  11. ionicToast
  12. ) {
  13. $scope.loading = false;
  14. $scope.stages = [];
  15. $scope.opportunities = [];
  16. $scope.currentStage = {};
  17. $scope.stageToMove = {};
  18. $scope.selected = -1;
  19. /**
  20. *
  21. */
  22. $scope.$on("$ionicSlides.sliderInitialized", function (event, data) {
  23. $scope.slider = data.slider;
  24. });
  25. /**
  26. *
  27. */
  28. $scope.$on("$ionicSlides.slideChangeEnd", function (event, data) {
  29. $scope.stageChanged(data.slider.activeIndex);
  30. });
  31. /**
  32. *
  33. */
  34. $scope.$on("$ionicView.enter", function () {
  35. $scope.initialize();
  36. });
  37. /**
  38. *
  39. */
  40. $scope.initialize = function () {
  41. $scope.loading = true;
  42. $scope.getStages(function (stages) {
  43. $scope.slider.updateLoop();
  44. $scope.getOpportunities(function (opportunities) {
  45. console.log(opportunities);
  46. $scope.loading = false;
  47. $scope.$apply();
  48. }, function (err) {
  49. $scope.loading = false;
  50. });
  51. }, function (err) {
  52. $scope.loading = false;
  53. });
  54. }
  55. /**
  56. *
  57. */
  58. $scope.getStages = function (success, error) {
  59. crmStagesDataFactory.sync(function (stages) {
  60. $scope.stages = stages;
  61. $scope.stageChanged(0);
  62. success(stages);
  63. }, function (syncErr) {
  64. crmStagesDataFactory.getAll(function (stages) {
  65. $scope.stages = stages;
  66. $scope.stageChanged(0);
  67. success(stages);
  68. }, function (getAllErr) {
  69. error(getAllErr);
  70. });
  71. });
  72. }
  73. /**
  74. *
  75. */
  76. $scope.getOpportunities = function (success, error) {
  77. opportunitiesDataFactory.sync(function (opportunities) {
  78. $scope.opportunities = opportunities;
  79. success(opportunities);
  80. }, function (syncErr) {
  81. opportunitiesDataFactory.getAll(function (opportunities) {
  82. $scope.opportunities = opportunities;
  83. success(opportunities);
  84. }, function (getAllErr) {
  85. error(getAllErr);
  86. });
  87. });
  88. }
  89. /**
  90. * Change the state
  91. */
  92. $scope.changeStage = function (mode) {
  93. $scope.loading = true;
  94. if (mode) {
  95. $scope.slider.slideNext();
  96. } else {
  97. $scope.slider.slidePrev();
  98. }
  99. $scope.loading = false;
  100. }
  101. /**
  102. * Change stage name on title
  103. */
  104. $scope.stageChanged = function (index) {
  105. if (!$scope.stages.length) {
  106. return;
  107. }
  108. $scope.currentStage = $scope.stages[index];
  109. if (!$scope.$$phase) {
  110. $scope.$apply();
  111. }
  112. }
  113. /**
  114. * Show availables stages to move
  115. */
  116. $scope.showStagesToMove = function () {
  117. $ionicPopup.show({
  118. scope: $scope,
  119. title: 'Mover a',
  120. templateUrl: 'templates/sales/crm-stages.html',
  121. buttons: [
  122. {
  123. text: 'Mover',
  124. type: 'button-positive',
  125. onTap: function (e) {
  126. // console.log(e);
  127. $scope.moveOpportunity();
  128. }
  129. },
  130. {
  131. text: 'Cancelar',
  132. type: 'button-default',
  133. onTap: function (e) {
  134. // e.preventDefault();
  135. }
  136. }
  137. ]
  138. });
  139. }
  140. /**
  141. *
  142. */
  143. $scope.selectStageToMove = function (index) {
  144. $scope.stageToMove = $scope.stages[index];
  145. }
  146. /**
  147. * Move opportunity to stage
  148. */
  149. $scope.moveOpportunity = function () {
  150. console.log($scope.stageToMove);
  151. ionicToast.show('Se movió la oportunidad ', 'bottom', false, 1500);
  152. }
  153. /**
  154. * Open the actionsheet action options
  155. */
  156. $scope.openOptions = function (index) {
  157. $scope.selected = index;
  158. $ionicActionSheet.show({
  159. titleText: 'Acciones',
  160. buttons: [
  161. {
  162. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  163. },
  164. {
  165. text: '<i class="icon ion-forward positive"></i> Mover a'
  166. }
  167. ],
  168. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  169. cancel: function () {
  170. $scope.customer = {};
  171. $scope.selected = -1;
  172. },
  173. buttonClicked: function (index) {
  174. switch (index) {
  175. case 0:
  176. $scope.show();
  177. break;
  178. case 1:
  179. $scope.showStagesToMove();
  180. break;
  181. default:
  182. $scope.show();
  183. }
  184. return true;
  185. },
  186. destructiveButtonClicked: function () {
  187. $scope.delete();
  188. return true;
  189. }
  190. });
  191. }
  192. });