Skip to content

reports: add PDF button(fixes #8603) #8604

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@
"date-fns": "^2.29.2",
"export-to-csv": "^0.2.1",
"html-to-pdfmake": "^1.2.0",
"jspdf": "^3.0.1",
"jspdf-autotable": "^5.0.2",
"jszip": "^3.2.2",
"material-design-icons": "^3.0.1",
"material-icons": "^0.3.1",
Expand Down
3 changes: 3 additions & 0 deletions src/app/teams/teams-reports.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<button mat-raised-button color="accent" i18n *ngIf="reports && reports.length > 0" (click)="exportReports()">
Export as CSV
</button>
<button mat-raised-button color="warn" i18n *ngIf="reports && reports.length > 0" (click)="exportReportsAsPdf()">
Export as PDF
</button>
</div>
<div *ngIf="!reports || reports?.length == 0">
<p i18n>No reports available at the moment.</p>
Expand Down
65 changes: 49 additions & 16 deletions src/app/teams/teams-reports.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ import { convertUtcDate } from './teams.utils';
import { CsvService } from '../shared/csv.service';
import { StateService } from '../shared/state.service';
import { PlanetMessageService } from '../shared/planet-message.service';
import { jsPDF } from 'jspdf';
import 'jspdf-autotable';
import autoTable from 'jspdf-autotable';

@Component({
selector: 'planet-teams-reports',
styleUrls: [ './teams-reports.scss' ],
styleUrls: ['./teams-reports.scss'],
templateUrl: './teams-reports.component.html'
})
export class TeamsReportsComponent implements DoCheck {
Expand Down Expand Up @@ -56,7 +59,6 @@ export class TeamsReportsComponent implements DoCheck {
openAddReportDialog(oldReport = {}, isEdit: boolean) {
const actionType = isEdit ? 'Edit' : 'Add';
const dialogTitle = $localize`${actionType} Report`;

this.couchService.currentTime().subscribe((time: number) => {
const currentDate = new Date(time);
const lastMonthStart = new Date(currentDate.getFullYear(), currentDate.getMonth() - 1, 1);
Expand Down Expand Up @@ -100,7 +102,6 @@ export class TeamsReportsComponent implements DoCheck {
deleteDialog.close();
},
onError: () => {
this.planetMessageService.showAlert('There was a problem deleting the report.');
this.dialogsLoadingService.stop();
}
},
Expand Down Expand Up @@ -131,25 +132,21 @@ export class TeamsReportsComponent implements DoCheck {
}

addFormValidator(fieldName) {
return fieldName === 'endDate' ?
CustomValidators.endDateValidator() :
[ 'sales', 'otherIncome', 'wages', 'otherExpenses' ].indexOf(fieldName) > -1 ?
Validators.min(0) :
() => {};
return fieldName === 'endDate'
? CustomValidators.endDateValidator()
: [ 'sales', 'otherIncome', 'wages', 'otherExpenses' ].indexOf(fieldName) > -1
? Validators.min(0)
: () => {};
}

updateReport(oldReport, newReport: any = {}) {
const dateFields = [ 'startDate', 'endDate' ];
const numberFields = [ 'beginningBalance', 'sales', 'otherIncome', 'wages', 'otherExpenses' ];
const transformFields = (key: string, value: Date | string) => dateFields.indexOf(key) > -1 ?
(<Date>value).getTime() :
numberFields.indexOf(key) > -1 ?
+value :
const transformFields = (key: string, value: Date | string) =>
dateFields.indexOf(key) > -1 ? (<Date>value).getTime() :
numberFields.indexOf(key) > -1 ? +value :
value;
const { _id, _rev, ...newDoc } = <any>Object.entries(newReport).reduce(
(obj, [ key, value ]: [ string, Date | string ]) => ({ ...obj, [key]: transformFields(key, value) }),
{}
);
const { _id, _rev, ...newDoc } = newReport;
const docs = [ { ...oldReport, status: 'archived' }, newDoc ].filter(doc => doc.startDate !== undefined);
return this.teamsService.updateAdditionalDocs(docs, this.team, 'report', { utcKeys: dateFields }).pipe(tap(() => {
this.reportsChanged.emit();
Expand Down Expand Up @@ -187,4 +184,40 @@ export class TeamsReportsComponent implements DoCheck {
});
}

exportReportsAsPdf() {
if (!this.reports?.length) { return; }
const doc = new jsPDF({ unit: 'pt', format: 'a4' });
const title = this.team.name || 'Reports';
doc.text(title, 40, 50);

const head = [
'Start Date',
'End Date',
'Beginning Balance',
'Sales',
'Other Income',
'Personnel',
'Non-Personnel',
'Profit/Loss',
'Ending Balance'
];

const body = this.reports.map(r => {
const start = new Date(r.startDate).toLocaleDateString();
const end = new Date(r.endDate).toLocaleDateString();
const profit = r.sales + r.otherIncome - r.wages - r.otherExpenses;
const ending = r.beginningBalance + r.sales + r.otherIncome - r.wages - r.otherExpenses;
return [ start, end, r.beginningBalance, r.sales, r.otherIncome, r.wages, r.otherExpenses, profit, ending ];
});

autoTable(doc, {
startY: 70,
head: [ head ],
body: body,
styles: { fontSize: 10, cellPadding: 4 },
headStyles: { fillColor: [22, 160, 133] }
});

doc.save(`${title}.pdf`);
}
}
Loading