<?php
session_start();
require_once "main.php";
require_once('tcpdf/config/lang/eng.php');
require_once('tcpdf/tcpdf.php');
Logger::configure('log4php.xml');
$log = Logger::getLogger('postaccounts');
global $db;
$mrnow1 = bill_get_billpost($_SESSION['comnum'], $_GET['id']);
//print_r($mrnow1);
$mrates = bill_get_rates($_SESSION['comnum']);
$mctype = bill_get_mctype($_SESSION['comnum']);
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]['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'];
}
//print_r($mrnow1);
$sql = 'Insert into bill_history VALUES ';
for ($i=0;$i<count($mrnow1);$i++) {
$sql = $sql . '(NULL, "'.$_SESSION['comnum'].'", "'.$mrnow1[$i]['0'].'", NULL, "'.$mctype[0]['id'].'", "'.$mrnow1[$i]['totbal']
        .'", "'.$mrnow1[$i]['luse'].'", "0.00", "Regular Bill","","","'.$mrnow1[$i]['zbalance'].'")';
        if ($i<count($mrnow1)-1) {
            $sql = $sql.',';
        }
}
echo 'Processing '.$i.' meter reads.'.$sql;
$st = $db->prepare($sql);
$st->execute();
echo 'Processing '.$i.' meter reads.';
for ($i=0;$i<count($mrnow1);$i++) {
$sql = 'UPDATE customer SET abalance = "'.$mrnow1[$i]['zbalance'].'", lmread="'.$mrnow1[$i]['lmread']
        .'", lread="'.$mrnow1[$i]['lread'].'", lmuse="'.$mrnow1[$i]['lmuse'].'", luse="'.$mrnow1[$i]['luse']
        .'" where id="'.$mrnow1[$i]['0'].'"';
echo 'Processing '.$i.' meter reads.'.$sql;
$st = $db->prepare($sql);
$st->execute();

}
$sql = 'DELETE from mtemp where batch = "'.$_GET['id'].'"';
$st = $db->prepare($sql);
$st->execute();
$sql = 'DELETE from mrbatch where id = "'.$_GET['id'].'"';
$st = $db->prepare($sql);
$st->execute();
$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 = '';
     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
);
$pdf->setFontSubsetting(true);
$pdf->SetFont('helvetica', '', 8);
$resolution= array(792,612);//array(792, 612);
$pdf->AddPage($bformat[0]['porient'], $resolution);
$today = date("m.d.y");
for ($i=0;$i<count($mrnow1);$i++) {
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>';
}
}
$mtbl = $mtbl.$mtbl1.'</table>';
$pdf->writeHTMLcell(0,0,15,15,$mtbl, false, true, false, false, false, '');
?>
