lead.controller.js 7.8 KB

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