<?php
/*
************************************************
*   1.0 Initial Report Released.  - Alberto Vasquez                                     *
*   1.1 Fixed to account for package and billing types.  - Alberto Vasquezs             *
*   1.2 Only include for active customers.  - Alberto Vasquezs             *
************************************************
*/

// don't show if not logged in
if (!isset($_SESSION['userId'])) die('Log in first');

  $report_version = "1.2";
  $report = new Report();
  $report->SetDescription(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
                WHERE r.customerid=u.id and u.status=1
                GROUP BY r.paymentterm";
  $result = $GLOBALS['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
                  $report->Add($aGroup,GetExtendedName($oldBillingCycle),array(lang('Items'),lang('Sum'),lang('Expected Yearly Income')));
                  unset($aGroup);
              }
              $aGroup = array();
              $oldBillingCycle = $tBillingCycle;
         }

         //truncate
         $tExpectedIncome = ShowCurrencySymbol($GLOBALS['gSettingsArray']['Default Currency'],false).sprintf("%01.2f", GetExpectedYearlyIncome($tBillingCycle,$tSumPerBillingCycle));
         $expectedincomeTotal += GetExpectedYearlyIncome($tBillingCycle,$tSumPerBillingCycle);
         $sumperbillingcycleTotal += $tSumPerBillingCycle;
         $tSumPerBillingCycle = ShowCurrencySymbol($GLOBALS['gSettingsArray']['Default Currency'],false).sprintf("%01.2f",$tSumPerBillingCycle);
         $aGroup[] = array($tNumberOfRecurringItems,$tSumPerBillingCycle,$tExpectedIncome);
         $itemTotal += $tNumberOfRecurringItems;
  }

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

  $expectedincomeTotal = ShowCurrencySymbol($GLOBALS['gSettingsArray']['Default Currency'],false).sprintf("%01.2f",$expectedincomeTotal);
  $sumperbillingcycleTotal = ShowCurrencySymbol($GLOBALS['gSettingsArray']['Default Currency'],false).sprintf("%01.2f",$sumperbillingcycleTotal);
  $aGroup[] = array($itemTotal,$sumperbillingcycleTotal,$expectedincomeTotal);
  $report->Add($aGroup,lang('Totals'),array("","",""));

  //Display Report
  $report->Show();

  //*********************************************
  // 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 lang('Monthly');
                        break;
                   case 3:
                        return lang('Quarterly');
                        break;
                   case 6:
                        return lang('Semiannually');
                        break;
                   case 12:
                        return lang('Annual');
                        break;
           }
  }

?>
