Discount Report Done

This commit is contained in:
Amritanshu
2019-08-21 12:31:52 +05:30
parent 8f6c5930ee
commit 2b04b624b3
18 changed files with 456 additions and 28 deletions
@@ -0,0 +1,90 @@
import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
import * as moment from 'moment';
import { DiscountReportDataSource } from './discount-report-datasource';
import { DiscountReport } from './discount-report';
import { ToCsvService } from '../shared/to-csv.service';
@Component({
selector: 'app-discount-report',
templateUrl: './discount-report.component.html',
styleUrls: ['./discount-report.component.css']
})
export class DiscountReportComponent implements OnInit {
dataSource: DiscountReportDataSource;
form: FormGroup;
info: DiscountReport;
/** 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
) {
this.createForm();
}
ngOnInit() {
this.route.data
.subscribe((data: { 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
}
});
}
createForm() {
this.form = this.fb.group({
startDate: '',
finishDate: ''
});
}
getInfo(): DiscountReport {
const formModel = this.form.value;
return {
startDate: moment(formModel.startDate).format('DD-MMM-YYYY'),
finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY')
};
}
exportCsv() {
const headers = {
Date: 'date',
Name: 'name',
Type: 'type',
Narration: 'narration',
Debit: 'debit',
Credit: 'credit',
Running: 'running',
Posted: 'posted'
};
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);
}
}