1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 13:42:49 +01:00
invoiceninja/app/Helpers/Epc/EpcQrGenerator.php

109 lines
3.1 KiB
PHP
Raw Normal View History

2022-11-11 04:52:50 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-11-11 04:52:50 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Helpers\Epc;
use App\Models\Company;
use App\Models\Invoice;
2023-01-03 09:41:20 +01:00
use App\Models\RecurringInvoice;
2022-11-13 07:28:17 +01:00
use App\Utils\Ninja;
2023-10-19 22:46:45 +02:00
use BaconQrCode\Exception\InvalidArgumentException;
2022-11-11 04:52:50 +01:00
use BaconQrCode\Renderer\Image\SvgImageBackEnd;
2023-02-16 02:36:09 +01:00
use BaconQrCode\Renderer\ImageRenderer;
2022-11-11 04:52:50 +01:00
use BaconQrCode\Renderer\RendererStyle\RendererStyle;
use BaconQrCode\Writer;
/**
* EpcQrGenerator.
*/
2023-02-16 02:36:09 +01:00
class EpcQrGenerator
2022-11-11 04:52:50 +01:00
{
private array $sepa = [
'serviceTag' => 'BCD',
'version' => 2,
'characterSet' => 1,
'identification' => 'SCT',
'bic' => '',
'purpose' => '',
];
2023-02-16 02:36:09 +01:00
public function __construct(protected Company $company, protected Invoice|RecurringInvoice $invoice, protected float $amount)
{
}
2022-11-11 04:52:50 +01:00
public function getQrCode()
{
2023-10-07 00:43:08 +02:00
$qr = '';
2024-01-14 05:05:00 +01:00
2023-10-07 00:43:08 +02:00
try {
$renderer = new ImageRenderer(
new RendererStyle(200),
new SvgImageBackEnd()
);
$writer = new Writer($renderer);
2022-11-11 04:52:50 +01:00
2023-10-19 22:46:45 +02:00
$this->validateFields();
2022-11-13 01:13:27 +01:00
2022-11-21 23:13:16 +01:00
$qr = $writer->writeString($this->encodeMessage(), 'utf-8');
2023-10-09 11:40:53 +02:00
return "<svg viewBox='0 0 200 200' width='200' height='200' x='0' y='0' xmlns='http://www.w3.org/2000/svg'>
<rect x='0' y='0' width='100%'' height='100%' />{$qr}</svg>";
2023-02-16 02:36:09 +01:00
} catch(\Throwable $e) {
2024-04-26 13:17:15 +02:00
// nlog("EPC QR failure => ".$e->getMessage());
2022-11-21 23:13:16 +01:00
return '';
2023-10-09 11:40:53 +02:00
} catch(\Exception $e) {
2024-04-26 13:17:15 +02:00
// nlog("EPC QR failure => ".$e->getMessage());
2023-10-09 11:40:53 +02:00
return '';
2023-10-26 04:57:44 +02:00
} catch(InvalidArgumentException $e) {
2024-04-26 13:17:15 +02:00
// nlog("EPC QR failure => ".$e->getMessage());
2023-10-19 22:46:45 +02:00
return '';
}
2024-01-14 05:05:00 +01:00
2022-11-11 04:52:50 +01:00
}
public function encodeMessage()
{
2023-02-16 02:36:09 +01:00
return rtrim(implode("\n", [
2022-11-11 04:52:50 +01:00
$this->sepa['serviceTag'],
sprintf('%03d', $this->sepa['version']),
$this->sepa['characterSet'],
$this->sepa['identification'],
2022-11-13 01:13:27 +01:00
isset($this->company?->custom_fields?->company2) ? $this->company->settings->custom_value2 : '',
2022-11-11 04:52:50 +01:00
$this->company->present()->name(),
2022-11-13 01:13:27 +01:00
isset($this->company?->custom_fields?->company1) ? $this->company->settings->custom_value1 : '',
2022-11-11 04:52:50 +01:00
$this->formatMoney($this->amount),
$this->sepa['purpose'],
2023-02-16 02:36:09 +01:00
substr($this->invoice->number, 0, 34),
2022-11-23 12:21:12 +01:00
'',
2023-11-13 23:59:20 +01:00
' '
2023-02-16 02:36:09 +01:00
]), "\n");
2022-11-11 04:52:50 +01:00
}
2022-11-13 01:13:27 +01:00
private function validateFields()
{
2023-02-16 02:36:09 +01:00
if (Ninja::isSelfHost() && isset($this->company?->custom_fields?->company2)) {
2022-11-13 01:13:27 +01:00
nlog('The BIC field is not present and _may_ be a required fields for EPC QR codes');
2023-02-16 02:36:09 +01:00
}
2022-11-13 01:13:27 +01:00
2023-02-16 02:36:09 +01:00
if (Ninja::isSelfHost() && isset($this->company?->custom_fields?->company1)) {
2022-11-13 01:13:27 +01:00
nlog('The IBAN field is required');
2023-02-16 02:36:09 +01:00
}
2023-10-18 11:03:26 +02:00
2022-11-13 01:13:27 +01:00
}
2023-02-16 02:36:09 +01:00
private function formatMoney($value)
{
2022-11-11 04:52:50 +01:00
return sprintf('EUR%s', number_format($value, 2, '.', ''));
}
2023-02-16 02:36:09 +01:00
}