255 lines
8.0 KiB
TypeScript
255 lines
8.0 KiB
TypeScript
import { SelectionModel } from '@angular/cdk/collections';
|
|
import { AsyncPipe, CurrencyPipe } 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 { MatCheckboxModule } from '@angular/material/checkbox';
|
|
import { 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 { MatPaginator, MatPaginatorModule } from '@angular/material/paginator';
|
|
import { MatSelectModule } from '@angular/material/select';
|
|
import { MatSort, MatSortModule } from '@angular/material/sort';
|
|
import { MatTableModule } from '@angular/material/table';
|
|
import { ActivatedRoute, Router, RouterModule } from '@angular/router';
|
|
import moment from 'moment';
|
|
import { BehaviorSubject, 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 { TagDialogComponent } from '../tag-dialog/tag-dialog.component';
|
|
import { Tag } from '../tag/tag';
|
|
import { TagService } from '../tag/tag.service';
|
|
import { Ledger } from './ledger';
|
|
import { LedgerDataSource } from './ledger-datasource';
|
|
import { LedgerItem } from './ledger-item';
|
|
|
|
@Component({
|
|
selector: 'app-ledger',
|
|
templateUrl: './ledger.component.html',
|
|
styleUrls: ['./ledger.component.css'],
|
|
imports: [
|
|
MatCardModule,
|
|
MatCheckboxModule,
|
|
MatChipsModule,
|
|
MatIconModule,
|
|
ReactiveFormsModule,
|
|
MatFormFieldModule,
|
|
MatInputModule,
|
|
MatDatepickerModule,
|
|
MatAutocompleteModule,
|
|
MatOptionModule,
|
|
MatButtonModule,
|
|
MatTableModule,
|
|
MatSelectModule,
|
|
MatSortModule,
|
|
RouterModule,
|
|
MatPaginatorModule,
|
|
AsyncPipe,
|
|
CurrencyPipe,
|
|
AccountingPipe,
|
|
ClearPipe,
|
|
],
|
|
})
|
|
export class LedgerComponent implements OnInit, AfterViewInit {
|
|
private route = inject(ActivatedRoute);
|
|
private router = inject(Router);
|
|
private dialog = inject(MatDialog);
|
|
private toCsv = inject(ToCsvService);
|
|
private accountSer = inject(AccountService);
|
|
private tagSer = inject(TagService);
|
|
|
|
@ViewChild('accountElement', { static: true }) accountElement!: ElementRef<HTMLInputElement>;
|
|
@ViewChild('startDateElement', { static: true }) startDate!: ElementRef<HTMLInputElement>;
|
|
@ViewChild(MatPaginator, { static: true }) paginator!: MatPaginator;
|
|
@ViewChild(MatSort, { static: true }) sort!: MatSort;
|
|
info: Ledger = new Ledger();
|
|
body = new BehaviorSubject<LedgerItem[]>([]);
|
|
filter = new BehaviorSubject<string[]>([]);
|
|
dataSource: LedgerDataSource = new LedgerDataSource(this.body, this.filter);
|
|
selection = new SelectionModel<LedgerItem>(true, []);
|
|
|
|
allTags: Tag[] = [];
|
|
tags: string[] = [];
|
|
form: FormGroup<{
|
|
startDate: FormControl<Date>;
|
|
finishDate: FormControl<Date>;
|
|
account: FormControl<string | null>;
|
|
tags: FormControl<string[]>;
|
|
}>;
|
|
|
|
selectedRowId = '';
|
|
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
|
|
displayedColumns = ['date', 'particulars', 'type', 'narration', 'debit', 'credit', 'running'];
|
|
|
|
accounts: Observable<Account[]>;
|
|
|
|
@HostListener('window:keydown.f2', ['$event'])
|
|
focusDate(event: KeyboardEvent) {
|
|
event.preventDefault();
|
|
this.startDate.nativeElement.focus();
|
|
this.startDate.nativeElement.select();
|
|
}
|
|
|
|
constructor() {
|
|
this.form = new FormGroup({
|
|
startDate: new FormControl(new Date(), { nonNullable: true }),
|
|
finishDate: new FormControl(new Date(), { nonNullable: true }),
|
|
account: new FormControl<string | null>(null),
|
|
tags: new FormControl<string[]>([], { nonNullable: true }),
|
|
});
|
|
|
|
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 { tags: Tag[]; info: Ledger };
|
|
|
|
this.allTags = data.tags;
|
|
this.info = data.info;
|
|
this.tags = [
|
|
'(None)',
|
|
...Array.from(new Set(this.info.body.map((x) => x.tags).reduce((list, tags) => list.concat(tags), []))),
|
|
];
|
|
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(),
|
|
tags: this.tags,
|
|
});
|
|
this.body.next(this.info.body);
|
|
this.filter.next(this.tags);
|
|
if (!this.dataSource.paginator) {
|
|
this.dataSource.paginator = this.paginator;
|
|
}
|
|
if (!this.dataSource.sort) {
|
|
this.dataSource.sort = this.sort;
|
|
}
|
|
this.selection.clear();
|
|
});
|
|
}
|
|
|
|
ngAfterViewInit() {
|
|
setTimeout(() => {
|
|
if (this.accountElement) {
|
|
this.accountElement.nativeElement.focus();
|
|
}
|
|
}, 0);
|
|
}
|
|
|
|
displayFn(account?: Account | string): string {
|
|
return !account ? '' : typeof account === 'string' ? account : account.name;
|
|
}
|
|
|
|
tagChanges(value: string[]) {
|
|
this.filter.next(value);
|
|
}
|
|
|
|
selected(event: MatAutocompleteSelectedEvent): void {
|
|
this.info.account = event.option.value as Account;
|
|
}
|
|
|
|
selectRow(id: string): void {
|
|
this.selectedRowId = id;
|
|
}
|
|
|
|
/** Whether the number of selected elements matches the total number of rows. */
|
|
isAllSelected() {
|
|
const numSelected = this.selection.selected.length;
|
|
const numRows = this.dataSource.data.length;
|
|
return numSelected === numRows;
|
|
}
|
|
|
|
/** Selects all rows if they are not all selected; otherwise clear selection. */
|
|
toggleAllRows() {
|
|
if (this.isAllSelected()) {
|
|
this.selection.clear();
|
|
return;
|
|
}
|
|
|
|
this.selection.select(...this.dataSource.data);
|
|
}
|
|
|
|
labels() {
|
|
if (!this.selection.selected.length) {
|
|
return;
|
|
}
|
|
|
|
this.dialog
|
|
.open(TagDialogComponent, {
|
|
width: '750px',
|
|
data: {
|
|
tags: this.allTags,
|
|
vouchers: this.selection.selected,
|
|
},
|
|
})
|
|
.afterClosed()
|
|
.subscribe({
|
|
next: (result) => {
|
|
if (result) {
|
|
this.router.navigateByUrl(this.router.url);
|
|
}
|
|
},
|
|
});
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|