<?PHP
/**************************************************************************
*===================Monthly Invoices Generated Report=====================*
*                                                                         *
* This report will tell you give you the invoices generated each month.   *
* Author: Kevin Irving                                                    *
* Created: January 2005                                                   *
***************************************************************************/

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

//Class Initialize
 $report = new Report();
  $report->SetDescription(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 = $GLOBALS['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 = $GLOBALS['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)){
      $report->Add($aGroup,"",array(lang('Month'),lang('Invoices Generated')));
      unset($aGroup);
  }

  $aGroup[] = array("<b>" . $num[0] . "</b>");
  $report->Add($aGroup,lang('Totals'),array(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'), paid, sent FROM invoice WHERE SUBSTRING(billdate, 1, 7) = ?";
$result = $GLOBALS['db']->query($sql, $_GET['month']);
while($currRow = $result->fetch()) {
        $userid = $currRow[1];
        $tUser =& new User($userid);
        $tUser->PopulateCustomFields();
         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=3&frmClientID=". $userid ."&action=0\" $title>" .$dusername."</a>";
            } else {
                $title = "";
                $customer =  "<a href=\"index.php?fuse=3&frmClientID=". $userid ."&action=0\" $title>" .$customer."</a>";
            }
        } else {
            $customer = $tUser->getFullName();
            $title = "";
            $customer =  "<a href=\"index.php?fuse=3&frmClientID=". $userid ."&action=0\" $title>" .$customer."</a>";
        }
        $aGroup[] = array($currRow[0], $customer, $currRow[2], $currRow[3], varLang(YesNo($currRow[4])), varLang(YesNo($currRow[5])));
  }

$report->Add($aGroup,"",array(lang('Invoice ID'), lang('Customer'), lang('Due Date'), lang('Sent Date'), lang('Paid'), lang('Sent')));

$report->Show();




} else {

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

}

function YesNo($int) {
    if($int==1) {
        return "Yes";
    } else {
        return "No";
    }
}


?>
