Manuelle Buchungen in Statementallocations integrieren
This commit is contained in:
@@ -8,7 +8,7 @@ import { s3 } from "../s3";
|
||||
import { secrets } from "../secrets";
|
||||
|
||||
// Drizzle Core Imports
|
||||
import { eq, and, inArray, gte, lte, asc, aliasedTable } from "drizzle-orm";
|
||||
import { eq, and, or, isNull, inArray, gte, lte, asc, aliasedTable } from "drizzle-orm";
|
||||
|
||||
// Tabellen Imports (keine Relations nötig!)
|
||||
import {
|
||||
@@ -136,6 +136,10 @@ export async function buildExportZip(
|
||||
|
||||
const CdCustomer = aliasedTable(customers, "cd_customer");
|
||||
const IiVendor = aliasedTable(vendors, "ii_vendor");
|
||||
const ContraAccount = aliasedTable(accounts, "contra_account");
|
||||
const ContraVendor = aliasedTable(vendors, "contra_vendor");
|
||||
const ContraCustomer = aliasedTable(customers, "contra_customer");
|
||||
const ContraOwnaccount = aliasedTable(ownaccounts, "contra_ownaccount");
|
||||
|
||||
const allocRaw = await server.db.select({
|
||||
allocation: statementallocations,
|
||||
@@ -148,11 +152,15 @@ export async function buildExportZip(
|
||||
acc: accounts,
|
||||
direct_vend: vendors, // Direkte Zuordnung an Kreditor
|
||||
direct_cust: customers, // Direkte Zuordnung an Debitor
|
||||
own: ownaccounts
|
||||
own: ownaccounts,
|
||||
contra_acc: ContraAccount,
|
||||
contra_vend: ContraVendor,
|
||||
contra_cust: ContraCustomer,
|
||||
contra_own: ContraOwnaccount
|
||||
})
|
||||
.from(statementallocations)
|
||||
// JOIN 1: Bankstatement (Pflicht, für Datum Filter)
|
||||
.innerJoin(bankstatements, eq(statementallocations.bankstatement, bankstatements.id))
|
||||
// JOIN 1: Bankstatement (optional, manuelle Buchungen haben keinen Bankumsatz)
|
||||
.leftJoin(bankstatements, eq(statementallocations.bankstatement, bankstatements.id))
|
||||
// JOIN 2: Bankaccount (für DATEV Nummer)
|
||||
.leftJoin(bankaccounts, eq(bankstatements.account, bankaccounts.id))
|
||||
|
||||
@@ -169,13 +177,25 @@ 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(ContraAccount, eq(statementallocations.contraAccount, ContraAccount.id))
|
||||
.leftJoin(ContraVendor, eq(statementallocations.contraVendor, ContraVendor.id))
|
||||
.leftJoin(ContraCustomer, eq(statementallocations.contraCustomer, ContraCustomer.id))
|
||||
.leftJoin(ContraOwnaccount, eq(statementallocations.contraOwnaccount, ContraOwnaccount.id))
|
||||
|
||||
.where(and(
|
||||
eq(statementallocations.tenant, tenantId),
|
||||
eq(statementallocations.archived, false),
|
||||
// Datum Filter direkt auf dem Bankstatement
|
||||
gte(bankstatements.date, startDate),
|
||||
lte(bankstatements.date, endDate)
|
||||
or(
|
||||
and(
|
||||
gte(bankstatements.date, startDate),
|
||||
lte(bankstatements.date, endDate)
|
||||
),
|
||||
and(
|
||||
isNull(statementallocations.bankstatement),
|
||||
gte(statementallocations.manualBookingDate, startDate),
|
||||
lte(statementallocations.manualBookingDate, endDate)
|
||||
)
|
||||
)
|
||||
));
|
||||
|
||||
// Mapping: Wir bauen das komplexe Objekt nach, das die CSV Logik erwartet
|
||||
@@ -196,7 +216,11 @@ export async function buildExportZip(
|
||||
account: r.acc,
|
||||
vendor: r.direct_vend,
|
||||
customer: r.direct_cust,
|
||||
ownaccount: r.own
|
||||
ownaccount: r.own,
|
||||
contraAccount: r.contra_acc,
|
||||
contraVendor: r.contra_vend,
|
||||
contraCustomer: r.contra_cust,
|
||||
contraOwnaccount: r.contra_own
|
||||
}));
|
||||
|
||||
// --- D) Stammdaten Accounts ---
|
||||
@@ -311,8 +335,32 @@ export async function buildExportZip(
|
||||
});
|
||||
|
||||
// Bank
|
||||
const getManualBookingSide = (alloc: any, side: "debit" | "credit") => {
|
||||
const prefix = side === "credit" ? "contra" : "";
|
||||
const account = side === "credit" ? alloc.contraAccount : alloc.account;
|
||||
const vendor = side === "credit" ? alloc.contraVendor : alloc.vendor;
|
||||
const customer = side === "credit" ? alloc.contraCustomer : alloc.customer;
|
||||
const ownaccount = side === "credit" ? alloc.contraOwnaccount : alloc.ownaccount;
|
||||
|
||||
if (account) return { number: account.number, name: account.label, type: "Sachkonto" };
|
||||
if (vendor) return { number: vendor.vendorNumber, name: vendor.name, type: "Kreditor" };
|
||||
if (customer) return { number: customer.customerNumber, name: customer.name, type: "Debitor" };
|
||||
if (ownaccount) return { number: ownaccount.number, name: ownaccount.name, type: "Eigenes Konto" };
|
||||
return { number: "", name: "", type: prefix };
|
||||
};
|
||||
|
||||
statementallocationsList.forEach(alloc => {
|
||||
const bs = alloc.bankstatement; // durch Mapping verfügbar
|
||||
|
||||
if(!bs && alloc.manualBookingDate) {
|
||||
const debit = getManualBookingSide(alloc, "debit");
|
||||
const credit = getManualBookingSide(alloc, "credit");
|
||||
const dateManual = dayjs(alloc.manualBookingDate).format("DDMM");
|
||||
const dateManualFull = dayjs(alloc.manualBookingDate).format("DD.MM.YYYY");
|
||||
bookingLines.push(`${displayCurrency(alloc.amount,true)};"S";;;;;${debit.number};${credit.number};"";${dateManual};"";;;"${`MB ${debit.number} an ${credit.number} ${escapeString(alloc.description)}`.substring(0,59)}";;;;;;;"Geschäftspartner";"${escapeString(debit.name || credit.name)}";"Kundennummer";"${debit.number}";"Belegnummer";"";"Leistungsdatum";"${dateManualFull}";"Belegdatum";"${dateManualFull}";;;;;;;;;;"";;;;;;;;Manuelle-Buchung;${alloc.id};;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;0;;;;"";;;;;;`);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!bs) return;
|
||||
|
||||
let shSelector = Math.sign(alloc.amount) === -1 ? "H" : "S";
|
||||
@@ -425,4 +473,4 @@ export async function buildExportZip(
|
||||
console.error("DATEV Export Error:", error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user