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()));
    }
 public function loadFrom(Pap_Db_Commission $commission){
     $this->tier = $commission->getTier();
     $this->type = $commission->getCommissionType();
     $this->value = $commission->getCommissionValue();
     $this->subtype = $commission->getSubtype();
     $this->groupid = $commission->getGroupId();
     $this->typeid = $commission->getCommissionTypeId();
 }
    private function createCommission($commissionGroupId, $commissionTypeId, $tier, $type, $value) {
        $c = new Pap_Db_Commission();

        $c->set("tier", $tier);
        $c->set("subtype", 'N');
        $c->set("commissiontype", $type);
        $c->set("commissionvalue", $value);
        $c->set("commtypeid", $commissionTypeId);
        $c->set("commissiongroupid", $commissionGroupId);

        $c->insert();
        return $c->get("commissionid");
    }
    /**
     * 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
  */
 protected function loadCommissionCollectionFromData(Pap_Db_Commission $commission) {
     return $commission->loadCollection();
 }
    public function recompute(Pap_Db_Commission $commission) {
        $commissionType = $commission->getCommissionType();
        $commissionValue = $commission->getCommissionValue();
        $realTotalCost = $this->getTotalCost() - $this->getFixedCost();

        if ($commissionType == Pap_Db_Commission::COMMISSION_TYPE_PERCENTAGE) {
            $newValue = $realTotalCost * ($commissionValue/100);
        } else if($commissionType == Pap_Db_Commission::COMMISSION_TYPE_FIXED) {
            $newValue = $commissionValue;
        } else {
            return;
        }
        $newValue = $newValue * $this->getSplit();

        if ($this->getType() == Pap_Db_Transaction::TYPE_REFUND ||
                $this->getType() == Pap_Db_Transaction::TYPE_CHARGE_BACK) {
            $newValue *= -1;
        }

        $this->setCommission($newValue);
    }
 public function deleteUnusedCommissions($fromTier, $subtype) {
     $commission = new Pap_Db_Commission();
     $commission->deleteUnusedCommissions($fromTier, $subtype, $this->commissionGroupId, $this->commissionTypeId, 'above');
 }
    /**
     * @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;
    }
 /**
  *
  * @return Pap_Db_Commission
  */
 protected function getCommissionForTransaction() {
     $transactions = new Gpf_SqlBuilder_SelectBuilder();
     $transactions->select->add('c.'.Pap_Db_Table_Commissions::TYPE);
     $transactions->select->add('c.'.Pap_Db_Table_Commissions::VALUE);
     $transactions->where->add('t.'.Pap_Db_Table_Transactions::TRANSACTION_ID, '=', $this->transaction->getId());
     $transactions->where->add('c.'.Pap_Db_Table_Commissions::SUBTYPE,'=',self::getCommissionTypeFromTransaction($this->transaction->getType()));
     $this->initTransactionSql($transactions);
     
     $commission = new Pap_Db_Commission();
     $commission->fillFromRecord($transactions->getOneRow());
     return $commission;
 }