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:
@@ -4,9 +4,9 @@ import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
import { round } from 'mathjs';
|
||||
import * as moment from 'moment';
|
||||
import { BehaviorSubject, Observable, of as observableOf } from 'rxjs';
|
||||
import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators';
|
||||
import { Period } from 'src/app/period/period';
|
||||
|
||||
import { Product } from '../../core/product';
|
||||
import { ProductSku } from '../../core/product-sku';
|
||||
@@ -31,8 +31,7 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
|
||||
public itemsObservable = new BehaviorSubject<RecipeItem[]>([]);
|
||||
dataSource: RecipeDetailDatasource = new RecipeDetailDatasource(this.itemsObservable);
|
||||
form: FormGroup<{
|
||||
validFrom: FormControl<Date>;
|
||||
validTill: FormControl<Date>;
|
||||
period: FormControl<Period>;
|
||||
recipeYield: FormControl<string | null>;
|
||||
costPrice: FormControl<string | null>;
|
||||
salePrice: FormControl<string | null>;
|
||||
@@ -45,6 +44,7 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
|
||||
}>;
|
||||
}>;
|
||||
|
||||
periods: Period[] = [];
|
||||
product: ProductSku | null;
|
||||
products: Observable<ProductSku[]>;
|
||||
ingredient: ProductSku | null;
|
||||
@@ -65,8 +65,7 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
|
||||
this.product = null;
|
||||
this.ingredient = null;
|
||||
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 }),
|
||||
recipeYield: new FormControl<string | null>(null),
|
||||
costPrice: new FormControl<string | null>(null),
|
||||
salePrice: new FormControl<string | null>(null),
|
||||
@@ -99,7 +98,8 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
|
||||
|
||||
ngOnInit() {
|
||||
this.route.data.subscribe((value) => {
|
||||
const data = value as { item: Recipe };
|
||||
const data = value as { item: Recipe; periods: Period[] };
|
||||
this.periods = data.periods;
|
||||
this.showItem(data.item);
|
||||
this.updateView();
|
||||
});
|
||||
@@ -107,9 +107,9 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
|
||||
|
||||
showItem(item: Recipe) {
|
||||
this.item = item;
|
||||
item.period = this.periods.find((x) => x.id == item.period.id) as Period;
|
||||
this.form.setValue({
|
||||
validFrom: moment(item.validFrom, 'DD-MMM-YYYY').toDate(),
|
||||
validTill: moment(item.validTill, 'DD-MMM-YYYY').toDate(),
|
||||
period: item.period,
|
||||
recipeYield: `${item.recipeYield}`,
|
||||
salePrice: `${item.salePrice}`,
|
||||
costPrice: `${item.costPrice}`,
|
||||
@@ -152,7 +152,7 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
|
||||
this.ingredient = ingredient;
|
||||
const item = this.getItem();
|
||||
this.ser
|
||||
.getIngredientDetails(ingredient.id, item.validFrom, item.validTill)
|
||||
.getIngredientDetails(ingredient.id, item.period.validFrom, item.period.validTill)
|
||||
.subscribe((x) => this.form.controls.addRow.controls.rate.setValue('' + x.costPrice));
|
||||
}
|
||||
|
||||
@@ -253,8 +253,9 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
|
||||
|
||||
getItem(): Recipe {
|
||||
const formModel = this.form.value;
|
||||
this.item.validFrom = moment(formModel.validFrom).format('DD-MMM-YYYY');
|
||||
this.item.validTill = moment(formModel.validTill).format('DD-MMM-YYYY');
|
||||
if (formModel.period) {
|
||||
this.item.period = formModel.period;
|
||||
}
|
||||
this.item.recipeYield = this.math.parseAmount(formModel.recipeYield ?? '1', 2);
|
||||
this.item.salePrice = this.math.parseAmount(formModel.salePrice ?? '0', 2);
|
||||
this.item.costPrice = this.math.parseAmount(formModel.costPrice ?? '0', 2);
|
||||
|
||||
Reference in New Issue
Block a user