lead.controller.js 6.3 KB

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