<? include "top.php"; 
    
    $stdt=$_REQUEST['stdt'];
    $enddt=$_REQUEST['enddt'];
    $logusers=$_REQUEST['logusers'];
    $dbarea=$_REQUEST['dbarea'];
    $bl_area=$_REQUEST['bl_area'];
    
    $area_filter = ($bl_area != '0') ? " AND bl_area='$bl_area'" : "";
   
    $csadded=$_SESSION['murgibhai'];
            //echo "SELECT * FROM tbl_login WHERE l_uid='$csadded'";	
            $getlog=f(q("SELECT * FROM tbl_login WHERE l_uid='$csadded'"));
            $ltype=$getlog['l_type'];
            $uidd=$getlog['user_id'];

            
            if($ltype==2){

               if($logusers==0){
				   
						if($dbarea=='0'){
							
							$qury="SELECT * FROM `cbill` WHERE bl_user='$getlog[l_uid]' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter";
						}else{
							
							$qury="SELECT * FROM `cbill` WHERE bl_user='$getlog[l_uid]' AND bl_line='$dbarea' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter";
							
						}
                  
               }else{
				   
				   if($dbarea=='0'){
					    $qury="SELECT * FROM `cbill` WHERE  bl_com='$logusers' AND bl_user='$getlog[l_uid]' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter";
					   
				   }else{
					   
					   
					    $qury="SELECT * FROM `cbill` WHERE  bl_com='$logusers'  AND bl_line='$dbarea' AND bl_user='$getlog[l_uid]' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter";
					   
				   }
   
                 
   
               }

               //$query="SELECT * FROM cbill WHERE  AND bl_dt BETWEEN '$stdt' AND '$enddt' ORDER BY blid DESC";
            }elseif($ltype==1){

               if($logusers==0){


                  if($dbarea=='0'){
							
                     $qury="SELECT * FROM `cbill` WHERE bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter";
						}else{
                     $qury="SELECT * FROM `cbill` WHERE bl_user='$dbarea' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter";
						//	$query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  AND bl_line='$dbarea' AND bl_paydt BETWEEN '$stdt' AND '$enddt'");
							
						}

                 
               }else{
   
                 // $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  bl_ccom='$logusers' AND bl_paydt BETWEEN '$stdt' AND '$enddt'");

                  if($dbarea=='0'){
							
                     $qury="SELECT * FROM `cbill` WHERE bl_com='$logusers' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter";
						}else{
                     $qury="SELECT * FROM `cbill` WHERE bl_user='$dbarea' AND bl_com='$logusers' AND bl_paydt BETWEEN '$stdt' AND '$enddt' $area_filter";
						//	$query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  AND bl_line='$dbarea' AND bl_paydt BETWEEN '$stdt' AND '$enddt'");
							
						}
   
               }

               
            }else{

               //$getu=f(q("SELECT * FROM adduser WHERE custid='$getlog[l_read]'"));

               

               $qury="SELECT * FROM cbill WHERE bl_cid='$uidd'  AND bl_dt BETWEEN '$stdt' AND '$enddt' $area_filter ORDER BY blid DESC";
               // $getu=f(q("SELECT * FROM adduser WHERE custid=''"));
            }

//echo $qury;

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


            


            header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=billgeneratereport.xls");
         ?>
      
   
      <table class="stripe">
         <thead>
            <tr>
               <th class="table-plus datatable-nosort">Sl. No</th>
               <th>Customer Id</th>
               <th>Customer Name</th>
               <th>Phone</th>
               <th>MSO No</th>
               <th>Address</th>
               <th>Start Date</th>
               <th>End Date</th>
               <th>Bill No</th>
               <th>Current Amt</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]"));
               $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']);
              
               ?>
            <tr>
               <td class="table-plus"><?=$c?></td>
               <td><?=$row['custid']?></td>
               <td><?=$row['name']?></td>
               <td><?=$row['phone']?></td>
               <td><?=$row['msoacno']?></td>
               <td><?=$row['address']?></td>
               <td><?=$arrc['bl_stdt']?></td>
               <td><?=$arrc['bl_expiry']?></td>
               <td><?=$arrc['bl_no']?></td>
               <td><?=$arrc['bl_amt']?></td>
               <td><?php echo $billamt;?></td>
               <td><?php echo $arrc['bl_payamt'];?></td>
               <td><?php echo $duess;?></td>
            </tr>
            <?}    
             $totdue=floatval($totblamt)-floatval($totpay);
            
            
            
            ?>	

            <tr>
               <td colspan="10" style="text-align:right;font-size:14px !important;color:red;"><b>Total</b></td>
               
               <td style="text-align:right;font-size:14px !important;color:red;"><?php echo $totblamt;?></td>
               <td style="text-align:right;font-size:14px !important;color:red;"><?php echo $totpay;?></td>
               <td style="text-align:right;font-size:14px !important;color:red;"><?php echo $totdue;?></td>
            </tr>
         </tbody>
      </table>
      
