customer.controller.js 8.7 KB

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