Index: branches/5.3.x/units/orders/orders_event_handler.php =================================================================== diff -u -N -r15671 -r15695 --- branches/5.3.x/units/orders/orders_event_handler.php (.../orders_event_handler.php) (revision 15671) +++ branches/5.3.x/units/orders/orders_event_handler.php (.../orders_event_handler.php) (revision 15695) @@ -1,6 +1,6 @@ getStatusField(); + $status_field = $event->getUnitConfig()->getStatusField(true); if ( isset($field_values[$status_field]) && $order_dummy->GetDBField($status_field) != $field_values[$status_field] ) { // user can't change status by himself @@ -318,11 +318,9 @@ $fields_hash['AffiliateId'] = $affiliate_id; } - $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); - $table_name = $this->Application->getUnitOption($event->Prefix, 'TableName'); + $config = $event->getUnitConfig(); + $this->Conn->doUpdate($fields_hash, $config->getTableName(), $config->getIDField() . ' = ' . $order_id); - $this->Conn->doUpdate($fields_hash, $table_name, $id_field . ' = ' . $order_id); - $object = $event->getObject(); /* @var $object kDBItem */ @@ -374,7 +372,7 @@ $billing_email = $order->GetDBField('BillingEmail'); $sql = 'SELECT Email - FROM ' . $this->Application->getUnitOption('u', 'TableName') . ' + FROM ' . $this->Application->getUnitConfig('u')->getTableName() . ' WHERE PortalUserId = ' . $order->GetDBField('PortalUserId'); $user_email = $this->Conn->GetOne($sql); @@ -514,13 +512,14 @@ // $event->CallSubEvent('OnSave'); } - $order_id = $order->GetId(); - $order_idfield = $this->Application->getUnitOption('ord','IDField'); - $order_table = $this->Application->getUnitOption('ord','TableName'); + $order_id = $order->GetID(); + $config = $event->getUnitConfig(); + $original_amount = $order->GetDBField('SubTotal') + $order->GetDBField('ShippingCost') + $order->GetDBField('VAT') + $order->GetDBField('ProcessingFee') + $order->GetDBField('InsuranceFee') - $order->GetDBField('GiftCertificateDiscount'); - $sql = 'UPDATE '.$order_table.' + + $sql = 'UPDATE '. $config->getTableName() .' SET OriginalAmount = '.$original_amount.' - WHERE '.$order_idfield.' = '.$order_id; + WHERE '. $config->getIDField() .' = '.$order_id; $this->Conn->Query($sql); $this->Application->StoreVar('front_order_id', $order_id); @@ -1038,8 +1037,10 @@ // 2. check for option combinations in stock $comb_salt = $this->OptionsSalt($options, true); if ($comb_salt) { + $poc_config = $this->Application->getUnitConfig('poc'); + // such option combination is defined explicitly - $poc_table = $this->Application->getUnitOption('poc', 'TableName'); + $poc_table = $poc_config->getTableName(); $sql = 'SELECT Availability FROM '.$poc_table.' WHERE CombinationCRC = '.$comb_salt; @@ -1049,8 +1050,8 @@ if ($comb_availble == 1) { // 2.2. check for quantity in stock $table = Array(); - $table['poc'] = $this->Application->getUnitOption('poc', 'TableName'); - $table['p'] = $this->Application->getUnitOption('p', 'TableName'); + $table['poc'] = $poc_config->getTableName(); + $table['p'] = $this->Application->getUnitConfig('p')->getTableName(); $table['oi'] = $this->TablePrefix($event).'OrderItems'; $object = $event->getObject(); @@ -1140,23 +1141,29 @@ function BuildSubscriptionItemData($item_id, $item_data) { - $products_table = $this->Application->getUnitOption('p', 'TableName'); - $products_idfield = $this->Application->getUnitOption('p', 'IDField'); - $sql = 'SELECT AccessGroupId FROM %s WHERE %s = %s'; - $item_data['PortalGroupId'] = $this->Conn->GetOne( sprintf($sql, $products_table, $products_idfield, $item_id) ); + $products_config = $this->Application->getUnitConfig('p'); - $pricing_table = $this->Application->getUnitOption('pr', 'TableName'); - $pricing_idfield = $this->Application->getUnitOption('pr', 'IDField'); + $sql = 'SELECT AccessGroupId + FROM ' . $products_config->getTableName() . ' + WHERE ' . $products_config->getIDField() . ' = ' . $item_id; + $item_data['PortalGroupId'] = $this->Conn->GetOne($sql); /* TODO check on implementation - $sql = 'SELECT AccessDuration, AccessUnit, DurationType, AccessExpiration FROM %s WHERE %s = %s'; + $sql = 'SELECT AccessDuration, AccessUnit, DurationType, AccessExpiration + FROM %s + WHERE %s = %s'; */ - $sql = 'SELECT * FROM %s WHERE %s = %s'; + $pricing_config = $this->Application->getUnitConfig('pr'); $pricing_id = $this->GetPricingId($item_id, $item_data); + + $sql = 'SELECT * + FROM ' . $pricing_config->getTableName() . ' + WHERE ' . $pricing_config->getIDField() . ' = ' . $pricing_id; + $pricing_info = $this->Conn->GetRow($sql); + $item_data['PricingId'] = $pricing_id; - $pricing_info = $this->Conn->GetRow( sprintf($sql, $pricing_table, $pricing_idfield, $pricing_id ) ); $unit_secs = Array(1 => 1, 2 => 60, 3 => 3600, 4 => 86400, 5 => 604800, 6 => 2592000, 7 => 31536000); /* TODO check on implementation (code from customization healtheconomics.org) @@ -1226,7 +1233,7 @@ $coupon->Update(); - $this->Application->setUnitOption('ord', 'AutoLoad', true); + $this->Application->getUnitConfig('ord')->setAutoLoad(true); $order = $this->Application->recallObject('ord'); /* @var $order OrdersItem */ @@ -1296,15 +1303,15 @@ else { $link_id = $this->Application->GetVar('l_id'); - $sql = 'SELECT ResourceId FROM '.$this->Application->getUnitOption('l', 'TableName').' + $sql = 'SELECT ResourceId FROM '.$this->Application->getUnitConfig('l')->getTableName().' WHERE LinkId = '.$link_id; - $sql = 'SELECT ListingTypeId FROM '.$this->Application->getUnitOption('ls', 'TableName').' + $sql = 'SELECT ListingTypeId FROM '.$this->Application->getUnitConfig('ls')->getTableName().' WHERE ItemResourceId = '.$this->Conn->GetOne($sql); $item_data['LinkId'] = $link_id; $item_data['ListingTypeId'] = $this->Conn->GetOne($sql); } - $sql = 'SELECT VirtualProductId FROM '.$this->Application->getUnitOption('lst', 'TableName').' + $sql = 'SELECT VirtualProductId FROM '.$this->Application->getUnitConfig('lst')->getTableName().' WHERE ListingTypeId = '.$item_data['ListingTypeId']; $item_id = $this->Conn->GetOne($sql); @@ -1799,7 +1806,8 @@ */ function ReadyToProcess($ord_id) { - $poc_table = $this->Application->getUnitOption('poc', 'TableName'); + $poc_table = $this->Application->getUnitConfig('poc')->getTableName(); + $query = ' SELECT SUM(IF( IF('.TABLE_PREFIX.'Products.InventoryStatus = 2, '.$poc_table.'.QtyInStock, '.TABLE_PREFIX.'Products.QtyInStock) - '.TABLE_PREFIX.'Products.QtyInStockMin >= ('.TABLE_PREFIX.'OrderItems.Quantity - '.TABLE_PREFIX.'OrderItems.QuantityReserved), 0, 1)) FROM '.TABLE_PREFIX.'OrderItems LEFT JOIN '.TABLE_PREFIX.'Products ON '.TABLE_PREFIX.'Products.ProductId = '.TABLE_PREFIX.'OrderItems.ProductId @@ -1835,7 +1843,8 @@ if ($combinations) { // 2. query data about combinations - $poc_table = $this->Application->getUnitOption('poc', 'TableName'); + $poc_table = $this->Application->getUnitConfig('poc')->getTableName(); + $sql = 'SELECT CONCAT(poc.ProductId, "_", poc.CombinationCRC) AS CombinationKey, poc.* FROM '.$poc_table.' poc WHERE ('.implode(') OR (', $combinations).')'; @@ -2100,26 +2109,26 @@ } $product_object->Load($product_item['ProductId']); $hits = floor( $product_object->GetDBField('Hits') ) + 1; - $sql = 'SELECT MAX(Hits) FROM '.$this->Application->getUnitOption('p', 'TableName').' + $sql = 'SELECT MAX(Hits) FROM '.$this->Application->getUnitConfig('p')->getTableName().' WHERE FLOOR(Hits) = '.$hits; $hits = ( $res = $this->Conn->GetOne($sql) ) ? $res + 0.000001 : $hits; $product_object->SetDBField('Hits', $hits); $product_object->Update(); - /*$sql = 'UPDATE '.$this->Application->getUnitOption('p', 'TableName').' - SET Hits = Hits + '.$product_item['Quantity'].' - WHERE ProductId = '.$product_item['ProductId']; + /*$sql = 'UPDATE ' . $this->Application->getUnitConfig('p')->getTableName() . ' + SET Hits = Hits + ' . $product_item['Quantity'] . ' + WHERE ProductId = ' . $product_item['ProductId']; $this->Conn->Query($sql);*/ } $this->PrepareCoupons($event, $object); $this->SplitOrder($event, $object); - if ( $object->GetDBField('IsRecurringBilling') != 1 ) { + if ($object->GetDBField('IsRecurringBilling') != 1) { $this->Application->emailUser('ORDER.APPROVE', null, $email_params); // Mask credit card with XXXX - if ( $this->Application->ConfigValue('Comm_MaskProcessedCreditCards') ) { + if ($this->Application->ConfigValue('Comm_MaskProcessedCreditCards')) { $this->maskCreditCard($object, 'PaymentAccount'); $set_new_status = 1; } @@ -2138,15 +2147,15 @@ $this->raiseProductEvent('Deny', $product_item['ProductId'], $product_item); } - if ( ($original_order_status != ORDER_STATUS_INCOMPLETE) && ($event->Name == 'OnMassOrderDeny' || $event->Name == 'OnOrderDeny') ) { + if ( ($original_order_status != ORDER_STATUS_INCOMPLETE ) && ($event->Name == 'OnMassOrderDeny' || $event->Name == 'OnOrderDeny') ) { $this->Application->emailUser('ORDER.DENY', null, $email_params); // inform payment gateway that order was declined $gw_data = $object->getGatewayData(); if ( $gw_data ) { - $this->Application->registerClass($gw_data['ClassName'], GW_CLASS_PATH . '/' . $gw_data['ClassFile']); - $gateway_object = $this->Application->recallObject($gw_data['ClassName']); + $this->Application->registerClass( $gw_data['ClassName'], GW_CLASS_PATH . '/' . $gw_data['ClassFile'] ); + $gateway_object = $this->Application->recallObject( $gw_data['ClassName'] ); $gateway_object->OrderDeclined($object->GetFieldValues(), $gw_data['gw_params']); } @@ -2188,7 +2197,7 @@ if ( !array_key_exists('error_number', $ret) ) { $set_new_status = $this->approveOrder($order_items); -// $set_new_status = $this->shipOrder($order_items); + // $set_new_status = $this->shipOrder($order_items); $object->SetDBField('ShippingDate', adodb_mktime()); $object->UpdateFormattersSubFields(); @@ -2199,8 +2208,8 @@ // inform payment gateway that order was shipped $gw_data = $object->getGatewayData(); - $this->Application->registerClass($gw_data['ClassName'], GW_CLASS_PATH . '/' . $gw_data['ClassFile']); - $gateway_object = $this->Application->recallObject($gw_data['ClassName']); + $this->Application->registerClass( $gw_data['ClassName'], GW_CLASS_PATH.'/'.$gw_data['ClassFile'] ); + $gateway_object = $this->Application->recallObject( $gw_data['ClassName'] ); $gateway_object->OrderShipped($object->GetFieldValues(), $gw_data['gw_params']); } @@ -2215,7 +2224,7 @@ case 'OnMassOrderProcess': case 'OnOrderProcess': - if ( $this->ReadyToProcess($object->GetID()) ) { + if ($this->ReadyToProcess($object->GetID())) { $event->CallSubEvent('OnReserveItems'); if ( $event->status == kEvent::erSUCCESS ) { @@ -2230,7 +2239,7 @@ break; } - if ( $set_new_status ) { + if ($set_new_status) { $object->Update(); } } @@ -2312,13 +2321,12 @@ parent::OnAfterClone($event); $id = $event->getEventParam('id'); - $table = $this->Application->getUnitOption($event->Prefix, 'TableName'); - $id_field = $this->Application->getUnitOption($event->Prefix, 'IDField'); + $config = $event->getUnitConfig(); // set cloned order status to Incomplete - $sql = 'UPDATE ' . $table . ' + $sql = 'UPDATE ' . $config->getTableName() . ' SET Status = 0 - WHERE ' . $id_field . ' = ' . $id; + WHERE ' . $config->getIDField() . ' = ' . $id; $this->Conn->Query($sql); } @@ -2634,7 +2642,7 @@ $ord_id = $order->GetId(); // TABLE_PREFIX and $table_prefix are NOT the same !!! - $poc_table = $this->Application->getUnitOption('poc', 'TableName'); + $poc_table = $this->Application->getUnitConfig('poc')->getTableName(); $query = ' SELECT BackOrderFlag, '. $table_prefix.'OrderItems.OrderItemId, '. @@ -2695,8 +2703,8 @@ } else { // inventory = 2 -> by product option combinations - $poc_idfield = $this->Application->getUnitOption('poc', 'IDField'); - $poc_table = $this->Application->getUnitOption('poc', 'TableName'); + $poc_table = $this->Application->getUnitConfig('poc')->getTableName(); + $query = ' UPDATE '.$poc_table.' SET '.$update_clause.' WHERE (ProductId = '.$an_item['ProductId'].') AND (CombinationCRC = '.$an_item['CombinationCRC'].')'; @@ -2734,8 +2742,8 @@ } else { // inventory by option combinations - $poc_idfield = $this->Application->getUnitOption('poc', 'IDField'); - $poc_table = $this->Application->getUnitOption('poc', 'TableName'); + $poc_table = $this->Application->getUnitConfig('poc')->getTableName(); + $query = ' UPDATE '.$poc_table.' SET '.$update_clause.' WHERE (ProductId = '.$an_item['ProductId'].') AND (CombinationCRC = '.$an_item['CombinationCRC'].')'; @@ -3338,7 +3346,12 @@ if ($object->GetDBField('Status') > ORDER_STATUS_PENDING) return; //no changes for orders other than incomple or pending $pt = $object->GetDBField('PaymentType'); - $processing_fee = $this->Conn->GetOne('SELECT ProcessingFee FROM '.$this->Application->getUnitOption('pt', 'TableName').' WHERE PaymentTypeId = '.$pt); + + $sql = 'SELECT ProcessingFee + FROM ' . $this->Application->getUnitConfig('pt')->getTableName() . ' + WHERE PaymentTypeId = ' . $pt; + $processing_fee = $this->Conn->GetOne($sql); + $object->SetDBField( 'ProcessingFee', $processing_fee ); $this->UpdateTotals($event); } @@ -3361,12 +3374,12 @@ return false; } - $sql = 'SELECT Price * Quantity AS Amount, ProductId FROM '.$this->Application->getUnitOption('orditems', 'TableName').' + $sql = 'SELECT Price * Quantity AS Amount, ProductId FROM '.$this->Application->getUnitConfig('orditems')->getTableName().' WHERE OrderId = '.$object->GetDBField('OrderId'); $orditems = $this->Conn->GetCol($sql, 'ProductId'); - $sql = 'SELECT coupi.ItemType, p.ProductId FROM '.$this->Application->getUnitOption('coupi', 'TableName').' coupi - LEFT JOIN '.$this->Application->getUnitOption('p', 'TableName').' p + $sql = 'SELECT coupi.ItemType, p.ProductId FROM '.$this->Application->getUnitConfig('coupi')->getTableName().' coupi + LEFT JOIN '.$this->Application->getUnitConfig('p')->getTableName().' p ON coupi.ItemResourceId = p.ResourceId WHERE CouponId = '.$object->GetDBField('CouponId'); $discounts = $this->Conn->GetCol($sql, 'ProductId'); @@ -3404,13 +3417,14 @@ { $id = current($this->StoreSelectedIDs($event)); - $id_field = $this->Application->getUnitOption($event->Prefix,'IDField'); - $table = $this->Application->getUnitOption($event->Prefix,'TableName'); + $config = $event->getUnitConfig(); + $id_field = $config->getIDField(); - $sql = 'SELECT Status FROM %s WHERE %s = %s'; + $sql = 'SELECT Status + FROM ' . $config->getTableName() . ' + WHERE ' . $id_field . ' = ' . $id; + $order_status = $this->Conn->GetOne($sql); - $order_status = $this->Conn->GetOne( sprintf($sql, $table, $id_field, $id) ); - $prefix_special = $event->Prefix.'.'.$this->getSpecialByType($order_status); $orders_list = $this->Application->recallObject($prefix_special, $event->Prefix.'_List', Array('per_page'=>-1) ); @@ -3480,14 +3494,14 @@ */ function getRecurringOrders($pre_expiration) { - $ord_table = $this->Application->getUnitOption('ord', 'TableName'); - $ord_idfield = $this->Application->getUnitOption('ord', 'IDField'); + $config = $this->Application->getUnitConfig('ord'); + $ord_id_field = $config->getIDField(); $processing_allowed = Array(ORDER_STATUS_PROCESSED, ORDER_STATUS_ARCHIVED); - $sql = 'SELECT '.$ord_idfield.', PortalUserId, GroupId, NextCharge - FROM '.$ord_table.' + $sql = 'SELECT '.$ord_id_field.', PortalUserId, GroupId, NextCharge + FROM '. $config->getTableName() .' WHERE (IsRecurringBilling = 1) AND (NextCharge < '.$pre_expiration.') AND Status IN ('.implode(',', $processing_allowed).')'; - return $this->Conn->Query($sql, $ord_idfield); + return $this->Conn->Query($sql, $ord_id_field); } /** @@ -3498,11 +3512,9 @@ function OnCheckRecurringOrders($event) { $skip_clause = Array(); - $ord_table = $this->Application->getUnitOption($event->Prefix, 'TableName'); - $ord_idfield = $this->Application->getUnitOption($event->Prefix, 'IDField'); - $pre_expiration = adodb_mktime() + $this->Application->ConfigValue('Comm_RecurringChargeInverval') * 3600 * 24; $to_charge = $this->getRecurringOrders($pre_expiration); + if ($to_charge) { $order_ids = Array(); foreach ($to_charge as $order_id => $record) { @@ -3537,9 +3549,11 @@ } // remove recurring flag from all orders found, not to select them next time script runs - $sql = 'UPDATE '.$ord_table.' + $config = $event->getUnitConfig(); + + $sql = 'UPDATE '. $config->getTableName() .' SET IsRecurringBilling = 0 - WHERE '.$ord_idfield.' IN ('.implode(',', array_keys($to_charge)).')'; + WHERE '. $config->getIDField() .' IN ('.implode(',', array_keys($to_charge)).')'; $this->Conn->Query($sql); } @@ -3658,14 +3672,16 @@ $order_number = (int)$this->Application->ConfigValue('Comm_Order_Number_Format_P'); $order_sub_number = (int)$this->Application->ConfigValue('Comm_Order_Number_Format_S'); - $calc_fields = $this->Application->getUnitOption($event->Prefix, 'CalculatedFields'); + $config = $event->getUnitConfig(); + $calc_fields = $config->getSetting('CalculatedFields'); + foreach ($calc_fields as $special => $fields) { $calc_fields[$special]['OrderNumber'] = str_replace('6', $order_number, $calc_fields[$special]['OrderNumber']); $calc_fields[$special]['OrderNumber'] = str_replace('3', $order_sub_number, $calc_fields[$special]['OrderNumber']); } - $this->Application->setUnitOption($event->Prefix, 'CalculatedFields', $calc_fields); + $config->setSetting('CalculatedFields', $calc_fields); - $fields = $this->Application->getUnitOption($event->Prefix, 'Fields'); + $fields = $config->getFields(); $fields['Number']['format'] = str_replace('%06d', '%0'.$order_number.'d', $fields['Number']['format']); $fields['SubNumber']['format'] = str_replace('%03d', '%0'.$order_sub_number.'d', $fields['SubNumber']['format']); @@ -3702,13 +3718,13 @@ ); } - $this->Application->setUnitOption($event->Prefix, 'Fields', $fields); + $config->setFields($fields); - $user_forms = $this->Application->getUnitOption('u', 'Forms'); + $user_default_form = $this->Application->getUnitConfig('u')->getFieldByName('default'); - $virtual_fields = $this->Application->getUnitOption($event->Prefix, 'VirtualFields'); - $virtual_fields['UserPassword']['hashing_method'] = $user_forms['default']['Fields']['PasswordHashingMethod']['default']; - $this->Application->setUnitOption($event->Prefix, 'VirtualFields', $virtual_fields); + $virtual_fields = $config->getVirtualFields(); + $virtual_fields['UserPassword']['hashing_method'] = $user_default_form['Fields']['PasswordHashingMethod']['default']; + $config->setVirtualFields($virtual_fields); } /**