1
0
mirror of synced 2024-12-12 07:06:04 +03:00
bitrix-module/intaro.retailcrm/lib/repository/catalogrepository.php

202 lines
5.6 KiB
PHP
Raw Permalink Normal View History

2021-05-31 16:33:02 +03:00
<?php
/**
* @category Integration
* @package Intaro\RetailCrm\Repository
* @author RetailCRM <integration@retailcrm.ru>
* @license MIT
* @link http://retailcrm.ru
* @see http://retailcrm.ru/docs
*/
2021-05-31 16:33:02 +03:00
namespace Intaro\RetailCrm\Repository;
2021-09-08 16:31:17 +03:00
use Bitrix\Catalog\StoreBarcodeTable;
2021-05-31 16:33:02 +03:00
use Bitrix\Iblock\IblockTable;
2021-09-08 16:31:17 +03:00
use Bitrix\Iblock\SectionElementTable;
2021-05-31 16:33:02 +03:00
use Bitrix\Iblock\SectionTable;
use Bitrix\Main\ArgumentException;
use Bitrix\Main\ObjectPropertyException;
use Bitrix\Main\ORM\Objectify\Collection;
use Bitrix\Main\ORM\Objectify\EntityObject;
use Bitrix\Main\SystemException;
use CCatalogGroup;
use CCatalogSku;
use CCatalogStoreBarCode;
use CIBlockElement;
use Intaro\RetailCrm\Icml\QueryParamsMolder;
use Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo;
use Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams;
use RetailcrmConfigProvider;
/**
* Class CatalogRepository
* @package Intaro\RetailCrm\Repository
*/
class CatalogRepository
{
/**
* @var \Intaro\RetailCrm\Icml\QueryParamsMolder
*/
private $builder;
2021-09-08 16:31:17 +03:00
/**
* @var bool
*/
private $isLoadNotActive = false;
2021-05-31 16:33:02 +03:00
/**
* CatalogRepository constructor.
*/
public function __construct()
{
$this->builder = new QueryParamsMolder();
}
2021-09-08 16:31:17 +03:00
public function setLoadNotActive(bool $isLoad)
{
$this->isLoadNotActive = $isLoad;
}
2021-05-31 16:33:02 +03:00
/**
* Получение категорий, к которым относится товар
*
* @param int $offerId
* @return array
*/
2021-09-08 16:31:17 +03:00
public function getProductCategories(int $offerId): array
2021-05-31 16:33:02 +03:00
{
2021-09-08 16:31:17 +03:00
try {
$categories = SectionElementTable::query()
->addSelect('IBLOCK_SECTION.ID')
->addSelect('IBLOCK_SECTION.CODE')
->where('IBLOCK_ELEMENT_ID', $offerId)
->fetchAll();
} catch (ObjectPropertyException | ArgumentException | SystemException $exception) {
return [];
2021-05-31 16:33:02 +03:00
}
2021-09-08 16:31:17 +03:00
return $categories;
2021-05-31 16:33:02 +03:00
}
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
/**
* Returns products IDs with barcodes by infoblock id
*
* @return array
*/
2021-09-08 16:31:17 +03:00
public function getBarcodes(): array
2021-05-31 16:33:02 +03:00
{
$barcodes = [];
2021-09-08 16:31:17 +03:00
try {
$arBarCodes = StoreBarcodeTable::query()
->addSelect('PRODUCT_ID')
->addSelect('BARCODE')
->fetchAll();
} catch (ObjectPropertyException | ArgumentException | SystemException $exception) {
return [];
2021-05-31 16:33:02 +03:00
}
2021-09-08 16:31:17 +03:00
foreach ($arBarCodes as $arBarCode){
$barcodes[$arBarCode['PRODUCT_ID']] = $arBarCode['BARCODE'];
}
2021-05-31 16:33:02 +03:00
return $barcodes;
}
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
/**
* @param \Intaro\RetailCrm\Model\Bitrix\Xml\SelectParams $param
* @param \Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo $catalogIblockInfo
* @return \CIBlockResult|int
*/
public function getProductPage(SelectParams $param, CatalogIblockInfo $catalogIblockInfo)
2021-05-31 16:33:02 +03:00
{
return CIBlockElement::GetList(
['ID' => 'ASC'],
$this->builder->getWhereForOfferPart($param->parentId, $catalogIblockInfo, $this->isLoadNotActive),
2021-05-31 16:33:02 +03:00
false,
['nPageSize' => $param->nPageSize, 'iNumPage' => $param->pageNumber, 'checkOutOfRange' => true],
2021-09-08 16:31:17 +03:00
$param->allParams
2021-05-31 16:33:02 +03:00
);
}
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
/**
* @param int $iblockId
* @return \Bitrix\Main\ORM\Objectify\Collection|null
*/
public function getCategoriesByIblockId(int $iblockId): ?Collection
{
try {
return SectionTable::query()
->addSelect('*')
->where('IBLOCK_ID', $iblockId)
->fetchCollection();
} catch (ObjectPropertyException | ArgumentException | SystemException $exception) {
return null;
}
}
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
/**
* @param $iblockId
* @return EntityObject|null
*/
public function getIblockById($iblockId): ?EntityObject
{
try {
return IblockTable::query()
->where('ID', $iblockId)
->fetchObject();
} catch (ObjectPropertyException | ArgumentException | SystemException $exception) {
return null;
}
}
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
/**
* Возвращает информацию об инфоблоке торговых предложений по ID инфоблока товаров
*
* @param int $productIblockId
* @return \Intaro\RetailCrm\Model\Bitrix\Orm\CatalogIblockInfo
*/
public function getCatalogIblockInfo(int $productIblockId): CatalogIblockInfo
{
$catalogIblockInfo = new CatalogIblockInfo();
$info = CCatalogSKU::GetInfoByProductIBlock($productIblockId);
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
if ($info === false) {
$catalogIblockInfo->productIblockId = $productIblockId;
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
return $catalogIblockInfo;
}
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
$catalogIblockInfo->skuIblockId = $info['IBLOCK_ID'];
$catalogIblockInfo->productIblockId = $info['PRODUCT_IBLOCK_ID'];
$catalogIblockInfo->skuPropertyId = $info['SKU_PROPERTY_ID'];
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
return $catalogIblockInfo;
}
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
/**
* @param int|null $profileId
* @return int
*/
public static function getBasePriceId(?int $profileId): int
{
$basePriceId = RetailcrmConfigProvider::getCatalogBasePriceByProfile($profileId);
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
if (!$basePriceId) {
$dbPriceType = CCatalogGroup::GetList(
[],
['BASE' => 'Y'],
false,
false,
['ID']
);
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
$result = $dbPriceType->GetNext();
return $result['ID'];
}
2021-09-08 16:31:17 +03:00
2021-05-31 16:33:02 +03:00
return $basePriceId;
}
}