2 files added
6 files modified
94 ■■■■■ changed files
composer.json 3 ●●●● patch | view | raw | blame | history
hook_install.php 3 ●●●●● patch | view | raw | blame | history
manifest.json 2 ●●● patch | view | raw | blame | history
public.php 14 ●●●●● patch | view | raw | blame | history
src/ContractorSynchronizer.php 1 ●●●● patch | view | raw | blame | history
src/InvoiceQrCodeSynchronizer.php 49 ●●●●● patch | view | raw | blame | history
src/UcrmAttributes.php 12 ●●●●● patch | view | raw | blame | history
src/UcrmHelper.php 10 ●●●●● patch | view | raw | blame | history
composer.json
@@ -13,6 +13,7 @@
        "php": ">=8.1",
        "ext-json": "*",
        "ubnt/ucrm-plugin-sdk": "^0.9.0",
        "webit/w-firma-api": "^2.6.0"
        "webit/w-firma-api": "^2.6.0",
        "chillerlan/php-qrcode": "^5.0"
    }
}
hook_install.php
New file
@@ -0,0 +1,3 @@
<?php
require_once(__DIR__ . '/vendor/autoload.php');
require_once(__DIR__ . '/hook_enable.php');
manifest.json
@@ -5,7 +5,7 @@
        "displayName": "wFirma.pl integration",
        "description": "Synchronizes customers and invoices with Polish invoicing system wFirma.pl",
        "url": "https://github.com/jacekkow/ucrm-wfirma",
        "version": "3.0",
        "version": "4.0",
        "ucrmVersionCompliancy": {
            "min": "4.0.0",
            "max": null
public.php
@@ -2,7 +2,16 @@
require_once(__DIR__ . '/vendor/autoload.php');
try {
    require_once(__DIR__ . '/hook_enable.php');
    if (isset($_GET['barcode'])) {
        header('Content-Type: image/svg+xml');
        echo (new \chillerlan\QRCode\QRCode(new \chillerlan\QRCode\QROptions([
            'outputBase64' => false,
            'addQuietzone' => false,
            'drawLightModules' => false,
            'connectPaths' => true,
        ])))->render($_GET['barcode']);
        die();
    }
    $helper = new \SIPL\UCRM\wFirma\UcrmHelper();
    $event = $helper->getCurrentEvent();
@@ -15,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/ContractorSynchronizer.php
@@ -48,6 +48,7 @@
        if ($contractor->nip() != $client['companyTaxId']) {
            $changed = TRUE;
            $contractor->changeTaxId(Contractors\TaxIdType::nip());
            $contractor->changeNip($client['companyTaxId']);
        }
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/UcrmAttributes.php
@@ -15,6 +15,18 @@
            'code' => 'invoice',
            'type' => 'invoice',
        ],
        'KSeF Number' => [
            'code' => 'ksef-number',
            'type' => 'invoice',
        ],
        'KSeF URL' => [
            'code' => 'ksef-url',
            'type' => 'invoice',
        ],
        'KSeF QR Code' => [
            'code' => 'ksef-qr-code',
            'type' => 'invoice',
        ],
        'wFirma Payment ID' => [
            'code' => 'payment',
            'type' => 'payment',
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;
@@ -12,7 +14,7 @@
    function __construct(?string $rootDirectory = NULL) {
        if ($rootDirectory === NULL) {
            $rootDirectory = __DIR__ . '/..';
            $rootDirectory = dirname(__DIR__);
        }
        $this->rootDirectory = $rootDirectory;
    }
@@ -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);