lead.controller.js 5.3 KB

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