82 lines
2.5 KiB
TypeScript
82 lines
2.5 KiB
TypeScript
import { Component, Inject, OnInit } from '@angular/core';
|
|
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
|
import { FormBuilder, FormGroup } from '@angular/forms';
|
|
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
|
import { Observable, of as observableOf } from 'rxjs';
|
|
import { MathService } from '../shared/math.service';
|
|
import { Batch } from '../core/voucher';
|
|
import { BatchService } from '../core/batch.service';
|
|
|
|
@Component({
|
|
selector: 'app-issue-dialog',
|
|
templateUrl: './issue-dialog.component.html',
|
|
styleUrls: ['./issue-dialog.component.css'],
|
|
})
|
|
export class IssueDialogComponent implements OnInit {
|
|
batches: Observable<Batch[]>;
|
|
form: FormGroup;
|
|
batch: Batch;
|
|
|
|
constructor(
|
|
public dialogRef: MatDialogRef<IssueDialogComponent>,
|
|
@Inject(MAT_DIALOG_DATA) public data: any,
|
|
private fb: FormBuilder,
|
|
private math: MathService,
|
|
private batchSer: BatchService,
|
|
) {
|
|
this.createForm();
|
|
this.listenToBatchAutocompleteChange();
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.form.setValue({
|
|
batch: this.data.inventory.batch,
|
|
quantity: this.data.inventory.quantity,
|
|
});
|
|
this.batch = this.data.inventory.batch;
|
|
}
|
|
|
|
createForm() {
|
|
this.form = this.fb.group({
|
|
batch: '',
|
|
quantity: '',
|
|
});
|
|
}
|
|
|
|
listenToBatchAutocompleteChange(): void {
|
|
const control = this.form.get('batch');
|
|
this.batches = control.valueChanges.pipe(
|
|
startWith(null),
|
|
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
|
debounceTime(150),
|
|
distinctUntilChanged(),
|
|
switchMap((x) =>
|
|
x === null ? observableOf([]) : this.batchSer.autocomplete(this.data.date, x),
|
|
),
|
|
);
|
|
}
|
|
|
|
displayBatchName(batch?: Batch): string | undefined {
|
|
return batch ? batch.name : undefined;
|
|
}
|
|
|
|
batchSelected(event: MatAutocompleteSelectedEvent): void {
|
|
this.batch = event.option.value;
|
|
}
|
|
|
|
accept(): void {
|
|
const formValue = this.form.value;
|
|
const quantity = this.math.parseAmount(formValue.quantity, 2);
|
|
this.data.inventory.batch = this.batch;
|
|
this.data.inventory.product = this.batch.product;
|
|
this.data.inventory.quantity = quantity;
|
|
this.data.inventory.rate = this.batch.rate;
|
|
this.data.inventory.tax = this.batch.tax;
|
|
this.data.inventory.discount = this.batch.discount;
|
|
this.data.inventory.amount =
|
|
quantity * this.batch.rate * (1 + this.batch.tax) * (1 - this.batch.discount);
|
|
this.dialogRef.close(this.data.inventory);
|
|
}
|
|
}
|