<?php include "top.php"; 

$stdt=$_REQUEST['stdt'];
$enddt=$_REQUEST['enddt'];
$area=isset($_REQUEST['dbarea']) ? (int)$_REQUEST['dbarea'] : 0;

$csadded=$_SESSION['murgibhai'];
$getlog=f(q("SELECT * FROM tbl_login WHERE l_uid='$csadded'"));
$ltype=$getlog['l_type'];
$custid=$getlog['user_id'];

$area_filter = ($area>0) ? " AND bl_area='$area'" : "";

if($ltype==1){
	$qury="SELECT * FROM `cbill` WHERE bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter ORDER BY blid DESC";
}elseif($ltype==2){
	$qury="SELECT * FROM `cbill` WHERE bl_user='$getlog[l_uid]' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter ORDER BY blid DESC";
}else{
	$qury="SELECT * FROM `cbill` WHERE bl_cid='$custid' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter ORDER BY blid DESC";
}

$query=mysqli_query($con,$qury);

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=total_payment_wise.xls");
?>

<table border="1">
	<thead>
		<tr>
			<th>Sl. No</th>
			<th>Lineman</th>
			<th>Customer Id</th>
			<th>Customer Name</th>
			<th>Area</th>
			<th>Phone</th>
			<th>Address</th>
			<th>Start Date</th>
			<th>End Date</th>
			<th>Bill No</th>
			<th>Current Amt</th>
			<th>Disc</th>
			<th>Bill Amt(incl Prev Due)</th>
			<th>Paid Amt</th>
			<th>Due Amt</th>
		</tr>
	</thead>
	<tbody>
		<?php
		while($arrc=mysqli_fetch_array($query)){$c++;
			$row=f(q("SELECT * FROM `adduser` WHERE id=$arrc[bl_cid]"));
			if(!isset($row['name']) || $row['name']==''){ continue; }

			$billamt=floatval($arrc['bl_amt'])+floatval($arrc['bl_prevdue']);
			$duess=floatval($billamt)-floatval($arrc['bl_payamt']);

			$totblamt=floatval($totblamt)+floatval($billamt);
			$totpay=floatval($totpay)+floatval($arrc['bl_payamt']);

			$time=strtotime($arrc['bl_stdt']);
			
			$userarea_name="";
			if(isset($row['area_id']) && $row['area_id']!=''){
				$getuarea=f(q("SELECT * FROM tbl_area WHERE arid='$row[area_id]'"));
				$userarea_name=isset($getuarea['arname']) ? $getuarea['arname'] : "";
			}

			$getline=f(q("SELECT * FROM tbl_login WHERE l_id='$arrc[bl_line]'"));
			$lineman_name=isset($getline['l_name']) ? $getline['l_name'] : "";
			
			$bill_no="";
			if(isset($arrc['bl_no']) && $arrc['bl_no']!=''){
				$bill_no=$arrc['bl_no'];
			}elseif(isset($arrc['bl_billno']) && $arrc['bl_billno']!=''){
				$bill_no=$arrc['bl_billno'];
			}elseif(isset($arrc['bl_bill_no']) && $arrc['bl_bill_no']!=''){
				$bill_no=$arrc['bl_bill_no'];
			}elseif(isset($arrc['bill_no']) && $arrc['bill_no']!=''){
				$bill_no=$arrc['bill_no'];
			}elseif(isset($arrc['blid']) && $arrc['blid']!=''){
				$bill_no=$arrc['blid'];
			}
		?>
		<tr>
			<td><?=$c?></td>
			<td><?php echo $lineman_name;?></td>
			<td><?=$row['custid']?></td>
			<td><?=$row['name']?></td>
			<td><?php echo $userarea_name;?></td>
			<td><?=$row['phone']?></td>
			<td><?=$row['address']?></td>
			<td><?=$arrc['bl_stdt']?></td>
			<td><?=$arrc['bl_expiry']?></td>
			<td><?php echo $bill_no;?></td>
			<td><?=$arrc['bl_amt']?></td>
			<td><?=$arrc['bl_disc']?></td>
			<td><?php echo $billamt;?></td>
			<td><?php echo $arrc['bl_payamt'];?></td>
			<td><?php echo $duess;?></td>
		</tr>
		<?php } 
		$totdue=floatval($totblamt)-floatval($totpay);
		?>
		<tr>
			<td colspan="12" style="text-align:right;color:red;"><b>Total</b></td>
			<td style="text-align:right;color:red;"><?php echo $totblamt;?></td>
			<td style="text-align:right;color:red;"><?php echo $totpay;?></td>
			<td style="text-align:right;color:red;"><?php echo $totdue;?></td>
		</tr>
	</tbody>
</table>
