Your IP : 216.73.216.40


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

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

$recal = $_REQUEST["recal"];
$month = $_REQUEST["month"];
if (!$month) $month = previous_month();//date("M-Y");

echo "<b>For meter billing, select Month</b> <select name='month'>";
$rs = mysql_query("select distinct month from $tbl 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'>";

$rs = mysql_query("select * from el_config");
if (mysql_num_rows($rs)) {
	$o = mysql_fetch_object($rs);
	$unitrate = $o->unitrate;
	$subsidymode = $o->subsidymode;
}
$recal=1;
if (!mysql_num_rows(mysql_query("select * from $tbl where month='$month'"))) $recal = 1;
if ($recal == 1) {
	$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");
		while ($q = mysql_fetch_object($sr)) {
			$prevreading = $q->reading;
		}
		if (!$prevreading) {
			$sr = mysql_query("select * from el_flats where flatno='$flatno'");
			while ($q = mysql_fetch_object($sr)) {
				$prevreading = $q->reading;
				$reading_new = $q->reading_new;
			}
		}
		//calculate units
		$units = $curreading - $prevreading;
		if ($reading_new) {
			$units_new = $curreading_new - $prevreading_new;
			$units = $units + $units_new;
		}
		//get category of licensee
		$sr = mysql_query("select * from el_licensee where flatno='$flatno' order by id desc limit 0,1");
		while ($q = mysql_fetch_object($sr)) {
			$category = $q->category;
			$subsidy = $q->subsidy;
		}
		//get subsidy for licensee
		$sr = mysql_query("select * from el_subsidy where category='$category'");
		while ($q = mysql_fetch_object($sr)) {
			$rsubsidy = $q->rsubsidy;
			$usubsidy = $q->usubsidy;
		}
		//calculate payables
		if ($subsidymode == 'rupees') {
			if (!$rsubsidy) $rsubsidy = 0.00;
			//calculate amount
			$amount = $units * $unitrate;
			//calculate payable
			if ($amount > $rsubsidy) $payable = $amount - $rsubsidy;
			else $payable = 0.00;
		} else {
			if (!$usubsidy) $usubsidy = 0.00;
			//calculate amount
			$amount = $units * $unitrate;
			//calculate payable
			if ($units > $usubsidy) $units = $units - $usubsidy;
			//calculate amount
			$payable = $units * $unitrate;
			else $payable = 0;
		}
		//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 prevreading=$prevreading,curreading=$curreading,units=$units,
			unitrate=$unitrate,amount=$amount,subsidy=$subsidy,payable=$payable 
			where month='$month' and flatno='$flatno'");
		} else {
        		mysql_query("insert into $tbl(flatno,month,prevreading,curreading,units,unitrate,amount,subsidy,payable,dated) 
			values('$flatno','$month',$prevreading,$curreading,$units,$unitrate,$amount,$subsidy,$payable,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') 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;
	$tunits += $o->units;
	$tamount += $o->amount;
	$tsubsidy += $o->subsidy;
	$tpayable += $o->payable;
        //get licensee details
        $sr = mysql_query("select * from el_licensee where flatno='$flatno'");
        if (mysql_num_rows($sr)) {
                $q = mysql_fetch_object($sr);
                $fullname = $q->fullname;
                $category = $q->category;
	}
	$data .= "<tr><td>$i</td>
	<td><a href='?pg=elreceipt&id=$o->id'>$flatno</a></td><td>$fullname</td><td>$o->prevreading</td><td>$o->curreading</td>
	<td align='right'>$o->units</td><td align='right'>Rs. $o->amount</td><td align='right'>Rs. $o->subsidy</td><td align='right'>Rs. $o->payable</td>
	</tr>";
}
if ($month == previous_month()) echo " [ <a href='?pg=elbilling&recal=1'>Re-calculate</a> ]";
echo "<p><table width='100%' border='1' style='border-collapse: collapse'>
<thead>
<tr><th colspan='9'>Electricity bill for the month of $month (@ Rs. $unitrate)</th></tr>
<tr><th rowspan='2'>No</th><th rowspan='2'>FlatNo</th><th rowspan='2'>Licensee</th><th colspan='3'>Readings & Consumption</th><th colspan='3'>Consumption Charges</th></tr>
<tr><th>Previous</th><th>Current</th><th>Units</th><th>Amount</th><th>Admissibility</th><th>Payable</th></tr>
</thead>
<tfoot>
<tr><th colspan='5'>Total</th><th>".number_format($tunits,2,'.','')."</th><th>Rs. ".number_format($tamount,2,'.','')."</th>
<th>Rs. ".number_format($tsubsidy,2,'.','')."</th><th>Rs. ".number_format($tpayable,2,'.','')."</th></tr>
<tr><th colspan='9'>Above readings have been taken on last date of $month and previous month.</th></tr>
</tfoot>
<tbody>$data</tbody>
</table></p>";

?>