2022-03-02 15:40:53 +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
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Intaro\RetailCrm\Repository;
|
|
|
|
|
|
|
|
use Bitrix\Main\ArgumentException;
|
|
|
|
use Bitrix\Main\ObjectPropertyException;
|
|
|
|
use Bitrix\Main\SystemException;
|
|
|
|
use Exception;
|
|
|
|
use Intaro\RetailCrm\Component\Constants;
|
|
|
|
use Intaro\RetailCrm\Component\Json\Deserializer;
|
|
|
|
use Intaro\RetailCrm\Component\Json\Serializer;
|
|
|
|
use Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData;
|
|
|
|
use Intaro\RetailCrm\Service\Utils;
|
|
|
|
use Logger;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class OrderLoyaltyDataRepository
|
|
|
|
*
|
|
|
|
* @package Intaro\RetailCrm\Repository
|
|
|
|
*/
|
|
|
|
class OrderLoyaltyDataRepository extends AbstractRepository
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \Bitrix\Main\Entity\DataManager|string|null
|
|
|
|
*/
|
|
|
|
private $dataManager;
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
/**
|
|
|
|
* @var \Logger
|
|
|
|
*/
|
|
|
|
private $logger;
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
/**
|
|
|
|
* OrderLoyaltyDataRepository constructor.
|
|
|
|
* @throws \Bitrix\Main\LoaderException
|
|
|
|
* @throws \Bitrix\Main\SystemException
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
$this->logger = Logger::getInstance();
|
|
|
|
$this->dataManager = Utils::getHlClassByName(Constants::HL_LOYALTY_CODE);
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
/**
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData $loyaltyHl
|
|
|
|
* @return int|null
|
|
|
|
*/
|
|
|
|
public function add(OrderLoyaltyData $loyaltyHl): ?int
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if ($this->dataManager === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
$result = Serializer::serializeArray($loyaltyHl, OrderLoyaltyData::class);
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
unset($result['ID']);
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
$result = $this->dataManager::add($result);
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
if ($result->isSuccess()) {
|
|
|
|
return $result->getId();
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
return null;
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
return null;
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
/**
|
|
|
|
* @param int $positionId
|
|
|
|
* @return \Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData|null
|
|
|
|
*/
|
|
|
|
public function getOrderLpDataByPosition(int $positionId): ?OrderLoyaltyData
|
|
|
|
{
|
|
|
|
if ($this->dataManager === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
try {
|
|
|
|
$product = $this->dataManager::query()
|
|
|
|
->setSelect(['*'])
|
|
|
|
->where('UF_ITEM_POS_ID', '=', $positionId)
|
|
|
|
->fetch();
|
2022-06-28 15:28:28 +03:00
|
|
|
|
|
|
|
if (false === $product) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
return Deserializer::deserializeArray($product, OrderLoyaltyData::class);
|
|
|
|
} catch (ObjectPropertyException | ArgumentException | SystemException $exception) {
|
|
|
|
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
|
|
|
|
}
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
/**
|
|
|
|
* @param $orderId
|
|
|
|
*
|
|
|
|
* @return OrderLoyaltyData[]
|
|
|
|
*/
|
|
|
|
public function getProductsByOrderId($orderId): array
|
|
|
|
{
|
|
|
|
$products = $this->getHlRowByOrderId($orderId);
|
|
|
|
|
|
|
|
if (count($products) === 0 || false === $products) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = [];
|
|
|
|
|
|
|
|
foreach ($products as $product) {
|
|
|
|
$result[$product['UF_ITEM_POS_ID']]
|
|
|
|
= Deserializer::deserializeArray($product, OrderLoyaltyData::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param int $orderId
|
|
|
|
*
|
|
|
|
* @return array|false //fetchAll может вернуть false
|
|
|
|
*/
|
|
|
|
private function getHlRowByOrderId(int $orderId)
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if ($this->dataManager === null) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->dataManager::query()
|
|
|
|
->setSelect(['*'])
|
|
|
|
->where('UF_ORDER_ID', '=', $orderId)
|
|
|
|
->fetchAll();
|
|
|
|
|
|
|
|
} catch (SystemException | Exception $exception) {
|
|
|
|
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param \Intaro\RetailCrm\Model\Bitrix\OrderLoyaltyData $position
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function edit(OrderLoyaltyData $position): bool
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if ($this->dataManager === null) {
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
$productAr = Serializer::serializeArray($position, OrderLoyaltyData::class);
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
unset($productAr['ID']);
|
2022-06-28 15:28:28 +03:00
|
|
|
|
|
|
|
foreach ($productAr as $key => $value) {
|
|
|
|
if (null === $value) {
|
|
|
|
unset($productAr[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
$result = $this->dataManager::update($position->id, $productAr);
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
if ($result->isSuccess()) {
|
|
|
|
return true;
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
return false;
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param mixed $primary
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function delete($primary): bool
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if ($this->dataManager === null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = $this->dataManager::delete($primary);
|
|
|
|
|
|
|
|
if ($result->isSuccess()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
/**
|
|
|
|
* @param int $externalId
|
|
|
|
* @return float|null
|
|
|
|
*/
|
|
|
|
public function getDefDiscountByProductPosition(int $externalId): ?float
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if ($this->dataManager === null) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
$result = $this->dataManager::query()
|
|
|
|
->setSelect(['UF_DEF_DISCOUNT'])
|
|
|
|
->where([
|
|
|
|
['UF_ITEM_POS_ID', '=', $externalId]
|
|
|
|
])
|
2022-06-28 15:28:28 +03:00
|
|
|
->fetch();
|
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
if ($result !== false) {
|
|
|
|
return (float) $result['UF_DEF_DISCOUNT'];
|
|
|
|
}
|
|
|
|
} catch (SystemException | Exception $exception) {
|
|
|
|
$this->logger->write($exception->getMessage(), Constants::LOYALTY_ERROR);
|
|
|
|
}
|
2022-06-28 15:28:28 +03:00
|
|
|
|
2022-03-02 15:40:53 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|