customer.controller.js 5.9 KB

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