Feature: Recipe module mostly working. What needs to be done is duplicating recipes and export for checking.
Feature: Non Contract Purchases Report
This commit is contained in:
@@ -0,0 +1,147 @@
|
||||
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 * as moment from 'moment';
|
||||
import { merge, Observable } from 'rxjs';
|
||||
import { map, tap } from 'rxjs/operators';
|
||||
|
||||
import { Recipe } from '../recipe';
|
||||
|
||||
export class RecipeListDatasource extends DataSource<Recipe> {
|
||||
public data: Recipe[];
|
||||
public filteredData: Recipe[];
|
||||
public validFrom: Date | null;
|
||||
public validTill: Date | null;
|
||||
public productGroup: string;
|
||||
|
||||
constructor(
|
||||
private readonly validFromFilter: Observable<Date | null>,
|
||||
private readonly validTillFilter: Observable<Date | null>,
|
||||
private readonly productGroupFilter: Observable<string>,
|
||||
private readonly dataObs: Observable<Recipe[]>,
|
||||
|
||||
private paginator?: MatPaginator,
|
||||
private sort?: MatSort,
|
||||
) {
|
||||
super();
|
||||
this.data = [];
|
||||
this.filteredData = [];
|
||||
this.validFrom = null;
|
||||
this.validTill = null;
|
||||
this.productGroup = '';
|
||||
}
|
||||
|
||||
connect(): Observable<Recipe[]> {
|
||||
const dataMutations: (
|
||||
| Observable<Recipe[]>
|
||||
| Observable<Date | null>
|
||||
| Observable<string>
|
||||
| EventEmitter<PageEvent>
|
||||
| EventEmitter<Sort>
|
||||
)[] = [
|
||||
this.dataObs.pipe(
|
||||
tap((x) => {
|
||||
this.data = x;
|
||||
}),
|
||||
),
|
||||
this.validFromFilter.pipe(
|
||||
tap((x) => {
|
||||
this.validFrom = x;
|
||||
}),
|
||||
),
|
||||
this.validTillFilter.pipe(
|
||||
tap((x) => {
|
||||
this.validTill = x;
|
||||
}),
|
||||
),
|
||||
this.productGroupFilter.pipe(
|
||||
tap((x) => {
|
||||
this.productGroup = x;
|
||||
}),
|
||||
),
|
||||
];
|
||||
if (this.paginator) {
|
||||
dataMutations.push((this.paginator as MatPaginator).page);
|
||||
}
|
||||
if (this.sort) {
|
||||
dataMutations.push((this.sort as MatSort).sortChange);
|
||||
}
|
||||
|
||||
return merge(...dataMutations).pipe(
|
||||
map(() => this.getFilteredData(this.data, this.productGroup, this.validFrom, this.validTill)),
|
||||
tap((x: Recipe[]) => {
|
||||
if (this.paginator) {
|
||||
this.paginator.length = x.length;
|
||||
}
|
||||
}),
|
||||
tap((x) => {
|
||||
this.filteredData = x;
|
||||
}),
|
||||
map(() => this.getPagedData(this.getSortedData([...this.filteredData]))),
|
||||
);
|
||||
}
|
||||
|
||||
disconnect() {}
|
||||
|
||||
private getFilteredData(
|
||||
data: Recipe[],
|
||||
productGroup: string,
|
||||
validFrom: Date | null,
|
||||
validTill: Date | null,
|
||||
): Recipe[] {
|
||||
return data
|
||||
.filter((x: Recipe) => productGroup === '' || x.notes === productGroup)
|
||||
.filter((x) => validFrom === null || validFrom <= moment(x.validFrom, 'DD-MMM-YYYY').toDate())
|
||||
.filter(
|
||||
(x) => validTill === null || validTill >= moment(x.validTill, 'DD-MMM-YYYY').toDate(),
|
||||
);
|
||||
}
|
||||
|
||||
private getPagedData(data: Recipe[]) {
|
||||
if (this.paginator === undefined) {
|
||||
return data;
|
||||
}
|
||||
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
|
||||
return data.splice(startIndex, this.paginator.pageSize);
|
||||
}
|
||||
|
||||
private getSortedData(data: Recipe[]) {
|
||||
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.sku.name, b.sku.name, isAsc);
|
||||
case 'validity':
|
||||
if (isAsc) {
|
||||
return compareDate(a.validFrom, b.validFrom, isAsc);
|
||||
} else {
|
||||
return compareDate(a.validTill, b.validTill, isAsc);
|
||||
}
|
||||
case 'salePrice':
|
||||
return compare(a.salePrice, b.salePrice, isAsc);
|
||||
case 'costPrice':
|
||||
return compare(a.costPrice, b.costPrice, isAsc);
|
||||
case 'costPercentage':
|
||||
return compare(a.costPrice / a.salePrice, b.costPrice / b.salePrice, isAsc);
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
|
||||
const compare = (a: string | number | Date, b: string | number | Date, isAsc: boolean) =>
|
||||
(a < b ? -1 : 1) * (isAsc ? 1 : -1);
|
||||
/** Simple sort comparator for example ID/Name columns (for client-side sorting). */
|
||||
const compareDate = (a: string, b: string, isAsc: boolean) =>
|
||||
(moment(a, 'DD-MMM-YYYY').toDate() < moment(b, 'DD-MMM-YYYY').toDate() ? -1 : 1) *
|
||||
(isAsc ? 1 : -1);
|
||||
Reference in New Issue
Block a user