barker/bookie/src/app/sales/bills/bills.component.ts

170 lines
4.7 KiB
TypeScript
Raw Normal View History

2019-06-19 18:25:42 +00:00
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';
import { Bill, Kot } from './bill';
2019-07-13 16:02:18 +00:00
import { BillsDataSource } from './bills-datasource';
import { BillService } from '../bill.service';
import { QuantityComponent } from '../quantity/quantity.component';
import { MatDialog } from '@angular/material/dialog';
import { Table } from '../../core/table';
import { Observable, throwError } from 'rxjs';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { map, switchMap } from 'rxjs/operators';
import { TablesDialogComponent } from '../tables-dialog/tables-dialog.component';
import { TableService } from '../../tables/table.service';
import { ToasterService } from '../../core/toaster.service';
import { AuthService } from '../../auth/auth.service';
import { PaxComponent } from '../pax/pax.component';
2019-06-19 18:25:42 +00:00
@Component({
selector: 'app-bills',
templateUrl: './bills.component.html',
styleUrls: ['./bills.component.css']
})
export class BillsComponent implements OnInit {
dataSource: BillsDataSource;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns: string[] = ['select', 'info', 'quantity'];
2019-06-19 18:25:42 +00:00
constructor(
private route: ActivatedRoute,
private router: Router,
2019-07-13 16:02:18 +00:00
private dialog: MatDialog,
private toaster: ToasterService,
private auth: AuthService,
private bs: BillService,
private tSer: TableService
) {
2019-06-19 18:25:42 +00:00
}
ngOnInit() {
this.route.data
.subscribe((data: { item: Bill }) => {
2019-07-13 16:02:18 +00:00
this.bs.loadData(data.item);
2019-06-19 18:25:42 +00:00
});
this.getPax();
2019-07-13 16:02:18 +00:00
this.dataSource = new BillsDataSource(this.bs.dataObs);
}
getPax(): void {
if (this.bs.bill.id || this.bs.bill.customer) {
return;
}
const dialogRef = this.dialog.open(PaxComponent, {
// width: '750px',
data: this.bs.bill.pax
});
dialogRef.afterClosed().subscribe((result: boolean | number) => {
if (!result) {
return;
}
this.bs.bill.pax = result as number;
});
}
isAllSelected(kot: Kot) {
return this.bs.data.filter(
x => x.kotId === kot.id
).reduce(
(p: boolean, c: any) => p && this.bs.selection.isSelected(c)
, true
);
}
isAnySelected(kot: Kot) {
let total = 0,
found = 0;
this.bs.data.filter(
x => x.kotId === kot.id
).forEach((c: any) => {
total += 1;
if (this.bs.selection.isSelected(c)) {
found += 1;
}
});
return found > 0 && found < total;
}
masterToggle(kot: Kot) {
const isAllSelected = this.isAllSelected(kot);
this.bs.data.filter(
x => x.kotId === kot.id
).forEach(
row => isAllSelected ? this.bs.selection.deselect(row) : this.bs.selection.select(row)
);
}
addOne(item: any): void {
this.bs.addOne(item);
}
quantity(item: any): void {
2019-07-13 16:02:18 +00:00
const dialogRef = this.dialog.open(QuantityComponent, {
// width: '750px',
data: item.quantity
});
dialogRef.afterClosed().subscribe((result: boolean | number) => {
if (!result) {
return;
}
this.bs.quantity(item, result as number);
});
}
subtractOne(item: any): void {
this.bs.subtractOne(item);
}
removeItem(item: any): void {
this.bs.removeItem(item);
}
modifier(item: any): void {
this.bs.modifier(item);
2019-06-19 18:25:42 +00:00
}
confirmMoveKotDialog(table: Table): Observable<{table: Table, confirmed: boolean}> {
return this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: {title: 'Move KOT?', content: 'Are you sure?'}
}).afterClosed().pipe(
map ((x: boolean) => ({table: table, confirmed: x}))
);
}
moveKot(kot: Kot) {
const canMergeTables = (this.auth.user.perms.indexOf('merge-tables') !== -1);
this.dialog.open(TablesDialogComponent, {
// width: '750px',
data: {
list: this.tSer.running(),
canChooseRunning: canMergeTables
}
}).afterClosed().pipe(
switchMap((x: boolean | Table) => {
if (!!x) {
return this.confirmMoveKotDialog(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.mergeKot(kot.id, value.table);
} else {
return this.bs.moveKot(kot.id, value.table);
}
}
)
).subscribe((x) => {
this.toaster.show('Success', '');
this.router.navigate(['/sales']);
},
x => {
this.toaster.show('Error', x);
});
}
2019-06-19 18:25:42 +00:00
}