- Модуль: crm
- Путь к файлу: ~/bitrix/modules/crm/classes/general/crm_invoice.php
- Класс: \CAllCrmInvoice
- Вызов: CAllCrmInvoice::createOfferIBlocks
static function createOfferIBlocks()
{
if (!Loader::includeModule('iblock'))
return false;
$quota = new \CDiskQuota();
if(!$quota->checkDiskQuota([]))
return false;
if (!Loader::includeModule('catalog'))
return true;
$simpleCatalogs = [];
$parentIblocks = [];
$iterator = Catalog\CatalogIblockTable::getList([
'select' => ['IBLOCK_ID', 'PRODUCT_IBLOCK_ID']
]);
while ($row = $iterator->fetch())
{
$row['IBLOCK_ID'] = (int)$row['IBLOCK_ID'];
$row['PRODUCT_IBLOCK_ID'] = (int)$row['PRODUCT_IBLOCK_ID'];
if ($row['PRODUCT_IBLOCK_ID'] > 0)
{
$parentIblocks[$row['PRODUCT_IBLOCK_ID']] = $row['PRODUCT_IBLOCK_ID'];
continue;
}
$simpleCatalogs[$row['IBLOCK_ID']] = $row['IBLOCK_ID'];
}
unset($row, $iterator);
foreach ($parentIblocks as $id)
{
if (isset($simpleCatalogs[$id]))
unset($simpleCatalogs[$id]);
}
unset($id, $parentIblocks);
if (empty($simpleCatalogs))
return true;
$iblockList = [];
$iterator = Iblock\IblockTable::getList([
'select' => ['ID', 'NAME', 'XML_ID', 'IBLOCK_TYPE_ID', 'SORT'],
'filter' => ['@ID' => $simpleCatalogs],
'order' => ['ID' => 'ASC']
]);
while ($row = $iterator->fetch())
{
$id = (int)$row['ID'];
$row['LID'] = [];
$iblockList[$id] = $row;
}
unset($row, $iterator);
unset($simpleCatalogs);
if (empty($iblockList))
return true;
$iterator = Iblock\IblockSiteTable::getList([
'select' => ['IBLOCK_ID', 'SITE_ID'],
'filter' => ['@IBLOCK_ID' => array_keys($iblockList)]
]);
while ($row = $iterator->fetch())
{
$id = (int)$row['IBLOCK_ID'];
$iblockList[$id]['LID'][] = $row['SITE_ID'];
}
unset($row, $iterator);
// get default vat
$iterator = Catalog\VatTable::getList([
'select' => ['ID', 'SORT'],
'order' => ['SORT' => 'ASC'],
'limit' => 1
]);
$vat = $iterator->fetch();
unset($iterator);
foreach ($iblockList as $id => $data)
{
$iblock = new \CIBlock();
$fields = [
'NAME' => Loc::getMessage('CRM_CATALOG_OFFERS_TITLE_FORMAT', ['#CATALOG#' => $data['NAME']]),
'ACTIVE' => 'Y',
'IBLOCK_TYPE_ID' => $data['IBLOCK_TYPE_ID'],
'LID' => $data['LID'],
'SORT' => $data['SORT'] + 10,
'INDEX_ELEMENT' => 'N',
'WORKFLOW' => 'N',
'BIZPROC' => 'N',
'VERSION' => 1,
'GROUP_ID' => array(1 => 'X', 2 => 'R'),
'LIST_MODE' => 'S'
];
$xmlId = $data['XML_ID'];
if (strncmp($xmlId, 'crm_external_', 13) == 0)
$xmlId = str_replace('crm_external_', 'crm_external_offers_', $xmlId);
elseif ($xmlId = 'FUTURE-1C-CATALOG')
$xmlId = 'FUTURE-1C-CATALOG-OFFERS';
else
$xmlId .= '_offers';
$fields['XML_ID'] = $xmlId;
unset($xmlId);
$offersId = $iblock->Add($fields);
if ($offersId === false)
return false;
$propertyId = \CIBlockPropertyTools::createProperty(
$offersId,
\CIBlockPropertyTools::CODE_SKU_LINK,
['LINK_IBLOCK_ID' => $id]
);
if (!$propertyId)
return false;
$offersFields = [
'IBLOCK_ID' => $offersId,
'PRODUCT_IBLOCK_ID' => $id,
'SKU_PROPERTY_ID' => $propertyId,
];
if (!empty($vat))
$offersFields['VAT_ID'] = (int)$vat['ID'];
if (!\CCatalog::Add($offersFields))
return false;
}
unset($id, $data);
unset($iblockList);
return true;
}