33 lines
1.0 KiB
PHP
33 lines
1.0 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Esun\Customer\Model\Order;
|
|
|
|
use Magento\Framework\Exception\LocalizedException;
|
|
use Magento\Sales\Api\OrderRepositoryInterface;
|
|
|
|
class ReceiveOrderService
|
|
{
|
|
public function __construct(private readonly OrderRepositoryInterface $orders)
|
|
{
|
|
}
|
|
|
|
public function receive(int $orderId, int $customerId): void
|
|
{
|
|
$order = $this->orders->get($orderId);
|
|
if ((int)$order->getCustomerId() !== $customerId) {
|
|
throw new LocalizedException(__('The order does not exist.'));
|
|
}
|
|
if ((string)$order->getStatus() === 'received') {
|
|
return;
|
|
}
|
|
if ((string)$order->getStatus() !== 'shipped') {
|
|
throw new LocalizedException(__('Only shipped orders can be confirmed as received.'));
|
|
}
|
|
$order->setStatus('received');
|
|
$order->setData('customer_confirmed_at', gmdate('Y-m-d H:i:s'));
|
|
$order->addCommentToStatusHistory(__('The customer confirmed receipt.'), 'received', false);
|
|
$this->orders->save($order);
|
|
}
|
|
}
|