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

97 lines
2.8 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
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()
{
$renderer = new ImageRenderer(
new RendererStyle(200),
new SvgImageBackEnd()
);
$writer = new Writer($renderer);
2022-11-13 01:13:27 +01:00
$this->validateFields();
2022-11-21 02:14:45 +01:00
try {
2022-11-21 23:13:16 +01:00
$qr = $writer->writeString($this->encodeMessage(), 'utf-8');
2023-02-16 02:36:09 +01:00
} catch(\Throwable $e) {
2022-11-21 23:13:16 +01:00
return '';
2023-02-16 02:36:09 +01:00
} catch(\Exception $e) {
2022-11-21 02:14:45 +01:00
return '';
}
2022-11-11 04:52:50 +01: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>";
}
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
'',
2022-11-11 04:52:50 +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
}
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
}