import { AfterViewInit, Component, ElementRef, OnInit, OnDestroy, ViewChild } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MatDialog } from '@angular/material/dialog'; import { ActivatedRoute, Router } from '@angular/router'; import { Hotkey, HotkeysService } from 'angular2-hotkeys'; import { round } from 'mathjs'; import * as moment from 'moment'; import { BehaviorSubject, Observable } from 'rxjs'; import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; import { AuthService } from '../auth/auth.service'; import { Batch } from '../core/batch'; import { BatchService } from '../core/batch.service'; import { CostCentre } from '../core/cost-centre'; import { Inventory } from '../core/inventory'; import { IssueGridItem } from '../core/issue-grid-item'; 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'; import { MathService } from '../shared/math.service'; import { IssueDataSource } from './issue-datasource'; import { IssueDialogComponent } from './issue-dialog.component'; import { IssueGridDataSource } from './issue-grid-datasource'; import { IssueGridService } from './issue-grid.service'; @Component({ selector: 'app-issue', templateUrl: './issue.component.html', styleUrls: ['./issue.component.css'], }) export class IssueComponent implements OnInit, AfterViewInit, OnDestroy { @ViewChild('batchElement', { static: true }) batchElement?: ElementRef; @ViewChild('dateElement', { static: true }) dateElement?: ElementRef; public inventoryObservable = new BehaviorSubject([]); public gridObservable = new BehaviorSubject([]); dataSource: IssueDataSource = new IssueDataSource(this.inventoryObservable); gridDataSource: IssueGridDataSource = new IssueGridDataSource(this.gridObservable); form: FormGroup<{ date: FormControl; source: FormControl; destination: FormControl; amount: FormControl; addRow: FormGroup<{ batch: FormControl; quantity: FormControl; }>; narration: FormControl; }>; voucher: Voucher = new Voucher(); costCentres: CostCentre[] = []; batch: Batch | null = null; displayedColumns = ['product', 'batch', 'quantity', 'rate', 'amount', 'action']; gridColumns = ['source', 'destination', 'gridAmount', 'load']; batches: Observable; constructor( private route: ActivatedRoute, private router: Router, private dialog: MatDialog, private hotkeys: HotkeysService, private toaster: ToasterService, private auth: AuthService, private math: MathService, private ser: VoucherService, private batchSer: BatchService, private issueGridSer: IssueGridService, ) { this.form = new FormGroup({ date: new FormControl(new Date(), { nonNullable: true }), source: new FormControl(null), destination: new FormControl(null), amount: new FormControl({ value: 0, disabled: true }, { nonNullable: true }), addRow: new FormGroup({ batch: new FormControl(null), quantity: new FormControl('', { nonNullable: true }), }), narration: new FormControl('', { nonNullable: true }), }); // Listen to Batch Autocomplete Change this.batches = this.form.controls.addRow.controls.batch.valueChanges.pipe( map((x) => ((x as Batch).name !== undefined ? (x as Batch).name : (x as string))), map((x) => (x !== null && x.length >= 1 ? x : '')), debounceTime(150), distinctUntilChanged(), switchMap((x) => this.batchSer.autocomplete(moment(this.form.value.date).format('DD-MMM-YYYY'), x), ), ); // Listen to Date Change this.form.controls.date.valueChanges .pipe(map((x) => moment(x).format('DD-MMM-YYYY'))) .subscribe((x) => this.showGrid(x)); } ngOnInit() { this.gridDataSource = new IssueGridDataSource(this.gridObservable); this.route.data.subscribe((value) => { const data = value as { voucher: Voucher; costCentres: CostCentre[] }; this.costCentres = data.costCentres; this.loadVoucher(data.voucher); }); this.hotkeys.add( new Hotkey( 'f2', (): boolean => { setTimeout(() => { if (this.dateElement) { this.dateElement.nativeElement.focus(); } }, 0); return false; // Prevent bubbling }, ['INPUT', 'SELECT', 'TEXTAREA'], ), ); this.hotkeys.add( new Hotkey( 'ctrl+s', (): boolean => { if (this.canSave()) { this.save(); } return false; // Prevent bubbling }, ['INPUT', 'SELECT', 'TEXTAREA'], ), ); } ngAfterViewInit() { this.focusBatch(); } ngOnDestroy() { this.hotkeys.reset(); } loadVoucher(voucher: Voucher) { this.voucher = voucher; this.form.setValue({ date: moment(this.voucher.date, 'DD-MMM-YYYY').toDate(), 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: '', quantity: '', }, narration: this.voucher.narration, }); this.dataSource = new IssueDataSource(this.inventoryObservable); this.updateView(); } focusBatch() { setTimeout(() => { if (this.batchElement) { this.batchElement.nativeElement.focus(); } }, 0); } addRow() { const formValue = this.form.value.addRow!; 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 old = this.voucher.inventories.find((x) => x.batch.id === (this.batch as Batch).id); if (old !== undefined) { if (isConsumption && old.quantity + quantity > this.batch.quantityRemaining) { this.toaster.show('Danger', 'Quantity issued cannot be more than quantity available'); return; } old.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( 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), batch: this.batch, }), ); } this.resetAddRow(); this.updateView(); } resetAddRow() { this.form.controls.addRow.reset(); this.batch = null; setTimeout(() => { if (this.batchElement) { this.batchElement.nativeElement.focus(); } }, 0); } updateView() { this.inventoryObservable.next(this.voucher.inventories); const amount = round( Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)), 2, ); this.form.controls.amount.setValue(amount); } editRow(row: Inventory) { const dialogRef = this.dialog.open(IssueDialogComponent, { width: '750px', data: { inventory: { ...row }, date: moment(this.form.value.date).format('DD-MMM-YYYY'), }, }); dialogRef.afterClosed().subscribe((result: boolean | Inventory) => { if (!result) { return; } const j = result as Inventory; if ( j.batch.sku.id !== row.batch.sku.id && this.voucher.inventories.filter((x) => x.batch.id === j.batch.id).length ) { return; } Object.assign(row, j); this.updateView(); }); } deleteRow(row: Inventory) { this.voucher.inventories.splice(this.voucher.inventories.indexOf(row), 1); this.updateView(); } canSave() { if (!this.voucher.id) { return true; } if (this.voucher.posted && this.auth.allowed('edit-posted-vouchers')) { return true; } return ( this.voucher.user.id === (this.auth.user as User).id || this.auth.allowed("edit-other-user's-vouchers") ); } save() { const voucher: Voucher = this.getVoucher(); this.ser.saveOrUpdate(voucher).subscribe( (result) => { this.toaster.show('Success', ''); if (voucher.id === result.id) { this.loadVoucher(result); } else { this.router.navigate(['/issue', result.id]); } }, (error) => { this.toaster.show('Danger', error); }, ); } newVoucher() { this.router.navigate(['/issue']); } getVoucher(): Voucher { const formModel = this.form.value; this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY'); (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 as string).subscribe( () => { this.toaster.show('Success', ''); this.router.navigate(['/issue'], { replaceUrl: true }); }, (error) => { this.toaster.show('Danger', error); }, ); } confirmDelete(): void { const dialogRef = this.dialog.open(ConfirmDialogComponent, { width: '250px', data: { title: 'Delete Voucher?', content: 'Are you sure? This cannot be undone.' }, }); dialogRef.afterClosed().subscribe((result: boolean) => { if (result) { this.delete(); } }); } showGrid(date: string) { this.issueGridSer.issueGrid(date).subscribe((x) => this.gridObservable.next(x)); } displayFn(batch?: Batch): string { return batch ? batch.name : ''; } batchSelected(event: MatAutocompleteSelectedEvent): void { this.batch = event.option.value; } goToVoucher(id: string) { this.router.navigate(['/issue', id]); } }