From b2e3ea53d03b391d575538a549d582b3bfd2c7fc Mon Sep 17 00:00:00 2001 From: checkitsedo <56700394+checkitsedo@users.noreply.github.com> Date: Sun, 30 Oct 2022 23:27:16 +0100 Subject: [PATCH] Process Invoice Numbers which includes letters -Using custom Invoice Number Patterns like R-ABC-P224301 breaks the QR Bill Generation -This solution loops through the Invoice Number and translates letter into ASCII -Makes sure that the field Additional Information not shows the wrong content --- app/Helpers/SwissQr/SwissQrGenerator.php | 51 ++++++++++++++++++------ 1 file changed, 39 insertions(+), 12 deletions(-) diff --git a/app/Helpers/SwissQr/SwissQrGenerator.php b/app/Helpers/SwissQr/SwissQrGenerator.php index 6d134adf90da..69788a88fe6a 100644 --- a/app/Helpers/SwissQr/SwissQrGenerator.php +++ b/app/Helpers/SwissQr/SwissQrGenerator.php @@ -19,7 +19,7 @@ use Sprain\SwissQrBill as QrBill; /** * SwissQrGenerator. */ -class SwissQrGenerator +class SwissQrGenerator { protected Company $company; @@ -33,7 +33,7 @@ class SwissQrGenerator $this->company = $company; $this->invoice = $invoice; - + $this->client = $invoice->client; } @@ -104,15 +104,42 @@ class SwissQrGenerator // Add payment reference // This is what you will need to identify incoming payments. - - if(stripos($this->invoice->number, "Live-") === 0) - { - // we're currently in preview status. Let's give a dummy reference for now - $invoice_number = "123456789"; - } - else - { - $invoice_number = $this->invoice->number; + + if(stripos($this->invoice->number, "Live-") === 0) + { + // we're currently in preview status. Let's give a dummy reference for now + $invoice_number = "123456789"; + } + else + { + $tempInvoiceNumber = $this->invoice->number; + $tempInvoiceNumber = preg_replace('/[^A-Za-z0-9]/', '', $tempInvoiceNumber); + $tempInvoiceNumber = substr($tempInvoiceNumber, 1); + + $calcInvoiceNumber = ""; + $array = str_split($tempInvoiceNumber); + foreach($array as $char) + { + if (is_numeric($char)) + { + // + } + else + { + if ($char) + { + $char = strtolower($char); + $char = ord($char) - 96; + } + else + { + return 0; + } + } + $calcInvoiceNumber .= $char; + } + + $invoice_number = $calcInvoiceNumber; } if(strlen($this->company->present()->besr_id()) > 1) @@ -141,7 +168,7 @@ class SwissQrGenerator // Optionally, add some human-readable information about what the bill is for. $qrBill->setAdditionalInformation( QrBill\DataGroup\Element\AdditionalInformation::create( - $this->invoice->public_notes ? substr($this->invoice->public_notes, 0, 139) : ctrans('texts.invoice_number_placeholder', ['invoice' => $invoice_number]) + $this->invoice->public_notes ? substr($this->invoice->public_notes, 0, 139) : ctrans('texts.invoice_number_placeholder', ['invoice' => $this->invoice->number]) ) );