<?php include "top.php"; ?>
<!DOCTYPE html>
<html>
   <head>
      <?php include('include/head.php'); 
         $stdt=$_POST['stdt'];
         $enddt=$_POST['enddt'];
         $logusers=$_POST['logusers'];
		 $dbarea=$_POST['dbarea'];
       if($dbarea=='0'){

         $dbarea='0';
       }else{

         $dbarea=md5($dbarea);
       }
         if($logusers=='0'){

            $comname="All Company";
         }else{

            $getc=f(q("SELECT * FROM tbl_company WHERE c_id='$logusers'"));
            $comname=$getc['c_name'];
         }
         ?>
      <title>Due Report for <?=$stdt?> and <?=$enddt?> for <?php echo $comname?></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>
      <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">
            <a href="billgenerate_report_excel.php?stdt=<?php echo $stdt?>&enddt=<?php echo $enddt?>&logusers=<?php echo $logusers?>&dbarea=<?php echo $dbarea?>" class="btn btn-primary" target="_blank">Export To Excel</a>
            <button type="button" class="btn btn-secondary" onclick="window.print()">Print</button>
            

      </div>

   
   <br><center><h4>Due Report from <?php echo $stdt?> to <?php echo $enddt?> for <?php echo $comname?></h4></center><br>
      <table class="stripe">
         <thead>
            <tr>
               <th class="table-plus datatable-nosort">Sl. No</th>
               <?php if($logusers=='0'){?><th>Company</th><?php } ?>
			   <th>Login User</th>
			   <th>Lineman</th>
               <th>Customer Id</th>
               <th>Customer Name</th>
               <th>Phone</th>
               <th>Address</th>
               <th>Start Date</th>
               <th>End Date</th>
               <th>Month</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
            $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'];
            //echo $ltype;
            
            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'";
						}else{
							
							$qury="SELECT * FROM `cbill` WHERE bl_user='$getlog[l_uid]' AND bl_paydt BETWEEN '$stdt' AND '$enddt'";
							
						}
                  
               }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'";
					   
				   }else{
					   
					   
					    $qury="SELECT * FROM `cbill` WHERE  bl_com='$logusers'  AND bl_user='$getlog[l_uid]' AND bl_paydt BETWEEN '$stdt' AND '$enddt'";
					   
				   }
   
                 
   
               }

               //$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'";
						}else{
                     $qury="SELECT * FROM `cbill` WHERE bl_user='$dbarea' AND bl_paydt BETWEEN '$stdt' AND '$enddt'";
						//	$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'";
						}else{
                     $qury="SELECT * FROM `cbill` WHERE bl_user='$dbarea' AND bl_com='$logusers' AND bl_paydt BETWEEN '$stdt' AND '$enddt'";
						//	$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' ORDER BY blid DESC";
               // $getu=f(q("SELECT * FROM adduser WHERE custid=''"));
            }

//echo $qury;

               $query=mysqli_query($con,$qury);
           
               
               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']);

               $getco=f(q("SELECT * FROM tbl_company WHERE c_id='$row[com_id]'"));
            $coname=$getco['c_name'];
            $time=strtotime($arrc['bl_stdt']);
            $month=date("F",$time);
            $year=date("Y",$time);
			
			$getline=f(q("SELECT * FROM tbl_login WHERE l_id='$arrc[bl_line]'"));
			$lineman_name=$getline['l_name'];
			
			$getlogged=f(q("SELECT * FROM tbl_login WHERE l_uid='$arrc[bl_user]'"));
			$logged_user=$getlogged['l_name'];


               ?>
            <tr>
               <td class="table-plus"><?=$c?></td>
               <?php if($logusers=='0'){?> <td><?php echo $coname;?></td> <?php } ?>
			   <td><?php echo $logged_user;?></td>
			   <td><?php echo $lineman_name;?></td>
               <td><?=$row['custid']?></td>
               <td><?=$row['name']?></td>
               <td><?=$row['phone']?></td>
               <td><?=$row['address']?></td>
               <td><?=$arrc['bl_stdt']?></td>
               <td><?=$arrc['bl_expiry']?></td>
               <td><?php echo $month; echo  $year;?></td>
               <?php
               $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'];
               }
               ?>
               <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="<?php echo ($logusers=='0') ? 14 : 13; ?>" 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>
      <?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>
      <!-- buttons for Export datatable -->
      <script src="src/plugins/datatables/media/js/button/dataTables.buttons.js"></script>
      <script src="src/plugins/datatables/media/js/button/buttons.bootstrap4.js"></script>
      <script src="src/plugins/datatables/media/js/button/buttons.print.js"></script>
      <script src="src/plugins/datatables/media/js/button/buttons.html5.js"></script>
      <script src="src/plugins/datatables/media/js/button/buttons.flash.js"></script>
      <script src="src/plugins/datatables/media/js/button/pdfmake.min.js"></script>
      <script src="src/plugins/datatables/media/js/button/vfs_fonts.js"></script>
      <script>
         $('document').ready(function(){
         	$('.data-table').DataTable({
         		scrollCollapse: true,
         		autoWidth: false,
         		responsive: true,
               paging: false,
               lengthchange: false,
               searching: false,
         		columnDefs: [{
         			targets: "datatable-nosort",
         			orderable: false,
         		}],
         		"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
         		"language": {
         			"info": "_START_-_END_ of _TOTAL_ entries",
         			searchPlaceholder: "Search"
         		},
         	});
         	$('.data-table-export').DataTable({
         		scrollCollapse: true,
         		autoWidth: false,
         		responsive: true,
               paging: false,
				lengthchange: false,
				searching: false,
         		columnDefs: [{
         			targets: "datatable-nosort",
         			orderable: false,
         		}],
         		"lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
         		"language": {
         			"info": "_START_-_END_ of _TOTAL_ entries",
         			searchPlaceholder: "Search"
         		},
         		dom: 'Bfrtip',
         		buttons: [
         		'copy', 'csv', 'pdf', 'print'
         		]
         	});
         	var table = $('.select-row').DataTable();
         	$('.select-row tbody').on('click', 'tr', function () {
         		if ($(this).hasClass('selected')) {
         			$(this).removeClass('selected');
         		}
         		else {
         			table.$('tr.selected').removeClass('selected');
         			$(this).addClass('selected');
         		}
         	});
         	var multipletable = $('.multiple-select-row').DataTable();
         	$('.multiple-select-row tbody').on('click', 'tr', function () {
         		$(this).toggleClass('selected');
         	});
         });
      </script>
      <script type="text/javascript"></script>
   </body>
</html>

