private function checkZeroOrdersCommissions(Pap_Contexts_Tracking $context) {
     if ($context->getRealTotalCost() == 0 &&
     $context->getCommissionTypeObject()->getZeroOrdersCommissions() != Gpf::YES) {
         $context->debug("    STOPPING (setting setDoCommissionsSave(false), because TotalCost is 0 and zero order commissions should not be saved.");
         $context->setDoCommissionsSave(false);
     }
 }
Ejemplo n.º 2
0
    public function saveCommissions(Pap_Contexts_Tracking $context) {
        $context->debug('Saving recurring commissions started');

        $commissionType = $context->getCommissionTypeObject();
        if ($commissionType == null) {
            $context->debug('No commission type defined. Recurring commissions saving ended');
            return;
        }
        
        if ($commissionType->getRecurrencePresetId() == Pap_Db_CommissionType::RECURRENCE_NONE
           || $commissionType->getRecurrencePresetId() == null) {
               $context->debug('Saving recurring commissions ended - No recurring commissions defined');
               return;
        }

        $tier = 1;
        $currentUser = $context->getUserObject();
        $currentCommission = $context->getCommission($tier, Pap_Db_Table_Commissions::SUBTYPE_RECURRING);

        if ($currentUser == null || $currentCommission == null) {
            $context->debug('Saving recurring commissions ended - current user or current commission is null');
            return;
        }
        $recurringCommission = new Pap_Db_RecurringCommission();
        $relatedTransaction = $this->getTransaction($context);
        $recurringCommission->setOrderId($relatedTransaction->getOrderId());
        $recurringCommission->setTransactionId($relatedTransaction->getTransactionId());
        $recurringCommission->setRecurrencePresetId($commissionType->getRecurrencePresetId());
        $recurringCommission->setCommissionTypeId($commissionType->getId());
        $recurringCommission->setStatus($relatedTransaction->getStatus());
        $recurringCommission->setLastCommissionDate($relatedTransaction->getDateInserted());
        $recurringCommission->insert();
        $context->debug('Recurring commission successfully saved.');

        while($currentUser != null && $currentCommission != null && $tier < 100) {
            $rcEntry = new Pap_Db_RecurringCommissionEntry();
            $rcEntry->setRecurringCommissionId($recurringCommission->getId());
            $rcEntry->setUserId($currentUser->getId());
            $rcEntry->setTier($tier);
            $rcEntry->setCommission($currentCommission->getCommission($context->getRealTotalCost()));
            $rcEntry->insert();

            $tier++;
            $currentUser = $currentUser->getParentUser();
            $currentCommission = $context->getCommission($tier, Pap_Db_Table_Commissions::SUBTYPE_RECURRING);
        }


        $context->debug('Saving recurring commissions 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;
    }
    /**
     * @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];
    }
Ejemplo n.º 5
0
 protected function getCommissionTypeId(Pap_Contexts_Tracking $context) {
     $commissionType = $context->getCommissionTypeObject();
     if ($commissionType == null) {
         return null;
     }
     return $commissionType->getId();   
 }