configuracion.component.ts 2.51 KB
import { Component, OnInit } from '@angular/core';
import { BsModalRef } from 'ngx-bootstrap';
import { PuntoVentaService } from 'src/app/services/punto-venta.service';
import { PuntoVenta } from 'src/app/wrappers/puntoVenta';
import { ImpresoraService } from 'src/app/services/impresora.service';
import { Impresora } from 'src/app/wrappers/impresora';
import { FormGroup, FormControl } from '@angular/forms';
import { Router } from '@angular/router';

@Component({
  selector: 'app-configuracion',
  templateUrl: './configuracion.component.html',
  styleUrls: ['./configuracion.component.scss']
})
export class ConfiguracionComponent implements OnInit {

  puntosVenta: PuntoVenta[] = [];
  impresoras: Impresora[] = [];
  form: FormGroup;
  reiniciar: boolean = false;

  constructor(
    private activeModal: BsModalRef,
    private puntoVentaService: PuntoVentaService,
    private impresoraService: ImpresoraService,
    private router: Router
  ) { }

  ngOnInit() {

    this.form = new FormGroup({
      usePlanillaPropia: new FormControl(false, []),
      puntoVenta: new FormControl(null, []),
      impresora: new FormControl(null, []),
    })

    this.puntoVentaService.getAll()
      .subscribe((res: PuntoVenta[]) => {

        this.puntosVenta = res;
        this.setPuntoVenta();
      }, console.error);

    this.impresoraService.getAll()
      .subscribe((res: Impresora[]) => {

        this.impresoras = res;
        this.setImpresora();
      }, console.error);

  }

  close() {

    this.activeModal.hide();
    if (this.reiniciar) {
      this.router.navigate(['inicio']);
    }
  }

  acept() {
    this.reiniciar = !localStorage.getItem('impresoraPVE') ? true : false;

    let auxPuntoVenta = this.puntosVenta.find(p => p.ID === this.form.get('puntoVenta').value);
    let auxImpresora = this.impresoras.find(p => p.PVE === this.form.get('impresora').value);
    localStorage.setItem('pve', auxPuntoVenta ? auxPuntoVenta.ID.toString() : null);
    localStorage.setItem('impresoraPVE', auxImpresora ? auxImpresora.PVE.toString() : null);
    this.close();
  }

  setPuntoVenta() {

    let pve = parseInt(localStorage.getItem('pve'));
    let auxPuntoVenta = this.puntosVenta.find(x => x.ID === pve);
    this.form.get('puntoVenta').setValue(auxPuntoVenta ? auxPuntoVenta.ID : null);
  }

  setImpresora() {

    let impresoraPVE = parseInt(localStorage.getItem('impresoraPVE'));
    let auxImpresora = this.impresoras.find(x => x.PVE === impresoraPVE);
    this.form.get('impresora').setValue(auxImpresora ? auxImpresora.PVE : null);
  }

}