1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file has been created by developers from BitBag. |
5
|
|
|
* Feel free to contact us once you face any issues or want to start |
6
|
|
|
* You can find more information about us on https://bitbag.io and write us |
7
|
|
|
* an email on [email protected]. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace BitBag\SyliusMolliePlugin\Checker\Refund; |
13
|
|
|
|
14
|
|
|
use BitBag\SyliusMolliePlugin\Entity\OrderInterface; |
15
|
|
|
use BitBag\SyliusMolliePlugin\Repository\CreditMemoRepositoryInterface; |
16
|
|
|
use BitBag\SyliusMolliePlugin\Repository\OrderRepositoryInterface; |
17
|
|
|
use Sylius\RefundPlugin\Command\RefundUnits; |
18
|
|
|
|
19
|
|
|
final class DuplicateRefundTheSameAmountChecker implements DuplicateRefundTheSameAmountCheckerInterface |
20
|
|
|
{ |
21
|
|
|
/** @var CreditMemoRepositoryInterface */ |
22
|
|
|
private $creditMemoRepository; |
23
|
|
|
|
24
|
|
|
/** @var OrderRepositoryInterface */ |
25
|
|
|
private $orderRepository; |
26
|
|
|
|
27
|
|
|
public function __construct( |
28
|
|
|
CreditMemoRepositoryInterface $creditMemoRepository, |
29
|
|
|
OrderRepositoryInterface $orderRepository |
30
|
|
|
) { |
31
|
|
|
$this->creditMemoRepository = $creditMemoRepository; |
32
|
|
|
$this->orderRepository = $orderRepository; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function check(RefundUnits $command): bool |
36
|
|
|
{ |
37
|
|
|
$dateTimeInterval = new \DateInterval(self::ONE_HOUR_INTERVAL); |
38
|
|
|
$now = new \DateTime('now'); |
39
|
|
|
$now->sub($dateTimeInterval); |
40
|
|
|
|
41
|
|
|
/** @var OrderInterface $order */ |
42
|
|
|
$order = $this->orderRepository->findOneBy(['number' => $command->orderNumber()]); |
43
|
|
|
|
44
|
|
|
$creditMemos = $this->creditMemoRepository->findByOrderNumberAndDateTime( |
45
|
|
|
$order->getId(), |
46
|
|
|
$now, |
47
|
|
|
$this->getTotalAmount($command) |
48
|
|
|
); |
49
|
|
|
|
50
|
|
|
return !empty($creditMemos); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
private function getTotalAmount(RefundUnits $command): int |
54
|
|
|
{ |
55
|
|
|
$total = 0; |
56
|
|
|
|
57
|
|
|
foreach ($command->units() as $unit) { |
58
|
|
|
$total += $unit->total(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
foreach ($command->shipments() as $shipment) { |
62
|
|
|
$total += $shipment->total(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $total; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|