Files
luthor/otis/src/app/discount-report/discount-report.component.ts
2021-01-05 13:02:52 +05:30

99 lines
2.9 KiB
TypeScript

import { Component, OnInit } from '@angular/core';
import { FormBuilder, 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 { DiscountReport } from './discount-report';
import { DiscountReportDataSource } from './discount-report-datasource';
import { DiscountReportService } from './discount-report.service';
@Component({
selector: 'app-discount-report',
templateUrl: './discount-report.component.html',
styleUrls: ['./discount-report.component.css'],
})
export class DiscountReportComponent implements OnInit {
info: DiscountReport = new DiscountReport();
dataSource: DiscountReportDataSource = new DiscountReportDataSource(this.info.amounts);
form: FormGroup;
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
displayedColumns = ['name', 'amount'];
constructor(
private route: ActivatedRoute,
private router: Router,
private fb: FormBuilder,
private toCsv: ToCsvService,
private toaster: ToasterService,
private ser: DiscountReportService,
) {
// Create form
this.form = this.fb.group({
startDate: '',
finishDate: '',
});
}
ngOnInit() {
this.route.data.subscribe((value) => {
const data = value as { info: DiscountReport };
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 DiscountReportDataSource(this.info.amounts);
});
}
show() {
const info = this.getInfo();
this.router.navigate(['discount-report'], {
queryParams: {
startDate: info.startDate,
finishDate: info.finishDate,
},
});
}
getInfo(): DiscountReport {
const formModel = this.form.value;
return new DiscountReport({
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',
Amount: 'amount',
};
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', 'discount-report.csv');
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
}