Jacek Kowalski
2026-02-16 68733e1d15a3b3cca8f1dc4e7c78dba1cd756e81
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
 
namespace SIPL\UCRM\wFirma;
 
class UcrmAttributes {
    protected string $attributesFile;
    protected \Ubnt\UcrmPluginSdk\Service\UcrmApi $api;
 
    protected static array $definition = [
        'wFirma Customer ID' => [
            'code' => 'client',
            'type' => 'client',
        ],
        'wFirma Invoice ID' => [
            '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',
        ],
    ];
    protected array $ids = [];
 
    function __construct(UcrmHelper $ucrmHelper) {
        $this->attributesFile = $ucrmHelper->getRootDirectory() . '/data/attributes.json';
        $this->api = $ucrmHelper->getApi();
 
        if (!is_file($this->attributesFile)) {
            $this->updateMapping();
            $this->saveMapping();
        } else {
            $this->ids = json_decode(file_get_contents($this->attributesFile), TRUE);
        }
    }
 
    function getIdForCode($attributeCode) {
        if (!isset($this->ids[$attributeCode])) {
            throw new \RuntimeException('Attribute for ' . $attributeCode . ' does not exist');
        }
        return $this->ids[$attributeCode];
    }
 
    function updateMapping(): bool {
        $attributes = $this->api->get('/custom-attributes');
        foreach ($attributes as $attribute) {
            $data = self::$definition[$attribute['name']] ?? NULL;
            if ($data !== NULL) {
                $this->ids[$data['code']] = $attribute['id'];
            }
        }
 
        $changed = FALSE;
        foreach (self::$definition as $name => $data) {
            if (isset($this->ids[$data['code']])) {
                continue;
            }
 
            $result = $this->api->post(
                '/custom-attributes',
                [
                    'name' => $name,
                    'clientZoneVisible' => FALSE,
                    'attributeType' => $data['type'],
                ]
            );
            $this->ids[$data['code']] = $result['id'];
            $changed = TRUE;
        }
 
        return $changed;
    }
 
    function saveMapping(): void {
        if (!is_dir(dirname($this->attributesFile))) {
            mkdir(dirname($this->attributesFile));
        }
 
        file_put_contents($this->attributesFile, json_encode($this->ids));
    }
}