2019-07-11 06:47:41 +00:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-07-12 07:06:38 +00:00
|
|
|
import { MatDialog } from '@angular/material';
|
2019-07-13 16:02:18 +00:00
|
|
|
import { BehaviorSubject } from 'rxjs';
|
2019-08-08 08:01:30 +00:00
|
|
|
import { Observable } from 'rxjs/internal/Observable';
|
2019-08-09 09:25:38 +00:00
|
|
|
import * as math from 'mathjs';
|
2019-07-11 06:47:41 +00:00
|
|
|
import { Product } from '../core/product';
|
|
|
|
import { ModifiersComponent } from './modifiers/modifiers.component';
|
|
|
|
import { ModifierCategoryService } from '../modifier-categories/modifier-category.service';
|
2019-07-13 16:02:18 +00:00
|
|
|
import { ModifierCategory } from '../core/modifier-category';
|
|
|
|
import { Bill, Inventory, Kot, PrintType } from './bills/bill';
|
2019-08-08 08:01:30 +00:00
|
|
|
import { VoucherService } from './bills/voucher.service';
|
2019-08-10 20:07:14 +00:00
|
|
|
import { ToasterService } from '../core/toaster.service';
|
|
|
|
import { Table } from '../core/table';
|
2019-08-17 19:35:59 +00:00
|
|
|
import { SelectionModel } from "@angular/cdk/collections";
|
2019-07-11 06:47:41 +00:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class BillService {
|
2019-07-12 07:06:38 +00:00
|
|
|
public dataObs;
|
2019-08-17 19:35:59 +00:00
|
|
|
public data: any[];
|
2019-07-13 16:02:18 +00:00
|
|
|
private bill;
|
2019-08-10 20:07:14 +00:00
|
|
|
public netAmount: BehaviorSubject<number>;
|
|
|
|
public discountAmount: BehaviorSubject<number>;
|
|
|
|
public taxAmount: BehaviorSubject<number>;
|
|
|
|
public amount: BehaviorSubject<number>;
|
2019-08-17 19:35:59 +00:00
|
|
|
public selection = new SelectionModel<any>(true, []);
|
2019-07-12 07:06:38 +00:00
|
|
|
|
2019-07-11 06:47:41 +00:00
|
|
|
constructor(
|
2019-07-12 07:06:38 +00:00
|
|
|
private dialog: MatDialog,
|
2019-08-08 08:01:30 +00:00
|
|
|
private toaster: ToasterService,
|
2019-07-13 16:02:18 +00:00
|
|
|
private ser: VoucherService,
|
2019-07-11 06:47:41 +00:00
|
|
|
private modifierCategoryService: ModifierCategoryService
|
2019-07-12 07:06:38 +00:00
|
|
|
) {
|
|
|
|
this.data = [];
|
|
|
|
this.dataObs = new BehaviorSubject<any[]>(this.data);
|
2019-08-10 20:07:14 +00:00
|
|
|
this.netAmount = new BehaviorSubject(0);
|
|
|
|
this.discountAmount = new BehaviorSubject(0);
|
|
|
|
this.taxAmount = new BehaviorSubject(0);
|
|
|
|
this.amount = new BehaviorSubject(0);
|
2019-07-12 07:06:38 +00:00
|
|
|
}
|
2019-07-11 06:47:41 +00:00
|
|
|
|
2019-07-13 16:02:18 +00:00
|
|
|
loadData(bill: Bill): void {
|
|
|
|
this.bill = bill;
|
|
|
|
const view = this.bill.kots.map(k => {
|
|
|
|
return [{
|
2019-08-17 19:35:59 +00:00
|
|
|
id: k.id,
|
2019-07-13 16:02:18 +00:00
|
|
|
isKot: true,
|
2019-08-10 05:00:27 +00:00
|
|
|
oldKot: true,
|
2019-07-13 16:02:18 +00:00
|
|
|
info: `Kot: ${k.code} / ${k.date} (${k.user.name}) `
|
|
|
|
}, ...k.inventories.map(i => {
|
|
|
|
return {
|
|
|
|
id: i.id,
|
2019-08-17 19:35:59 +00:00
|
|
|
kotId: k.id,
|
2019-07-13 16:02:18 +00:00
|
|
|
isKot: false,
|
|
|
|
product: i.product,
|
|
|
|
productId: i.product.id,
|
|
|
|
isHappyHour: i.isHappyHour,
|
|
|
|
isPrinted: true,
|
2019-08-09 09:25:38 +00:00
|
|
|
info: `${i.product.name} (${i.product.units}) @ ${i.price} - ${math.round(i.discount * 100, 2)}%`,
|
2019-08-08 08:01:30 +00:00
|
|
|
price: i.price,
|
2019-07-13 16:02:18 +00:00
|
|
|
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 =='});
|
2019-07-12 07:06:38 +00:00
|
|
|
this.dataObs.next(this.data);
|
2019-08-10 20:07:14 +00:00
|
|
|
this.updateAmounts();
|
2019-07-12 07:06:38 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
addProduct(product: Product): void {
|
2019-08-08 11:05:03 +00:00
|
|
|
const old = this.data.find(
|
|
|
|
x => !x.isKot && !x.id && x.productId === product.id && x.isHappyHour === product.hasHappyHour
|
|
|
|
);
|
2019-07-12 07:06:38 +00:00
|
|
|
if (old !== undefined) {
|
|
|
|
old.quantity += 1;
|
|
|
|
} else {
|
2019-07-13 16:02:18 +00:00
|
|
|
const item = {
|
2019-07-12 07:06:38 +00:00
|
|
|
isKot: false,
|
|
|
|
product: product,
|
|
|
|
productId: product.id,
|
|
|
|
isHappyHour: product.hasHappyHour,
|
2019-08-08 08:01:30 +00:00
|
|
|
info: `${product.name} (${product.units}) @ ${product.price} - ${0}%`,
|
2019-07-13 16:02:18 +00:00
|
|
|
price: product.price,
|
2019-07-12 07:06:38 +00:00
|
|
|
quantity: 1,
|
2019-07-13 16:02:18 +00:00
|
|
|
discount: 0,
|
2019-08-10 20:07:14 +00:00
|
|
|
taxRate: product.tax.rate,
|
|
|
|
tax: product.tax,
|
2019-07-12 07:06:38 +00:00
|
|
|
modifiers: []
|
|
|
|
};
|
|
|
|
this.data.push(item);
|
|
|
|
this.modifierCategoryService.listIsActiveOfProduct(product.id).subscribe(result => {
|
|
|
|
if (result.reduce((a: any, c: ModifierCategory) => {
|
2019-07-13 16:02:18 +00:00
|
|
|
return a + c.minimum;
|
2019-07-12 07:06:38 +00:00
|
|
|
}, 0)) {
|
|
|
|
this.showModifier(item);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2019-07-11 06:47:41 +00:00
|
|
|
this.dataObs.next(this.data);
|
2019-08-10 20:07:14 +00:00
|
|
|
this.updateAmounts();
|
2019-07-11 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 => {
|
2019-07-12 07:06:38 +00:00
|
|
|
if (result !== undefined) {
|
2019-07-11 06:47:41 +00:00
|
|
|
item.modifiers = result;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
addOne(item: any): void {
|
|
|
|
item.quantity += 1;
|
|
|
|
this.dataObs.next(this.data);
|
2019-08-10 20:07:14 +00:00
|
|
|
this.updateAmounts();
|
2019-07-11 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
2019-07-13 16:02:18 +00:00
|
|
|
quantity(item: any, quantity: number): void {
|
|
|
|
item.quantity = quantity;
|
|
|
|
this.dataObs.next(this.data);
|
2019-08-10 20:07:14 +00:00
|
|
|
this.updateAmounts();
|
2019-07-11 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
subtractOne(item: any): void {
|
|
|
|
if (item.quantity > 1) {
|
|
|
|
item.quantity -= 1;
|
|
|
|
this.dataObs.next(this.data);
|
2019-08-10 20:07:14 +00:00
|
|
|
this.updateAmounts();
|
2019-07-11 06:47:41 +00:00
|
|
|
} 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);
|
2019-08-10 20:07:14 +00:00
|
|
|
this.updateAmounts();
|
2019-07-11 06:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
modifier(item: any): void {
|
|
|
|
this.showModifier(item);
|
|
|
|
}
|
|
|
|
|
2019-08-10 20:07:14 +00:00
|
|
|
discount(discounts: {id: string, name: string, discount: number}[]): void {
|
|
|
|
this.data.forEach(x => {
|
2019-08-08 08:01:30 +00:00
|
|
|
if (!x.isKot) {
|
|
|
|
x.discount = discounts.find(d => d.id === x.product.saleCategory.id).discount / 100;
|
2019-08-09 09:25:38 +00:00
|
|
|
x.info = `${x.product.name} (${x.product.units}) @ ${x.price} - ${math.round(x.discount * 100, 2)}%`;
|
2019-08-08 08:01:30 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
this.dataObs.next(this.data);
|
2019-08-10 20:07:14 +00:00
|
|
|
this.updateAmounts();
|
2019-08-08 08:01:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private getKot(): Kot {
|
|
|
|
return new Kot({
|
2019-07-13 16:02:18 +00:00
|
|
|
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
|
|
|
|
}))
|
|
|
|
});
|
2019-08-08 08:01:30 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 19:35:59 +00:00
|
|
|
printKot(guest_book_id: string): Observable<boolean> {
|
2019-08-10 20:07:14 +00:00
|
|
|
const item = JSON.parse(JSON.stringify(this.bill));
|
|
|
|
const newKot = this.getKot();
|
2019-08-08 08:01:30 +00:00
|
|
|
if (newKot.inventories.length == 0) {
|
|
|
|
this.toaster.show('Error', 'Cannot print a blank KOT\nPlease add some products!');
|
|
|
|
}
|
|
|
|
item.kots.push(newKot);
|
2019-08-10 20:07:14 +00:00
|
|
|
return this.ser.saveOrUpdate(item, PrintType.Kot, guest_book_id, true);
|
2019-07-13 16:02:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-17 19:35:59 +00:00
|
|
|
printBill(guest_book_id: string, printType: PrintType): Observable<boolean> {
|
2019-08-10 20:07:14 +00:00
|
|
|
const item = JSON.parse(JSON.stringify(this.bill));
|
2019-08-08 08:01:30 +00:00
|
|
|
item.kots.forEach(k => {
|
|
|
|
k.inventories.forEach(i => {
|
|
|
|
i.discount = this.data.find(x => !x.isKot && x.id === i.id).discount;
|
2019-08-10 20:07:14 +00:00
|
|
|
});
|
2019-08-08 08:01:30 +00:00
|
|
|
});
|
|
|
|
item.kots.push(this.getKot());
|
2019-08-10 20:07:14 +00:00
|
|
|
return this.ser.saveOrUpdate(item, printType, guest_book_id, true);
|
2019-07-13 16:02:18 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 09:25:38 +00:00
|
|
|
type() {
|
|
|
|
return this.bill.voucherType;
|
|
|
|
}
|
|
|
|
|
2019-08-10 13:19:05 +00:00
|
|
|
receivePayment(amounts: { id: string; name: string; amount: number }[]): Observable<boolean> {
|
2019-08-10 20:07:14 +00:00
|
|
|
return this.ser.receivePayment(this.bill.id, amounts, true);
|
2019-08-09 09:25:38 +00:00
|
|
|
}
|
2019-08-10 08:51:40 +00:00
|
|
|
|
|
|
|
moveTable(table: Table): Observable<boolean> {
|
|
|
|
return this.ser.moveTable(this.bill.id, table);
|
|
|
|
}
|
2019-08-10 13:19:05 +00:00
|
|
|
|
2019-08-17 19:35:59 +00:00
|
|
|
mergeTable(table: Table): Observable<boolean> {
|
|
|
|
return this.ser.mergeTable(this.bill.id, table);
|
|
|
|
}
|
|
|
|
|
|
|
|
moveKot(kotId: string, table: Table): Observable<boolean> {
|
|
|
|
return this.ser.moveKotToNewTable(this.bill.id, kotId, table);
|
|
|
|
}
|
|
|
|
|
|
|
|
mergeKot(kotId: string, table: Table): Observable<boolean> {
|
|
|
|
return this.ser.mergeKotWithOldBill(this.bill.id, kotId, table);
|
2019-08-10 13:19:05 +00:00
|
|
|
}
|
2019-08-10 05:00:27 +00:00
|
|
|
|
2019-08-10 20:07:14 +00:00
|
|
|
voidBill(reason: string): Observable<boolean> {
|
|
|
|
return this.ser.voidBill(this.bill.id, reason, true);
|
2019-08-10 05:00:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 20:07:14 +00:00
|
|
|
updateAmounts() {
|
|
|
|
this.netAmount.next(
|
|
|
|
math.round(this.data.filter(x => !x.isKot).reduce(
|
|
|
|
(ca: number, c: any) => ca + ((c.isHappyHour ? 0 : c.price) * c.quantity)
|
|
|
|
, 0))
|
|
|
|
);
|
|
|
|
this.discountAmount.next(
|
|
|
|
math.round(this.data.filter(x => !x.isKot).reduce(
|
2019-08-10 05:00:27 +00:00
|
|
|
(ca: number, c: Inventory) => ca + ((c.isHappyHour ? 0 : c.price) * c.quantity * c.discount)
|
2019-08-10 20:07:14 +00:00
|
|
|
, 0))
|
|
|
|
);
|
|
|
|
this.taxAmount.next(
|
|
|
|
math.round(this.data.filter(x => !x.isKot).reduce(
|
2019-08-10 05:00:27 +00:00
|
|
|
(ca: number, c: Inventory) => ca + ((c.isHappyHour ? 0 : c.price) * c.quantity * (1 - c.discount) * c.taxRate)
|
2019-08-10 20:07:14 +00:00
|
|
|
, 0))
|
|
|
|
);
|
|
|
|
this.amount.next(
|
|
|
|
math.round(this.data.filter(x => !x.isKot).reduce(
|
|
|
|
(ca: number, c: Inventory) => ca + ((c.isHappyHour ? 0 : c.price) * c.quantity * (1 - c.discount) * (1 + c.taxRate))
|
|
|
|
, 0))
|
|
|
|
);
|
2019-08-10 05:00:27 +00:00
|
|
|
}
|
|
|
|
|
2019-08-10 20:07:14 +00:00
|
|
|
amountVal(): number {
|
2019-08-10 05:00:27 +00:00
|
|
|
return math.round(this.bill.kots.reduce(
|
|
|
|
(ka: number, k: Kot) => ka + k.inventories.reduce(
|
|
|
|
(ca: number, c: Inventory) => ca + ((c.isHappyHour ? 0 : c.price) * c.quantity * (1 - c.discount) * (1 + c.taxRate))
|
|
|
|
, 0)
|
2019-08-10 20:07:14 +00:00
|
|
|
, 0));
|
2019-08-10 05:00:27 +00:00
|
|
|
}
|
2019-08-17 19:35:59 +00:00
|
|
|
|
|
|
|
splitBill(table: Table): Observable<boolean> {
|
|
|
|
const inventoriesToMove: string[] = this.selection.selected.map((x:any)=> x.id)
|
|
|
|
return this.ser.splitBill(this.bill.id, inventoriesToMove, table);
|
|
|
|
}
|
2019-07-11 06:47:41 +00:00
|
|
|
}
|