Index: branches/5.2.x/units/pricing/pricing_event_handler.php =================================================================== diff -u -N -r14258 -r14436 --- branches/5.2.x/units/pricing/pricing_event_handler.php (.../pricing_event_handler.php) (revision 14258) +++ branches/5.2.x/units/pricing/pricing_event_handler.php (.../pricing_event_handler.php) (revision 14436) @@ -1,6 +1,6 @@ Application->RecallVar('UserGroups'); + if ( $this->Application->ConfigValue('Comm_PriceBracketCalculation') == 1 ) { + $sql = 'SELECT PrimaryGroupId + FROM ' . TABLE_PREFIX . 'PortalUser + WHERE PortalUserId = ' . $this->Application->GetVar('u_id'); + $pricing_group = $this->Conn->GetOne($sql); - $pricing_method = $this->Application->ConfigValue('Comm_PriceBracketCalculation'); - if ($pricing_method == 1) { - $primary_group = $this->Conn->GetOne('SELECT GroupId FROM '.TABLE_PREFIX.'UserGroup WHERE PortalUserId='.$this->Application->GetVar('u_id').' AND PrimaryGroup=1'); - $pricing_group = $primary_group; if ($pricing_group) { - $pricing_for_group_exists = $this->Conn->GetOne('SELECT COUNT(*) FROM '.TABLE_PREFIX.'ProductsPricing WHERE ProductId='.$this->Application->GetVar('p_id').' AND GroupId='.$primary_group.' AND Price IS NOT NULL'); + $sql = 'SELECT COUNT(*) + FROM ' . TABLE_PREFIX . 'ProductsPricing + WHERE ProductId = ' . $this->Application->GetVar('p_id') . ' AND GroupId = ' . $pricing_group . ' AND Price IS NOT NULL'; + $pricing_for_group_exists = $this->Conn->GetOne($sql); } - if ($pricing_group && $pricing_for_group_exists > 0) { - $pricing_group = $primary_group; - } - else { + + if ( !$pricing_group || !$pricing_for_group_exists ) { $pricing_group = $this->Application->ConfigValue('User_LoggedInGroup'); } - - } else { + $user_groups = $this->Application->RecallVar('UserGroups'); + //$cheapest_group = $this->Conn->GetOne('SELECT GroupId FROM '.$object->TableName.' WHERE ProductId='.$this->Application->GetVar('p_id').' AND Price IS NOT NULL AND GroupId IN ('.$user_groups.') AND MinQty = 1 GROUP BY GroupId ORDER BY Price ASC'); - $effective_brackets = $this->Conn->Query('SELECT PriceId, Price, GroupId FROM '.$object->TableName.' - WHERE ProductId='.$this->Application->GetVar('p_id').' AND Price IS NOT NULL AND GroupId IN ('.$user_groups.') ORDER BY GroupId ASC, MinQty ASC', 'PriceId'); + + $sql = 'SELECT PriceId, Price, GroupId + FROM ' . $object->TableName . ' + WHERE ProductId = ' . $this->Application->GetVar('p_id') . ' AND Price IS NOT NULL AND GroupId IN (' . $user_groups . ') + ORDER BY GroupId ASC, MinQty ASC'; + $effective_brackets = $this->Conn->Query($sql, 'PriceId'); + $group_prices = array(); $min_price = -1; $cheapest_group = 0; Index: branches/5.2.x/units/products/products_event_handler.php =================================================================== diff -u -N -r14258 -r14436 --- branches/5.2.x/units/products/products_event_handler.php (.../products_event_handler.php) (revision 14258) +++ branches/5.2.x/units/products/products_event_handler.php (.../products_event_handler.php) (revision 14436) @@ -1,6 +1,6 @@ getEventParam('field_values'); $item_data = unserialize($field_values['ItemData']); - if (!getArrayValue($item_data,'PortalGroupId')) { + if ( !getArrayValue($item_data,'PortalGroupId') ) { // is subscription product, but no group defined in it's properties trigger_error('Invalid product '.$field_values['ProductName'].' (id: '.$field_values['ProductId'].')', E_USER_WARNING); return false; } - $order_table = $this->Application->getUnitOption('ord', 'TableName'); - $order_idfield = $this->Application->getUnitOption('ord', 'IDField'); - $sql = 'SELECT PortalUserId FROM %s WHERE %s = %s'; - $user_id = $this->Conn->GetOne( sprintf($sql, $order_table, $order_idfield, $field_values['OrderId']) ); + $sql = 'SELECT PortalUserId + FROM ' . $this->Application->getUnitOption('ord', 'TableName') . ' + WHERE ' . $this->Application->getUnitOption('ord', 'IDField') . ' = ' . $field_values['OrderId']; + $user_id = $this->Conn->GetOne($sql); $group_id = $item_data['PortalGroupId']; $duration = $item_data['Duration']; - $sql = 'SELECT * FROM '.TABLE_PREFIX.'UserGroup WHERE PortalUserId = %s'; - $user_groups = $this->Conn->Query( sprintf($sql, $user_id), 'GroupId' ); + $sql = 'SELECT * + FROM ' . TABLE_PREFIX . 'UserGroup + WHERE PortalUserId = ' . $user_id; + $user_groups = $this->Conn->Query($sql, 'GroupId'); - $sql = 'REPLACE INTO '.TABLE_PREFIX.'UserGroup (PortalUserId,GroupId,MembershipExpires,PrimaryGroup) VALUES (%s,%s,%s,%s)'; - if( !isset($user_groups[$group_id]) ) - { - $primary_group = count($user_groups) == 0 ? 1 : 0; + if ( !isset($user_groups[$group_id]) ) { $expire = adodb_mktime() + $duration; } else { - $primary_group = $user_groups[$group_id]['PrimaryGroup']; $expire = $user_groups[$group_id]['MembershipExpires']; $expire = $expire < adodb_mktime() ? adodb_mktime() + $duration : $expire + $duration; } -/* - // Customization healtheconomics.org + /*// Customization healtheconomics.org if ($item_data['DurationType'] == 2) { $expire = $item_data['AccessExpiration']; } - // Customization healtheconomics.org -- -*/ - $this->Conn->Query( sprintf($sql, $user_id, $group_id, $expire, $primary_group) ); + // Customization healtheconomics.org --*/ + $fields_hash = Array ( + 'PortalUserId' => $user_id, + 'GroupId' => $group_id, + 'MembershipExpires' => $expire, + ); + + $this->Conn->doInsert($fields_hash, TABLE_PREFIX . 'UserGroup', 'REPLACE'); + $sub_order =& $this->Application->recallObject('ord.-sub'.$event->getEventParam('next_sub_number'), 'ord'); $sub_order->SetDBField('IsRecurringBilling', getArrayValue($item_data, 'IsRecurringBilling') ? 1 : 0); $sub_order->SetDBField('GroupId', $group_id); $sub_order->SetDBField('NextCharge_date', $expire); $sub_order->SetDBField('NextCharge_time', $expire); } - - function OnDownloadableApprove(&$event) { $field_values = $event->getEventParam('field_values'); @@ -1000,7 +1001,7 @@ parent::OnAfterItemLoad($event); $object =& $event->getObject(); - /* @var $object kDBItem */ + /* @var $object ProductsItem */ $a_pricing = $object->getPrimaryPricing(); if (!$a_pricing) { @@ -1266,22 +1267,22 @@ $user_id = $this->Application->RecallVar('user_id'); - $calculated_fields = $this->Application->getUnitOption($event->Prefix, 'CalculatedFields'); + $sql = 'SELECT PrimaryGroupId + FROM ' . TABLE_PREFIX . 'PortalUser + WHERE PortalUserId = ' . $user_id; + $primary_group_id = $this->Conn->GetOne($sql); - $sql = 'SELECT GroupId - FROM ' . TABLE_PREFIX . 'UserGroup - WHERE PrimaryGroup = 1 AND PortalUserId = ' . $user_id; - $primary_group = $this->Conn->GetOne($sql); - - if (!$primary_group) { + if (!$primary_group_id) { return; } $sub_select = ' SELECT pp.Price FROM ' . TABLE_PREFIX . 'ProductsPricing AS pp - WHERE pp.ProductId = %1$s.ProductId AND GroupId = ' . $primary_group . ' + WHERE pp.ProductId = %1$s.ProductId AND GroupId = ' . $primary_group_id . ' ORDER BY MinQty LIMIT 0,1'; + + $calculated_fields = $this->Application->getUnitOption($event->Prefix, 'CalculatedFields'); $calculated_fields['']['Price'] = 'IFNULL((' . $sub_select . '), ' . $calculated_fields['']['Price'] . ')'; $this->Application->setUnitOption($event->Prefix, 'CalculatedFields', $calculated_fields); } Index: branches/5.2.x/units/orders/order_calculator.php =================================================================== diff -u -N -r14258 -r14436 --- branches/5.2.x/units/orders/order_calculator.php (.../order_calculator.php) (revision 14258) +++ branches/5.2.x/units/orders/order_calculator.php (.../order_calculator.php) (revision 14436) @@ -1,6 +1,6 @@ 0) { - $sql = 'SELECT GroupId - FROM ' . TABLE_PREFIX . 'UserGroup - WHERE PortalUserId = ' . $user_id . ' AND PrimaryGroup = 1'; + $sql = 'SELECT PrimaryGroupId + FROM ' . TABLE_PREFIX . 'PortalUser + WHERE PortalUserId = ' . $user_id; return $this->Conn->GetOne($sql); }