Your IP : 216.73.216.40


Current Path : /var/www/html/mmishra/ilight/
Upload File :
Current File : /var/www/html/mmishra/ilight/elbilling.inc.bak

<h3>Meter Billing</h3>
<center>

<?php
$tbl = "el_billing";

$recal = $_REQUEST["recal"];
$ext = $_REQUEST["ext"];
$month = $_REQUEST["month"];

if (!$month) $month = previous_month();//date("M-Y");

echo "<font size='+1'>[ </font>";
if ($ext == 1) {
        echo "<a href='?pg=$pg&month=$month&ext=0'>Internal</a> | <font color='red' size='+1'>External</font>";
} else {
        echo "<font color='red' size='+1'>Internal</font> | <a href='?pg=$pg&month=$month&ext=1'>External</a>";  
}       
echo "<font size='+1'> ]</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Month <select name='month' onchange=\"javascript: document.location.href='?pg=$pg&month='+this.value\">";
$rs = mysql_query("select distinct month from el_reading order by id desc");
while ($o = mysql_fetch_object($rs)) {
	echo "<option value='$o->month'";
	if ($o->month == $month) echo " selected";
	echo ">$o->month</option>";
}
echo "</select>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";

if ($month == previous_month()) {
	echo "<font size='+1'>[ </font><a href='?pg=$pg&month=$month&recal=1'>Re-calculate</a><font size='+1'> ]</font>";
}
echo "<input type='hidden' name='ext' value='$ext'>";

$rs = mysql_query("select * from el_config");
if (mysql_num_rows($rs)) {
	$o = mysql_fetch_object($rs);
	$unitrate = $o->unitrate;
	$fixedrate = $o->fixedrate;
	$plrate = $o->plrate;
	$edrate = $o->edrate;
	$rscrate = $o->rscrate;
}

if ($recal == 1) {
	//create new records of this month
	$rs = mysql_query("select * from el_flats order by flatno");
	while ($o = mysql_fetch_object($rs)) {
		$flatno = $o->flatno;
		$meterno = $o->meterno;
		$contactedload = $o->contactedload;
		$prevreading = $o->reading;
		$meterno_new = $o->meterno_new;
		$prevreading_new = $o->reading_new;
	
                //get category of licensee
                $sr = mysql_query("select * from el_licensee where flatno='$flatno'");
                if (mysql_num_rows($sr)) {
                        $q = mysql_fetch_object($sr);
                        $licensee = $q->fullname;
                        $category = $q->category;
                        $sharing = $q->sharing;
                        $external = $q->external;
                } else {
                        $licensee = 'VACANT';
                        $category = 'None';
                        $external = 'No';
                }

		//get subsidy for licensee
		$sr = mysql_query("select * from el_subsidy where category='$category'");
		if (mysql_num_rows($sr)) {
			$q = mysql_fetch_object($sr);
			$userfees = $q->userfees;
			$subsidymode = $q->subsidymode;
			$subsidy = $q->subsidy;
		}

		//get current reading
		$sr = mysql_query("select * from el_reading where flatno='$flatno' and month='$month' order by id limit 0,1");
		if (mysql_num_rows($sr)) {
			$q = mysql_fetch_object($sr);
			$id = $q->id;
			$curreading = $q->reading;
		}	

		//get previous reading
                $sr = mysql_query("select reading from el_reading where flatno='$flatno' and month<>'$month' and id<$id order by id desc limit 0,1");
                if (mysql_num_rows($sr)) {
                        $q = mysql_fetch_object($sr);
                        $prevreading = $q->reading;
		}

		if ($curreading < $prevreading) $curreading = $prevreading;

		//consumed units
		$consumedunits = $curreading - $prevreading;

		//additional units
		if ($meterno_new) {
                	$sr = mysql_query("select reading from el_reading where flatno='$flatno' and month='$month' order by id desc limit 0,1");
                	if (mysql_num_rows($sr)) {
                        	$q = mysql_fetch_object($sr);
                        	$curreading_new = $q->reading;
				//consumed units
				$additionalunits = $curreading_new - $prevreading_new;
				$consumedunits = $consumedunits + $additionalunits;
				$licensee .= '+';
			}
		} else {
			$additionalunits = 0;
		}

		$consumedcharge = $consumedunits * $unitrate;
                //calculate payable
                $payable = 0.00;
                $excessunits = 0.00;
                $consumedcharge = 0.00;
                $excesscharge = 0.00;
                if (!$subsidy) $subsidy = 0.00;
		if ($sharing == 'Yes') $subsidy += $subsidy;

                if ($subsidymode == 'Rupees') {
			if ($consumedcharge > $subsidy) {
                		$excesscharge = $excesscharge - $subsidy;
				$payable = $consumedcharge;
                        	$excessunits = $payable;
			}
                } else if ($subsidymode == 'Units') {
			if ($consumedunits > $subsidy) {
                        	$excessunits = $consumedunits - $subsidy;
                        	$excesscharge = $excessunits * $unitrate;
				$payable = $excesscharge;
			}
                } else if ($subsidymode == 'None') {
                        $excessunits = $consumedunits;
                        $excesscharge = $excessunits * $unitrate;
                        $payable = $excesscharge;
		}
        	//payable with userfees
		$payable += $userfees;
		
		//insert/update into billing table
		if (mysql_num_rows(mysql_query("select * from $tbl where month='$month' and flatno='$flatno'"))) {
			mysql_query("update $tbl set userfees=$userfees,month='$month',licensee='$licensee',category='$category',
			external='$external',meterno='$meterno',prevreading=$prevreading,curreading=$curreading,addunits=$additionalunits,
			contactedload=$contactedload,unitrate=$unitrate,fixedrate=$fixedrate,plrate=$plrate,edrate=$edrate,
			rscrate=$rscrate,subsidymode='$subsidymode',subsidy=$subsidy,dated=now()
			where month='$month' and flatno='$flatno'");
		} else {
        		mysql_query("insert into $tbl(flatno,userfees,month,licensee,category,external,meterno,prevreading,curreading,
			addunits,contactedload,unitrate,fixedrate,plrate,edrate,rscrate,subsidymode,subsidy,dated)
			values('$flatno',$userfees,'$month','$licensee','$category','$external','$meterno',$prevreading,$curreading,
			$additionalunits,$contactedload,$unitrate,$fixedrate,$plrate,$edrate,$rscrate,'$subsidymode',$subsidy,now())");
		}
		mysql_query("update el_config set datebilling=now()");
	}
}

$rs = mysql_query("select *,
    	least(
      	case when locate('0', flatno)>0 then locate('0', flatno) else length(flatno)+1 end,
      	case when locate('1', flatno)>0 then locate('1', flatno) else length(flatno)+1 end,
      	case when locate('2', flatno)>0 then locate('2', flatno) else length(flatno)+1 end,
      	case when locate('3', flatno)>0 then locate('3', flatno) else length(flatno)+1 end,
      	case when locate('4', flatno)>0 then locate('4', flatno) else length(flatno)+1 end,
      	case when locate('5', flatno)>0 then locate('5', flatno) else length(flatno)+1 end,
      	case when locate('6', flatno)>0 then locate('6', flatno) else length(flatno)+1 end,
      	case when locate('7', flatno)>0 then locate('7', flatno) else length(flatno)+1 end,
      	case when locate('8', flatno)>0 then locate('8', flatno) else length(flatno)+1 end,
      	case when locate('9', flatno)>0 then locate('9', flatno) else length(flatno)+1 end) pos
  	from $tbl where month='$month' and external='".($ext ? "Yes" : "No")."' 
  	order by left(flatno, pos-1), mid(flatno, pos, length(flatno)-pos+1)+0");

while ($o = mysql_fetch_object($rs)) {
	$i++;
	$flatno = $o->flatno;
	$userfees = $o->userfees;
	$licensee = $o->licensee;
	$meterno = $o->meterno;
	$contactedload = $o->contactedload;
	$unitrate = $o->unitrate;
	$fixedrate = $o->fixedrate;
	$plrate = $o->plrate;
	$edrate = $o->edrate;
	$rscrate = $o->rscrate;
	$subsidymode = $o->subsidymode;
	$subsidy = $o->subsidy;
	$external = $o->external;
	
	$prevreading = $o->prevreading;
	$curreading = $o->curreading;
	$addunits = $o->addunits;

	$consumedunits = $curreading - $prevreading + $addunits;;
	$consumedcharge = $consumedunits * $unitrate;
	$payable = 0.00;
	
	if ($external == 'Yes') {
		$fixedcharge = $contactedload * $fixedrate;
		$subtotal = $consumedcharge + $fixedcharge;
	
		$plcharge = $contactedload * $plrate;
		$edcharge = $subtotal * $edrate/100;
		$rscharge = $subtotal * $rscrate/100;
		
		$totalamount = $subtotal + $plcharge + $edcharge + $rscharge;
		$payable = $totalamount;
	} else {
		//calculate payable
                $payable = 0.00;
                $excessunits = 0.00;
                $consumedcharge = 0.00;
                $excesscharge = 0.00;
                if (!$subsidy) $subsidy = 0.00;

                if ($subsidymode == 'Rupees') {
			if ($consumedcharge > $subsidy) {
                		$excesscharge = $excesscharge - $subsidy;
				$payable = $consumedcharge;
                        	$excessunits = $payable;
			}
                } else if ($subsidymode == 'Units') {
			if ($consumedunits > $subsidy) {
                        	$excessunits = $consumedunits - $subsidy;
                        	$excesscharge = $excessunits * $unitrate;
				$payable = $excesscharge;
			}
                } else if ($subsidymode == 'None') {
                        $excessunits = $consumedunits;
                        $excesscharge = $excessunits * $unitrate;
                        $payable = $excesscharge;
		}
        	//payable with userfees
		$payable += $userfees;
	}
	//summarized figures
	$tunit += $consumedunits;
	$tcharge += $consumedcharge;
	$tamount += $totalamount;
	$tpayable += $payable;
	
	$data .= "<tr><td><a href='?pg=elreceipt&id=$o->id'>$i</a></td>
	<th>$flatno</th><td nowrap>$licensee</td><td>$meterno</td><td>$contactedload</td><td>$prevreading</td><td>$curreading</td>
	<td align='right'>".number_format($consumedunits,2)."</td>
	<td align='right'>Rs.".number_format($consumedcharge,2)."</td>";
	
	if ($external == 'Yes') {
		$data .= "<td align='right'>Rs.".number_format($fixedcharge,2)."</td>
		<td align='right'>Rs.".number_format($subtotal,2)."</td>
		<td align='right'>Rs.".number_format($plcharge,2)."</td>
		<td align='right'>Rs.".number_format($edcharge,2)."</td>
		<td align='right'>Rs.".number_format($rscharge,2)."</td>
		<td align='right'>Rs.".number_format($totalamount,2)."</td>";
	} else {
		$data .= "<td align='right'>".number_format($subsidy,2)." $subsidymode</td><td align='right'>".number_format($excessunits,2)."</td>";
	}
	$data .= "<td align='right'>Rs.".number_format($userfees,2)."</td>
	<td align='right'>Rs.".($excessunits ? "<font color='red'>".number_format($payable,2)."</font>" : number_format($payable,2))."</td></tr>";
}

echo "<table width='100%' border='1' style='border-collapse: collapse'>
<thead>
<tr><th colspan='".($external == 'Yes' ? 17 : 13)."'><h3>Electricity bill for the month of $month</h3>
(Unit rate: Rs. $unitrate/unit, Fixed rate: $fixedrate/KVA/month, P.Load rate: $plrate/KVA/month, ED rate: $edrate%, Reg. Surcharge: $rscrate%)</th></tr>
<tr><th rowspan='2'>No</th>
<th rowspan='2'>SiteNo</th>
<th rowspan='2'>Licensee</th>
<th rowspan='2'>MeterNo</th>
<th rowspan='2'>Contacted<br>Load</th>
<th colspan='2'>KWH Meter Reading</th>
<th colspan='2'>Consumption</th>";

if ($external == 'Yes') echo "<th colspan='6'>Connection Charges</th>";
else echo "<th rowspan='2'>Admissibility<br>(Rs./Units)</th><th rowspan='2'>Excess<br>(Rs./Units)</th>";

echo "<th rowspan='2'>User<br>Fees</th>
<th rowspan='2'>Payable<br>Amount</th></tr>
<tr><th>Previous</th><th>Current</th><th>Units</th><th>Charges</th>";

if ($external == 'Yes') echo "<th>Fixed</th><th>SubTotal</th><th>PLC</th><th>ED</th><th>Sur</th><th>Total</th>";

echo "</tr>
</thead>
<tfoot>
<tr><th colspan='7' align='right'>Total</th>
<th>".number_format($tunit,2)."</th>
<th>Rs.".number_format(round($tcharge),2)."</th>";

if ($external == 'Yes') echo "<th colspan='5'></th><th nowrap>Rs.".number_format($tamount,2)."</th><th></th>";
else echo "<th></th><th></th><th></th>";

echo "<th>Rs.".number_format(round($tpayable),2)."</th></tr>
<tr><th colspan='".($external == 'Yes' ? 17 : 13)."'>Above readings have been taken on the last dates of this and previous month.</th></tr>
</tfoot>
<tbody>$data</tbody>
</table></p>";

?>