brewman/overlord/src/app/product/product-detail/product-detail.component.ts

292 lines
9.5 KiB
TypeScript

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { ActivatedRoute, Router } from '@angular/router';
import { BehaviorSubject } from 'rxjs';
import { Product, StockKeepingUnit } from '../../core/product';
import { ProductGroup } from '../../core/product-group';
import { ToasterService } from '../../core/toaster.service';
import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.component';
import { ProductService } from '../product.service';
import { ProductDetailDatasource } from './product-detail-datasource';
import { ProductDetailDialogComponent } from './product-detail-dialog.component';
@Component({
selector: 'app-product-detail',
templateUrl: './product-detail.component.html',
styleUrls: ['./product-detail.component.css'],
})
export class ProductDetailComponent implements OnInit, AfterViewInit {
@ViewChild('nameElement', { static: true }) nameElement?: ElementRef;
form: FormGroup<{
code: FormControl<string | number>;
name: FormControl<string | null>;
fractionUnits: FormControl<string | null>;
addRow: FormGroup<{
units: FormControl<string | null>;
fraction: FormControl<string | null>;
productYield: FormControl<string | null>;
costPrice: FormControl<string | null>;
salePrice: FormControl<string | null>;
}>;
isPurchased: FormControl<boolean>;
isSold: FormControl<boolean>;
isActive: FormControl<boolean>;
productGroup: FormControl<string | null>;
protein: FormControl<number>;
carbohydrate: FormControl<number>;
totalSugar: FormControl<number>;
addedSugar: FormControl<number>;
totalFat: FormControl<number>;
saturatedFat: FormControl<number>;
transFat: FormControl<number>;
cholestrol: FormControl<number>;
sodium: FormControl<number>;
msnf: FormControl<number>;
otherSolids: FormControl<number>;
totalSolids: FormControl<number>;
water: FormControl<number>;
}>;
productGroups: ProductGroup[] = [];
public skus = new BehaviorSubject<StockKeepingUnit[]>([]);
dataSource: ProductDetailDatasource = new ProductDetailDatasource(this.skus);
item: Product = new Product();
displayedColumns = ['units', 'fraction', 'yield', 'costPrice', 'salePrice', 'action'];
constructor(
private route: ActivatedRoute,
private router: Router,
private dialog: MatDialog,
private toaster: ToasterService,
private ser: ProductService,
) {
this.form = new FormGroup({
code: new FormControl<string | number>({ value: 0, disabled: true }, { nonNullable: true }),
name: new FormControl<string | null>(null),
fractionUnits: new FormControl<string | null>(null),
addRow: new FormGroup({
units: new FormControl<string | null>(null),
fraction: new FormControl<string | null>(null),
productYield: new FormControl<string | null>(null),
costPrice: new FormControl<string | null>(null),
salePrice: new FormControl<string | null>(null),
}),
isPurchased: new FormControl<boolean>(true, { nonNullable: true }),
isSold: new FormControl<boolean>(true, { nonNullable: true }),
isActive: new FormControl<boolean>(true, { nonNullable: true }),
productGroup: new FormControl<string | null>(null),
protein: new FormControl<number>(0, { nonNullable: true }),
carbohydrate: new FormControl<number>(0, { nonNullable: true }),
totalSugar: new FormControl<number>(0, { nonNullable: true }),
addedSugar: new FormControl<number>(0, { nonNullable: true }),
totalFat: new FormControl<number>(0, { nonNullable: true }),
saturatedFat: new FormControl<number>(0, { nonNullable: true }),
transFat: new FormControl<number>(0, { nonNullable: true }),
cholestrol: new FormControl<number>(0, { nonNullable: true }),
sodium: new FormControl<number>(0, { nonNullable: true }),
msnf: new FormControl<number>(0, { nonNullable: true }),
otherSolids: new FormControl<number>(0, { nonNullable: true }),
totalSolids: new FormControl<number>(0, { nonNullable: true }),
water: new FormControl<number>(0, { nonNullable: true }),
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { item: Product; productGroups: ProductGroup[] };
this.productGroups = data.productGroups;
this.showItem(data.item);
});
this.dataSource = new ProductDetailDatasource(this.skus);
this.skus.next(this.item.skus);
}
showItem(item: Product) {
item.productGroup = this.productGroups.find((x) => x.id === item.productGroup?.id);
this.item = item;
this.form.setValue({
code: this.item.code || '(Auto)',
name: this.item.name,
fractionUnits: this.item.fractionUnits ?? '',
addRow: {
units: '',
fraction: '',
productYield: '',
costPrice: '',
salePrice: '',
},
isPurchased: this.item.isPurchased,
isSold: this.item.isSold,
isActive: this.item.isActive,
productGroup: this.item.productGroup ? this.item.productGroup.id ?? '' : '',
protein: this.item.protein,
carbohydrate: this.item.carbohydrate,
totalSugar: this.item.totalSugar,
addedSugar: this.item.addedSugar,
totalFat: this.item.totalFat,
saturatedFat: this.item.saturatedFat,
transFat: this.item.transFat,
cholestrol: this.item.cholestrol,
sodium: this.item.sodium,
msnf: this.item.msnf,
otherSolids: this.item.otherSolids,
totalSolids: this.item.totalSolids,
water: this.item.water,
});
}
ngAfterViewInit() {
setTimeout(() => {
if (this.nameElement) {
this.nameElement.nativeElement.focus();
}
}, 0);
}
updatePG(id: string) {
this.item.productGroup = this.productGroups.find((x) => x.id == id);
}
addRow() {
const formValue = this.form.value.addRow;
if (formValue === undefined) {
return;
}
const fraction = Number(formValue.fraction);
if (fraction < 1) {
this.toaster.show('Danger', 'Fraction has to be >= 1');
return;
}
const productYield = Number(formValue.productYield);
if (productYield < 0 || productYield > 1) {
this.toaster.show('Danger', 'Product Yield has to be > 0 and <= 1');
return;
}
const costPrice = Number(formValue.costPrice);
if (costPrice < 0) {
this.toaster.show('Danger', 'Price has to be >= 0');
return;
}
const salePrice = Number(formValue.salePrice);
if (salePrice < 0) {
this.toaster.show('Danger', 'Sale Price has to be >= 0');
return;
}
this.item.skus.push(
new StockKeepingUnit({
units: formValue.units ?? '',
fraction,
productYield,
costPrice,
salePrice,
}),
);
this.skus.next(this.item.skus);
this.resetAddRow();
}
resetAddRow() {
this.form.controls.addRow.reset();
}
editRow(row: StockKeepingUnit) {
const dialogRef = this.dialog.open(ProductDetailDialogComponent, {
width: '750px',
data: {
item: { ...row },
isSold: this.item.isSold,
isPurchased: this.item.isPurchased,
},
});
dialogRef.afterClosed().subscribe((result: boolean | StockKeepingUnit) => {
if (!result) {
return;
}
const j = result as StockKeepingUnit;
Object.assign(row, j);
this.skus.next(this.item.skus);
this.resetAddRow();
});
}
deleteRow(row: StockKeepingUnit) {
this.item.skus.splice(this.item.skus.indexOf(row), 1);
this.skus.next(this.item.skus);
}
save() {
this.ser.saveOrUpdate(this.getItem()).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/products');
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
delete() {
this.ser.delete(this.item.id as string).subscribe(
() => {
this.toaster.show('Success', '');
this.router.navigateByUrl('/products');
},
(error) => {
this.toaster.show('Danger', error);
},
);
}
confirmDelete(): void {
const dialogRef = this.dialog.open(ConfirmDialogComponent, {
width: '250px',
data: { title: 'Delete Product?', content: 'Are you sure? This cannot be undone.' },
});
dialogRef.afterClosed().subscribe((result: boolean) => {
if (result) {
this.delete();
}
});
}
getItem(): Product {
const formModel = this.form.value;
this.item.name = formModel.name ?? '';
this.item.fractionUnits = formModel.fractionUnits ?? '';
this.item.isPurchased = formModel.isPurchased ?? true;
this.item.isSold = formModel.isSold ?? false;
this.item.isActive = formModel.isActive ?? true;
this.item.protein = formModel.protein ?? 0;
this.item.carbohydrate = formModel.carbohydrate ?? 0;
this.item.totalSugar = formModel.totalSugar ?? 0;
this.item.addedSugar = formModel.addedSugar ?? 0;
this.item.totalFat = formModel.totalFat ?? 0;
this.item.saturatedFat = formModel.saturatedFat ?? 0;
this.item.transFat = formModel.transFat ?? 0;
this.item.cholestrol = formModel.cholestrol ?? 0;
this.item.sodium = formModel.sodium ?? 0;
this.item.msnf = formModel.msnf ?? 0;
this.item.otherSolids = formModel.otherSolids ?? 0;
this.item.totalSolids = formModel.totalSolids ?? 0;
this.item.water = formModel.water ?? 0;
return this.item;
}
}