10 private const TYPE_INDIVIDUAL =
'individual';
11 private const TYPE_COMPANY =
'company';
22 if (isset($result[
'PRODUCTS']))
32 $productIdsFromCheck = self::getProductIdsFromCheck($result[
'PRODUCTS']);
46 $productsInfo = self::getProductsInfo($productIdsFromCheck);
60 $contractIds = self::getContractIdsByProductsInfo($productsInfo);
61 $contractIdList = array_unique(array_merge(
62 array_values($contractIds[
'products']),
63 array_values($contractIds[
'sections'])
88 $contractorData = self::getContractorDataByContractIds($contractIdList);
90 foreach ($result[
'PRODUCTS'] as $index => $product)
92 $productId = isset($product[
'PRODUCT_ID']) && (int)$product[
'PRODUCT_ID'] > 0
93 ? (
int)$product[
'PRODUCT_ID']
99 if (!isset($productsInfo[$productId]))
106 $productInfo = $productsInfo[$productId];
107 foreach ($productInfo[
'sections'] as $sectionId)
109 if (isset($contractIds[
'sections'][$sectionId]))
111 $contractId = $contractIds[
'sections'][$sectionId];
118 $contractId = $contractIds[
'products'][$productInfo[
'parent_id']] ??
null;
126 $contractor = $contractorData[
'contracts'][$contractId] ??
null;
129 $company = $contractor[
'company'] ??
null;
130 $individual = $contractor[
'individual'] ??
null;
132 if ($company && !empty($contractorData[self::TYPE_COMPANY][$company]))
134 $result[
'PRODUCTS'][$index][
'SUPPLIER_INFO'] = self::getSupplierInfo(
135 $contractorData[self::TYPE_COMPANY][$company]
138 elseif ($individual && !empty($contractorData[self::TYPE_INDIVIDUAL][$individual]))
140 $result[
'PRODUCTS'][$index][
'SUPPLIER_INFO'] = self::getSupplierInfo(
141 $contractorData[self::TYPE_INDIVIDUAL][$individual]
158 private static function getProductIdsFromCheck(array $products): array
160 return array_unique(array_filter(array_column($products,
'PRODUCT_ID')));
163 private static function getProductsInfo(array $productIdsFromCheck): array
167 $repositoryFacade = Catalog\v2\IoC\ServiceContainer::getRepositoryFacade();
169 foreach ($productIdsFromCheck as $productIdFromCheck)
171 $product = $repositoryFacade->loadProduct($productIdFromCheck);
174 $result[$productIdFromCheck] = [
175 'parent_id' => $product->getId(),
176 'sections' => $product->getSectionCollection()->getValues(),
181 $variation = $repositoryFacade->loadVariation($productIdFromCheck);
184 $parent = $variation->getParent();
187 $product = $repositoryFacade->loadProduct($parent->getId());
190 $result[$productIdFromCheck] = [
191 'parent_id' => $product->getId(),
192 'sections' => $product->getSectionCollection()->getValues(),
203 private static function getContractIdsByProductsInfo(array $productsInfo): array
213 foreach ($productsInfo as $productInfo)
215 $productIds[] = $productInfo[
'parent_id'];
216 $sectionIds[] = $productInfo[
'sections'];
219 $sectionIds = array_unique(array_merge(...$sectionIds));
221 $agentProductIterator = Catalog\AgentProductTable::getList([
222 'select' => [
'CONTRACT_ID',
'PRODUCT_ID',
'PRODUCT_TYPE'],
226 '=PRODUCT_ID' => $productIds,
230 '=PRODUCT_ID' => $sectionIds,
235 'order' => [
'ID' =>
'ASC'],
238 while ($agentProduct = $agentProductIterator->fetch())
242 $result[
'products'][$agentProduct[
'PRODUCT_ID']] = (int)$agentProduct[
'CONTRACT_ID'];
246 $result[
'sections'][$agentProduct[
'PRODUCT_ID']] = (int)$agentProduct[
'CONTRACT_ID'];
259 private static function getContractorDataByContractIds(array $contractIds): array
261 $contractorsProvider = Catalog\v2\Contractor\Provider\Manager::getActiveProvider(
262 Catalog\v2\Contractor\Provider\Manager::PROVIDER_AGENT_CONTRACT
264 if ($contractorsProvider)
266 return self::getProviderContractorDataByContractIds($contractorsProvider, $contractIds);
269 return self::getCatalogContractorDataByContractIds($contractIds);
272 private static function getCatalogContractorDataByContractIds(array $contractIds): array
280 $contractorIterator = Catalog\AgentContractTable::getList([
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',
293 '=ID' => $contractIds,
297 while ($contractor = $contractorIterator->fetch())
300 if (!empty($contractor[
'CONTRACTOR_PERSON_TYPE']))
302 $personName = $contractor[
'CONTRACTOR_PERSON_TYPE'];
305 if (!empty($contractor[
'CONTRACTOR_PERSON_LASTNAME']))
307 $personName = $personName
308 ? $personName .
' ' . $contractor[
'CONTRACTOR_PERSON_LASTNAME']
309 : $contractor[
'CONTRACTOR_PERSON_LASTNAME']
313 $type = self::TYPE_INDIVIDUAL;
316 $type = self::TYPE_COMPANY;
319 $phone = $contractor[
'CONTRACTOR_PHONE'];
322 $phone = preg_split(
'/[.,;]+/', $phone);
323 $phone = array_filter(array_map(
'trim', $phone));
330 $result[
'contracts'][$contractor[
'ID']][$type] = $contractor[
'CONTRACTOR_ID'];
331 $result[$type][$contractor[
'CONTRACTOR_ID']] = [
333 'PERSON_NAME' => $personName,
334 'COMPANY_NAME' => $contractor[
'CONTRACTOR_COMPANY'],
336 'INN' => $contractor[
'CONTRACTOR_INN'],
343 private static function getProviderContractorDataByContractIds(Catalog\v2\Contractor\Provider\IProvider $provider, array $contractIds): array
351 if (!Main\Loader::includeModule(
'crm'))
356 foreach ($contractIds as $contractId)
358 $contractor = $provider::getContractorByDocumentId($contractId);
361 $type = self::TYPE_INDIVIDUAL;
362 if ($contractor instanceof \
Bitrix\Crm\Integration\Catalog\Contractor\Company)
364 $type = self::TYPE_COMPANY;
367 $result[
'contracts'][$contractId][$type] = $contractor->getId();
368 $result[$type][$contractor->getId()] = [
370 'PERSON_NAME' => $contractor->getContactPersonFullName(),
371 'COMPANY_NAME' => $contractor->getName(),
372 'PHONES' => [$contractor->getPhone()],
373 'INN' => $contractor->getInn(),
387 private static function getSupplierInfo(array $contractor): array
389 $name = ($contractor[
'TYPE'] === self::TYPE_INDIVIDUAL)
390 ? $contractor[
'PERSON_NAME']
391 : $contractor[
'COMPANY_NAME']
394 $phone = $contractor[
'PHONES'] ?: [];
399 'INN' => $contractor[
'INN'],