/**
     * @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;
    }
	/**
	 * @param $ip
	 * @return Pap_Db_VisitorAffiliate
	 */
	protected function getLatestVisitorAffiliateFromDbByIp($ip, $accountId) {
		$selectBuilder = new Gpf_SqlBuilder_SelectBuilder();
		$selectBuilder->from->add(Pap_Db_Table_VisitorAffiliates::getName());
		$selectBuilder->select->addAll(Pap_Db_Table_VisitorAffiliates::getInstance());
		$selectBuilder->where->add(Pap_Db_Table_VisitorAffiliates::IP, '=', $ip);
		$selectBuilder->where->add(Pap_Db_Table_VisitorAffiliates::ACCOUNTID, '=', $accountId);
		$selectBuilder->where->add(Pap_Db_Table_VisitorAffiliates::VALIDTO, '>=', Gpf_Common_DateUtils::now());
		$selectBuilder->orderBy->add(Pap_Db_Table_VisitorAffiliates::DATEVISIT, false);
		$selectBuilder->limit->set(0, 1);

		try {
			$visitorAffiliate = new Pap_Db_VisitorAffiliate();
			$visitorAffiliate->fillFromRecord($selectBuilder->getOneRow());
		} catch (Gpf_Exception $e) {
			return null;
		}
		 
		return $visitorAffiliate;
	}
 public static function getVisitorAffiliateValidity(Pap_Contexts_Tracking $context, Pap_Db_VisitorAffiliate $visitorAffiliate) {
     return Gpf_Common_DateUtils::addDateUnit($visitorAffiliate->getDateVisit(),
         Pap_Tracking_Cookie::getCookieLifeTimeInDays($context),
         Gpf_Common_DateUtils::DAY);
 }
    private function getVisitorAffiliate(Pap_Db_VisitorAffiliate $firstClickVisitorAffiliate,
    Pap_Db_VisitorAffiliate $oldVisitorAffiliate = null, Pap_Tracking_Visit_VisitorAffiliateCache $cache,
    Pap_Contexts_Tracking $context) {

        if ($firstClickVisitorAffiliate->getDateVisit() > $context->getDateCreated()) {
            return $firstClickVisitorAffiliate;
        }

        if ($oldVisitorAffiliate == null) {
            $visitorAffiliate = $cache->createVisitorAffiliate($context->getVisitorId());
            $visitorAffiliate->setDateVisit($context->getDateCreated());
            return $visitorAffiliate;
        }

        if ($oldVisitorAffiliate->getDateVisit() < $context->getDateCreated()) {
            return $oldVisitorAffiliate;
        }

        return null;
    }
 private function isSameVisitorAffiliates(Pap_Db_VisitorAffiliate $saleVisitorAffiliate, Pap_Db_VisitorAffiliate $clickVisitorAffilaite) {
     return ($saleVisitorAffiliate->getUserId() == $clickVisitorAffilaite->getUserId() &&
     $saleVisitorAffiliate->getCampaignId() == $clickVisitorAffilaite->getCampaignId() &&
     $saleVisitorAffiliate->getChannelId() == $clickVisitorAffilaite->getChannelId());
 }
    private function checkTwoVisitorAffiliateAreSameAndRemove($index1, $index2, Pap_Tracking_Common_VisitorAffiliateCollection $rows,
    Pap_Db_VisitorAffiliate $firstClickVisitorAffiliate, Pap_Tracking_Visit_VisitorAffiliateCache $cache) {
        if ($rows->getValid($index1)->getUserId() != $rows->getValid($index2)->getUserId()) {
            return false;
        }

        if ($rows->getValid($index1)->getUserId() == $firstClickVisitorAffiliate->getUserId()) {
            return false;
        }

        $cache->removeVisitorAffiliate($rows->getValid($index2)->getId());
        $rows->remove($index2);
        $rows->correctIndexes();
        
        $this->cloneVisitorAffiliate($firstClickVisitorAffiliate, $rows, false);
        return true;
    }