lead.controller.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. console.log(leads);
  79. $scope.leads = [];
  80. for (var i = 0; i < leads.length; i++) {
  81. $scope.leads.push(leads.item(i));
  82. }
  83. $scope.$apply();
  84. success();
  85. }, function (sqlFactoryError) {
  86. error(sqlFactoryError);
  87. });
  88. }
  89. /**
  90. *
  91. */
  92. $scope.show = function (event) {
  93. $scope.leadModal.show();
  94. }
  95. /**
  96. *
  97. */
  98. $scope.save = function () {
  99. leadsStorageFactory.save($scope.lead, function (leadId) {
  100. if (!$scope.lead.id) {
  101. $scope.lead.id = leadId;
  102. $scope.leads.push($scope.lead);
  103. }
  104. $scope.lead = {};
  105. $scope.leadModal.hide();
  106. console.log('Lead saved');
  107. }, function (error) {
  108. $ionicPopup.alert({ title: 'No se ha podido guardar la iniciativa', template: JSON.stringify(error) });
  109. console.log(JSON.stringify(error));
  110. });
  111. }
  112. /**
  113. *
  114. */
  115. $scope.delete = function () {
  116. $ionicPopup.confirm({
  117. title: 'Confirmar',
  118. template: 'Estás seguro que quieres eliminar esta iniciativa?'
  119. }).then(function (confirmation) {
  120. if (confirmation) {
  121. leadsStorageFactory.remove($scope.lead, function (affected) {
  122. if (affected != 0) {
  123. var index = $scope.leads.indexOf($scope.lead);
  124. $scope.leads.splice(index, 1);
  125. $scope.lead = {};
  126. $scope.$apply();
  127. }
  128. }, function (error) {
  129. $ionicPopup.alert({ title: 'No se puedo eliminar la iniciativa', template: JSON.stringify(error) });
  130. console.log(JSON.stringify(error));
  131. });
  132. }
  133. });
  134. }
  135. /**
  136. *
  137. */
  138. $scope.convertToOpportunity = function () {
  139. opportunitiesStorageFactory.save($scope.lead, function (opportunityId) {
  140. $scope.leadToOpportunityModal.hide();
  141. var index = $scope.leads.indexOf($scope.lead);
  142. $scope.leads.splice(index, 1);
  143. $scope.lead = {};
  144. $scope.$apply();
  145. }, function (saveErr) {
  146. $scope.leadToOpportunityModal.hide();
  147. $ionicPopup.alert({
  148. title: 'No se puedo convertir la iniciativa',
  149. template: JSON.stringify(saveErr)
  150. });
  151. });
  152. }
  153. /**
  154. *
  155. */
  156. $scope.toggleSearch = function () {
  157. $scope.search = $ionicFilterBar.show({
  158. items: $scope.leads,
  159. update: function (filtered, text) {
  160. $scope.leads = filtered;
  161. }
  162. });
  163. }
  164. /**
  165. *
  166. */
  167. $scope.openOptions = function (index) {
  168. $cordovaVibration.vibrate(100);
  169. if (index == -1) {
  170. $scope.lead = {};
  171. } else {
  172. $scope.lead = $scope.leads[index];
  173. }
  174. console.log('Customer selected => ' + JSON.stringify($scope.lead));
  175. $ionicActionSheet.show({
  176. titleText: 'Acciones',
  177. buttons: [
  178. {
  179. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  180. },
  181. {
  182. text: '<i class="icon ion-wand positive"></i> Convertir a oportunidad'
  183. }
  184. ],
  185. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  186. cancel: function () {
  187. $scope.lead = {};
  188. console.log('ActionSheet canceled');
  189. },
  190. buttonClicked: function (index) {
  191. switch (index) {
  192. case 0:
  193. $scope.show();
  194. break;
  195. case 1:
  196. $scope.leadToOpportunityModal.show();
  197. break;
  198. default:
  199. $scope.show();
  200. }
  201. return true;
  202. },
  203. destructiveButtonClicked: function () {
  204. $scope.delete();
  205. return true;
  206. }
  207. });
  208. }
  209. });