barker/bookie/src/app/sales/bills/voucher.service.ts

189 lines
6.3 KiB
TypeScript

import { HttpClient, HttpParams } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ErrorLoggerService } from '../../core/error-logger.service';
import { Table } from '../../core/table';
import { Bill } from './bill';
import { VoucherType } from './voucher-type';
const url = '/api/voucher';
const urlMoveTable = '/api/move-table';
const urlMoveKot = '/api/move-kot';
const urlSplitBill = '/api/split-bill';
const serviceName = 'VoucherService';
@Injectable({ providedIn: 'root' })
export class VoucherService {
constructor(private http: HttpClient, private log: ErrorLoggerService) {}
get(id: string): Observable<Bill> {
return this.http
.get<Bill>(`${url}/from-id/${id}`)
.pipe(catchError(this.log.handleError(serviceName, `get id=${id}`))) as Observable<Bill>;
}
getFromTable(tableId: string, voucherId: string | null, guestId: string | null): Observable<Bill> {
let params = new HttpParams();
if (voucherId !== null) {
params = params.set('v', voucherId);
}
if (guestId !== null) {
params = params.set('g', guestId);
}
return this.http
.get<Bill>(`${url}/from-table/${tableId}`, { params })
.pipe(
catchError(
this.log.handleError(
serviceName,
`getFromTable tableId=${tableId} voucherId=${voucherId} guestId=${guestId}`,
),
),
) as Observable<Bill>;
}
getFromBill(billId: string): Observable<Bill> {
return this.http
.get<Bill>(`${url}/from-bill/${billId}`)
.pipe(catchError(this.log.handleError(serviceName, `getFromBill billId=${billId}`))) as Observable<Bill>;
}
save(voucher: Bill, voucherType: VoucherType, guestBookId: string | null, updateTable: boolean): Observable<boolean> {
const options = {
params: new HttpParams().set('p', voucherType.toString()).set('u', updateTable.toString()),
};
if (guestBookId !== null) {
options.params = options.params.set('g', guestBookId);
}
return this.http
.post<boolean>(`${url}/save`, voucher, options)
.pipe(catchError(this.log.handleError(serviceName, 'save'))) as Observable<boolean>;
}
update(
voucher: Bill,
voucherType: VoucherType,
guestBookId: string | null,
updateTable: boolean,
): Observable<boolean> {
const options = {
params: new HttpParams().set('p', voucherType.toString()).set('u', updateTable.toString()),
};
if (guestBookId !== null) {
options.params = options.params.set('g', guestBookId);
}
return this.http
.put<boolean>(`${url}/update/${voucher.id}`, voucher, options)
.pipe(catchError(this.log.handleError(serviceName, 'update'))) as Observable<boolean>;
}
change(
voucher: Bill,
voucherType: VoucherType,
guestBookId: string | null,
updateTable: boolean,
): Observable<boolean> {
const options = { params: new HttpParams().set('u', updateTable.toString()) };
if (guestBookId !== null) {
options.params = options.params.set('g', guestBookId);
}
return this.http
.put<boolean>(`${url}/change/${voucher.id}`, voucher, options)
.pipe(catchError(this.log.handleError(serviceName, 'change'))) as Observable<boolean>;
}
saveOrUpdate(
voucher: Bill,
voucherType: VoucherType,
guestBookId: string | null,
updateTable: boolean,
): Observable<boolean> {
if (!voucher.id) {
return this.save(voucher, voucherType, guestBookId, updateTable);
}
if (voucher.voucherType === VoucherType.Kot) {
return this.update(voucher, voucherType, guestBookId, updateTable);
}
return this.change(voucher, voucherType, guestBookId, updateTable);
}
receivePayment(
id: string,
amounts: { id: number; name: string; amount: number }[],
reason: string,
updateTable: boolean,
): Observable<boolean> {
const options = { params: new HttpParams().set('u', updateTable.toString()) };
return this.http
.post<boolean>(`${url}/receive-payment/${id}`, { reason, amounts }, options)
.pipe(catchError(this.log.handleError(serviceName, 'receivePayment'))) as Observable<boolean>;
}
cancelBill(id: string, reason: string, updateTable: boolean): Observable<boolean> {
const options = {
params: new HttpParams().set('reason', reason).set('u', updateTable.toString()),
};
return this.http
.post<boolean>(`${url}/cancel-bill/${id}`, {}, options)
.pipe(catchError(this.log.handleError(serviceName, 'cancelBill'))) as Observable<boolean>;
}
moveTable(id: string, table: Table): Observable<boolean> {
return this.http
.post<boolean>(`${urlMoveTable}/move`, {
voucherId: id,
tableId: table.id,
})
.pipe(catchError(this.log.handleError(serviceName, 'moveTable'))) as Observable<boolean>;
}
mergeTable(id: string, table: Table): Observable<boolean> {
return this.http
.post<boolean>(`${urlMoveTable}/merge`, {
voucherId: id,
tableId: table.id,
newVoucherId: table.voucherId,
})
.pipe(catchError(this.log.handleError(serviceName, 'mergeTable'))) as Observable<boolean>;
}
moveKotToNewTable(id: string, kotId: string, table: Table): Observable<boolean> {
return this.http
.post<boolean>(`${urlMoveKot}/move`, {
voucherId: id,
kotId,
tableId: table.id,
})
.pipe(catchError(this.log.handleError(serviceName, 'moveKotToNewTable'))) as Observable<boolean>;
}
mergeKotWithOldBill(id: string, kotId: string, table: Table): Observable<boolean> {
return this.http
.post<boolean>(`${urlMoveKot}/merge`, {
voucherId: id,
kotId,
tableId: table.id,
newVoucherId: table.voucherId,
})
.pipe(catchError(this.log.handleError(serviceName, 'mergeKotWithOldBill'))) as Observable<boolean>;
}
splitBill(id: string, inventoriesToMove: string[], table: Table, updateTable: boolean) {
const options = { params: new HttpParams().set('u', updateTable.toString()) };
return this.http
.post<boolean>(
`${urlSplitBill}/${id}`,
{
inventories: inventoriesToMove,
tableId: table.id,
},
options,
)
.pipe(catchError(this.log.handleError(serviceName, 'splitBill'))) as Observable<boolean>;
}
}