import { COMMA, ENTER } from '@angular/cdk/keycodes'; import { AsyncPipe, CurrencyPipe, DecimalPipe, PercentPipe } from '@angular/common'; import { AfterViewInit, Component, ElementRef, HostListener, inject, OnInit, ViewChild } from '@angular/core'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { MatAutocompleteModule, MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; import { MatChipInputEvent, MatChipsModule } from '@angular/material/chips'; import { MatOptionModule } from '@angular/material/core'; import { MatDatepickerModule } from '@angular/material/datepicker'; import { MatDialog } from '@angular/material/dialog'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatIconModule } from '@angular/material/icon'; import { MatInputModule } from '@angular/material/input'; import { MatSnackBar } from '@angular/material/snack-bar'; import { MatSortModule } from '@angular/material/sort'; import { MatTableModule } from '@angular/material/table'; import { ActivatedRoute, Router } from '@angular/router'; import { round } from 'mathjs'; import moment from 'moment'; import { BehaviorSubject, Observable, of as observableOf } from 'rxjs'; import { debounceTime, distinctUntilChanged, map, switchMap } from 'rxjs/operators'; import { AuthService } from '../auth/auth.service'; import { Account } from '../core/account'; import { AccountBalance } from '../core/account-balance'; import { AccountService } from '../core/account.service'; import { Batch } from '../core/batch'; import { DbFile } from '../core/db-file'; import { Inventory } from '../core/inventory'; import { Product } from '../core/product'; import { ProductSku } from '../core/product-sku'; import { User } from '../core/user'; import { Voucher } from '../core/voucher'; import { VoucherService } from '../core/voucher.service'; import { ProductService } from '../product/product.service'; import { AccountingPipe } from '../shared/accounting.pipe'; import { ConfirmDialogComponent } from '../shared/confirm-dialog/confirm-dialog.component'; import { ImageDialogComponent } from '../shared/image-dialog/image-dialog.component'; import { ImageService } from '../shared/image.service'; import { LocalTimePipe } from '../shared/local-time.pipe'; import { MathService } from '../shared/math.service'; import { Tag } from '../tag/tag'; import { TagService } from '../tag/tag.service'; import { PurchaseDataSource } from './purchase-datasource'; import { PurchaseDialogComponent } from './purchase-dialog.component'; @Component({ selector: 'app-purchase', templateUrl: './purchase.component.html', styleUrls: ['./purchase.component.css'], imports: [ MatCardModule, MatIconModule, ReactiveFormsModule, MatFormFieldModule, MatInputModule, MatDatepickerModule, MatAutocompleteModule, MatOptionModule, MatButtonModule, MatTableModule, MatSortModule, MatChipsModule, AsyncPipe, DecimalPipe, PercentPipe, CurrencyPipe, AccountingPipe, LocalTimePipe, ], }) export class PurchaseComponent implements OnInit, AfterViewInit { private route = inject(ActivatedRoute); private router = inject(Router); private dialog = inject(MatDialog); private snackBar = inject(MatSnackBar); auth = inject(AuthService); private math = inject(MathService); image = inject(ImageService); private ser = inject(VoucherService); private productSer = inject(ProductService); private accountSer = inject(AccountService); private tagSer = inject(TagService); @ViewChild('accountElement', { static: true }) accountElement!: ElementRef; @ViewChild('productElement', { static: true }) productElement!: ElementRef; @ViewChild('dateElement', { static: true }) dateElement!: ElementRef; @ViewChild('tagInput') tagInput?: ElementRef; @HostListener('window:keydown.f2', ['$event']) focusDate(event: KeyboardEvent) { event.preventDefault(); this.dateElement.nativeElement.focus(); this.dateElement.nativeElement.select(); } @HostListener('window:keydown.control.s', ['$event']) saveListner(event: KeyboardEvent) { event.preventDefault(); if (this.canSave()) { this.save(); } } @HostListener('window:keydown.control.p', ['$event']) postListner(event: KeyboardEvent) { event.preventDefault(); if (this.voucher.id && !this.voucher.posted && this.auth.allowed('post-vouchers')) { this.post(); } } separatorKeysCodes: number[] = [ENTER, COMMA]; public inventoryObservable = new BehaviorSubject([]); dataSource: PurchaseDataSource = new PurchaseDataSource(this.inventoryObservable); form: FormGroup<{ date: FormControl; account: FormControl; amount: FormControl; addRow: FormGroup<{ product: FormControl; quantity: FormControl; price: FormControl; tax: FormControl; discount: FormControl; }>; narration: FormControl; tags: FormControl; }>; voucher: Voucher = new Voucher(); product: ProductSku | null = null; accBal: AccountBalance | null = null; displayedColumns = ['product', 'quantity', 'rate', 'tax', 'discount', 'amount', 'action']; accounts: Observable; tags: Observable; products: Observable; constructor() { this.form = new FormGroup({ date: new FormControl(moment(new Date()), { nonNullable: true }), account: new FormControl(null), amount: new FormControl({ value: 0, disabled: true }, { nonNullable: true }), addRow: new FormGroup({ product: new FormControl(''), quantity: new FormControl('', { nonNullable: true }), price: new FormControl('', { nonNullable: true }), tax: new FormControl('', { nonNullable: true }), discount: new FormControl('', { nonNullable: true }), }), narration: new FormControl('', { nonNullable: true }), tags: new FormControl(''), }); this.accBal = null; // Listen to Account Autocomplete Change this.accounts = this.form.controls.account.valueChanges.pipe( debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), ); this.tags = this.form.controls.tags.valueChanges.pipe( debounceTime(150), distinctUntilChanged(), map((tag: string | Tag | null) => (tag === null ? '' : typeof tag !== 'string' ? tag.name.toLowerCase() : tag)), switchMap((tag: string) => this.tagSer.autocomplete(tag)), ); // Listen to Product Autocomplete Change this.products = this.form.controls.addRow.controls.product.valueChanges.pipe( debounceTime(150), distinctUntilChanged(), switchMap((x) => x === null ? observableOf([]) : this.productSer.autocompleteSku( x, true, moment(this.form.value.date).format('DD-MMM-YYYY'), // this.form.value.account?.id, '', ), ), ); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { voucher: Voucher }; this.loadVoucher(data.voucher); }); } ngAfterViewInit() { this.focusAccount(); } loadVoucher(voucher: Voucher) { this.voucher = voucher; this.form.setValue({ date: moment(this.voucher.date, 'DD-MMM-YYYY'), account: this.voucher.vendor?.name ?? null, amount: Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)), addRow: { product: '', quantity: '', price: '', tax: '', discount: '', }, narration: this.voucher.narration, tags: '', }); this.dataSource = new PurchaseDataSource(this.inventoryObservable); this.updateView(); } focusAccount() { setTimeout(() => { this.accountElement.nativeElement.focus(); }, 0); } addRow() { const formValue = this.form.value.addRow; if (formValue === undefined) { return; } const quantity = this.math.parseAmount(formValue.quantity, 2); if (this.product === null || quantity <= 0) { return; } const price = this.product.isRateContracted ? (this.product.costPrice as number) : this.math.parseAmount(formValue.price, 2); const tax = this.product.isRateContracted ? 0 : this.math.parseAmount(formValue.tax, 5); const discount = this.product.isRateContracted ? 0 : this.math.parseAmount(formValue.discount, 5); if ((price as number) <= 0 || (tax as number) < 0 || (discount as number) < 0) { return; } const oldFiltered = this.voucher.inventories.filter((x) => x.batch?.sku.id === (this.product as ProductSku).id); if (oldFiltered.length) { this.snackBar.open('Product already added', 'Danger'); return; } this.voucher.inventories.push( new Inventory({ quantity, rate: price, tax, discount, amount: round(quantity * (price as number) * (1 + tax) * (1 - discount), 2), batch: new Batch({ sku: this.product }), }), ); this.resetAddRow(); this.updateView(); } resetAddRow() { this.form.controls.addRow.reset(); this.product = null; this.form.controls.addRow.controls.price.enable(); this.form.controls.addRow.controls.tax.enable(); this.form.controls.addRow.controls.discount.enable(); setTimeout(() => { this.productElement.nativeElement.focus(); }, 0); } updateView() { this.inventoryObservable.next(this.voucher.inventories); this.form.controls.amount.setValue( round(Math.abs(this.voucher.inventories.map((x) => x.amount).reduce((p, c) => p + c, 0)), 2), ); } editRow(row: Inventory) { const dialogRef = this.dialog.open(PurchaseDialogComponent, { width: '750px', data: { inventory: { ...row } }, }); 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?.sku.id === j.batch?.sku.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"); } post() { this.ser.post(this.voucher.id as string).subscribe({ next: (result) => { this.loadVoucher(result); this.snackBar.open('Voucher Posted', 'Success'); }, error: (error) => { this.snackBar.open(error, 'Danger'); }, }); } save() { const voucher: Voucher = this.getVoucher(); this.ser.saveOrUpdate(voucher).subscribe({ next: (result) => { this.snackBar.open('', 'Success'); if (voucher.id === result.id) { this.loadVoucher(result); } else { this.router.navigate(['/purchase', result.id]); } }, error: (error) => { this.snackBar.open(error, 'Danger'); }, }); } getVoucher(): Voucher { const formModel = this.form.value; this.voucher.date = moment(formModel.date).format('DD-MMM-YYYY'); if (formModel.account !== null && typeof formModel.account !== 'string') { this.voucher.vendor = formModel.account; } this.voucher.narration = formModel.narration ?? ''; return this.voucher; } delete() { this.ser.delete(this.voucher.id as string).subscribe({ next: () => { this.snackBar.open('', 'Success'); this.router.navigate(['/purchase'], { replaceUrl: true }); }, error: (error) => { this.snackBar.open(error, 'Danger'); }, }); } 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(); } }); } displayFn(item?: Account | Product | string): string { return !item ? '' : typeof item === 'string' ? item : item.name; } accountSelected(event: MatAutocompleteSelectedEvent): void { this.form.controls.account.setValue(event.option.value); } productSelected(event: MatAutocompleteSelectedEvent): void { const product: ProductSku = event.option.value; const addRowForm = this.form.controls.addRow; this.product = product; addRowForm.controls.price.setValue(`${product.costPrice}`); if (product.isRateContracted) { addRowForm.controls.price.disable(); addRowForm.controls.tax.disable(); addRowForm.controls.discount.disable(); addRowForm.controls.tax.setValue('RC'); addRowForm.controls.discount.setValue('RC'); } else { addRowForm.controls.price.enable(); addRowForm.controls.tax.enable(); addRowForm.controls.discount.enable(); addRowForm.controls.tax.setValue(''); addRowForm.controls.discount.setValue(''); } } zoomImage(file: DbFile) { this.dialog.open(ImageDialogComponent, { width: '750px', data: file.resized, }); } deleteImage(file: DbFile) { const index = this.voucher.files.indexOf(file); this.voucher.files.splice(index, 1); } removeTag(tag: Tag): void { const index = this.voucher.tags.indexOf(tag); if (index >= 0) { this.voucher.tags.splice(index, 1); } } addTag(event: MatChipInputEvent): void { const value = (event.value || '').trim(); // Add our tag if (value) { this.voucher.tags.push(new Tag({ name: value })); } // Clear the input value event.chipInput!.clear(); this.form.controls.tags.setValue(null); } selectedTag(event: MatAutocompleteSelectedEvent): void { const tag = event.option.value as Tag; const index = this.voucher.tags.findIndex((t) => (tag.id === null ? t.name === tag.name : t.id === tag.id)); if (index === -1) { this.voucher.tags.push(tag); } if (this.tagInput) { this.tagInput.nativeElement.value = ''; } this.form.controls.tags.setValue(null); } }