<?php
session_start();
// - acreports.php
require_once "main.php";
ini_set('display_errors', 0);
error_reporting(0);
//require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
Logger::configure('log4php.xml');
$log = Logger::getLogger('acreports');
global $db;
$zid = $_GET['id'];
$comz = $_SESSION['comnum'];
$zxy1 = '';
$zxy2 = '';
if ($_GET['submitType']=='cust') {
    $sql = 'SELECT * FROM customer WHERE company = "'. $comz . '" AND account = "'.$zid.'"';
    $st = $db->prepare($sql);
    $st->execute();
    $result = $st->fetchAll();
    $sql = 'SELECT * FROM bill_history WHERE company = "'. $comz . '" AND masterid = "'.$zid.'" AND bamount>"0" ORDER BY date desc LIMIT 1';
    $st = $db->prepare($sql);
    $st->execute();
    $result1 = $st->fetchAll();
    $sql = 'SELECT * FROM bill_history WHERE company = "'. $comz . '" AND masterid = "'.$zid.'" AND pamount>"0" ORDER BY date desc LIMIT 1';
    $st = $db->prepare($sql);
    $st->execute();
    $result2 = $st->fetchAll();
//print_r($result);
    ob_end_clean();
    //$log->info($_GET['submitType']);
    $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('WtrBill');
    $pdf->SetTitle('Account Report');
    $pdf->SetSubject($_GET['id']);
    $pdf->SetKeywords('WtrBill');
    $style = array(
        'position' => '',
        'align' => 'L',
        'stretch' => false,
        'fitwidth' => false,
        'cellfitalign' => '',
        'border' => false,
        '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>Detail Report for : '.testx($result[0]['name']).'</h2></center>
        <center><h3>Account Number: '.testx($result[0]['account']).'</h3></center>
        <table cellpadding="3" cellspacing="1" border="1" style="text-align:center;">
        <tr>
            <td>Billing Name</td><td>'.testx($result[0]['name']).'</td>
            <td>Owner Name</td><td>'. testx($result[0]['oname']).'</td>
            <td>Account Number</td><td>'. testx($result[0]['account']).'</td>
        </tr>
        <tr>
            <td>Address</td><td>'. testx($result[0]['addr']).'</td>
            <td>Owner Address</td><td>'. testx($result[0]['oaddr']).'</td>
            <td>Physical Address</td><td>'. testx($result[0]['maddr']).'</td>
        </tr>
        <tr>
            <td>Address 1</td><td>'. testx($result[0]['addr1']).'</td>
            <td>Owner Address 1</td><td>'. testx($result[0]['oaddr1']).'</td>
            <td>Meter Serial Number</td><td>'. testx($result[0]['meternum']).'</td>
        </tr>
        <tr>
            <td>City</td><td>'. testx($result[0]['city']).'</td>
            <td>Owner City</td><td>'. testx($result[0]['ocity']).'</td>
            <td>Meter Type</td><td>'. testx($result[0]['metertype']).'</td>
        </tr>
        <tr>
            <td>State</td><td>'. testx($result[0]['state']).'</td>
            <td>Owner State</td><td>'. testx($result[0]['ostate']).'</td>
            <td>Radio Serial Number</td><td>'. testx($result[0]['metermiu']).'</td>
        </tr>
        <tr>
            <td>Zip</td><td>'. testx($result[0]['zip']).'</td>
            <td>Owner Zip</td><td>'. testx($result[0]['ozip']).'</td>
            <td>Meter Latitude</td><td>'. testx($result[0]['lat']).'</td>
        </tr>
        <tr>
            <td>Billing Email</td><td>'. testx($result[0]['bill_email']).'</td>
            <td>Owner Email</td><td>'. testx($result[0]['oemail']).'</td>
            <td>Meter Longitude</td><td>'. testx($result[0]['lon']).'</td>
        </tr>
        <tr>
            <td>Billing Phone</td><td>'. testx($result[0]['phone']).'</td>
            <td>Owner Phone</td><td>'. testx($result[0]['ophone']).'</td>
            <td>Web User Name</td><td>'. testx($result[0]['customer_username']).'</td>
        </tr>
        <tr>
            <td>Billing Phone 2</td><td>'. testx($result[0]['phone1']).'</td>
            <td>Customer Type</td><td>';
                 if ($result[0]['ctype']==6){
                     $rep1 = $rep1 . 'Bill to All Addresses';
                } else if ($result[0]['ctype']==2){
                    $rep1 = $rep1 . 'Bill to Both mail';
                   } else if ($result[0]['ctype']==3){
                       $rep1 = $rep1 . 'Bill to Billing email';
                   } else if ($result[0]['ctype']==4){
                       $rep1 = $rep1 . 'Bill to Both email';
                   } else if ($result[0]['ctype']==5){
                       $rep1 = $rep1 . 'Bill to Both Billing';
                   } else {
                    $rep1 = $rep1 . 'Bill to Billing mail';
                   }
                $rep1 = $rep1 . '</td>
                <td>Read Order</td><td>'. testx($result[0]['rorder']).'</td>
            </tr>
            <tr>
                 <td>CASS Sequence</td>
                 <td>'. testx(@$result[0]['casssequence']).'</td>
                 <td>Owner Cass Sequence</td>
                 <td>'. testx(@$result[0]['ocasssequence']).'</td>
                 <td></td>
                <td></td>
             </tr>
             <tr>
                 <td>Delivery Barcode</td>
                 <td>'. testx(@$result[0]['delbarcode']).'</td>
                 <td>Owner Del Barcode</td>
                 <td>'. testx(@$result[0]['odelbarcode']).'</td>
                 <td>Account Active</td><td>';
                if ($result[0]['active']==1){
                       $rep1 = $rep1 . 'Active';
                } else {
                      $rep1 = $rep1 . 'Not Active';
                  }
                $rep1 = $rep1 .'</td>
             </tr>
             <tr>
                 <td>CASS Deliverable</td>
                 <td>'. testx(@$result[0]['cassDeliverable']).'</td>
                 <td>Owner CASS Deliverable</td>
                 <td>'. testx(@$result[0]['ocassdeliverable']).'</td>
                 <td>Last Charge</td>
                 <td>$'. number_format(testxint(@$result1[0]['bamount']),2).'</td>
             </tr>
            <tr>
                <td>Last Month Read</td>
                <td>'. testx(@$result[0]['lmread']).'</td>
                <td>Last Read</td>
                <td>'. testx(@$result[0]['lread']).'</td>
                <td>Last Credit</td>
                <td>$'. number_format(testxint(@$result2[0]['pamount']),2).'</td>
            </tr>
            <tr>
                <td>Last Month Usage</td>
                <td>'. testx(@$result[0]['lmuse']).'</td>
                <td>Last Usage</td>
                <td>'. testx(@$result[0]['luse']).'</td>
                <td>Account Balance</td>
                <td>$'. number_format(testxint(@$result[0]['abalance']),2).'</td>
            </tr>
        </table>
        <h3>Account Note : '. testx($result[0]['anote']).'<h3>';
        $pdf->SetFont('helvetica', '', 10);
        $pdf->writeHTML($rep1, 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('AccountDetail'.$_GET['id'].'.pdf', 'I');
} 
else if ($_GET['submitType']=='audit') {
    $result = bill_get_customer($_SESSION['comnum'], $_GET['id']);
    $xyz = testx($result[0]['name']);
    $xyz1 = testx($result[0]['account']);
    $resultx = bill_get_ptypes($_SESSION['comnum']);
    $resulty = bill_get_pctypes($_SESSION['comnum']);
    $resultz = bill_get_rates($_SESSION['comnum']);
    $result = bill_get_bill_history($_SESSION['comnum'], $_GET['id']);
    ob_end_clean();
    $z1 = 'Account # '.$_GET['id'].' Audit';
    $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('WtrBill');
    $pdf->SetTitle($z1);
    $pdf->SetSubject($_GET['id']);
    $pdf->SetKeywords('WtrBill');
    $style = array(
        'position' => '',
        'align' => 'L',
        'stretch' => false,
        'fitwidth' => false,
        'cellfitalign' => '',
        'border' => false,
        'hpadding' => 'auto',
        'vpadding' => 'auto',
        'fgcolor' => array(0,0,0),
        'bgcolor' => false, //array(255,255,255),
        'text' => false,
        'font' => 'helvetica',
        'fontsize' => 6,
        'stretchtext' => 4
        );
    $pdf->setFontSubsetting(true);
    $pdf->SetFont('helvetica', '', 6);
    $resolution= array(792,612);//array(792, 612);
    $pdf->AddPage("P", $resolution);
    $rep1 = '<h3>Audit Report for : '.$xyz.' Account Number: '.$xyz1.'</h3>
    <center>
    <table cellpadding="3" cellspacing="1" border="1" style="text-align:center;">
        <tr>
            <th>Date</th>
            <th>Type</th>
            <th>Debit Amount</th>
            <th>Credit Amount</th>
            <th>Reference</th>
            <th>Note</th>
            <th>Balance</th>
        </tr>';
    //extract($result);
    for ($i=0;$i<count($result);$i++) {
        $res1 = $result[$i];        
        if ($res1['pamount']>0) {
            for ($ix=0;$ix<count($resultx);$ix++){                
                $res2 = $resultx[$ix];
                if ($res2['id'] = $res1['type']){
                    $zxy1 = $res2['name'];                 
                }   
            }
             $rep1 = $rep1 . '<tr style="background-color:#90ff83;"><td>'.date('m-d-Y',strtotime($res1['date'])).
             '</td><td>'.$zxy1.'</td><td>$'.$res1['bamount'].
                '</td><td>$'.$res1['pamount'].'</td><td>'.testx($res1['ref1']).
                '</td><td>'.testx($res1['note1']).'</td><td>$'.$res1['balance'].'</td></tr>';
        } else if ($res1['bamount']>0) {
                if ($res1['type']<>999) {
                for ($iz=0;$iz<count($resulty);$iz++){
                    $res2 = $resulty[$iz];
                    if ($res2['id'] = $res1['type']){
                        $zxy2 = $res2['name'];
                    }
                }
                } else {
                for ($iz1=0;$iz1<count($resultz);$iz1++){
                    $res2 = $resultz[$iz1];
                    if ($res2['id'] = $res1['rcode']){
                        $zxy2 = $res2['name'];
                    }
                }
                }
             $rep1 = $rep1 .  '<tr style="background-color:#fdfec2;"><td>'.date('m-d-Y',strtotime($res1['date'])).
             '</td><td>'.$zxy2.'</td><td>$'.$res1['bamount'].
                '</td><td>$'.$res1['pamount'].'</td><td>'.testx($res1['ref1']).
                '</td><td>'.testx($res1['note1']).'</td><td>$'.$res1['balance'].'</td></tr>';
        } else {
            $rep1 = $rep1 .  '<tr><td>'.$res1['date'].'</td><td>'.$res1['type'].'</td><td>$'.$res1['bamount'].
                '</td><td>$'.$res1['pamount'].'</td><td>'.testx($res1['ref1']).
                '</td><td>'.testx($res1['note1']).'</td><td>$'.$res1['balance'].'</td></tr>';
        }
    }
    $rep1 = $rep1 . '</table></center>';
        $pdf->SetFont('helvetica', '', 10);
        $pdf->writeHTML($rep1, 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('AccountAudit'.$_GET['id'].'.pdf', 'I');   
}
?>

