From 478aa15a9dcf86dff9408694f4450244f217cbe9 Mon Sep 17 00:00:00 2001
From: Jacek Kowalski <Jacek@jacekk.info>
Date: Mon, 16 Feb 2026 21:31:48 +0000
Subject: [PATCH] Release version 4.0

---
 src/InvoiceQrCodeSynchronizer.php |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 49 insertions(+), 0 deletions(-)

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', []);
+		}
+	}
+}

--
Gitblit v1.10.0