Jacek Kowalski
2026-02-16 478aa15a9dcf86dff9408694f4450244f217cbe9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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', []);
        }
    }
}