2 files added
7 files modified
121 ■■■■■ 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 24 ●●●●● patch | view | raw | blame | history
src/InvoiceQrCodeSynchronizer.php 46 ●●●●● patch | view | raw | blame | history
src/PaymentSynchronizer.php 7 ●●●●● 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": "^3.0.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": "5.1",
        "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
@@ -3,6 +3,7 @@
namespace SIPL\UCRM\wFirma;
use Webit\WFirmaSDK\Contractors as Contractors;
use Webit\WFirmaSDK\Contractors\ContactDetails;
use Webit\WFirmaSDK\Payments as Payments;
class ContractorSynchronizer extends Synchronizer {
@@ -48,6 +49,7 @@
        if ($contractor->nip() != $client['companyTaxId']) {
            $changed = TRUE;
            $contractor->changeTaxId(Contractors\TaxIdType::nip());
            $contractor->changeNip($client['companyTaxId']);
        }
@@ -94,6 +96,28 @@
            }
        }
        $email = NULL;
        $phone = NULL;
        foreach ($client['contacts'] as $contact) {
            if ($contact['isContact']) {
                $email = $contact['email'];
                $phone = strtr($contact['phone'], ['+' => '00']);
                break;
            }
        }
        $contactDetails = new ContactDetails(
            $phone,
            $contractor->contactDetails()->skype(),
            $contractor->contactDetails()->fax(),
            $email,
            $contractor->contactDetails()->url(),
        );
        if ($contractor->contactDetails() != $contactDetails) {
            $changed = TRUE;
            $contractor->changeContactDetails($contactDetails);
        }
        $paymentSettings = new Contractors\PaymentSettings(
            $client['invoiceMaturityDays'],
            Payments\PaymentMethod::transfer(),
src/InvoiceQrCodeSynchronizer.php
New file
@@ -0,0 +1,46 @@
<?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');
        $qrCodeGenUrlAttribute = $this->helper->getAttributes()->getIdForCode('qr-code-gen-url');
        $invoice = $this->helper->getApi()->get('/invoices/' . $ucrmInvoiceId);
        $currentKsefUrl = '';
        $currentQrCodeGenUrl = '';
        foreach ($invoice['attributes'] ?? [] as $attribute) {
            if ($attribute['customAttributeId'] == $ksefUrlAttribute) {
                $currentKsefUrl = $attribute['value'];
            }
            if ($attribute['customAttributeId'] == $qrCodeGenUrlAttribute) {
                $currentQrCodeGenUrl = $attribute['value'];
            }
        }
        $newQrCodeGenUrl = null;
        $expectedQrCodeGenUrl = $this->helper->getSelfUrl() . '_plugins/wfirma/public.php?barcode=';
        if ($currentQrCodeGenUrl != $expectedQrCodeGenUrl) {
            $newQrCodeGenUrl = $expectedQrCodeGenUrl;
        }
        if ($newQrCodeGenUrl != null) {
            $this->helper->getApi()->patch('/invoices/' . $ucrmInvoiceId, [
                'attributes' => [
                    [
                        'customAttributeId' => $qrCodeGenUrlAttribute,
                        'value' => $newQrCodeGenUrl,
                    ],
                ],
            ]);
            $this->helper->getApi()->patch('/invoices/' . $ucrmInvoiceId . '/regenerate-pdf', []);
        }
    }
}
src/PaymentSynchronizer.php
@@ -6,15 +6,8 @@
use Webit\WFirmaSDK\Payments as Payments;
class PaymentSynchronizer extends Synchronizer {
    protected $ucrmMainDir;
    function __construct(\Webit\WFirmaSDK\Entity\ModuleApiFactory $wFirmaApi, UcrmHelper $ucrmHelper) {
        parent::__construct($wFirmaApi, $ucrmHelper);
        $backtrace = debug_backtrace();
        $backtrace = end($backtrace);
        // (...)/web/_plugins/wfirma/public.php
        $this->ucrmMainDir = dirname($backtrace['file'], 4);
    }
    function comparePayment(Payments\Payment $p1, Payments\Payment $p2): int {
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',
        ],
        'QR Code Generator URL' => [
            'code' => 'qr-code-gen-url',
            '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);