customer.controller.js 6.9 KB

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