Your IP : 216.73.216.40


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

<h1><img src='images/bulb7.jpg' height='12'>Meter Billing</h1>
<?php
$tbl = "el_billing";

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

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

echo "<b>Meter billing of</b> <select name='month'>";
$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> <input type='submit' name='submit' value='OK'>
[ ";

if ($ext == 1) {
	echo "<a href='?pg=elbilling&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=elbilling&month=$month&ext=1'>External</a>";  
}
echo " ] [ <a href='?pg=elbilling&month=$month&recal=1'>Re-calculate</a> ] 
<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;
	$subsidymode = $o->subsidymode;
}

//if (!mysql_num_rows(mysql_query("select * from $tbl where month='$month'"))) $recal = 1;
if ($recal == 1) {
	//delete all old records of this month
	mysql_query("delete from el_billing where month='$month'");
	//create new records of this month
	$rs = mysql_query("select * from el_reading where month='$month' order by flatno");
	while ($o = mysql_fetch_object($rs)) {
		$prevreading = 0.00;
		$flatno = $o->flatno;
		$curreading = $o->reading;
	
		//get previous reading
		$sr = mysql_query("select * from el_reading where flatno='$flatno' order by id desc limit 1,1");
		if (mysql_num_rows($sr)) {
			$q = mysql_fetch_object($sr);
			$prevreading = $q->reading;
		}
		//if old meter is replaced with new one
		$sr = mysql_query("select * from el_flats where flatno='$flatno'");
		if (mysql_num_rows($sr)) {
			$q = mysql_fetch_object($sr);
			$meterno = $q->meterno;
			$contactedload = $q->contactedload;
			if (!$prevreading) $prevreading = $q->reading;
			else $prevreading_new = $q->reading;
			$curreading_new = $q->reading_new;
		}

		//calculate units
		$consumedunits = $curreading - $prevreading;
		if ($reading_new) {
			$units_new = $curreading_new - $prevreading_new;
			$consumedunits += $units_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;
			$subsidy = $q->subsidy;
		}

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

		//calculate payables
		$consumedcharge = $consumedunits * $unitrate;
		if ($subsidymode == 'rupees') {
			if (!$rsubsidy) $rsubsidy = 0.00;
			//calculate payable
			if ($consumedcharge > $rsubsidy) {
				$payable = $consumedcharge - $rsubsidy;
			} else {
				$payable = 0.00;
			}
			//assign subsidy
			$subsidy = $rsubsidy;
		} else {
			if (!$usubsidy) $usubsidy = 0.00;
			//calculate payable
			if ($consumedunits > $usubsidy) {
				$excessunits = $consumedunits - $usubsidy;
				//calculate payable
				$payable = $excessunits * $unitrate;
			} else {
				$payable = 0.00;
			}
			//assign subsidy
			$subsidy = $usubsidy;
		}
		
		if ($sharing == 'Yes') $subsidy += $subsidy;
		//insert/update into billing table
        	mysql_query("insert into $tbl(flatno,month,licensee,category,external,meterno,prevreading,curreading,
		contactedload,unitrate,fixedrate,plrate,edrate,rscrate,subsidy,dated)
		values('$flatno','$month','$licensee','$category','$external','$meterno',$prevreading,$curreading,
		$contactedload,$unitrate,$fixedrate,$plrate,$edrate,$rscrate,$subsidy,now())");
		
		mysql_query("update el_config set datebilling=now()");
	}
}

//$rs = mysql_query("select * from $tbl where month='$month' order by flatno");
$rs = mysql_query("select * from (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")."') el_billing2
  order by left(flatno, pos-1), mid(flatno, pos, length(flatno)-pos+1)+0");

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

	$consumedunit = $curreading - $prevreading;
	$consumedcharge = $consumedunit * $unitrate;
	
	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 {
		if ($consumedunit > $subsidy) {
			$excessunit =  $consumedunit - $subsidy;
			$payable = $excessunit * $unitrate;
		} else {
			$excessunit =  0;
			$payable = $totalamount;
		}
	}
	$tunit += $consumedunit;
	$tcharge += $consumedcharge;
	$tamount += $totalamount;
	$tsubsidy += $subsidy;
	$tpayable += $payable;
	
	$data .= "<tr><td>$i</td>
	<td><a href='?pg=elreceipt&id=$o->id'>$flatno</a></td>
	<td nowrap>$licensee</td>
	<td>$meterno</td>
	<td>$contactedload</td>
	<td>$prevreading</td>
	<td>$curreading</td>
	<td align='right'>".number_format($consumedunit,2)."</td>
	<td align='right'>Rs. ".number_format($consumedcharge,2)."</td>";
	
	if ($external == 'Yes') {
		$data .= "<td align='right'>".number_format($fixedcharge,2)."</td>
		<td align='right'>".number_format(round($subtotal),2)."</td>
		<td align='right'>".number_format($plcharge,2)."</td>
		<td align='right'>".number_format($edcharge,2)."</td>
		<td align='right'>".number_format($rscharge,2)."</td>
		<td align='right'>Rs.".number_format($totalamount,2)."</td>";
	} else {
		$data .= "<td align='right'>".($subsidymode == 'Rupees' ? 'Rs.' : '').number_format($subsidy,2).
		($subsidymode == 'Units' ? ' Units': '')."</td><td align='right'>".number_format($excessunit,2)."</td>";
	}
	$data .= "<td align='right'>Rs.".number_format($payable,2)."</td></tr>";
}

echo "<p><table width='100%' border='1' style='border-collapse: collapse'>
<thead>
<tr><th colspan='".($external == 'Yes' ? 16 : 12)."'>Electricity bill for the month of $month (Unit rate: Rs. $unitrate";

if ($external == 'Yes') echo ", Fixed rate: $fixedrate/KVA/month, P.Load rate: $plrate/KVA/month, ED rate: $edrate%, Reg. Surcharge: $rscrate%";

echo ")</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</th><th rowspan='2'>Excess<br>Units</th>";

echo "<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'>Total</th>
<th>".number_format($tunit,2)."</th>
<th>Rs.".number_format(round($tcharge),2)."</th>";

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

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

?>