Passed
Pull Request — developer (#17179)
by Mariusz
29:27 queued 10:21
created

Account::convertVatId()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 12
rs 10
c 1
b 0
f 0
cc 4
nc 3
nop 3
1
<?php
2
3
/**
4
 * Comarch account synchronization map file.
5
 *
6
 * The file is part of the paid functionality. Using the file is allowed only after purchasing a subscription.
7
 * File modification allowed only with the consent of the system producer.
8
 *
9
 * @package Integration
10
 *
11
 * @copyright YetiForce S.A.
12
 * @license   YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com)
13
 * @author    Mariusz Krzaczkowski <[email protected]>
14
 */
15
16
namespace App\Integrations\Comarch\Xl\Maps;
17
18
/**
19
 * Comarch account synchronization map class.
20
 */
21
class Account extends \App\Integrations\Comarch\Map
22
{
23
	/** {@inheritdoc} */
24
	const API_NAME_ID = 'knt_GidNumer';
25
	/** {@inheritdoc} */
26
	protected $moduleName = 'Accounts';
27
	/** {@inheritdoc} */
28
	protected $fieldMap = [
29
		'accountname' => ['names' => ['get' => 'knt_Nazwa1', 'create' => 'Nazwa1', 'update' => 'Nazwa1']],
30
		'vat_id' => ['names' => ['get' => 'knt_Nip', 'create' => 'Nip', 'update' => 'Nip'], 'fn' => 'convertVatId'],
31
		'registration_number_2' => ['names' => ['get' => 'knt_Regon', 'create' => 'Regon', 'update' => 'Regon']],
32
		'account_short_name' => ['names' => ['get' => 'knt_Akronim', 'create' => 'Akronim', 'update' => 'Akronim']],
33
		'account_second_name' => ['names' => ['get' => 'knt_Nazwa2', 'create' => 'Nazwa2', 'update' => 'Nazwa2']],
34
		'account_third_name' => ['names' => ['get' => 'knt_Nazwa3', 'create' => 'Nazwa3', 'update' => 'Nazwa3']],
35
		'accounttype' => [
36
			'names' => ['get' => 'knt_Rodzaj', 'create' => 'Rodzaj', 'update' => 'Rodzaj'],
37
			'fn' => 'findBySynchronizer', 'synchronizer' => 'AccountTypes'
38
		],
39
		'payment_methods' => [
40
			'names' => ['get' => 'knt_FormaPl', 'create' => 'FormaPl', 'update' => 'FormaPl'],
41
			'fn' => 'findBySynchronizer', 'synchronizer' => 'PaymentMethods'
42
		],
43
		'crmactivity' => ['names' => ['get' => 'knt_SpTerminPlSpr', 'create' => 'TerminPlSpr', 'update' => 'LimitOkres']],
44
		'addresslevel1a' => [
45
			'names' => ['get' => 'knt_Kraj', 'create' => 'Kraj', 'update' => 'Kraj'], 'fn' => 'convertCountry'
46
		],
47
		'addresslevel2a' => ['names' => ['get' => 'knt_Wojewodztwo', 'create' => 'Wojewodztwo', 'update' => 'Wojewodztwo']],
48
		'addresslevel3a' => ['names' => ['get' => 'knt_Powiat', 'create' => 'Powiat', 'update' => 'Powiat']],
49
		'addresslevel4a' => ['names' => ['get' => 'knt_Gmina', 'create' => 'Gmina', 'update' => 'Gmina']],
50
		'addresslevel5a' => ['names' => ['get' => 'knt_Miasto', 'create' => 'Miasto', 'update' => 'Miasto']],
51
		'addresslevel7a' => ['names' => ['get' => 'knt_KodP', 'create' => 'KodP', 'update' => 'KodP']],
52
		'addresslevel8a' => ['names' => ['get' => 'knt_Ulica', 'create' => 'Ulica', 'update' => 'Ulica']],
53
		'email1' => ['names' => ['get' => 'knt_Email', 'create' => 'Email', 'update' => 'Email']],
54
		'phone' => [
55
			'names' => ['get' => 'knt_Telefon1', 'create' => 'Telefon1', 'update' => 'Telefon1'],
56
			'fn' => 'convertPhone'
57
		],
58
		'otherphone' => [
59
			'names' => ['get' => 'knt_Gsm', 'create' => 'Gsm', 'update' => 'Gsm'],
60
			'fn' => 'convertPhone', 'optional' => true
61
		],
62
		'fax' => [
63
			'names' => ['get' => 'knt_Fax', 'create' => 'Fax', 'update' => 'Fax'],
64
			'fn' => 'convertPhone', 'optional' => true
65
		],
66
	];
67
	/** {@inheritdoc} */
68
	protected $defaultDataYf = [
69
		'fieldMap' => [
70
			'legal_form' => 'PLL_COMPANY'
71
		]
72
	];
73
	/** {@inheritdoc} */
74
	protected $dependentSynchronizations = ['BankAccounts'];
75
76
	/** {@inheritdoc} */
77
	public function findRecordInYf(): ?int
78
	{
79
		$queryGenerator = new \App\QueryGenerator($this->moduleName);
80
		$queryGenerator->setStateCondition('All');
81
		$queryGenerator->setFields(['id'])->permissions = false;
82
		if (!empty($this->dataYf[self::FIELD_NAME_ID])) {
83
			$queryGenerator->addCondition('comarch_server_id', $this->synchronizer->config->get('id'), 'e');
84
			$queryGenerator->addCondition(self::FIELD_NAME_ID, $this->dataYf[self::FIELD_NAME_ID], 'e');
85
		} elseif (!empty($this->dataYf['account_short_name'])) {
86
			$queryGenerator->addCondition('account_short_name', $this->dataYf['account_short_name'], 'e');
87
		} elseif (!empty($this->dataYf['vat_id'])) {
88
			$queryGenerator->addCondition('vat_id', $this->dataYf['vat_id'], 'e');
89
		}
90
		return $queryGenerator->createQuery()->scalar() ?: null;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $queryGenerator->...ery()->scalar() ?: null could return the type string which is incompatible with the type-hinted return integer|null. Consider adding an additional type-check to rule them out.
Loading history...
91
	}
92
93
	/** {@inheritdoc} */
94
	public function saveInApi(): void
95
	{
96
		if (empty($this->dataApi['id'])) {
97
			$response = $this->synchronizer->controller->getConnector()
98
				->request('POST', 'customer/create', $this->dataApi);
99
			$response = \App\Json::decode($response);
100
			$this->recordModel->set(self::FIELD_NAME_ID, $response['id']);
101
			$this->recordModel->save();
102
			$this->dataYf[self::FIELD_NAME_ID] = $this->dataApi['id'] = $response['id'];
103
		} else {
104
			$id = $this->dataApi['id'];
105
			unset($this->dataApi['id']);
106
			$this->synchronizer->controller->getConnector()
107
				->request('PUT', 'Customer/Update/' . $id, $this->dataApi);
108
			$this->dataApi['id'] = $id;
109
		}
110
		$this->synchronizer->updateMapIdCache(
111
			$this->recordModel->getModuleName(),
112
			$this->dataApi['id'], $this->recordModel->getId()
113
		);
114
		$this->runDependentSynchronizer(false);
115
	}
116
117
	/** {@inheritdoc} */
118
	public function findRecordInApi(): int
119
	{
120
		$response = '';
121
		if (!empty($this->dataYf['account_short_name'])) {
122
			try {
123
				$response = $this->synchronizer->controller->getConnector()
124
					->request('GET', 'Customer/Get?akronim=' . $this->dataYf['account_short_name']);
125
				if ($response && ($response = \App\Json::decode($response))) {
126
					return $response[0]['knt_GidNumer'];
127
				}
128
			} catch (\Throwable $th) {
129
				$response = 0;
130
			}
131
		}
132
		if (!empty($this->dataApi['Nip'])) {
133
			try {
134
				$vatId = str_replace([' ', ',', '.', '-'], '', $this->dataApi['Nip']);
135
				$response = $this->synchronizer->controller->getConnector()
136
					->request('GET', 'Customer/Get?nip=' . $vatId);
137
				if ($response && ($response = \App\Json::decode($response))) {
138
					return $response[0]['knt_GidNumer'];
139
				}
140
			} catch (\Throwable $th) {
141
				$response = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $response is dead and can be removed.
Loading history...
142
			}
143
		}
144
		return 0;
145
	}
146
147
	/**
148
	 * Convert Vat Id.
149
	 *
150
	 * @param mixed $value
151
	 * @param array $field
152
	 * @param bool  $fromApi
153
	 *
154
	 * @return string
155
	 */
156
	protected function convertVatId($value, array $field, bool $fromApi): string
157
	{
158
		if ($fromApi) {
159
			$value = ($this->dataApi['knt_NipPrefiks'] ?? '') . $value;
160
		} else {
161
			$this->dataApi['NipPrefiks'] = '';
162
			if (($pre = substr($value, 0, 2)) && !\is_numeric($pre)) {
163
				$value = substr($value, 2);
164
				$this->dataApi['NipPrefiks'] = $pre;
165
			}
166
		}
167
		return $value;
168
	}
169
}
170