barker/bookie/src/app/sales/bill.service.ts
Amritanshu 4513e8b263 Bills initially working just as proof of concept
ng linted
modifier categories list is better at displaying data sanely now
2019-07-11 12:17:41 +05:30

85 lines
2.2 KiB
TypeScript

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<any[]>();
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);
}
}