123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227 |
- import { Component } from '@angular/core';
- import { NavController, NavParams, ActionSheetController, ToastController } from 'ionic-angular';
- import { Shake, BarcodeScanner } from 'ionic-native';
- // import { Product } from '../../models/product'
- import { DataProvider } from '../../providers/data-provider'
- import { CameraProvider } from '../../providers/camera-provider';
- import { PreferencesProvider } from '../../providers/preferences-provider';
- @Component({
- selector: 'page-product-details',
- providers: [CameraProvider],
- templateUrl: 'product-details.html'
- })
- export class ProductDetailsPage {
- product = {
- company_id: 0,
- default_code: null,
- description: null,
- ean13: null,
- image_medium: null,
- image_small: null,
- name: null,
- list_price: 1,
- purchase_ok: true,
- qty_available: 0,
- rental: false,
- sale_ok: true,
- standard_price: 0,
- type: "product",
- website_published: false,
- remote_id: 0,
- doc_state: "created",
- attribute_line_ids: []
- }
- attributes = [
- {
- id: 1,
- name: "Talle",
- },
- {
- id: 2,
- name: "Color",
- },
- {
- id: 3,
- name: "Género",
- },
- {
- id: 4,
- name: "Marca",
- }
- ];
- values = [
- {
- id: 1,
- name: "36",
- attr_id: 1
- },
- {
- id: 2,
- name: "40",
- attr_id: 1
- },
- {
- id: 3,
- name: "Azul Prelavado",
- attr_id: 2
- },
- {
- id: 4,
- name: "Femenino",
- attr_id: 3
- },
- {
- id: 5,
- name: "RI19",
- attr_id: 4
- }
- ];
- view: string = "information";
- watcher: any;
- constructor(
- public navCtrl: NavController,
- public params: NavParams,
- public data: DataProvider,
- public toastCtrl: ToastController,
- public actionSheetCtrl: ActionSheetController,
- public cameraProvider: CameraProvider,
- public preferencesPreferences: PreferencesProvider
- ) { }
- /**
- *
- */
- ionViewDidLoad() {
- this.initialize();
- this.watcher = Shake.startWatch(40).subscribe(() => {
- BarcodeScanner.scan().then(bar => {
- this.product.ean13 = bar.cancelled ? null : bar.text;
- }).catch(e => {
- this.toastCtrl.create({
- message: 'No se ha podido leer el código',
- duration: 3000
- }).present();
- });
- });
- }
- /**
- *
- */
- ionViewDidLeave() {
- if (this.watcher) {
- this.watcher.unsubscribe();
- }
- }
- /**
- *
- */
- private initialize() {
- if (!(this.params.data instanceof Array)) {
- this.product = this.params.data;
- this.product.doc_state = "updated";
- }
- this.data.getAll('product_attribute_line').then(lines => {
- console.log(lines);
- }).catch(e => {
- console.log(e);
- });
- }
- /**
- *
- */
- private isValidForm() {
- if (!this.product.name) {
- return false;
- }
- return true;
- }
- /**
- *
- */
- showPhotoOptions() {
- this.actionSheetCtrl.create({
- title: "Tomar una foto",
- buttons: [
- {
- text: "Desde la cámara",
- icon: "camera",
- handler: () => {
- this.takePicture("camera");
- }
- },
- {
- text: "Desde la galería",
- icon: "images",
- handler: () => {
- this.takePicture("album");
- }
- },
- {
- text: "Cancelar",
- role: "cancel"
- }
- ]
- }).present();
- }
- /**
- *
- */
- takePicture(source: string): void {
- this.cameraProvider.getPicture(source).then(i => {
- this.product.image_medium = i;
- this.product.image_small = i;
- }).catch(e => {
- console.log(e);
- });
- }
- /**
- *
- */
- addAttribute(): void {
- if (!this.product.attribute_line_ids) {
- this.product.attribute_line_ids = [];
- }
-
- this.product.attribute_line_ids.push({
- attribute: 1,
- value: 1
- });
- }
- /**
- *
- */
- save(): void {
- if (!this.isValidForm()) {
- return;
- }
- this.product.company_id = this.preferencesPreferences.getCompany();
-
- this.data.save('product', this.product).then(result => {
- if (this.params.data instanceof Array) {
- this.params.data.push(result.products[0]);
- }
-
- this.navCtrl.pop(this);
- }).catch(e => {
- console.log(e);
- });
-
- }
- }
|