Conditions | 13 |
Paths | 17 |
Total Lines | 94 |
Code Lines | 63 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
215 | protected function workoutActualQuantity() |
||
216 | { |
||
217 | $actualQuantity = 0; |
||
218 | if ($buyable = $this->getBuyable()) { |
||
219 | $query = Order::get() |
||
220 | ->where(' |
||
221 | "OrderItem"."BuyableID" = '.(intval($this->BuyableID) - 0).' |
||
222 | AND |
||
223 | "OrderItem"."BuyableClassName" = \''.$this->BuyableClassName.'\' |
||
224 | AND |
||
225 | "OrderStep"."CustomerCanEdit" = 0 |
||
226 | AND |
||
227 | "Order"."ID" <> '.ShoppingCart::current_order()->ID.' |
||
228 | ') |
||
229 | ->innerJoin('OrderAttribute', '"OrderAttribute"."OrderID" = "Order"."ID"') |
||
230 | ->innerJoin('OrderItem', '"OrderAttribute"."ID" = "OrderItem"."ID"') |
||
231 | ->innerJoin('OrderStep', '"OrderStep"."ID" = "Order"."StatusID"'); |
||
232 | $amountPerOrder = array(); |
||
233 | if($query->count()) { |
||
234 | foreach ($query as $row) { |
||
235 | if(!isset($amountPerOrder[$row->OrderID])) { |
||
236 | $amountPerOrder[$row->OrderID] = 0; |
||
237 | } |
||
238 | $amountPerOrder[$row->OrderID] += $row->Quantity; |
||
239 | } |
||
240 | foreach($amountPerOrder as $orderID => $sum) { |
||
241 | if ($orderID && $sum) { |
||
242 | $buyableStockOrderEntry = BuyableStockOrderEntry::get() |
||
243 | ->filter( |
||
244 | array( |
||
245 | 'OrderID' => $orderID, |
||
246 | 'ParentID' => $this->ID |
||
247 | ) |
||
248 | ) |
||
249 | ->First(); |
||
250 | if ($buyableStockOrderEntry) { |
||
251 | //do nothing |
||
252 | } else { |
||
253 | $buyableStockOrderEntry = new BuyableStockOrderEntry(); |
||
254 | $buyableStockOrderEntry->OrderID = $orderID; |
||
255 | $buyableStockOrderEntry->ParentID = $this->ID; |
||
256 | $buyableStockOrderEntry->IncludeInCurrentCalculation = 1; |
||
257 | $buyableStockOrderEntry->Quantity = 0; |
||
258 | } |
||
259 | if ($buyableStockOrderEntry->Quantity != $sum) { |
||
260 | $buyableStockOrderEntry->Quantity = $sum; |
||
261 | $buyableStockOrderEntry->write(); |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | } |
||
266 | //find last adjustment |
||
267 | $latestManualUpdate = BuyableStockManualUpdate::get() |
||
268 | ->filter(array('ParentID' => $this->ID)) |
||
269 | ->sort(array('LastEdited' => 'DESC')) |
||
270 | ->First(); |
||
271 | //nullify order quantities that were entered before last adjustment |
||
272 | if ($latestManualUpdate) { |
||
273 | $latestManualUpdateQuantity = $latestManualUpdate->Quantity; |
||
274 | DB::query(" |
||
275 | UPDATE \"BuyableStockOrderEntry\" |
||
276 | SET \"IncludeInCurrentCalculation\" = 0 |
||
277 | WHERE |
||
278 | \"LastEdited\" < '".$latestManualUpdate->LastEdited."' |
||
279 | AND |
||
280 | \"ParentID\" = ".$this->ID |
||
281 | ); |
||
282 | } else { |
||
283 | $latestManualUpdateQuantity = 0; |
||
284 | } |
||
285 | //work out additional purchases |
||
286 | $orderQuantityToDeduct = BuyableStockOrderEntry::get() |
||
287 | ->filter( |
||
288 | array( |
||
289 | 'ParentID' => $this->ID, |
||
290 | 'IncludeInCurrentCalculation' => 1 |
||
291 | ) |
||
292 | )->sum('Quantity'); |
||
293 | if (!$orderQuantityToDeduct) { |
||
294 | $orderQuantityToDeduct = 0; |
||
295 | } |
||
296 | //work out base total |
||
297 | $actualQuantity = $latestManualUpdateQuantity - $orderQuantityToDeduct; |
||
298 | if (isset($_GET["debug"])) { |
||
299 | echo "<hr />"; |
||
300 | echo $this->Name; |
||
301 | echo " | Manual SUM: ".$latestManualUpdateQuantity; |
||
302 | echo " | Order SUM: ".$orderQuantityToDeduct; |
||
303 | echo " | Total SUM: ".$this->BaseQuantity; |
||
304 | echo "<hr />"; |
||
305 | } |
||
306 | } |
||
307 | return $actualQuantity; |
||
308 | } |
||
309 | } |
||
310 |