Your IP : 216.73.216.40


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

<h3>Licensee Billing</h3>
<center>

<?php
$tbl = "el_billing";

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

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

//check meter reading records in database
if (mysql_num_rows(mysql_query("select distinct month from el_reading where month='$month'")));


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&ext=$ext&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;";

echo "Calculate <font size='+1'>[ </font>";
if ($month == previous_month()) {
	echo "<a href='?pg=$pg&month=$month&ext=$ext&recal=1'>Bill</a> | ";
}
echo "<a href='?pg=$pg&month=$month&ext=$ext&recal=2'>Arrear</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;
	$arrearrate = $o->arrear_rate;
}

if ($recal == 1 && mysql_num_rows(mysql_query("select * from el_reading where month='$month'"))) {
	//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;
		$flattype = $o->flattype;
		$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 = $flattype;
                        $category = 'None';
                        $external = 'Vacant/Common';
                }

		//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;
		} else {
			$userfees = 0.00;
			$subsidymode = 'None';
			$subsidy = 0.00;
		}
		//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;
				$licensee .= '+';
			}
		} else {
			$additionalunits = 0;
		}

		$consumedunits += $additionalunits;
		$consumedcharge = $consumedunits * $unitrate;
                
		//calculate payable
               	$excessunits = 0.00;
                $excesscharge = 0.00;
                
		if ($sharing == 'Yes') {
			$subsidy += $subsidy;
			$userfees += $userfees;
		}
                if ($subsidymode == 'Rupees') {
			if ($consumedcharge > $subsidy) {
               			$excesscharge = $consumedcharge - $subsidy;
				$payable = $excesscharge;
               	        	$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()");
	}
}

if ($recal == 2) {
        $rs = mysql_query("select * from $tbl where month='$month' order by id");
        while ($o = mysql_fetch_object($rs)) {
		$id = $o->id;
		$unitrate = $o->unitrate;
		$subsidy = $o->subsidy;
		$subsidymode = $o->subsidymode;
		$consumedunits = $o->curreading - $o->prevreading + $addunits;
		$consumedcharge = $consumedunits * $unitrate;
        	$arrear = 0.00;
		
		//calculate arrear
                if ($subsidymode == 'Rupees') {
			$arrearcharge = $consumedcharge + ($consumedunits * $arrearrate);
			if ($arrearcharge > $subsidy) {
				$arrear = $arrearcharge - $consumedcharge;
			}
                } else if ($subsidymode == 'Units') {
			if ($consumedunits > $subsidy) {
                       		$arrear = ($consumedunits - $subsidy) * $arrearrate;
			}
                } else if ($subsidymode == 'None') {
                        $arrear = $consumedunits * $arrearrate;
                
		} else { //for external licensees
                        $arrear = $consumedunits * $arrearrate;
		}

		mysql_query("update $tbl set arrear=$arrear where id=$id");
	}
}

if ($ext == 1) {
	include "elexternalbills.inc";
} else {
	include "elinternalbills.inc";
}

?>
</center>