2021-05-31 16:33:02 +03:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Intaro\RetailCrm\Icml;
|
|
|
|
|
|
2021-11-02 11:25:26 +03:00
|
|
|
|
use Bitrix\Catalog\ProductTable;
|
2021-05-31 16:33:02 +03:00
|
|
|
|
use Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo;
|
|
|
|
|
use Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams;
|
|
|
|
|
use Intaro\RetailCrm\Model\Bitrix\Xml\XmlData;
|
|
|
|
|
use Intaro\RetailCrm\Model\Bitrix\Xml\XmlOffer;
|
|
|
|
|
use Intaro\RetailCrm\Model\Bitrix\Xml\XmlSetup;
|
|
|
|
|
use Intaro\RetailCrm\Repository\CatalogRepository;
|
|
|
|
|
use Logger;
|
|
|
|
|
use RetailcrmConfigProvider;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class IcmlDirector
|
|
|
|
|
* @package Intaro\RetailCrm\Icml
|
|
|
|
|
*/
|
|
|
|
|
class IcmlDirector
|
|
|
|
|
{
|
|
|
|
|
public const INFO = 'INFO';
|
|
|
|
|
public const OFFERS_PART = 500;
|
|
|
|
|
public const FILE_LOG_NAME = 'i_crm_load_log';
|
|
|
|
|
public const DEFAULT_PRODUCT_PAGE_SIZE = 1;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var IcmlWriter
|
|
|
|
|
*/
|
|
|
|
|
private $icmlWriter;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var \Intaro\RetailCrm\Icml\XmlOfferDirector
|
|
|
|
|
*/
|
|
|
|
|
private $xmlOfferDirector;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var \Intaro\RetailCrm\Model\Bitrix\Xml\XmlSetup
|
|
|
|
|
*/
|
|
|
|
|
private $setup;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var \Intaro\RetailCrm\Repository\CatalogRepository
|
|
|
|
|
*/
|
|
|
|
|
private $catalogRepository;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $shopName;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var \Intaro\RetailCrm\Icml\XmlCategoryDirector
|
|
|
|
|
*/
|
|
|
|
|
private $xmlCategoryDirector;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var \Intaro\RetailCrm\Icml\QueryParamsMolder
|
|
|
|
|
*/
|
|
|
|
|
private $queryBuilder;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var \Intaro\RetailCrm\Model\Bitrix\Xml\XmlData
|
|
|
|
|
*/
|
|
|
|
|
private $xmlData;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @var \Logger
|
|
|
|
|
*/
|
|
|
|
|
private $logger;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* RetailCrmlXml constructor.
|
|
|
|
|
*
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlSetup $setup
|
|
|
|
|
* @param \Logger $logger
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(XmlSetup $setup, Logger $logger)
|
|
|
|
|
{
|
|
|
|
|
$this->setup = $setup;
|
|
|
|
|
$this->shopName = RetailcrmConfigProvider::getSiteName();
|
|
|
|
|
$this->icmlWriter = new IcmlWriter($this->setup->filePath);
|
|
|
|
|
$this->xmlOfferDirector = new XmlOfferDirector($this->setup);
|
2024-04-24 09:48:51 +03:00
|
|
|
|
$this->xmlCategoryDirector = new XmlCategoryDirector($this->setup);
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$this->queryBuilder = new QueryParamsMolder();
|
|
|
|
|
$this->xmlData = new XmlData();
|
|
|
|
|
$this->logger = $logger;
|
2024-04-24 09:48:51 +03:00
|
|
|
|
$this->catalogRepository = new CatalogRepository();
|
|
|
|
|
|
|
|
|
|
$this->catalogRepository->setLoadNotActive($this->setup->loadNonActivity);
|
2021-05-31 16:33:02 +03:00
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* Основной метод. Генерирует icml файл католога товаров Битрикс
|
|
|
|
|
*/
|
|
|
|
|
public function generateXml(): void
|
|
|
|
|
{
|
2021-11-12 17:11:52 +03:00
|
|
|
|
unlink($this->setup->filePath);
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$this->setXmlData();
|
|
|
|
|
$this->icmlWriter->writeToXmlTop($this->xmlData);
|
|
|
|
|
$this->logger->write(
|
|
|
|
|
self::INFO . ': Start writing categories and header',
|
|
|
|
|
self::FILE_LOG_NAME
|
|
|
|
|
);
|
|
|
|
|
$this->icmlWriter->writeToXmlHeaderAndCategories($this->xmlData);
|
|
|
|
|
$this->logger->write(
|
|
|
|
|
self::INFO . ': End writing categories in XML and Start writing offers',
|
|
|
|
|
self::FILE_LOG_NAME
|
|
|
|
|
);
|
|
|
|
|
$this->writeOffers();
|
|
|
|
|
$this->logger->write(
|
|
|
|
|
self::INFO . ': End writing offers in XML',
|
|
|
|
|
self::FILE_LOG_NAME
|
|
|
|
|
);
|
|
|
|
|
$this->icmlWriter->writeToXmlBottom();
|
|
|
|
|
$this->logger->write(
|
|
|
|
|
self::INFO . ': Loading complete (peak memory usage: ' . memory_get_peak_usage() . ')',
|
|
|
|
|
self::FILE_LOG_NAME
|
|
|
|
|
);
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
private function setXmlData(): void
|
|
|
|
|
{
|
|
|
|
|
$this->xmlData->shopName = $this->shopName;
|
|
|
|
|
$this->xmlData->company = $this->shopName;
|
|
|
|
|
$this->xmlData->filePath = $this->setup->filePath;
|
|
|
|
|
$this->xmlData->categories = $this->xmlCategoryDirector->getXmlCategories();
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* записывает оферы всех торговых каталогов в xml файл
|
|
|
|
|
*/
|
|
|
|
|
private function writeOffers(): void
|
|
|
|
|
{
|
|
|
|
|
$this->icmlWriter->startOffersBlock();
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
foreach ($this->setup->iblocksForExport as $iblockId) {
|
|
|
|
|
$this->writeIblockOffers($iblockId);
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$this->icmlWriter->endBlock();
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* записывает оферы конкретного торгового каталога товаров в xml файл
|
|
|
|
|
*
|
|
|
|
|
* @param int $productIblockId //ID инфоблока товаров в торговом каталоге
|
|
|
|
|
*/
|
|
|
|
|
private function writeIblockOffers(int $productIblockId): void
|
|
|
|
|
{
|
|
|
|
|
$catalogIblockInfo = $this->catalogRepository->getCatalogIblockInfo($productIblockId);
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
//если нет торговых предложений
|
|
|
|
|
if ($catalogIblockInfo->skuIblockId === null) {
|
|
|
|
|
$selectParams
|
|
|
|
|
= $this->queryBuilder->getSelectParams(
|
|
|
|
|
$this->setup->properties->products->names[$productIblockId],
|
|
|
|
|
$this->setup->basePriceId
|
|
|
|
|
);
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$selectParams->pageNumber = 1;
|
|
|
|
|
$selectParams->nPageSize = self::OFFERS_PART;
|
|
|
|
|
$selectParams->parentId = null;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
$selectParams->allParams = array_merge($selectParams->configurable, $selectParams->main);
|
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
while ($xmlOffers = $this->xmlOfferDirector->getXmlOffersPart($selectParams, $catalogIblockInfo)) {
|
2024-04-24 09:48:51 +03:00
|
|
|
|
$this->icmlWriter->writeOffers($xmlOffers);
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$selectParams->pageNumber++;
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
//если есть торговые предложения
|
|
|
|
|
$paramsForProduct
|
|
|
|
|
= $this->queryBuilder->getSelectParams(
|
|
|
|
|
$this->setup->properties->products->names[$productIblockId],
|
|
|
|
|
$this->setup->basePriceId
|
|
|
|
|
);
|
2021-11-02 11:25:26 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$paramsForOffer
|
|
|
|
|
= $this->queryBuilder->getSelectParams(
|
|
|
|
|
$this->setup->properties->sku->names[$productIblockId],
|
|
|
|
|
$this->setup->basePriceId
|
|
|
|
|
);
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$this->writeOffersAsOffersInXml($paramsForProduct, $paramsForOffer, $catalogIblockInfo);
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* Эта стратегия записи используется,
|
|
|
|
|
* когда в каталоге есть торговые предложения
|
|
|
|
|
*
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams $paramsForProduct
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams $paramsForOffer
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo $catalogIblockInfo
|
|
|
|
|
*/
|
|
|
|
|
private function writeOffersAsOffersInXml(
|
|
|
|
|
SelectParams $paramsForProduct,
|
|
|
|
|
SelectParams $paramsForOffer,
|
|
|
|
|
CatalogIblockInfo $catalogIblockInfo
|
|
|
|
|
): void {
|
|
|
|
|
$paramsForProduct->pageNumber = 1;
|
|
|
|
|
$paramsForProduct->nPageSize = $this->calculateProductPageSize();
|
2021-09-08 16:31:17 +03:00
|
|
|
|
$paramsForProduct->allParams = array_merge($paramsForProduct->configurable, $paramsForProduct->main);
|
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
do {
|
|
|
|
|
$productsPart = $this->xmlOfferDirector->getXmlOffersPart($paramsForProduct, $catalogIblockInfo);
|
|
|
|
|
$paramsForProduct->pageNumber++;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$this->writeProductsOffers($productsPart, $paramsForOffer, $catalogIblockInfo);
|
|
|
|
|
} while (!empty($productsPart));
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* Записывает в файл оферы всех товаров из $products
|
|
|
|
|
*
|
|
|
|
|
* @param XmlOffer[] $products
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams $paramsForOffer
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo $catalogIblockInfo
|
|
|
|
|
*/
|
|
|
|
|
private function writeProductsOffers(
|
|
|
|
|
array $products,
|
|
|
|
|
SelectParams $paramsForOffer,
|
|
|
|
|
CatalogIblockInfo $catalogIblockInfo
|
|
|
|
|
): void {
|
|
|
|
|
$paramsForOffer->nPageSize = $this->calculateOffersPageSize();
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
foreach ($products as $product) {
|
|
|
|
|
$this->writeProductOffers($paramsForOffer, $catalogIblockInfo, $product);
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* Записывает оферы отдельного продукта в xml файл
|
|
|
|
|
*
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams $paramsForOffer
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo $catalogIblockInfo
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlOffer $product
|
|
|
|
|
*/
|
|
|
|
|
private function writeProductOffers(
|
|
|
|
|
SelectParams $paramsForOffer,
|
|
|
|
|
CatalogIblockInfo $catalogIblockInfo,
|
|
|
|
|
XmlOffer $product
|
|
|
|
|
): void {
|
|
|
|
|
$paramsForOffer->pageNumber = 1;
|
|
|
|
|
$writingOffersCount = 0;
|
|
|
|
|
$paramsForOffer->parentId = $product->id;
|
2021-09-08 16:31:17 +03:00
|
|
|
|
$paramsForOffer->allParams = array_merge($paramsForOffer->configurable, $paramsForOffer->main);
|
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
do {
|
2021-11-02 11:25:26 +03:00
|
|
|
|
//Если каталог проиндексирован, у товара есть Тип и это простой товар, то просто записываем его
|
2022-02-04 15:16:14 +03:00
|
|
|
|
if ($product->productType === ProductTable::TYPE_PRODUCT) {
|
2024-04-24 09:48:51 +03:00
|
|
|
|
$this->icmlWriter->writeOffers([$product]);
|
2021-11-02 11:25:26 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$xmlOffersPart
|
|
|
|
|
= $this->xmlOfferDirector->getXmlOffersBySingleProduct($paramsForOffer, $catalogIblockInfo, $product);
|
|
|
|
|
|
|
|
|
|
// если это "простой товар", у которого нет ТП, то просто записываем его
|
|
|
|
|
if ($paramsForOffer->pageNumber === 1 && count($xmlOffersPart) === 0) {
|
2024-04-24 09:48:51 +03:00
|
|
|
|
$this->icmlWriter->writeOffers([$product]);
|
2021-05-31 16:33:02 +03:00
|
|
|
|
break;
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
if (!empty($xmlOffersPart)) {
|
|
|
|
|
$xmlOffersPart
|
|
|
|
|
= $this->trimOffersList($writingOffersCount, $xmlOffersPart);
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2024-04-24 09:48:51 +03:00
|
|
|
|
$this->icmlWriter->writeOffers($xmlOffersPart);
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
$writingOffersCount += count($xmlOffersPart);
|
|
|
|
|
$paramsForOffer->pageNumber++;
|
|
|
|
|
}
|
|
|
|
|
} while ($this->shouldContinueWriting($writingOffersCount, $xmlOffersPart));
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* Проверяет,не достигнул ли лимит по записываемым оферам maxOffersValue
|
|
|
|
|
* и обрезает массив до лимита, если он достигнут
|
|
|
|
|
*
|
|
|
|
|
* @param int $writingOffers
|
|
|
|
|
* @param XmlOffer[] $xmlOffers
|
|
|
|
|
*
|
|
|
|
|
* @return XmlOffer[]
|
|
|
|
|
*/
|
|
|
|
|
private function trimOffersList(int $writingOffers, array $xmlOffers): array
|
|
|
|
|
{
|
|
|
|
|
if (!empty($this->setup->maxOffersValue) && ($writingOffers + count($xmlOffers)) > $this->setup->maxOffersValue) {
|
|
|
|
|
$sliceIndex
|
|
|
|
|
= count($xmlOffers) - ($writingOffers + count($xmlOffers) - $this->setup->maxOffersValue);
|
|
|
|
|
return array_slice($xmlOffers, 0, $sliceIndex);
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
return $xmlOffers;
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* Возвращает размер страницы для запроса товаров
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
private function calculateProductPageSize(): int
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->setup->maxOffersValue)) {
|
|
|
|
|
return self::DEFAULT_PRODUCT_PAGE_SIZE;
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
return (int) ceil(self::OFFERS_PART / $this->setup->maxOffersValue);
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* Возвращает размер страницы для офферов
|
|
|
|
|
*
|
|
|
|
|
* @return int
|
|
|
|
|
*/
|
|
|
|
|
private function calculateOffersPageSize(): int
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->setup->maxOffersValue)) {
|
|
|
|
|
return self::OFFERS_PART;
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
return $this->setup->maxOffersValue < self::OFFERS_PART ?
|
|
|
|
|
$this->setup->maxOffersValue : self::OFFERS_PART;
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
/**
|
|
|
|
|
* Проверяет, нужно ли дальше записывать офферы
|
|
|
|
|
*
|
|
|
|
|
* @param int $writingOffers
|
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\XmlOffer[] $xmlOffers
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function shouldContinueWriting(int $writingOffers, array $xmlOffers): bool
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->setup->maxOffersValue)) {
|
|
|
|
|
return !empty($xmlOffers);
|
|
|
|
|
}
|
2021-09-08 16:31:17 +03:00
|
|
|
|
|
2021-05-31 16:33:02 +03:00
|
|
|
|
return !empty($xmlOffers) && $writingOffers < $this->setup->maxOffersValue;
|
|
|
|
|
}
|
|
|
|
|
}
|