product-details.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. this.data.getAll('product_attribute_line').then(lines => {
  123. console.log(lines);
  124. }).catch(e => {
  125. console.log(e);
  126. });
  127. }
  128. /**
  129. *
  130. */
  131. private isValidForm() {
  132. if (!this.product.name) {
  133. return false;
  134. }
  135. return true;
  136. }
  137. /**
  138. *
  139. */
  140. showPhotoOptions() {
  141. this.actionSheetCtrl.create({
  142. title: "Tomar una foto",
  143. buttons: [
  144. {
  145. text: "Desde la cámara",
  146. icon: "camera",
  147. handler: () => {
  148. this.takePicture("camera");
  149. }
  150. },
  151. {
  152. text: "Desde la galería",
  153. icon: "images",
  154. handler: () => {
  155. this.takePicture("album");
  156. }
  157. },
  158. {
  159. text: "Cancelar",
  160. role: "cancel"
  161. }
  162. ]
  163. }).present();
  164. }
  165. /**
  166. *
  167. */
  168. takePicture(source: string): void {
  169. this.cameraProvider.getPicture(source).then(i => {
  170. this.product.image_medium = i;
  171. this.product.image_small = i;
  172. }).catch(e => {
  173. console.log(e);
  174. });
  175. }
  176. /**
  177. *
  178. */
  179. addAttribute(): void {
  180. if (!this.product.attribute_line_ids) {
  181. this.product.attribute_line_ids = [];
  182. }
  183. this.product.attribute_line_ids.push({
  184. attribute: 1,
  185. value: 1
  186. });
  187. }
  188. /**
  189. *
  190. */
  191. save(): void {
  192. if (!this.isValidForm()) {
  193. return;
  194. }
  195. this.product.company_id = this.preferencesPreferences.getCompany();
  196. this.data.save('product', this.product).then(result => {
  197. if (this.params.data instanceof Array) {
  198. this.params.data.push(result.products[0]);
  199. }
  200. this.navCtrl.pop(this);
  201. }).catch(e => {
  202. console.log(e);
  203. });
  204. }
  205. }