|
| 1 | +import dayjs from 'dayjs' |
| 2 | +import type { Context } from 'hono' |
| 3 | +import prisma from '../../prisma.js' |
| 4 | +import type { AuthorizeResults } from './middleware/authorize.js' |
| 5 | + |
| 6 | +const COLUMNS = [ |
| 7 | + 'Mitgliedsnummer', |
| 8 | + 'Datensatztyp', |
| 9 | + 'Rechnungsinkrement', |
| 10 | + 'Rechnungsbezeichnung', |
| 11 | + 'Datum', |
| 12 | + 'Positionsinkrement', |
| 13 | + 'Positionsbezeichnung', |
| 14 | + 'Positionsbeschreibung', |
| 15 | + 'Menge', |
| 16 | + 'Einzelpreis(Brutto)', |
| 17 | + 'Inkasso', |
| 18 | + 'Zustellung', |
| 19 | + 'Zahlungsziel', |
| 20 | + 'Intervall', |
| 21 | + 'Termin', |
| 22 | + 'Fälligkeit', |
| 23 | + 'Ende', |
| 24 | + 'Mwst', |
| 25 | + 'Rechnungsvermerk', |
| 26 | + 'Spendenfähig', |
| 27 | + 'Spendenart', |
| 28 | + 'Buchhaltungskonto', |
| 29 | + 'Steuerschlüssel', |
| 30 | + 'Kostenstelle', |
| 31 | + 'Auswertungskennziffer', |
| 32 | + 'Nachlass', |
| 33 | + 'Nachlassgrund', |
| 34 | + 'Empfänger-Email', |
| 35 | + 'Zusatzinformationen', |
| 36 | +] |
| 37 | + |
| 38 | +function escapeCsvField(value: string): string { |
| 39 | + if (value.includes(';') || value.includes('"') || value.includes('\n')) { |
| 40 | + return `"${value.replace(/"/g, '""')}"` |
| 41 | + } |
| 42 | + return value |
| 43 | +} |
| 44 | + |
| 45 | +export async function veranstaltungRechnungsimport(ctx: Context<{ Variables: AuthorizeResults }>) { |
| 46 | + const { query, gliederung } = ctx.var |
| 47 | + |
| 48 | + if (!query.veranstaltungId) { |
| 49 | + return ctx.text('veranstaltungId is required', 400) |
| 50 | + } |
| 51 | + |
| 52 | + const veranstaltung = await prisma.veranstaltung.findUnique({ |
| 53 | + where: { id: query.veranstaltungId }, |
| 54 | + select: { |
| 55 | + name: true, |
| 56 | + teilnahmegebuehr: true, |
| 57 | + unterveranstaltungen: { |
| 58 | + where: { gliederungId: gliederung?.id }, |
| 59 | + select: { |
| 60 | + Anmeldung: { |
| 61 | + where: { status: 'BESTAETIGT' }, |
| 62 | + select: { |
| 63 | + person: { |
| 64 | + select: { |
| 65 | + email: true, |
| 66 | + }, |
| 67 | + }, |
| 68 | + customFieldValues: { |
| 69 | + select: { |
| 70 | + value: true, |
| 71 | + field: { |
| 72 | + select: { |
| 73 | + name: true, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + }) |
| 84 | + |
| 85 | + if (!veranstaltung) { |
| 86 | + return ctx.text('Veranstaltung not found', 404) |
| 87 | + } |
| 88 | + |
| 89 | + const anmeldungenList = veranstaltung.unterveranstaltungen |
| 90 | + .flatMap((unterveranstaltung) => unterveranstaltung.Anmeldung) |
| 91 | + .sort((a, b) => a.person.email.localeCompare(b.person.email)) |
| 92 | + |
| 93 | + const today = dayjs().format('DD.MM.YYYY') |
| 94 | + const faelligkeit = dayjs().add(14, 'day').format('DD.MM.YYYY') |
| 95 | + |
| 96 | + const rechnungsbezeichnung = veranstaltung.name.substring(0, 70) |
| 97 | + const positionsbezeichnung = `Teilnahmegebühr ${veranstaltung.name}`.substring(0, 70) |
| 98 | + const preis = veranstaltung.teilnahmegebuehr.toFixed(2).replace('.', ',') |
| 99 | + |
| 100 | + const rows = anmeldungenList.map((anmeldung) => { |
| 101 | + const mitgliedsnummerValue = anmeldung.customFieldValues.find( |
| 102 | + (cfv) => cfv.field.name.toLowerCase() === 'mitgliedsnummer' |
| 103 | + )?.value |
| 104 | + const mitgliedsnummer = |
| 105 | + typeof mitgliedsnummerValue === 'string' |
| 106 | + ? mitgliedsnummerValue |
| 107 | + : typeof mitgliedsnummerValue === 'number' |
| 108 | + ? String(mitgliedsnummerValue) |
| 109 | + : '' |
| 110 | + |
| 111 | + return [ |
| 112 | + mitgliedsnummer, |
| 113 | + '2', |
| 114 | + '1', |
| 115 | + rechnungsbezeichnung, |
| 116 | + today, |
| 117 | + '1', |
| 118 | + positionsbezeichnung, |
| 119 | + '', |
| 120 | + '1', |
| 121 | + preis, |
| 122 | + '2', |
| 123 | + '2', |
| 124 | + '', |
| 125 | + '0', |
| 126 | + today, |
| 127 | + faelligkeit, |
| 128 | + '31.12.2099', |
| 129 | + '0', |
| 130 | + '', |
| 131 | + '0', |
| 132 | + '', |
| 133 | + '', |
| 134 | + '', |
| 135 | + '', |
| 136 | + '', |
| 137 | + '', |
| 138 | + '', |
| 139 | + anmeldung.person.email, |
| 140 | + '', |
| 141 | + ] |
| 142 | + }) |
| 143 | + |
| 144 | + const csvLines = [COLUMNS.join(';'), ...rows.map((row) => row.map(escapeCsvField).join(';'))] |
| 145 | + |
| 146 | + const csvContent = '' + csvLines.join('\r\n') |
| 147 | + const filename = `${dayjs().format('YYYYMMDD-HHmm')}-Rechnungsimport.csv` |
| 148 | + |
| 149 | + ctx.header('Content-Disposition', `attachment; filename="${filename}"`) |
| 150 | + ctx.header('Content-Type', 'text/csv; charset=utf-8') |
| 151 | + return ctx.body(csvContent, 200) |
| 152 | +} |
0 commit comments