customer.controller.js 6.1 KB

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