customer.controller.js 5.9 KB

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