lead.controller.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. angular.module('odoo')
  2. /**
  3. *
  4. */
  5. .controller('LeadsController', function (
  6. $q,
  7. $scope,
  8. $ionicModal,
  9. $ionicActionSheet,
  10. $ionicFilterBar,
  11. leadsRemoteFactory,
  12. leadsStorageFactory,
  13. opportunitiesStorageFactory,
  14. customersStorageFactory,
  15. sqlFactory,
  16. deviceFactory
  17. ) {
  18. $scope.loading = false;
  19. $scope.selectedIndex = -1;
  20. $scope.leads = [];
  21. $scope.lead = {};
  22. $scope.conversion = {
  23. action: 'convert',
  24. customer: 'create'
  25. };
  26. $scope.search = null;
  27. $ionicModal.fromTemplateUrl('templates/sales/lead.html', {
  28. scope: $scope
  29. }).then(function (modal) {
  30. $scope.leadModal = modal;
  31. });
  32. $ionicModal.fromTemplateUrl('templates/sales/leadToOpportunity.html', {
  33. scope: $scope
  34. }).then(function (modal) {
  35. $scope.leadToOpportunityModal = modal;
  36. });
  37. /**
  38. *
  39. */
  40. $scope.$on('$ionicView.enter', function () {
  41. $scope.fill(false);
  42. });
  43. /**
  44. *
  45. */
  46. $scope.$on('$destroy', function () {
  47. $scope.leadModal.remove();
  48. $scope.leadToOpportunityModal.remove();
  49. });
  50. /**
  51. *
  52. */
  53. $scope.$on('modal.hidden', function () {
  54. $scope.lead = {};
  55. });
  56. /**
  57. *
  58. */
  59. $scope.$on('device.shaked', function () {
  60. if (!$scope.leadModal.isShown()) {
  61. $scope.fill(false);
  62. }
  63. });
  64. /**
  65. *
  66. */
  67. $scope.fill = function (refresh) {
  68. $scope.loading = !refresh;
  69. leadsRemoteFactory.sync(function (leads) {
  70. console.log('Leads sync correctly and receive ' + leads.length + ' items');
  71. $scope.getLeads(function () {
  72. $scope.$broadcast('scroll.refreshComplete');
  73. $scope.loading = false;
  74. }, function (getLeadsErr) {
  75. $scope.$broadcast('scroll.refreshComplete');
  76. $scope.loading = false;
  77. deviceFactory.toast('No se pudo obtener las iniciativas');
  78. });
  79. }, function (syncErr) {
  80. $scope.getLeads(function () {
  81. $scope.$broadcast('scroll.refreshComplete');
  82. $scope.loading = false;
  83. }, function (getLeadsErr) {
  84. $scope.$broadcast('scroll.refreshComplete');
  85. $scope.loading = false;
  86. deviceFactory.toast('No se pudo obtener las iniciativas');
  87. });
  88. });
  89. }
  90. /**
  91. *
  92. */
  93. $scope.getLeads = function (success, error) {
  94. sqlFactory.selectByConstraint('crm_lead', "type = 'lead' AND modified != 2", function (leads) {
  95. $scope.leads = [];
  96. for (var i = 0; i < leads.length; i++) {
  97. $scope.leads.push(leads.item(i));
  98. }
  99. $scope.$apply();
  100. success();
  101. }, function (sqlFactoryError) {
  102. error(sqlFactoryError);
  103. });
  104. }
  105. /**
  106. *
  107. */
  108. $scope.toogleNew = function () {
  109. $scope.leadModal.show();
  110. }
  111. /**
  112. *
  113. */
  114. $scope.getCustomersSuggestions = function (query) {
  115. var defer = $q.defer();
  116. customersStorageFactory.getAll(function (customers) {
  117. defer.resolve(customers.filter(function (item) {
  118. return item.name.toLowerCase().indexOf(query) != -1 ? item : null;
  119. }));
  120. }, function (err) {
  121. defer.reject([]);
  122. });
  123. return defer.promise;
  124. }
  125. /**
  126. *
  127. */
  128. $scope.selectCustomer = function (callback) {
  129. $scope.lead.contact_name = callback.item.name;
  130. }
  131. /**
  132. *
  133. */
  134. $scope.deselectCustomer = function (callback) {
  135. $scope.opportunity.contact_name = null;
  136. }
  137. /**
  138. *
  139. */
  140. $scope.toggleSearch = function () {
  141. $scope.search = $ionicFilterBar.show({
  142. items: $scope.leads,
  143. update: function (filtered, text) {
  144. $scope.leads = filtered;
  145. }
  146. });
  147. }
  148. /**
  149. *
  150. */
  151. $scope.save = function () {
  152. leadsStorageFactory.save($scope.lead, function (leadId) {
  153. if (!$scope.lead.id) {
  154. $scope.lead.id = leadId;
  155. $scope.leads.push($scope.lead);
  156. }
  157. $scope.lead = {};
  158. $scope.leadModal.hide();
  159. console.log('Lead saved');
  160. }, function (error) {
  161. console.log(error);
  162. deviceFactory.toast('No se ha podido guardar la iniciativa');
  163. });
  164. }
  165. /**
  166. *
  167. */
  168. $scope.delete = function () {
  169. deviceFactory.confirm('Estás seguro que quieres eliminar esta iniciativa?', 'Confirmar', function (index) {
  170. if (index == 1) {
  171. leadsStorageFactory.remove($scope.lead, function (affected) {
  172. if (affected != 0) {
  173. var index = $scope.leads.indexOf($scope.lead);
  174. $scope.leads.splice(index, 1);
  175. $scope.lead = {};
  176. $scope.$apply();
  177. }
  178. }, function (error) {
  179. console.log(error);
  180. deviceFactory.toast('No se ha podido eliminar la iniciativa');
  181. });
  182. }
  183. });
  184. }
  185. /**
  186. *
  187. */
  188. $scope.convertToOpportunity = function () {
  189. opportunitiesStorageFactory.save($scope.lead, function (opportunityId) {
  190. // $scope.leadToOpportunityModal.hide();
  191. var index = $scope.leads.indexOf($scope.lead);
  192. $scope.leads.splice(index, 1);
  193. $scope.lead = {};
  194. $scope.$apply();
  195. deviceFactory.toast('Se convirtió la iniciativa');
  196. }, function (saveErr) {
  197. console.log(saveErr);
  198. // $scope.leadToOpportunityModal.hide();
  199. deviceFactory.toast('No se pudo convertir la iniciativa');
  200. });
  201. }
  202. /**
  203. *
  204. */
  205. $scope.openOptions = function (index) {
  206. deviceFactory.vibrate();
  207. $scope.selectedIndex = index;
  208. if (index == -1) {
  209. $scope.lead = {};
  210. } else {
  211. $scope.lead = $scope.leads[index];
  212. }
  213. console.log($scope.lead);
  214. $ionicActionSheet.show({
  215. titleText: 'Acciones',
  216. buttons: [
  217. {
  218. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  219. },
  220. {
  221. text: '<i class="icon ion-wand positive"></i> Convertir a oportunidad'
  222. }
  223. ],
  224. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  225. cancel: function () {
  226. $scope.lead = {};
  227. $scope.selectedIndex = -1;
  228. },
  229. buttonClicked: function (index) {
  230. $scope.selectedIndex = -1;
  231. switch (index) {
  232. case 0:
  233. $scope.toogleNew();
  234. break;
  235. case 1:
  236. // $scope.leadToOpportunityModal.show();
  237. $scope.convertToOpportunity();
  238. break;
  239. default:
  240. $scope.toogleNew();
  241. }
  242. return true;
  243. },
  244. destructiveButtonClicked: function () {
  245. $scope.selectedIndex = -1;
  246. $scope.delete();
  247. return true;
  248. }
  249. });
  250. }
  251. });