Index: branches/5.2.x/units/affiliates/affiliates_event_handler.php =================================================================== diff -u -N -r14767 -r14986 --- branches/5.2.x/units/affiliates/affiliates_event_handler.php (.../affiliates_event_handler.php) (revision 14767) +++ branches/5.2.x/units/affiliates/affiliates_event_handler.php (.../affiliates_event_handler.php) (revision 14986) @@ -1,6 +1,6 @@ Array ('self' => true), ); @@ -37,7 +40,7 @@ * @return bool * @access public */ - public function CheckPermission(&$event) + public function CheckPermission(kEvent &$event) { if ( $event->Name == 'OnBecomeAffiliate' || $event->Name == 'OnChangePaymentType' ) { return $this->Application->LoggedIn() && $this->Application->ConfigValue('Comm_RegisterAsAffiliate'); @@ -51,17 +54,18 @@ * * @param kEvent $event * @return int + * @access public */ - function getPassedID(&$event) + public function getPassedID(kEvent &$event) { if ( $event->Special == 'user' ) { $event->setEventParam('raise_warnings', 0); - + $sql = 'SELECT ' . $this->Application->getUnitOption($event->Prefix, 'IDField') . ' FROM ' . $this->Application->getUnitOption($event->Prefix, 'TableName') . ' WHERE PortalUserId = ' . (int)$this->Application->RecallVar('user_id'); $id = $this->Conn->GetOne($sql); - + if ( $id ) { return $id; } @@ -163,17 +167,17 @@ function OnStoreAffiliate(&$event) { $event->Init($event->Prefix, '-item'); - $object =& $event->getObject( Array('skip_autoload'=>true) ); + $object =& $event->getObject(Array ('skip_autoload' => true)); + /* @var $object kDBItem */ + $affiliate_storage_method = $this->Application->ConfigValue('Comm_AffiliateStorageMethod'); $affiliate = $this->Application->GetVar('affiliate'); - if($affiliate) - { + if ( $affiliate ) { $object->Load($affiliate, 'AffiliateCode'); } - elseif($affiliate_storage_method == 2) - { + elseif ( $affiliate_storage_method == 2 ) { $affiliate_id = $this->Application->GetVar('affiliate_id'); $object->Load($affiliate_id); } @@ -221,26 +225,32 @@ */ function OnOrderApprove(&$event) { - $order =& $this->Application->recallObject( $event->getEventParam('Order_PrefixSpecial') ); + $order =& $this->Application->recallObject($event->getEventParam('Order_PrefixSpecial')); + /* @var $order OrdersItem */ + $affiliate_id = $order->GetDBField('AffiliateId'); - if(!$affiliate_id) return false; + if ( !$affiliate_id ) { + return false; + } - $object =& $event->getObject( Array('ship_autoload' => true) ); - if( $object->Load($affiliate_id) ) - { - $affiliate_plan =& $this->Application->recallObject('ap', null, Array('skip_autoload'=>true) ); - $affiliate_plan->Load( $object->GetDBField('AffiliatePlanId') ); - if( $affiliate_plan->isLoaded() ) - { + $object =& $event->getObject(Array ('ship_autoload' => true)); + /* @var $object kDBItem */ + + if ( $object->Load($affiliate_id) ) { + $affiliate_plan =& $this->Application->recallObject('ap', null, Array ('skip_autoload' => true)); + /* @var $affiliate_plan kDBItem */ + + $affiliate_plan->Load($object->GetDBField('AffiliatePlanId')); + + if ( $affiliate_plan->isLoaded() ) { $sql = 'SELECT SUM(Quantity) FROM %s WHERE OrderId = %s'; $orderitems_table = $this->Application->getUnitOption('orditems', 'TableName'); - $items_sold = $this->Conn->GetOne( sprintf($sql, $orderitems_table, $order->GetID() ) ); + $items_sold = $this->Conn->GetOne(sprintf($sql, $orderitems_table, $order->GetID())); - $object->SetDBField('AccumulatedAmount', $object->GetDBField('AccumulatedAmount') + $order->GetDBField('TotalAmount') ); + $object->SetDBField('AccumulatedAmount', $object->GetDBField('AccumulatedAmount') + $order->GetDBField('TotalAmount')); $object->SetDBField('ItemsSold', $object->GetDBField('ItemsSold') + $items_sold); - switch( $affiliate_plan->GetDBField('PlanType') ) - { + switch ($affiliate_plan->GetDBField('PlanType')) { case 1: // by amount $value = $object->GetDBField('AccumulatedAmount'); break; @@ -252,32 +262,33 @@ $apb_table = $this->Application->getUnitOption('apbrackets', 'TableName'); $sql = 'SELECT Percent FROM %1$s WHERE (%2$s >= FromAmount) AND ( (%2$s <= ToAmount) OR (ToAmount = -1) ) AND (AffiliatePlanId = %3$s)'; - $commission_percent = $this->Conn->GetOne( sprintf($sql, $apb_table, $this->Conn->qstr($value), $affiliate_plan->GetID() ) ); + $commission_percent = $this->Conn->GetOne(sprintf($sql, $apb_table, $this->Conn->qstr($value), $affiliate_plan->GetID())); // process only orders of current affiliate from period start to this order date $period_ends = $order->GetDBField('OrderDate'); - $period_starts = $this->getPeriodStartTS( $period_ends, $affiliate_plan->GetDBField('ResetInterval') ); - $sql = 'SELECT AffiliateCommission, (SubTotal+ShippingCost+VAT) AS TotalAmount, OrderId FROM '.$order->TableName.' WHERE OrderDate >= %s AND OrderDate <= %s AND AffiliateId = ' . $affiliate_id; + $period_starts = $this->getPeriodStartTS($period_ends, $affiliate_plan->GetDBField('ResetInterval')); + $sql = 'SELECT AffiliateCommission, (SubTotal+ShippingCost+VAT) AS TotalAmount, OrderId + FROM ' . $order->TableName . ' + WHERE OrderDate >= %s AND OrderDate <= %s AND AffiliateId = ' . $affiliate_id; + $amount_to_pay_before = 0; $amount_to_pay_after = 0; - $order_update_sql = 'UPDATE '.$order->TableName.' SET AffiliateCommission = %s WHERE '.$order->IDField.' = %s'; - $orders = $this->Conn->Query( sprintf($sql, $period_starts, $period_ends), 'OrderId' ); - if($orders) - { - foreach($orders as $order_id => $order_data) - { + $order_update_sql = 'UPDATE ' . $order->TableName . ' SET AffiliateCommission = %s WHERE ' . $order->IDField . ' = %s'; + $orders = $this->Conn->Query(sprintf($sql, $period_starts, $period_ends), 'OrderId'); + if ( $orders ) { + foreach ($orders as $order_id => $order_data) { $amount_to_pay_before += $order_data['AffiliateCommission']; $commission = $order_data['TotalAmount'] * ($commission_percent / 100); - $this->Conn->Query( sprintf($order_update_sql, $this->Conn->qstr($commission), $order_id) ); + $this->Conn->Query(sprintf($order_update_sql, $this->Conn->qstr($commission), $order_id)); $amount_to_pay_after += $commission; } } - $object->SetDBField('AmountToPay', $object->GetDBField('AmountToPay') - $amount_to_pay_before + $amount_to_pay_after ); - $object->SetDBField('LastOrderDate_date', $order->GetDBField('OrderDate_date') ); - $object->SetDBField('LastOrderDate_time', $order->GetDBField('OrderDate_time') ); + $object->SetDBField('AmountToPay', $object->GetDBField('AmountToPay') - $amount_to_pay_before + $amount_to_pay_after); + $object->SetDBField('LastOrderDate_date', $order->GetDBField('OrderDate_date')); + $object->SetDBField('LastOrderDate_time', $order->GetDBField('OrderDate_time')); $object->Update(); $order->SetDBField('AffiliateCommission', $commission); // set last commission to this order, because ApproveEvent was called for him @@ -297,7 +308,7 @@ if ( $this->Application->GetVar('RegisterAsAffiliate') != 'on' || $event->MasterEvent->status != kEvent::erSUCCESS ) { return; } - + $object =& $event->getObject( Array('form_name' => 'registration', 'skip_autoload' => true) ); /* @var $object kDBItem */ @@ -617,15 +628,18 @@ * * @param kEvent $event * @return bool + * @access protected */ - function checkItemStatus(&$event) + protected function checkItemStatus(kEvent &$event) { - if ($this->Application->isAdminUser) { + if ( $this->Application->isAdminUser ) { return true; } $object =& $event->getObject(); - if (!$object->isLoaded()) { + /* @var $object kDBItem */ + + if ( !$object->isLoaded() ) { return true; }