<?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=isset($_POST['dbarea']) ? (int)$_POST['dbarea'] : 0;

            $areaname="All Areas";
            if($area>0){
               $getarea=f(q("SELECT * FROM tbl_area WHERE arid='$area'"));
               if(isset($getarea['arname']) && $getarea['arname']!=''){
                  $areaname=$getarea['arname'];
               }
            }
         ?>
      <title>Area Wise Due 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">
       <style type="text/css">
         th{
         font-size: 13px !important;
         }
         td{
         font-size: 12px !important;
         border:1px solid #b5b5b5;
         }
         table{

            width: 100%;

         }
         tr{

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

         }
         .report-actions{
            width:100%;
            padding:10px;
            text-align:right;
         }
         @media print{
            @page{
               size: A4 landscape;
               margin: 10mm;
            }
            .report-actions{ display:none; }
            body{ padding:0 !important; }
         }

      </style>
   </head>
   <body style="background-color: white; padding: 3%;">
      <div class="report-actions">
         <button type="button" class="btn btn-secondary" onclick="window.print()">Print</button>
         <a href="total_payment_wise_excel.php?stdt=<?php echo $stdt?>&enddt=<?php echo $enddt?>&dbarea=<?php echo $area?>" class="btn btn-primary" target="_blank">Export To Excel</a>
      </div>
      <br><center><h4>Area Wise Due Report for <?php echo $areaname?> from <?=$stdt?> to <?=$enddt?></h4></center><br>
      <table class="stripe">
         <thead>
            <tr>
               <th class="table-plus datatable-nosort">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

               $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);

               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 class="table-plus"><?=$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;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><br><br><br><br>
      <?php include('include/script.php'); ?>
      <script src="src/plugins/datatables/media/js/jquery.dataTables.min.js"></script>
      <script src="src/plugins/datatables/media/js/dataTables.bootstrap4.js"></script>
      <script src="src/plugins/datatables/media/js/dataTables.responsive.js"></script>
      <script src="src/plugins/datatables/media/js/responsive.bootstrap4.js"></script>
      <script>
         $('document').ready(function(){
         	$('.stripe').DataTable({
         		scrollCollapse: true,
         		autoWidth: false,
         		responsive: true,
               paging: false,
               lengthchange: false,
               searching: false,
         		columnDefs: [{
         			targets: "datatable-nosort",
         			orderable: false,
         		}],
         		"language": {
         			"info": "_START_-_END_ of _TOTAL_ entries",
         			searchPlaceholder: "Search"
         		}
         	});
         });
      </script>
   </body>
</html>

