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

             $area=$_POST['dbarea'];


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

             if($paym=="All"){
                  $paymodes='';
             }else{
                  $paymodes=mysqli_real_escape_string($con, $paym);
             }



         ?>
      <title>Total Payment Report from <?=$stdt?> to <?=$enddt?></title>
      <!-- <link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/jquery.dataTables.css">
      <link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/dataTables.bootstrap4.css">
      <link rel="stylesheet" type="text/css" href="https://sarmac.b-cdn.net/plugins/datatables/media/css/responsive.dataTables.css">
      <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/sweetalert2@9"></script> -->
       

      <?php 

               if($paytype=='All'){

                  $heading="Payment/Due ";

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

                     $heading="Payment ";

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

                     $heading="Due ";
                  }

      ?>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
   </head>
   <body style="background-color: white; padding: 3%;">

      <div style="width:100%;padding:10px;text-align:right;">
            <a href="mso_pay_excel.php?stdt=<?php echo $stdt?>&enddt=<?php echo $enddt?>&dbarea=<?php echo $area?>&paymodes=<?php echo $paym?>&paytype=<?php echo $paytype?>" class="btn btn-primary" target="_blank">Export To Excel</a>
            <button id="print" class="btn btn-primary" onclick="printContent('printoutdiv');" >Print</button>

      </div>

        <div id="printoutdiv">


         <style type="text/css">
         th{
         font-size: 15px !important;
         padding: 5px;
         }
         td{
         font-size: 14px !important;
         padding-left: 4px;
         padding-right: 4px;
         }
         table{

            width: 100%;

         }
         tr{

            width: 100%;
         }
         thead > tr > td{
            background-color: #bad5e4;

         }

      </style>

      <br><center><h4><?php echo $heading?> Report for <?php echo $getmso['m_name']?> from <?=$stdt?> to <?=$enddt?></h4></center><br>
      <table class="stripe hover multiple-select-row data-table-export nowrap">
         <thead>
            <tr>
               <th class="table-plus datatable-nosort">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>
         </thead>
         <tbody>
            <?php

              
               // if($area=='0'){
               //    $query=mysqli_query($con,"SELECT * FROM `cbill` WHERE bl_paydt BETWEEN '$stdt' AND '$enddt'");

               // }else{
               //echo "SELECT * FROM `cbill` WHERE  bl_mso='$area' AND bl_paydt BETWEEN '$stdt' AND '$enddt'";
              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>
            <?} }?>	
         </tbody>
         <tfoot>
            <tr>
               <td class="table-plus"></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> 
            </tfoot>
      </table><br><br><br><br>

   </div>

   <script>
function printContent(el){
var restorepage = $('body').html();
var printcontent = $('#' + el).clone();
$('body').empty().html(printcontent);
window.print();
$('body').html(restorepage);
}
</script>
      <?php //include('include/script.php'); ?>
     
   </body>
</html>

