product-details.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. import { Component } from '@angular/core';
  2. import { NavController, NavParams, ActionSheetController, ToastController } from 'ionic-angular';
  3. import { Shake, BarcodeScanner } from 'ionic-native';
  4. // import { Product } from '../../models/product'
  5. import { DataProvider } from '../../providers/data-provider'
  6. import { CameraProvider } from '../../providers/camera-provider';
  7. import { PreferencesProvider } from '../../providers/preferences-provider';
  8. @Component({
  9. selector: 'page-product-details',
  10. providers: [CameraProvider],
  11. templateUrl: 'product-details.html'
  12. })
  13. export class ProductDetailsPage {
  14. product = {
  15. company_id: 0,
  16. default_code: null,
  17. description: null,
  18. ean13: null,
  19. image_medium: null,
  20. image_small: null,
  21. name: null,
  22. list_price: 1,
  23. purchase_ok: true,
  24. qty_available: 0,
  25. rental: false,
  26. sale_ok: true,
  27. standard_price: 0,
  28. type: "product",
  29. website_published: false,
  30. remote_id: 0,
  31. doc_state: "created",
  32. attribute_line_ids: []
  33. }
  34. attributes = [
  35. {
  36. id: 1,
  37. name: "Talle",
  38. },
  39. {
  40. id: 2,
  41. name: "Color",
  42. },
  43. {
  44. id: 3,
  45. name: "Género",
  46. },
  47. {
  48. id: 4,
  49. name: "Marca",
  50. }
  51. ];
  52. values = [
  53. {
  54. id: 1,
  55. name: "36",
  56. attr_id: 1
  57. },
  58. {
  59. id: 2,
  60. name: "40",
  61. attr_id: 1
  62. },
  63. {
  64. id: 3,
  65. name: "Azul Prelavado",
  66. attr_id: 2
  67. },
  68. {
  69. id: 4,
  70. name: "Femenino",
  71. attr_id: 3
  72. },
  73. {
  74. id: 5,
  75. name: "RI19",
  76. attr_id: 4
  77. }
  78. ];
  79. view: string = "information";
  80. watcher: any;
  81. constructor(
  82. public navCtrl: NavController,
  83. public params: NavParams,
  84. public data: DataProvider,
  85. public toastCtrl: ToastController,
  86. public actionSheetCtrl: ActionSheetController,
  87. public cameraProvider: CameraProvider,
  88. public preferencesPreferences: PreferencesProvider
  89. ) { }
  90. /**
  91. *
  92. */
  93. ionViewDidLoad() {
  94. this.initialize();
  95. this.watcher = Shake.startWatch(40).subscribe(() => {
  96. BarcodeScanner.scan().then(bar => {
  97. this.product.ean13 = bar.cancelled ? null : bar.text;
  98. }).catch(e => {
  99. this.toastCtrl.create({
  100. message: 'No se ha podido leer el código',
  101. duration: 3000
  102. }).present();
  103. });
  104. });
  105. }
  106. /**
  107. *
  108. */
  109. ionViewDidLeave() {
  110. if (this.watcher) {
  111. this.watcher.unsubscribe();
  112. }
  113. }
  114. /**
  115. *
  116. */
  117. private initialize() {
  118. if (!(this.params.data instanceof Array)) {
  119. this.product = this.params.data;
  120. this.product.doc_state = "updated";
  121. }
  122. }
  123. /**
  124. *
  125. */
  126. private isValidForm() {
  127. if (!this.product.name) {
  128. return false;
  129. }
  130. return true;
  131. }
  132. /**
  133. *
  134. */
  135. showPhotoOptions() {
  136. this.actionSheetCtrl.create({
  137. title: "Tomar una foto",
  138. buttons: [
  139. {
  140. text: "Desde la cámara",
  141. icon: "camera",
  142. handler: () => {
  143. this.takePicture("camera");
  144. }
  145. },
  146. {
  147. text: "Desde la galería",
  148. icon: "images",
  149. handler: () => {
  150. this.takePicture("album");
  151. }
  152. },
  153. {
  154. text: "Cancelar",
  155. role: "cancel"
  156. }
  157. ]
  158. }).present();
  159. }
  160. /**
  161. *
  162. */
  163. takePicture(source: string): void {
  164. this.cameraProvider.getPicture(source).then(i => {
  165. this.product.image_medium = i;
  166. this.product.image_small = i;
  167. }).catch(e => {
  168. console.log(e);
  169. });
  170. }
  171. /**
  172. *
  173. */
  174. addAttribute(): void {
  175. if (!this.product.attribute_line_ids) {
  176. this.product.attribute_line_ids = [];
  177. }
  178. this.product.attribute_line_ids.push({
  179. attribute: 1,
  180. value: 1
  181. });
  182. }
  183. /**
  184. *
  185. */
  186. save(): void {
  187. if (!this.isValidForm()) {
  188. return;
  189. }
  190. this.product.company_id = this.preferencesPreferences.getCompany();
  191. this.data.save('product', this.product).then(result => {
  192. if (this.params.data instanceof Array) {
  193. this.params.data.push(result.products[0]);
  194. }
  195. this.navCtrl.pop(this);
  196. }).catch(e => {
  197. console.log(e);
  198. });
  199. }
  200. }