/**
  * A method to estimate the impressions required to fulfill a campaign's
  * impression, click, or conversion requirements.
  *
  * The $oCampaign parameter is passed by reference and will have
  * the calculated impression requirement added to it in the position
  * $oCampaign->requiredImpressions
  *
  * @param OX_Maintenance_Priority_Campaign $oCampaign A reference to the campaign.
  * @param string $type Ether "total" or "daily", depending on whether the
  *                     required impressions should be based on the campaign
  *                     lifetime totals, or the campaign daily totals.
  * @param boolean $ignorePast When true, the required impressions will
  *                            ignore any delivery that has occurred to
  *                            date, to the value returned will represent
  *                            the expected TOTAL lifetime or daily
  *                            impressions required.
  */
 function getCampaignImpressionInventoryRequirement($oCampaign, $type, $ignorePast = false)
 {
     OA::debug('  - Getting impression inventory requirements for campaign ID: ' . $oCampaign->id, PEAR_LOG_DEBUG);
     $aConf = $GLOBALS['_MAX']['CONF'];
     if (!$ignorePast) {
         // Get campaign summary statistic totals
         if ($type == 'total') {
             $oCampaign->setSummaryStatisticsToDate();
         } else {
             $oTodayDate = new Date($this->_getDate());
             $oTodayDate->convertTZ($this->currentTz);
             $oCampaign->setSummaryStatisticsToday($oTodayDate->format('%Y-%m-%d'));
         }
     }
     // Calculate impressions required to fulfill click requirement
     if ($type == 'total') {
         $clickTarget = $oCampaign->clickTargetTotal;
     } else {
         $clickTarget = $oCampaign->clickTargetDaily;
     }
     $clickImpressions = $this->_getInventoryImpressionsRequired($clickTarget, $aConf['priority']['defaultClickRatio'], $oCampaign->deliveredClicks, $oCampaign->deliveredImpressions);
     // Calculate impressions required to fulfill conversion requirement
     if ($type == 'total') {
         $conversionTarget = $oCampaign->conversionTargetTotal;
     } else {
         $conversionTarget = $oCampaign->conversionTargetDaily;
     }
     $conversionImpressions = $this->_getInventoryImpressionsRequired($conversionTarget, $aConf['priority']['defaultConversionRatio'], $oCampaign->deliveredConversions, $oCampaign->deliveredImpressions);
     // Get impression requirement
     if ($type == 'total') {
         $impressionTarget = $oCampaign->impressionTargetTotal;
     } else {
         $impressionTarget = $oCampaign->impressionTargetDaily;
     }
     if ($impressionTarget > 0) {
         $impressions = $impressionTarget - $oCampaign->deliveredImpressions;
     } else {
         $impressions = 0;
     }
     // Choose smallest required impression
     $requiredImpressions = $this->_getSmallestNonZeroInteger(array($clickImpressions, $conversionImpressions, $impressions));
     // Apply globally defined level of intentional over-delivery from
     // $GLOBALS['_MAX']['CONF']['priority']['intentionalOverdelivery'] to
     // the calculated required impressions
     if (isset($GLOBALS['_MAX']['CONF']['priority']['intentionalOverdelivery']) && is_numeric($GLOBALS['_MAX']['CONF']['priority']['intentionalOverdelivery']) && $GLOBALS['_MAX']['CONF']['priority']['intentionalOverdelivery'] > 0) {
         // Convert the % into a usable number
         $scale = 1 + $GLOBALS['_MAX']['CONF']['priority']['intentionalOverdelivery'] / 100;
         // Final check
         if ($scale > 1) {
             $requiredImpressions = $requiredImpressions * $scale;
         }
     }
     $oCampaign->requiredImpressions = $requiredImpressions;
 }
 /**
  * A method to test the setSummaryStatisticsToday() method.
  *
  * Requirements:
  * Test 1: Test with no delivery today in the database, and ensure that
  *         zero is set for all delivery values.
  * Test 2: Test with delivery today in the database, and ensure the values
  *         are correctly stored.
  */
 function testSetSummaryStatisticsToday()
 {
     $aCampaignStats = array('advertiser_id' => 1, 'campaign_id' => 1, 'name' => 'Campaign Name', 'active' => 't', 'num_children' => 1, 'sum_requests' => 100, 'sum_views' => 99, 'sum_clicks' => 5, 'sum_conversions' => 1);
     $oServiceLocator =& OA_ServiceLocator::instance();
     $oMaxDalMaintenancePriority =& $oServiceLocator->get('OA_Dal_Maintenance_Priority');
     $oMaxDalMaintenancePriority->setReturnValueAt(0, 'getCampaignStats', null);
     $oMaxDalMaintenancePriority->setReturnValueAt(1, 'getCampaignStats', $aCampaignStats);
     $oMaxDalMaintenancePriority->expectArgumentsAt(0, 'getCampaignStats', array(1, true, '2006-11-10'));
     $oMaxDalMaintenancePriority->expectArgumentsAt(1, 'getCampaignStats', array(1, true, '2006-11-10'));
     $oMaxDalMaintenancePriority->expectCallCount('getCampaignStats', 2);
     // Test 1
     $aParams = array('campaignid' => 1);
     $oCampaign = new OX_Maintenance_Priority_Campaign($aParams);
     $this->assertNull($oCampaign->deliveredRequests);
     $this->assertNull($oCampaign->deliveredImpressions);
     $this->assertNull($oCampaign->deliveredClicks);
     $this->assertNull($oCampaign->deliveredConversions);
     $oCampaign->setSummaryStatisticsToday('2006-11-10');
     $this->assertEqual($oCampaign->deliveredRequests, 0);
     $this->assertEqual($oCampaign->deliveredImpressions, 0);
     $this->assertEqual($oCampaign->deliveredClicks, 0);
     $this->assertEqual($oCampaign->deliveredConversions, 0);
     // Test 2
     $oCampaign->setSummaryStatisticsToday('2006-11-10');
     $this->assertEqual($oCampaign->deliveredRequests, 100);
     $this->assertEqual($oCampaign->deliveredImpressions, 99);
     $this->assertEqual($oCampaign->deliveredClicks, 5);
     $this->assertEqual($oCampaign->deliveredConversions, 1);
     $oMaxDalMaintenancePriority->tally();
 }
 /**
  * A method to estimate the impressions required to fulfill a campaign's
  * impression, click, or conversion requirements.
  *
  * The $oCampaign parameter is passed by reference and will have
  * the calculated impression requirement added to it in the position
  * $oCampaign->requiredImpressions
  *
  * @param OX_Maintenance_Priority_Campaign $oCampaign A reference to the campaign.
  * @param string $type Ether "total" or "daily", depending on whether the
  *                     required impressions should be based on the campaign
  *                     lifetime totals, or the campaign daily totals.
  * @param boolean $ignorePast When true, the required impressions will
  *                            ignore any delivery that has occurred to
  *                            date, to the value returned will represent
  *                            the expected TOTAL lifetime or daily
  *                            impressions required.
  */
 function getCampaignImpressionInventoryRequirement(&$oCampaign, $type, $ignorePast = false)
 {
     OA::debug('  - Getting impression inventory requirements for campaign ID: ' . $oCampaign->id, PEAR_LOG_DEBUG);
     $aConf = $GLOBALS['_MAX']['CONF'];
     if (!$ignorePast) {
         // Get campaign summary statistic totals
         if ($type == 'total') {
             $oCampaign->setSummaryStatisticsToDate();
         } else {
             $oTodayDate = $this->_getDate();
             $oCampaign->setSummaryStatisticsToday($oTodayDate->format('%Y-%m-%d'));
         }
     }
     // Calculate impressions required to fulfill click requirement
     if ($type == 'total') {
         $clickTarget = $oCampaign->clickTargetTotal;
     } else {
         $clickTarget = $oCampaign->clickTargetDaily;
     }
     $clickImpressions = $this->_getInventoryImpressionsRequired($clickTarget, $aConf['priority']['defaultClickRatio'], $oCampaign->deliveredClicks, $oCampaign->deliveredImpressions);
     // Calculate impressions required to fulfill conversion requirement
     if ($type == 'total') {
         $conversionTarget = $oCampaign->conversionTargetTotal;
     } else {
         $conversionTarget = $oCampaign->conversionTargetDaily;
     }
     $conversionImpressions = $this->_getInventoryImpressionsRequired($conversionTarget, $aConf['priority']['defaultConversionRatio'], $oCampaign->deliveredConversions, $oCampaign->deliveredImpressions);
     // Get impression requirement
     if ($type == 'total') {
         $impressionTarget = $oCampaign->impressionTargetTotal;
     } else {
         $impressionTarget = $oCampaign->impressionTargetDaily;
     }
     if ($impressionTarget > 0) {
         $impressions = $impressionTarget - $oCampaign->deliveredImpressions;
     } else {
         $impressions = 0;
     }
     // Choose smallest required impression
     $oCampaign->requiredImpressions = $this->_getSmallestNonZeroInteger(array($clickImpressions, $conversionImpressions, $impressions));
 }