Index: branches/5.3.x/units/orders/orders_event_handler.php =================================================================== diff -u -N -r16106 -r16192 --- branches/5.3.x/units/orders/orders_event_handler.php (.../orders_event_handler.php) (revision 16106) +++ branches/5.3.x/units/orders/orders_event_handler.php (.../orders_event_handler.php) (revision 16192) @@ -1,6 +1,6 @@ getInventoryObject($product_object, $combination_item, $combinations[ $rec['ProductId'].'_'.$rec['OptionsSalt'] ]); $lack = $rec['Quantity'] - $rec['QuantityReserved']; - if ($lack > 0) { - // reserve lack or what is available (in case if we need to reserve anything, by Alex) - $to_reserve = min($lack, $inv_object->GetDBField('QtyInStock') - $product_object->GetDBField('QtyInStockMin')); - if ($to_reserve < $lack) $event->status = kEvent::erFAIL; // if we can't reserve the full lack + if ( $lack > 0 ) { + // Reserve lack or what is available (in case if we need to reserve anything, by Alex). + $to_reserve = min( + $lack, + $inv_object->GetDBField('QtyInStock') - $product_object->GetDBField('QtyInStockMin') + ); - //reserve in order - $order_item->SetDBFieldsFromHash($rec); - $order_item->SetDBField('QuantityReserved', $rec['QuantityReserved'] + $to_reserve); - $order_item->SetId($rec['OrderItemId']); - $order_item->Update(); + // If we can't reserve the full lack. + if ( $to_reserve < $lack ) { + $event->status = kEvent::erFAIL; + } - //update product - increase reserved, decrease in stock + // Reserve in order. + $order_item->SetDBFieldsFromHash($rec); + $order_item->SetDBField('QuantityReserved', $rec['QuantityReserved'] + $to_reserve); + $new_lack = $order_item->GetDBField('Quantity') - $order_item->GetDBField('QuantityReserved'); + $order_item->SetDBField('BackOrderFlag', abs($new_lack) <= 0.0001 ? 0 : 1); + $order_item->SetId($rec['OrderItemId']); + $order_item->Update(); + + // Update product - increase reserved, decrease in stock. $inv_object->SetDBField('QtyReserved', $inv_object->GetDBField('QtyReserved') + $to_reserve); $inv_object->SetDBField('QtyInStock', $inv_object->GetDBField('QtyInStock') - $to_reserve); $inv_object->SetDBField('QtyBackOrdered', $inv_object->GetDBField('QtyBackOrdered') - $to_reserve); $inv_object->Update(); - if ($product_object->GetDBField('InventoryStatus') == 2) { - // inventory by options, then restore changed combination values back to common $combinations array !!! - $combinations[ $rec['ProductId'].'_'.$rec['OptionsSalt'] ] = $inv_object->GetFieldValues(); + if ( $product_object->GetDBField('InventoryStatus') == 2 ) { + // Inventory by options, then restore changed combination + // values back to common $combinations array !!! + $combinations[$rec['ProductId'] . '_' . $rec['OptionsSalt']] = $inv_object->GetFieldValues(); } } $order_items->GoNext(); @@ -3810,9 +3820,7 @@ return ; } - $copied_ids = unserialize($this->Application->RecallVar($event->Prefix . '_copied_ids' . $this->Application->GetVar('wid'), serialize(Array ()))); - - foreach ($copied_ids as $id) { + foreach ( $this->trackCopiedOrderIDs($event) as $id ) { $an_event = new kEvent($this->Prefix . ':Dummy'); $this->Application->SetVar($this->Prefix . '_id', $id); $this->Application->SetVar($this->Prefix . '_mode', ''); // this is to fool ReserveItems to use live table @@ -3821,22 +3829,39 @@ } /** - * Occurs before an item is copied to live table (after all foreign keys have been updated) - * Id of item being copied is passed as event' 'id' param + * Occurs after an item has been copied to live table + * Id of copied item is passed as event' 'id' param * * @param kEvent $event * @return void * @access protected */ - protected function OnBeforeCopyToLive(kEvent $event) + protected function OnAfterCopyToLive(kEvent $event) { - parent::OnBeforeCopyToLive($event); + parent::OnAfterCopyToLive($event); - $id = $event->getEventParam('id'); - $copied_ids = unserialize($this->Application->RecallVar($event->Prefix . '_copied_ids' . $this->Application->GetVar('wid'), serialize(array ()))); - array_push($copied_ids, $id); + $this->trackCopiedOrderIDs($event, $event->getEventParam('id')); + } - $this->Application->StoreVar($event->Prefix . '_copied_ids' . $this->Application->GetVar('wid'), serialize($copied_ids)); + /** + * Tracks copied order IDs. + * + * @param kEvent $event Event. + * @param integer $id Order ID. + * + * @return array + */ + protected function trackCopiedOrderIDs(kEvent $event, $id = null) + { + $setting_name = $event->Prefix . '_copied_ids' . $this->Application->GetVar('wid'); + $ids = $this->Application->GetVar($setting_name, array()); + + if ( isset($id) ) { + array_push($ids, $id); + $this->Application->SetVar($setting_name, $ids); + } + + return $ids; } /**