@@ -18,102 +18,84 @@ class State
18
18
*
19
19
* @param Order $order
20
20
* @return $this
21
- * @SuppressWarnings(PHPMD.CyclomaticComplexity)
22
- * @SuppressWarnings(PHPMD.NPathComplexity)
23
21
*/
24
22
public function check (Order $ order )
25
23
{
24
+ if ($ order ->isCanceled () && $ order ->canUnhold () && $ order ->canInvoice ()) {
25
+ return $ this ;
26
+ }
27
+
26
28
$ currentState = $ order ->getState ();
27
- if ($ currentState == Order:: STATE_NEW && $ order -> getIsInProcess ( )) {
29
+ if ($ this -> canBeProcessingStatus ( $ order , $ currentState )) {
28
30
$ order ->setState (Order::STATE_PROCESSING )
29
31
->setStatus ($ order ->getConfig ()->getStateDefaultStatus (Order::STATE_PROCESSING ));
30
32
$ currentState = Order::STATE_PROCESSING ;
31
33
}
32
34
33
- if (!$ order ->isCanceled () && !$ order ->canUnhold () && !$ order ->canInvoice ()) {
34
- if (in_array ($ currentState , [Order::STATE_PROCESSING , Order::STATE_COMPLETE ])
35
- && !$ order ->canCreditmemo ()
36
- && !$ order ->canShip ()
37
- && $ order ->getIsNotVirtual ()
38
- ) {
39
- $ order ->setState (Order::STATE_CLOSED )
40
- ->setStatus ($ order ->getConfig ()->getStateDefaultStatus (Order::STATE_CLOSED ));
41
- } elseif ($ currentState === Order::STATE_PROCESSING
42
- && (!$ order ->canShip () ||
43
- ($ this ->isPartiallyRefundedOrderShipped ($ order ) && !$ this ->hasPendingShipmentItems ($ order )))
44
- ) {
45
- $ order ->setState (Order::STATE_COMPLETE )
46
- ->setStatus ($ order ->getConfig ()->getStateDefaultStatus (Order::STATE_COMPLETE ));
47
- } elseif ($ order ->getIsVirtual () && $ order ->getStatus () === Order::STATE_CLOSED ) {
48
- $ order ->setState (Order::STATE_CLOSED );
49
- }
35
+ if ($ this ->canBeClosedStatus ($ order , $ currentState )) {
36
+ $ order ->setState (Order::STATE_CLOSED )
37
+ ->setStatus ($ order ->getConfig ()->getStateDefaultStatus (Order::STATE_CLOSED ));
38
+ return $ this ;
39
+ }
40
+
41
+ if ($ this ->canBeCompleteStatus ($ order , $ currentState )) {
42
+ $ order ->setState (Order::STATE_COMPLETE )
43
+ ->setStatus ($ order ->getConfig ()->getStateDefaultStatus (Order::STATE_COMPLETE ));
44
+ return $ this ;
50
45
}
46
+
51
47
return $ this ;
52
48
}
53
49
54
50
/**
55
- * Check if all items are remaining items after partially refunded are shipped
51
+ * Check if order can be automatically switched to complete status
56
52
*
57
53
* @param Order $order
54
+ * @param string $currentState
58
55
* @return bool
59
56
*/
60
- public function isPartiallyRefundedOrderShipped (Order $ order ): bool
57
+ private function canBeCompleteStatus (Order $ order, string $ currentState ): bool
61
58
{
62
- $ isPartiallyRefundedOrderShipped = false ;
63
- if ($ this ->getShippedItems ($ order ) > 0
64
- && $ order ->getTotalQtyOrdered () <= $ this ->getRefundedItems ($ order ) + $ this ->getShippedItems ($ order )) {
65
- $ isPartiallyRefundedOrderShipped = true ;
59
+ if ($ currentState === Order::STATE_PROCESSING && !$ order ->canShip ()) {
60
+ return true ;
66
61
}
67
62
68
- return $ isPartiallyRefundedOrderShipped ;
63
+ return false ;
69
64
}
70
65
71
66
/**
72
- * Check if order has items that haven't been shipped yet
67
+ * Check if order can be automatically switched to closed status
73
68
*
74
69
* @param Order $order
70
+ * @param string $currentState
75
71
* @return bool
76
72
*/
77
- private function hasPendingShipmentItems (Order $ order ): bool
73
+ private function canBeClosedStatus (Order $ order, string $ currentState ): bool
78
74
{
79
- foreach ($ order ->getAllItems () as $ item ) {
80
- if ($ item ->canShip ()) {
81
- return true ;
82
- }
75
+ if (in_array ($ currentState , [Order::STATE_PROCESSING , Order::STATE_COMPLETE ])
76
+ && !$ order ->canCreditmemo ()
77
+ && !$ order ->canShip ()
78
+ && $ order ->getIsNotVirtual ()
79
+ ) {
80
+ return true ;
83
81
}
84
82
85
83
return false ;
86
84
}
87
85
88
86
/**
89
- * Get all refunded items number
87
+ * Check if order can be automatically switched to processing status
90
88
*
91
89
* @param Order $order
92
- * @return int
90
+ * @param string $currentState
91
+ * @return bool
93
92
*/
94
- private function getRefundedItems (Order $ order ): int
93
+ private function canBeProcessingStatus (Order $ order, string $ currentState ): bool
95
94
{
96
- $ numOfRefundedItems = 0 ;
97
- foreach ($ order ->getAllItems () as $ item ) {
98
- if ($ item ->getProductType () == 'simple ' ) {
99
- $ numOfRefundedItems += (int )$ item ->getQtyRefunded ();
100
- }
95
+ if ($ currentState == Order::STATE_NEW && $ order ->getIsInProcess ()) {
96
+ return true ;
101
97
}
102
- return $ numOfRefundedItems ;
103
- }
104
98
105
- /**
106
- * Get all shipped items number
107
- *
108
- * @param Order $order
109
- * @return int
110
- */
111
- private function getShippedItems (Order $ order ): int
112
- {
113
- $ numOfShippedItems = 0 ;
114
- foreach ($ order ->getAllItems () as $ item ) {
115
- $ numOfShippedItems += (int )$ item ->getQtyShipped ();
116
- }
117
- return $ numOfShippedItems ;
99
+ return false ;
118
100
}
119
101
}
0 commit comments