import { Injectable } from '@angular/core'; import { MatDialog } from '@angular/material'; import { BehaviorSubject } from 'rxjs'; import { Product } from '../core/product'; import { ModifiersComponent } from './modifiers/modifiers.component'; import { ModifierCategoryService } from '../modifier-categories/modifier-category.service'; import { ModifierCategory } from '../core/modifier-category'; import { Bill, Inventory, Kot, PrintType } from './bills/bill'; import {VoucherService} from './bills/voucher.service'; @Injectable() export class BillService { public dataObs; public data; private bill; constructor( private dialog: MatDialog, private ser: VoucherService, private modifierCategoryService: ModifierCategoryService ) { this.data = []; this.dataObs = new BehaviorSubject(this.data); } loadData(bill: Bill): void { this.bill = bill; const view = this.bill.kots.map(k => { return [{ isKot: true, info: `Kot: ${k.code} / ${k.date} (${k.user.name}) ` }, ...k.inventories.map(i => { return { id: i.id, isKot: false, product: i.product, productId: i.product.id, isHappyHour: i.isHappyHour, isPrinted: true, info: `${i.product.name} (${i.product.units}) @ ${i.price}`, quantity: i.quantity, discount: i.discount, taxRate: i.taxRate, tax: i.tax, modifiers: i.modifiers }; })]; }); this.data = view.reduce((a, c) => a.concat(c) , []); this.data.push({isKot: true, newKot: true, info: '== New Kot =='}); this.dataObs.next(this.data); } addProduct(product: Product): void { const old = this.data.find(x => !x.isKot && x.productId === product.id && x.isHappyHour === product.hasHappyHour); if (old !== undefined) { old.quantity += 1; } else { const item = { isKot: false, product: product, productId: product.id, isHappyHour: product.hasHappyHour, info: `${product.name} (${product.units}) @ ${product.price}`, price: product.price, quantity: 1, discount: 0, taxRate: product.saleCategory.tax.rate, tax: product.saleCategory.tax, modifiers: [] }; this.data.push(item); this.modifierCategoryService.listIsActiveOfProduct(product.id).subscribe(result => { if (result.reduce((a: any, c: ModifierCategory) => { return a + c.minimum; }, 0)) { this.showModifier(item); } }); } this.dataObs.next(this.data); } showModifier(item: any): void { // [routerLink]="['/sales', 'modifiers', item.id]" const dialogRef = this.dialog.open(ModifiersComponent, { position: { top: '10vh' }, data: { list: this.modifierCategoryService.listIsActiveOfProduct(item.productId), selected: item.modifiers } }); dialogRef.afterClosed().subscribe(result => { if (result !== undefined) { item.modifiers = result; } }); } addOne(item: any): void { item.quantity += 1; this.dataObs.next(this.data); } quantity(item: any, quantity: number): void { item.quantity = quantity; this.dataObs.next(this.data); } subtractOne(item: any): void { if (item.quantity > 1) { item.quantity -= 1; this.dataObs.next(this.data); } else if (item.quantity === 0) { this.removeItem(item); } } removeItem(item: any): void { this.data.splice(this.data.indexOf(item), 1); this.dataObs.next(this.data); } modifier(item: any): void { this.showModifier(item); } printKot(guest_book_id: string): void { const nk = new Kot({ inventories: this.data.filter(x => !x.isKot && !x.isPrinted).map(y => new Inventory({ product: y.product, quantity: y.quantity, price: y.price, isHappyHour: y.isHappyHour, discount: y.discount, modifiers: y.modifiers, taxRate: y.taxRate, tax: y.tax })) }); let item = JSON.parse(JSON.stringify(this.bill)) item.kots.push(nk); this.ser.saveOrUpdate(item, PrintType.Kot, guest_book_id, true).subscribe(x => console.log(x) ); } printBill(): boolean { return false; } }