2020-10-01 15:21:22 +00:00
|
|
|
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
2022-07-11 14:42:38 +00:00
|
|
|
import { UntypedFormBuilder, UntypedFormControl, UntypedFormGroup } from '@angular/forms';
|
2019-06-12 11:55:10 +00:00
|
|
|
import { MatAutocompleteSelectedEvent } from '@angular/material/autocomplete';
|
|
|
|
import { MatPaginator } from '@angular/material/paginator';
|
|
|
|
import { MatSort } from '@angular/material/sort';
|
2020-10-01 15:21:22 +00:00
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
2020-10-10 03:15:05 +00:00
|
|
|
import * as moment from 'moment';
|
2020-10-01 15:21:22 +00:00
|
|
|
import { Observable, of as observableOf } from 'rxjs';
|
|
|
|
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
2020-10-10 03:15:05 +00:00
|
|
|
|
2020-10-01 15:21:22 +00:00
|
|
|
import { Account } from '../core/account';
|
|
|
|
import { AccountService } from '../core/account.service';
|
|
|
|
import { ToCsvService } from '../shared/to-csv.service';
|
2018-05-25 13:49:00 +00:00
|
|
|
|
2020-10-10 03:15:05 +00:00
|
|
|
import { Ledger } from './ledger';
|
|
|
|
import { LedgerDataSource } from './ledger-datasource';
|
|
|
|
import { LedgerService } from './ledger.service';
|
|
|
|
|
2018-05-25 13:49:00 +00:00
|
|
|
@Component({
|
|
|
|
selector: 'app-ledger',
|
|
|
|
templateUrl: './ledger.component.html',
|
2020-10-01 15:21:22 +00:00
|
|
|
styleUrls: ['./ledger.component.css'],
|
2018-05-25 13:49:00 +00:00
|
|
|
})
|
|
|
|
export class LedgerComponent implements OnInit, AfterViewInit {
|
2020-11-23 04:55:53 +00:00
|
|
|
@ViewChild('accountElement', { static: true }) accountElement?: ElementRef;
|
|
|
|
@ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator;
|
|
|
|
@ViewChild(MatSort, { static: true }) sort?: MatSort;
|
2020-11-23 11:12:54 +00:00
|
|
|
info: Ledger = new Ledger();
|
|
|
|
dataSource: LedgerDataSource = new LedgerDataSource(this.info.body);
|
2022-07-11 14:42:38 +00:00
|
|
|
form: UntypedFormGroup;
|
2020-11-23 11:12:54 +00:00
|
|
|
selectedRowId = '';
|
|
|
|
debit = 0;
|
|
|
|
credit = 0;
|
|
|
|
running = 0;
|
2018-05-25 13:49:00 +00:00
|
|
|
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
|
|
|
|
displayedColumns = ['date', 'particulars', 'type', 'narration', 'debit', 'credit', 'running'];
|
|
|
|
|
|
|
|
accounts: Observable<Account[]>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
2022-07-11 14:42:38 +00:00
|
|
|
private fb: UntypedFormBuilder,
|
2018-06-11 16:44:04 +00:00
|
|
|
private toCsv: ToCsvService,
|
2018-05-25 13:49:00 +00:00
|
|
|
private ser: LedgerService,
|
2020-10-01 15:21:22 +00:00
|
|
|
private accountSer: AccountService,
|
2018-05-25 13:49:00 +00:00
|
|
|
) {
|
2020-11-23 03:48:02 +00:00
|
|
|
this.form = this.fb.group({
|
|
|
|
startDate: '',
|
|
|
|
finishDate: '',
|
|
|
|
account: '',
|
|
|
|
});
|
2018-05-25 13:49:00 +00:00
|
|
|
|
2022-07-11 14:42:38 +00:00
|
|
|
this.accounts = (this.form.get('account') as UntypedFormControl).valueChanges.pipe(
|
2020-10-01 15:21:22 +00:00
|
|
|
startWith(null),
|
|
|
|
map((x) => (x !== null && x.length >= 1 ? x : null)),
|
|
|
|
debounceTime(150),
|
|
|
|
distinctUntilChanged(),
|
|
|
|
switchMap((x) => (x === null ? observableOf([]) : this.accountSer.autocomplete(x))),
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-11-23 03:48:02 +00:00
|
|
|
this.route.data.subscribe((value) => {
|
|
|
|
const data = value as { info: Ledger };
|
|
|
|
|
2020-10-01 15:21:22 +00:00
|
|
|
this.info = data.info;
|
|
|
|
this.calculateTotals();
|
|
|
|
this.form.setValue({
|
|
|
|
account: this.info.account,
|
|
|
|
startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(),
|
|
|
|
finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(),
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
2020-11-23 11:12:54 +00:00
|
|
|
this.dataSource = new LedgerDataSource(this.info.body, this.paginator, this.sort);
|
2020-10-01 15:21:22 +00:00
|
|
|
});
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
setTimeout(() => {
|
2020-12-08 06:39:19 +00:00
|
|
|
if (this.accountElement) {
|
|
|
|
this.accountElement.nativeElement.focus();
|
|
|
|
}
|
2018-05-25 13:49:00 +00:00
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2020-11-23 11:12:54 +00:00
|
|
|
displayFn(account?: Account): string {
|
|
|
|
return account ? account.name : '';
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
selectRow(id: string): void {
|
|
|
|
this.selectedRowId = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
show() {
|
|
|
|
const info = this.getInfo();
|
2019-06-12 14:04:50 +00:00
|
|
|
this.router.navigate(['ledger', info.account.id], {
|
2018-05-25 13:49:00 +00:00
|
|
|
queryParams: {
|
|
|
|
startDate: info.startDate,
|
2020-10-01 15:21:22 +00:00
|
|
|
finishDate: info.finishDate,
|
|
|
|
},
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
getInfo(): Ledger {
|
|
|
|
const formModel = this.form.value;
|
|
|
|
|
2020-11-23 11:12:54 +00:00
|
|
|
return new Ledger({
|
2018-05-25 13:49:00 +00:00
|
|
|
account: formModel.account,
|
|
|
|
startDate: moment(formModel.startDate).format('DD-MMM-YYYY'),
|
2020-10-01 15:21:22 +00:00
|
|
|
finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'),
|
2020-11-23 11:12:54 +00:00
|
|
|
});
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
exportCsv() {
|
2018-06-11 16:44:04 +00:00
|
|
|
const headers = {
|
|
|
|
Date: 'date',
|
|
|
|
Name: 'name',
|
|
|
|
Type: 'type',
|
|
|
|
Narration: 'narration',
|
|
|
|
Debit: 'debit',
|
|
|
|
Credit: 'credit',
|
|
|
|
Running: 'running',
|
2020-10-01 15:21:22 +00:00
|
|
|
Posted: 'posted',
|
2018-06-11 16:44:04 +00:00
|
|
|
};
|
2020-10-01 15:21:22 +00:00
|
|
|
const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], {
|
|
|
|
type: 'text/csv;charset=utf-8;',
|
|
|
|
});
|
2018-05-25 13:49:00 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|