Fix: Bill Deactivate guard was running when printing kots / bills when items were added to new kot.

This commit is contained in:
Amritanshu Agrawal 2021-07-02 09:05:07 +05:30
parent 1287d8f7ac
commit 17702a433b
3 changed files with 16 additions and 9 deletions

View File

@ -74,8 +74,6 @@ def settlements(s: date, f: date, db: Session) -> List[BillSettlementItem]:
amount=round(so.amount, 2), amount=round(so.amount, 2),
) )
) )
for i in report:
print(i.json())
return report return report

View File

@ -34,6 +34,7 @@ export class BillService {
public selection = new SelectionModel<string>(true, []); public selection = new SelectionModel<string>(true, []);
private amountBs: BehaviorSubject<number>; private amountBs: BehaviorSubject<number>;
private updateTable: boolean; private updateTable: boolean;
private allowDeactivate: boolean; // To disable Deactivate Guard on navigation after printing bill or kot.
constructor( constructor(
private dialog: MatDialog, private dialog: MatDialog,
@ -49,17 +50,19 @@ export class BillService {
this.amountBs = new BehaviorSubject(0); this.amountBs = new BehaviorSubject(0);
this.amountVal = 0; this.amountVal = 0;
this.updateTable = true; this.updateTable = true;
this.allowDeactivate = false;
this.amount = this.amountBs.pipe(tap((x) => (this.amountVal = x))); this.amount = this.amountBs.pipe(tap((x) => (this.amountVal = x)));
} }
displayBill(): void { displayBill(): void {
this.allowDeactivate = false;
const data = this.transformBillToView(this.bill); const data = this.transformBillToView(this.bill);
this.dataObs.next(data); this.dataObs.next(data);
this.updateAmounts(); this.updateAmounts();
} }
transformBillToView(bill: Bill): BillViewItem[] { transformBillToView(bill: Bill): BillViewItem[] {
const view: BillViewItem[] = bill.kots return bill.kots
.map((k: Kot) => [ .map((k: Kot) => [
new BillViewItem({ new BillViewItem({
kotId: k.id, kotId: k.id,
@ -85,7 +88,6 @@ export class BillService {
), ),
]) ])
.reduce((a, c) => a.concat(c), []); .reduce((a, c) => a.concat(c), []);
return view;
} }
loadData(bill: Bill, updateTable: boolean): void { loadData(bill: Bill, updateTable: boolean): void {
@ -241,7 +243,9 @@ export class BillService {
if (!this.happyHourItemsBalanced() || this.happyHourItemsMoreThanRegular()) { if (!this.happyHourItemsBalanced() || this.happyHourItemsMoreThanRegular()) {
return throwError('Happy hour products are not balanced.'); return throwError('Happy hour products are not balanced.');
} }
return this.ser.saveOrUpdate(item, VoucherType.Kot, guestBookId, this.updateTable); return this.ser
.saveOrUpdate(item, VoucherType.Kot, guestBookId, this.updateTable)
.pipe(tap(() => (this.allowDeactivate = true)));
} }
printBill(guest_book_id: string | null, voucherType: VoucherType): Observable<boolean> { printBill(guest_book_id: string | null, voucherType: VoucherType): Observable<boolean> {
@ -253,7 +257,9 @@ export class BillService {
if (!this.happyHourItemsBalanced() || this.happyHourItemsMoreThanRegular()) { if (!this.happyHourItemsBalanced() || this.happyHourItemsMoreThanRegular()) {
return throwError('Happy hour products are not balanced.'); return throwError('Happy hour products are not balanced.');
} }
return this.ser.saveOrUpdate(item, voucherType, guest_book_id, this.updateTable); return this.ser
.saveOrUpdate(item, voucherType, guest_book_id, this.updateTable)
.pipe(tap(() => (this.allowDeactivate = true)));
} }
receivePayment(value: { choices: ReceivePaymentItem[]; reason: string }): Observable<boolean> { receivePayment(value: { choices: ReceivePaymentItem[]; reason: string }): Observable<boolean> {
@ -397,8 +403,11 @@ export class BillService {
return false; return false;
} }
public isDirty(): boolean { public canDeactivate(): boolean {
if (this.allowDeactivate) {
return true;
}
const newKot = this.bill.kots.find((k) => k.id === undefined) as Kot; const newKot = this.bill.kots.find((k) => k.id === undefined) as Kot;
return newKot.inventories.length !== 0; return newKot.inventories.length === 0;
} }
} }

View File

@ -14,7 +14,7 @@ import { BillsComponent } from './bills/bills.component';
export class CanDeactivateBillGuard implements CanDeactivate<BillsComponent> { export class CanDeactivateBillGuard implements CanDeactivate<BillsComponent> {
constructor(private dialog: MatDialog) {} constructor(private dialog: MatDialog) {}
canDeactivate(component: BillsComponent): Observable<boolean> | boolean { canDeactivate(component: BillsComponent): Observable<boolean> | boolean {
if (!component.bs.isDirty()) { if (component.bs.canDeactivate()) {
return observableOf(true); return observableOf(true);
} }
const dialogRef = this.dialog.open(ConfirmDialogComponent, { const dialogRef = this.dialog.open(ConfirmDialogComponent, {