112 lines
3.1 KiB
TypeScript
112 lines
3.1 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 { User } from '../core/user';
|
|
import { ToCsvService } from '../shared/to-csv.service';
|
|
|
|
import { CashierReport } from './cashier-report';
|
|
import { CashierReportDataSource } from './cashier-report-datasource';
|
|
import { CashierReportService } from './cashier-report.service';
|
|
|
|
@Component({
|
|
selector: 'app-cashier-report',
|
|
templateUrl: './cashier-report.component.html',
|
|
styleUrls: ['./cashier-report.component.css'],
|
|
})
|
|
export class CashierReportComponent implements OnInit {
|
|
dataSource: CashierReportDataSource;
|
|
form: FormGroup;
|
|
activeCashiers: User[];
|
|
info: CashierReport;
|
|
|
|
/** 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: CashierReportService,
|
|
) {
|
|
this.createForm();
|
|
}
|
|
|
|
ngOnInit() {
|
|
this.route.data.subscribe((data: { cashiers: User[]; info: CashierReport }) => {
|
|
this.activeCashiers = data.cashiers;
|
|
this.info = data.info;
|
|
this.form.setValue({
|
|
cashier: this.info.cashier.id,
|
|
startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(),
|
|
finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate(),
|
|
});
|
|
this.dataSource = new CashierReportDataSource(this.info.amounts);
|
|
});
|
|
}
|
|
|
|
show() {
|
|
const info = this.getInfo();
|
|
const url = ['cashier-report'];
|
|
if (info.cashier.id) {
|
|
url.push(info.cashier.id);
|
|
}
|
|
this.router.navigate(url, {
|
|
queryParams: {
|
|
startDate: info.startDate,
|
|
finishDate: info.finishDate,
|
|
},
|
|
});
|
|
}
|
|
|
|
print() {
|
|
this.ser.print(this.info.cashier.id, this.info.startDate, this.info.finishDate).subscribe(
|
|
() => {
|
|
this.toaster.show('', 'Successfully Printed');
|
|
},
|
|
(error) => {
|
|
this.toaster.show('Error', error.error);
|
|
},
|
|
);
|
|
}
|
|
|
|
createForm() {
|
|
this.form = this.fb.group({
|
|
startDate: '',
|
|
finishDate: '',
|
|
cashier: '',
|
|
});
|
|
}
|
|
|
|
getInfo(): CashierReport {
|
|
const formModel = this.form.value;
|
|
|
|
return {
|
|
cashier: new User({ id: formModel.cashier }),
|
|
cashiers: this.info.cashiers,
|
|
startDate: moment(formModel.startDate).format('DD-MMM-YYYY'),
|
|
finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY'),
|
|
};
|
|
}
|
|
|
|
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', 'cashier-report.csv');
|
|
document.body.appendChild(link);
|
|
link.click();
|
|
document.body.removeChild(link);
|
|
}
|
|
}
|