customer.controller.js 7.2 KB

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