2020-10-01 15:21:22 +00:00
|
|
|
import { AfterViewInit, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
|
2022-07-15 07:54:25 +00:00
|
|
|
import { FormControl, 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';
|
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';
|
2022-07-17 09:01:19 +00:00
|
|
|
import { debounceTime, distinctUntilChanged, switchMap } from 'rxjs/operators';
|
2020-10-10 03:15:05 +00:00
|
|
|
|
2020-10-01 15:21:22 +00:00
|
|
|
import { Product } from '../core/product';
|
2021-11-02 08:20:35 +00:00
|
|
|
import { ProductSku } from '../core/product-sku';
|
2020-10-01 15:21:22 +00:00
|
|
|
import { ProductService } from '../product/product.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 { ProductLedger } from './product-ledger';
|
|
|
|
import { ProductLedgerDataSource } from './product-ledger-datasource';
|
|
|
|
import { ProductLedgerService } from './product-ledger.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 {
|
2020-11-23 04:55:53 +00:00
|
|
|
@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);
|
2022-07-15 07:54:25 +00:00
|
|
|
form: FormGroup<{
|
|
|
|
startDate: FormControl<Date>;
|
|
|
|
finishDate: FormControl<Date>;
|
2022-07-17 09:01:19 +00:00
|
|
|
product: FormControl<string | null>;
|
2022-07-15 07:54:25 +00:00
|
|
|
}>;
|
|
|
|
|
2020-11-23 11:12:54 +00:00
|
|
|
selectedRowId = '';
|
|
|
|
debitQuantity = 0;
|
|
|
|
debitAmount = 0;
|
|
|
|
creditQuantity = 0;
|
|
|
|
creditAmount = 0;
|
|
|
|
runningQuantity = 0;
|
|
|
|
runningAmount = 0;
|
2018-05-25 13:49:00 +00:00
|
|
|
/** 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
|
|
|
];
|
|
|
|
|
2021-11-02 08:20:35 +00:00
|
|
|
products: Observable<ProductSku[]>;
|
2018-05-25 13:49:00 +00:00
|
|
|
|
|
|
|
constructor(
|
|
|
|
private route: ActivatedRoute,
|
|
|
|
private router: Router,
|
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
|
|
|
) {
|
2022-07-15 07:54:25 +00:00
|
|
|
this.form = new FormGroup({
|
|
|
|
startDate: new FormControl(new Date(), { nonNullable: true }),
|
|
|
|
finishDate: new FormControl(new Date(), { nonNullable: true }),
|
2022-07-17 09:01:19 +00:00
|
|
|
product: new FormControl<string | null>(null),
|
2020-11-23 03:48:02 +00:00
|
|
|
});
|
2018-05-25 13:49:00 +00:00
|
|
|
|
2022-07-15 07:54:25 +00:00
|
|
|
this.products = this.form.controls.product.valueChanges.pipe(
|
2020-10-01 15:21:22 +00:00
|
|
|
debounceTime(150),
|
|
|
|
distinctUntilChanged(),
|
2021-09-27 04:01:58 +00:00
|
|
|
switchMap((x) =>
|
2021-11-02 08:20:35 +00:00
|
|
|
x === null ? observableOf([]) : this.productSer.autocompleteProduct(x, null),
|
2021-09-27 04:01:58 +00:00
|
|
|
),
|
2020-10-01 15:21:22 +00:00
|
|
|
);
|
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: ProductLedger };
|
|
|
|
|
2020-10-01 15:21:22 +00:00
|
|
|
this.info = data.info;
|
|
|
|
this.calculateTotals();
|
|
|
|
this.form.setValue({
|
2022-07-17 09:01:19 +00:00
|
|
|
product: this.info.product.name,
|
2020-10-01 15:21:22 +00:00
|
|
|
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 ProductLedgerDataSource(this.info.body, this.paginator, this.sort);
|
2020-10-01 15:21:22 +00:00
|
|
|
});
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
2020-05-14 08:08:13 +00:00
|
|
|
ngAfterViewInit() {
|
|
|
|
setTimeout(() => {
|
2020-12-08 06:39:19 +00:00
|
|
|
if (this.productElement) {
|
|
|
|
this.productElement.nativeElement.focus();
|
|
|
|
}
|
2020-05-14 08:08:13 +00:00
|
|
|
}, 0);
|
|
|
|
}
|
|
|
|
|
2022-07-17 09:01:19 +00:00
|
|
|
displayFn(product?: Product | string): string {
|
|
|
|
return !product ? '' : typeof product === 'string' ? product : product.name;
|
2018-05-25 13:49:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2022-07-15 07:54:25 +00:00
|
|
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-05-14 08:08:13 +00:00
|
|
|
getInfo(): ProductLedger {
|
2018-05-25 13:49:00 +00:00
|
|
|
const formModel = this.form.value;
|
|
|
|
|
2020-11-23 11:12:54 +00:00
|
|
|
return new ProductLedger({
|
2022-07-17 09:01:19 +00:00
|
|
|
product: this.info.product,
|
2018-05-25 13:49:00 +00:00
|
|
|
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
|
|
|
});
|
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
|
|
|
}
|
|
|
|
}
|