customer.controller.js 8.1 KB

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