customer.controller.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. angular.module('odoo')
  2. .controller('CustomersController', function (
  3. $scope,
  4. $ionicModal,
  5. $ionicActionSheet,
  6. $ionicPopup,
  7. customersRemoteFactory,
  8. customersStorageFactory,
  9. sqlFactory,
  10. cameraFactory,
  11. contactFactory,
  12. $ionicPopover
  13. ) {
  14. $scope.loading = false;
  15. $scope.customers = [];
  16. $scope.customer = {};
  17. $ionicPopover.fromTemplateUrl('templates/options.html', {
  18. scope: $scope,
  19. }).then(function(popover) {
  20. $scope.popover = popover;
  21. });
  22. $ionicModal.fromTemplateUrl('templates/sales/customer.html', {
  23. scope: $scope,
  24. animation: 'slide-in-up'
  25. }).then(function(modal) {
  26. $scope.customerModal = modal;
  27. });
  28. $scope.$on('$ionicView.enter', function () {
  29. $scope.fill();
  30. });
  31. $scope.$on('$destroy', function() {
  32. $scope.customerModal.remove();
  33. });
  34. $scope.$on('modal.hidden', function() {
  35. $scope.customer = {};
  36. });
  37. // Fill view list from data readed from local database
  38. $scope.fill = function (refresh = false) {
  39. $scope.loading = !refresh;
  40. customersRemoteFactory.sync(function (customers) {
  41. console.log('Customers sync correctly and receive ' + customers.length + ' items');
  42. $scope.getCustomers(function () {
  43. $scope.$broadcast('scroll.refreshComplete');
  44. $scope.loading = false;
  45. }, function (getCustomersError) {
  46. $scope.$broadcast('scroll.refreshComplete');
  47. $scope.loading = false;
  48. $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
  49. });
  50. }, function (syncErr) {
  51. $scope.getCustomers(function () {
  52. $scope.$broadcast('scroll.refreshComplete');
  53. $scope.loading = false;
  54. }, function (getCustomersError) {
  55. $scope.$broadcast('scroll.refreshComplete');
  56. $scope.loading = false;
  57. $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
  58. });
  59. });
  60. }
  61. // Get filtered customers for correct data visualization
  62. $scope.getCustomers = function (success, error) {
  63. sqlFactory.selectByConstraint('partner', 'customer = 1 AND modified != 2', function (customers) {
  64. $scope.customers = [];
  65. for (var i = 0; i < customers.length; i++) {
  66. $scope.customers.push(customers.item(i));
  67. }
  68. $scope.$apply();
  69. success();
  70. }, function (customerGetByConstraintError) {
  71. error(customerGetByConstraintError);
  72. });
  73. }
  74. // Show action sheet for customer options
  75. $scope.openOptions = function(index) {
  76. if (index == -1) {
  77. $scope.customer = {};
  78. } else {
  79. $scope.customer = $scope.customers[index];
  80. }
  81. console.log('Customer selected => ' + JSON.stringify($scope.customer));
  82. $ionicActionSheet.show({
  83. titleText: 'Acciones',
  84. buttons: [
  85. {
  86. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  87. },
  88. {
  89. text: '<i class="icon ion-archive positive"></i> Agregar a contactos'
  90. }
  91. ],
  92. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  93. cancel: function() {
  94. $scope.customer = {};
  95. console.log('ActionSheet canceled');
  96. },
  97. buttonClicked: function(index) {
  98. switch (index) {
  99. case 0:
  100. $scope.show();
  101. break;
  102. case 1:
  103. $scope.addContact();
  104. break;
  105. default:
  106. $scope.show();
  107. }
  108. return true;
  109. },
  110. destructiveButtonClicked: function() {
  111. $scope.delete();
  112. return true;
  113. }
  114. });
  115. }
  116. // Save a customer data on local database
  117. $scope.save = function() {
  118. customersStorageFactory.save($scope.customer, function (customerId) {
  119. if (!$scope.customer.id) {
  120. $scope.customer.id = customerId;
  121. $scope.customers.push($scope.customer);
  122. }
  123. $scope.customer = {};
  124. $scope.customerModal.hide();
  125. console.log('Customer saved');
  126. }, function (error) {
  127. $ionicPopup.alert({ title: 'No se ha podido guardar el cliente', template: JSON.stringify(error) });
  128. console.log(JSON.stringify(error));
  129. });
  130. }
  131. // Delete a customer from local database
  132. $scope.delete = function () {
  133. $ionicPopup.confirm({
  134. title: 'Confirmar',
  135. template: 'Estás seguro que quieres eliminar este cliente?'
  136. }).then(function (confirmation) {
  137. if(confirmation) {
  138. customersStorageFactory.remove($scope.customer, function (affected) {
  139. if (affected != 0) {
  140. var index = $scope.customers.indexOf($scope.customer);
  141. $scope.customers.splice(index, 1);
  142. $scope.customer = {};
  143. $scope.$apply();
  144. }
  145. }, function (error) {
  146. $ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
  147. console.log(JSON.stringify(error));
  148. });
  149. }
  150. });
  151. }
  152. // Show customer data in modal
  153. $scope.show = function (e) {
  154. // $scope.customerModal.show();
  155. $scope.popover.show(e);
  156. }
  157. // Take a picture from camera for customer profile
  158. $scope.takePicture = function () {
  159. cameraFactory.takePicture(function (imageData) {
  160. $scope.customer.image_small = imageData;
  161. $scope.customer.image_medium = imageData;
  162. }, function (error) {
  163. console.log(error);
  164. });
  165. }
  166. // Add customer data contact to device mobile contacts
  167. $scope.addContact = function () {
  168. contactFactory.save($scope.customer, function (result) {
  169. $ionicPopup.alert({ title: 'Contacto guardado' });
  170. }, function (error) {
  171. console.log(error);
  172. })
  173. }
  174. });