Steuerberater Modul tax

This commit is contained in:
Kevin Adametz 2026-05-08 15:34:57 +02:00
parent 0f82fea88a
commit 245c281541
22 changed files with 1489 additions and 139 deletions

View file

@ -221,7 +221,7 @@ class DatevExportService
$csvContent = $this->buildCsv($export);
$filename = $this->generateFilename($month, $year);
$storagePath = $export->getStoragePath();
$fullPath = $storagePath . '/' . $filename;
$fullPath = $storagePath.'/'.$filename;
Storage::disk(config('datev.storage_disk'))->makeDirectory($storagePath);
Storage::disk(config('datev.storage_disk'))->put($fullPath, $csvContent);
@ -254,6 +254,7 @@ class DatevExportService
'shopping_order.country.country',
'shopping_order.shopping_user',
'shopping_order.auth_user.account',
'shopping_order.shopping_collect_order',
])
->where('month', $month)
->where('year', $year)
@ -289,13 +290,15 @@ class DatevExportService
// Tax-Split vorhanden? -> Mehrere Zeilen pro Steuersatz
if ($order->tax_split && is_array($order->tax_split) && count($order->tax_split) > 0) {
$netSplit = $this->resolveNetSplit($order);
foreach ($order->tax_split as $taxRate => $taxAmount) {
$taxRate = intval($taxRate);
$taxAmountFloat = $this->parseNumber($taxAmount);
$taxAmountFloat = $this->parseNumber($taxAmount, 'ek_tax');
$netAmount = 0;
if ($order->net_split && isset($order->net_split[$taxRate])) {
$netAmount = $this->parseNumber($order->net_split[$taxRate]);
if ($netSplit && isset($netSplit[$taxRate])) {
$netAmount = $this->parseNumber($netSplit[$taxRate], 'ek_net');
}
$grossAmount = round($netAmount + $taxAmountFloat, 2);
@ -389,7 +392,7 @@ class DatevExportService
// Steuerstatus des Beraters ermitteln
$taxStatus = $this->determineCommissionTaxStatus($account);
$buSchluessel = config('datev.commission_tax_keys.' . $taxStatus, 9);
$buSchluessel = config('datev.commission_tax_keys.'.$taxStatus, 9);
// USt-ID für Reverse Charge
$euUstid = null;
@ -487,6 +490,7 @@ class DatevExportService
'shopping_order.country.country',
'shopping_order.shopping_user',
'shopping_order.auth_user.account',
'shopping_order.shopping_collect_order',
])
->where('month', $month)
->where('year', $year)
@ -500,7 +504,7 @@ class DatevExportService
}
$gegenkonto = $this->determineCounterAccountForOrder($order);
$buchungstext = 'STORNO ' . $this->buildBuchungstext($order);
$buchungstext = 'STORNO '.$this->buildBuchungstext($order);
$buchungstext = mb_substr($buchungstext, 0, 60);
$belegdatum = $this->parseBelegdatum($invoice);
@ -512,13 +516,15 @@ class DatevExportService
$hasValidUstid = ! empty($euUstid);
if ($order->tax_split && is_array($order->tax_split) && count($order->tax_split) > 0) {
$netSplit = $this->resolveNetSplit($order);
foreach ($order->tax_split as $taxRate => $taxAmount) {
$taxRate = intval($taxRate);
$taxAmountFloat = $this->parseNumber($taxAmount);
$taxAmountFloat = $this->parseNumber($taxAmount, 'ek_tax');
$netAmount = 0;
if ($order->net_split && isset($order->net_split[$taxRate])) {
$netAmount = $this->parseNumber($order->net_split[$taxRate]);
if ($netSplit && isset($netSplit[$taxRate])) {
$netAmount = $this->parseNumber($netSplit[$taxRate], 'ek_net');
}
$grossAmount = round($netAmount + $taxAmountFloat, 2);
@ -598,18 +604,18 @@ class DatevExportService
}
// Zeile 1: Header
$output .= $this->buildHeaderLine($export) . $lineEnding;
$output .= $this->buildHeaderLine($export).$lineEnding;
// Zeile 2: Spaltenüberschriften
$output .= implode($delimiter, self::COLUMN_HEADERS) . $lineEnding;
$output .= implode($delimiter, self::COLUMN_HEADERS).$lineEnding;
// Zeile 3+: Datenzeilen
$lines = $export->lines()->orderBy('line_number')->get();
foreach ($lines as $line) {
if ($line->row_csv) {
$output .= $line->row_csv . $lineEnding;
$output .= $line->row_csv.$lineEnding;
} else {
$output .= $this->renderCsvRow($line->toArray()) . $lineEnding;
$output .= $this->renderCsvRow($line->toArray()).$lineEnding;
}
}
@ -722,7 +728,7 @@ class DatevExportService
'user_id' => $line['user_id'] ?? null,
'belegfeld1' => $line['belegfeld1'] ?? null,
];
$lineRef = ($meta['source_type'] ?? '?') . ' #' . ($meta['source_id'] ?? '?');
$lineRef = ($meta['source_type'] ?? '?').' #'.($meta['source_id'] ?? '?');
if (empty($line['belegdatum'])) {
$errors[] = $this->buildValidationEntry("{$lineRef}: Belegdatum fehlt.", $meta);
@ -790,7 +796,7 @@ class DatevExportService
// Gruppierte Zusammenfassung nach Konto + BU
$grouped = $allLines->groupBy(function ($line) {
return $line['konto'] . '-' . $line['bu_schluessel'] . '-' . $line['soll_haben'];
return $line['konto'].'-'.$line['bu_schluessel'].'-'.$line['soll_haben'];
})->map(function ($group, $key) {
$first = $group->first();
@ -869,7 +875,7 @@ class DatevExportService
// Zusammengesetzter Key aus clearingtype + wallettype
$key = $payment->clearingtype;
if ($payment->wallettype) {
$key .= '_' . $payment->wallettype;
$key .= '_'.$payment->wallettype;
}
$map = config('datev.counteraccount_map', []);
@ -984,7 +990,7 @@ class DatevExportService
{
if ($order->shopping_user) {
$name = trim(
($order->shopping_user->billing_lastname ?? '') . ' ' .
($order->shopping_user->billing_lastname ?? '').' '.
($order->shopping_user->billing_firstname ?? '')
);
@ -1005,7 +1011,7 @@ class DatevExportService
$parts = [];
if ($account) {
$name = trim(($account->last_name ?? '') . ' ' . ($account->first_name ?? ''));
$name = trim(($account->last_name ?? '').' '.($account->first_name ?? ''));
if (! empty($name)) {
$parts[] = $name;
}
@ -1013,7 +1019,7 @@ class DatevExportService
$userId = $user?->id ?? $creditUserId;
if ($userId) {
$parts[] = 'Nr.' . $userId;
$parts[] = 'Nr.'.$userId;
}
$text = implode('; ', $parts);
@ -1038,20 +1044,60 @@ class DatevExportService
return Carbon::create($invoice->year, $invoice->month, 1)->format('Y-m-d');
}
/**
* Ermittelt den Netto-Split. Historische Sammelrechnungen haben ihn teils nur
* an der ShoppingCollectOrder, nicht an der erzeugten ShoppingOrder.
*/
private function resolveNetSplit($order): ?array
{
if ($order->net_split && is_array($order->net_split)) {
return $order->net_split;
}
$collectOrderNetSplit = $order->shopping_collect_order?->net_split;
if ($collectOrderNetSplit && is_array($collectOrderNetSplit)) {
return $collectOrderNetSplit;
}
return null;
}
/**
* Parst einen formatierten Zahlenwert (z.B. "5.00" oder "5,00") zu float.
* Behandelt sowohl einfache Werte als auch tax_split Arrays (homeparty).
*/
private function parseNumber($value): float
private function parseNumber($value, ?string $preferredArrayKey = null): float
{
if (is_array($value)) {
// Homeparty tax_split Format: ['vk_tax' => '5.00', 'ek_tax' => '2.00']
return floatval(str_replace(',', '.', $value['vk_tax'] ?? 0));
$arrayKeys = $this->getSplitArrayKeys($preferredArrayKey);
foreach ($arrayKeys as $arrayKey) {
if (array_key_exists($arrayKey, $value)) {
return $this->parseNumber($value[$arrayKey]);
}
}
return 0.0;
}
return floatval(str_replace(',', '.', $value));
}
/**
* @return array<int, string>
*/
private function getSplitArrayKeys(?string $preferredArrayKey): array
{
return match ($preferredArrayKey) {
'ek_tax' => ['ek_tax', 'vk_tax', 'ek_net', 'vk_net'],
'ek_net' => ['ek_net', 'vk_net', 'ek_tax', 'vk_tax'],
'vk_tax' => ['vk_tax', 'ek_tax', 'vk_net', 'ek_net'],
'vk_net' => ['vk_net', 'ek_net', 'vk_tax', 'ek_tax'],
default => ['vk_tax', 'vk_net', 'ek_tax', 'ek_net'],
};
}
/**
* Escaped ein CSV-Feld (Semikolon, Anführungszeichen, Newlines).
*/
@ -1061,7 +1107,7 @@ class DatevExportService
$value = str_replace('"', '""', $value);
if (str_contains($value, ';') || str_contains($value, '"') || str_contains($value, "\n")) {
return '"' . $value . '"';
return '"'.$value.'"';
}
return $value;
@ -1074,6 +1120,6 @@ class DatevExportService
{
$monthPad = str_pad($month, 2, '0', STR_PAD_LEFT);
return "EXTF_Buchungsstapel_{$year}_{$monthPad}_" . date('YmdHis') . '.csv';
return "EXTF_Buchungsstapel_{$year}_{$monthPad}_".date('YmdHis').'.csv';
}
}