customer.controller.js 7.6 KB

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