Created a period table for date ranges and made the recipes dependent on it instead of having arbitrary date ranges. This will enable easy copying into new months.

This commit is contained in:
2022-07-28 22:10:45 +05:30
parent 492b80e116
commit 3eb715e2de
68 changed files with 996 additions and 353 deletions

View File

@ -2,9 +2,9 @@ import { Component, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { MatPaginator } from '@angular/material/paginator';
import { MatSort } from '@angular/material/sort';
import { ActivatedRoute } from '@angular/router';
import * as moment from 'moment';
import { ActivatedRoute, Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { Period } from 'src/app/period/period';
import { ProductGroup } from '../../core/product-group';
import { Recipe } from '../recipe';
@ -20,39 +20,38 @@ export class RecipeListComponent implements OnInit {
@ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator;
@ViewChild(MatSort, { static: true }) sort?: MatSort;
form: FormGroup<{
validFrom: FormControl<Date>;
validTill: FormControl<Date>;
period: FormControl<Period>;
productGroup: FormControl<ProductGroup | string | null>;
}>;
productGroups: ProductGroup[] = [];
validFromFilter: BehaviorSubject<Date | null> = new BehaviorSubject<Date | null>(null);
validTillFilter: BehaviorSubject<Date | null> = new BehaviorSubject<Date | null>(null);
periods: Period[] = [];
periodFilter: BehaviorSubject<Period> = new BehaviorSubject<Period>(new Period());
productGroupFilter: BehaviorSubject<string> = new BehaviorSubject('');
list: Recipe[] = [];
data: BehaviorSubject<Recipe[]> = new BehaviorSubject<Recipe[]>([]);
dataSource: RecipeListDatasource = new RecipeListDatasource(
this.validFromFilter,
this.validTillFilter,
this.productGroupFilter,
this.data,
);
dataSource: RecipeListDatasource = new RecipeListDatasource(this.productGroupFilter, this.data);
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['name', 'validity', 'salePrice', 'costPrice', 'costPercentage'];
displayedColumns = ['name', 'salePrice', 'costPrice', 'costPercentage'];
constructor(private route: ActivatedRoute) {
constructor(private route: ActivatedRoute, private router: Router) {
this.form = new FormGroup({
validFrom: new FormControl(new Date(), { nonNullable: true }),
validTill: new FormControl(new Date(), { nonNullable: true }),
period: new FormControl(new Period(), { nonNullable: true }),
productGroup: new FormControl<ProductGroup | string | null>(null),
});
// Listen to Payment Account Change
this.form.controls.period.valueChanges.subscribe((x) =>
this.router.navigate([], {
relativeTo: this.route,
queryParams: { p: x.id },
replaceUrl: true,
}),
);
}
ngOnInit() {
this.dataSource = new RecipeListDatasource(
this.validFromFilter,
this.validTillFilter,
this.productGroupFilter,
this.data,
this.paginator,
@ -60,25 +59,15 @@ export class RecipeListComponent implements OnInit {
);
// this.dataSource = new RecipeListDatasource(this.validFromFilter, this.validTillFilter, this.productGroupFilter, this.data);
this.route.data.subscribe((value) => {
const data = value as { list: Recipe[]; productGroups: ProductGroup[] };
const vf = data.list
.map((x) => x.validFrom)
.reduce((p, c) => {
const pdate = moment(p, 'DD-MMM-YYYY').toDate();
const cdate = moment(c, 'DD-MMM-YYYY').toDate();
return pdate < cdate ? p : c;
});
const vt = data.list
.map((x) => x.validTill)
.reduce((p, c) => {
const pdate = moment(p, 'DD-MMM-YYYY').toDate();
const cdate = moment(c, 'DD-MMM-YYYY').toDate();
return pdate > cdate ? p : c;
});
const data = value as { list: Recipe[]; productGroups: ProductGroup[]; periods: Period[] };
this.productGroups = data.productGroups;
this.periods = data.periods;
const period =
this.periods.find((x) => x.id === this.route.snapshot.queryParamMap.get('p')) ||
this.periods[0];
this.form.setValue({
validFrom: vf === null ? new Date() : moment(vf, 'DD-MMM-YYYY').toDate(),
validTill: vt === null ? new Date() : moment(vt, 'DD-MMM-YYYY').toDate(),
period: period,
productGroup: '',
});
this.data.next(data.list);
@ -88,12 +77,4 @@ export class RecipeListComponent implements OnInit {
filterProductGroup(val: string) {
this.productGroupFilter.next(val || '');
}
filterValidFrom(val: Date) {
this.validFromFilter.next(val);
}
filterValidTill(val: Date) {
this.validTillFilter.next(val);
}
}