Skip to content

Commit 9a202d9

Browse files
authored
Added Summary sheet to exported strecklists (#299)
1 parent 08dbbec commit 9a202d9

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

src/server/trpc/router/streck.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -663,6 +663,51 @@ export const streckRouter = router({
663663
]);
664664
}
665665

666+
interface Summary {
667+
total: number;
668+
amount: number;
669+
}
670+
671+
const summarySheet = workbook.addWorksheet('Sammanfattning', {
672+
pageSetup: {
673+
paperSize: 9,
674+
orientation: 'portrait',
675+
printTitlesRow: `${headerRow}:${headerRow}`,
676+
},
677+
});
678+
679+
const summaryHeader = summarySheet.getRow(headerRow);
680+
summaryHeader.values = ['Artikel', 'Styckpris', 'Antal', 'Totalpris'];
681+
summaryHeader.font = {
682+
name: 'Arial',
683+
bold: true,
684+
};
685+
summarySheet.getColumn(1).width = 19;
686+
summarySheet.getColumn(2).width = 11;
687+
summarySheet.getColumn(4).width = 11;
688+
689+
const summaries = streckList.transactions.reduce((acc, transaction) => {
690+
const prev = acc.get(transaction.item);
691+
acc.set(transaction.item, {
692+
total: (prev?.total ?? 0) + transaction.totalPrice,
693+
amount: (prev?.amount ?? 0) + transaction.amount,
694+
});
695+
return acc;
696+
}, new Map<string, Summary>());
697+
698+
for (const item of items) {
699+
const summary = summaries.get(item.name);
700+
if (!summary) {
701+
continue;
702+
}
703+
summarySheet.addRow([
704+
item.name,
705+
item.pricePer,
706+
summary.amount,
707+
-summary.total,
708+
]);
709+
}
710+
666711
const filename = `Strecklista_${dayjs(streckList.time)
667712
.locale('sv')
668713
.format('YYYY-MM-DD_HH-mm')}.xlsx`;

0 commit comments

Comments
 (0)