Index: branches/5.2.x/units/order_items/order_items_event_handler.php =================================================================== diff -u -N -r14625 -r14641 --- branches/5.2.x/units/order_items/order_items_event_handler.php (.../order_items_event_handler.php) (revision 14625) +++ branches/5.2.x/units/order_items/order_items_event_handler.php (.../order_items_event_handler.php) (revision 14641) @@ -1,6 +1,6 @@ Application->recallObject('p.-item', null, array('skip_autoload' => true)); + /* @var $product_object ProductsItem */ foreach ($product_ids as $product_id) { $product_object->Load($product_id); @@ -79,44 +80,139 @@ } } - $this->finalizePopup($event); + $event->SetRedirectParam('opener', 'u'); } /** * Updates subtotal field in order record. * Only for "Items" tab in "Orders -> Order Edit" in Admin * * @param kEvent $event + * @access protected */ - function OnUpdate(&$event) + protected function OnUpdate(&$event) { - parent::OnUpdate($event); - if ( ($this->Application->GetVar('t') != 'in-commerce/orders/orders_edit_items') ) { - return true; - } + $items_info = $this->Application->GetVar($event->getPrefixSpecial(true)); - $object =& $event->getObject(); - - $items_info = $this->Application->GetVar( $event->getPrefixSpecial(true) ); - if (!$items_info) { - return ; + if ( !$items_info ) { + return; } - $sub_total = $this->getSubTotal($items_info); - $return_total = $this->getReturnTotal($items_info); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ $table_info = $object->getLinkedInfo(); + $main_object =& $this->Application->recallObject($table_info['ParentPrefix']); + /* @var $main_object OrdersItem */ - if ($sub_total !== false) { + foreach ($items_info as $id => $field_values) { + $object->Clear(); // otherwise validation errors will be passed to next object + + $object->Load($id); + $object->SetFieldsFromHash($field_values); + $this->customProcessing($event, 'before'); + + if ( $object->Update($id) ) { + $this->customProcessing($event, 'after'); + $event->status = kEvent::erSUCCESS; + } + else { + $oi_string = $object->GetDBField('ProductId') . ':' . $object->GetDBField('OptionsSalt') . ':' . $object->GetDBField('BackOrderFlag'); + + $field_errors = $object->GetFieldErrors(); + + foreach ($field_errors as $field => $error_params) { + $error_msg = $object->GetErrorMsg($field); + + if ( $error_msg ) { + $main_object->setCheckoutError(OrderCheckoutErrorType::PRODUCT, OrderCheckoutError::FIELD_UPDATE_ERROR, $oi_string . ':' . $field); + } + } + + $event->status = kEvent::erFAIL; + $event->redirect = false; +// break; + } + } + + if ( $this->Application->GetVar('t') != 'in-commerce/orders/orders_edit_items' ) { + return; + } + + $sub_total = $this->getSubTotal($items_info); + + if ( $sub_total !== false ) { $main_object->SetDBField('SubTotal', $sub_total); } - $main_object->SetDBField('ReturnTotal', $return_total); + $main_object->SetDBField('ReturnTotal', $this->getReturnTotal($items_info)); $main_object->Update(); } /** + * Remembers what fields were changed + * + * @param kEvent $event + */ + function OnAfterItemUpdate(&$event) + { + parent::OnAfterItemUpdate($event); + + if ( $this->Application->isAdmin ) { + return ; + } + + $object =& $event->getObject(); + /* @var $object kDBItem */ + + $changed_fields = $object->GetChangedFields(); + + if ( $changed_fields ) { + $table_info = $object->getLinkedInfo(); + + $main_object =& $this->Application->recallObject( $table_info['ParentPrefix'] ); + /* @var $main_object OrdersItem */ + + $oi_string = $object->GetDBField('ProductId') . ':' . $object->GetDBField('OptionsSalt') . ':' . $object->GetDBField('BackOrderFlag'); + + foreach ($changed_fields as $changed_field => $change_info) { + $error_code = OrderCheckoutError::FIELD_UPDATE_SUCCESS; + + if ( $changed_field == 'ItemData' ) { + $item_data_old = unserialize( $change_info['old'] ); + $item_data_new = unserialize( $change_info['new'] ); + + if ( $item_data_old['DiscountId'] != $item_data_new['DiscountId'] || $item_data_old['DiscountType'] != $item_data_new['DiscountType'] ) { + if ( $item_data_new['DiscountId'] > 0 ) { + $error_code = $item_data_new['DiscountType'] == 'discount' ? OrderCheckoutError::DISCOUNT_APPLIED : OrderCheckoutError::COUPON_APPLIED; + } + else { + $error_code = $item_data_old['DiscountType'] == 'discount' ? OrderCheckoutError::DISCOUNT_REMOVED : OrderCheckoutError::COUPON_REMOVED; + } + } + + if ( $error_code == OrderCheckoutError::DISCOUNT_APPLIED || $error_code == OrderCheckoutError::DISCOUNT_REMOVED ) { + // set general error too + $main_object->setCheckoutError(OrderCheckoutErrorType::DISCOUNT, $error_code); + } + } + elseif ( $changed_field == 'Quantity' ) { + // here is how qty is changed: + // OLD QTY -> NEW QTY + // RECALCULATE + // NEW QTY = IN_STOCK_QTY + // NEW ORDER ITEM with LEFTOVER QTY + $this->Application->Debugger->appendTrace(); + $this->Application->Debugger->appendHTML('QTY_CHANGE (' . $oi_string . '): ' . $change_info['old'] . ' => ' . $change_info['new']); + } + + $main_object->setCheckoutError(OrderCheckoutErrorType::PRODUCT, $error_code, $oi_string . ':' . $changed_field); + } + } + } + + /** * Returns subtotal * * @param Array $items_info @@ -193,7 +289,9 @@ $object =& $event->getObject(); /* @var $object kDBItem */ - if ( $item_info = $object->GetDBField('ItemData') ) { + $item_info = $object->GetDBField('ItemData'); + + if ( $item_info ) { $item_info = unserialize($item_info); $object->SetDBField('DiscountType', getArrayValue($item_info, 'DiscountType')); $object->SetDBField('DiscountId', getArrayValue($item_info, 'DiscountId')); @@ -213,13 +311,16 @@ parent::SetCustomQuery($event); $object =& $event->getObject(); + /* @var $object kDBList */ $package_num = $event->getEventParam('package_num'); - if ($package_num) $object->addFilter('package_num', 'PackageNum = '.$package_num); + if ( $package_num ) { + $object->addFilter('package_num', 'PackageNum = ' . $package_num); + } $type = $event->getEventParam('product_type'); - if ($type) { - $object->addFilter('product_type', 'p.Type ='.$type); + if ( $type ) { + $object->addFilter('product_type', 'p.Type =' . $type); } } @@ -231,19 +332,21 @@ */ function checkItemStatus(&$event) { - if ($this->Application->IsAdmin()) { + if ( $this->Application->isAdmin ) { return true; } $object =& $event->getObject(); - if (!$object->isLoaded()) { + /* @var $object kDBItem */ + + if ( !$object->isLoaded() ) { return true; } $order =& $this->Application->recallObject('ord'); /* @var $order kDBItem */ - if ($order->isLoaded() && ($order->GetID() == $object->GetDBField('OrderId'))) { + if ( $order->isLoaded() && ($order->GetID() == $object->GetDBField('OrderId')) ) { return $order->GetDBField('PortalUserId') == $this->Application->RecallVar('user_id'); }