<?php
session_start();
// - acreports.php
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('acreports');
global $db;
$zid = $_GET['id'];
$comz = $_SESSION['comnum'];
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 id = "'.$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 id = "'.$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'].' - 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('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>Meter Serial Number</td>
            <td>'. testx($result[0]['meternum']).'</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>Physical Address</td>
            <td>'. testx($result[0]['maddr']).'</td>
        </tr>
        <tr>
            <td>City</td>
            <td>'. testx($result[0]['city']).'</td>
            <td>Owner City</td>
            <td>'. testx($result[0]['ocity']).'</td>
            <td>Meter Latitude</td>
            <td>'. testx($result[0]['lat']).'</td>
        </tr>
        <tr>
            <td>State</td>
            <td>'. testx($result[0]['state']).'</td>
            <td>Owner State</td>
            <td>'. testx($result[0]['ostate']).'</td>
            <td>Meter Longitude</td>
            <td>'. testx($result[0]['lon']).'</td>
        </tr>
        <tr>
            <td>Zip</td>
            <td>'. testx($result[0]['zip']).'</td>
            <td>Owner Zip</td>
            <td>'. testx($result[0]['ozip']).'</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></tr>
                <tr>
                    <td>Billing Email</td>
                    <td>'. testx($result[0]['bill_email']).'</td>
                    <td>Owner Email</td>
                    <td>'. testx($result[0]['oemail']).'</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>Billing Phone</td>
                <td>'. testx($result[0]['phone']).'</td>
                <td>Owner Phone</td>
                <td>'. testx($result[0]['ophone']).'</td>
                 <td>Read Order</td>
                <td>'. testx($result[0]['rorder']).'</td>
            </tr>
            <tr>
                <td>Billing Phone 2</td>
                <td>'. testx($result[0]['phone1']).'</td>
                <td></td>
                <td></td>
                <td></td>
                <td></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></td>
                <td></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>$'. testx($result1[0]['bamount']).'</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>$'. testx($result2[0]['pamount']).'</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>$'. testx($result[0]['abalance']).'</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') {
    $sql = 'SELECT * FROM customer WHERE company = "'.
    $_SESSION['comnum'] . '" AND account = "'.$_GET['id'].'"';
    $st = $db->prepare($sql);
    $st->execute();
    $result = $st->fetchAll();
    $xyz = testx($result[0]['name']);
    $xyz1 = testx($result[0]['account']);
    $sql = 'select * from ptypes where company = "'.$_SESSION['comnum'].'"';
    $st = $db->prepare($sql);
    $st->execute();
    $resultx = $st->fetchAll();
    $sql = 'select * from pctypes where company = "'.$_SESSION['comnum'].'"';
    $st = $db->prepare($sql);
    $st->execute();
    $resulty = $st->fetchAll();
    //$sql = 'select * from bill_history where cus_id = "'.$_GET['id'].'"';
    //$st = $db->prepare($sql);
    //$st->execute();
    //$result = $st->fetchAll();
    $result = bill_get_bill_history($_SESSION['comnum'], $_GET['id']);
//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('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>Audit Report for : '.$xyz.'</h2></center>
    <center><h3>Account Number: '.$xyz1.'</h3></center>
    <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>Usage</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) {
          $zxy1='N/A';
            for ($ix=0;$ix<count($resultx);$ix++){                
                $res2 = $resultx[$ix];
                if ($res2['id']==$res1['type']){
                    $zxy1 = $res2['name'];                 
                }   
            }
             $rep1 = $rep1 . '<tr style="background-color: yellowgreen;"><td>'.date('m-d-Y',strtotime($res1[0]['date'])).'</td><td>'.$zxy1.'</td><td>'.$res1['bamount'].
                '</td><td>'.$res1['pamount'].'</td><td>'.$res1['usage'].'</td><td>'.testx($res1['ref1']).
                '</td><td>'.testx($res1['note1']).'</td><td>'.$res1['balance'].'</td></tr>';   
        } else if ($res1['bamount']>0) {
            $zxy2='N/A';
            for ($iz=0;$iz<count($resulty);$iz++){
                $res2 = $resulty[$iz];
                if ($res2['id']==$res1['type']){
                    $zxy2 = $res2['name']; 
                }   
            }
             $rep1 = $rep1 .  '<tr style="background-color: yellow;"><td>'.date('m-d-Y',strtotime($res1['date'])).'</td><td>'.$zxy2.'</td><td>'.$res1['bamount'].
                '</td><td>'.$res1['pamount'].'</td><td>'.$res1['usage'].'</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>'.$res1['usage'].'</td><td>'.testx($res1['ref1']).
                '</td><td>'.testx($res1['note1']).'</td><td>'.$res1['balance'].'</td></tr>';
        }
    }
    $rep1 = $rep1 . '</table></center>';
        $pdf->SetFont('helvetica', '', 12);
        $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');   
}
?>

