<?php

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

    function process()
    {
        /**************************************************************************
        *===================Monthly Invoices Generated Report=====================*
        *                                                                         *
        * This report will tell you give you the invoices generated each month.   *
        * Author: Kevin Irving                                                    *
        * Created: January 2005                                                   *
        ***************************************************************************/
        
        //Class Initialize
         
          $this->SetDescription($this->user->lang('Displays all invoices generated for each month (click on a month to see the invoices for that month).'));
        
        //Get total number of invoices generated
        $sql = "SELECT COUNT(*) FROM invoice";
        $result = $this->db->query($sql);
        $num = $result->fetch();
        
        //Now we query for all invoices all time
        $sql = "SELECT "
              ."DATE_FORMAT( billdate, '%M,%Y' ) AS date_generated, "
              ."billdate AS date_unformatted, "
              ."COUNT(*) AS opened "
              ."FROM invoice "
              ."GROUP BY DATE_FORMAT( billdate, '%M,%Y' ) "
              ."ORDER BY DATE_FORMAT( billdate, '%Y' ) ASC, DATE_FORMAT(billdate, '%m') ASC";
        $result = $this->db->query($sql);
        while (list($month,$unformatted,$invoices) = $result->fetch()){
            $basePage = $_SERVER["REQUEST_URI"];
            $nPos = strpos($basePage, "&month=");
            if ( $nPos === false ) {
                $basePage .= "&month=";
            } else {
                $basePage = substr($basePage, 0, $nPos) . "&month=";
            }
            $monthLi = substr($unformatted, 0, 7);
            $month = "<a href=\"" . $basePage . $monthLi . "\">" . $month . "</a>";
            $aGroup[] = array($month,$invoices);
          }
        
          //add final group
          if (isset($aGroup)){
              $this->Add($aGroup,"",array($this->user->lang('Month'),$this->user->lang('Invoices Generated')));
              unset($aGroup);
          }
        
          $aGroup[] = array("<b>" . $num[0] . "</b>");
          $this->Add($aGroup,$this->user->lang('Totals'),array($this->user->lang('Year-to-date')));
          unset($aGroup);
        
        
        if(@$_GET['month']) {
        //Now we query for all invoices in this month
        $sql = "SELECT id, customerid, DATE_FORMAT(billdate, '%M,%D %Y'), DATE_FORMAT(sentdate, '%M %D, %Y'), status, sent FROM invoice WHERE SUBSTRING(billdate, 1, 7) = ?";
        $result = $this->db->query($sql, $_GET['month']);
        while($currRow = $result->fetch()) {
                $userid = $currRow[1];
                $tUser =& new User($userid);
                 if ($tUser->IsOrganization() == true) {
                    $customer = $tUser->getOrganization();
                    if (strlen($customer) > 20) {
                        $title = "title=\"$customer\"";
                        $dusername = substr($customer, 0,20) . "...";
                        $customer =  "<a href=\"index.php?fuse=clients&amp;frmClientID=". $userid ."&amp;view=ShowCustomerData\" $title>" .$dusername."</a>";
                    } else {
                        $title = "";
                        $customer =  "<a href=\"index.php?fuse=clients&amp;frmClientID=". $userid ."&amp;view=ShowCustomerData\" $title>" .$customer."</a>";
                    }
                } else {
                    $customer = $tUser->getFullName();
                    $title = "";
                    $customer =  "<a href=\"index.php?fuse=clients&amp;frmClientID=". $userid ."&amp;view=ShowCustomerData\" $title>" .$customer."</a>";
                }
                $aGroup[] = array($currRow[0], $customer, $currRow[2], $currRow[3], $this->user->varLang($this->YesNo($currRow[4])), $this->user->varLang($this->YesNo($currRow[5])));
          }
        
        $this->Add($aGroup,"",array($this->user->lang('Invoice ID'), $this->user->lang('Customer'), $this->user->lang('Due Date'), $this->user->lang('Sent Date'), $this->user->lang('Paid'), $this->user->lang('Sent')));
        
        $this->Show();
        
        
        
        
        } else {
        
          //Display Report
          $this->Show();
        
        }
    }
    
    function YesNo($int) {
        if($int==1) {
            return "Yes";
        } else {
            return "No";
        }
    }
}
?>
