New SEPA logic

This commit is contained in:
2026-06-21 01:28:28 +02:00
parent e9aa66a860
commit 63c7b8dfb1
8 changed files with 318 additions and 35 deletions
+13 -17
View File
@@ -2,25 +2,23 @@
namespace App\Providers;
use App\Models\Invoice;
use App\Resources\InvoiceResource;
use App\Models\SepaPaymentElement;
use DOMDocument;
use Exception;
use Illuminate\Http\Request;
class PainFileProvider {
public string $senderIban;
public string $senderName;
public string $senderBic;
/* @var Invoice[] */
public array $invoices;
/** @var SepaPaymentElement[] */
public array $elements;
public function __construct(string $senderIban, string $senderName, string $senderBic, array $invoices) {
public function __construct(string $senderIban, string $senderName, string $senderBic, array $elements) {
$this->senderIban = $senderIban;
$this->senderName = $senderName;
$this->senderBic = $senderBic;
$this->invoices = $invoices;
$this->elements = $elements;
}
public function createPainFileContent() : string {
@@ -46,9 +44,9 @@ class PainFileProvider {
$grp_hdr->appendChild($doc->createElement('MsgId', uniqid('MSG')));
$grp_hdr->appendChild($doc->createElement('CreDtTm', date('c')));
$grp_hdr->appendChild($doc->createElement('NbOfTxs', count($this->invoices)));
$grp_hdr->appendChild($doc->createElement('NbOfTxs', count($this->elements)));
$totalAmount = array_sum(array_column($this->invoices, 'amount'));
$totalAmount = array_sum(array_map(fn(SepaPaymentElement $e) => $e->amount, $this->elements));
$grp_hdr->appendChild($doc->createElement('CtrlSum', number_format($totalAmount, 2, '.', '')));
$initg_pty = $doc->createElement('InitgPty');
@@ -62,7 +60,7 @@ class PainFileProvider {
$pmt_inf->appendChild($doc->createElement('PmtInfId', uniqid('PMT')));
$pmt_inf->appendChild($doc->createElement('PmtMtd', 'TRF'));
$pmt_inf->appendChild($doc->createElement('BtchBookg', 'false'));
$pmt_inf->appendChild($doc->createElement('NbOfTxs', count($this->invoices)));
$pmt_inf->appendChild($doc->createElement('NbOfTxs', count($this->elements)));
$pmt_inf->appendChild($doc->createElement('CtrlSum', number_format($totalAmount, 2, '.', '')));
$pmt_tp_inf = $doc->createElement('PmtTpInf');
@@ -90,9 +88,7 @@ class PainFileProvider {
$dbtr_agt->appendChild($id);
$pmt_inf->appendChild($dbtr_agt);
foreach ($this->invoices as $index => $invoice) {
$invoiceResource = new InvoiceResource($invoice)->toArray(new Request());
foreach ($this->elements as $index => $element) {
$cdt_trf_tx_inf = $doc->createElement('CdtTrfTxInf');
$pmt_id = $doc->createElement('PmtId');
@@ -100,23 +96,23 @@ class PainFileProvider {
$cdt_trf_tx_inf->appendChild($pmt_id);
$amt = $doc->createElement('Amt');
$instd_amt = $doc->createElement('InstdAmt', number_format($invoice['amount'], 2, '.', ''));
$instd_amt = $doc->createElement('InstdAmt', number_format($element->amount, 2, '.', ''));
$instd_amt->setAttribute('Ccy', 'EUR');
$amt->appendChild($instd_amt);
$cdt_trf_tx_inf->appendChild($amt);
$cdtr = $doc->createElement('Cdtr');
$cdtr->appendChild($doc->createElement('Nm', $invoice['contact_bank_owner']));
$cdtr->appendChild($doc->createElement('Nm', $element->recipient_name));
$cdt_trf_tx_inf->appendChild($cdtr);
$cdtr_acct = $doc->createElement('CdtrAcct');
$cdtr_id = $doc->createElement('Id');
$cdtr_id->appendChild($doc->createElement('IBAN', str_replace(' ', '', $invoice['contact_bank_iban'])));
$cdtr_id->appendChild($doc->createElement('IBAN', str_replace(' ', '', $element->recipient_iban)));
$cdtr_acct->appendChild($cdtr_id);
$cdt_trf_tx_inf->appendChild($cdtr_acct);
$rmt_inf = $doc->createElement('RmtInf');
$rmt_inf->appendChild($doc->createElement('Ustrd', $invoiceResource['paymentPurpose']));
$rmt_inf->appendChild($doc->createElement('Ustrd', $element->payment_purpose));
$cdt_trf_tx_inf->appendChild($rmt_inf);
$pmt_inf->appendChild($cdt_trf_tx_inf);