product-details.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { Component } from '@angular/core';
  2. import { NavController, NavParams, ActionSheetController } from 'ionic-angular';
  3. // import { Product } from '../../models/product'
  4. import { DataProvider } from '../../providers/data-provider'
  5. import { CameraProvider } from '../../providers/camera-provider';
  6. import { PreferencesProvider } from '../../providers/preferences-provider';
  7. @Component({
  8. selector: 'page-product-details',
  9. providers: [CameraProvider],
  10. templateUrl: 'product-details.html'
  11. })
  12. export class ProductDetailsPage {
  13. product = {
  14. company_id: 0,
  15. default_code: false,
  16. description: false,
  17. ean13: false,
  18. image_medium: null,
  19. image_small: null,
  20. name: null,
  21. list_price: 1,
  22. purchase_ok: true,
  23. qty_available: 0,
  24. rental: false,
  25. sale_ok: true,
  26. standard_price: 0,
  27. type: "product",
  28. remote_id: 0,
  29. doc_state: "created"
  30. }
  31. constructor(
  32. public navCtrl: NavController,
  33. public params: NavParams,
  34. public data: DataProvider,
  35. public actionSheetCtrl: ActionSheetController,
  36. private cameraProvider: CameraProvider,
  37. private preferencesPreferences: PreferencesProvider
  38. ) {
  39. this.initialize();
  40. }
  41. /**
  42. *
  43. */
  44. private initialize() {
  45. if (!(this.params.data instanceof Array)) {
  46. this.product = this.params.data;
  47. this.product.doc_state = "updated";
  48. }
  49. }
  50. /**
  51. *
  52. */
  53. private isValidForm() {
  54. if (!this.product.name) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. /**
  60. *
  61. */
  62. showPhotoOptions() {
  63. this.actionSheetCtrl.create({
  64. title: "Tomar una foto",
  65. buttons: [
  66. {
  67. text: "Desde la cámara",
  68. icon: "camera",
  69. handler: () => {
  70. this.takePicture("camera");
  71. }
  72. },
  73. {
  74. text: "Desde la galería",
  75. icon: "images",
  76. handler: () => {
  77. this.takePicture("album");
  78. }
  79. },
  80. {
  81. text: "Cancelar",
  82. role: "cancel"
  83. }
  84. ]
  85. }).present();
  86. }
  87. /**
  88. *
  89. */
  90. takePicture(source: string): void {
  91. this.cameraProvider.getPicture(source).then(i => {
  92. this.product.image_medium = i;
  93. this.product.image_small = i;
  94. }).catch(e => {
  95. console.log(e);
  96. });
  97. }
  98. /**
  99. *
  100. */
  101. save(): void {
  102. if (!this.isValidForm()) {
  103. return;
  104. }
  105. this.product.company_id = this.preferencesPreferences.getCompany();
  106. this.data.save('product', this.product).then(result => {
  107. if (this.params.data instanceof Array) {
  108. this.params.data.push(result.products[0]);
  109. }
  110. this.navCtrl.pop(this);
  111. }).catch(e => {
  112. console.log(e);
  113. });
  114. }
  115. }