Files
barker/bookie/src/app/sales/bills/bills-datasource.ts
Amritanshu 8957e4ce8c All voucher routes now work with bundles.
Need to check / update all reports and prints
2026-02-05 00:46:49 +00:00

53 lines
1.5 KiB
TypeScript

import { DataSource } from '@angular/cdk/collections';
import { map, Observable, tap } from 'rxjs';
import { BillRow } from './bill-row';
import { Kot } from './kot';
export class BillsDataSource extends DataSource<BillRow> {
private data: Kot[] = [];
constructor(private readonly dataObs: Observable<Kot[]>) {
super();
}
connect(): Observable<BillRow[]> {
return this.dataObs.pipe(
tap((x) => {
this.data = x;
}),
map((d) =>
d.flatMap((k) => {
const kotRow: BillRow = new BillRow({ kind: 'kot', kot: k, id: k.id! });
const rows: BillRow[] = [];
for (const inv of k.inventories) {
if (!inv.id) {
inv.clientId = inv.clientId || crypto.randomUUID();
}
rows.push(new BillRow({ kind: 'inv', kot: k, inv, id: `${k.id}-${inv.id || inv.clientId}` }));
if (inv.type === 'bundle') {
for (const child of inv.children) {
if (!child.id) {
child.clientId = child.clientId || crypto.randomUUID();
}
child.parentId = inv.id || inv.clientId;
rows.push(
new BillRow({
kind: 'child',
kot: k,
parent: inv,
inv: child,
id: `${k.id}-${child.id || child.clientId}`,
}),
);
}
}
}
return [kotRow, ...rows];
}),
),
);
}
disconnect() {}
}