1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sylius package. |
5
|
|
|
* |
6
|
|
|
* (c) Paweł Jędrzejewski |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sylius\Component\Core\OrderProcessing; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Collections\Collection; |
15
|
|
|
use SM\Factory\FactoryInterface as StateMachineFactoryInteraface; |
16
|
|
|
use Sylius\Bundle\OrderBundle\Factory\OrderItemUnitFactoryInterface; |
17
|
|
|
use Sylius\Component\Core\Model\OrderInterface; |
18
|
|
|
use Sylius\Component\Core\Model\OrderItemInterface; |
19
|
|
|
use Sylius\Component\Core\Model\OrderItemUnitInterface; |
20
|
|
|
use Sylius\Component\Inventory\InventoryUnitTransitions; |
21
|
|
|
use Sylius\Component\Inventory\Model\InventoryUnitInterface; |
22
|
|
|
use Sylius\Component\Inventory\Operator\InventoryOperatorInterface; |
23
|
|
|
use Sylius\Component\Resource\Factory\FactoryInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
27
|
|
|
* @author Saša Stamenković <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class InventoryHandler implements InventoryHandlerInterface |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var InventoryOperatorInterface |
33
|
|
|
*/ |
34
|
|
|
protected $inventoryOperator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var OrderItemUnitFactoryInterface |
38
|
|
|
*/ |
39
|
|
|
protected $orderItemUnitFactory; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @var StateMachineFactoryInteraface |
43
|
|
|
*/ |
44
|
|
|
protected $stateMachineFactory; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param InventoryOperatorInterface $inventoryOperator |
48
|
|
|
* @param OrderItemUnitFactoryInterface $orderItemUnitFactory |
49
|
|
|
* @param StateMachineFactoryInteraface $stateMachineFactory |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
InventoryOperatorInterface $inventoryOperator, |
53
|
|
|
OrderItemUnitFactoryInterface $orderItemUnitFactory, |
54
|
|
|
StateMachineFactoryInteraface $stateMachineFactory |
55
|
|
|
) { |
56
|
|
|
$this->inventoryOperator = $inventoryOperator; |
57
|
|
|
$this->orderItemUnitFactory = $orderItemUnitFactory; |
58
|
|
|
$this->stateMachineFactory = $stateMachineFactory; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function holdInventory(OrderInterface $order) |
65
|
|
|
{ |
66
|
|
|
foreach ($order->getItems() as $item) { |
67
|
|
|
$quantity = $this->applyTransition($item->getUnits(), InventoryUnitTransitions::SYLIUS_HOLD); |
68
|
|
|
|
69
|
|
|
$this->inventoryOperator->hold($item->getVariant(), $quantity); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* {@inheritdoc} |
75
|
|
|
*/ |
76
|
|
|
public function releaseInventory(OrderInterface $order) |
77
|
|
|
{ |
78
|
|
|
foreach ($order->getItems() as $item) { |
79
|
|
|
$quantity = $this->applyTransition($item->getUnits(), InventoryUnitTransitions::SYLIUS_RELEASE); |
80
|
|
|
|
81
|
|
|
$this->inventoryOperator->release($item->getVariant(), $quantity); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function updateInventory(OrderInterface $order) |
89
|
|
|
{ |
90
|
|
|
foreach ($order->getItems() as $item) { |
91
|
|
|
$units = $item->getUnits(); |
92
|
|
|
$quantity = 0; |
93
|
|
|
|
94
|
|
|
foreach ($units as $unit) { |
95
|
|
|
$stateMachine = $this->stateMachineFactory->get($unit, InventoryUnitTransitions::GRAPH); |
96
|
|
|
if ($stateMachine->can(InventoryUnitTransitions::SYLIUS_SELL)) { |
97
|
|
|
if ($stateMachine->can(InventoryUnitTransitions::SYLIUS_RELEASE)) { |
98
|
|
|
$quantity++; |
99
|
|
|
} |
100
|
|
|
$stateMachine->apply(InventoryUnitTransitions::SYLIUS_SELL); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$this->inventoryOperator->release($item->getVariant(), $quantity); |
105
|
|
|
$this->inventoryOperator->decrease($units); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Apply and count a transition on all given units |
111
|
|
|
* |
112
|
|
|
* @param Collection $units |
113
|
|
|
* @param string $transition |
114
|
|
|
* |
115
|
|
|
* @return int |
116
|
|
|
*/ |
117
|
|
|
protected function applyTransition(Collection $units, $transition) |
118
|
|
|
{ |
119
|
|
|
$quantity = 0; |
120
|
|
|
|
121
|
|
|
foreach ($units as $unit) { |
122
|
|
|
$stateMachine = $this->stateMachineFactory->get($unit, InventoryUnitTransitions::GRAPH); |
123
|
|
|
if ($stateMachine->can($transition)) { |
124
|
|
|
$stateMachine->apply($transition); |
125
|
|
|
$quantity++; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $quantity; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|