<?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('woprint');
if ($_GET['submitType']=='workorder') {
$result = bill_get_worder($_GET['id']);
$mresult = bill_get_customer3($result[0]['masterid']);
$resx = bill_get_company($_SESSION['comnum']);
$zzzzz = ob_get_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' => 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 = '<h3>'.testx($resx[0]['name']).'</h3>
    <p>'.testx($resx[0]['addr']).'<br />'.testx($resx[0]['addr1']).'<br />'
        .testx($resx[0]['city']).', '.testx($resx[0]['state']).'  '.testx($resx[0]['zip']).'</p>
    <h3>Work Order #'.testx($result[0]['id']).'</h3>
    <h3>Account Name :'.testx($mresult[0]['name']).'</h3>        
    <table cellpadding="3" cellspacing="1" border="1" style="text-align:center;">
        <tr>
            <td>Account</td>
            <td>'.testx($mresult[0]['account']).'</td>
            <td>Meter ID</td>
            <td>'.testx($mresult[0]['meternum']).'</td>
        </tr>
        <tr>
            <td>Address</td>
            <td>'.testx($result[0]['addr']).'</td>
            <td>Meter Address</td>
            <td>'.testx($mresult[0]['maddr']).'</td>
        </tr>
        <tr>
            <td>Problem</td>
            <td>'.testx($result[0]['rinfo1']).'</td>
            <td>Problem ext.</td>
            <td>'.testx($result[0]['rinfo2']).'</td>
        </tr>
        <tr>
            <td>Completion</td>
            <td>'.testx($result[0]['cinfo1']).'</td>
            <td>Latitude</td>
            <td>'.testx($mresult[0]['lat']).'</td>
        </tr>
        <tr>
            <td>Completion ext</td>
            <td>'.testx($result[0]['cinfo2']).'</td>
            <td>Longitude</td>
            <td>'.testx($mresult[0]['lon']).'</td>
        </tr>
        <tr>
            <td>Assigned to</td>
            <td>'.testx($result[0]['rtech']).'</td>
            <td>
            </td>
            <td>
            </td>
        </tr>
        <tr>
            <td>Entered by</td>
            <td>'.testx($result[0]['rentered']).'</td>
            <td>Date Entered</td>
            <td>'.date('m/d/Y', strtotime($result[0]['rdate'])).'</td>
        </tr>
        <tr>
            <td>Completed By</td>
            <td>'.testx($result[0]['centered']).'</td>
            <td>Date Completed</td>
            <td>'.date('m/d/Y', strtotime($result[0]['cdate'])).'</td>
        </tr>
    </table>
    <table cellpadding="4" cellspacing="1" border="1" style="text-align:center;">
        <tr>
            <th width="80%">Work / Part Description</th>
            <th width="20%">Cost</th>
        </tr>
        <tr>
            <td width="80%">'.testx($result[0]['part1']).'</td>
            <td width="20%">'.testxint($result[0]['cost1']).'</td>
        </tr>
        <tr>
            <td>'.testx($result[0]['part2']).'</td>
            <td>'.testxint($result[0]['cost2']).'</td>
        </tr>
        <tr>
            <td>'.testx($result[0]['part3']).'</td>
            <td>'.testxint($result[0]['cost3']).'</td>
        </tr>
        <tr>
            <td>'.testx($result[0]['part4']).'</td>
            <td>'.testxint($result[0]['cost4']).'</td>
        </tr>
        <tr>
            <td>'.testx($result[0]['part5']).'</td>
            <td>'.testxint($result[0]['cost5']).'</td>
        </tr>
        <tr>
            <td>Total Cost</td>
            <td>'.testxint($result[0]['cost_total']).'</td>
        </tr>
        <tr>
            <td>Comments</td>
            <td>'.testx($result[0]['comment']).'</td>
        </tr></table>
        <p>You may include any additional informaiton below:</p>';
$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('workorder'.$_GET['id'].'.pdf', 'I');
} else if ($_GET['submitType']=='list') {
    $result = bill_get_worders($_SESSION['comnum'], $_GET['id']);
    $mresult = bill_get_customer($_SESSION['comnum'], $_GET['id']);
    $rex2 = '';
    $rex1 = '<h3>Work Order List for Account :'.testx($mresult[0]['name']).'  -  '.testx($mresult[0]['account']).'</h3>        
    <table cellpadding="3" cellspacing="1" border="1" style="text-align:center;">
        <tr>
            <th>Work Order ID</th>
            <th>Entered Date</th>
            <th>Completed Date</th>
            <th>Last Updated Date</th>
            <th>Description of Problem</th>
        </tr>';  
    for ($i=0;$i<count($result);$i++) {
        $res1 = $result[$i];
        $rex2 = $rex2.'<tr><td>'.testx($res1['id']).'</td><td>'.date('m/d/Y', strtotime($res1['rdate'])).
                '</td><td>'.date('m/d/Y', strtotime($res1['cdate'])).'</td><td>'.
                date('m/d/Y', strtotime($res1['udate'])).'</td><td>'.testx($res1['rinfo1']).'</td></tr>';
        }         
        
   $rex1 = $rex1.$rex2.'</table>'; 

   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' => 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);
$pdf->SetFont('helvetica', '', 10);
        $pdf->writeHTML($rex1, 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('WOList'.$_GET['id'].'.pdf', 'I');

  // print_r($rex1);
}
?>
