Strict done!!
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from '@angular/core';
|
||||
import { FormBuilder, FormGroup } from '@angular/forms';
|
||||
import { FormBuilder, FormControl, FormGroup } from '@angular/forms';
|
||||
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
||||
import { MatDialog } from '@angular/material/dialog';
|
||||
import { ActivatedRoute, Router } from '@angular/router';
|
||||
@ -15,6 +15,7 @@ import { BatchService } from '../core/batch.service';
|
||||
import { CostCentre } from '../core/cost-centre';
|
||||
import { Inventory } from '../core/inventory';
|
||||
import { ToasterService } from '../core/toaster.service';
|
||||
import { User } from '../core/user';
|
||||
import { Voucher } from '../core/voucher';
|
||||
import { VoucherService } from '../core/voucher.service';
|
||||
import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component';
|
||||
@ -35,12 +36,12 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
@ViewChild('dateElement', { static: true }) dateElement?: ElementRef;
|
||||
public inventoryObservable = new BehaviorSubject<Inventory[]>([]);
|
||||
public gridObservable = new BehaviorSubject<any[]>([]);
|
||||
dataSource: IssueDataSource;
|
||||
gridDataSource: IssueGridDataSource;
|
||||
dataSource: IssueDataSource = new IssueDataSource(this.inventoryObservable);
|
||||
gridDataSource: IssueGridDataSource = new IssueGridDataSource(this.gridObservable);
|
||||
form: FormGroup;
|
||||
voucher: Voucher;
|
||||
costCentres: CostCentre[];
|
||||
batch: Batch;
|
||||
voucher: Voucher = new Voucher();
|
||||
costCentres: CostCentre[] = [];
|
||||
batch: Batch | null = null;
|
||||
|
||||
displayedColumns = ['product', 'batch', 'quantity', 'rate', 'amount', 'action'];
|
||||
gridColumns = ['source', 'destination', 'gridAmount', 'load'];
|
||||
@ -71,8 +72,21 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
}),
|
||||
narration: '',
|
||||
});
|
||||
this.listenToBatchAutocompleteChange();
|
||||
this.listenToDateChange();
|
||||
// Listen to Batch Autocomplete Change
|
||||
this.batches = ((this.form.get('addRow') as FormControl).get(
|
||||
'batch',
|
||||
) as FormControl).valueChanges.pipe(
|
||||
startWith('null'),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) =>
|
||||
this.batchSer.autocomplete(moment(this.form.value.date).format('DD-MMM-YYYY'), x),
|
||||
),
|
||||
);
|
||||
// Listen to Date Change
|
||||
(this.form.get('date') as FormControl).valueChanges
|
||||
.pipe(map((x) => moment(x).format('DD-MMM-YYYY')))
|
||||
.subscribe((x) => this.showGrid(x));
|
||||
}
|
||||
|
||||
ngOnInit() {
|
||||
@ -88,7 +102,7 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
'f2',
|
||||
(): boolean => {
|
||||
setTimeout(() => {
|
||||
this.dateElement.nativeElement.focus();
|
||||
if (this.dateElement) this.dateElement.nativeElement.focus();
|
||||
}, 0);
|
||||
return false; // Prevent bubbling
|
||||
},
|
||||
@ -121,8 +135,8 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
this.voucher = voucher;
|
||||
this.form.setValue({
|
||||
date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(),
|
||||
source: this.voucher.source.id,
|
||||
destination: this.voucher.destination.id,
|
||||
source: (this.voucher.source as CostCentre).id,
|
||||
destination: (this.voucher.destination as CostCentre).id,
|
||||
amount: Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)),
|
||||
addRow: {
|
||||
batch: '',
|
||||
@ -136,59 +150,60 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
|
||||
focusBatch() {
|
||||
setTimeout(() => {
|
||||
this.batchElement.nativeElement.focus();
|
||||
if (this.batchElement) this.batchElement.nativeElement.focus();
|
||||
}, 0);
|
||||
}
|
||||
|
||||
addRow() {
|
||||
const formValue = this.form.get('addRow').value;
|
||||
const formValue = (this.form.get('addRow') as FormControl).value;
|
||||
const quantity = this.math.parseAmount(formValue.quantity, 2);
|
||||
const isConsumption = this.form.value.source === '7b845f95-dfef-fa4a-897c-f0baf15284a3';
|
||||
if (this.batch === null || quantity <= 0) {
|
||||
return;
|
||||
}
|
||||
const oldFiltered = this.voucher.inventories.filter(
|
||||
(x) => x.product.id === this.batch.product.id,
|
||||
(x) => x.product.id === (this.batch as Batch).product.id,
|
||||
);
|
||||
const old = oldFiltered.length ? oldFiltered[0] : null;
|
||||
if (oldFiltered.length) {
|
||||
if (old.batch.id !== this.batch.id) {
|
||||
if (((old as Inventory).batch as Batch).id !== this.batch.id) {
|
||||
this.toaster.show('Danger', 'Product with a different batch already added');
|
||||
return;
|
||||
}
|
||||
if (isConsumption && old.quantity + quantity > this.batch.quantityRemaining) {
|
||||
if (isConsumption && (old as Inventory).quantity + quantity > this.batch.quantityRemaining) {
|
||||
this.toaster.show('Danger', 'Quantity issued cannot be more than quantity available');
|
||||
return;
|
||||
}
|
||||
old.quantity += quantity;
|
||||
(old as Inventory).quantity += quantity;
|
||||
} else {
|
||||
if (isConsumption && quantity > this.batch.quantityRemaining) {
|
||||
this.toaster.show('Danger', 'Quantity issued cannot be more than quantity available');
|
||||
return;
|
||||
}
|
||||
this.voucher.inventories.push({
|
||||
id: null,
|
||||
quantity,
|
||||
rate: this.batch.rate,
|
||||
tax: this.batch.tax,
|
||||
discount: this.batch.discount,
|
||||
amount: quantity * this.batch.rate * (1 + this.batch.tax) * (1 - this.batch.discount),
|
||||
product: this.batch.product,
|
||||
batch: this.batch,
|
||||
});
|
||||
this.voucher.inventories.push(
|
||||
new Inventory({
|
||||
quantity,
|
||||
rate: this.batch.rate,
|
||||
tax: this.batch.tax,
|
||||
discount: this.batch.discount,
|
||||
amount: quantity * this.batch.rate * (1 + this.batch.tax) * (1 - this.batch.discount),
|
||||
product: this.batch.product,
|
||||
batch: this.batch,
|
||||
}),
|
||||
);
|
||||
}
|
||||
this.resetAddRow();
|
||||
this.updateView();
|
||||
}
|
||||
|
||||
resetAddRow() {
|
||||
this.form.get('addRow').reset({
|
||||
(this.form.get('addRow') as FormControl).reset({
|
||||
batch: null,
|
||||
quantity: '',
|
||||
});
|
||||
this.batch = null;
|
||||
setTimeout(() => {
|
||||
this.batchElement.nativeElement.focus();
|
||||
if (this.batchElement) this.batchElement.nativeElement.focus();
|
||||
}, 0);
|
||||
}
|
||||
|
||||
@ -198,7 +213,7 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)),
|
||||
2,
|
||||
);
|
||||
this.form.get('amount').setValue(amount);
|
||||
(this.form.get('amount') as FormControl).setValue(amount);
|
||||
}
|
||||
|
||||
editRow(row: Inventory) {
|
||||
@ -235,12 +250,12 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
if (!this.voucher.id) {
|
||||
return true;
|
||||
}
|
||||
if (this.voucher.posted && this.auth.user.perms.indexOf('edit-posted-vouchers') !== -1) {
|
||||
if (this.voucher.posted && this.auth.allowed('edit-posted-vouchers')) {
|
||||
return true;
|
||||
}
|
||||
return (
|
||||
this.voucher.user.id === this.auth.user.id ||
|
||||
this.auth.user.perms.indexOf("edit-other-user's-vouchers") !== -1
|
||||
this.voucher.user.id === (this.auth.user as User).id ||
|
||||
this.auth.allowed("edit-other-user's-vouchers")
|
||||
);
|
||||
}
|
||||
|
||||
@ -268,14 +283,14 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
getVoucher(): Voucher {
|
||||
const formModel = this.form.value;
|
||||
this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY');
|
||||
this.voucher.source.id = formModel.source;
|
||||
this.voucher.destination.id = formModel.destination;
|
||||
(this.voucher.source as CostCentre).id = formModel.source;
|
||||
(this.voucher.destination as CostCentre).id = formModel.destination;
|
||||
this.voucher.narration = formModel.narration;
|
||||
return this.voucher;
|
||||
}
|
||||
|
||||
delete() {
|
||||
this.ser.delete(this.voucher.id).subscribe(
|
||||
this.ser.delete(this.voucher.id as string).subscribe(
|
||||
() => {
|
||||
this.toaster.show('Success', '');
|
||||
this.router.navigate(['/issue'], { replaceUrl: true });
|
||||
@ -299,31 +314,12 @@ export class IssueComponent implements OnInit, AfterViewInit, OnDestroy {
|
||||
});
|
||||
}
|
||||
|
||||
listenToBatchAutocompleteChange(): void {
|
||||
const control = this.form.get('addRow').get('batch');
|
||||
this.batches = control.valueChanges.pipe(
|
||||
startWith('null'),
|
||||
debounceTime(150),
|
||||
distinctUntilChanged(),
|
||||
switchMap((x) =>
|
||||
this.batchSer.autocomplete(moment(this.form.value.date).format('DD-MMM-YYYY'), x),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
listenToDateChange(): void {
|
||||
this.form
|
||||
.get('date')
|
||||
.valueChanges.pipe(map((x) => moment(x).format('DD-MMM-YYYY')))
|
||||
.subscribe((x) => this.showGrid(x));
|
||||
}
|
||||
|
||||
showGrid(date: string) {
|
||||
this.issueGridSer.issueGrid(date).subscribe((x) => this.gridObservable.next(x));
|
||||
}
|
||||
|
||||
displayFn(batch?: Batch): string | undefined {
|
||||
return batch ? batch.name : undefined;
|
||||
displayFn(batch?: Batch): string {
|
||||
return batch ? batch.name : '';
|
||||
}
|
||||
|
||||
batchSelected(event: MatAutocompleteSelectedEvent): void {
|
||||
|
||||
Reference in New Issue
Block a user