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

307 lines
8.6 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { MatDialog } from '@angular/material';
import { map, switchMap, tap } from 'rxjs/operators';
import { 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() {
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(x => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
});
}
discount(): void {
if (!this.discountAllowed()) {
return;
}
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> {
if (canGiveDiscount) {
return this.showDiscount();
} else {
return observableOf('');
}
}
billTypeDialog() {
return this.dialog.open(BillTypeComponent).afterClosed().pipe(
tap(x => {
if (!x) {
throwError ('No Bill Type Chosen');
}
})
);
}
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: 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() {
if (!this.printBillAllowed()) {
return;
}
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(
switchMap(() => this.billTypeDialog()),
switchMap((x: boolean | PrintType) => {
if (!!x) {
return this.bs.printBill(guestBookId, x as PrintType);
} else {
return throwError(x);
}
}),
).subscribe(() => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
() => {
this.toaster.show('Error', 'No Bill Type Chosen');
});
}
receivePayment() {
if (!this.receivePaymentAllowed()) {
return;
}
const amount = this.bs.amountVal();
const 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() {
const canMergeTables = this.auth.hasPermission("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);
} else {
return throwError('Please choose a table');
}
}),
switchMap((value: { table: Table, confirmed: boolean }, index: number) => {
if (!value.confirmed) {
return throwError('Please confirm move');
} else if (value.table.status) {
return this.bs.mergeTable(value.table);
} else {
return this.bs.moveTable(value.table);
}
}
)
).subscribe(() => {
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);
});
}
splitBill() {
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);
} else {
return throwError('Please choose a table');
}
}),
switchMap((value: { table: Table, confirmed: boolean }, index: number) => {
if (!value.confirmed) {
return throwError('Please confirm split');
} else {
return this.bs.splitBill(value.table);
}
})
).subscribe((x) => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
x => {
this.toaster.show('Error', x);
});
}
discountAllowed(): boolean {
return this.auth.hasPermission('Discount');
}
printKotAllowed(): boolean {
if (!this.auth.hasPermission('Print Kot')) {
return false;
}
if (!this.bs.bill.id) {
return true;
}
if (this.bs.bill.voucherType !== PrintType.Kot) {
return false;
}
if (this.bs.bill.isVoid) {
return false;
}
return true;
}
printBillAllowed(): boolean {
if (!this.auth.hasPermission('Print Bill')) {
return false;
}
if (!this.bs.bill.id) {
return true;
}
if (this.bs.bill.voucherType !== PrintType.Kot && !this.auth.hasPermission('Edit Printed Bill')) {
return false;
}
if (this.bs.bill.isVoid) {
return false;
}
return true;
}
receivePaymentAllowed(): boolean {
if (!this.auth.hasPermission('Settle Bill')) {
return false;
}
if (!this.bs.bill.id) {
return false;
}
if (this.bs.bill.voucherType === PrintType.Kot) {
return false
}
if (this.bs.bill.isVoid) {
return false;
}
return true;
}
}