import { DecimalPipe } from '@angular/common'; import { Component, OnInit } from '@angular/core'; import { FormControl, FormGroup, ReactiveFormsModule } from '@angular/forms'; import { MatButtonModule } from '@angular/material/button'; import { MatCardModule } from '@angular/material/card'; import { MatDatepickerModule } from '@angular/material/datepicker'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatIconModule } from '@angular/material/icon'; import { MatInputModule } from '@angular/material/input'; import { MatSelectModule } from '@angular/material/select'; import { MatTableModule } from '@angular/material/table'; import { ActivatedRoute, Router } from '@angular/router'; import moment from 'moment'; import { Section } from '../core/section'; 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'], imports: [ MatCardModule, MatButtonModule, ReactiveFormsModule, MatFormFieldModule, MatInputModule, MatIconModule, MatDatepickerModule, MatTableModule, MatSelectModule, DecimalPipe, ], }) export class ProductSaleReportComponent implements OnInit { sections: Section[] = []; info: ProductSaleReport = new ProductSaleReport(); dataSource: ProductSaleReportDataSource = new ProductSaleReportDataSource(this.info.amounts); form: FormGroup<{ startDate: FormControl; finishDate: FormControl; section: FormControl; }>; /** 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 }), section: new FormControl(null), }); } ngOnInit() { this.route.data.subscribe((value) => { const data = value as { sections: Section[]; info: ProductSaleReport }; this.sections = data.sections; 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(), section: this.info.sectionId || null, }); this.dataSource = new ProductSaleReportDataSource(this.info.amounts); }); } show() { const info = this.getInfo(); const params = { startDate: info.startDate, finishDate: info.finishDate, } as { startDate: string; finishDate: string; section?: string }; if (info.sectionId) { params['section'] = info.sectionId; } this.router.navigate(['product-sale-report'], { queryParams: params, }); } getInfo(): ProductSaleReport { const formModel = this.form.value; const data = new ProductSaleReport({ startDate: moment(formModel.startDate).format('DD-MMM-YYYY'), finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'), }); if (formModel.section) { data.sectionId = formModel.section; } return data; } print() { this.ser.print(this.info.startDate, this.info.finishDate, this.info.sectionId).subscribe({ next: () => { this.toaster.show('', 'Successfully Printed'); }, error: (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); } }