Feature: Adding recipe templates to print recipes.

Feautre: Recipe export to xlsx
Chore: Python 11 style type annotations
Chore: Moved to sqlalchemy 2.0
Chore: Minimum python is 3.11
Fix: Fix nullability of a lot of fields in the database.
This commit is contained in:
2023-07-23 08:12:21 +05:30
parent d2d26ab1ae
commit 22cac61761
344 changed files with 3247 additions and 2370 deletions
@@ -5,12 +5,22 @@
<mat-card-content>
<form [formGroup]="form" class="flex flex-col">
<div class="flex flex-row justify-around content-start items-start">
<mat-form-field class="flex-auto mr-5">
<mat-label>Date</mat-label>
<input
matInput
[matDatepicker]="date"
formControlName="date"
autocomplete="off"
#dateElement
(focus)="dateElement.select()"
/>
<mat-datepicker-toggle matSuffix [for]="date"></mat-datepicker-toggle>
<mat-datepicker #date></mat-datepicker>
</mat-form-field>
<mat-form-field class="flex-auto">
<mat-select formControlName="period">
<mat-option *ngFor="let p of periods" [value]="p">
{{ p.validFrom }} to {{ p.validTill }}
</mat-option>
</mat-select>
<mat-label>Source</mat-label>
<input type="text" matInput formControlName="source" autocomplete="off" />
</mat-form-field>
</div>
<div class="flex flex-row justify-around content-start items-start sm:max-lg:flex-col">
@@ -39,27 +49,12 @@
<mat-label>Yield</mat-label>
<input type="text" matInput formControlName="recipeYield" autocomplete="off" />
</mat-form-field>
<mat-form-field class="flex-auto basis-1/10 mr-5">
<mat-label>Sale Price</mat-label>
<input type="text" matInput formControlName="salePrice" autocomplete="off" />
<span matTextPrefix>&nbsp;</span>
</mat-form-field>
<mat-form-field class="flex-auto basis-1/10 mr-5">
<mat-label>Cost Price</mat-label>
<input type="text" matInput formControlName="costPrice" autocomplete="off" />
<span matTextPrefix>&nbsp;</span>
</mat-form-field>
<mat-form-field class="flex-auto basis-1/10">
<mat-label>Cost Percentage</mat-label>
<input type="text" matInput formControlName="costPercentage" autocomplete="off" />
<span matSuffix>%</span>
</mat-form-field>
</div>
<div
formGroupName="addRow"
class="flex flex-row justify-around content-start items-start sm:max-lg:flex-col"
>
<mat-form-field class="flex-auto basis-1/2 mr-5">
<mat-form-field class="flex-auto basis-2/5 mr-5">
<mat-label>Ingredient</mat-label>
<input
type="text"
@@ -85,6 +80,10 @@
<input type="text" matInput formControlName="quantity" autocomplete="off" />
</mat-form-field>
<mat-form-field class="flex-auto basis-1/5 mr-5">
<mat-label>Description</mat-label>
<input type="text" matInput formControlName="description" autocomplete="off" />
</mat-form-field>
<mat-form-field class="flex-auto basis-1/10 mr-5">
<mat-label>Rate</mat-label>
<input type="text" matInput formControlName="rate" autocomplete="off" />
<span matTextPrefix>&nbsp;</span>
@@ -98,7 +97,7 @@
<!-- Ingredient Column -->
<ng-container matColumnDef="product">
<mat-header-cell *matHeaderCellDef>Product</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.product.name }}</mat-cell>
<mat-cell *matCellDef="let row">{{ row.product.name }} {{ row.description }}</mat-cell>
</ng-container>
<!-- Quantity Column -->
@@ -109,22 +108,6 @@
>
</ng-container>
<!-- Rate Column -->
<ng-container matColumnDef="rate">
<mat-header-cell *matHeaderCellDef class="right">Rate</mat-header-cell>
<mat-cell *matCellDef="let row" class="right">{{
row.price | currency : 'INR'
}}</mat-cell>
</ng-container>
<!-- Amount Column -->
<ng-container matColumnDef="amount">
<mat-header-cell *matHeaderCellDef class="right">Amount</mat-header-cell>
<mat-cell *matCellDef="let row" class="right">{{
row.quantity * row.price | currency : 'INR'
}}</mat-cell>
</ng-container>
<!-- Action Column -->
<ng-container matColumnDef="action">
<mat-header-cell *matHeaderCellDef class="center">Action</mat-header-cell>
@@ -138,6 +121,18 @@
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
<mat-row *matRowDef="let row; columns: displayedColumns"></mat-row>
</mat-table>
<mat-form-field class="flex-auto">
<mat-label>Instructions</mat-label>
<textarea matInput matAutosizeMinRows="5" formControlName="instructions"></textarea>
</mat-form-field>
<mat-form-field class="flex-auto">
<mat-label>Garnishing</mat-label>
<textarea matInput matAutosizeMinRows="5" formControlName="garnishing"></textarea>
</mat-form-field>
<mat-form-field class="flex-auto">
<mat-label>Plating</mat-label>
<textarea matInput matAutosizeMinRows="5" formControlName="plating"></textarea>
</mat-form-field>
</form>
</mat-card-content>
<mat-card-actions>
@@ -3,7 +3,7 @@ import { FormControl, FormGroup } from '@angular/forms';
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';
@@ -31,17 +31,19 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
public itemsObservable = new BehaviorSubject<RecipeItem[]>([]);
dataSource: RecipeDetailDatasource = new RecipeDetailDatasource(this.itemsObservable);
form: FormGroup<{
period: FormControl<Period>;
date: FormControl<Date>;
source: FormControl<string>;
recipeYield: FormControl<string | null>;
costPrice: FormControl<string | null>;
salePrice: FormControl<string | null>;
costPercentage: FormControl<number | null>;
product: FormControl<ProductSku | string | null>;
addRow: FormGroup<{
ingredient: FormControl<string | null>;
quantity: FormControl<string>;
description: FormControl<string>;
rate: FormControl<string>;
}>;
instructions: FormControl<string>;
garnishing: FormControl<string>;
plating: FormControl<string>;
}>;
periods: Period[] = [];
@@ -51,7 +53,7 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
ingredients: Observable<ProductSku[]>;
item: Recipe = new Recipe();
displayedColumns = ['product', 'quantity', 'rate', 'amount', 'action'];
displayedColumns = ['product', 'quantity', 'action'];
constructor(
private route: ActivatedRoute,
@@ -65,17 +67,19 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
this.product = null;
this.ingredient = null;
this.form = new FormGroup({
period: new FormControl(new Period(), { nonNullable: true }),
date: new FormControl(new Date(), { nonNullable: true }),
source: new FormControl('', { nonNullable: true }),
recipeYield: new FormControl<string | null>(null),
costPrice: new FormControl<string | null>(null),
salePrice: new FormControl<string | null>(null),
costPercentage: new FormControl<number | null>(null),
product: new FormControl<ProductSku | string | null>(new ProductSku()),
addRow: new FormGroup({
ingredient: new FormControl(''),
quantity: new FormControl('', { nonNullable: true }),
description: new FormControl('', { nonNullable: true }),
rate: new FormControl('', { nonNullable: true }),
}),
instructions: new FormControl('', { nonNullable: true }),
garnishing: new FormControl('', { nonNullable: true }),
plating: new FormControl('', { nonNullable: true }),
});
// Setup Product Autocomplete
this.products = this.form.controls.product.valueChanges.pipe(
@@ -107,19 +111,20 @@ 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({
period: item.period,
date: moment(item.date, 'DD-MMM-YYYY').toDate(),
source: item.source,
recipeYield: `${item.recipeYield}`,
salePrice: `${item.salePrice}`,
costPrice: `${item.costPrice}`,
costPercentage: null,
product: item.sku,
addRow: {
ingredient: null,
quantity: '',
description: '',
rate: '',
},
instructions: item.instructions,
garnishing: item.garnishing,
plating: item.plating,
});
this.dataSource = new RecipeDetailDatasource(this.itemsObservable);
}
@@ -144,16 +149,11 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
const product: ProductSku = event.option.value;
this.item.sku = product;
this.form.controls.product.setValue(product);
this.form.controls.salePrice.setValue('' + (product.salePrice ?? 0));
}
ingredientSelected(event: MatAutocompleteSelectedEvent): void {
const ingredient: ProductSku = event.option.value;
this.ingredient = ingredient;
const item = this.getItem();
this.ser
.getIngredientDetails(ingredient.id, item.period.validFrom, item.period.validTill)
.subscribe((x) => this.form.controls.addRow.controls.rate.setValue('' + x.costPrice));
}
addRow() {
@@ -177,7 +177,7 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
new RecipeItem({
product: this.ingredient,
quantity,
price: rate,
description: formValue.description ?? '',
}),
);
this.resetAddRow();
@@ -196,17 +196,6 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
updateView() {
this.itemsObservable.next(this.item.items);
const costPrice = round(
this.item.items.map((x) => x.quantity * x.price).reduce((p, c) => p + c, 0),
2,
);
this.form.controls.costPrice.setValue(`${costPrice}`);
const salePrice = this.math.parseAmount(this.form.value.salePrice ?? '0', 2);
if (salePrice < 0) {
return;
}
const costPercentage = round((100 * costPrice) / salePrice, 2);
this.form.controls.costPercentage.setValue(costPercentage);
}
deleteRow(row: RecipeItem) {
@@ -253,12 +242,12 @@ export class RecipeDetailComponent implements OnInit, AfterViewInit {
getItem(): Recipe {
const formModel = this.form.value;
if (formModel.period) {
this.item.period = formModel.period;
}
this.item.date = moment(formModel.date).format('DD-MMM-YYYY');
this.item.source = formModel.source ?? '';
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);
this.item.instructions = formModel.instructions ?? '';
this.item.garnishing = formModel.garnishing ?? '';
this.item.plating = formModel.plating ?? '';
return this.item;
}
}
+2 -2
View File
@@ -4,12 +4,12 @@ export class RecipeItem {
id: string | undefined;
product: ProductSku;
quantity: number;
price: number;
description: string;
public constructor(init?: Partial<RecipeItem>) {
this.product = new ProductSku();
this.quantity = 0;
this.price = 0;
this.description = '';
Object.assign(this, init);
}
}
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs/internal/Observable';
import { Recipe } from './recipe';
@@ -8,7 +8,7 @@ import { RecipeService } from './recipe.service';
@Injectable({
providedIn: 'root',
})
export class RecipeListResolver implements Resolve<Recipe[]> {
export class RecipeListResolver {
constructor(private ser: RecipeService) {}
resolve(route: ActivatedRouteSnapshot): Observable<Recipe[]> {
@@ -2,7 +2,6 @@ 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';
@@ -27,23 +26,17 @@ export class RecipeListDatasource extends DataSource<Recipe> {
}
connect(): Observable<Recipe[]> {
const dataMutations: (
| Observable<Recipe[]>
| Observable<string>
| EventEmitter<PageEvent>
| EventEmitter<Sort>
)[] = [
this.dataObs.pipe(
tap((x) => {
this.data = x;
}),
),
this.productGroupFilter.pipe(
tap((x) => {
this.productGroup = x;
}),
),
];
const dataMutations: (EventEmitter<PageEvent> | EventEmitter<Sort>)[] = [];
const d = this.dataObs.pipe(
tap((x) => {
this.data = x;
}),
);
const pg = this.productGroupFilter.pipe(
tap((x) => {
this.productGroup = x;
}),
);
if (this.paginator) {
dataMutations.push((this.paginator as MatPaginator).page);
}
@@ -51,7 +44,7 @@ export class RecipeListDatasource extends DataSource<Recipe> {
dataMutations.push((this.sort as MatSort).sortChange);
}
return merge(...dataMutations).pipe(
return merge(d, pg, ...dataMutations).pipe(
map(() => this.getFilteredData(this.data, this.productGroup)),
tap((x: Recipe[]) => {
if (this.paginator) {
@@ -68,7 +61,7 @@ export class RecipeListDatasource extends DataSource<Recipe> {
disconnect() {}
private getFilteredData(data: Recipe[], productGroup: string): Recipe[] {
return data.filter((x: Recipe) => productGroup === '' || x.notes === productGroup);
return data.filter((x: Recipe) => productGroup === '' || x.productGroupId === productGroup);
}
private getPagedData(data: Recipe[]) {
@@ -93,12 +86,6 @@ export class RecipeListDatasource extends DataSource<Recipe> {
switch (sort.active) {
case 'name':
return compare(a.sku.name, b.sku.name, 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;
}
@@ -108,7 +95,6 @@ export class RecipeListDatasource extends DataSource<Recipe> {
/** 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);
// 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);
@@ -2,6 +2,9 @@
<mat-card-header>
<mat-card-title-group>
<mat-card-title>Recipes</mat-card-title>
<a mat-icon-button href="{{ excelLink() }}">
<mat-icon>save_alt</mat-icon>
</a>
<a mat-button [routerLink]="['/recipes', 'new']">
<mat-icon>add_box</mat-icon>
Add
@@ -40,25 +43,22 @@
><a [routerLink]="['/recipes', row.id]">{{ row.sku.name }}</a></mat-cell
>
</ng-container>
<!-- Sale Price Column -->
<ng-container matColumnDef="salePrice">
<mat-header-cell *matHeaderCellDef mat-sort-header>Sale Price</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.salePrice | currency : 'INR' }}</mat-cell>
<!-- Yield Column -->
<ng-container matColumnDef="yield">
<mat-header-cell *matHeaderCellDef mat-sort-header>Yield</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.recipeYield }} {{ row.units }}</mat-cell>
</ng-container>
<!-- Cost Price Column -->
<ng-container matColumnDef="costPrice">
<mat-header-cell *matHeaderCellDef mat-sort-header>Cost Price</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.costPrice | currency : 'INR' }}</mat-cell>
<!-- Date Column -->
<ng-container matColumnDef="date">
<mat-header-cell *matHeaderCellDef mat-sort-header>Date</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.date }}</mat-cell>
</ng-container>
<!-- Cost Percentage Column -->
<ng-container matColumnDef="costPercentage">
<mat-header-cell *matHeaderCellDef mat-sort-header>Cost %age</mat-header-cell>
<mat-cell *matCellDef="let row">{{
row.costPrice / row.salePrice | percent : '1.2-2'
}}</mat-cell>
<!-- Source Column -->
<ng-container matColumnDef="source">
<mat-header-cell *matHeaderCellDef mat-sort-header>Source</mat-header-cell>
<mat-cell *matCellDef="let row">{{ row.source }}</mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
@@ -33,7 +33,7 @@ export class RecipeListComponent implements OnInit {
dataSource: RecipeListDatasource = new RecipeListDatasource(this.productGroupFilter, this.data);
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['name', 'salePrice', 'costPrice', 'costPercentage'];
displayedColumns = ['name', 'yield', 'date', 'source'];
constructor(private route: ActivatedRoute, private router: Router) {
this.form = new FormGroup({
@@ -77,4 +77,8 @@ export class RecipeListComponent implements OnInit {
filterProductGroup(val: string) {
this.productGroupFilter.next(val || '');
}
excelLink() {
return `/api/recipes/xlsx`;
}
}
@@ -1,5 +1,5 @@
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, Resolve } from '@angular/router';
import { ActivatedRouteSnapshot } from '@angular/router';
import { Observable } from 'rxjs/internal/Observable';
import { Recipe } from './recipe';
@@ -8,7 +8,7 @@ import { RecipeService } from './recipe.service';
@Injectable({
providedIn: 'root',
})
export class RecipeResolver implements Resolve<Recipe> {
export class RecipeResolver {
constructor(private ser: RecipeService) {}
resolve(route: ActivatedRouteSnapshot): Observable<Recipe> {
@@ -1,53 +1,62 @@
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { RouterModule, Routes } from '@angular/router';
import { NgModule, inject } from '@angular/core';
import { ActivatedRouteSnapshot, RouterModule, RouterStateSnapshot, Routes } from '@angular/router';
import { AuthGuard } from '../auth/auth-guard.service';
import { PeriodListResolver } from '../period/period-list-resolver.service';
import { ProductGroupListResolver } from '../product-group/product-group-list-resolver.service';
import { RecipeDetailComponent } from './recipe-detail/recipe-detail.component';
import { RecipeListResolver } from './recipe-list-resolver.service';
import { RecipeListComponent } from './recipe-list/recipe-list.component';
import { RecipeListResolver } from './recipe-list-resolver.service';
import { RecipeResolver } from './recipe-resolver.service';
const recipeRoutes: Routes = [
{
path: '',
component: RecipeListComponent,
canActivate: [AuthGuard],
canActivate: [
(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) =>
inject(AuthGuard).canActivate(route, state),
],
data: {
permission: 'Recipes',
},
resolve: {
list: RecipeListResolver,
productGroups: ProductGroupListResolver,
periods: PeriodListResolver,
list: (route: ActivatedRouteSnapshot) => inject(RecipeListResolver).resolve(route),
productGroups: () => inject(ProductGroupListResolver).resolve(),
periods: () => inject(PeriodListResolver).resolve(),
},
runGuardsAndResolvers: 'paramsOrQueryParamsChange',
},
{
path: 'new',
component: RecipeDetailComponent,
canActivate: [AuthGuard],
canActivate: [
(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) =>
inject(AuthGuard).canActivate(route, state),
],
data: {
permission: 'Recipes',
},
resolve: {
item: RecipeResolver,
periods: PeriodListResolver,
item: (route: ActivatedRouteSnapshot) => inject(RecipeResolver).resolve(route),
periods: () => inject(PeriodListResolver).resolve(),
},
},
{
path: ':id',
component: RecipeDetailComponent,
canActivate: [AuthGuard],
canActivate: [
(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) =>
inject(AuthGuard).canActivate(route, state),
],
data: {
permission: 'Recipes',
},
resolve: {
item: RecipeResolver,
periods: PeriodListResolver,
item: (route: ActivatedRouteSnapshot) => inject(RecipeResolver).resolve(route),
periods: () => inject(PeriodListResolver).resolve(),
},
},
];
+1 -1
View File
@@ -2,7 +2,6 @@ import { CdkTableModule } from '@angular/cdk/table';
import { CommonModule } from '@angular/common';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { MomentDateAdapter } from '@angular/material-moment-adapter';
import { MatAutocompleteModule } from '@angular/material/autocomplete';
import { MatButtonModule } from '@angular/material/button';
import { MatCardModule } from '@angular/material/card';
@@ -17,6 +16,7 @@ import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { MatSelectModule } from '@angular/material/select';
import { MatSortModule } from '@angular/material/sort';
import { MatTableModule } from '@angular/material/table';
import { MomentDateAdapter } from '@angular/material-moment-adapter';
import { SharedModule } from '../shared/shared.module';
-23
View File
@@ -4,7 +4,6 @@ import { Observable, of as observableOf } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ErrorLoggerService } from '../core/error-logger.service';
import { ProductSku } from '../core/product-sku';
import { Recipe } from './recipe';
@@ -55,26 +54,4 @@ export class RecipeService {
.delete<Recipe>(`${url}/${id}`)
.pipe(catchError(this.log.handleError(serviceName, 'delete'))) as Observable<Recipe>;
}
getIngredientDetails(
id: string,
startDate: string | null,
finishDate: string | null,
): Observable<ProductSku> {
const getUrl = `${url}/ingredient-details/${id}`;
const options = {
params: new HttpParams(),
};
if (startDate !== null) {
options.params = options.params.set('s', startDate);
}
if (finishDate !== null) {
options.params = options.params.set('f', finishDate);
}
return this.http
.get<ProductSku>(getUrl, options)
.pipe(
catchError(this.log.handleError(serviceName, `get id=${id}`)),
) as Observable<ProductSku>;
}
}
+12 -7
View File
@@ -1,26 +1,31 @@
import { ProductSku } from '../core/product-sku';
import { Period } from '../period/period';
import { RecipeItem } from './recipe-item';
export class Recipe {
id: string | undefined;
date: string;
source: string;
instructions: string;
garnishing: string;
plating: string;
sku: ProductSku;
recipeYield: number;
costPrice: number;
salePrice: number;
notes: string;
items: RecipeItem[];
period: Period;
units: string | undefined;
productGroupId: string | undefined;
public constructor(init?: Partial<Recipe>) {
this.date = '';
this.source = '';
this.instructions = '';
this.garnishing = '';
this.plating = '';
this.sku = new ProductSku();
this.recipeYield = 0;
this.salePrice = 0;
this.costPrice = 0;
this.notes = '';
this.items = [];
this.period = new Period();
Object.assign(this, init);
}
}