barker/bookie/src/app/product-sale-report/product-sale-report.compone...

105 lines
3.2 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import * as moment from 'moment';
import { ToasterService } from '../core/toaster.service';
import { ToCsvService } from '../shared/to-csv.service';
import { ProductSaleReport } from './product-sale-report';
import { ProductSaleReportDataSource } from './product-sale-report-datasource';
import { ProductSaleReportService } from './product-sale-report.service';
@Component({
selector: 'app-product-sale-report',
templateUrl: './product-sale-report.component.html',
styleUrls: ['./product-sale-report.component.css'],
})
export class ProductSaleReportComponent implements OnInit {
info: ProductSaleReport = new ProductSaleReport();
dataSource: ProductSaleReportDataSource = new ProductSaleReportDataSource(this.info.amounts);
form: FormGroup<{
startDate: FormControl<Date>;
finishDate: FormControl<Date>;
}>;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['name', 'unbilled', 'sale', 'noCharge', 'staff', 'cancel'];
constructor(
private route: ActivatedRoute,
private router: Router,
private toCsv: ToCsvService,
private toaster: ToasterService,
private ser: ProductSaleReportService,
) {
// Create form
this.form = new FormGroup({
startDate: new FormControl(new Date(), { nonNullable: true }),
finishDate: new FormControl(new Date(), { nonNullable: true }),
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { info: ProductSaleReport };
this.info = data.info;
this.form.setValue({
startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(),
finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(),
});
this.dataSource = new ProductSaleReportDataSource(this.info.amounts);
});
}
show() {
const info = this.getInfo();
this.router.navigate(['product-sale-report'], {
queryParams: {
startDate: info.startDate,
finishDate: info.finishDate,
},
});
}
getInfo(): ProductSaleReport {
const formModel = this.form.value;
return new ProductSaleReport({
startDate: moment(formModel.startDate).format('DD-MMM-YYYY'),
finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'),
});
}
print() {
this.ser.print(this.info.startDate, this.info.finishDate).subscribe(
() => {
this.toaster.show('', 'Successfully Printed');
},
(error) => {
this.toaster.show('Error', error);
},
);
}
exportCsv() {
const headers = {
Name: 'name',
Unbilled: 'kot',
Sold: 'regularBill',
'No Charge': 'noCharge',
Staff: 'staff',
Void: 'void',
};
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', 'product-sale-report.csv');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}