2018-05-25 13:49:00 +00:00
|
|
|
import {Component, OnInit, ViewChild} from '@angular/core';
|
|
|
|
import {FormBuilder, FormGroup} from '@angular/forms';
|
|
|
|
import {MatAutocompleteSelectedEvent, MatPaginator, MatSort} from '@angular/material';
|
|
|
|
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 '../product/product';
|
|
|
|
import {ProductService} from '../product/product.service';
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-product-ledger',
|
|
|
|
templateUrl: './product-ledger.component.html',
|
|
|
|
styleUrls: ['./product-ledger.component.css']
|
|
|
|
})
|
|
|
|
export class ProductLedgerComponent implements OnInit {
|
|
|
|
@ViewChild(MatPaginator) paginator: MatPaginator;
|
|
|
|
@ViewChild(MatSort) 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,
|
|
|
|
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() {
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
|
|
|
const l = this.prepareSubmit();
|
|
|
|
this.router.navigate(['ProductLedger', l.product.id], {
|
|
|
|
queryParams: {
|
|
|
|
startDate: l.startDate,
|
|
|
|
finishDate: l.finishDate
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createForm() {
|
|
|
|
this.form = this.fb.group({
|
|
|
|
startDate: '',
|
|
|
|
finishDate: '',
|
|
|
|
product: ''
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
prepareSubmit(): ProductLedger {
|
|
|
|
const formModel = this.form.value;
|
|
|
|
|
|
|
|
return {
|
|
|
|
product: formModel.product,
|
|
|
|
startDate: moment(formModel.startDate).format('DD-MMM-YYYY'),
|
|
|
|
finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'),
|
|
|
|
body: this.info.body
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|