|
|
|
|
@@ -23,7 +23,8 @@ import {
|
|
|
|
|
vendors,
|
|
|
|
|
bankaccounts,
|
|
|
|
|
bankstatements,
|
|
|
|
|
ownaccounts
|
|
|
|
|
ownaccounts,
|
|
|
|
|
costcentres
|
|
|
|
|
} from "../../../db/schema";
|
|
|
|
|
|
|
|
|
|
dayjs.extend(isBetween);
|
|
|
|
|
@@ -80,6 +81,29 @@ const formatDatevDate = (date: dayjs.ConfigType, format: string) => {
|
|
|
|
|
return parsed.isValid() ? parsed.tz(DATEV_TIMEZONE).format(format) : "";
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getBankAccountBookingLine = (alloc: any) => {
|
|
|
|
|
const bs = alloc.bankstatement;
|
|
|
|
|
const account = alloc.account || alloc.ownaccount;
|
|
|
|
|
const columns = Array(124).fill("");
|
|
|
|
|
const quote = (value: any) => `"${escapeString(String(value ?? ""))}"`;
|
|
|
|
|
columns[0] = displayCurrency(Number(alloc.amount), true);
|
|
|
|
|
columns[1] = quote(Number(alloc.amount) < 0 ? "H" : "S");
|
|
|
|
|
columns[6] = bs.account?.datevNumber || "";
|
|
|
|
|
columns[7] = account.number;
|
|
|
|
|
columns[8] = quote(alloc.datevTaxKey || "");
|
|
|
|
|
columns[9] = formatDatevDate(bs.date, "DDMM");
|
|
|
|
|
columns[13] = quote(`${Number(alloc.amount) > 0 ? "ZE" : "ZA"} ${account.number} - ${account.label || account.name || ""} ${alloc.description || ""} ${bs.text || ""}`.substring(0, 59));
|
|
|
|
|
columns[20] = quote("Geschäftspartner");
|
|
|
|
|
columns[21] = quote(bs.debName || bs.credName || "");
|
|
|
|
|
columns[28] = quote("Belegdatum");
|
|
|
|
|
columns[29] = quote(formatDatevDate(bs.date, "DD.MM.YYYY"));
|
|
|
|
|
columns[36] = quote(alloc.costcentre?.number || "");
|
|
|
|
|
columns[47] = "Bank-Id";
|
|
|
|
|
columns[48] = bs.id;
|
|
|
|
|
columns[112] = "0";
|
|
|
|
|
return columns.join(";");
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getIncomingInvoiceTaxOverride = (invoice: any) => {
|
|
|
|
|
if (invoice.taxAmountOverride === null || invoice.taxAmountOverride === undefined || invoice.taxAmountOverride === "") return null;
|
|
|
|
|
|
|
|
|
|
@@ -136,7 +160,7 @@ export async function buildExportZip(
|
|
|
|
|
const endDateFmt = formatDatevDate(endDate, "YYYYMMDD");
|
|
|
|
|
|
|
|
|
|
let header = `"EXTF";700;21;"Buchungsstapel";13;${dateNowStr};;"FE";"Florian Federspiel";;${beraternr};${mandantennr};20250101;4;${startDateFmt};${endDateFmt};"Buchungsstapel";"FF";1;0;1;"EUR";;"";;;"03";;;"";""`;
|
|
|
|
|
let colHeaders = `Umsatz;Soll-/Haben-Kennzeichen;WKZ Umsatz;Kurs;Basisumsatz;WKZ Basisumsatz;Konto;Gegenkonto;BU-Schluessel;Belegdatum;Belegfeld 1;Belegfeld 2;Skonto;Buchungstext;Postensperre;Diverse Adressnummer;Geschaeftspartnerbank;Sachverhalt;Zinssperre;Beleglink;Beleginfo - Art 1;Beleginfo - Inhalt 1;Beleginfo - Art 2;Beleginfo - Inhalt 2;Beleginfo - Art 3;Beleginfo - Inhalt 3;Beleginfo - Art 4;Beleginfo - Inhalt 4;Beleginfo - Art 5;Beleginfo - Inhalt 5;Beleginfo - Art 6;Beleginfo - Inhalt 6;Beleginfo - Art 7;Beleginfo - Inhalt 7;Beleginfo - Art 8;Beleginfo - Inhalt 8;KOST1 - Kostenstelle;KOST2 - Kostenstelle;Kost Menge;EU-Land u. USt-IdNr. (Bestimmung);EU-Steuersatz (Bestimmung);Abw. Versteuerungsart;Sachverhalt L+L;Funktionsergaenzung L+L;BU 49 Hauptfunktionstyp;BU 49 Hauptfunktionsnummer;BU 49 Funktionsergaenzung;Zusatzinformation - Art 1;Zusatzinformation - Inhalt 1;Zusatzinformation - Art 2;Zusatzinformation - Inhalt 2;Zusatzinformation - Art 3;Zusatzinformation - Inhalt 3;Zusatzinformation - Art 4;Zusatzinformation - Inhalt 4;Zusatzinformation - Art 5;Zusatzinformation - Inhalt 5;Zusatzinformation - Art 6;Zusatzinformation - Inhalt 6;Zusatzinformation - Art 7;Zusatzinformation - Inhalt 7;Zusatzinformation - Art 8;Zusatzinformation - Inhalt 8;Zusatzinformation - Art 9;Zusatzinformation - Inhalt 9;Zusatzinformation - Art 10;Zusatzinformation - Inhalt 10;Zusatzinformation - Art 11;Zusatzinformation - Inhalt 11;Zusatzinformation - Art 12;Zusatzinformation - Inhalt 12;Zusatzinformation - Art 13;Zusatzinformation - Inhalt 13;Zusatzinformation - Art 14;Zusatzinformation - Inhalt 14;Zusatzinformation - Art 15;Zusatzinformation - Inhalt 15;Zusatzinformation - Art 16;Zusatzinformation - Inhalt 16;Zusatzinformation - Art 17;Zusatzinformation - Inhalt 17;Zusatzinformation - Art 18;Zusatzinformation - Inhalt 18;Zusatzinformation - Art 19;Zusatzinformation - Inhalt 19;Zusatzinformation - Art 20;Zusatzinformation - Inhalt 20;Stueck;Gewicht;Zahlweise;Zahlweise;Veranlagungsjahr;Zugeordnete Faelligkeit;Skontotyp;Auftragsnummer;Buchungstyp;USt-Schluessel (Anzahlungen);EU-Mitgliedstaat (Anzahlungen);Sachverhalt L+L (Anzahlungen);EU-Steuersatz (Anzahlungen);Erloeskonto (Anzahlungen);Herkunft-Kz;Leerfeld;KOST-Datum;SEPA-Mandatsreferenz;Skontosperre;Gesellschaftername;Beteiligtennummer;Identifikationsnummer;Zeichnernummer;Postensperre bis;Bezeichnung SoBil-Sachverhalt;Kennzeichen SoBil-Buchung;Festschreibung;Leistungsdatum;Datum Zuord. Steuerperiode;Faelligkeit;Generalumkehr;Steuersatz;Land;Abrechnungsreferenz;BVV-Position;EU-Mitgliedstaat u. UStID(Ursprung);EU-Steuersatz(Ursprung);Abw. Skontokonto`;
|
|
|
|
|
let colHeaders = `Umsatz;Soll-/Haben-Kennzeichen;WKZ Umsatz;Kurs;Basisumsatz;WKZ Basisumsatz;Konto;Gegenkonto;BU-Schluessel;Belegdatum;Belegfeld 1;Belegfeld 2;Skonto;Buchungstext;Postensperre;Diverse Adressnummer;Geschaeftspartnerbank;Sachverhalt;Zinssperre;Beleglink;Beleginfo - Art 1;Beleginfo - Inhalt 1;Beleginfo - Art 2;Beleginfo - Inhalt 2;Beleginfo - Art 3;Beleginfo - Inhalt 3;Beleginfo - Art 4;Beleginfo - Inhalt 4;Beleginfo - Art 5;Beleginfo - Inhalt 5;Beleginfo - Art 6;Beleginfo - Inhalt 6;Beleginfo - Art 7;Beleginfo - Inhalt 7;Beleginfo - Art 8;Beleginfo - Inhalt 8;KOST1 - Kostenstelle;KOST2 - Kostenstelle;Kost Menge;EU-Land u. USt-IdNr. (Bestimmung);EU-Steuersatz (Bestimmung);Abw. Versteuerungsart;Sachverhalt L+L;Funktionsergaenzung L+L;BU 49 Hauptfunktionstyp;BU 49 Hauptfunktionsnummer;BU 49 Funktionsergaenzung;Zusatzinformation - Art 1;Zusatzinformation - Inhalt 1;Zusatzinformation - Art 2;Zusatzinformation - Inhalt 2;Zusatzinformation - Art 3;Zusatzinformation - Inhalt 3;Zusatzinformation - Art 4;Zusatzinformation - Inhalt 4;Zusatzinformation - Art 5;Zusatzinformation - Inhalt 5;Zusatzinformation - Art 6;Zusatzinformation - Inhalt 6;Zusatzinformation - Art 7;Zusatzinformation - Inhalt 7;Zusatzinformation - Art 8;Zusatzinformation - Inhalt 8;Zusatzinformation - Art 9;Zusatzinformation - Inhalt 9;Zusatzinformation - Art 10;Zusatzinformation - Inhalt 10;Zusatzinformation - Art 11;Zusatzinformation - Inhalt 11;Zusatzinformation - Art 12;Zusatzinformation - Inhalt 12;Zusatzinformation - Art 13;Zusatzinformation - Inhalt 13;Zusatzinformation - Art 14;Zusatzinformation - Inhalt 14;Zusatzinformation - Art 15;Zusatzinformation - Inhalt 15;Zusatzinformation - Art 16;Zusatzinformation - Inhalt 16;Zusatzinformation - Art 17;Zusatzinformation - Inhalt 17;Zusatzinformation - Art 18;Zusatzinformation - Inhalt 18;Zusatzinformation - Art 19;Zusatzinformation - Inhalt 19;Zusatzinformation - Art 20;Zusatzinformation - Inhalt 20;Stueck;Gewicht;Zahlweise;Veranlagungsjahr;Zugeordnete Faelligkeit;Skontotyp;Auftragsnummer;Buchungstyp;USt-Schluessel (Anzahlungen);EU-Mitgliedstaat (Anzahlungen);Sachverhalt L+L (Anzahlungen);EU-Steuersatz (Anzahlungen);Erloeskonto (Anzahlungen);Herkunft-Kz;Leerfeld;KOST-Datum;SEPA-Mandatsreferenz;Skontosperre;Gesellschaftername;Beteiligtennummer;Identifikationsnummer;Zeichnernummer;Postensperre bis;Bezeichnung SoBil-Sachverhalt;Kennzeichen SoBil-Buchung;Festschreibung;Leistungsdatum;Datum Zuord. Steuerperiode;Faelligkeit;Generalumkehr;Steuersatz;Land;Abrechnungsreferenz;BVV-Position;EU-Mitgliedstaat u. UStID(Ursprung);EU-Steuersatz(Ursprung);Abw. Skontokonto`;
|
|
|
|
|
|
|
|
|
|
// ---------------------------------------------------------
|
|
|
|
|
// 1. DATEN LADEN (CORE API SELECT & JOIN)
|
|
|
|
|
@@ -209,6 +233,7 @@ export async function buildExportZip(
|
|
|
|
|
direct_vend: vendors, // Direkte Zuordnung an Kreditor
|
|
|
|
|
direct_cust: customers, // Direkte Zuordnung an Debitor
|
|
|
|
|
own: ownaccounts,
|
|
|
|
|
costcentre: costcentres,
|
|
|
|
|
contra_acc: ContraAccount,
|
|
|
|
|
contra_vend: ContraVendor,
|
|
|
|
|
contra_cust: ContraCustomer,
|
|
|
|
|
@@ -233,6 +258,7 @@ export async function buildExportZip(
|
|
|
|
|
.leftJoin(vendors, eq(statementallocations.vendor, vendors.id))
|
|
|
|
|
.leftJoin(customers, eq(statementallocations.customer, customers.id))
|
|
|
|
|
.leftJoin(ownaccounts, eq(statementallocations.ownaccount, ownaccounts.id))
|
|
|
|
|
.leftJoin(costcentres, and(eq(statementallocations.costcentre, costcentres.id), eq(costcentres.tenant, tenantId)))
|
|
|
|
|
.leftJoin(ContraAccount, eq(statementallocations.contraAccount, ContraAccount.id))
|
|
|
|
|
.leftJoin(ContraVendor, eq(statementallocations.contraVendor, ContraVendor.id))
|
|
|
|
|
.leftJoin(ContraCustomer, eq(statementallocations.contraCustomer, ContraCustomer.id))
|
|
|
|
|
@@ -257,10 +283,10 @@ export async function buildExportZip(
|
|
|
|
|
// Mapping: Wir bauen das komplexe Objekt nach, das die CSV Logik erwartet
|
|
|
|
|
const statementallocationsList = allocRaw.map(r => ({
|
|
|
|
|
...r.allocation,
|
|
|
|
|
bankstatement: {
|
|
|
|
|
bankstatement: r.bs ? {
|
|
|
|
|
...r.bs,
|
|
|
|
|
account: r.ba // Nesting für bs.account.datevNumber
|
|
|
|
|
},
|
|
|
|
|
} : null,
|
|
|
|
|
createddocument: r.cd ? {
|
|
|
|
|
...r.cd,
|
|
|
|
|
customer: r.cd_cust
|
|
|
|
|
@@ -273,6 +299,7 @@ export async function buildExportZip(
|
|
|
|
|
vendor: r.direct_vend,
|
|
|
|
|
customer: r.direct_cust,
|
|
|
|
|
ownaccount: r.own,
|
|
|
|
|
costcentre: r.costcentre,
|
|
|
|
|
contraAccount: r.contra_acc,
|
|
|
|
|
contraVendor: r.contra_vend,
|
|
|
|
|
contraCustomer: r.contra_cust,
|
|
|
|
|
@@ -453,9 +480,7 @@ export async function buildExportZip(
|
|
|
|
|
const vend = ii.vendor;
|
|
|
|
|
bookingLines.push(`${displayCurrency(alloc.amount,true)};"${shSelector}";;;;;${datevKonto};${vend?.vendorNumber};"";${formatDatevDate(ii.date, "DDMM")};"${ii.reference}";;;"${`ZA${alloc.description} ${bsText} `.substring(0,59)}";;;;;;;"Geschäftspartner";"${vend?.name}";"Kundennummer";"${vend?.vendorNumber}";"Belegnummer";"${ii.reference}";"Leistungsdatum";"${formatDatevDate(ii.date, "DD.MM.YYYY")}";"Belegdatum";"${dateFull}";;;;;;;;;;"";;;;;;;;Bank-Id;${alloc.bankstatement.id};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0;;;;"";;;;;;`);
|
|
|
|
|
} else if(alloc.account) {
|
|
|
|
|
const acc = alloc.account;
|
|
|
|
|
let vorzeichen = Math.sign(alloc.amount) > 0 ? "ZE" : "ZA";
|
|
|
|
|
bookingLines.push(`${displayCurrency(alloc.amount,true)};"${shSelector}";;;;;${datevKonto};${acc.number};"";${dateVal};"";;;"${`${vorzeichen} ${acc.number} - ${escapeString(acc.label)}${escapeString(alloc.description)}${bsText}`.substring(0,59)}";;;;;;;"Geschäftspartner";"${bs.credName || ''}";"Kundennummer";"";"Belegnummer";"";"Leistungsdatum";"";"Belegdatum";"${dateFull}";;;;;;;;;;"";;;;;;;;Bank-Id;${alloc.bankstatement.id};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0;;;;"";;;;;;`);
|
|
|
|
|
bookingLines.push(getBankAccountBookingLine(alloc));
|
|
|
|
|
} else if(alloc.vendor) {
|
|
|
|
|
const vend = alloc.vendor;
|
|
|
|
|
let vorzeichen = Math.sign(alloc.amount) > 0 ? "ZE" : "ZA";
|
|
|
|
|
@@ -465,9 +490,7 @@ export async function buildExportZip(
|
|
|
|
|
let vorzeichen = Math.sign(alloc.amount) > 0 ? "ZE" : "ZA";
|
|
|
|
|
bookingLines.push(`${displayCurrency(alloc.amount,true)};"${shSelector}";;;;;${datevKonto};${cust.customerNumber};"";${dateVal};"";;;"${`${vorzeichen} ${cust.customerNumber} - ${escapeString(cust.name)}${escapeString(alloc.description)}${bsText}`.substring(0,59)}";;;;;;;"Geschäftspartner";"${cust.name}";"Kundennummer";"";"Belegnummer";"";"Leistungsdatum";"";"Belegdatum";"${dateFull}";;;;;;;;;;"";;;;;;;;Bank-Id;${alloc.bankstatement.id};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0;;;;"";;;;;;`);
|
|
|
|
|
} else if(alloc.ownaccount) {
|
|
|
|
|
const own = alloc.ownaccount;
|
|
|
|
|
let vorzeichen = Math.sign(alloc.amount) > 0 ? "ZE" : "ZA";
|
|
|
|
|
bookingLines.push(`${displayCurrency(alloc.amount,true)};"${shSelector}";;;;;${datevKonto};${own.number};"";${dateVal};"";;;"${`${vorzeichen} ${own.number} - ${escapeString(own.name)}${escapeString(alloc.description)}${bsText}`.substring(0,59)}";;;;;;;"Geschäftspartner";"${own.name}";"Kundennummer";"";"Belegnummer";"";"Leistungsdatum";"";"Belegdatum";"${dateFull}";;;;;;;;;;"";;;;;;;;Bank-Id;${alloc.bankstatement.id};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0;;;;"";;;;;;`);
|
|
|
|
|
bookingLines.push(getBankAccountBookingLine(alloc));
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|