Files
brewman/overlord/src/app/purchase/purchase-dialog.component.ts
T
tanshu bddd5ec749 Fix: Autocomplete could fuck up and was definitely doing it in product.
When checking for .name, it would error out if the input was null

Fix: Journal addRow reset would reset Debit/Credit
2022-07-17 14:31:19 +05:30

106 lines
3.9 KiB
TypeScript

import { Component, Inject, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
import { round } from 'mathjs';
import { Observable, of as observableOf } from 'rxjs';
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
import { Batch } from '../core/batch';
import { Inventory } from '../core/inventory';
import { Product } from '../core/product';
import { ProductSku } from '../core/product-sku';
import { ProductService } from '../product/product.service';
import { MathService } from '../shared/math.service';
@Component({
selector: 'app-purchase-dialog',
templateUrl: './purchase-dialog.component.html',
styleUrls: ['./purchase-dialog.component.css'],
})
export class PurchaseDialogComponent implements OnInit {
products: Observable<ProductSku[]>;
form: FormGroup<{
product: FormControl<string | null>;
quantity: FormControl<string>;
price: FormControl<string>;
tax: FormControl<string>;
discount: FormControl<string>;
}>;
product: ProductSku = new ProductSku();
constructor(
public dialogRef: MatDialogRef<PurchaseDialogComponent>,
@Inject(MAT_DIALOG_DATA) public data: { inventory: Inventory },
private math: MathService,
private productSer: ProductService,
) {
this.form = new FormGroup({
product: new FormControl<string | null>(null),
quantity: new FormControl('', { nonNullable: true }),
price: new FormControl('', { nonNullable: true }),
tax: new FormControl('', { nonNullable: true }),
discount: new FormControl('', { nonNullable: true }),
});
// Listen to Product Autocomplete Change
this.products = this.form.controls.product.valueChanges.pipe(
debounceTime(150),
distinctUntilChanged(),
switchMap((x) => (x === null ? observableOf([]) : this.productSer.autocompleteSku(x, true))),
);
}
ngOnInit() {
this.form.setValue({
product: this.data.inventory.batch?.sku.name,
quantity: `${this.data.inventory.quantity}`,
price: `${this.data.inventory.rate}`,
tax: `${this.data.inventory.tax}`,
discount: `${this.data.inventory.discount}`,
});
this.product = this.data.inventory.batch?.sku;
}
displayFn(product?: Product | string): string {
return !product ? '' : typeof product === 'string' ? product : product.name;
}
productSelected(event: MatAutocompleteSelectedEvent): void {
const product: ProductSku = event.option.value;
this.product = product;
this.form.controls.price.setValue(`${product.costPrice}`);
if (product.isRateContracted) {
this.form.controls.price.disable();
this.form.controls.tax.disable();
this.form.controls.discount.disable();
this.form.controls.tax.setValue('RC');
this.form.controls.discount.setValue('RC');
} else {
this.form.controls.price.enable();
this.form.controls.tax.enable();
this.form.controls.discount.enable();
this.form.controls.tax.setValue('');
this.form.controls.discount.setValue('');
}
}
accept(): void {
const formValue = this.form.value;
const quantity = this.math.parseAmount(formValue.quantity, 2);
const price = this.math.parseAmount(formValue.price, 2);
const tax = this.math.parseAmount(formValue.tax, 5);
const discount = this.math.parseAmount(formValue.discount, 5);
if (this.data.inventory.batch === null) {
this.data.inventory.batch = new Batch();
}
this.data.inventory.batch.sku = this.product;
this.data.inventory.quantity = quantity;
this.data.inventory.rate = price;
this.data.inventory.tax = tax;
this.data.inventory.discount = discount;
this.data.inventory.amount = round(quantity * price * (1 + tax) * (1 - discount), 2);
this.dialogRef.close(this.data.inventory);
}
}