import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { FormControl, FormGroup } from '@angular/forms'; import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete'; import { MatPaginator } from '@angular/material/paginator'; import { MatSort } from '@angular/material/sort'; import { ActivatedRoute, Router } from '@angular/router'; import * as moment from 'moment'; import { Observable, of as observableOf } from 'rxjs'; import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Product } from '../core/product'; import { ProductSku } from '../core/product-sku'; import { ProductService } from '../product/product.service'; import { ToCsvService } from '../shared/to-csv.service'; import { ProductLedger } from './product-ledger'; import { ProductLedgerDataSource } from './product-ledger-datasource'; import { ProductLedgerService } from './product-ledger.service'; @Component({ selector: 'app-product-ledger', templateUrl: './product-ledger.component.html', styleUrls: ['./product-ledger.component.css'], }) export class ProductLedgerComponent implements OnInit, AfterViewInit { @ViewChild('productElement', { static: true }) productElement?: ElementRef; @ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator; @ViewChild(MatSort, { static: true }) sort?: MatSort; info: ProductLedger = new ProductLedger(); dataSource: ProductLedgerDataSource = new ProductLedgerDataSource(this.info.body); form: FormGroup<{ startDate: FormControl; finishDate: FormControl; product: FormControl; }>; selectedRowId = ''; debitQuantity = 0; debitAmount = 0; creditQuantity = 0; creditAmount = 0; runningQuantity = 0; runningAmount = 0; /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ displayedColumns = [ 'date', 'particulars', 'type', 'narration', 'debitQuantity', 'debitAmount', 'creditQuantity', 'creditAmount', 'runningQuantity', 'runningAmount', ]; products: Observable; constructor( private route: ActivatedRoute, private router: Router, private toCsv: ToCsvService, private ser: ProductLedgerService, private productSer: ProductService, ) { this.form = new FormGroup({ startDate: new FormControl(new Date(), { nonNullable: true }), finishDate: new FormControl(new Date(), { nonNullable: true }), product: new FormControl(null), }); this.products = this.form.controls.product.valueChanges.pipe( debounceTime(150), distinctUntilChanged(), switchMap((x) => x === null ? observableOf([]) : this.productSer.autocompleteProduct(x, null), ), ); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { info: ProductLedger }; this.info = data.info; this.calculateTotals(); this.form.setValue({ product: this.info.product?.name ?? '', startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(), finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(), }); this.dataSource = new ProductLedgerDataSource(this.info.body, this.paginator, this.sort); }); } ngAfterViewInit() { setTimeout(() => { if (this.productElement) { this.productElement.nativeElement.focus(); } }, 0); } displayFn(product?: Product | string): string { return !product ? '' : typeof product === 'string' ? product : product.name; } calculateTotals() { this.debitQuantity = 0; this.debitAmount = 0; this.creditQuantity = 0; this.creditAmount = 0; this.runningQuantity = 0; this.runningAmount = 0; this.info.body.forEach((item) => { if (item.type !== 'Opening Balance') { this.debitAmount += item.debitAmount; this.creditQuantity += item.creditQuantity; this.creditAmount += item.creditAmount; } this.runningQuantity += item.debitQuantity - item.creditQuantity; this.runningAmount += item.debitAmount - item.creditAmount; item.runningQuantity = this.runningQuantity; item.runningAmount = this.runningAmount; }); } selected(event: MatAutocompleteSelectedEvent): void { this.info.product = event.option.value; } selectRow(id: string): void { this.selectedRowId = id; } show() { const info = this.getInfo(); if (info.product) { this.router.navigate(['product-ledger', info.product.id], { queryParams: { startDate: info.startDate, finishDate: info.finishDate, }, }); } } getInfo(): ProductLedger { const formModel = this.form.value; return new ProductLedger({ product: this.info.product, startDate: moment(formModel.startDate).format('DD-MMM-YYYY'), finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'), }); } exportCsv() { const headers = { Date: 'date', Name: 'name', Type: 'type', Narration: 'narration', Debit: 'debit', Credit: 'credit', Running: 'running', Posted: 'posted', }; const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], { type: 'text/csv;charset=utf-8;', }); const link = document.createElement('a'); link.href = window.URL.createObjectURL(csvData); link.setAttribute('download', 'ledger.csv'); document.body.appendChild(link); link.click(); document.body.removeChild(link); } }