lead.controller.js 5.7 KB

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