comanda.component.ts 3.17 KB
import { Component, OnInit } from '@angular/core';
import { IComanda, IComandaDetalle, IComponente } from 'src/app/interfaces/IComanda';
import { ComandaService } from 'src/app/services/comanda/comanda.service';
import * as _ from 'lodash';

@Component({
  selector: 'app-comanda',
  templateUrl: './comanda.component.html',
  styleUrls: ['./comanda.component.scss']
})
export class ComandaComponent implements OnInit {
  comandas: IComanda[] = [];
  cicloTime: any;

  constructor(
    private comandaService: ComandaService,
  ) { }

  ngOnInit() {
    this.getComandas();
    this.timerGetComandas();
  }

  ngOnDestroy() {
    clearInterval(this.cicloTime);
  }

  timerGetComandas() {
    this.cicloTime = setInterval(() => {
      this.getComandas();
    }, 5000);
  }

  getComandas() {
    this.comandaService.getAll()
      .subscribe((resComandas: IComanda[]) => {
        this.addNewComandas(resComandas);
      }, e => console.error(e));
  }

  addNewComandas(resComandas: IComanda[]) {
    for (let j = 0; j < resComandas.length; j++) {
      for (let i = 0; i < this.comandas.length; i++) {
        if (this.comandas[i].id === resComandas[j].id) {
          resComandas.splice(j, 1);
        }
      }
    }
    if (!resComandas.length) return;
    Array.prototype.push.apply(this.comandas, resComandas);
    this.startTimersPedido(resComandas);
  }

  updateComanda(comanda: IComanda, estadoNuevo: number, observacionNueva: string) {
    let data = {
      idComanda: comanda.id,
      estado: estadoNuevo,
      observacion: observacionNueva,
      tiempoEspera: `${comanda.hoursPedido}:${comanda.secondsPedido}:${comanda.secondsPedido}`,
      tiempoElaboracion: `${comanda.hoursElaboracion}:${comanda.secondsElaboracion}:${comanda.secondsElaboracion}`,
    }
    if (data.estado == 3) {
      this.comandaService.imprimirComandaCocina(parseInt(data.idComanda))
        .subscribe(res => { }, err => console.error(err)
        );
    }

    if (estadoNuevo !== 2) comanda.detalles.forEach(d => d.seeDetalle = false);

    this.comandaService.update(data)
      .subscribe((res: any) => {
        if (res.data) {
          comanda.estado = estadoNuevo;
          comanda.observacion = observacionNueva;
          if (estadoNuevo == 2) {
            this.startTimerElaboracion(comanda);
          } else if (comanda.timerElaboracion) {
            clearInterval(comanda.timerElaboracion)
          }
        }
      }, e => console.error(e));
  }

  rellenar(relleno, longitud) {
    relleno = '' + relleno;
    while (relleno.length < longitud) {
      relleno = '0' + relleno;
    }
    return relleno;
  }

  toggleVerComponentes(detalle: IComandaDetalle, comanda: IComanda) {
    detalle.seeDetalle = !detalle.seeDetalle;
  }

  hasTipo(componentes: IComponente[]) {
    return componentes.some(c => c.tipoArticulo === 6);
  }

  //#region TIMERS
  startTimersPedido(comandas) {
    comandas.forEach((comanda: IComanda) => {
      this.comandaService.startTimerComanda(comanda, 'Pedido');
      if (comanda.estado === 2) {
        this.startTimerElaboracion(comanda);
      }
    });
  }

  startTimerElaboracion(comanda: IComanda) {
    this.comandaService.startTimerComanda(comanda, 'Elaboracion');
  }
  //#endregion

}