149 lines
4.2 KiB
TypeScript
149 lines
4.2 KiB
TypeScript
import { CurrencyPipe } from '@angular/common';
|
|
import { Component, OnInit } from '@angular/core';
|
|
import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms';
|
|
import { MatIconButton, MatButton } from '@angular/material/button';
|
|
import { MatCard, MatCardHeader, MatCardTitleGroup, MatCardTitle, MatCardContent } from '@angular/material/card';
|
|
import { MatDatepickerInput, MatDatepickerToggle, MatDatepicker } from '@angular/material/datepicker';
|
|
import { MatFormField, MatLabel, MatSuffix } from '@angular/material/form-field';
|
|
import { MatIcon } from '@angular/material/icon';
|
|
import { MatInput } from '@angular/material/input';
|
|
import {
|
|
MatTable,
|
|
MatColumnDef,
|
|
MatHeaderCellDef,
|
|
MatHeaderCell,
|
|
MatCellDef,
|
|
MatCell,
|
|
MatHeaderRowDef,
|
|
MatHeaderRow,
|
|
MatRowDef,
|
|
MatRow,
|
|
} from '@angular/material/table';
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
|
import 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'],
|
|
imports: [
|
|
MatCard,
|
|
MatCardHeader,
|
|
MatCardTitleGroup,
|
|
MatCardTitle,
|
|
MatIconButton,
|
|
MatIcon,
|
|
MatCardContent,
|
|
ReactiveFormsModule,
|
|
MatFormField,
|
|
MatLabel,
|
|
MatInput,
|
|
MatDatepickerInput,
|
|
MatDatepickerToggle,
|
|
MatSuffix,
|
|
MatDatepicker,
|
|
MatButton,
|
|
MatTable,
|
|
MatColumnDef,
|
|
MatHeaderCellDef,
|
|
MatHeaderCell,
|
|
MatCellDef,
|
|
MatCell,
|
|
MatHeaderRowDef,
|
|
MatHeaderRow,
|
|
MatRowDef,
|
|
MatRow,
|
|
CurrencyPipe,
|
|
],
|
|
})
|
|
export class DiscountReportComponent implements OnInit {
|
|
info: DiscountReport = new DiscountReport();
|
|
dataSource: DiscountReportDataSource = new DiscountReportDataSource(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', 'amount'];
|
|
|
|
constructor(
|
|
private route: ActivatedRoute,
|
|
private router: Router,
|
|
private toCsv: ToCsvService,
|
|
private toaster: ToasterService,
|
|
private ser: DiscountReportService,
|
|
) {
|
|
// 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: 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({
|
|
next: () => {
|
|
this.toaster.show('', 'Successfully Printed');
|
|
},
|
|
error: (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);
|
|
}
|
|
}
|