<?php
session_start();
ini_set('display_errors', 1);
error_reporting(E_ALL);
require_once "main.php";
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
Logger::configure('log4php.xml');
$log = Logger::getLogger('billingpdf');
global $db;
$log->info($_GET['submitType'].' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum'].' = '.$_GET['id']);
$mrnow1 = bill_get_billpost($_SESSION['comnum'], $_GET['id']);
$mrates = bill_get_rates($_SESSION['comnum']);
$mtbl = '<h3>Billing Report</h3><table width="700" cell-padding="3" align="center">
 <tr><td><b>Account</b></td><td><b>Name</b></td><td><b>Service</b></td><td><b>Prev Reading</b></td><td><b>Current Reading</b></td><td><b>Use</b></td><td><b>Prev Balance</b></td><td><b>Current Bill Amount</b></td><td><b>After Billing Balance</b></td></tr>';
$mtbl1 = '';
for ($i=0;$i<count($mrnow1);$i++) {
    $ub1=0; //usa e 1
    $ub2=0; 
    $tot1=0;
    $zb1=0;
    $zb2=0;
    $zbal=0;
    $mrnow1[$i]['lmuse'] = $mrnow1[$i]['luse'];
    $mrnow1[$i]['luse'] = $mrnow1[$i]['xread'] - $mrnow1[$i]['lread'];
    $mrnow1[$i]['lmread'] = $mrnow1[$i]['lread'];
    $mrnow1[$i]['lread'] = $mrnow1[$i]['xread'];
    $tes1 = split("[:]",$mrnow1[$i]['rates']);
    $mrnow1[$i]['z'] = count($tes1);
    $ybal = $mrnow1[$i]['abalance'];
    for ($z=0;$z<count($tes1);$z++) {
        $tes2 = $tes1[$z];
        for ($y=0;$y<count($mrates);$y++) {
            if ($tes2 === $mrates[$y]['name']){
                $mrnow1[$i]['rcname1'] = $mrates[$y]['name'];
                if ($mrates[$y]['gal1'] === 0){
                    $zbal = $zbal + $mrates[$y]['amount1'];
                    $ub1 = $mrnow1[$i]['luse'];
                } else {
                    $ub1 = $mrnow1[$i]['luse'] - ($mrates[$y]['gal1']/1000);
                    $zbal = $zbal + $mrates[$y]['amount1']; 
                }
                //echo '1: '.$zbal.' - '.$ub1.' x '; 
                if ($ub1 > 0){
                    if ($mrates[$y]['gal2'] > 0){
                        if (($mrates[$y]['gal2']/1000) < $ub1){
                            $ub1 = $ub1 - ($mrates[$y]['gal2']/1000);  
                            $zb1 = ($mrates[$y]['gal2']/1000) * $mrates[$y]['amount2'];
                            $zbal =  $zbal + $zb1;
                        } else {
                            $uba = $ub1 - ($mrates[$y]['gal2']/1000);
                            $ub1 = 0;
                            $zb1 = $uba * $mrates[$y]['amount2'];
                            $zbal =  $zbal + $zb1;
                        }
                    }
                }
                //echo '2: '.$zbal.' - '.$ub1.' +'.($mrates[$y]['gal2']/1000).' x ';
                if ($ub1 > 0){
                    if ($mrates[$y]['gal3'] > 0){
                        if (($mrates[$y]['gal3']/1000) < $ub1){
                            $ub1 = $ub1 - ($mrates[$y]['gal3']/1000);   
                            $zb1 = ($mrates[$y]['gal3']/1000) * $mrates[$y]['amount3'];
                            $zbal =  $zbal + $zb1;
                        } else {
                            $uba = $ub1 - ($mrates[$y]['gal3']/1000);
                            $ub1 = 0;
                            $zb1 = $uba * $mrates[$y]['amount3'];
                            $zbal =  $zbal + $zb1;
                        }
                    }
                }
                //echo '3: '.$zbal.' - '.$ub1.' x ';
                if ($ub1 > 0){
                    if ($mrates[$y]['gal4'] > 0){
                        if (($mrates[$y]['gal4']/1000) < $ub1){
                            $ub1 = $ub1 - ($mrates[$y]['gal4']/1000);  
                            $zb1 = ($mrates[$y]['gal4']/1000) * $mrates[$y]['amount4'];
                            $zbal =  $zbal + $zb1;
                        } else {
                            $uba = $ub1 - ($mrates[$y]['gal4']/1000);
                            $ub1 = 0;
                            $zb1 = $uba * $mrates[$y]['amount4'];
                            $zbal =  $zbal + $zb1;
                        }
                    }
                }
                //echo '4: '.$zbal.' - '.$ub1.' x ';
                if ($ub1 > 0){
                    if ($mrates[$y]['gal5'] > 0){
                        if (($mrates[$y]['gal5']/1000) < $ub1){
                            $ub1 = $ub1 - ($mrates[$y]['gal5']/1000);   
                            $zb1 = ($mrates[$y]['gal5']/1000) * $mrates[$y]['amount5'];
                            $zbal =  $zbal + $zb1;
                        } else {
                            $uba = $ub1 - ($mrates[$y]['gal5']/1000);
                            $ub1 = 0;
                            $zb1 = $uba * $mrates[$y]['amount5'];
                            $zbal =  $zbal + $zb1;
                        }
                    }
                }
                //echo '5: '.$zbal.' - '.$ub1.' x ';
                if ($ub1 > 0){
                    if ($mrates[$y]['gal6'] > 0){
                        if (($mrates[$y]['gal6']/1000) < $ub1){
                            $ub1 = $ub1 - ($mrates[$y]['gal6']/1000);
                            $zb1 = ($mrates[$y]['gal6']/1000) * $mrates[$y]['amount6'];
                            $zbal =  $zbal + $zb1;
                        } else {
                            $uba = $ub1 - ($mrates[$y]['gal6']/1000);
                            $ub1 = 0;
                            $zb1 = $uba * $mrates[$y]['amount6'];
                            $zbal =  $zbal + $zb1;
                        }
                    }
                }
                if ($mrates[$y]['tax'] > 0){
                    $tx1 = $zbal * ($mrates[$y]['tax']/100);
                }
                $mrnow1[$i]['ybal'.($z+1)] = $zbal;
                $mrnow1[$i]['tx1'.($z+1)] = $tx1;
                $tot1 = $tot1 + $zbal;
                $ttax = $ttax = $tx1;
                $zbal = 0;
            }    
        }
    }
    $mrnow1[$i]['notice'] = $_GET['note1']; // notice
    $mrnow1[$i]['subbal'] = round($tot1, 2);;
    $mrnow1[$i]['taxbal'] = round($ttax, 2);
    $mrnow1[$i]['totbal'] = round($tot1+$ttax, 2);
    $mrnow1[$i]['zbalance'] = $mrnow1[$i]['abalance']+$mrnow1[$i]['totbal'];
}
$sql = 'select * from company where id = '.$_SESSION['comnum'];
    $st = $db->prepare($sql);
    $st->execute();
    $result = $st->fetchAll();
    $comz = $result;
$sql = 'select * from bformat where id = '.$comz[0]['billformat'];
    $st = $db->prepare($sql);
    $st->execute();
    $result = $st->fetchAll();
    $bformat = $result;
     ob_end_clean();
$pdf = new TCPDF($bformat[0]['porient'], "pt", "USLETTER", true, 'UTF-8', false);
$pdf->SetAutoPageBreak("false");
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('ProTekBill');
$pdf->SetTitle('Billing');
$pdf->SetSubject('Utility Bills');
$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
);
$cz = 0;
$czq=$comz[0]['uspstrack'];
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 8);
$resolution= array($bformat[0]['pheight'],$bformat[0]['plength']);//array(792, 612);
$pdf->AddPage($bformat[0]['porient'], $resolution);
$today = date("m.d.y");
$due  = date("m.d.y",mktime(0, 0, 0, date("m")  , date("d")+15 , date("Y")));
$due1  = date("m.d");
$duex  = date("m.d",mktime(0, 0, 0, date("m")-1  , date("d"), date("Y"))).'-'.$due1;
$czq1= date("m");
$htmlz = '<table>
    <tr><td align="center">'.$comz[0]['uspspermit'].'</td></tr>
    <tr><td align="center">'.$comz[0]['uspspermit1'].'</td></tr>
    <tr><td align="center">'.$comz[0]['uspspermit2'].'</td></tr>
    <tr><td align="center">'.$comz[0]['uspspermit3'].'</td></tr>
    <tr><td align="center">'.$comz[0]['uspspermit4'].'</td></tr>
    <tr><td align="center">'.$comz[0]['uspspermit5'].'</td></tr>
    </table>';
$htmlx = '
<table width="110">
 <tr>
  <td align="center"><b>'.$comz[0]['name'].'</b></td>
 </tr>
  <tr>
  <td align="center">'.$comz[0]['addr'].'</td>
 </tr>
   <tr>
  <td align="center">'.$comz[0]['addr1'].'</td>
 </tr>
  <tr>
  <td align="center">'.$comz[0]['city'].', '.$comz[0]['state'].'  '.$comz[0]['zip'].'</td>
 </tr>
  <tr>
  <td align="center">'.$comz[0]['phone'].'</td>
 </tr>
   <tr>
  <td align="center">'.$comz[0]['web'].'</td>
 </tr>
 <tr>
  <td align="center">............................</td>
 </tr>
  <tr><td></td>
 </tr>
 <tr>
  <td></td>
 </tr>
  <tr>';
for ($i=0;$i<count($mrnow1);$i++) {
    if (($mrnow1[$i]['ctype']==1) or($mrnow1[$i]['ctype']==2) or ($mrnow1[$i]['ctype']==5) or ($mrnow1[$i]['ctype']==6) ){
    $cz = $cz +1;
    $czq = $czq+1;
 $tblx = '<td >Acct# '.$mrnow1[$i]['account'].'</td>
 </tr>
  <tr>
  <td >'.$mrnow1[$i]['name'].'</td>
 </tr>
 <tr>
  <td>Service Address</td>
 </tr>
  <tr>
  <td>'.$mrnow1[$i]['maddr'].'</td>
 </tr>
  <tr>
  <td></td>
 </tr>
  <tr>
  <td>Bill Date : '.$today.'</td>
 </tr>
  <tr>
  <td>Due Date : '.$due.'</td>
 </tr>
 <tr>
  <td></td>
 </tr>
  <tr>
  <td align="center"><b>Total Due: $'.number_format($mrnow1[$i]['zbalance'], 2, '.', '').'</b></td>
 </tr>
  <tr>
  <td></td>
 </tr>
  <tr>
  <td align="center">Please return this portion</td>
 </tr>
 <tr>
  <td align="center">with your payment</td>
 </tr>
 </table>';
 $tbl1 = '
 <table width="240">
 <tr>
  <td>ADDRESS SERVICE REQUESTED</td>
 </tr>
  <tr>
  <td></td>
 </tr>
  <tr>
  <td>Bill Date '.$today.'  Due Date '.$due.'</td>
 </tr>
  <tr>
  <td>Acct# '.$mrnow1[$i]['account'].'</td>
 </tr>
 <tr>
  <td><i>Service from '.$duex.'</i></td>
 </tr>
  <tr>
  <td><b>'.stripslashes($mrnow1[$i]['notice']).'</b></td>
 </tr>
 </table>';
 $tbl2 = '
 <table width="220" border="1" align="center">
 <tr><td><b>Service</b></td><td><b>Curr</b></td><td><b>Prev</b></td><td><b>Use</b></td><td><b>Amount</b></td></tr>';
 $xrow = '';
 if ($mrnow1[$i]['abalance']>0) {
     $xrow = '<tr><td>Prev Bal</td><td></td><td></td><td></td><td>$'.number_format($mrnow1[$i]['abalance'], 2, '.', '').'</td></tr>';
 }
 if ($mrnow1[$i]['rcname1']>'') {
     $xrow = $xrow.'<tr><td>'.$mrnow1[$i]['rcname1'].'</td><td>'.$mrnow1[$i]['lread'].'</td><td>'
             .$mrnow1[$i]['lmread'].'</td><td>'.$mrnow1[$i]['luse'].'</td><td>$'.number_format($mrnow1[$i]['subbal'], 2, '.', '').'</td></tr>';
 }
  if ($mrnow1[$i]['taxbal']>0) {
     $xrow = $xrow . '<tr><td>Tax</td><td></td><td></td><td></td><td>$'.number_format($mrnow1[$i]['taxbal'], 2, '.', '').'</td></tr>';
 }
$tbl3 ='</table>
 <table width="219">
 <tr>
 <td>Service Address:'.$mrnow1[$i]['maddr'].'</td><td align="right">Total Due: $'.number_format($mrnow1[$i]['zbalance'], 2, '.', '').'</td>
 </tr>
 </table>';
 /*</td></tr>
 </table>';*/
//$onecode = $comz[0]['bonecodeid'].$czq1.sprintf("%04d",$czq).'-'.$mrnow1[$i]['delbarcode'];
$tad = '
<table width="200">
<tr><td>'.$mrnow1[$i]['name'].'</td></tr><tr>
<tr><td>'.$mrnow1[$i]['addr'].'</td></tr>
<tr><td>'.$mrnow1[$i]['addr1'].'</td></tr>
<tr><td>'.$mrnow1[$i]['city'].', '.$mrnow1[$i]['state'].'  '.$mrnow1[$i]['zip'].'</td></tr>
</table>';
$tbl = $htmlx . $tblx;
if (is_odd($i)){
$mtbl1 = $mtbl1.'<tr><td>'.$mrnow1[$i]['account'].'</td><td>'.$mrnow1[$i]['name'].'</td><td>'.$mrnow1[$i]['rcname1'].'</td><td>'.$mrnow1[$i]['lmread'].'</td><td>'
             .$mrnow1[$i]['lread'].'</td><td>'.$mrnow1[$i]['luse'].'</td><td>$'.number_format($mrnow1[$i]['abalance'], 2, '.', '').'</td><td>$'.number_format($mrnow1[$i]['totbal'], 2, '.', '').'</td><td>$'.number_format($mrnow1[$i]['zbalance'], 2, '.', '').'</td></tr>';
    
} else {
$mtbl1 = $mtbl1.'<tr><td><font color="blue">'.$mrnow1[$i]['account'].'</font></td><td><font color="blue">'.$mrnow1[$i]['name'].'</font></td><td><font color="blue">'.$mrnow1[$i]['rcname1'].'</font></td><td><font color="blue">'.$mrnow1[$i]['lmread'].'</font></td><td><font color="blue">'
             .$mrnow1[$i]['lread'].'</font></td><td><font color="blue">'.$mrnow1[$i]['luse'].'</font></td><td><font color="blue">$'.number_format($mrnow1[$i]['abalance'], 2, '.', '').'</font></td><td><font color="blue">$'.number_format($mrnow1[$i]['totbal'], 2, '.', '').'</font></td><td><font color="blue">$'.number_format($mrnow1[$i]['zbalance'], 2, '.', '').'</font></td></tr>';
}
//print_r($tbl.$tbl2.$xrow.$tbl3);
$onecode = '01234567094987654321-01234567891';
//$pdf->write1DBarcode('01234567094987654321-01234567891', 'IMB', '', '', '', 15, 0.6, $style, 'N');
if ($cz==1){
$pdf->SetFont('helvetica', '', 4);
$pdf->writeHTMLCell(50,20,$bformat[0]['lstub1']+300,$bformat[0]['tstub1'],$htmlz, true, true, false, false, false, '');
$pdf->SetFont('helvetica', '', 7);
$pdf->write1DBarcode($onecode, 'IMB', $bformat[0]['lbarcode1'],$bformat[0]['tbarcode1'], '', 18, 1, $style, 'N');
$pdf->writeHTMLcell(0,0,$bformat[0]['lstub1'],$bformat[0]['tstub1'],$tbl, false, true, false, false, false, '');//stub
$pdf->writeHTMLcell(0,0,$bformat[0]['lbill1'],$bformat[0]['tbill1'],$tbl1, false, true, false, false, false, '');//bill
$pdf->writeHTMLcell(0,0,$bformat[0]['lpost1'],$bformat[0]['tpost1'],$tbl2.$xrow.$tbl3, false, true, false, false, false, '');//post
$pdf->writeHTMLcell(0,0,$bformat[0]['laddr1'],$bformat[0]['taddr1'],$tad, false, true, false, false, false, '');//address
} else if ($cz==2) {
$pdf->SetFont('helvetica', '', 4);
$pdf->writeHTMLCell(50,20,$bformat[0]['lstub2']+300,$bformat[0]['tstub2'],$htmlz, true, true, false, false, false, '');
$pdf->SetFont('helvetica', '', 7);
$pdf->write1DBarcode($onecode, 'IMB', $bformat[0]['lbarcode2'],$bformat[0]['tbarcode2'], '', 18, 1, $style, 'N');
$pdf->writeHTMLcell(0,0,$bformat[0]['lstub2'],$bformat[0]['tstub2'],$tbl, false, true, false, false, false, '');//stub
$pdf->writeHTMLcell(0,0,$bformat[0]['lbill2'],$bformat[0]['tbill2'],$tbl1, false, true, false, false, false, '');//bill
$pdf->writeHTMLcell(0,0,$bformat[0]['lpost2'],$bformat[0]['tpost2'],$tbl2.$xrow.$tbl3, false, true, false, false, false, '');//post
$pdf->writeHTMLcell(0,0,$bformat[0]['laddr2'],$bformat[0]['taddr2'],$tad, false, true, false, false, false, '');//address
} else if ($cz==3) {
$pdf->SetFont('helvetica', '', 4);
$pdf->writeHTMLCell(50,20,$bformat[0]['lstub3']+300,$bformat[0]['tstub3'],$htmlz, true, true, false, false, false, '');
$pdf->SetFont('helvetica', '', 7);
$pdf->write1DBarcode($onecode, 'IMB', $bformat[0]['lbarcode3'],$bformat[0]['tbarcode3'], '', 18, 1, $style, 'N');
$pdf->writeHTMLcell(0,0,$bformat[0]['lstub3'],$bformat[0]['tstub3'],$tbl, false, true, false, false, false, '');//stub
$pdf->writeHTMLcell(0,0,$bformat[0]['lbill3'],$bformat[0]['tbill3'],$tbl1, false, true, false, false, false, '');//bill
$pdf->writeHTMLcell(0,0,$bformat[0]['lpost3'],$bformat[0]['tpost3'],$tbl2.$xrow.$tbl3, false, true, false, false, false, '');//post
$pdf->writeHTMLcell(0,0,$bformat[0]['laddr3'],$bformat[0]['taddr3'],$tad, false, true, false, false, false, '');//address
} else if ($cz==4) {
$pdf->SetFont('helvetica', '', 4);
$pdf->writeHTMLCell(50,20,$bformat[0]['lstub4']+300,$bformat[0]['tstub4'],$htmlz, true, true, false, false, false, '');
$pdf->SetFont('helvetica', '', 7);
$pdf->write1DBarcode($onecode, 'IMB', $bformat[0]['lbarcode4'],$bformat[0]['tbarcode4'], '', 18, 1, $style, 'N');
$pdf->writeHTMLcell(0,0,$bformat[0]['lstub4'],$bformat[0]['tstub4'],$tbl, false, true, false, false, false, '');//stub
$pdf->writeHTMLcell(0,0,$bformat[0]['lbill4'],$bformat[0]['tbill4'],$tbl1, false, true, false, false, false, '');//bill
$pdf->writeHTMLcell(0,0,$bformat[0]['lpost4'],$bformat[0]['tpost4'],$tbl2.$xrow.$tbl3, false, true, false, false, false, '');//post
$pdf->writeHTMLcell(0,0,$bformat[0]['laddr4'],$bformat[0]['taddr4'],$tad, false, true, false, false, false, '');//address
}
if ($cz==$bformat[0]['pbqty']) {
    $cz=0;
    $pdf->AddPage($bformat[0]['porient'], $resolution);
}
    }
if (($mrnow1[$i]['ctype']==2) or ($mrnow1[$i]['ctype']==6) ){
    $cz = $cz +1;   
    $czq = $czq+1;
 $tblx = '<td >Acct# '.$mrnow1[$i]['account'].'</td>
 </tr>
  <tr>
  <td >'.$mrnow1[$i]['name'].'</td>
 </tr>
 <tr>
  <td>Service Address</td>
 </tr>
  <tr>
  <td>'.$mrnow1[$i]['maddr'].'</td>
 </tr>
  <tr>
  <td></td>
 </tr>
  <tr>
  <td>Bill Date : '.$today.'</td>
 </tr>
  <tr>
  <td>Due Date : '.$due.'</td>
 </tr>
 <tr>
  <td></td>
 </tr>
  <tr>
  <td align="center"><b>Total Due: $'.number_format($mrnow1[$i]['zbalance'], 2, '.', '').'</b></td>
 </tr>
  <tr>
  <td></td>
 </tr>
  <tr>
  <td align="center">Please return this portion</td>
 </tr>
 <tr>
  <td align="center">with your payment</td>
 </tr>
 </table>';

 $tbl1 = '
 <table width="240">
 <tr>
  <td>ADDRESS SERVICE REQUESTED</td>
 </tr>
  <tr>
  <td></td>
 </tr>
  <tr>
  <td>Bill Date '.$today.'  Due Date '.$due.'</td>
 </tr>
  <tr>
  <td>Acct# '.$mrnow1[$i]['account'].'</td>
 </tr>
 <tr>
  <td><i>Service from '.$duex.'</i></td>
 </tr>
  <tr>
  <td><b>'.stripslashes($mrnow1[$i]['notice']).'</b></td>
 </tr>
 </table>';
 $tbl2 = '
 <table width="220" border="1" align="center">
 <tr><td><b>Service</b></td><td><b>Curr</b></td><td><b>Prev</b></td><td><b>Use</b></td><td><b>Amount</b></td></tr>';
 $xrow = '';
 if ($mrnow1[$i]['abalance']>0) {
     $xrow = '<tr><td>Prev Bal</td><td></td><td></td><td></td><td>$'.number_format($mrnow1[$i]['abalance'], 2, '.', '').'</td></tr>';
 }if ($mrnow1[$i]['rcname1']>'') {
     $xrow = $xrow.'<tr><td>'.$mrnow1[$i]['rcname1'].'</td><td>'.$mrnow1[$i]['lread'].'</td><td>'
             .$mrnow1[$i]['lmread'].'</td><td>'.$mrnow1[$i]['luse'].'</td><td>$'.number_format($mrnow1[$i]['subbal'], 2, '.', '').'</td></tr>';
 }
   if ($mrnow1[$i]['taxbal']>0) {
     $xrow = $xrow.'<tr><td>Tax</td><td></td><td></td><td></td><td>$'.number_format($mrnow1[$i]['taxbal'], 2, '.', '').'</td></tr>';
 }
$tbl3 ='</table>
 <table width="219">
 <tr>
 <td>Service Address:'.$mrnow1[$i]['maddr'].'</td><td align="right">Total Due: $'.number_format($mrnow1[$i]['zbalance'], 2, '.', '').'</td>
 </tr>
 </table>
 </td></tr>
 </table>';
$onecode = $comz[0]['obonecodeid'].$czq1.sprintf("%04d",$czq).'-'.$mrnow1[$i]['odelbarcode'];
$tad = '
<table width="200">
<tr><td>'.$mrnow1[$i]['oname'].'</td></tr><tr>
<tr><td>'.$mrnow1[$i]['oaddr'].'</td></tr>
<tr><td>'.$mrnow1[$i]['oaddr1'].'</td></tr>
<tr><td>'.$mrnow1[$i]['ocity'].', '.$mrnow1[$i]['ostate'].'  '.$mrnow1[$i]['ozip'].'</td></tr>
</table>';
$tbl = $htmlx . $tblx;
if ($cz==1){
$pdf->SetFont('helvetica', '', 4);
$pdf->writeHTMLCell(50,20,$bformat[0]['lstub1']+300,$bformat[0]['tstub1'],$htmlz, true, true, false, false, false, '');
$pdf->SetFont('helvetica', '', 7);
$pdf->write1DBarcode($onecode, 'IMB', $bformat[0]['lbarcode1'],$bformat[0]['tbarcode1'], '', 18, 1, $style, 'N');
$pdf->writeHTMLcell(0,0,$bformat[0]['lstub1'],$bformat[0]['tstub1'],$tbl, false, true, false, false, false, '');//stub
$pdf->writeHTMLcell(0,0,$bformat[0]['lbill1'],$bformat[0]['tbill1'],$tbl1, false, true, false, false, false, '');//bill
$pdf->writeHTMLcell(0,0,$bformat[0]['lpost1'],$bformat[0]['tpost1'],$tbl2.$xrow.$tbl3, false, true, false, false, false, '');//post
$pdf->writeHTMLcell(0,0,$bformat[0]['laddr1'],$bformat[0]['taddr1'],$tad, false, true, false, false, false, '');//address
} else if ($cz==2) {
$pdf->SetFont('helvetica', '', 4);
$pdf->writeHTMLCell(50,20,$bformat[0]['lstub2']+300,$bformat[0]['tstub2'],$htmlz, true, true, false, false, false, '');
$pdf->SetFont('helvetica', '', 7);
$pdf->write1DBarcode($onecode, 'IMB', $bformat[0]['lbarcode2'],$bformat[0]['tbarcode2'], '', 18, 1, $style, 'N');
$pdf->writeHTMLcell(0,0,$bformat[0]['lstub2'],$bformat[0]['tstub2'],$tbl, false, true, false, false, false, '');//stub
$pdf->writeHTMLcell(0,0,$bformat[0]['lbill2'],$bformat[0]['tbill2'],$tbl1, false, true, false, false, false, '');//bill
$pdf->writeHTMLcell(0,0,$bformat[0]['lpost2'],$bformat[0]['tpost2'],$tbl2.$xrow.$tbl3, false, true, false, false, false, '');//post
$pdf->writeHTMLcell(0,0,$bformat[0]['laddr2'],$bformat[0]['taddr2'],$tad, false, true, false, false, false, '');//address
} else if ($cz==3) {
$pdf->SetFont('helvetica', '', 4);
$pdf->writeHTMLCell(50,20,$bformat[0]['lstub3']+300,$bformat[0]['tstub3'],$htmlz, true, true, false, false, false, '');
$pdf->SetFont('helvetica', '', 7);
$pdf->write1DBarcode($onecode, 'IMB', $bformat[0]['lbarcode3'],$bformat[0]['tbarcode3'], '', 18, 1, $style, 'N');
$pdf->writeHTMLcell(0,0,$bformat[0]['lstub3'],$bformat[0]['tstub3'],$tbl, false, true, false, false, false, '');//stub
$pdf->writeHTMLcell(0,0,$bformat[0]['lbill3'],$bformat[0]['tbill3'],$tbl1, false, true, false, false, false, '');//bill
$pdf->writeHTMLcell(0,0,$bformat[0]['lpost3'],$bformat[0]['tpost3'],$tbl2.$xrow.$tbl3, false, true, false, false, false, '');//post
$pdf->writeHTMLcell(0,0,$bformat[0]['laddr3'],$bformat[0]['taddr3'],$tad, false, true, false, false, false, '');//address
} else if ($cz==4) {
$pdf->SetFont('helvetica', '', 4);
$pdf->writeHTMLCell(50,20,$bformat[0]['lstub4']+300,$bformat[0]['tstub4'],$htmlz, true, true, false, false, false, '');
$pdf->SetFont('helvetica', '', 7);
$pdf->write1DBarcode($onecode, 'IMB', $bformat[0]['lbarcode4'],$bformat[0]['tbarcode4'], '', 18, 1, $style, 'N');
$pdf->writeHTMLcell(0,0,$bformat[0]['lstub4'],$bformat[0]['tstub4'],$tbl, false, true, false, false, false, '');//stub
$pdf->writeHTMLcell(0,0,$bformat[0]['lbill4'],$bformat[0]['tbill4'],$tbl1, false, true, false, false, false, '');//bill
$pdf->writeHTMLcell(0,0,$bformat[0]['lpost4'],$bformat[0]['tpost4'],$tbl2.$xrow.$tbl3, false, true, false, false, false, '');//post
$pdf->writeHTMLcell(0,0,$bformat[0]['laddr4'],$bformat[0]['taddr4'],$tad, false, true, false, false, false, '');//address
}
if ($cz==$bformat[0]['pbqty']) {
    $cz=0;
    $pdf->AddPage($bformat[0]['porient'], $resolution);
}
}
}
//$mtbl = $mtbl.$mtbl1.'</table>';
//$pdf->AddPage($bformat[0]['porient'], $resolution);
//$pdf->writeHTMLcell(0,0,15,15,$mtbl, false, true, false, false, false, '');
$pdf->Output('example_001.pdf', 'I');

//============================================================+
// END OF FILE
//============================================================+
