<?php
session_start();
require_once "main.php";
//ini_set('display_errors', 1);
//error_reporting(E_ALL);
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
Logger::configure('log4php.xml');
$log = Logger::getLogger('printbatch');
global $db;
if ($_GET['submitType']=='totals') {
$group0 = bill_get_tot_group0($_SESSION['comnum']);
//print_r($group0);
ob_end_clean();
$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
$pdf = new TCPDF("P", "pt", "USLETTER", true, 'UTF-8', false);
$pdf->SetAutoPageBreak("true");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('ProTekBill');
$pdf->SetTitle('Account Report');
$pdf->SetSubject($_GET['id']);
$pdf->SetKeywords('ProTekBill');
$style = array(
	'position' => '',
	'align' => 'L',
	'stretch' => false,
	'fitwidth' => false,
	'cellfitalign' => '',
	'border' => true,
	'hpadding' => 'auto',
	'vpadding' => 'auto',
	'fgcolor' => array(0,0,0),
	'bgcolor' => false, //array(255,255,255),
	'text' => false,
	'font' => 'helvetica',
	'fontsize' => 10,
	'stretchtext' => 4
);
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 6);
$resolution= array(792,612);//array(792, 612);
$pdf->AddPage("P", $resolution);


$rep1 = '<center><h2>System Totals Report</h2></center>
    <center><h3>'.$_SESSION['name'].'name</h3></center>
    <table cellpadding="1" border="0" style="text-align:right;">
    <tr>
        <td></td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Water</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Sewer</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Return Ck & Fee</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Late Charge</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Reconnect Fee</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Tag Fee</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Sales Tax</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Payment Agreement</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Total Adjustments Etc</td>
        <td></td>
        <td style="border-bottom: .75px solid black;"><b># of Accounts</b></td>
        <td style="border-bottom: .75px solid black;"><b>Amount($)</b></td>
    </tr>
    <tr>
        <td><b>Total Current Charges</b></td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr><td colspan="4" style="text-align:right;"> </td>
    </tr>
    <tr>
        <td>Current Balance</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Past Due 1-30 Days</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Past Due 31-60 Days</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Past Due Over 60 Days</td>
        <td></td>
        <td style="border-bottom: .75px solid black;"><b># of Accounts</b></td>
        <td style="border-bottom: .75px solid black;"><b>Amount($)</b></td>
    </tr>
    <tr>
        <td><b>Total Recievables</b></td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr><td colspan="4" style="text-align:right;"> </td>
    </tr>
    <tr>
        <td>Total Payments Received</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Active Accounts</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Inactive Accounts</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Collection Accounts</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr><td colspan="4" style="text-align:right;"> </td>
    </tr>
    <tr>
        <td>Average Usage</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Average Water Charge</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr><td colspan="4" style="text-align:right;"> </td>
    </tr>
    <tr>
        <td>Water Produced</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Water Purchased</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td>Water Sold</td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    <tr>
        <td style="border-bottom: .75px solid black;">Accountable Loss</td>
        <td style="border-bottom: .75px solid black;"></td>
        <td style="border-bottom: .75px solid black;"><b># of Accounts</b></td>
        <td style="border-bottom: .75px solid black;"><b>Amount($)</b></td>
    </tr>
    <tr>
        <td><b>Water Loss</b></td>
        <td></td>
        <td><b># of Accounts</b></td>
        <td><b>Amount($)</b></td>
    </tr>
    </table>
    <table style="text-align:right;">
    <tr><td colspan="6" style="text-align:right;"> </td>
    </tr>
    <tr>
        <td style="border-bottom: .5px solid black; text-align:left;"><b>Usage Groups</b></td>
        <td style="border-bottom: .5px solid black;"><b># of Accounts</b></td>
        <td style="border-bottom: .5px solid black;"><b>Usage</b></td>
        <td style="border-bottom: .5px solid black;"><b>Sales</b></td>
        <td style="border-bottom: .5px solid black;"><b>% Usage</b></td>
        <td style="border-bottom: .5px solid black;"><b>% Sales</b></td>
    </tr>
        <tr>
        <td style="text-align:left;">0 Usage</td>
        <td>'.number_format ($group0[0]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[0]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[0]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[0]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[0]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
        <tr>
        <td style="text-align:left;">1-2,000</td>
        <td>'.number_format ($group0[1]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[1]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[1]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[1]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[1]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
        <tr>
        <td style="text-align:left;">2,001-4,000</td>
        <td>'.number_format ($group0[2]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[2]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[2]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[2]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[2]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
        <tr>
        <td style="text-align:left;">4,001-6,000</td>
        <td>'.number_format ($group0[3]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[3]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[3]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[3]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[3]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
        <tr>
        <td style="text-align:left;">6,001-8,000</td>
        <td>'.number_format ($group0[4]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[4]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[4]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[4]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[4]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
        <tr>
        <td style="text-align:left;">8,001-10,000</td>
        <td>'.number_format ($group0[5]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[5]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[5]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[5]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[5]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
        <tr>
        <td style="text-align:left;">10,001-20,000</td>
        <td>'.number_format ($group0[6]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[6]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[6]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[6]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[6]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
    <tr>
        <td style="text-align:left;">20,001-30,000</td>
        <td>'.number_format ($group0[7]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[7]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[7]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[7]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[7]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
    <tr>
        <td style="text-align:left;">30,001-40,000</td>
        <td>'.number_format ($group0[8]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[8]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[8]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[8]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[8]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
    <tr>
        <td style="text-align:left;">40,001-50,000</td>
        <td>'.number_format ($group0[9]['cnt1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[9]['use1'],0 , '.' , ',' ).'</td>
        <td>'.number_format ($group0[9]['sale1'],2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[9]['u1']*100),2 , '.' , ',' ).'</td>
        <td>'.number_format (($group0[9]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
    <tr>
        <td style="border-bottom: .75px solid black; text-align:left;">Over 50,000</td>
        <td style="border-bottom: .75px solid black;">'.number_format ($group0[10]['cnt1'],0 , '.' , ',' ).'</td>
        <td style="border-bottom: .75px solid black;">'.number_format ($group0[10]['use1'],0 , '.' , ',' ).'</td>
        <td style="border-bottom: .75px solid black;">'.number_format ($group0[10]['sale1'],2 , '.' , ',' ).'</td>
        <td style="border-bottom: .75px solid black;">'.number_format (($group0[10]['u1']*100),2 , '.' , ',' ).'</td>
        <td style="border-bottom: .75px solid black;">'.number_format (($group0[10]['s1']*100),2 , '.' , ',' ).'</td>
    </tr>
    <tr>
        <td style="text-align:left;"><b>Total Meters</b></td>
        <td>'.$group0[11]['cnt1'].'</td>
        <td>'.$group0[11]['use1'].'</td>
        <td>'.number_format ($group0[11]['sale1'],2 , '.' , ',' ).'</td>
        <td>100.00</td>
        <td>100.00</td>
    </tr>
    </table>';

$rep3=$rep1;
        $pdf->SetFont('helvetica', '', 10);
        $pdf->writeHTML($rep3, true, false, true, false, '');
        //$pdf->writeHTML($rep2, true, false, true, false, '');
        //$pdf->writeHTMLcell(0,0,5,5,$rep1, false, true, false, false, false, '');//address
        $pdf->lastPage();
       
$pdf->Output('MeterReadWKSHT.pdf', 'I');
} else if ($_GET['submitType']=='reread') {
$result = bill_get_reread($_SESSION['comnum'], $_SESSION['batch']);
//print_r($result);
ob_end_clean();
$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
$pdf = new TCPDF("P", "pt", "USLETTER", true, 'UTF-8', false);
$pdf->SetAutoPageBreak("true");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('ProTekBill');
$pdf->SetTitle('Account Report');
$pdf->SetSubject($_GET['id']);
$pdf->SetKeywords('ProTekBill');
$style = array(
	'position' => '',
	'align' => 'L',
	'stretch' => false,
	'fitwidth' => false,
	'cellfitalign' => '',
	'border' => true,
	'hpadding' => 'auto',
	'vpadding' => 'auto',
	'fgcolor' => array(0,0,0),
	'bgcolor' => false, //array(255,255,255),
	'text' => false,
	'font' => 'helvetica',
	'fontsize' => 10,
	'stretchtext' => 4
);
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 6);
$resolution= array(792,612);//array(792, 612);
$pdf->AddPage("P", $resolution);


$rep1 = '<center><h2>ReRead Worksheet for batch # : '.testx($_SESSION['batch']).'</h2></center>
    <table cellpadding="3" cellspacing="1" border="1" style="text-align:center;">
    <tr>
        <td>Account</td>
        <td>Name</td>
        <td>Address</td>
        <td>2M Reading</td>
        <td>Last Reading</td>
        <td>Reading</td>
    </tr>';
$rep2 = '';
for ($i=0;$i<count($result);$i++) {
$rep2 = $rep2 .'
    <tr>
       <td>'.testx($result[$i]['account']).'</td>
       <td>'.testx($result[$i]['name']).'</td>
       <td>'.testx($result[$i]['maddr']).'</td>
       <td>'.testx($result[$i]['lmread']).'</td>
       <td>'.testx($result[$i]['lread']).'</td>
       <td></td>
    </tr>';
}
$rep3=$rep1.$rep2.'</table>';
        $pdf->SetFont('helvetica', '', 10);
        $pdf->writeHTML($rep3, true, false, true, false, '');
        //$pdf->writeHTML($rep2, true, false, true, false, '');
        //$pdf->writeHTMLcell(0,0,5,5,$rep1, false, true, false, false, false, '');//address
        $pdf->lastPage();
       
$pdf->Output('MeterReadWKSHT.pdf', 'I');    
}
?>
