Jacek Kowalski
2023-06-14 5516fefb1c810bbef2302f61ad2863745e0866fd
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
 
namespace SIPL\UCRM\wFirma;
 
use Webit\WFirmaSDK\Contractors as Contractors;
use Webit\WFirmaSDK\Payments as Payments;
 
class ContractorSynchronizer extends Synchronizer {
    function synchronize(int $ucrmClientId): bool {
        $crm = $this->helper->getApi();
        $wFirmaContractors = $this->wfirma->contractorsApi();
 
        $client = $crm->get('/clients/' . $ucrmClientId);
        $clientInvoiceCountry = [];
        if ($client['invoiceCountryId']) {
            $clientInvoiceCountry = $crm->get('/countries/' . $client['invoiceCountryId']);
        }
        $clientCountry = [];
        if ($client['countryId']) {
            $clientCountry = $crm->get('/countries/' . $client['countryId']);
        }
 
        $clientAttributeId = $this->helper->getAttributes()->getIdForCode('client');
 
        $wFirmaId = '';
        foreach ($client['attributes'] as $attribute) {
            if ($attribute['customAttributeId'] === $clientAttributeId) {
                $wFirmaId = $attribute['value'];
            }
        }
 
        $contractor = new Contractors\Contractor('');
        if ($wFirmaId) {
            $contractor = $wFirmaContractors->get(
                Contractors\ContractorId::create($wFirmaId)
            );
        }
 
        /** @var Contractors\Contractor $contractor */
 
        $changed = FALSE;
 
        $name = $client['clientType'] == 1 ? trim($client['firstName'] . ' ' . $client['lastName']) : $client['companyName'];
        if ($contractor->name() !== $name and $contractor->altName() !== $name) {
            $changed = TRUE;
            $contractor->rename($name, $contractor->altName());
        }
 
        if ($contractor->nip() != $client['companyTaxId']) {
            $changed = TRUE;
            $contractor->changeNip($client['companyTaxId']);
        }
 
        if ($client['invoiceAddressSameAsContact']) {
            $invoiceAddress = new Contractors\InvoiceAddress(
                $client['street1'],
                $client['zipCode'],
                $client['city'],
                $clientCountry['code']
            );
 
            if ($contractor->invoiceAddress() != $invoiceAddress) {
                $changed = TRUE;
                $contractor->changeInvoiceAddress($invoiceAddress);
            }
            if ($contractor->contactAddress() != NULL) {
                $changed = TRUE;
                $contractor->changeContactAddress();
            }
        } else {
            $invoiceAddress = new Contractors\InvoiceAddress(
                $client['invoiceStreet1'],
                $client['invoiceZipCode'],
                $client['invoiceCity'],
                $clientInvoiceCountry['code'] ?? NULL
            );
            $contactAddress = new Contractors\ContactAddress(
                $client['companyName'],
                $client['street1'],
                $client['zipCode'],
                $client['city'],
                $clientCountry['code'] ?? NULL,
                trim($client['companyContactFirstName'] . ' ' . $client['companyContactLastName'])
            );
 
            if ($contractor->invoiceAddress() != $invoiceAddress) {
                $changed = TRUE;
                $contractor->changeInvoiceAddress($invoiceAddress);
            }
 
            if ($contractor->contactAddress() != $contactAddress) {
                $changed = TRUE;
                $contractor->changeContactAddress($contactAddress);
            }
        }
 
        $paymentSettings = new Contractors\PaymentSettings(
            $client['invoiceMaturityDays'],
            Payments\PaymentMethod::transfer(),
            NULL,
            FALSE
        );
        if ($contractor->paymentSettings() != $paymentSettings) {
            $changed = TRUE;
            $contractor->changePaymentSettings($paymentSettings);
        }
 
        if ($wFirmaId) {
            if ($changed) {
                $wFirmaContractors->edit($contractor);
                return TRUE;
            }
            return FALSE;
        } else {
            $contractor = $wFirmaContractors->add($contractor);
            $crm->patch(
                '/clients/' . $ucrmClientId,
                [
                    'attributes' => [
                        [
                            'customAttributeId' => $clientAttributeId,
                            'value' => (string)($contractor->id()->id()),
                        ],
                    ],
                ]
            );
            return TRUE;
        }
    }
}