opportunity.controller.js 12 KB

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