<?php
require_once 'classes/Currency.php';

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

    function process()
    {
        //** Total Income to Date Report  **//
        //** Written By: Bart Wegrzyn     **//
        //** Written For: ClientExec v1.1 **//
          
          //GLOBALs available to you
          $currency = new Currency($this->user);
          $this->SetDescription($this->user->lang('Displays all paid invoices to date, with total income.'));
          $this->showReportHeader();
          $groupHeader = array(
            'name'      => $this->user->lang('Invoices'),
            'groupid'   => 'idInvoices',
            'hidden'    => false,
            'labels'    => array($this->user->lang('Invoice #'),$this->user->lang('Date Paid'),$this->user->lang('Invoice Amount'), $this->user->lang('Tax Charged')),
          );
          $this->showGroupHeader($groupHeader);
          //SQL to generate the the result set of the report
          $reportSQL = "SELECT id, description, amount, subtotal, datepaid, status, tax FROM `invoice` WHERE status='1' ORDER BY id";
          $result = $this->db->query($reportSQL);
        
          //initialize
          $TotalIncome = 0;
          $TotalInvoices = 0;
          $totalTaxCharged = 0;
          while (list($tInvoiceId,$tDescription,$tAmount,$tSubTotal,$tDatePaid,$tPaid,$tTaxRate) = $result->fetch()){
                 $tInvoiceAmount = $currency->format($this->settings->get('Default Currency'), $tAmount, true, false);
                 $TotalIncome += $tAmount;
                 $TotalInvoices += 1;
                 $taxCharged = ($tAmount - $tSubTotal);
                 $totalTaxCharged += $taxCharged;
                 $this->showRowItem(array($tDescription,$tDatePaid,$tInvoiceAmount, $currency->ShowCurrencySymbol(null,$this->settings->get('Default Currency'),false).sprintf("%01.2f", $taxCharged)));
          }
          $this->showGroupFooter();
        
          //Add 'Invoices' groud
        
          $aGroup = array();
          $totalIncomeMinusTax = $currency->format($this->settings->get('Default Currency'), ($TotalIncome - $totalTaxCharged), true, false);
          $TotalIncome = $currency->format($this->settings->get('Default Currency'), $TotalIncome, true, false);
          $totalTaxCharged = $currency->format($this->settings->get('Default Currency'), $totalTaxCharged, true, false);
          $aGroup[] = array($TotalInvoices,$TotalIncome, $totalTaxCharged, "<b>" .$totalIncomeMinusTax. "</b>");
          $this->Add($aGroup,$this->user->lang('Totals'),array($this->user->lang('Total Paid Invoices'),$this->user->lang('Gross Income'), $this->user->lang('Total Taxes'), $this->user->lang('Real Income')));
          $this->show();
          
          $this->showReportFooter(true);
        
    }
}
?>
