controllers.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. angular.module('odoo.controllers', ['odoo.factories', 'ngCordova'])
  2. .controller('AppController', function ($scope) {
  3. console.log("I'm AppController");
  4. })
  5. .controller('MainController', function ($scope) {
  6. console.log("I'm MainController");
  7. })
  8. .controller('VentasController', function ($scope) {
  9. })
  10. .controller('CustomersController', function ($scope, $log, $ionicLoading, $ionicModal, $ionicActionSheet, $ionicPopup, $cordovaContacts, $cordovaCamera, sync, odoo, storage) {
  11. // Objects
  12. $scope.customer = {};
  13. $scope.customers = [];
  14. $ionicModal.fromTemplateUrl('templates/customer.html', {
  15. scope: $scope,
  16. animation: 'slide-in-up'
  17. }).then(function(modal) {
  18. $scope.customerModal = modal;
  19. });
  20. $scope.$on('$ionicView.enter', function () {
  21. $scope.fill();
  22. });
  23. $scope.$on('$destroy', function() {
  24. $scope.customerModal.remove();
  25. });
  26. $scope.$on('modal.hidden', function() {
  27. $scope.customer = {};
  28. });
  29. // Fill view list
  30. $scope.fill = function () {
  31. $ionicLoading.show();
  32. sync.syncCustomers(function (customers) {
  33. console.log('Customers sync correctly and receive ' + customers.length + ' items');
  34. $scope.getCustomers(function () {
  35. $scope.$broadcast('scroll.refreshComplete');
  36. $ionicLoading.hide();
  37. }, function (getCustomersError) {
  38. $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
  39. });
  40. }, function (syncCustomersError) {
  41. console.error(syncCustomersError);
  42. $scope.getCustomers(function () {
  43. $scope.$broadcast('scroll.refreshComplete');
  44. $ionicLoading.hide();
  45. }, function (getCustomersError) {
  46. $ionicPopup.alert({ title: 'No se pudo obtener los clientes' });
  47. });
  48. });
  49. }
  50. $scope.getCustomers = function (success, error) {
  51. storage.getByConstraint('partner', 'customer = 1', function (customers) {
  52. console.log(customers);
  53. $scope.customers = [];
  54. for (var i = 0; i < customers.length; i++) {
  55. $scope.customers.push(customers.item(i));
  56. }
  57. $scope.$apply();
  58. success();
  59. }, function (customerGetByConstraintError) {
  60. error(customerGetByConstraintError);
  61. });
  62. }
  63. $scope.openOptions = function(index) {
  64. if (index == -1) {
  65. $scope.customer = {};
  66. } else {
  67. $scope.customer = $scope.customers[index];
  68. }
  69. $log.info('Customer selected => ' + JSON.stringify($scope.customer));
  70. $ionicActionSheet.show({
  71. titleText: 'Acciones',
  72. buttons: [
  73. {
  74. text: '<i class="icon ion-eye positive"></i> Detallar'
  75. },
  76. {
  77. text: '<i class="icon ion-archive positive"></i> Agregar a contactos'
  78. }
  79. ],
  80. destructiveText: 'Eliminar',
  81. cancel: function() {
  82. $scope.customer = {};
  83. $log.info('ActionSheet canceled');
  84. },
  85. buttonClicked: function(index) {
  86. switch (index) {
  87. case 0:
  88. $scope.show();
  89. break;
  90. case 1:
  91. $scope.addContact();
  92. break;
  93. default:
  94. $scope.show();
  95. }
  96. return true;
  97. },
  98. destructiveButtonClicked: function() {
  99. $scope.delete();
  100. return true;
  101. }
  102. });
  103. }
  104. $scope.save = function() {
  105. storage.saveCustomer($scope.customer, function (customerId) {
  106. if (!$scope.customer.id) {
  107. $scope.customer.id = customerId;
  108. $scope.customers.push($scope.customer);
  109. }
  110. $scope.customer = {};
  111. $scope.customerModal.hide();
  112. $log.info('Customer saved');
  113. }, function (error) {
  114. $ionicPopup.alert({ title: 'No se ha podido guardar el cliente', template: JSON.stringify(error) });
  115. $log.error(JSON.stringify(error));
  116. });
  117. }
  118. $scope.takePicture = function () {
  119. var options = {
  120. quality: 75,
  121. destinationType: Camera.DestinationType.DATA_URL,
  122. sourceType: Camera.PictureSourceType.CAMERA,
  123. allowEdit: true,
  124. encodingType: Camera.EncodingType.JPEG,
  125. targetWidth: 300,
  126. targetHeight: 300,
  127. popoverOptions: CameraPopoverOptions,
  128. saveToPhotoAlbum: false,
  129. correctOrientation:true
  130. };
  131. $cordovaCamera.getPicture(options).then(function(imageData) {
  132. $scope.customer.image_small = imageData;
  133. $scope.customer.image_medium = imageData;
  134. }, function(err) {
  135. $log.error(JSON.stringify(err));
  136. });
  137. }
  138. $scope.delete = function () {
  139. $ionicPopup.confirm({
  140. title: 'Confirmar',
  141. template: 'Estás seguro que quieres eliminar este cliente?'
  142. }).then(function (confirmation) {
  143. if(confirmation) {
  144. storage.deleteCustomer($scope.customer, function (affected) {
  145. if (affected != 0) {
  146. var index = $scope.customers.indexOf($scope.customer);
  147. $scope.customers.splice(index, 1);
  148. $scope.customer = {};
  149. $scope.$apply();
  150. }
  151. }, function (error) {
  152. $ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
  153. $log.error(JSON.stringify(error));
  154. });
  155. }
  156. });
  157. }
  158. $scope.show = function () {
  159. $scope.customerModal.show();
  160. }
  161. $scope.addContact = function () {
  162. if(!$scope.customer.mobile && !$scope.customer.phone && !$scope.customer.email) {
  163. $ionicPopup.alert({ title: 'No se puede guardar el contacto', template: 'El cliente no tiene registrado un teléfono, celular y email'});
  164. return;
  165. }
  166. var contact = {
  167. name: {
  168. givenName: $scope.customer.name,
  169. familyName: '',
  170. formatted: ''
  171. },
  172. nickname: '',
  173. phoneNumbers: [
  174. {
  175. value: $scope.customer.phone,
  176. type: 'phone'
  177. },
  178. {
  179. value: $scope.customer.mobile,
  180. type: 'mobile'
  181. }
  182. ],
  183. emails: [
  184. {
  185. value: $scope.customer.email,
  186. type: 'home'
  187. }
  188. ],
  189. addresses: [
  190. {
  191. type: 'home',
  192. formatted: '',
  193. streetAddress: $scope.customer.street,
  194. locality: $scope.customer.city,
  195. region: '',
  196. postalCode: '',
  197. country: 'Paraguay'
  198. }
  199. ],
  200. ims: null,
  201. organizations: null,
  202. birthday: null,
  203. note: null,
  204. photos: null,
  205. categories: null,
  206. urls: null
  207. };
  208. $cordovaContacts.save(contact).then(function (result) {
  209. $ionicPopup.alert({ title: 'Contacto creado con éxito' });
  210. }, function (err) {
  211. $ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
  212. $log.error(JSON.stringify(err));
  213. });
  214. }
  215. })
  216. .controller('ConfigurationController', function($scope, $state, $ionicLoading, $ionicPopup, odoo, storage) {
  217. $scope.config = { host: '192.168.88.116', port: 8069, database: 'odoo', username: 'admin', password: 'admin' }
  218. // Apply configuration for app
  219. $scope.configure = function () {
  220. $ionicLoading.show();
  221. storage.count('user', function (total) {
  222. if (total == 0) {
  223. odoo.auth($scope.config, function (user) {
  224. storage.saveUser([user.id, $scope.config.host, $scope.config.port, $scope.config.database, user.username, user.password], function (newId) {
  225. $ionicLoading.hide();
  226. $state.go('app.main');
  227. console.log(newId);
  228. }, function (error) {
  229. $ionicLoading.hide();
  230. console.log(error);
  231. });
  232. }, function (error) {
  233. $ionicLoading.hide();
  234. var message = 'Configuración no válida';
  235. if (angular.isObject(error)) {
  236. message = 'No se pudo establecer una llamada al servidor';
  237. }
  238. $ionicPopup.alert({ title: 'Ha fallado el inicio de sesión', template: message });
  239. });
  240. } else {
  241. $ionicLoading.hide();
  242. $state.go('app.main');
  243. }
  244. }, function (error) {
  245. $ionicLoading.hide();
  246. console.log(error);
  247. });
  248. }
  249. });