controllers.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. // Fill view list
  24. $scope.fill = function () {
  25. // sync.syncCustomers(function (customers) {
  26. // console.log(customers);
  27. // }, function (error) {
  28. // console.log(error);
  29. // });
  30. storage.getByConstraint('partner', 'customer = 1', function (customers) {
  31. $scope.customers = [];
  32. for (var i = 0; i < customers.length; i++) {
  33. $scope.customers.push(customers.item(i));
  34. }
  35. $scope.$apply();
  36. $scope.$broadcast('scroll.refreshComplete');
  37. }, function (error) {
  38. $log.error(error);
  39. });
  40. // $ionicLoading.show();
  41. //
  42. // storage.get('user', function (users) {
  43. // if (users.length == 1) {
  44. // var userConfig = users.item(0);
  45. //
  46. // odoo.fetch('res.partner', [[['customer', '=', true]]], userConfig, function (customers) {
  47. // $ionicLoading.hide();
  48. // $scope.customers = customers;
  49. // $scope.$broadcast('scroll.refreshComplete');
  50. // }, function (error) {
  51. // $ionicLoading.hide();
  52. // console.error(error);
  53. //
  54. // var message = 'Configuración no válida';
  55. // if (angular.isObject(error)) {
  56. // message = 'Credenciales no válidas';
  57. // }
  58. //
  59. // $ionicPopup.alert({ title: 'No se pudo establecer una conexión al servidor', template: message });
  60. // });
  61. // } else {
  62. // $ionicLoading.hide();
  63. // $ionicPopup.alert({ title: 'Nada que hacer' });
  64. // }
  65. // }, function (error) {
  66. // $ionicLoading.hide();
  67. // console.error(error);
  68. // $ionicPopup.alert({ title: 'Atención', template: 'No se pudo ejecutar la instrucción SQL' });
  69. // });
  70. }
  71. $scope.openOptions = function(index) {
  72. if (index == -1) {
  73. $scope.customer = {};
  74. } else {
  75. $scope.customer = $scope.customers[index];
  76. }
  77. $log.info('Customer selected => ' + JSON.stringify($scope.customer));
  78. $ionicActionSheet.show({
  79. titleText: 'Acciones',
  80. buttons: [
  81. {
  82. text: '<i class="icon ion-eye positive"></i> Detallar'
  83. },
  84. {
  85. text: '<i class="icon ion-archive positive"></i> Agregar a contactos'
  86. }
  87. ],
  88. destructiveText: 'Eliminar',
  89. cancel: function() {
  90. $scope.customer = {};
  91. $log.info('ActionSheet canceled');
  92. },
  93. buttonClicked: function(index) {
  94. switch (index) {
  95. case 0:
  96. $scope.show();
  97. break;
  98. case 1:
  99. $scope.addContact();
  100. break;
  101. default:
  102. $scope.show();
  103. }
  104. return true;
  105. },
  106. destructiveButtonClicked: function() {
  107. $scope.delete();
  108. return true;
  109. }
  110. });
  111. }
  112. $scope.save = function() {
  113. var data = [0, // remote_id
  114. $scope.customer.name,
  115. $scope.customer.city,
  116. $scope.customer.mobile,
  117. $scope.customer.phone,
  118. $scope.customer.fax,
  119. $scope.customer.email,
  120. $scope.customer.street,
  121. $scope.customer.street2,
  122. $scope.customer.image_medium,
  123. $scope.customer.image_small,
  124. $scope.customer.comment,
  125. 1, // customer
  126. 0, // employee
  127. 0, // is_company
  128. 0, // debit
  129. 0, // debit_limit
  130. 0, // opportunity_count
  131. 0, // contracts_count
  132. 0, // journal_item_count
  133. 0, // meeting_count
  134. 0, // phonecall_count
  135. 0, // sale_order_count
  136. 0]; // total_invoiced
  137. storage.saveCustomer(data, function (customerId) {
  138. $scope.customer.id = customerId;
  139. $scope.customers.push($scope.customer);
  140. $scope.customer = {};
  141. $scope.customerModal.hide();
  142. $log.info('Customer saved');
  143. }, function (error) {
  144. $ionicPopup.alert({ title: 'No se puedo guardar el cliente', template: JSON.stringify(error) });
  145. $log.error(JSON.stringify(error));
  146. });
  147. }
  148. $scope.takePicture = function () {
  149. var options = {
  150. quality: 75,
  151. destinationType: Camera.DestinationType.DATA_URL,
  152. sourceType: Camera.PictureSourceType.CAMERA,
  153. allowEdit: false,
  154. encodingType: Camera.EncodingType.JPEG,
  155. targetWidth: 300,
  156. targetHeight: 300,
  157. popoverOptions: CameraPopoverOptions,
  158. saveToPhotoAlbum: false,
  159. correctOrientation:true
  160. };
  161. $cordovaCamera.getPicture(options).then(function(imageData) {
  162. $scope.customer.image_small = imageData;
  163. $scope.customer.image_medium = imageData;
  164. }, function(err) {
  165. $log.error(JSON.stringify(err));
  166. });
  167. }
  168. $scope.delete = function () {
  169. $ionicPopup.confirm({
  170. title: 'Confirmar',
  171. template: 'Estás seguro que quieres eliminar este cliente?'
  172. }).then(function (confirmation) {
  173. if(confirmation) {
  174. storage.deleteCustomer($scope.customer.id, function (affected) {
  175. if (affected != 0) {
  176. var index = $scope.customers.indexOf($scope.customer);
  177. $scope.customers.splice(index, 1);
  178. $scope.customer = {};
  179. $scope.$apply();
  180. }
  181. }, function (error) {
  182. $ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
  183. $log.error(JSON.stringify(error));
  184. });
  185. }
  186. });
  187. }
  188. $scope.show = function () {
  189. $scope.customerModal.show();
  190. }
  191. $scope.addContact = function () {
  192. var contact = {
  193. name: {
  194. givenName: $scope.customer.name,
  195. familyName: '',
  196. formatted: ''
  197. },
  198. nickname: '',
  199. phoneNumbers: [
  200. {
  201. value: $scope.customer.phone,
  202. type: 'phone'
  203. },
  204. {
  205. value: $scope.customer.mobile,
  206. type: 'mobile'
  207. }
  208. ],
  209. emails: [
  210. {
  211. value: $scope.customer.email,
  212. type: 'home'
  213. }
  214. ],
  215. addresses: [
  216. {
  217. type: 'home',
  218. formatted: '',
  219. streetAddress: $scope.customer.street,
  220. locality: $scope.customer.city,
  221. region: '',
  222. postalCode: '',
  223. country: 'Paraguay'
  224. }
  225. ],
  226. ims: null,
  227. organizations: null,
  228. birthday: null,
  229. note: null,
  230. photos: null,
  231. categories: null,
  232. urls: null
  233. };
  234. $cordovaContacts.save(contact).then(function (result) {
  235. $ionicPopup.alert({ title: 'Contacto creado con éxito' });
  236. }, function (err) {
  237. $ionicPopup.alert({ title: 'No se puedo eliminar el cliente', template: JSON.stringify(error) });
  238. $log.error(JSON.stringify(err));
  239. });
  240. }
  241. })
  242. .controller('ConfigurationController', function($scope, $state, $ionicLoading, $ionicPopup, odoo, storage) {
  243. $scope.config = { host: '192.168.88.116', port: 8069, database: 'odoo', username: 'admin', password: 'admin' }
  244. // Apply configuration for app
  245. $scope.configure = function () {
  246. $ionicLoading.show();
  247. storage.count('user', function (total) {
  248. if (total == 0) {
  249. odoo.auth($scope.config, function (user) {
  250. storage.saveUser([user.id, $scope.config.host, $scope.config.port, $scope.config.database, user.username, user.password], function (newId) {
  251. $ionicLoading.hide();
  252. $state.go('app.main');
  253. console.log(newId);
  254. }, function (error) {
  255. $ionicLoading.hide();
  256. console.log(error);
  257. });
  258. }, function (error) {
  259. $ionicLoading.hide();
  260. var message = 'Configuración no válida';
  261. if (angular.isObject(error)) {
  262. message = 'No se pudo establecer una llamada al servidor';
  263. }
  264. $ionicPopup.alert({ title: 'Ha fallado el inicio de sesión', template: message });
  265. });
  266. } else {
  267. $ionicLoading.hide();
  268. $state.go('app.main');
  269. }
  270. }, function (error) {
  271. $ionicLoading.hide();
  272. console.log(error);
  273. });
  274. }
  275. });