protected function recomputeCommissions() {
        $transactions = $this->getTransactionsSelect();
        $this->rule->setTransactionsWhere($transactions->where, 't');

        foreach ($transactions->getAllRowsIterator() as $record) {       	
            $newCommission = new Pap_Db_Commission();
            $newCommission->setTypeId($record->get(Pap_Db_Table_Transactions::COMMISSIONTYPEID));
            $newCommission->setTier($record->get(Pap_Db_Table_Transactions::TIER));
            $newCommission->setGroupId($this->rule->getCommissionGroupId());          
            if ($record->get(Pap_Db_Table_Transactions::R_TYPE) == Pap_Common_Constants::TYPE_RECURRING) { 
            	$newCommission->setSubtype(Pap_Db_Table_Commissions::SUBTYPE_RECURRING);
            } else {
            	$newCommission->setSubtype(Pap_Db_Table_Commissions::SUBTYPE_NORMAL);
            }
            try {
            	$newCommission->loadFromData();
            } catch (Exception $e) {
            	$this->logMessage(sprintf("Error loading commission (%s)", $e->getMessage()));
            	return;
            }            
            $transaction = new Pap_Db_Transaction();
            $transaction->fillFromRecord($record);
            $transaction->recompute($newCommission);            
            $transaction->update();         
            $refundTransaction = $transaction->getRefundOrChargebackTransaction();
            if (!is_null($refundTransaction) &&
                    $refundTransaction->getStatus() != Pap_Common_Constants::STATUS_DECLINED &&
                    $refundTransaction->getPayoutStatus() == Pap_Common_Constants::PSTATUS_UNPAID) {
                $refundTransaction->recompute($newCommission);
                $refundTransaction->update();
            }
        }
        $this->logMessage(sprintf("Transactions were updated based on new commission group %s", $this->rule->getCommissionGroupId()));
    }
    /**
     * returns recordset with commission objects
     *
     * @param string $commissionGroupId
     * @param string $commissionTypeId
     * @return Gpf_DbEngine_Row_Collection <Pap_Db_Commission>
     */
    public function getCommissionsCollection($commissionGroupId, $commissionTypeId) {
        $commission = new Pap_Db_Commission();
        $commission->setGroupId($commissionGroupId);
        $commission->setTypeId($commissionTypeId);

        try {
            return $commission->loadCollection();
        } catch (Gpf_DbEngine_NoRowException $e) {
            throw new Gpf_Exception("Cannot load commission settings for comgroupid=".$commissionGroupId.", commtypeid=".$commissionTypeId);
        }
    }
 protected function insertCommissionObject($tier, $subType, $commissionValueType, $commissionValue, $groupId, $commissionTypeId) {
     $obj = new Pap_Db_Commission();
     $obj->setTier($tier);
     $obj->setSubtype($subType);
     $obj->setCommType($commissionValueType);
     $obj->setCommission($commissionValue);
     $obj->setGroupId($groupId);
     $obj->setCommissionTypeId($commissionTypeId);
     $obj->save();
 }
    /**
     * @return Gpf_DbEngine_Row_Collection
     */
    private function getTierCommissionCollection(Pap_Contexts_Tracking $context, $userId, $tier) {
    	$context->debug('Loading tier commission collection for userid: ' . $userId . ' and tier: ' . $tier);
        $commissionTypeId = $context->getCommissionTypeObject()->getId();
        $groupId = $this->getCommissionGroupForUser($context->getCampaignObject(), $userId);
        $hash = $commissionTypeId.$groupId.$tier;

        if (isset($this->commissions[$hash])) {
        	$context->debug('Record found in cache.');
            return $this->commissions[$hash];
        }

        $context->debug('Trying to load commission for typeid:' . $commissionTypeId . ', groupId:' . $groupId . ',tier:' . $tier);
        $commission = new Pap_Db_Commission();
        $commission->setCommissionTypeId($commissionTypeId);
        $commission->setGroupId($groupId);
        $commission->setTier($tier);
        try {
            $commissions = $this->loadCommissionCollectionFromData($commission);
        } catch (Gpf_DbEngine_NoRowException $e) {
        	$context->debug('Error loading collection from data. returning empty collection.');
            return new Gpf_DbEngine_Row_Collection();
        }
        $context->debug('Commissions succ. loaded, saving to cache.');
        $this->commissions[$hash] = $commissions;
        return $this->commissions[$hash];
    }
    private function saveCommissionRecord($tier, $subtype, $commissionType, $commissionValue) {
        $commission = new Pap_Db_Commission();
        $commission->setTier($tier);
        $commission->setSubtype($subtype);
        $commission->setGroupId($this->commissionGroupId);
        $commission->setTypeId($this->commissionTypeId);
        if ($commissionValue == null) {
            $commissionValue = 0;
        }

        try {
            $commission->loadFromData();
            // loaded, change commission value and save
            $commission->setCommType($commissionType);
            $commission->setCommission($commissionValue);
            $commission->save();
        } catch(Gpf_DbEngine_NoRowException $e) {
            // doesn't exist, insert new record
            $commission->setCommType($commissionType);
            $commission->setCommission($commissionValue);
            $commission->insert();
        } catch(Gpf_DbEngine_TooManyRowsException $e) {
            // there are multiple rows, it is a mistake
            $commission->deleteUnusedCommissions($tier, $subtype, $this->commissionGroupId, $this->commissionTypeId, 'exact');
            $commission->setCommType($commissionType);
            $commission->setCommission($commissionValue);
            $commission->insert();
        }
    }
    /**
     * @param Pap_Db_Transaction $transaction
     * @return Pap_Db_Commission
     */
    protected function getCommissionForTransaction(Pap_Db_Transaction $transaction) {
        $commission = new Pap_Db_Commission();
        $commission->setCommissionTypeId($transaction->getCommissionTypeId());
        $commission->setGroupId($transaction->getCommissionGroupId());
        $commission->setTier($transaction->getTier());
        try {
            $commission->loadFromData(array(Pap_Db_Table_Commissions::TYPE_ID, Pap_Db_Table_Commissions::GROUP_ID));
        } catch (Gpf_Exception $e) {
            $userInGroup = Pap_Db_Table_UserInCommissionGroup::getInstance()->getUserCommissionGroup($transaction->getUserId(), $transaction->getCampaignId());
            $commission->setGroupId($userInGroup->getCommissionGroupId());
            try {
                $commission->loadFromData(array(Pap_Db_Table_Commissions::TYPE_ID, Pap_Db_Table_Commissions::GROUP_ID, Pap_Db_Table_Commissions::TIER));
            } catch (Gpf_Exception $e) {
                throw new Gpf_Exception($this->_('Unable to find commision for transaction id=' . $transaction->getId()));
            }
        }

        return $commission;
    }