customer.controller.js 5.9 KB

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