<?php

require_once 'classes/Currency.php';

/**
* @package Reports
*/
class Billing_Cycle_Income extends Report
{

    function process()
    {
        $currency = new Currency($this->user);
      $this->SetDescription($this->user->lang('Displays total recurring transactions broken down by billing cycles with the sum and expected yearly income from each.'));
      //SQL to generate the the result set of the report
      $reportSQL = "SELECT COUNT(r.id), SUM(r.amount), r.paymentterm "
                  ."FROM recurringfee r, users u, domains d "
                  ."WHERE r.customerid=u.id AND u.status=1 AND r.appliestoid=d.id AND d.status=1 "
                  ."GROUP BY r.paymentterm";
      $result = $this->db->query($reportSQL);

      //initialize
      $oldBillingCycle = -1;
      $expectedincomeTotal = 0;
      $sumperbillingcycleTotal = 0;
      $itemTotal = 0;
      while (list($tNumberOfRecurringItems,$tSumPerBillingCycle,$tBillingCycle) = $result->fetch()){
             if($oldBillingCycle!=$tBillingCycle){
                  if (isset($aGroup)){
                      //add previous group before getting next group
                      $this->Add($aGroup,$this->GetExtendedName($oldBillingCycle),array($this->user->lang('Items'),$this->user->lang('Sum'),$this->user->lang('Expected Yearly Income')));
                      unset($aGroup);
                  }
                  $aGroup = array();
                  $oldBillingCycle = $tBillingCycle;
             }

             //truncate
             $tExpectedIncome = $currency->format($this->settings->get('Default Currency'), $this->GetExpectedYearlyIncome($tBillingCycle,$tSumPerBillingCycle), true, false);
             $expectedincomeTotal += $this->GetExpectedYearlyIncome($tBillingCycle,$tSumPerBillingCycle);
             $sumperbillingcycleTotal += $tSumPerBillingCycle;
             $tSumPerBillingCycle = $currency->format($this->settings->get('Default Currency'), $tSumPerBillingCycle, true, false);
             $aGroup[] = array($tNumberOfRecurringItems,$tSumPerBillingCycle,$tExpectedIncome);
             $itemTotal += $tNumberOfRecurringItems;
      }

      //add final group
      if (isset($aGroup)){
          $this->Add($aGroup,$this->GetExtendedName($oldBillingCycle),array($this->user->lang('Items'),$this->user->lang('Sum'),$this->user->lang('Expected Yearly Income')));
          unset($aGroup);
      }

      $expectedincomeTotal = $currency->format($this->settings->get('Default Currency'), $expectedincomeTotal, true, false);
      $sumperbillingcycleTotal = $currency->format($this->settings->get('Default Currency'), $sumperbillingcycleTotal, true, false);
      $aGroup[] = array($itemTotal,$sumperbillingcycleTotal,$expectedincomeTotal);
      $this->Add($aGroup,$this->user->lang('Totals'),array("","",""));

      //Display Report
      $this->Show($this->tpl);
    }

  //*********************************************
  // Custom Function Definitions for this report
  //*********************************************

  //function get the expected yearly income per billing cycle
  function GetExpectedYearlyIncome($billingCycle,$tempSum)
  {
           switch ($billingCycle){
                   case 1:
                        return $tempSum*12;
                        break;
                   case 3:
                        return $tempSum*4;
                        break;
                   case 6:
                        return $tempSum*2;
                        break;
                   case 12:
                        return $tempSum;
                        break;
           }
  }

  //function to translate the digit billing cycle to the preferred header ( e.g. 1 returns Monthly )
  function GetExtendedName($billingCycle)
  {
           switch ($billingCycle){
                   case 1:
                        return $this->user->lang('Monthly');
                        break;
                   case 3:
                        return $this->user->lang('Quarterly');
                        break;
                   case 6:
                        return $this->user->lang('Semiannually');
                        break;
                   case 12:
                        return $this->user->lang('Annual');
                        break;
           }
  }
}

?>
