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

383 lines
10 KiB
TypeScript

import { Component } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { Observable, of as observableOf } from 'rxjs';
import { map, switchMap, tap } from 'rxjs/operators';
import { AuthService } from '../../auth/auth.service';
import { ReceivePaymentItem } from '../../core/receive-payment-item';
import { Table } from '../../core/table';
import { ToasterService } from '../../core/toaster.service';
import { SaleCategoryService } from '../../sale-category/sale-category.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { TableService } from '../../tables/table.service';
import { BillTypeComponent } from '../bill-type/bill-type.component';
import { BillService } from '../bill.service';
import { VoucherType } from '../bills/voucher-type';
import { DiscountComponent } from '../discount/discount.component';
import { ReasonComponent } from '../reason/reason.component';
import { ReceivePaymentComponent } from '../receive-payment/receive-payment.component';
import { TablesDialogComponent } from '../tables-dialog/tables-dialog.component';
@Component({
selector: 'app-sales-home',
templateUrl: './sales-home.component.html',
styleUrls: ['./sales-home.component.css'],
})
export class SalesHomeComponent {
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,
) {}
printKotAllowed(): boolean {
if (!this.auth.allowed('print-kot')) {
return false;
}
if (!this.bs.bill.id) {
return true;
}
if (this.bs.bill.voucherType !== VoucherType.Kot) {
return false;
}
return true;
}
printKot() {
if (!this.printKotAllowed()) {
return;
}
let guestBookId = null;
if (this.route.snapshot.queryParamMap.has('guest')) {
guestBookId = this.route.snapshot.queryParamMap.get('guest');
}
this.bs.printKot(guestBookId).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
(result) => {
this.toaster.show('Error', result);
},
);
}
discountAllowed(): boolean {
return this.auth.allowed('discount');
}
showDiscount(): Observable<boolean | { id: string; name: string; discount: number }[]> {
if (this.discountAllowed()) {
const dialogRef = this.dialog.open(DiscountComponent, {
// width: '750px',
data: this.mcSer.listForDiscount(),
});
return dialogRef.afterClosed();
}
return observableOf(false);
}
discount(): void {
this.showDiscount().subscribe(
(result: boolean | { id: string; name: string; discount: number }[]) => {
if (result) {
this.bs.discount(result as { id: string; name: string; discount: number }[]);
}
},
);
}
billTypeDialog() {
if (this.bs.bill.voucherType !== VoucherType.Kot) {
return observableOf(this.bs.bill.voucherType);
}
return this.dialog.open(BillTypeComponent).afterClosed();
}
confirmTableDialog(table: Table): Observable<{ table: Table; confirmed: boolean }> {
return this.dialog
.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Select Table?', content: 'Are you sure?' },
})
.afterClosed()
.pipe(map((x: boolean) => ({ 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)));
}
printBillAllowed(): boolean {
if (!this.auth.allowed('print-bill')) {
return false;
}
if (!this.bs.bill.id) {
return true;
}
if (this.bs.bill.voucherType !== VoucherType.Kot && !this.auth.allowed('edit-printed-bill')) {
return false;
}
if (this.bs.bill.voucherType === VoucherType.Void) {
return false;
}
return true;
}
printBill() {
if (!this.printBillAllowed()) {
return;
}
let guestBookId: string | null = null;
if (this.route.snapshot.queryParamMap.has('guest')) {
guestBookId = this.route.snapshot.queryParamMap.get('guest');
}
this.showDiscount()
.pipe(
tap((result: boolean | { id: string; name: string; discount: number }[]) => {
if (result) {
this.bs.discount(result as { id: string; name: string; discount: number }[]);
} else {
throw new Error('Cancelled');
}
}),
switchMap(() => this.billTypeDialog()),
switchMap((x) => {
if (x) {
return observableOf(x);
}
throw new Error('No Bill Type Chosen');
}),
switchMap((x: VoucherType) => this.bs.printBill(guestBookId, x as VoucherType)),
)
.subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
(result) => {
this.toaster.show('Error', result);
},
);
}
receivePaymentAllowed(): boolean {
if (!this.auth.allowed('settle-bill')) {
return false;
}
if (!this.bs.bill.id) {
return false;
}
if (this.bs.bill.voucherType === VoucherType.Kot) {
return false;
}
if (this.bs.bill.voucherType === VoucherType.Void) {
return false;
}
return true;
}
receivePayment() {
if (!this.receivePaymentAllowed()) {
return;
}
const amount = this.bs.amountVal;
const type = this.bs.bill.voucherType;
this.dialog
.open(ReceivePaymentComponent, {
data: {
type,
amount,
},
})
.afterClosed()
.pipe(
switchMap((value: undefined | { choices: ReceivePaymentItem[]; reason: string }) => {
if (value === undefined) {
throw new Error('Receive Payment Choices / Reason not selected');
}
return this.bs.receivePayment(value);
}),
)
.subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
(value) => {
this.toaster.show('Error', value);
},
);
}
moveTableAllowed(): boolean {
if (!this.auth.allowed('move-table') && !this.auth.allowed('merge-tables')) {
return false;
}
if (!this.bs.bill.id) {
return false;
}
return true;
}
moveTable() {
if (!this.moveTableAllowed()) {
return;
}
const canMergeTables = this.auth.allowed('merge-tables');
this.dialog
.open(TablesDialogComponent, {
// width: '750px',
data: {
list: this.tSer.running(),
canChooseRunning: canMergeTables,
},
})
.afterClosed()
.pipe(
switchMap((x: boolean | Table) => {
if (x) {
return this.confirmTableDialog(x as Table);
}
throw new Error('Please choose a table');
}),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
switchMap((value: { table: Table; confirmed: boolean }, index: number) => {
if (!value.confirmed) {
throw new Error('Please confirm move');
}
if (value.table.status) {
return this.bs.mergeTable(value.table);
}
return this.bs.moveTable(value.table);
}),
)
.subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
(x) => {
this.toaster.show('Error', x);
},
);
}
voidBillAllowed(): boolean {
if (!this.auth.allowed('void-bill')) {
return false;
}
if (!this.bs.bill.id) {
return false;
}
return true;
}
voidBill() {
if (!this.voidBillAllowed()) {
return;
}
this.dialog
.open(ReasonComponent, {
// width: '750px'
data: {
title: 'Void Reason',
reasons: [
'Discount',
'Printing fault',
'Item changed',
'Quantity reduced',
'Costing bill for party',
'Cashier mistake',
'Management free sale',
],
},
})
.afterClosed()
.pipe(
switchMap((x: boolean | string) => {
if (x) {
return this.confirmVoidDialog(x as string);
}
throw new Error('Please choose a reason to void the bill');
}),
switchMap((x: boolean | string) => {
if (x) {
return this.bs.voidBill(x as string);
}
throw new Error('You chose not to void the bill');
}),
)
.subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
(x) => {
this.toaster.show('Error', x);
},
);
}
splitBillAllowed(): boolean {
if (!this.auth.allowed('split-bill')) {
return false;
}
if (!this.bs.bill.id) {
return false;
}
return true;
}
splitBill() {
if (!this.splitBillAllowed()) {
return;
}
this.dialog
.open(TablesDialogComponent, {
// width: '750px',
data: {
list: this.tSer.running(),
canChooseRunning: false,
},
})
.afterClosed()
.pipe(
switchMap((x: boolean | Table) => {
if (x) {
return this.confirmTableDialog(x as Table);
}
throw new Error('Please choose a table');
}),
// eslint-disable-next-line @typescript-eslint/no-unused-vars
switchMap((value: { table: Table; confirmed: boolean }, index: number) => {
if (!value.confirmed) {
throw new Error('Please confirm split');
}
return this.bs.splitBill(value.table);
}),
)
.subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
(x) => {
this.toaster.show('Error', x);
},
);
}
}