opportunity.controller.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. angular.module('odoo')
  2. /**
  3. *
  4. */
  5. .controller('OpportunitiesController', function (
  6. $scope,
  7. $ionicPopup,
  8. $ionicModal,
  9. $ionicFilterBar,
  10. $ionicActionSheet,
  11. deviceFactory,
  12. crmStagesDataFactory,
  13. opportunitiesDataFactory
  14. ) {
  15. // =======================================================================================================
  16. $scope.loading = false;
  17. $scope.selectedIndex = -1;
  18. $scope.search = null;
  19. $scope.stages = [];
  20. $scope.stagesToMove = [];
  21. $scope.opportunity = {};
  22. $scope.opportunities = [];
  23. $scope.groupedOpportunities = [];
  24. $scope.stage = {
  25. previous: null,
  26. current: null,
  27. next: null
  28. };
  29. // =======================================================================================================
  30. /**
  31. *
  32. */
  33. $ionicModal.fromTemplateUrl('templates/sales/opportunity.html', {
  34. scope: $scope
  35. }).then(function (modal) {
  36. $scope.modal = modal;
  37. });
  38. /**
  39. *
  40. */
  41. $scope.$on('$ionicSlides.sliderInitialized', function (event, data) {
  42. $scope.slider = data.slider;
  43. });
  44. /**
  45. *
  46. */
  47. $scope.$on('$ionicSlides.slideChangeStart', function (event, data) {
  48. $scope.loading = true;
  49. });
  50. /**
  51. *
  52. */
  53. $scope.$on('$ionicSlides.slideChangeEnd', function (event, data) {
  54. $scope.loading = false;
  55. $scope.stageChanged(data.slider.activeIndex);
  56. });
  57. /**
  58. *
  59. */
  60. $scope.$on('$ionicView.enter', function () {
  61. $scope.initialize();
  62. });
  63. /**
  64. *
  65. */
  66. $scope.$on('$destroy', function () {
  67. $scope.modal.remove();
  68. });
  69. /**
  70. *
  71. */
  72. $scope.initialize = function () {
  73. $scope.loading = true;
  74. $scope.getStages(function (stages) {
  75. $scope.slider.updateLoop();
  76. $scope.getOpportunities(function (opportunities) {
  77. $scope.loading = false;
  78. $scope.groupOpportunities();
  79. $scope.$apply();
  80. }, function (err) {
  81. $scope.loading = false;
  82. });
  83. }, function (err) {
  84. $scope.loading = false;
  85. deviceFactory.toast('No se ha podido cargar las oportunidades');
  86. });
  87. }
  88. /**
  89. *
  90. */
  91. $scope.getStages = function (success, error) {
  92. crmStagesDataFactory.sync(function (stages) {
  93. $scope.stages = stages;
  94. $scope.stageChanged(0);
  95. success(stages);
  96. }, function (syncErr) {
  97. crmStagesDataFactory.getAll(function (stages) {
  98. $scope.stages = stages;
  99. $scope.stageChanged(0);
  100. success(stages);
  101. }, function (getAllErr) {
  102. error(getAllErr);
  103. });
  104. });
  105. }
  106. /**
  107. *
  108. */
  109. $scope.getOpportunities = function (success, error) {
  110. opportunitiesDataFactory.sync(function (opportunities) {
  111. $scope.opportunities = opportunities;
  112. success(opportunities);
  113. }, function (syncErr) {
  114. opportunitiesDataFactory.getAll(function (opportunities) {
  115. $scope.opportunities = opportunities;
  116. success(opportunities);
  117. }, function (getAllErr) {
  118. error(getAllErr);
  119. });
  120. });
  121. }
  122. /**
  123. *
  124. */
  125. $scope.groupOpportunities = function () {
  126. $scope.groupedOpportunities = $scope.opportunities.filter(function (item) {
  127. return item.stage_id == $scope.stage.current.remote_id;
  128. });
  129. }
  130. /**
  131. *
  132. */
  133. $scope.filterStages = function () {
  134. $scope.stagesToMove = $scope.stages.filter(function (item) {
  135. return item.id != $scope.stage.current.id;
  136. });
  137. }
  138. /**
  139. *
  140. */
  141. $scope.toogleNew = function () {
  142. if ($scope.stages.length > 0) {
  143. $scope.opportunity.stage_id = $scope.stages[0].remote_id;
  144. }
  145. $scope.modal.show();
  146. }
  147. /**
  148. *
  149. */
  150. $scope.toggleSearch = function () {
  151. $scope.search = $ionicFilterBar.show({
  152. items: $scope.groupedOpportunities,
  153. update: function (filtered, text) {
  154. $scope.groupedOpportunities = filtered;
  155. }
  156. });
  157. }
  158. /**
  159. *
  160. */
  161. $scope.save = function () {
  162. opportunitiesDataFactory.save($scope.opportunity, function (opportunityId) {
  163. $scope.modal.hide();
  164. console.log($scope.opportunity);
  165. if (!$scope.opportunity.id) {
  166. console.log('Entra aqui');
  167. $scope.opportunity.id = opportunityId;
  168. $scope.opportunities.push($scope.opportunity);
  169. }
  170. $scope.groupOpportunities();
  171. $scope.opportunity = {};
  172. $scope.$apply();
  173. deviceFactory.toast('Nueva opportunidad creada');
  174. }, function (err) {
  175. console.log(err);
  176. deviceFactory.toast('No se ha podido guardar la opportunidad');
  177. });
  178. }
  179. /**
  180. *
  181. */
  182. $scope.delete = function () {
  183. deviceFactory.confirm('Estás seguro que quieres eliminar ésta oportunidad?', 'Confirmar', function (index) {
  184. if (index == 1) {
  185. opportunitiesDataFactory.remove($scope.opportunity, function (affected) {
  186. if (affected != 0) {
  187. var index = $scope.opportunities.indexOf($scope.opportunity);
  188. $scope.opportunities.splice(index, 1);
  189. $scope.opportunity = {};
  190. $scope.groupOpportunities();
  191. $scope.$apply();
  192. }
  193. }, function (err) {
  194. console.log(err);
  195. deviceFactory.toast('No se ha podido eliminar la oportunidad');
  196. });
  197. }
  198. });
  199. }
  200. /**
  201. * Change the state
  202. */
  203. $scope.changeStage = function (index) {
  204. switch (index) {
  205. case 0:
  206. $scope.slider.slidePrev();
  207. break;
  208. case 1:
  209. $scope.slider.slideTo(0);
  210. break;
  211. case 2:
  212. $scope.slider.slideNext();
  213. break;
  214. }
  215. }
  216. /**
  217. * Change stage name on title
  218. */
  219. $scope.stageChanged = function (index) {
  220. if (!$scope.stages.length) {
  221. return;
  222. }
  223. $scope.stage.previous = index - 1 >= 0 ? $scope.stages[index - 1] : $scope.stages[index];
  224. $scope.stage.current = $scope.stages[index];
  225. $scope.stage.next = index + 1 <= $scope.stages.length ? $scope.stages[index + 1] : $scope.stages[index];
  226. $scope.groupOpportunities();
  227. if (!$scope.$$phase) {
  228. $scope.$apply();
  229. }
  230. }
  231. /**
  232. * Show availables stages to move
  233. */
  234. $scope.showStagesToMove = function () {
  235. $scope.filterStages();
  236. $scope.selectStage = $ionicPopup.show({
  237. scope: $scope,
  238. title: 'Mover a',
  239. templateUrl: 'templates/sales/crm-stages.html',
  240. });
  241. }
  242. /**
  243. * Move opportunity to stage
  244. */
  245. $scope.moveToStage = function (index) {
  246. $scope.selectStage.close();
  247. var opportunityToMove = $scope.groupedOpportunities[$scope.selectedIndex];
  248. var indexToUpdate = $scope.opportunities.indexOf(opportunityToMove);
  249. var stageToMove = $scope.stagesToMove[index];
  250. $scope.selectedIndex = -1;
  251. $scope.loading = true;
  252. opportunitiesDataFactory.changeStage(opportunityToMove, stageToMove.remote_id, function (opportunityId) {
  253. $scope.opportunities[indexToUpdate] = opportunityToMove;
  254. $scope.groupOpportunities();
  255. $scope.$apply();
  256. $scope.loading = false;
  257. deviceFactory.toast('Se movió la oportunidad a ' + $scope.stagesToMove[index].name);
  258. }, function (err) {
  259. $scope.loading = false;
  260. deviceFactory.toast('No se ha podido mover la oportunidad');
  261. });
  262. }
  263. /**
  264. * Open the actionsheet action options
  265. */
  266. $scope.openOptions = function (index) {
  267. deviceFactory.vibrate();
  268. $scope.selectedIndex = index;
  269. if (index == -1) {
  270. $scope.opportunity = {};
  271. } else {
  272. $scope.opportunity = $scope.groupedOpportunities[index];
  273. }
  274. console.log($scope.opportunity);
  275. $ionicActionSheet.show({
  276. titleText: 'Acciones',
  277. buttons: [
  278. {
  279. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  280. },
  281. {
  282. text: '<i class="icon ion-forward positive"></i> Mover a'
  283. }
  284. ],
  285. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  286. cancel: function () {
  287. $scope.customer = {};
  288. $scope.selectedIndex = -1;
  289. },
  290. buttonClicked: function (index) {
  291. switch (index) {
  292. case 0:
  293. $scope.selectedIndex = -1;
  294. $scope.toogleNew();
  295. break;
  296. case 1:
  297. $scope.showStagesToMove();
  298. break;
  299. }
  300. return true;
  301. },
  302. destructiveButtonClicked: function () {
  303. $scope.selectedIndex = -1;
  304. $scope.delete();
  305. return true;
  306. }
  307. });
  308. }
  309. });