Jacek Kowalski
2026-02-16 b7736cfb71f73b036d34995a5540eeb202ce3e19
Put full URL to public.php in "KSeF QR Code" field
1 files added
2 files modified
60 ■■■■■ changed files
public.php 3 ●●●●● patch | view | raw | blame | history
src/InvoiceQrCodeSynchronizer.php 49 ●●●●● patch | view | raw | blame | history
src/UcrmHelper.php 8 ●●●●● patch | view | raw | blame | history
public.php
@@ -24,6 +24,9 @@
    } elseif ($event['entity'] === 'client') {
        $synchronizer = new \SIPL\UCRM\wFirma\ContractorSynchronizer($wFirmaApi, $helper);
    } elseif ($event['entity'] === 'invoice') {
        $qrSync = new \SIPL\UCRM\wFirma\InvoiceQrCodeSynchronizer($helper);
        $qrSync->synchronize($event['entityId'], $event['extraData']['entityBeforeEdit'] ?? null);
        $synchronizer = new \SIPL\UCRM\wFirma\InvoiceSynchronizer($wFirmaApi, $helper);
    } elseif ($event['entity'] === 'payment') {
        $synchronizer = new \SIPL\UCRM\wFirma\PaymentSynchronizer($wFirmaApi, $helper);
src/InvoiceQrCodeSynchronizer.php
New file
@@ -0,0 +1,49 @@
<?php
namespace SIPL\UCRM\wFirma;
class InvoiceQrCodeSynchronizer {
    protected UcrmHelper $helper;
    public function __construct(UcrmHelper $helper) {
        $this->helper = $helper;
    }
    public function synchronize(string $ucrmInvoiceId, array $previousEntity = []): void {
        $ksefUrlAttribute = $this->helper->getAttributes()->getIdForCode('ksef-url');
        $ksefQrCodeAttribute = $this->helper->getAttributes()->getIdForCode('ksef-qr-code');
        $invoice = $this->helper->getApi()->get('/invoices/' . $ucrmInvoiceId);
        $currentUrl = '';
        $currentQrCode = '';
        foreach ($invoice['attributes'] ?? [] as $attribute) {
            if ($attribute['customAttributeId'] == $ksefUrlAttribute) {
                $currentUrl = $attribute['value'];
            }
            if ($attribute['customAttributeId'] == $ksefQrCodeAttribute) {
                $currentQrCode = $attribute['value'];
            }
        }
        $newQrCode = null;
        $expectedQrCodeUrl = $this->helper->getSelfUrl() . '_plugins/wfirma/public.php?barcode=';
        if ($currentUrl == '' && $currentQrCode != '') {
            $newQrCode = '';
        }
        if ($currentUrl != '' && $currentQrCode != $expectedQrCodeUrl) {
            $newQrCode = $expectedQrCodeUrl;
        }
        if ($newQrCode != null) {
            $this->helper->getApi()->patch('/invoices/' . $ucrmInvoiceId, [
                'attributes' => [
                    [
                        'customAttributeId' => $ksefQrCodeAttribute,
                        'value' => $newQrCode,
                    ],
                ],
            ]);
            $this->helper->getApi()->patch('/invoices/' . $ucrmInvoiceId . '/regenerate-pdf', []);
        }
    }
}
src/UcrmHelper.php
@@ -2,6 +2,8 @@
namespace SIPL\UCRM\wFirma;
use Ubnt\UcrmPluginSdk\Service\UcrmOptionsManager;
class UcrmHelper {
    protected string $rootDirectory;
    protected ?\Ubnt\UcrmPluginSdk\Service\UcrmApi $api = NULL;
@@ -28,6 +30,12 @@
        return $this->api;
    }
    function getSelfUrl(): string {
        $ucrmOptionsManager = new UcrmOptionsManager($this->rootDirectory);
        $options = $ucrmOptionsManager->loadOptions();
        return $options->ucrmLocalUrl ?? $options->ucrmPublicUrl ?? '';
    }
    function getAttributes(): UcrmAttributes {
        if ($this->attributes === NULL) {
            $this->attributes = new UcrmAttributes($this);