Split bill working along with all checks.

Update bill ensures that the total number of happy hour punches of a product in a bill are less than or equal to the regular punches
This commit is contained in:
2020-12-18 13:24:05 +05:30
parent 608dde4619
commit f28cf1eea0
5 changed files with 158 additions and 61 deletions

View File

@ -230,7 +230,7 @@ export class BillService {
if (newKot.inventories.length === 0) {
return throwError('Cannot print a blank KOT\nPlease add some products!');
}
if (!this.happyHourItemsBalanced()) {
if (!this.happyHourItemsBalanced() || this.happyHourItemsMoreThanRegular()) {
return throwError('Happy hour products are not balanced.');
}
return this.ser.saveOrUpdate(item, VoucherType.Kot, guestBookId, true);
@ -242,7 +242,7 @@ export class BillService {
if (item.kots.length === 1 && newKot.inventories.length === 0) {
return throwError('Cannot print a blank Bill\nPlease add some products!');
}
if (!this.happyHourItemsBalanced()) {
if (!this.happyHourItemsBalanced() || this.happyHourItemsMoreThanRegular()) {
return throwError('Happy hour products are not balanced.');
}
return this.ser.saveOrUpdate(item, voucherType, guest_book_id, true);
@ -334,18 +334,42 @@ export class BillService {
}
private happyHourItemsBalanced(): boolean {
const newKot = this.bill.kots.find((k) => k.id === undefined) as Kot;
const happyHourItems = newKot.inventories
.filter((x) => x.isHappyHour)
.map((x) => ({ id: x.product.id as string, quantity: x.quantity }));
for (const item of happyHourItems) {
const q = newKot.inventories.find(
(x) => !x.isHappyHour && x.product.id === item.id && x.quantity === item.quantity,
);
if (q === undefined) {
return false;
for (const kot of this.bill.kots) {
const happyHourItems = kot.inventories
.filter((x) => x.isHappyHour)
.map((x) => ({ id: x.product.id as string, quantity: x.quantity }));
for (const item of happyHourItems) {
const q = kot.inventories.find(
(x) => !x.isHappyHour && x.product.id === item.id && x.quantity === item.quantity,
);
if (q === undefined) {
return false;
}
}
}
return true;
}
private happyHourItemsMoreThanRegular(): boolean {
const invs: { [id: string]: { normal: number; happy: number } } = {};
for (const kot of this.bill.kots) {
for (const inventory of kot.inventories) {
const pid = inventory.product.id as string;
if (invs[pid] === undefined) {
invs[pid] = { normal: 0, happy: 0 };
}
if (inventory.isHappyHour) {
invs[pid].happy += inventory.quantity;
} else {
invs[pid].normal += inventory.quantity;
}
}
}
for (const [, value] of Object.entries(invs)) {
if (value.happy > value.normal) {
return true;
}
}
return false;
}
}

View File

@ -126,7 +126,7 @@
<button
mat-icon-button
(click)="moveKot(row)"
[disabled]="row.isKot && !row.id"
[disabled]="row.isKot && !row.kotId"
*ngIf="row.isKot"
>
<mat-icon class="del">open_in_new</mat-icon>