173 lines
5.4 KiB
TypeScript
173 lines
5.4 KiB
TypeScript
import { DataSource } from '@angular/cdk/collections';
|
|
import { EventEmitter } from '@angular/core';
|
|
import { MatPaginator, PageEvent } from '@angular/material/paginator';
|
|
import { MatSort, Sort } from '@angular/material/sort';
|
|
import { merge, Observable, of as observableOf } from 'rxjs';
|
|
import { map, tap } from 'rxjs/operators';
|
|
|
|
import { MenuEngineeringReportItem } from './menu-engineering-report-item';
|
|
|
|
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
|
|
const compare = (a: string | number | boolean, b: string | number | boolean, isAsc: boolean) =>
|
|
(a < b ? -1 : 1) * (isAsc ? 1 : -1);
|
|
export class MenuEngineeringReportDataSource extends DataSource<MenuEngineeringReportItem> {
|
|
private filterValue = '';
|
|
constructor(
|
|
public data: MenuEngineeringReportItem[],
|
|
private filter: Observable<string>,
|
|
private paginator?: MatPaginator,
|
|
private sort?: MatSort,
|
|
) {
|
|
super();
|
|
this.filter = filter.pipe(
|
|
tap((x) => {
|
|
this.filterValue = x;
|
|
}),
|
|
);
|
|
}
|
|
|
|
connect(): Observable<MenuEngineeringReportItem[]> {
|
|
const dataMutations: (EventEmitter<PageEvent> | EventEmitter<Sort>)[] = [];
|
|
if (this.paginator) {
|
|
dataMutations.push((this.paginator as MatPaginator).page);
|
|
}
|
|
if (this.sort) {
|
|
dataMutations.push((this.sort as MatSort).sortChange);
|
|
}
|
|
return merge(observableOf(this.data), this.filter, ...dataMutations)
|
|
.pipe(
|
|
map(() => this.getFilteredData([...this.data])),
|
|
map((x) => this.recalculate(x)),
|
|
tap((x: MenuEngineeringReportItem[]) => {
|
|
if (this.paginator) {
|
|
this.paginator.length = x.length;
|
|
}
|
|
}),
|
|
)
|
|
.pipe(map((x: MenuEngineeringReportItem[]) => this.getPagedData(this.getSortedData(x))));
|
|
}
|
|
|
|
disconnect() {}
|
|
|
|
private getFilteredData(data: MenuEngineeringReportItem[]): MenuEngineeringReportItem[] {
|
|
return this.filterValue.split(' ').reduce(
|
|
(p: MenuEngineeringReportItem[], c: string) =>
|
|
p.filter((x) => {
|
|
if (c.startsWith('n:')) {
|
|
return x.name.toLowerCase().indexOf(c.substring(2)) !== -1;
|
|
}
|
|
if (c.startsWith('sc:')) {
|
|
return x.saleCategory.toLowerCase().indexOf(c.substring(3)) !== -1;
|
|
}
|
|
if (c.startsWith('mc:')) {
|
|
return x.menuCategory.toLowerCase().indexOf(c.substring(3)) !== -1;
|
|
}
|
|
if (c.startsWith('q:')) {
|
|
const result = c.match(/^q:(?<sign>=|<=|>=|<|>)(?<amount>\d*)$/);
|
|
if (result && result.groups) {
|
|
return math_it_up(result.groups['sign'])(x.quantity, +result.groups['amount'] ?? '');
|
|
}
|
|
}
|
|
if (c.startsWith('s:')) {
|
|
const result = c.match(/^s:(?<sign>=|<=|>=|<|>)(?<amount>\d*)$/);
|
|
if (result && result.groups) {
|
|
return math_it_up(result.groups['sign'])(x.sales, +result.groups['amount'] ?? '');
|
|
}
|
|
}
|
|
const itemString = `${x.name} ${x.saleCategory} ${x.menuCategory}`.toLowerCase();
|
|
return itemString.indexOf(c) !== -1;
|
|
}),
|
|
Object.assign([], data),
|
|
);
|
|
}
|
|
|
|
private recalculate(data: MenuEngineeringReportItem[]): MenuEngineeringReportItem[] {
|
|
const gr = data.reduce(function (res: Record<string, { q: number; s: number }>, value: MenuEngineeringReportItem) {
|
|
if (!res[value['saleCategory']]) {
|
|
res[value['saleCategory']] = { q: 0, s: 0 };
|
|
}
|
|
res[value['saleCategory']].q += value.quantity;
|
|
res[value['saleCategory']].s += value.sales;
|
|
return res;
|
|
}, {});
|
|
data.forEach((v) => {
|
|
v.quantityPercent = v.quantity / gr[v.saleCategory].q;
|
|
v.salesPercent = v.sales / gr[v.saleCategory].s;
|
|
});
|
|
return data;
|
|
}
|
|
|
|
private getPagedData(data: MenuEngineeringReportItem[]) {
|
|
if (this.paginator === undefined) {
|
|
return data;
|
|
}
|
|
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
|
|
return data.splice(startIndex, this.paginator.pageSize);
|
|
}
|
|
|
|
private getSortedData(data: MenuEngineeringReportItem[]): MenuEngineeringReportItem[] {
|
|
if (this.sort === undefined) {
|
|
return data;
|
|
}
|
|
if (!this.sort.active || this.sort.direction === '') {
|
|
return data;
|
|
}
|
|
|
|
const sort = this.sort as MatSort;
|
|
return data.sort((a, b) => {
|
|
const isAsc = sort.direction === 'asc';
|
|
switch (sort.active) {
|
|
case 'name':
|
|
return compare(a.name, b.name, isAsc);
|
|
case 'price':
|
|
return compare(a.price, b.price, isAsc);
|
|
case 'saleCategory':
|
|
return compare(a.saleCategory, b.saleCategory, isAsc);
|
|
case 'menuCategory':
|
|
return compare(a.menuCategory, b.menuCategory, isAsc);
|
|
case 'quantity':
|
|
return compare(a.quantity, b.quantity, isAsc);
|
|
case 'sales':
|
|
return compare(a.sales, b.sales, isAsc);
|
|
default:
|
|
return 0;
|
|
}
|
|
});
|
|
}
|
|
}
|
|
|
|
function eq(x: number, y: number) {
|
|
return x === y;
|
|
}
|
|
function gt(x: number, y: number) {
|
|
return x > y;
|
|
}
|
|
function lt(x: number, y: number) {
|
|
return x < y;
|
|
}
|
|
function gte(x: number, y: number) {
|
|
return x >= y;
|
|
}
|
|
function lte(x: number, y: number) {
|
|
return x <= y;
|
|
}
|
|
|
|
function math_it_up(sign: string) {
|
|
if (sign == '=') {
|
|
return eq;
|
|
}
|
|
if (sign == '>') {
|
|
return gt;
|
|
}
|
|
if (sign == '<') {
|
|
return lt;
|
|
}
|
|
if (sign == '>=') {
|
|
return gte;
|
|
}
|
|
if (sign == '<=') {
|
|
return lte;
|
|
}
|
|
return eq;
|
|
}
|