opportunity.controller.js 6.1 KB

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