/**
     * @param Pap_Db_VisitorAffiliate
     * @return Pap_Db_VisitorAffiliate
     */
    public function recognizeAffiliate(Pap_Db_VisitorAffiliate $visitorAffiliate) {
        $user = $this->getUserById($visitorAffiliate->getUserId());

        if ($user == null) {
            $this->isValid = false;
            return;
        }
        
        $this->context->setUserObject($user);

        $campaign = $this->getCampaignById($visitorAffiliate->getCampaignId());
        if ($campaign != null && $this->context->getCampaignObject() == null) {
            $this->context->setCampaignObject($campaign);
        }

        $banner = $this->getBannerById($visitorAffiliate->getBannerId());
        if ($banner != null && $this->context->getBannerObject() == null) {
            $this->context->setBannerObject($banner);
        }

        $channel = $this->getChannelById($visitorAffiliate->getChannelId());
        if ($channel != null) {
            $this->context->setChannelObject($channel);
        }
        $this->context->setVisitorAffiliate($visitorAffiliate);

        $this->isValid = true;
    }
  /**
  * recognizes commission type for campaign
  *
  * @param Pap_Plugins_Tracking_Action_Context $context
  */
 public function getCommissionType(Pap_Contexts_Action $context) {
     $campaign = $context->getCampaignObject();
     
     $context->debug('Recognizing commission type started');
     $actionCode = $context->getActionCodeFromRequest();
     if ($actionCode == null || $actionCode == '') {
         $type = Pap_Common_Constants::TYPE_SALE;
     } else {
         $type = Pap_Common_Constants::TYPE_ACTION;
     }
     
     try {
         $context->debug('    Checking commission type : '.$type.' is in campaign');
         $commissionType = $campaign->getCommissionTypeObject($type, $context->getActionCodeFromRequest(), $context->getVisit()->getCountryCode());
     } catch (Pap_Tracking_Exception $e) {          
         $context->debug("    STOPPING, This commission type is not supported by current campaign or is NOT enabled! ");
         throw $e;
     }
     $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("");
 }    
    /**
     * checks for duplicate records from same IP
     *
     * @param Pap_Contexts_Action $context
     * @return string
     */
    private function checkMultipleSalesFromSameIP(Pap_Contexts_Action $context) {
        $checkIt = Gpf_Settings::get(Pap_Settings::DUPLICATE_ORDERS_IP_SETTING_NAME);
        if($checkIt != Gpf::YES) {
            $context->debug('    Check for duplicate sales / leads with the same IP is not turned on');
            return true;
        }

        $context->debug('    Checking duplicate sales / leads from the same IP started');

        $checkPeriod = Gpf_Settings::get(Pap_Settings::DUPLICATE_ORDERS_IP_SECONDS_SETTING_NAME);
        $checkAction = Gpf_Settings::get(Pap_Settings::DUPLICATE_ORDERS_IP_ACTION_SETTING_NAME);
        if($checkPeriod == '' || $checkPeriod == '0' || !is_numeric($checkPeriod)) {
            $context->debug("Checking period is not correct: '$checkPeriod'");
            return true;
        }
        if($checkAction != self::ACTION_DECLINE && $checkAction != self::ACTION_DONTSAVE) {
            $context->debug("Action after check is not correct: '$checkAction'");
            return true;
        }

        $campaignId = null; 
        if (Gpf_Settings::get(Pap_Settings::DUPLICATE_ORDERS_IP_SAMECAMPAIGN_SETTING_NAME) == Gpf::YES) {
            $campaignId = $context->getCampaignObject()->getId();
        }
        $orderId = null; 
        if (Gpf_Settings::get(Pap_Settings::DUPLICATE_ORDERS_IP_SAMEORDERID_SETTING_NAME) == Gpf::YES) {
            $orderId = $context->getOrderIdFromRequest();
            if (trim($orderId) == '') {
                $orderId = null;
            }
        }
        $ip = $context->getIp();
        $context->debug("    Looking transactions with IP: $ip" . (!is_null($campaignId) ? ", campaignid: $campaignId" : '') . (!is_null($orderId) ? ", order ID: $orderId" : '') . '.');
        $transactionsObject = $context->getTransactionObject();
        $recordsCount = $transactionsObject->getNumberOfRecordsFromSameIP($ip,  $this->getTransactionType($context), $checkPeriod, $context->getParentTransactionId(), $context->getVisitDateTime(), $campaignId, $orderId);
        if($recordsCount > 0) {
            if($checkAction == self::ACTION_DONTSAVE) {
                $context->debug("    STOPPING (setting setDoCommissionsSave(false), found another sales / leads from the same IP: $ip". (!is_null($campaignId) ? ", campaignid: $campaignId" : '') . (!is_null($orderId) ? ", order ID: $orderId" : '') ."within $checkPeriod seconds");
                $context->setDoCommissionsSave(false);
                $context->debug('      Checking duplicate sales / leads from the same IP endeded');
                return false;

            } else {
                $context->debug("  DECLINING, found another sales / leads from the same IP: $ip". (!is_null($campaignId) ? ", campaignid: $campaignId" : '') . (!is_null($orderId) ? ", order ID: $orderId" : '') ." within $checkPeriod seconds");

                $message = Gpf_Settings::get(Pap_Settings::DUPLICATE_ORDERS_IP_MESSAGE_SETTING_NAME);

                $this->declineAction($context, $message);

                $context->debug('      Checking duplicate sales / leads from the same IP endeded');
                return true;
            }
        } else {
            $context->debug("    No duplicate sales / leads from the same IP: $ip".(!is_null($campaignId) ? ", campaignid: $campaignId" : '') . (!is_null($orderId) ? ", order ID: $orderId" : '')." found");
        }

        $context->debug('      Checking duplicate sales / leads from the same IP endeded');
        return true;
    }
 /**
  * @param Pap_Contexts_Action $context
  * @return Pap_Db_CommissionType
  */
 protected function getCommissionType(Pap_Contexts_Action $context) {
     $campaign = $context->getCampaignObject();
     
     $actionCode = $context->getActionCodeFromRequest();
     if ($actionCode != '') {
         return $campaign->getCommissionTypeObject(Pap_Common_Constants::TYPE_ACTION, $actionCode, $context->getVisit()->getCountryCode());
     } else {
         return $campaign->getCommissionTypeObject(Pap_Common_Constants::TYPE_SALE, '', $context->getVisit()->getCountryCode());
     }
 }