/**
     * recognizes commission type for campaign
     *
     * @param Pap_Plugins_Tracking_Action_Context $context
     */
    public function getCommissionType(Pap_Contexts_Tracking $context) {
        $campaign = $context->getCampaignObject();

        $context->debug('Recognizing commission type started');
        $type = $context->getActionType();

        try {
            $context->debug('    Checking commission type : '.$type.' is in campaign');
            	
            $hash = $campaign->getId().$type.Pap_Db_CommissionType::STATUS_ENABLED;
            if (isset($this->commissionTypesCache[$hash])) {
                return $this->commissionTypesCache[$hash];
            }

            $commissionType = $campaign->getCommissionTypeObject($type, '', $context->getCountryCode());
            $this->commissionTypesCache[$hash] = $commissionType;
        } catch (Pap_Tracking_Exception $e) {
            $context->debug("    STOPPING, This commission type is not supported by current campaign or is NOT enabled! ");
            return;
        }

        $context->setCommissionTypeObject($commissionType);

        $context->getTransaction(1)->setType($type);
        $context->debug('    Commission type set to: '.$type.', ID: '.$commissionType->getId());
        $context->debug('Recognizing commission type ended');
        $context->debug("");
    }
   public function saveCommission(Pap_Contexts_Tracking $context, Pap_Common_User $user, Pap_Tracking_Common_Commission $commission) {
        $context->debug('Saving '.$context->getActionType().' commission started');

        $transaction = $context->getTransaction($commission->getTier());
        if ($transaction == null) {
            $transaction = clone $context->getTransaction(1);
            $transaction->setPersistent(false);
            $transaction->generateNewTransactionId();
        }
        if (($parentTransaction = $context->getTransaction($commission->getTier() - 1)) != null) {
            $transaction->setParentTransactionId($parentTransaction->getId());
        }
        if (($channel = $context->getChannelObject()) != null) {
            $transaction->setChannel($channel->getId());
        }

        $transaction->setTier($commission->getTier());
        $transaction->setUserId($user->getId());
        $transaction->setCampaignId($context->getCampaignObject()->getId());
        $transaction->setAccountId($context->getAccountId());

        $banner = $context->getBannerObject();
        if (!is_null($banner)) {
            $transaction->setBannerId($banner->getId());
        }

        if ($user->getStatus() == 'P') {
        	$transaction->setStatus('P');
        	$context->debug('Commission is saved as pending because user state is in pending');
        } else {
            $transaction->setStatus($commission->getStatus());
        }
        $transaction->setPayoutStatus(Pap_Common_Transaction::PAYOUT_UNPAID);
        $transaction->setCommissionTypeId($context->getCommissionTypeObject()->getId());
        $transaction->setCountryCode(($context->getVisit()!=null)?$context->getVisit()->getCountryCode():'');
        $transaction->setType($context->getCommissionTypeObject()->getType());
        $transaction->setCommission($commission->getCommission($context->getRealTotalCost()-$context->getFixedCost()));
        $context->debug('  Computed commission is: '.$transaction->getCommission());
        $transaction->setClickCount(1);
        $transaction->setLogGroupId($context->getLoggerGroupId());

        if ($transaction->getTier() == 1) {
            $transaction->setSaleId($transaction->getId());
        } else {
            $transaction->setSaleId($context->getTransaction(1)->getSaleId());
        }

        //check if we can save zero commission
        if ($transaction->getCommission() == 0 &&
        $context->getCommissionTypeObject()->getSaveZeroCommissions() != Gpf::YES) {
            $context->debug('  Saving of commission transaction was STOPPED. Saving of zero commissions is disabled. Trans id: '.$transaction->getId());
            return Gpf_Plugins_Engine::PROCESS_CONTINUE;
        }


        $transactionCompoundContext = new Pap_Common_TransactionCompoundContext($transaction, $context);
        Gpf_Plugins_Engine::extensionPoint('Tracker.saveCommissions.beforeSaveTransaction', $transactionCompoundContext);
        if (!$transactionCompoundContext->getSaveTransaction()) {
            $context->debug('  Saving of commission transaction was STOPPED by plugin. Trans id: '.$transaction->getId());
            return Gpf_Plugins_Engine::PROCESS_CONTINUE;
        }

        $this->saveTransaction($transaction, $context->getVisitDateTime());
        $context->setTransactionObject($transaction, $commission->getTier());

        $context->debug('    Commission transaction was successfully saved with ID: '.$transaction->getId());
        $context->debug('Saving '.$context->getActionType().' commission ended');
        $context->debug('');

        return Gpf_Plugins_Engine::PROCESS_CONTINUE;
    }
 /**
  * recomputes total cost to default currency
  *
  * @return unknown
  */
 public function computeRealTotalCost(Pap_Contexts_Tracking $context) {
     if($context->getActionType() != Pap_Common_Constants::TYPE_ACTION) {
     	$context->debug('Setting commission to 0 as the transaction type is not sale/action but '.$context->getActionType());
         return 0;
     }
     $this->recognizeCurrency->processTotalCost($context);
     $newTotalCost = $this->computeRealCost($context,$context->getRealTotalCost() ,'realTotalCost');
     $context->debug('Setting realTotalCost to '.$newTotalCost);
     $context->setRealTotalCost($newTotalCost);
 }