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/elrmailing.inc

<h3>Receipt Mailing</h3>
<center>

<?php
$tbl = 'el_billing';

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

$mailing = $_REQUEST['mailing'];
$external = $_REQUEST['external'];
if (!$external) $external = 'No';

echo "<font size='+1'>[ </font>";
if ($external == 'Yes') {
        echo "<a href='?pg=$pg&month=$month&external=No'>Internal</a> | <font color='red' size='+1'>External</font>";
} else {
        echo "<font color='red' size='+1'>Internal</font> | <a href='?pg=$pg&month=$month&external=Yes'>External</a>";
}
echo "<font size='+1'> ]</font>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
Select month <select name='month' onchange=\"javascript: document.location.href='?pg=$pg&external=$external&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;
<font size='+1'>[ </font>
<a href='?pg=$pg&id=$id&month=$month&external=$external&mailing=1'>Send Mail".($id ? '' : 's')."</a>
<font size='+1'> ] </font>
<input type='hidden' name='external' value='$external'>
<br>";

$sql = "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 category not in ('Common','None') and month='$month'";
$sql .= ($id ? " and id=$id" : '');
$sql .= ($external ? " and external='$external'" : '');
$sql .= " order by left(flatno, pos-1), mid(flatno, pos, length(flatno)-pos+1)+0";

echo "<div align='right'>
Individual <select name='id' onchange=\"javascript: document.location.href='?pg=$pg&month=$month&id='+this.value\">
<option value=''>Licensee</option>";
$rs = mysql_query($sql);
while ($o = mysql_fetch_object($rs)) {
        echo "<option value='$o->id'";
        if ($o->id == $id) echo " selected";
        echo ">$o->flatno ($o->licensee)</option>";
}
echo "</select>
</div></center>";

$rs = mysql_query("select * from el_config");
if (mysql_num_rows($rs)) {
	$o = mysql_fetch_object($rs);
	$unitrate = $o->unitrate;
	$subsidymode = $o->subsidymode;
	$receipt = $o->receipt;
	$receiptto = $o->receiptto;
	$summary = $o->summary;
	$summaryto = $o->summaryto;
	$bill = $o->bill;
	$billto = $o->billto;
}

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

        $prevreading = $o->prevreading;
        $curreading = $o->curreading;
        $addunits = $o->addunits;

        //get licensee details
        $sr = mysql_query("select * from el_licensee where flatno='$flatno'");
        while ($q = mysql_fetch_object($sr)) {
                $fullname = $q->fullname;
                $designation = $q->designation;
                $email = $q->email;
                $category = $q->category;
                $external = $q->external;
                $sharing = $q->sharing;
        }
        
	$consumedunits = $curreading - $prevreading + $addunits;
        $consumedcharge = $consumedunits * $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 {
		//calculate payable
	        $excessunits = 0.00;
        	$excesscharge = 0.00;
	        if (!$subsidy) $subsidy = 0.00;

	        if ($subsidymode == 'Rupees') {
			if ($consumedcharge > $subsidy) {
               			$excesscharge = $consumedcharge - $subsidy;
				$payable = $excesscharge;
	                       	$excessunits = $payable;
			} else $payable = 0.00;
	        } else if ($subsidymode == 'Units') {
			if ($consumedunits > $subsidy) {
                	       	$excessunits = $consumedunits - $subsidy;
                       		$excesscharge = $excessunits * $unitrate;
				$payable = $excesscharge;
			} else $payable = 0.00;
	        } else if ($subsidymode == 'None') {
        	        $excessunits = $consumedunits;
                	$excesscharge = $excessunits * $unitrate;
	                $payable = $excesscharge;
		}
	}
	//payable with userfees
	$payable += $userfees;
	
	//summarized figures
	$tunit += $consumedunits;
        $tcharge += $consumedcharge;
        $tamount += $totalamount;
        $tsubsidy += $subsidy;
        $tpayable += $payable;

        $data = "<tr>
        <td align='center'>$meterno</td>
        <td align='center'>$contactedload</td>
        <td align='center'>$prevreading</td>
        <td align='center'>$curreading</td>
        <td align='center'>".number_format($consumedunits,2)."</td>
       	<td align='center'>Rs.".number_format($consumedcharge,2)."</td>";

        if ($external == 'Yes') {
        	$data .= "<td align='center'>Rs.".number_format($fixedcharge,2)."</td>
                <td align='center'>Rs.".number_format(round($subtotal),2)."</td>
                <td align='center'>Rs.".number_format($plcharge,2)."</td>
                <td align='center'>Rs.".number_format($edcharge,2)."</td>
                <td align='center'>Rs.".number_format($rscharge,2)."</td>
                <td align='center'>Rs.".number_format($totalamount,2)."</td>";
        } else {
                $data .= "<td align='center'>".($subsidymode=='Rupees' ? 'Rs.' : '').number_format($subsidy,2).($subsidymode=='Units' ? ' Units' : '')."</td>
                <td align='center'>".number_format($excessunits,2)."</td>
        	<td align='center'>Rs.".number_format($excesscharge,2)."</td>";
        }
        $data .= "<td  align='center'>Rs.".number_format($userfees,2)."</td>
        <td align='center'>Rs.".number_format($payable,2)."</td></tr>";

	$str = "<div align='right'>Ref.No.: IIIT-A/iLight-$id/$month<br>Date: ".date("d/m/Y")."</div><b>
        	To-<dir> 
                	$fullname ($email)<br>
                	$flatno, IIITA Campus, Devghat, Jhalwa<br>
			Allahabad-211012 (UP) INDIA
        	</dir></b>
		<center>
		<table width='100%' border='1' style='border-collapse: collapse'>
		<thead>";

	if ($external == 'Yes') {
		$str .= "<tr><th colspan='14'><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'>MeterNo</th>
		<th rowspan='2'>Contacted<br>Load</th>
		<th colspan='2'>KWH Meter Reading</th>
		<th colspan='2'>Consumption</th>
		<th colspan='8'>Connection Charges</th></tr>
		<tr><th>Previous</th><th>Current</th><th>Units</th><th>Charge (a)</th>
		<th>Fixed (b)</th><th>Total (a+b)</th><th>PLC (c)</th><th>ED (d)</th><th>Sur (e)</th><th>Total (A=a+b+c+d+e)</th><th>Rent (B)</th><th>Payable (A+B)</th></tr>
		</thead>
		<tfoot>
		<tr><th colspan='13' align='right'>Total</th><th>Rs.".number_format($tpayable,2)."</th></tr>
		<tr><th colspan='14'>Above readings have been taken on the last dates of this and previous month.</th></tr>
		</tfoot>
		<tbody>$data</tbody>
		</table></center><br>
		<p>$bill</p>";
	} else {
		$str .= "<tr><th colspan='10'><h3>Electricity payable for the month of $month</h3>
		(Unit rate: Rs.$unitrate/unit, admissibility and user charges are applicable category-wise)</th></tr>
		<tr><th rowspan='2'>MeterNo</th><th colspan='2'>Readings</th><th colspan='2'>Consumed</th><th rowspan='2'>Admisibility</th>
		<th rowspan='2'>Excess</th><th colspan='2'>Charges</th><th rowspan='2'>Payable<br>(A+B)</th></tr>
		<tr><th>Previous</th><th>Current</th><th>Units</th><th>Charges</th><th>Consumption (A)</th><th>User (B)</th></tr>
		</thead>
		<tfoot>
		<tr><th colspan='9' align='right'>Total</th><th>Rs.".number_format($tpayable,2)."</th></tr>
		<tr><th colspan='10'>Above readings have been taken on the last dates of this and previous month.</th></tr>
		</tfoot>
		<tbody>
		<tr><td align='center'>$meterno</td><td align='center'>$prevreading</td><td align='center'>$curreading</td>
		<td align='center'>".number_format($consumedunits,2)."</td>
		<td align='center'>Rs. ".number_format($consumedcharge,2)."</td>
		<td align='center' nowrap>".($subsidymode=='Rupees' ? 'Rs.' : '').number_format($subsidy,2).($subsidymode=='Units' ? ' Units' : '')."</td>
		<td align='center'>".($subsidymode=='Rupees' ? 'Rs.' : '').number_format($excessunits,2).($subsidymode=='Units' ? ' Units' : '')."</td>
		<td align='center' nowrap>Rs.".number_format($excesscharge,2)."</td><td align='center' nowrap>Rs.".number_format($userfees,2)."</td>
		<td align='center' nowrap>Rs.".number_format($payable,2)."</td></tr>
		</tbody>
		</table></center><br>";
		if ($category == 'PhD Sponsored') $str .= "<p>$bill</p>";
		else $str .= "<p>$receipt</p>";
	}
	
	if ($mailing) {
		$mailbody = $str;
		if ($receiptto) $email = $receiptto;

                if (strpos($email, ',')) {
			$emails = explode(',', $email);
			foreach($emails as $email) {
				if (sendmail($email, "Electricity charges for the month of $month", $mailbody) == TRUE)
					echo "[$i OK]";
				else
					echo "[$i XX]";
			}
		} else if ($email) {
                	if (sendmail($email, "Electricity charges for the month of $month", $mailbody) == TRUE) 
				echo "[$i OK]";
			else 
				echo "[$i XX]";
		}
		sleep(9);
	}
	echo "<hr>(Receipt #$i) $str";
	$tunit = 0; $tcharge = 0; $tamount = 0; $tsubsidy = 0; $tpayable = 0;
}
if ($mailing) mysql_query("update el_config set datemailing=now()");

?>
</center>