121 lines
3.8 KiB
TypeScript
121 lines
3.8 KiB
TypeScript
import { CurrencyPipe } from '@angular/common';
|
|
import { Component, inject, computed, linkedSignal, input } from '@angular/core';
|
|
import { form, FormField } from '@angular/forms/signals';
|
|
import { MatButtonModule } from '@angular/material/button';
|
|
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 { MatSnackBar } from '@angular/material/snack-bar';
|
|
import { MatTableModule } from '@angular/material/table';
|
|
import { Router } from '@angular/router';
|
|
import { DateTime } from 'luxon';
|
|
|
|
import { ErrorStateComponent } from '../shared/error-state/error-state.component';
|
|
import { SkeletonLoaderComponent } from '../shared/skeleton-loader/skeleton-loader.component';
|
|
import { ToCsvService } from '../shared/to-csv.service';
|
|
import { DiscountReport } from './discount-report';
|
|
import { DiscountReportService } from './discount-report.service';
|
|
|
|
export interface DiscountReportFormData {
|
|
startDate: DateTime;
|
|
finishDate: DateTime;
|
|
}
|
|
|
|
@Component({
|
|
selector: 'app-discount-report',
|
|
templateUrl: './discount-report.component.html',
|
|
styleUrls: ['./discount-report.component.css'],
|
|
imports: [
|
|
CurrencyPipe,
|
|
MatButtonModule,
|
|
MatDatepickerModule,
|
|
MatFormFieldModule,
|
|
MatIconModule,
|
|
MatInputModule,
|
|
MatTableModule,
|
|
FormField,
|
|
ErrorStateComponent,
|
|
SkeletonLoaderComponent,
|
|
],
|
|
})
|
|
export class DiscountReportComponent {
|
|
private router = inject(Router);
|
|
private toCsv = inject(ToCsvService);
|
|
private snackBar = inject(MatSnackBar);
|
|
private ser = inject(DiscountReportService);
|
|
startDate = input(null, { transform: (v: string | null | undefined) => v ?? null });
|
|
finishDate = input(null, { transform: (v: string | null | undefined) => v ?? null });
|
|
|
|
infoResource = this.ser.get(this.startDate, this.finishDate);
|
|
|
|
info = computed(() => this.infoResource.value() ?? new DiscountReport());
|
|
dataSource = computed(() => this.info().amounts);
|
|
formModel = linkedSignal({
|
|
source: this.info,
|
|
computation: (infoVal): DiscountReportFormData => {
|
|
if (infoVal) {
|
|
return {
|
|
startDate: DateTime.fromFormat(this.info().startDate, 'dd-MMM-yyyy'),
|
|
finishDate: DateTime.fromFormat(this.info().finishDate, 'dd-MMM-yyyy'),
|
|
};
|
|
}
|
|
return {
|
|
startDate: DateTime.now(),
|
|
finishDate: DateTime.now(),
|
|
};
|
|
},
|
|
});
|
|
|
|
form = form(this.formModel);
|
|
|
|
/** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */
|
|
displayedColumns = ['name', 'amount'];
|
|
|
|
show() {
|
|
const info = this.getInfo();
|
|
this.router.navigate(['discount-report'], {
|
|
queryParams: {
|
|
startDate: info.startDate,
|
|
finishDate: info.finishDate,
|
|
},
|
|
});
|
|
}
|
|
|
|
getInfo(): DiscountReport {
|
|
const formModel = this.formModel();
|
|
|
|
return new DiscountReport({
|
|
startDate: formModel.startDate.toFormat('dd-MMM-yyyy'),
|
|
finishDate: formModel.finishDate.toFormat('dd-MMM-yyyy'),
|
|
});
|
|
}
|
|
|
|
print() {
|
|
this.ser.print(this.info().startDate, this.info().finishDate).subscribe({
|
|
next: () => {
|
|
this.snackBar.open('Successfully Printed', '');
|
|
},
|
|
error: (error: unknown) => {
|
|
this.snackBar.open(error as string, 'Error');
|
|
},
|
|
});
|
|
}
|
|
|
|
exportCsv() {
|
|
const headers = {
|
|
Name: 'name',
|
|
Amount: 'amount',
|
|
};
|
|
const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource())], {
|
|
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);
|
|
}
|
|
}
|