From b7736cfb71f73b036d34995a5540eeb202ce3e19 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 16 Feb 2026 21:31:48 +0000
Subject: [PATCH] Put full URL to public.php in "KSeF QR Code" field

---
 public.php                        |    3 +++
 src/InvoiceQrCodeSynchronizer.php |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 src/UcrmHelper.php                |    8 ++++++++
 3 files changed, 60 insertions(+), 0 deletions(-)

diff --git a/public.php b/public.php
index 06eb6c9..a0e2a68 100644
--- a/public.php
+++ b/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);
diff --git a/src/InvoiceQrCodeSynchronizer.php b/src/InvoiceQrCodeSynchronizer.php
new file mode 100644
index 0000000..a782c2c
--- /dev/null
+++ b/src/InvoiceQrCodeSynchronizer.php
@@ -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', []);
+		}
+	}
+}
diff --git a/src/UcrmHelper.php b/src/UcrmHelper.php
index 93fb323..d5f2eeb 100644
--- a/src/UcrmHelper.php
+++ b/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);

--
Gitblit v1.10.0