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