customer.controller.js 6.6 KB

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