2020-10-01 15:21:22 +00:00
|
|
|
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
|
|
|
import { FormBuilder, FormGroup } 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';
|
|
|
|
import { Observable, of as observableOf } from 'rxjs';
|
|
|
|
import { debounceTime, distinctUntilChanged, map, startWith, switchMap } from 'rxjs/operators';
|
2018-05-25 13:49:00 +00:00
|
|
|
import * as moment from 'moment';
|
2020-10-01 15:21:22 +00:00
|
|
|
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';
|
|
|
|
import { ToCsvService } from '../shared/to-csv.service';
|
2018-05-25 13:49:00 +00:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-product-ledger',
|
|
|
|
templateUrl: './product-ledger.component.html',
|
2020-10-01 15:21:22 +00:00
|
|
|
styleUrls: ['./product-ledger.component.css'],
|
2018-05-25 13:49:00 +00:00
|
|
|
})
|
2020-05-14 08:08:13 +00:00
|
|
|
export class ProductLedgerComponent implements OnInit, AfterViewInit {
|
|
|
|
@ViewChild('productElement', { static: true }) productElement: ElementRef;
|
2019-06-12 11:55:10 +00:00
|
|
|
@ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
|
|
|
|
@ViewChild(MatSort, { static: true }) sort: MatSort;
|
2018-05-25 13:49:00 +00:00
|
|
|
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 = [
|
2020-10-01 15:21:22 +00:00
|
|
|
'date',
|
|
|
|
'particulars',
|
|
|
|
'type',
|
|
|
|
'narration',
|
|
|
|
'debitQuantity',
|
|
|
|
'debitAmount',
|
|
|
|
'creditQuantity',
|
|
|
|
'creditAmount',
|
|
|
|
'runningQuantity',
|
|
|
|
'runningAmount',
|
2018-05-25 13:49:00 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
products: Observable<Product[]>;
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
|
|
|
private fb: FormBuilder,
|
2020-05-14 08:08:13 +00:00
|
|
|
private toCsv: ToCsvService,
|
2018-05-25 13:49:00 +00:00
|
|
|
private ser: ProductLedgerService,
|
2020-10-01 15:21:22 +00:00
|
|
|
private productSer: ProductService,
|
2018-05-25 13:49:00 +00:00
|
|
|
) {
|
|
|
|
this.createForm();
|
|
|
|
|
2020-10-01 15:21:22 +00:00
|
|
|
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))),
|
|
|
|
);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnInit() {
|
2020-05-14 08:08:13 +00:00
|
|
|
this.route.data.subscribe((data: { info: ProductLedger }) => {
|
2020-10-01 15:21:22 +00:00
|
|
|
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(),
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
2020-10-01 15:21:22 +00:00
|
|
|
this.dataSource = new ProductLedgerDataSource(this.paginator, this.sort, this.info.body);
|
|
|
|
});
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 08:08:13 +00:00
|
|
|
ngAfterViewInit() {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.productElement.nativeElement.focus();
|
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2018-05-25 13:49:00 +00:00
|
|
|
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;
|
|
|
|
}
|
2018-06-28 11:08:27 +00:00
|
|
|
this.runningQuantity += item.debitQuantity - item.creditQuantity;
|
|
|
|
this.runningAmount += item.debitAmount - item.creditAmount;
|
2018-05-25 13:49:00 +00:00
|
|
|
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], {
|
2018-05-25 13:49:00 +00:00
|
|
|
queryParams: {
|
2020-05-14 08:08:13 +00:00
|
|
|
startDate: info.startDate,
|
2020-10-01 15:21:22 +00:00
|
|
|
finishDate: info.finishDate,
|
|
|
|
},
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createForm() {
|
|
|
|
this.form = this.fb.group({
|
|
|
|
startDate: '',
|
|
|
|
finishDate: '',
|
2020-10-01 15:21:22 +00:00
|
|
|
product: '',
|
2018-05-25 13:49:00 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-14 08:08:13 +00:00
|
|
|
getInfo(): ProductLedger {
|
2018-05-25 13:49:00 +00:00
|
|
|
const formModel = this.form.value;
|
|
|
|
|
|
|
|
return {
|
|
|
|
product: formModel.product,
|
|
|
|
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-05-14 08:08:13 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
exportCsv() {
|
|
|
|
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-05-25 13:49:00 +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;',
|
|
|
|
});
|
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);
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
}
|