New SEPA logic
This commit is contained in:
@@ -4,14 +4,13 @@ namespace App\Domains\CostUnit\Controllers;
|
||||
|
||||
use App\Domains\Invoice\Actions\ChangeStatus\ChangeStatusCommand;
|
||||
use App\Domains\Invoice\Actions\ChangeStatus\ChangeStatusRequest;
|
||||
use App\Domains\Invoice\Actions\CreateInvoice\CreateInvoiceRequest;
|
||||
use App\Domains\Invoice\Actions\CreateInvoiceReceipt\CreateInvoiceReceiptCommand;
|
||||
use App\Domains\Invoice\Actions\CreateInvoiceReceipt\CreateInvoiceReceiptRequest;
|
||||
use App\Enumerations\InvoiceStatus;
|
||||
use App\Models\SepaPaymentElement;
|
||||
use App\Models\Tenant;
|
||||
use App\Providers\FileWriteProvider;
|
||||
use App\Providers\InvoiceCsvFileProvider;
|
||||
use App\Providers\PainFileProvider;
|
||||
use App\Providers\WebDavProvider;
|
||||
use App\Providers\ZipArchiveFileProvider;
|
||||
use App\Scopes\CommonController;
|
||||
@@ -25,24 +24,19 @@ class ExportController extends CommonController {
|
||||
|
||||
$webdavProvider = new WebDavProvider(WebDavProvider::INVOICE_PREFIX . $this->tenant->url . '/' . $costUnit->name);
|
||||
|
||||
$painFileData = $this->painData($invoicesForExport);
|
||||
$this->createSepaPaymentElements($invoicesForExport, $costUnit);
|
||||
$csvData = $this->csvData($invoicesForExport);
|
||||
|
||||
$filePrefix = Tenant::getTempDirectory();
|
||||
|
||||
$painFileWriteProvider = new FileWriteProvider($filePrefix . 'abrechnungen-' . date('Y-m-d_H-i') . '-sepa.xml', $painFileData);
|
||||
$painFileWriteProvider->writeToFile();
|
||||
|
||||
$csvFileWriteProvider = new FileWriteProvider($filePrefix . 'abrechnungen-' . date('Y-m-d_H-i') . '.csv', $csvData);
|
||||
$csvFileWriteProvider->writeToFile();
|
||||
|
||||
if ($this->tenant->upload_exports) {
|
||||
$webdavProvider->uploadFile($painFileWriteProvider->fileName);
|
||||
$webdavProvider->uploadFile($csvFileWriteProvider->fileName);
|
||||
}
|
||||
|
||||
$downloadZipArchiveFiles = [
|
||||
$painFileWriteProvider->fileName,
|
||||
$csvFileWriteProvider->fileName
|
||||
];
|
||||
|
||||
@@ -72,7 +66,6 @@ class ExportController extends CommonController {
|
||||
Storage::delete($file);
|
||||
}
|
||||
|
||||
Storage::delete($painFileWriteProvider->fileName);
|
||||
Storage::delete($csvFileWriteProvider->fileName);
|
||||
|
||||
return response()->download(
|
||||
@@ -82,24 +75,28 @@ class ExportController extends CommonController {
|
||||
);
|
||||
}
|
||||
|
||||
Storage::delete($painFileWriteProvider->fileName);
|
||||
Storage::delete($csvFileWriteProvider->fileName);
|
||||
return response()->json([
|
||||
'message' => 'Die Abrechnungen wurden exportiert.' . PHP_EOL .'Die Belege werden asynchron auf dem Webdav-Server hinterlegt.' . PHP_EOL . PHP_EOL . 'Sollten diese in 15 Minuten nicht vollständig sein, kontaktiere den Adminbistrator.'
|
||||
'message' => 'Die Abrechnungen wurden exportiert.' . PHP_EOL . 'Die SEPA-Überweisungsdatei kann über den Tab "Globale Aktionen" in der Kostenstellenübersicht erzeugt werden.' . PHP_EOL . PHP_EOL . 'Die Belege werden asynchron auf dem Webdav-Server hinterlegt.' . PHP_EOL . 'Sollten diese in 15 Minuten nicht vollständig sein, kontaktiere den Administrator.'
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
private function painData(array $invoices) : string {
|
||||
$invoicesForPainFile = [];
|
||||
private function createSepaPaymentElements(array $invoices, $costUnit): void
|
||||
{
|
||||
foreach ($invoices as $invoice) {
|
||||
if ($invoice->contact_bank_owner !== null && $invoice->contact_bank_iban !== '' && !$invoice->donation) {
|
||||
$invoicesForPainFile[] = $invoice;
|
||||
SepaPaymentElement::create([
|
||||
'tenant' => $this->tenant->slug,
|
||||
'invoice_id' => $invoice->id,
|
||||
'cost_unit_id' => $costUnit->id,
|
||||
'amount' => $invoice->amount,
|
||||
'recipient_name' => $invoice->contact_bank_owner,
|
||||
'recipient_iban' => $invoice->contact_bank_iban,
|
||||
'payment_purpose' => $invoice->payment_purpose ?? 'Auslagenerstattung Rechnungsnummer ' . $invoice->invoice_number,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
$painFileProvider = new PainFileProvider($this->tenant->account_iban, $this->tenant->account_name, $this->tenant->account_bic, $invoicesForPainFile);
|
||||
return $painFileProvider->createPainFileContent();
|
||||
}
|
||||
|
||||
public function csvData(array $invoices) : string {
|
||||
@@ -107,4 +104,3 @@ class ExportController extends CommonController {
|
||||
return $csvDateProvider->createCsvFileContent();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\CostUnit\Controllers;
|
||||
|
||||
use App\Enumerations\UserRole;
|
||||
use App\Models\SepaPaymentElement;
|
||||
use App\Models\Tenant;
|
||||
use App\Providers\AuthCheckProvider;
|
||||
use App\Providers\FileWriteProvider;
|
||||
use App\Providers\PainFileProvider;
|
||||
use App\Scopes\CommonController;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
|
||||
class GlobalSepaExportController extends CommonController {
|
||||
|
||||
private function checkAuthorization(): void
|
||||
{
|
||||
$authCheck = new AuthCheckProvider();
|
||||
$role = $authCheck->getUserRole();
|
||||
if (!in_array($role, [UserRole::USER_ROLE_ADMIN, UserRole::USER_ROLE_GROUP_LEADER], true)) {
|
||||
abort(403);
|
||||
}
|
||||
}
|
||||
|
||||
public function getGlobalActions()
|
||||
{
|
||||
$this->checkAuthorization();
|
||||
|
||||
$pendingElements = SepaPaymentElement::where('exported', false)->get();
|
||||
$pendingCount = $pendingElements->count();
|
||||
$pendingAmount = number_format($pendingElements->sum('amount'), 2, ',', '.');
|
||||
|
||||
return response()->json([
|
||||
'pending_count' => $pendingCount,
|
||||
'pending_amount' => $pendingAmount,
|
||||
]);
|
||||
}
|
||||
|
||||
public function exportSepaFile()
|
||||
{
|
||||
$this->checkAuthorization();
|
||||
|
||||
return DB::transaction(function () {
|
||||
$elements = SepaPaymentElement::where('exported', false)->lockForUpdate()->get();
|
||||
|
||||
if ($elements->isEmpty()) {
|
||||
return response()->json([
|
||||
'message' => 'Es gibt keine ausstehenden SEPA-Überweisungen.'
|
||||
], 404);
|
||||
}
|
||||
|
||||
$painFileProvider = new PainFileProvider(
|
||||
$this->tenant->account_iban,
|
||||
$this->tenant->account_name,
|
||||
$this->tenant->account_bic,
|
||||
$elements->all()
|
||||
);
|
||||
|
||||
$painContent = $painFileProvider->createPainFileContent();
|
||||
|
||||
$filePrefix = Tenant::getTempDirectory();
|
||||
$fileName = $filePrefix . 'sepa-pain-' . date('Y-m-d_H-i') . '.xml';
|
||||
|
||||
$fileWriteProvider = new FileWriteProvider($fileName, $painContent);
|
||||
$fileWriteProvider->writeToFile();
|
||||
|
||||
$elements->each(function (SepaPaymentElement $element) {
|
||||
$element->update([
|
||||
'exported' => true,
|
||||
'exported_at' => now(),
|
||||
]);
|
||||
});
|
||||
|
||||
$filePath = storage_path('app/private/' . $fileName);
|
||||
|
||||
return response()->download($filePath, basename($fileName), [
|
||||
'Content-Type' => 'application/xml',
|
||||
])->deleteFileAfterSend(true);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user