import { AsyncPipe, CurrencyPipe } from '@angular/common'; import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { MatAutocompleteSelectedEvent, MatAutocompleteTrigger, MatAutocomplete } from '@angular/material/autocomplete'; import { MatIconButton, MatButton } from '@angular/material/button'; import { MatCard, MatCardHeader, MatCardTitleGroup, MatCardTitle, MatCardContent } from '@angular/material/card'; import { MatOption } from '@angular/material/core'; import { MatDatepickerInput, MatDatepickerToggle, MatDatepicker } from '@angular/material/datepicker'; import { MatFormField, MatLabel, MatSuffix } from '@angular/material/form-field'; import { MatIcon } from '@angular/material/icon'; import { MatInput } from '@angular/material/input'; import { MatPaginator } from '@angular/material/paginator'; import { MatSort, MatSortHeader } from '@angular/material/sort'; import { MatTable, MatColumnDef, MatHeaderCellDef, MatHeaderCell, MatCellDef, MatCell, MatFooterCellDef, MatFooterCell, MatHeaderRowDef, MatHeaderRow, MatRowDef, MatRow, MatFooterRowDef, MatFooterRow, } from '@angular/material/table'; import { ActivatedRoute, Router, RouterLink } from '@angular/router'; import moment from 'moment'; import { Observable, of as observableOf } from 'rxjs'; import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators'; import { Account } from '../core/account'; import { AccountService } from '../core/account.service'; import { AccountingPipe } from '../shared/accounting.pipe'; import { ClearPipe } from '../shared/clear.pipe'; import { ToCsvService } from '../shared/to-csv.service'; import { Ledger } from './ledger'; import { LedgerDataSource } from './ledger-datasource'; import { LedgerService } from './ledger.service'; @Component({ selector: 'app-ledger', templateUrl: './ledger.component.html', styleUrls: ['./ledger.component.css'], standalone: true, imports: [ MatCard, MatCardHeader, MatCardTitleGroup, MatCardTitle, MatIconButton, MatIcon, MatCardContent, ReactiveFormsModule, MatFormField, MatLabel, MatInput, MatDatepickerInput, MatDatepickerToggle, MatSuffix, MatDatepicker, MatAutocompleteTrigger, MatAutocomplete, MatOption, MatButton, MatTable, MatSort, MatColumnDef, MatHeaderCellDef, MatHeaderCell, MatSortHeader, MatCellDef, MatCell, MatFooterCellDef, MatFooterCell, RouterLink, MatHeaderRowDef, MatHeaderRow, MatRowDef, MatRow, MatFooterRowDef, MatFooterRow, MatPaginator, AsyncPipe, CurrencyPipe, AccountingPipe, ClearPipe, ], }) export class LedgerComponent implements OnInit, AfterViewInit { @ViewChild('accountElement', { static: true }) accountElement!: ElementRef; @ViewChild('startDateElement', { static: true }) startDate!: ElementRef; @ViewChild(MatPaginator, { static: true }) paginator!: MatPaginator; @ViewChild(MatSort, { static: true }) sort!: MatSort; info: Ledger = new Ledger(); dataSource: LedgerDataSource = new LedgerDataSource(this.info.body); form: FormGroup<{ startDate: FormControl; finishDate: FormControl; account: FormControl; }>; selectedRowId = ''; debit = 0; credit = 0; running = 0; /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ displayedColumns = ['date', 'particulars', 'type', 'narration', 'debit', 'credit', 'running']; accounts: Observable; @HostListener('window:keydown.f2', ['$event']) focusDate(event: KeyboardEvent) { event.preventDefault(); this.startDate.nativeElement.focus(); this.startDate.nativeElement.select(); } constructor( private route: ActivatedRoute, private router: Router, private toCsv: ToCsvService, private ser: LedgerService, private accountSer: AccountService, ) { this.form = new FormGroup({ startDate: new FormControl(new Date(), { nonNullable: true }), finishDate: new FormControl(new Date(), { nonNullable: true }), account: new FormControl(null), }); this.accounts = this.form.controls.account.valueChanges.pipe( debounceTime(150), distinctUntilChanged(), switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))), ); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { info: Ledger }; this.info = data.info; this.calculateTotals(); this.form.setValue({ account: this.info.account?.name ?? '', startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(), finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(), }); this.dataSource = new LedgerDataSource(this.info.body, this.paginator, this.sort); }); } ngAfterViewInit() { setTimeout(() => { if (this.accountElement) { this.accountElement.nativeElement.focus(); } }, 0); } displayFn(account?: Account | string): string { return !account ? '' : typeof account === 'string' ? account : account.name; } calculateTotals() { this.debit = 0; this.credit = 0; this.running = 0; this.info.body.forEach((item) => { if (item.type !== 'Opening Balance') { this.debit += item.debit; this.credit += item.credit; if (item.posted) { this.running += item.debit - item.credit; } } else { this.running += item.debit - item.credit; } item.running = this.running; }); } selected(event: MatAutocompleteSelectedEvent): void { this.info.account = event.option.value as Account; } selectRow(id: string): void { this.selectedRowId = id; } show() { const info = this.getInfo(); if (info.account) { this.router.navigate(['ledger', info.account.id], { queryParams: { startDate: info.startDate, finishDate: info.finishDate, }, }); } } getInfo(): Ledger { const formModel = this.form.value; return new Ledger({ account: this.info.account, 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); } }