barker/bookie/src/app/sales/home/sales-home.component.ts

214 lines
6.4 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from "@angular/router";
import { MatDialog } from "@angular/material";
import { concatMap, map, switchMap, tap } from "rxjs/operators";
import { iif, Observable, of as observableOf, throwError } from "rxjs";
import { BillService } from '../bill.service';
import { ToasterService } from "../../core/toaster.service";
import { DiscountComponent } from "../discount/discount.component";
import { SaleCategoryService } from "../../sale-category/sale-category.service";
import { BillTypeComponent } from "../bill-type/bill-type.component";
import { PrintType } from "../bills/bill";
import { AuthService } from "../../auth/auth.service";
import { ReceivePaymentComponent } from "../receive-payment/receive-payment.component";
import { TableService } from "../../tables/table.service";
import { Table } from "../../core/table";
import { TablesDialogComponent } from "../tables-dialog/tables-dialog.component";
import { ConfirmDialogComponent } from "../../shared/confirm-dialog/confirm-dialog.component";
import { VoidReasonComponent } from "../void-reason/void-reason.component";
@Component({
selector: 'app-sales-home',
templateUrl: './sales-home.component.html',
styleUrls: ['./sales-home.component.css']
})
export class SalesHomeComponent implements OnInit {
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private auth: AuthService,
private toaster: ToasterService,
private mcSer: SaleCategoryService,
private tSer: TableService,
private bs: BillService) {
}
ngOnInit() {
}
printKot() {
let guestBookId = null;
if (this.route.snapshot.queryParamMap.has("guest")) {
guestBookId = this.route.snapshot.queryParamMap.get("guest");
}
this.bs.printKot(guestBookId).subscribe(x => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
});
}
discount(): void {
if (this.auth.hasPermission("Discount")) {
this.showDiscount().subscribe();
}
}
showDiscount(): Observable<boolean | { id: string, name: string, discount: number }[]> {
const dialogRef = this.dialog.open(DiscountComponent, {
// width: '750px',
data: this.mcSer.listForDiscount()
});
return dialogRef.afterClosed().pipe(
tap((result: boolean | { id: string, name: string, discount: number }[]) => {
if (!!result) {
this.bs.discount(result as { id: string, name: string, discount: number }[]);
}
})
)
}
discountDialog (canGiveDiscount: boolean) : Observable<any> {
let discObs = null;
if (canGiveDiscount) {
return discObs = this.showDiscount();
} else {
return discObs = observableOf("");
}
}
billTypeDialog() {
return this.dialog.open(BillTypeComponent).afterClosed().pipe(
tap(x => {
if (!x) {
throwError ("No Bill Type Chosen")
}
})
)
}
confirmMoveTableDialog(table: Table): Observable<{table: Table, confirmed: boolean}> {
return this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {title: 'Move Table?', content: 'Are you sure?'}
}).afterClosed().pipe(
map ((x: boolean) => ({table: table, confirmed: x}))
);
}
confirmVoidDialog(reason: string): Observable<boolean | string> {
return this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {title: 'Void Bill?', content: 'Are you sure?'}
}).afterClosed().pipe(
map((x: boolean) => x ? reason : x)
);
}
printBill() {
const canGiveDiscount = this.auth.hasPermission("Discount");
let guestBookId = null;
if (this.route.snapshot.queryParamMap.has("guest")) {
guestBookId = this.route.snapshot.queryParamMap.get("guest");
}
this.discountDialog(canGiveDiscount).pipe(
concatMap(() => this.billTypeDialog())
).pipe(
concatMap(
(x: boolean | PrintType) => iif(
() => !!x,
this.bs.printBill(guestBookId, x as PrintType),
throwError(x)
)
),
).subscribe(() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
x => {
this.toaster.show('Error', "No Bill Type Chosen")
});
}
receivePayment() {
let amount = this.bs.amount();
let type = this.bs.type();
const dialogRef = this.dialog.open(ReceivePaymentComponent, {
// width: '750px',
data: {
type: type,
amount: amount
}
});
dialogRef.afterClosed().subscribe((result: boolean | { id: string, name: string, amount: number }[]) => {
if (!!result) {
this.bs.receivePayment(result as { id: string, name: string, amount: number }[]).subscribe(() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
});
}
})
}
moveTable() {
this.dialog.open(TablesDialogComponent, {
// width: '750px',
data: {
list: this.tSer.running(),
canChooseRunning: false
}
}).afterClosed().pipe(
switchMap((x: boolean | Table) => {
if (!!x) {
return this.confirmMoveTableDialog(x as Table);
} else {
return throwError("Please choose a table");
}
}),
switchMap((value: { table: Table, confirmed: boolean }, index: number) => {
if (!!value.confirmed) {
return this.bs.moveTable(value.table)
} else {
return throwError("Please confirm move")
}
}
)
).subscribe((x) => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
x => {
this.toaster.show('Error', x)
})
}
voidBill() {
this.dialog.open(VoidReasonComponent, {
// width: '750px'
}).afterClosed().pipe(
switchMap((x: boolean | string) => {
if (!!x) {
return this.confirmVoidDialog(x as string);
} else {
return throwError("Please choose a reason to void the bill");
}
}),
switchMap((x: boolean | string) => {
if (!!x) {
return this.bs.voidBill(x as string)
} else {
return throwError("You chose not to void the bill")
}
})
).subscribe((x) => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
x => {
this.toaster.show('Error', x)
})
}
}