96 lines
3.0 KiB
TypeScript
96 lines
3.0 KiB
TypeScript
import { CdkScrollableModule } from '@angular/cdk/scrolling';
|
|
import { Component, computed, inject, linkedSignal } from '@angular/core';
|
|
import { form, FormField, FormRoot } from '@angular/forms/signals';
|
|
import { MatAutocompleteModule } from '@angular/material/autocomplete';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
import { MatOptionModule } from '@angular/material/core';
|
|
import { MAT_DIALOG_DATA, MatDialogModule, MatDialogRef } from '@angular/material/dialog';
|
|
import { MatFormFieldModule } from '@angular/material/form-field';
|
|
import { MatInputModule } from '@angular/material/input';
|
|
import { round } from 'mathjs';
|
|
|
|
import { Batch } from '../core/batch';
|
|
import { BatchService } from '../core/batch.service';
|
|
import { Inventory } from '../core/inventory';
|
|
import { MathService } from '../shared/math.service';
|
|
|
|
export interface IssueDialogFormData {
|
|
batch: Batch | string | null;
|
|
quantity: string;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-issue-dialog',
|
|
templateUrl: './issue-dialog.component.html',
|
|
styleUrls: ['./issue-dialog.component.css'],
|
|
imports: [
|
|
MatDialogModule,
|
|
CdkScrollableModule,
|
|
FormField,
|
|
FormRoot,
|
|
MatFormFieldModule,
|
|
MatInputModule,
|
|
MatAutocompleteModule,
|
|
MatOptionModule,
|
|
MatButtonModule,
|
|
],
|
|
})
|
|
export class IssueDialogComponent {
|
|
readonly dialogRef = inject<MatDialogRef<IssueDialogComponent, Inventory>>(MatDialogRef);
|
|
data = inject<{
|
|
inventory: Inventory;
|
|
date: string;
|
|
}>(MAT_DIALOG_DATA);
|
|
|
|
private math = inject(MathService);
|
|
private batchSer = inject(BatchService);
|
|
|
|
formModel = linkedSignal<
|
|
{
|
|
inventory: Inventory;
|
|
date: string;
|
|
},
|
|
IssueDialogFormData
|
|
>({
|
|
source: () => this.data,
|
|
computation: (data: { inventory: Inventory; date: string }): IssueDialogFormData => ({
|
|
batch: data.inventory.batch ?? null,
|
|
quantity: `${data.inventory.quantity}`,
|
|
}),
|
|
});
|
|
|
|
form = form(this.formModel);
|
|
|
|
batchSearch = computed(() => {
|
|
const v = this.form.batch().value();
|
|
return typeof v === 'string' ? v : null;
|
|
});
|
|
|
|
batchesResource = this.batchSer.autocomplete(
|
|
computed(() => this.data.date),
|
|
this.batchSearch,
|
|
);
|
|
|
|
batches = computed(() => this.batchesResource.value() ?? []);
|
|
|
|
displayFn(batch?: Batch | string): string {
|
|
return !batch ? '' : typeof batch === 'string' ? batch : batch.name;
|
|
}
|
|
|
|
accept(): void {
|
|
const formValue = this.form().value();
|
|
const batch = typeof formValue.batch === 'string' ? null : formValue.batch;
|
|
if (!batch || !batch.id) {
|
|
return;
|
|
}
|
|
const quantity = this.math.parseAmount(formValue.quantity ?? '0', 2);
|
|
this.data.inventory.batch = batch;
|
|
this.data.inventory.quantity = quantity;
|
|
this.data.inventory.rate = batch.rate;
|
|
this.data.inventory.tax = batch.tax;
|
|
this.data.inventory.discount = batch.discount;
|
|
this.data.inventory.amount = round(quantity * batch.rate * (1 + batch.tax) * (1 - batch.discount), 2);
|
|
this.dialogRef.close(this.data.inventory);
|
|
}
|
|
}
|