customer.controller.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. console.log(customers);
  110. $scope.customers = [];
  111. for (var i = 0; i < customers.length; i++) {
  112. $scope.customers.push(customers.item(i));
  113. }
  114. $scope.$apply();
  115. success();
  116. }, function (customerGetByConstraintError) {
  117. error(customerGetByConstraintError);
  118. });
  119. }
  120. /**
  121. *
  122. */
  123. $scope.openOptions = function (index, e) {
  124. deviceFactory.vibrate();
  125. $scope.selectedIndex = index;
  126. if (index == -1) {
  127. $scope.customer = {};
  128. } else {
  129. $scope.customer = $scope.customers[index];
  130. }
  131. console.log($scope.customer);
  132. $ionicActionSheet.show({
  133. titleText: 'Acciones',
  134. buttons: [
  135. {
  136. text: '<i class="icon ion-arrow-expand positive"></i> Abrir'
  137. },
  138. {
  139. text: '<i class="icon ion-archive positive"></i> Agregar a contactos'
  140. },
  141. {
  142. text: '<i class="icon ion-navigate positive"></i> Ir a la ubicación'
  143. }
  144. ],
  145. destructiveText: '<i class="icon ion-trash-a assertive"></i> Eliminar',
  146. cancel: function () {
  147. $scope.customer = {};
  148. $scope.selectedIndex = -1;
  149. },
  150. buttonClicked: function (index) {
  151. $scope.selectedIndex = -1;
  152. switch (index) {
  153. case 0:
  154. $scope.toogleNew();
  155. break;
  156. case 1:
  157. $scope.addContact();
  158. break;
  159. case 2:
  160. $scope.navigate();
  161. break;
  162. default:
  163. $scope.toogleNew();
  164. }
  165. return true;
  166. },
  167. destructiveButtonClicked: function () {
  168. $scope.selectedIndex = -1;
  169. $scope.delete();
  170. return true;
  171. }
  172. });
  173. }
  174. /**
  175. *
  176. */
  177. $scope.save = function () {
  178. customersStorageFactory.save($scope.customer, function (customerId) {
  179. if (!$scope.customer.id) {
  180. $scope.customer.id = customerId;
  181. $scope.customers.push($scope.customer);
  182. }
  183. $scope.customer = {};
  184. $scope.modal.hide();
  185. console.log('Customer saved');
  186. }, function (error) {
  187. console.log(error);
  188. deviceFactory.toast('No se ha podido guardar el cliente');
  189. });
  190. }
  191. /**
  192. *
  193. */
  194. $scope.delete = function () {
  195. deviceFactory.confirm('Estás seguro que quieres eliminar este cliente?', 'Confirmar', function (index) {
  196. if (index == 1) {
  197. customersStorageFactory.remove($scope.customer, function (affected) {
  198. if (affected != 0) {
  199. var index = $scope.customers.indexOf($scope.customer);
  200. $scope.customers.splice(index, 1);
  201. $scope.customer = {};
  202. $scope.$apply();
  203. }
  204. }, function (error) {
  205. console.log(error);
  206. deviceFactory.toast('No se ha podido eliminar el cliente');
  207. });
  208. }
  209. });
  210. }
  211. /**
  212. *
  213. */
  214. $scope.takePicture = function () {
  215. deviceFactory.takePicture(function (imageData) {
  216. $scope.customer.image_small = imageData;
  217. $scope.customer.image_medium = imageData;
  218. }, function (error) {
  219. console.log(error);
  220. deviceFactory.toast('No se ha podido tomar la foto');
  221. });
  222. }
  223. /**
  224. *
  225. */
  226. $scope.addContact = function () {
  227. deviceFactory.saveContact($scope.customer, function (result) {
  228. console.log(result);
  229. }, function (error) {
  230. console.log(error);
  231. deviceFactory.toast('Este cliente no tiene contactos que guardar');
  232. })
  233. }
  234. /**
  235. *
  236. */
  237. $scope.navigate = function () {
  238. deviceFactory.navigate([
  239. $scope.customer.partner_latitude,
  240. $scope.customer.partner_longitude
  241. ], function (message) {
  242. console.log(message);
  243. }, function (err) {
  244. console.log(err);
  245. deviceFactory.toast('Este cliente no tiene ubicación que navegar');
  246. })
  247. }
  248. });