/**
	 * returns commission group for user (if not set already)
	 * Commission group can be set previously in the checkCampaignType() function
	 *
	 */
	protected function getCommissionGroup(Pap_Contexts_Tracking $context) {
		$context->debug('Recognizing commission group started');

		if (($user = $context->getUserObject()) == null) {
		    $context->debug('STOPPING, user is not set - cannot find commission group');
		    return;
		}
		
		$commGroupId = $this->getUserCommissionGroupFromCache($context->getCampaignObject(), $user->getId());
		if($commGroupId == false) {
			$context->debug("STOPPING, Cannot find commission group for this affiliate and campaign! ".$context->getCampaignObject()->getId().' - '.$user->getId());
			$context->setDoCommissionsSave(false);
			$context->setDoTrackerSave(false);
			return;
		}
			
		Gpf_Plugins_Engine::extensionPoint('PostAffiliate.RecognizeCommGroup.getCommissionGroup', $context);

        $commissionGroup = $this->getCommissionGroupFromCache($commGroupId);
        if ($commissionGroup == null) {
        	$context->debug('    Commission group with ID '.$commGroupId . ' does not exist');
        	return;
        }

		$context->setCommissionGroup($commissionGroup);
	    $context->debug('Received commission group ID = '.$commGroupId);
	}
    /**
     * @return Pap_Common_Banner
     */
    protected function recognizeCampaigns(Pap_Contexts_Tracking $context) {
        if ($context->getCampaignObject() != null) {
            $context->debug('Campaign already recognized, skipping. CampaignId: '.$context->getCampaignObject()->getId());
            return $context->getCampaignObject();
        }

        try {
            return $this->getCampaignFromForcedParameter($context);
        } catch (Gpf_Exception $e) {
        }
        	
        try {
            return $this->getCampaignFromParameter($context);
        } catch (Gpf_Exception $e) {
        }

        try {
            return $this->recognizeCampaignFromBanner($context);
        } catch (Gpf_Exception $e) {
        }

        try {
            return $this->getDefaultCampaign($context);
        } catch (Gpf_Exception $e) {
        }
    }
    /**
     * @return Pap_Common_Banner
     * @throws Gpf_Exception
     */
    protected function getBannerById(Pap_Contexts_Tracking $context, $id) {
        if (!is_null($context->getCampaignObject())) {
            $campaignId = $context->getCampaignObject()->getId();
        } else {
            $campaignId = '';
        }
        if (isset($this->bannersCache[$id.$campaignId])) {
            return $this->bannersCache[$id.$campaignId];
        }

        $bannerFactory = new Pap_Common_Banner_Factory();
        $banner = $bannerFactory->getBanner($id);
        $this->checkBanner($context, $banner);        
        $this->bannersCache[$id.$campaignId] = $banner;
        return $this->bannersCache[$id.$campaignId];
    }
    /**
     * 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("");
    }
    /**
     * @return Pap_Common_Banner
     */
    protected function recognizeCampaigns(Pap_Contexts_Tracking $context) {
        if ($context->getCampaignObject() != null) {
            return $context->getCampaignObject();
        }

        try {
            return $this->getCampaignFromForcedBanner($context);
        } catch (Gpf_Exception $e) {
        }
        
        try {
            return $this->getCampaignFromParameter($context);
        } catch (Gpf_Exception $e) {
        }

        try {
            return $this->getCampaignFromProductID($context);
        } catch (Gpf_Exception $e) {
            if (Gpf_Settings::get(Pap_Settings::FORCE_CHOOSING_PRODUCTID_SETTING_NAME) == Gpf::YES) {
                $context->setDoCommissionsSave(false);
                return; 
            }
        }

        try {
            $visitorAffiliate = $context->getVisitorAffiliate();
            if ($visitorAffiliate != null) {
                $context->debug('Getting campaign from visitor affiliate, visitorId: '.$visitorAffiliate->getVisitorId());
                $context->debug('Checking campaign with Id: '.$visitorAffiliate->getCampaignId());
                return $this->getCampaignById($context, $visitorAffiliate->getCampaignId());
            }
        } catch (Gpf_Exception $e) {
        }

        try {
            return $this->getDefaultCampaign($context);
        } catch (Gpf_Exception $e) {
        }
    }
   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;
    }
    /**
     * @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];
    }
    public static function prepareVisitorAffiliate(Pap_Db_VisitorAffiliate $visitorAffiliate, Pap_Contexts_Tracking $context) {
        $visitorAffiliate->setUserId($context->getUserObject()->getId());

        if ($context->getBannerObject() != null) {
            $visitorAffiliate->setBannerId($context->getBannerObject()->getId());
        } else {
            $visitorAffiliate->setBannerId(null);
        }
        
        if ($context->getChannelObject() != null) {
            $visitorAffiliate->setChannelId($context->getChannelObject()->getId());
        }

        $visitorAffiliate->setCampaignId($context->getCampaignObject()->getId());
        $visitorAffiliate->setIp($context->getIp());
        $visitorAffiliate->setDateVisit($context->getDateCreated());
        $visitorAffiliate->setReferrerUrl($context->getReferrerUrl());
        $visitorAffiliate->setData1($context->getExtraDataFromRequest(1));
        $visitorAffiliate->setData2($context->getExtraDataFromRequest(2));
        $visitorAffiliate->setValidTo(self::getVisitorAffiliateValidity($context, $visitorAffiliate));
    }
Example #9
0
 public static function getCookieLifetime(Pap_Contexts_Tracking $context) {
     if ($context->getCommissionGroup() !== null &&
     ($cookieLifetime = $context->getCommissionGroup()->getCookieLifetime()) > Pap_Db_CommissionGroup::COOKIE_LIFETIME_VALUE_SAME_AS_CAMPAIGN) {
     	return $cookieLifetime;
     }
     $lifetime = 0;
     if ($context->getCampaignObject() != null) {
         $lifetime = $context->getCampaignObject()->getCookieLifetime();
     }
     if($lifetime == 0) {
         $lifetime = self::COOKIE_LIFETIME_FOREVER;
     }
     return $lifetime;
 }
Example #10
0
 public function getCommissionGroup(Pap_Contexts_Tracking $context) {
     if ($context->getCampaignObject()->getCampaignType() != Pap_Db_Campaign::CAMPAIGN_TYPE_PUBLIC) {
         try {
             $status = Pap_Db_Table_UserInCommissionGroup::getStatus($context->getCampaignObject()->getId(), $context->getUserObject()->getId());
             if ($status != Pap_Features_PerformanceRewards_Condition::STATUS_APPROVED &&
             $status != Pap_Features_PerformanceRewards_Condition::STATUS_FIXED) {
                 throw new Gpf_Exception('');
             }
         } catch (Gpf_Exception $e) {
             $context->debug('    STOPPING, User is not approved in this campaign!');
             $context->setDoCommissionsSave(false);
             $context->setDoTrackerSave(false);
         }
     }
 }