lead.controller.js 4.3 KB

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