import { Injectable } from '@angular/core'; import { Product } from '../core/product'; import { ModifiersComponent } from './modifiers/modifiers.component'; import { MatDialog } from '@angular/material'; import { ModifierCategoryService } from '../modifier-categories/modifier-category.service'; import {Subject} from "rxjs"; import {Modifier} from "../core/modifier"; import {ModifierCategory} from "../core/modifier-category"; import {isNotNullOrUndefined} from "codelyzer/util/isNotNullOrUndefined"; @Injectable() export class BillService { public dataObs = new Subject(); public data = []; constructor( private dialog: MatDialog, private modifierCategoryService: ModifierCategoryService ) { } addProduct(product: Product) { let item = { isKot: false, product: product, productId: product.id, info: `${product.name} (${product.units}) @ ${product.price}`, quantity: 1, 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 (isNotNullOrUndefined(result)) { item.modifiers = result; } }); } addOne(item: any): void { item.quantity += 1; this.dataObs.next(this.data); } quantity(item: any): void { } 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); } }