customer.controller.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. angular.module('odoo')
  2. .controller('CustomersController', function (
  3. $scope,
  4. $ionicModal,
  5. $ionicActionSheet,
  6. $ionicPopup,
  7. customers,
  8. sqlFactory,
  9. customerStorage,
  10. camera,
  11. contact
  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. customers.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 (syncCustomersError) {
  45. console.error(syncCustomersError);
  46. $scope.getCustomers(function () {
  47. $scope.$broadcast('scroll.refreshComplete');
  48. $scope.loading = false;
  49. }, function (getCustomersError) {
  50. $scope.$broadcast('scroll.refreshComplete');
  51. $scope.loading = false;
  52. $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
  53. });
  54. });
  55. }
  56. // Get filtered customers for correct data visualization
  57. $scope.getCustomers = function (success, error) {
  58. sqlFactory.selectByConstraint('partner', 'customer = 1 AND modified != 2', function (customers) {
  59. $scope.customers = [];
  60. for (var i = 0; i < customers.length; i++) {
  61. $scope.customers.push(customers.item(i));
  62. }
  63. $scope.$apply();
  64. success();
  65. }, function (customerGetByConstraintError) {
  66. error(customerGetByConstraintError);
  67. });
  68. }
  69. // Show action sheet for customer options
  70. $scope.openOptions = function(index) {
  71. if (index == -1) {
  72. $scope.customer = {};
  73. } else {
  74. $scope.customer = $scope.customers[index];
  75. }
  76. console.log('Customer selected => ' + JSON.stringify($scope.customer));
  77. $ionicActionSheet.show({
  78. titleText: 'Acciones',
  79. buttons: [
  80. {
  81. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  82. },
  83. {
  84. text: '<i class="icon ion-archive positive"></i> Agregar a contactos'
  85. }
  86. ],
  87. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  88. cancel: function() {
  89. $scope.customer = {};
  90. console.log('ActionSheet canceled');
  91. },
  92. buttonClicked: function(index) {
  93. switch (index) {
  94. case 0:
  95. $scope.show();
  96. break;
  97. case 1:
  98. $scope.addContact();
  99. break;
  100. default:
  101. $scope.show();
  102. }
  103. return true;
  104. },
  105. destructiveButtonClicked: function() {
  106. $scope.delete();
  107. return true;
  108. }
  109. });
  110. }
  111. // Save a customer data on local database
  112. $scope.save = function() {
  113. customerStorage.save($scope.customer, function (customerId) {
  114. if (!$scope.customer.id) {
  115. $scope.customer.id = customerId;
  116. $scope.customers.push($scope.customer);
  117. }
  118. $scope.customer = {};
  119. $scope.customerModal.hide();
  120. console.log('Customer saved');
  121. }, function (error) {
  122. $ionicPopup.alert({ title: 'No se ha podido guardar el cliente', template: JSON.stringify(error) });
  123. console.log(JSON.stringify(error));
  124. });
  125. }
  126. // Delete a customer from local database
  127. $scope.delete = function () {
  128. $ionicPopup.confirm({
  129. title: 'Confirmar',
  130. template: 'Estás seguro que quieres eliminar este cliente?'
  131. }).then(function (confirmation) {
  132. if(confirmation) {
  133. customerStorage.remove($scope.customer, function (affected) {
  134. if (affected != 0) {
  135. var index = $scope.customers.indexOf($scope.customer);
  136. $scope.customers.splice(index, 1);
  137. $scope.customer = {};
  138. $scope.$apply();
  139. }
  140. }, function (error) {
  141. $ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
  142. console.log(JSON.stringify(error));
  143. });
  144. }
  145. });
  146. }
  147. // Show customer data in modal
  148. $scope.show = function () {
  149. $scope.customerModal.show();
  150. }
  151. // Take a picture from camera for customer profile
  152. $scope.takePicture = function () {
  153. camera.takePicture(function (imageData) {
  154. $scope.customer.image_small = imageData;
  155. $scope.customer.image_medium = imageData;
  156. }, function (error) {
  157. console.log(error);
  158. });
  159. }
  160. // Add customer data contact to device mobile contacts
  161. $scope.addContact = function () {
  162. contact.save($scope.customer, function (result) {
  163. $ionicPopup.alert({ title: 'Contacto guardado' });
  164. }, function (error) {
  165. console.log(error);
  166. })
  167. }
  168. });