<? include "top.php"; 
    
        $stdt=$_REQUEST['stdt'];
         $enddt=$_REQUEST['enddt'];
         $csadded=$_SESSION['murgibhai'];
            $getlog=f(q("SELECT * FROM tbl_login WHERE l_uid='$csadded'"));
            $ltype=$getlog['l_type'];
            $custid=$getlog['user_id'];

             $area=$_REQUEST['dbarea'];


             $getmso=f(q("SELECT * FROM tbl_mso WHERE m_id='$area'"));
             $msoname=$getmso['m_name'];
             $paym=$_REQUEST['paymodes'];
             $paytype=$_REQUEST['paytype'];

              if($paytype=='All'){

                  $heading=$msoname."-Payment-Due";

               }elseif($paytype=='Payments'){

                     $heading=$msoname."-Payment";

               }elseif($paytype=='Due'){

                     $heading=$msoname."-Due";
                  }
            


            header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$heading.".xls");
         ?>
      
   
      <table style="width:100%;" border='1'>
         
            <tr>
               <th>Sl. No</th>
               <th>Customer Id</th>
               <th>Customer Name</th>
               <th>Address</th>
               <th>Phone</th>
               <th>MSO</th>
               <th>Total(in Rs.)</th>
               <th>Paid(in Rs.)</th>
               <th>Mode</th>
               <th>Due(in Rs.)</th>
            </tr>
        
        
            <?php

              
               
              if($ltype=='1'){

                   if($paytype=='All'){
                          if($paym=="All"){
                               $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  bl_mso='$area' AND bl_mso='$area' AND  bl_paydt BETWEEN '$stdt' AND '$enddt'");
                          }else{
                               $paymodes=mysqli_real_escape_string($con, $paym);
                               $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  bl_mso='$area' AND bl_paymd='$paymodes' AND bl_mso='$area' AND  bl_paydt BETWEEN '$stdt' AND '$enddt'");
                          }


                         
                  }elseif($paytype=='Payments'){


                         if($paym=="All"){
                   $paymodes='';
                      $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  bl_mso='$area' AND bl_payamt>0 AND bl_mso='$area' AND  bl_paydt BETWEEN '$stdt' AND '$enddt'");
              }else{
                   $paymodes=mysqli_real_escape_string($con, $paym);
                      $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  bl_mso='$area' AND bl_paymd='$paymodes' AND bl_payamt>0 AND bl_mso='$area' AND  bl_paydt BETWEEN '$stdt' AND '$enddt'");
              }

                      


                  }elseif($paytype=='Due'){
                            if($paym=="All"){
                   $paymodes='';
                   $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  bl_mso='$area' AND bl_due>0 AND bl_mso='$area' AND  bl_paydt BETWEEN '$stdt' AND '$enddt'");
              }else{
                   $paymodes=mysqli_real_escape_string($con, $paym);
                   $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE  bl_mso='$area' AND bl_paymd='$paymodes' AND bl_due>0 AND bl_mso='$area' AND  bl_paydt BETWEEN '$stdt' AND '$enddt'");
              }

                         
                  }

                 

               }elseif($ltype=='3'){


                      $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE bl_cid='$custid' AND  bl_paymd='$paymodes' AND bl_paydt BETWEEN '$stdt' AND '$enddt'");
                     // echo "SELECT * FROM `cbill` WHERE bl_cid='$custid' AND bl_paydt BETWEEN '$stdt' AND '$enddt'";
               }

               //}

               //echo $query;

               
               while($arrc=mysqli_fetch_array($query)){
                  //echo "hello";
                  //echo "SELECT * FROM `adduser` WHERE id='$arrc[bl_cid]' AND area_id='$area'";

                  if($area=='0' OR $area==''){

                     $getq="SELECT * FROM `adduser` WHERE id='$arrc[bl_cid]'";
                  }else{

                     $getq="SELECT * FROM `adduser` WHERE id='$arrc[bl_cid]' AND mso='$area'";
                  }
                  //echo $getq;
               $row=f(q($getq));
              // echo $row['id'];
               if($row['name']!=''){
                  $c++;
                  $total=floatval($total)+floatval($arrc['bl_amt']);
                  $paid=floatval($paid)+floatval($arrc['bl_payamt']);
                  $dues=floatval($dues)+floatval($arrc['bl_due']);

                   $getarea=f(q("SELECT * FROM tbl_area WHERE arid='$row[area_id]'"));  
                   $mso=$row['mso'];
                   

                     $getmso=f(q("SELECT * FROM tbl_mso WHERE m_id='$mso'"));
                     $msoname=$getmso['m_name'];
               ?>



            <tr style="border-bottom: 1px solid gray;">
               <td class="table-plus" style="text-align: center;"><?=$c?></td>
               <td><?=$row['custid']?></td>
               <td><?=$row['name']?></td>
               <td>Area: <?=$getarea['arname']?><br><?=$row['address']?></td>
               <td><?=$row['phone']?></td>
               <td><?=$msoname?></td>
                <td><?=$arrc['bl_amt']?></td>
               <td><?=$arrc['bl_payamt']?></td>
               <td><?=$arrc['bl_paymd']?></td>
               <td><?=$arrc['bl_due']?></td>
            </tr>
            <?} }?>  	
            
      </table><br>
      <table>
            <tr>
             <tr>
               <td></td>
               
                <td></td>
                 <td></td>
               <td></td>
               <td></td>
               <td><b>Total</b></td>
               <td><b><?=$total?></b></td>
                <td><b><?=$paid?></b></td>
                <td></td>
                 <td><b><?=$dues?></b></td>

            </tr> 

      </table>
      
