230 lines
7.0 KiB
TypeScript
230 lines
7.0 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>;
|
|
}>;
|
|
|
|
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),
|
|
});
|
|
}
|
|
|
|
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) {
|
|
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 ?? '' : '',
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.nameElement) {
|
|
this.nameElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
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;
|
|
if (this.item.productGroup === null || this.item.productGroup === undefined) {
|
|
this.item.productGroup = new ProductGroup();
|
|
}
|
|
this.item.productGroup.id = formModel.productGroup ?? '';
|
|
return this.item;
|
|
}
|
|
}
|