lead.controller.js 7.2 KB

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