| | |
| | | |
| | | public function synchronize(string $ucrmInvoiceId, array $previousEntity = []): void { |
| | | $ksefUrlAttribute = $this->helper->getAttributes()->getIdForCode('ksef-url'); |
| | | $ksefQrCodeAttribute = $this->helper->getAttributes()->getIdForCode('ksef-qr-code'); |
| | | $qrCodeGenUrlAttribute = $this->helper->getAttributes()->getIdForCode('qr-code-gen-url'); |
| | | |
| | | $invoice = $this->helper->getApi()->get('/invoices/' . $ucrmInvoiceId); |
| | | $currentUrl = ''; |
| | | $currentQrCode = ''; |
| | | $currentKsefUrl = ''; |
| | | $currentQrCodeGenUrl = ''; |
| | | foreach ($invoice['attributes'] ?? [] as $attribute) { |
| | | if ($attribute['customAttributeId'] == $ksefUrlAttribute) { |
| | | $currentUrl = $attribute['value']; |
| | | $currentKsefUrl = $attribute['value']; |
| | | } |
| | | if ($attribute['customAttributeId'] == $ksefQrCodeAttribute) { |
| | | $currentQrCode = $attribute['value']; |
| | | if ($attribute['customAttributeId'] == $qrCodeGenUrlAttribute) { |
| | | $currentQrCodeGenUrl = $attribute['value']; |
| | | } |
| | | } |
| | | |
| | | $newQrCode = null; |
| | | $expectedQrCodeUrl = $this->helper->getSelfUrl() . '_plugins/wfirma/public.php?barcode='; |
| | | if ($currentUrl == '' && $currentQrCode != '') { |
| | | $newQrCode = ''; |
| | | } |
| | | if ($currentUrl != '' && $currentQrCode != $expectedQrCodeUrl) { |
| | | $newQrCode = $expectedQrCodeUrl; |
| | | $newQrCodeGenUrl = null; |
| | | $expectedQrCodeGenUrl = $this->helper->getSelfUrl() . '_plugins/wfirma/public.php?barcode='; |
| | | if ($currentQrCodeGenUrl != $expectedQrCodeGenUrl) { |
| | | $newQrCodeGenUrl = $expectedQrCodeGenUrl; |
| | | } |
| | | |
| | | if ($newQrCode != null) { |
| | | if ($newQrCodeGenUrl != null) { |
| | | $this->helper->getApi()->patch('/invoices/' . $ucrmInvoiceId, [ |
| | | 'attributes' => [ |
| | | [ |
| | | 'customAttributeId' => $ksefQrCodeAttribute, |
| | | 'value' => $newQrCode, |
| | | 'customAttributeId' => $qrCodeGenUrlAttribute, |
| | | 'value' => $newQrCodeGenUrl, |
| | | ], |
| | | ], |
| | | ]); |