controllers.js 9.2 KB

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