Files
2025-08-02 16:30:27 +02:00

141 lines
4.2 KiB
PHP

<?php
declare(strict_types=1);
namespace External\HannahBundle\SAP\Util;
use External\HannahBundle\SAP\Exception\SAPException;
use External\HannahBundle\SAP\MappingType;
use External\HannahBundle\SAP\Util\DataFactory\OrderDataFactory;
use External\HannahBundle\SAP\Util\DataFactory\ReturnDataFactory;
use External\HannahBundle\Util\OrderUtil;
use KupShop\KupShopBundle\Util\Logging\SentryLogger;
use KupShop\ReturnsBundle\Entity\ReturnEntity;
class SAPApi
{
public function __construct(
private SAPClient $client,
private SAPUtil $sapUtil,
private OrderDataFactory $orderDataFactory,
private OrderUtil $orderUtil,
private ReturnDataFactory $returnDataFactory,
private SentryLogger $sentryLogger,
) {
}
public function createReturn(ReturnEntity $return, ?\Order $order = null, bool $throwException = false): ?string
{
try {
$result = $this->client->returnCreate(
$this->returnDataFactory->getData(['return' => $return]),
$order ? $this->orderUtil->getOrderProcess($order) : null,
);
if (!$result->SapReturnOrderId) {
throw new SAPException('Missing \'SapReturnOrderId\' in result object!');
}
$this->sapUtil->createMapping(
MappingType::RETURNS,
$result->SapReturnOrderId,
(int) $return->getId()
);
return (string) $result->SapReturnOrderId;
} catch (SAPException $e) {
if ($throwException) {
throw $e;
}
$this->sentryLogger->captureException($e);
}
return null;
}
public function createOrder(\Order $order, bool $throwException = false): ?string
{
try {
$result = $this->client->orderCreate(
$this->orderDataFactory->getData(['order' => $order]),
$this->orderUtil->getOrderProcess($order)
);
if (!$result->SapOrderId) {
throw new SAPException('Missing \'SapOrderId\' in result object!');
}
$this->sapUtil->createMapping(
MappingType::ORDERS,
$result->SapOrderId,
(int) $order->id
);
return (string) $result->SapOrderId;
} catch (SAPException $e) {
if ($throwException) {
throw $e;
}
$this->sentryLogger->captureException($e);
}
return null;
}
public function updateOrder(\Order $order, bool $throwException = false): ?string
{
// pokud se objednavka uz nachazi v SAPu, tak ji smazu
if ($this->sapUtil->getSapId(MappingType::ORDERS, (int) $order->id)) {
$this->client->orderCancel(
$order->order_no,
processName: $this->orderUtil->getOrderProcess($order)
);
}
// smaznu mapping
$this->sapUtil->deleteMapping(MappingType::ORDERS, (int) $order->id);
// a ted objednavku znovu zapisu do SAPu
return $this->createOrder($order, $throwException);
}
public function activateCoupons(string $sapId, array $coupons, bool $throwErrors = false): void
{
foreach ($coupons as $coupon) {
try {
$this->client->couponChangeState($coupon, SAPClient::COUPON_STATE_SOLD, $sapId);
} catch (\Throwable $e) {
if (isLocalDevelopment() || $throwErrors) {
throw $e;
}
}
}
}
public function updateOrderPayment(\Order $order): bool
{
try {
if ($this->client->orderPayment($order, processName: $this->orderUtil->getOrderProcess($order))) {
$this->sapUtil->setMappingData(
MappingType::ORDERS,
(int) $order->id,
['paidSent' => true]
);
}
} catch (\Throwable $e) {
$this->sentryLogger->captureException($e);
return false;
}
return true;
}
public function getClient(): SAPClient
{
return $this->client;
}
}