brewman/overlord/src/app/product-ledger/product-ledger.component.ts

174 lines
5.2 KiB
TypeScript
Raw Normal View History

import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
2020-11-23 11:12:54 +00:00
import { FormBuilder, 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, map, startWith, switchMap } from 'rxjs/operators';
import { Product } from '../core/product';
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'],
})
2020-05-14 08:08:13 +00:00
export class ProductLedgerComponent implements OnInit, AfterViewInit {
@ViewChild('productElement', { static: true }) productElement?: ElementRef;
@ViewChild(MatPaginator, { static: true }) paginator?: MatPaginator;
@ViewChild(MatSort, { static: true }) sort?: MatSort;
2020-11-23 11:12:54 +00:00
info: ProductLedger = new ProductLedger();
dataSource: ProductLedgerDataSource = new ProductLedgerDataSource(this.info.body);
form: FormGroup;
2020-11-23 11:12:54 +00:00
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<Product[]>;
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
2020-05-14 08:08:13 +00:00
private toCsv: ToCsvService,
private ser: ProductLedgerService,
private productSer: ProductService,
) {
this.form = this.fb.group({
startDate: '',
finishDate: '',
product: '',
});
2020-11-23 11:12:54 +00:00
this.products = (this.form.get('product') as FormControl).valueChanges.pipe(
startWith(null),
map((x) => (x !== null && x.length >= 1 ? x : null)),
debounceTime(150),
distinctUntilChanged(),
switchMap((x) => (x === null ? observableOf([]) : this.productSer.autocomplete(x))),
);
}
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,
startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(),
finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(),
});
2020-11-23 11:12:54 +00:00
this.dataSource = new ProductLedgerDataSource(this.info.body, this.paginator, this.sort);
});
}
2020-05-14 08:08:13 +00:00
ngAfterViewInit() {
setTimeout(() => {
if (this.productElement) {
this.productElement.nativeElement.focus();
}
2020-05-14 08:08:13 +00:00
}, 0);
}
2020-11-23 11:12:54 +00:00
displayFn(product?: Product): string {
return 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() {
2020-05-14 08:08:13 +00:00
const info = this.getInfo();
2020-11-23 11:12:54 +00:00
this.router.navigate(['product-ledger', (info.product as Product).id], {
queryParams: {
2020-05-14 08:08:13 +00:00
startDate: info.startDate,
finishDate: info.finishDate,
},
});
}
2020-05-14 08:08:13 +00:00
getInfo(): ProductLedger {
const formModel = this.form.value;
2020-11-23 11:12:54 +00:00
return new ProductLedger({
product: formModel.product,
startDate: moment(formModel.startDate).format('DD-MMM-YYYY'),
finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'),
2020-11-23 11:12:54 +00:00
});
2020-05-14 08:08:13 +00:00
}
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;',
});
2020-05-14 08:08:13 +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);
}
}