<?php
/**
 * Billing Cycle Income Report
 *
 * @category Report
 * @package  ClientExec
 * @author   Jason Yates <jason@clientexec.com>
 * @license  ClientExec License
 * @version  1.1
 * @link     http://www.clientexec.com
 *
 *************************************************
 *   1.0 Initial Report Released
 *   1.1 Updated report to include a title & PEAR commenting
 ************************************************
 */

require_once 'modules/billing/models/Currency.php';

/**
 * Billing_Cycle_Income Report Class
 *
 * @category Report
 * @package  ClientExec
 * @author   Jason Yates <jason@clientexec.com>
 * @license  ClientExec License
 * @version  1.1
 * @link     http://www.clientexec.com
 */
class Billing_Cycle_Income extends Report
{

    /**
     * Report Process Method
     *
     * @return null - direct output
     */
    function process()
    {
        // Set the report information
        $this->setName('Billing Cycle Income');
        $this->setVersion("1.1");
        $this->SetDescription($this->user->lang('Displays total recurring transactions broken down by billing cycles with the sum and expected yearly income from each.'));

        // Load the currency information
        $currency = new Currency($this->user);

        //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 "
                ."AND r.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
     *
     * @return var - estimated income
     */
    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 )
     *
     * @return var - header item
     */
    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;
        }
    }
}

?>
