lead.controller.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. $scope.$apply();
  71. success();
  72. }, function (sqlFactoryError) {
  73. error(sqlFactoryError);
  74. });
  75. }
  76. $scope.show = function () {
  77. $scope.leadModal.show();
  78. }
  79. $scope.save = function () {
  80. leadsStorageFactory.save($scope.lead, function (leadId) {
  81. if (!$scope.lead.id) {
  82. $scope.lead.id = leadId;
  83. $scope.leads.push($scope.lead);
  84. }
  85. $scope.lead = {};
  86. $scope.leadModal.hide();
  87. console.log('Lead saved');
  88. }, function (error) {
  89. $ionicPopup.alert({ title: 'No se ha podido guardar la iniciativa', template: JSON.stringify(error) });
  90. console.log(JSON.stringify(error));
  91. });
  92. }
  93. $scope.delete = function () {
  94. $ionicPopup.confirm({
  95. title: 'Confirmar',
  96. template: 'Estás seguro que quieres eliminar esta iniciativa?'
  97. }).then(function (confirmation) {
  98. if(confirmation) {
  99. leadsStorageFactory.remove($scope.lead, function (affected) {
  100. if (affected != 0) {
  101. var index = $scope.leads.indexOf($scope.customer);
  102. $scope.leads.splice(index, 1);
  103. $scope.lead = {};
  104. $scope.$apply();
  105. }
  106. }, function (error) {
  107. $ionicPopup.alert({ title: 'No se puedo eliminar la iniciativa', template: JSON.stringify(error) });
  108. console.log(JSON.stringify(error));
  109. });
  110. }
  111. });
  112. }
  113. $scope.convertToOpportunity = function () {
  114. console.log($scope.conversion);
  115. $scope.leadToOpportunityModal.hide();
  116. }
  117. $scope.toggleSearch = function () {
  118. $scope.search = $ionicFilterBar.show({
  119. items: $scope.leads,
  120. update: function (filtered, text) {
  121. $scope.leads = filtered;
  122. }
  123. });
  124. }
  125. $scope.openOptions = function (index) {
  126. if (index == -1) {
  127. $scope.lead = {};
  128. } else {
  129. $scope.lead = $scope.leads[index];
  130. }
  131. console.log('Customer selected => ' + JSON.stringify($scope.lead));
  132. $ionicActionSheet.show({
  133. titleText: 'Acciones',
  134. buttons: [
  135. {
  136. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  137. },
  138. {
  139. text: '<i class="icon ion-wand positive"></i> Convertir a oportunidad'
  140. }
  141. ],
  142. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  143. cancel: function() {
  144. $scope.lead = {};
  145. console.log('ActionSheet canceled');
  146. },
  147. buttonClicked: function(index) {
  148. switch (index) {
  149. case 0:
  150. $scope.show();
  151. break;
  152. case 1:
  153. $scope.leadToOpportunityModal.show();
  154. break;
  155. default:
  156. $scope.show();
  157. }
  158. return true;
  159. },
  160. destructiveButtonClicked: function() {
  161. $scope.delete();
  162. return true;
  163. }
  164. });
  165. }
  166. });