@@ -93,32 +93,80 @@ public function execute()
93
93
->join (
94
94
['sop ' => $ this ->getSalesOrderPaymentTableName ()],
95
95
'main_table.entity_id = sop.parent_id ' ,
96
- ['method ' ]
96
+ ['method ' , ' additional_information ' ]
97
97
)
98
98
->where (new \Zend_Db_Expr (
99
99
"sop.method = ? AND TIME_TO_SEC(TIMEDIFF(CURRENT_TIMESTAMP, CAST(JSON_EXTRACT(sop.additional_information, '$.date_of_expiration') AS DATETIME))) >= 0 "
100
100
), ConfigCheckoutPro::METHOD );
101
101
102
- foreach ($ orders as $ order ) {
103
- $ orderId = $ order ->getEntityId ();
104
- $ amount = $ order ->getTotalDue ();
105
- $ baseAmount = $ order ->getBaseTotalDue ();
106
- $ payment = $ order ->getPayment ();
107
- $ payment ->setPreparedMessage (__ ('Order Canceled. ' ));
108
- $ payment ->registerVoidNotification ($ amount );
109
- $ payment ->setIsTransactionApproved (false );
110
- $ payment ->setIsTransactionDenied (true );
111
- $ payment ->setIsTransactionPending (false );
112
- $ payment ->setIsInProcess (true );
113
- $ payment ->setIsTransactionClosed (true );
114
- $ payment ->setShouldCloseParentTransaction (true );
115
- $ payment ->setAmountCanceled ($ amount );
116
- $ payment ->setBaseAmountCanceled ($ baseAmount );
117
- $ order ->cancel ();
118
- $ order ->save ();
102
+
103
+ $ this ->logger ->debug ([
104
+ 'Total orders to cancel: ' => $ orders ->count ()
105
+ ]);
106
+
107
+ $ dateRange = [];
108
+ $ orderId = null ;
109
+
110
+ try {
111
+ $ this ->logger ->debug ([
112
+ 'Cancel Start '
113
+ ]);
114
+ foreach ($ orders as $ order ) {
115
+ $ orderAdditionalInformation = json_decode ($ order ->getData ('additional_information ' ));
116
+ $ orderId = $ order ->getEntityId ();
117
+ $ amount = $ order ->getTotalDue ();
118
+ $ baseAmount = $ order ->getBaseTotalDue ();
119
+ $ payment = $ order ->getPayment ();
120
+ $ payment ->setPreparedMessage (__ ('Order Canceled. ' ));
121
+ $ payment ->registerVoidNotification ($ amount );
122
+ $ payment ->setIsTransactionApproved (false );
123
+ $ payment ->setIsTransactionDenied (true );
124
+ $ payment ->setIsTransactionPending (false );
125
+ $ payment ->setIsInProcess (true );
126
+ $ payment ->setIsTransactionClosed (true );
127
+ $ payment ->setShouldCloseParentTransaction (true );
128
+ $ payment ->setAmountCanceled ($ amount );
129
+ $ payment ->setBaseAmountCanceled ($ baseAmount );
130
+ $ order ->cancel ();
131
+ $ order ->save ();
132
+
133
+ $ dateRange [] = $ orderAdditionalInformation ->date_of_expiration ;
134
+ $ this ->logger ->debug ([
135
+ 'fetch ' => 'Cancel Order Id ' . $ orderId . ' successfully ' ,
136
+ 'order_date_of_expiration ' => $ orderAdditionalInformation ->date_of_expiration
137
+ ]);
138
+ }
139
+
140
+ $ this ->calculateExpirationRange ($ dateRange );
141
+ $ this ->logger ->debug ([
142
+ 'Cancel Finish '
143
+ ]);
144
+
145
+ } catch (\Exception $ e ) {
119
146
$ this ->logger ->debug ([
120
- 'fetch ' => 'Cancel Order Id ' . $ orderId ,
147
+ 'Cancel order execution error ' => $ e ->getMessage (),
148
+ 'Order Id ' => $ orderId
121
149
]);
122
150
}
123
151
}
152
+
153
+ /**
154
+ * Calculate expiration range date
155
+ *
156
+ * @param array $dateRange
157
+ * @return void
158
+ */
159
+ private function calculateExpirationRange (array $ dateRange ): void
160
+ {
161
+ usort ($ dateRange , function ($ a , $ b ) {
162
+ return strtotime ($ a ) - strtotime ($ b );
163
+ });
164
+
165
+ $ initialDate = reset ($ dateRange );
166
+ $ endDate = end ($ dateRange );
167
+
168
+ $ this ->logger ->debug ([
169
+ 'Orders expiration range: ' => "from: $ initialDate to: $ endDate "
170
+ ]);
171
+ }
124
172
}
0 commit comments