Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
check.php
1<?php
2
4
7
8class Check
9{
10 private const TYPE_INDIVIDUAL = 'individual';
11 private const TYPE_COMPANY = 'company';
12
20 public static function onSaleCheckPrepareData($result, $type): array
21 {
22 if (isset($result['PRODUCTS']))
23 {
32 $productIdsFromCheck = self::getProductIdsFromCheck($result['PRODUCTS']);
33
46 $productsInfo = self::getProductsInfo($productIdsFromCheck);
47
60 $contractIds = self::getContractIdsByProductsInfo($productsInfo);
61 $contractIdList = array_unique(array_merge(
62 array_values($contractIds['products']),
63 array_values($contractIds['sections'])
64 ));
65
88 $contractorData = self::getContractorDataByContractIds($contractIdList);
89
90 foreach ($result['PRODUCTS'] as $index => $product)
91 {
92 $productId = isset($product['PRODUCT_ID']) && (int)$product['PRODUCT_ID'] > 0
93 ? (int)$product['PRODUCT_ID']
94 : null
95 ;
96
97 if ($productId)
98 {
99 if (!isset($productsInfo[$productId]))
100 {
101 continue;
102 }
103
104 $contractId = null;
105
106 $productInfo = $productsInfo[$productId];
107 foreach ($productInfo['sections'] as $sectionId)
108 {
109 if (isset($contractIds['sections'][$sectionId]))
110 {
111 $contractId = $contractIds['sections'][$sectionId];
112 break;
113 }
114 }
115
116 if (!$contractId)
117 {
118 $contractId = $contractIds['products'][$productInfo['parent_id']] ?? null;
119 }
120
121 if (!$contractId)
122 {
123 continue;
124 }
125
126 $contractor = $contractorData['contracts'][$contractId] ?? null;
127 if ($contractor)
128 {
129 $company = $contractor['company'] ?? null;
130 $individual = $contractor['individual'] ?? null;
131
132 if ($company && !empty($contractorData[self::TYPE_COMPANY][$company]))
133 {
134 $result['PRODUCTS'][$index]['SUPPLIER_INFO'] = self::getSupplierInfo(
135 $contractorData[self::TYPE_COMPANY][$company]
136 );
137 }
138 elseif ($individual && !empty($contractorData[self::TYPE_INDIVIDUAL][$individual]))
139 {
140 $result['PRODUCTS'][$index]['SUPPLIER_INFO'] = self::getSupplierInfo(
141 $contractorData[self::TYPE_INDIVIDUAL][$individual]
142 );
143 }
144 }
145 }
146 }
147 }
148
149 return $result;
150 }
151
158 private static function getProductIdsFromCheck(array $products): array
159 {
160 return array_unique(array_filter(array_column($products, 'PRODUCT_ID')));
161 }
162
163 private static function getProductsInfo(array $productIdsFromCheck): array
164 {
165 $result = [];
166
167 $repositoryFacade = Catalog\v2\IoC\ServiceContainer::getRepositoryFacade();
168
169 foreach ($productIdsFromCheck as $productIdFromCheck)
170 {
171 $product = $repositoryFacade->loadProduct($productIdFromCheck);
172 if ($product)
173 {
174 $result[$productIdFromCheck] = [
175 'parent_id' => $product->getId(),
176 'sections' => $product->getSectionCollection()->getValues(),
177 ];
178 }
179 else
180 {
181 $variation = $repositoryFacade->loadVariation($productIdFromCheck);
182 if ($variation)
183 {
184 $parent = $variation->getParent();
185 if ($parent)
186 {
187 $product = $repositoryFacade->loadProduct($parent->getId());
188 if ($product)
189 {
190 $result[$productIdFromCheck] = [
191 'parent_id' => $product->getId(),
192 'sections' => $product->getSectionCollection()->getValues(),
193 ];
194 }
195 }
196 }
197 }
198 }
199
200 return $result;
201 }
202
203 private static function getContractIdsByProductsInfo(array $productsInfo): array
204 {
205 $result = [
206 'products' => [],
207 'sections' => [],
208 ];
209
210 $productIds = [];
211 $sectionIds = [];
212
213 foreach ($productsInfo as $productInfo)
214 {
215 $productIds[] = $productInfo['parent_id'];
216 $sectionIds[] = $productInfo['sections'];
217 }
218
219 $sectionIds = array_unique(array_merge(...$sectionIds));
220
221 $agentProductIterator = Catalog\AgentProductTable::getList([
222 'select' => ['CONTRACT_ID', 'PRODUCT_ID', 'PRODUCT_TYPE'],
223 'filter' => [
224 'LOGIC' => 'OR',
225 [
226 '=PRODUCT_ID' => $productIds,
227 '=PRODUCT_TYPE' => Catalog\AgentProductTable::PRODUCT_TYPE_PRODUCT,
228 ],
229 [
230 '=PRODUCT_ID' => $sectionIds,
231 '=PRODUCT_TYPE' => Catalog\AgentProductTable::PRODUCT_TYPE_SECTION,
232 ]
233
234 ],
235 'order' => ['ID' => 'ASC'], // DESC ?
236 // group ?
237 ]);
238 while ($agentProduct = $agentProductIterator->fetch())
239 {
240 if ($agentProduct['PRODUCT_TYPE'] === Catalog\AgentProductTable::PRODUCT_TYPE_PRODUCT)
241 {
242 $result['products'][$agentProduct['PRODUCT_ID']] = (int)$agentProduct['CONTRACT_ID'];
243 }
244 elseif ($agentProduct['PRODUCT_TYPE'] === Catalog\AgentProductTable::PRODUCT_TYPE_SECTION)
245 {
246 $result['sections'][$agentProduct['PRODUCT_ID']] = (int)$agentProduct['CONTRACT_ID'];
247 }
248 }
249
250 return $result;
251 }
252
259 private static function getContractorDataByContractIds(array $contractIds): array
260 {
261 $contractorsProvider = Catalog\v2\Contractor\Provider\Manager::getActiveProvider(
262 Catalog\v2\Contractor\Provider\Manager::PROVIDER_AGENT_CONTRACT
263 );
264 if ($contractorsProvider)
265 {
266 return self::getProviderContractorDataByContractIds($contractorsProvider, $contractIds);
267 }
268
269 return self::getCatalogContractorDataByContractIds($contractIds);
270 }
271
272 private static function getCatalogContractorDataByContractIds(array $contractIds): array
273 {
274 $result = [
275 'contracts' => [],
276 'individual' => [],
277 'company' => [],
278 ];
279
280 $contractorIterator = Catalog\AgentContractTable::getList([
281 'select' => [
282 'ID',
283 'CONTRACTOR_ID',
284 'CONTRACTOR_PERSON_TYPE' => 'CONTRACTOR.PERSON_TYPE',
285 'CONTRACTOR_PERSON_NAME' => 'CONTRACTOR.PERSON_NAME',
286 'CONTRACTOR_PERSON_LASTNAME' => 'CONTRACTOR.PERSON_LASTNAME',
287 'CONTRACTOR_COMPANY' => 'CONTRACTOR.COMPANY',
288 'CONTRACTOR_PHONE' => 'CONTRACTOR.PHONE',
289 'CONTRACTOR_ADDRESS' => 'CONTRACTOR.ADDRESS',
290 'CONTRACTOR_INN' => 'CONTRACTOR.INN',
291 ],
292 'filter' => [
293 '=ID' => $contractIds,
294 ],
295 ]);
296
297 while ($contractor = $contractorIterator->fetch())
298 {
299 $personName = '';
300 if (!empty($contractor['CONTRACTOR_PERSON_TYPE']))
301 {
302 $personName = $contractor['CONTRACTOR_PERSON_TYPE'];
303 }
304
305 if (!empty($contractor['CONTRACTOR_PERSON_LASTNAME']))
306 {
307 $personName = $personName
308 ? $personName . ' ' . $contractor['CONTRACTOR_PERSON_LASTNAME']
309 : $contractor['CONTRACTOR_PERSON_LASTNAME']
310 ;
311 }
312
313 $type = self::TYPE_INDIVIDUAL;
314 if ($contractor['CONTRACTOR_PERSON_TYPE'] === Catalog\ContractorTable::TYPE_COMPANY)
315 {
316 $type = self::TYPE_COMPANY;
317 }
318
319 $phone = $contractor['CONTRACTOR_PHONE'];
320 if ($phone)
321 {
322 $phone = preg_split('/[.,;]+/', $phone);
323 $phone = array_filter(array_map('trim', $phone));
324 }
325 else
326 {
327 $phone = [];
328 }
329
330 $result['contracts'][$contractor['ID']][$type] = $contractor['CONTRACTOR_ID'];
331 $result[$type][$contractor['CONTRACTOR_ID']] = [
332 'TYPE' => $type,
333 'PERSON_NAME' => $personName,
334 'COMPANY_NAME' => $contractor['CONTRACTOR_COMPANY'],
335 'PHONES' => $phone,
336 'INN' => $contractor['CONTRACTOR_INN'],
337 ];
338 }
339
340 return $result;
341 }
342
343 private static function getProviderContractorDataByContractIds(Catalog\v2\Contractor\Provider\IProvider $provider, array $contractIds): array
344 {
345 $result = [
346 'contracts' => [],
347 'individual' => [],
348 'company' => [],
349 ];
350
351 if (!Main\Loader::includeModule('crm'))
352 {
353 return $result;
354 }
355
356 foreach ($contractIds as $contractId)
357 {
358 $contractor = $provider::getContractorByDocumentId($contractId);
359 if ($contractor)
360 {
361 $type = self::TYPE_INDIVIDUAL;
362 if ($contractor instanceof \Bitrix\Crm\Integration\Catalog\Contractor\Company)
363 {
364 $type = self::TYPE_COMPANY;
365 }
366
367 $result['contracts'][$contractId][$type] = $contractor->getId();
368 $result[$type][$contractor->getId()] = [
369 'TYPE' => $type,
370 'PERSON_NAME' => $contractor->getContactPersonFullName(),
371 'COMPANY_NAME' => $contractor->getName(),
372 'PHONES' => [$contractor->getPhone()],
373 'INN' => $contractor->getInn(),
374 ];
375 }
376 }
377
378 return $result;
379 }
380
387 private static function getSupplierInfo(array $contractor): array
388 {
389 $name = ($contractor['TYPE'] === self::TYPE_INDIVIDUAL)
390 ? $contractor['PERSON_NAME']
391 : $contractor['COMPANY_NAME']
392 ;
393
394 $phone = $contractor['PHONES'] ?: [];
395
396 return [
397 'PHONES' => $phone,
398 'NAME' => $name,
399 'INN' => $contractor['INN'],
400 ];
401 }
402}