Your IP : 216.73.216.40


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

<h3>Meter Reading</h3>
<center>

<?php
$tbl = "el_reading";
$month = $_REQUEST["month"];
if (!$month) $month = previous_month();//date("M-Y");
//if (!$month) $month = previous_month1(date("m-Y"));//date("M-Y");

$flatnos = $_POST["flatno"];
$readings = $_POST["reading"];
$refresh = $_REQUEST["refresh"];
$dated = date("Y-m-d");

if ($_POST["submit"] == 'SAVE') {
	$no = 0;
	foreach ($flatnos as $flatno) {
		if (mysql_num_rows(mysql_query("select * from $tbl where month='$month' and flatno='$flatno'"))) {
			//set null reading to 0
			if (!$readings[$no]) $readings[$no] = 0; 
			mysql_query("update $tbl set reading=".$readings[$no].",datereading='$dated' 
			where month='$month' and flatno='$flatno'");
		} else {
                       	mysql_query("insert into $tbl(flatno,month,reading,datereading,dated) 
			values('$flatno','$month',".$readings[$no].",'$dated',now())");
		}
		$no++;
	}
	mysql_query("update el_config set datereading=now()");
}

echo "Month <select name='month' onchange=\"javascript: document.location.href='?pg=$pg&month='+this.value\">";

if (!mysql_num_rows(mysql_query("select distinct month from $tbl where month='$month'")))
        echo "<option value='$month'>$month</option>";

$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>";
?>

<table width='80%' border='1' style='border-collapse: collapse'>
<tr><th rowspan='2'>No</th><th rowspan='2'>SiteNo</th><th rowspan='2'>Licensee</th><th rowspan='2'>MeterNo</th>
<th colspan='2'>Readings</th><th rowspan='2'>Dated</th></tr><tr><th>Previous</th><th>Current</th></tr>

<?php

$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 el_flats order by left(flatno, pos-1), mid(flatno, pos, length(flatno)-pos+1)+0");

while ($o = mysql_fetch_object($rs)) {
        $i++;
        $flatno = $o->flatno;
        $flattype = $o->flattype;
        $meterno = $o->meterno;
        $prevreading = $o->reading;
        //get category of licensee
        $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;
                $sharing = $q->sharing;
                $external = $q->external;
	} else {
		$fullname = $flattype;
		$category = 'None';
		$external = 'Vacant/Common';
		$sharing = 'No';
        }
        //get current reading
        $sr = mysql_query("select * from $tbl 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;
                $dated = $q->datereading;
	} else {
                $curreading = 0;
	}
        //get previous reading
        if ($curreading) { 
		$sql = "select reading from $tbl where flatno='$flatno' and id<$id order by id desc limit 0,1";
        } else {
		$sql = "select reading from $tbl where flatno='$flatno' order by id desc limit 0,1";
        }
	$sr = mysql_query($sql);
        if (mysql_num_rows($sr)) {
                $q = mysql_fetch_object($sr);
                $prevreading = $q->reading;
	}
        echo "<tr><td>$i</td><th>$flatno <input type='hidden' name='flatno[]' value='$flatno'></th>
	<td>$fullname (".($external == 'Yes' ? 'External' : ($external == 'No' ? 'Internal' : $external)).")</td>
	<td>$meterno</td><td>$prevreading</td><th><input type='text' name='reading[]' size='10' value='$curreading'></th><td>$dated</td></tr>";
}

//if ($month == previous_month())
	echo "<tr><th colspan='7'><input type='submit' name='submit' value='SAVE'></th></tr>";

?>
</table>
</center>