lead.controller.js 6.3 KB

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