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

166 lines
5.1 KiB
TypeScript
Raw Normal View History

2020-05-14 08:08:13 +00:00
import {AfterViewInit, Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {FormBuilder, 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 {Observable, of as observableOf} from 'rxjs';
import {debounceTime, distinctUntilChanged, map, startWith, switchMap} from 'rxjs/operators';
import * as moment from 'moment';
import {ProductLedgerDataSource} from './product-ledger-datasource';
import {ProductLedger} from './product-ledger';
import {ProductLedgerService} from './product-ledger.service';
import {Product} from '../core/product';
import {ProductService} from '../product/product.service';
2020-05-14 08:08:13 +00:00
import {ToCsvService} from '../shared/to-csv.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;
dataSource: ProductLedgerDataSource;
form: FormGroup;
info: ProductLedger;
selectedRowId: string;
debitQuantity: number;
debitAmount: number;
creditQuantity: number;
creditAmount: number;
runningQuantity: number;
runningAmount: number;
/** 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.createForm();
this.products = this.form.get('product').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() {
2020-05-14 08:08:13 +00:00
this.route.data.subscribe((data: { 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()
});
this.dataSource = new ProductLedgerDataSource(this.paginator, this.sort, this.info.body);
2020-05-14 08:08:13 +00:00
});
}
2020-05-14 08:08:13 +00:00
ngAfterViewInit() {
setTimeout(() => {
this.productElement.nativeElement.focus();
}, 0);
}
displayFn(product?: Product): string | undefined {
return product ? product.name : undefined;
}
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();
this.router.navigate(['product-ledger', info.product.id], {
queryParams: {
2020-05-14 08:08:13 +00:00
startDate: info.startDate,
finishDate: info.finishDate
}
});
}
createForm() {
this.form = this.fb.group({
startDate: '',
finishDate: '',
product: ''
});
}
2020-05-14 08:08:13 +00:00
getInfo(): ProductLedger {
const formModel = this.form.value;
return {
product: formModel.product,
startDate: moment(formModel.startDate).format('DD-MMM-YYYY'),
2020-05-14 08:08:13 +00:00
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'
};
2020-05-14 08:08:13 +00:00
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);
}
}