customer.controller.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. angular.module('odoo')
  2. .controller('CustomersController', function (
  3. $scope,
  4. $log,
  5. $ionicLoading,
  6. $ionicModal,
  7. $ionicActionSheet,
  8. $ionicPopup,
  9. customers,
  10. odoo,
  11. storage,
  12. camera,
  13. contact
  14. ) {
  15. // Objects
  16. $scope.customer = {};
  17. $scope.customers = [];
  18. $ionicModal.fromTemplateUrl('templates/sale/customer.html', {
  19. scope: $scope,
  20. animation: 'slide-in-up'
  21. }).then(function(modal) {
  22. $scope.customerModal = modal;
  23. });
  24. $scope.$on('$ionicView.enter', function () {
  25. $scope.fill();
  26. });
  27. $scope.$on('$destroy', function() {
  28. $scope.customerModal.remove();
  29. });
  30. $scope.$on('modal.hidden', function() {
  31. $scope.customer = {};
  32. });
  33. // Fill view list from data readed from local database
  34. $scope.fill = function () {
  35. $ionicLoading.show();
  36. customers.sync(function (customers) {
  37. console.log('Customers sync correctly and receive ' + customers.length + ' items');
  38. $scope.getCustomers(function () {
  39. $scope.$broadcast('scroll.refreshComplete');
  40. $ionicLoading.hide();
  41. }, function (getCustomersError) {
  42. $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
  43. });
  44. }, function (syncCustomersError) {
  45. console.error(syncCustomersError);
  46. $scope.getCustomers(function () {
  47. $scope.$broadcast('scroll.refreshComplete');
  48. $ionicLoading.hide();
  49. }, function (getCustomersError) {
  50. $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
  51. });
  52. });
  53. }
  54. // Get filtered customers for correct data visualization
  55. $scope.getCustomers = function (success, error) {
  56. storage.getByConstraint('partner', 'customer = 1 AND modified != 2', function (customers) {
  57. console.log(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. $log.info('Customer selected => ' + JSON.stringify($scope.customer));
  76. $ionicActionSheet.show({
  77. titleText: 'Acciones',
  78. buttons: [
  79. {
  80. text: '<i class="icon ion-eye positive"></i> Detallar'
  81. },
  82. {
  83. text: '<i class="icon ion-archive positive"></i> Agregar a contactos'
  84. }
  85. ],
  86. destructiveText: 'Eliminar',
  87. cancel: function() {
  88. $scope.customer = {};
  89. $log.info('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. storage.saveCustomer($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. $log.info('Customer saved');
  120. }, function (error) {
  121. $ionicPopup.alert({ title: 'No se ha podido guardar el cliente', template: JSON.stringify(error) });
  122. $log.error(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. storage.deleteCustomer($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. $log.error(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. camera.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. contact.save($scope.customer, function (result) {
  162. $ionicPopup.alert({ title: 'Contacto guardado' });
  163. }, function (error) {
  164. console.log(error);
  165. })
  166. }
  167. });