<?php
session_start();

if(!isset($_SESSION["sess_user"])) {
    header("Location: index.php");
    exit();
}
?>

<!DOCTYPE html>
  <?php  
  if(isset($_GET['search'])){ ?><html>
  
          
<head>
    <meta charset="utf-8" />
   <?php
 include('includes/config.php');
          $vendor=$_GET['vendor'];
          $fromdate = $_GET['from_date'];
          $todate =$_GET['to_date'];
          $results = mysqli_query($con, "SELECT * FROM `vendorAdditional_balance` as vb  WHERE vb.vendor_id ='".$vendor."' AND vb.balance_date BETWEEN '".$fromdate."' AND '".$todate."' ORDER BY vb.balance_date ASC LIMIT 1");

      $frdate = $_GET['from_date']; $frmdates =date("j-M-Y",strtotime($frdate)); 
      $todate =$_GET['to_date']; $tomdates = date("j-M-Y",strtotime($todate));
      
      
       while($rows = mysqli_fetch_assoc($results)) {
       
       ?> <title><?php $company = $rows['vendor_id']; echo ("$company.\n From $frmdates To $tomdates."); ?>
     
</title>
   
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <!-- Favicon icon -->
    <link rel="shortcut icon" href="images/logos/soft-lg-1.png" type="image/x-icon" >
 <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.1/css/bootstrap.css">
	<link rel="stylesheet" href="https://cdn.datatables.net/1.10.19/css/dataTables.bootstrap4.min.css">
	<link rel="stylesheet" href="https://cdn.datatables.net/buttons/1.5.2/css/buttons.bootstrap4.min.css">
	<link rel="stylesheet" href="https://cdn.datatables.net/responsive/2.2.3/css/responsive.bootstrap4.min.css">
 <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js"></script>
      <script src="main/assets/js/html2canvas.js"></script>
</head>



<!--<body onload="window.print()">-->
<body>
<div class="container">

<div class="page-header">
                                            
                                                
                                         
                                
                   <?php
if(!$_GET['hide_logo']){
    $img_file = 'images/logos/madinaconst-lgnew.png';

    // Get image MIME type and base64 encode the image
    $imageInfo = getimagesize($img_file);
    $src = 'data:' . $imageInfo['mime'] . ';base64,' . base64_encode(file_get_contents($img_file));

    // Display the image
    echo '<center><div class="logo"><img height="250" width="350" src="' . $src . '"></div></center>';
}
?>
    
                                       
   
    <br>
     
    <h3 align="center"><?php echo $rows['vendor_id']; ?></small></h3>
    <?php  $frdate = $_GET['from_date']; $frmdates =date("j-M-Y",strtotime($frdate)); 
      $todate =$_GET['to_date']; $tomdates = date("j-M-Y",strtotime($todate));
      echo  ("<center><h3>From $frmdates To $tomdates.<h3></center>");
       ?>

</div>


<?php
include('includes/config.php');



?>

 
<table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">                  
                        

                  <thead>
                      
                  <tr>
                     <th class="text-center">SNO</th>
               
                    <!--<th>Vendor</th>-->
                     <th class="text-center">Balance</th>
                      <th class="text-center">Date</th>
                  
                   
                  </tr>
                  </thead>
                  <tbody>

 <?php   include('includes/config.php');
       
          $vendor=$_GET['vendor'];
          $fromdate = $_GET['from_date'];
          $todate =$_GET['to_date'];
          
      $no=0;
     

 $result = mysqli_query($con, "SELECT * FROM `vendorAdditional_balance` as vb   WHERE vb.vendor_id ='".$vendor."' AND vb.balance_date BETWEEN '".$fromdate."' AND '".$todate."'  ORDER BY vb.balance_date ASC, vb.vab_id ASC ");

      
      
      
       while($row = mysqli_fetch_assoc($result)) {
       $no++;    
    
      $allbalance +=  $row['add_balance'];
    
     
       ?>
       
       
        <tr>
                     <td class="text-center"><?php echo $no; ?></td>
                     
                     
                    <td class="text-center"><?php echo  $row['add_balance']; ?></td>
                    <td class="text-center">
                     <?php $ordate = $row['balance_date']; echo date("d/m/Y", strtotime($ordate));
                     //$ordates =date("M-j-Y",strtotime($ordate)); echo $ordates; ?>
                     </td>
                   
                   
                     
                       
                      </tr>
                     
 <?php } 
           
        
 
 ?>
                  </tbody>
              <tfoot>
                  <tr>
                   
                    <th class="text-center">
                  Total <?php
// check all bill amount
include('includes/config.php');

$resultres = mysqli_query($con, "SELECT Sum(ship_total) as totalamount FROM `shipment` as s  WHERE s.vendor_id='$vendor' AND s.s_status='1'");
while($rowrev = mysqli_fetch_assoc($resultres)) {
    $totalbillamount = $rowrev['totalamount'];

    // Cast to integer to remove decimal part and trailing zeros
    $formattedTotal = (int)$totalbillamount;

    echo $formattedTotal;
}
?> </th>
                    <th class="text-center"> <?php echo $allbalance; ?></th>
                   
                    
                    <th class="text-center">
   New Total <?php
       $newbal = $totalbillamount +$allbalance;
       echo $newbal;
 ?></th>
                    
                    </tr>
                  </tfoot>
                </table>
                 </tbody>
    </table>
   
</div>
<?php } ?>
<!-- you need to include the shieldui css and js assets in order for the components to work -->
  <script src="https://code.jquery.com/jquery-3.3.1.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.datatables.net/1.10.19/js/dataTables.bootstrap4.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.bootstrap4.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>
    <script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.colVis.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.2.3/js/dataTables.responsive.min.js"></script>
    <script src="https://cdn.datatables.net/responsive/2.2.3/js/responsive.bootstrap4.min.js"></script>
    <script>
$(document).ready(function() {
    
  
    $('#example').DataTable( {
        dom: 'Bfrtip',
        //"paging": false,
          
        buttons: [
            { extend: 'copyHtml5', footer: true },
            { extend: 'excelHtml5', footer: true },
           // { extend: 'csvHtml5', footer: true },
            { extend: 'pdfHtml5',header:true, footer: true, orientation: 'portrait',
        pageSize: 'A4', customize:  function (doc) { 
                     
                     
                    doc.layout = 'lightHorizotalLines;'
                 //   doc.pageMargins = [25, 0, 30, 0];
                    doc.pageMargins = [45, 30, 50, 90];
                    doc.defaultStyle.fontSize = 8;
                    doc.styles.tableHeader.fontSize = 9;
                    doc.styles.title.fontSize = 10;
                    
                   // Get the table object from the PDF document
                    var table = doc.content[1].table;
                    
                    // Set the widths of the columns here
                    table.widths = ['20%', '60%', '20%'];
                    
                    
                    
                     // Set text alignment for td cells to center
                    for (var rowIndex = 0; rowIndex < table.body.length; rowIndex++) {
                        var row = table.body[rowIndex];
                        row.forEach(function(cell) {
                            cell.alignment = 'center';
                        });
                    }
                    
                    // Customize the tfoot style
                    if (table.footer && table.footer.length > 0) {
    var footerRow = table.footer[table.footer.length - 1];
    footerRow.forEach(function(cell) {
        cell.alignment = 'center';
        cell.fontSize = 7; // Adjust the font size as needed
    });
}
                  
                    
                    // Add the image to the beginning of the PDF
                    doc.content.splice(0, 0, {
                        alignment: 'center',
                        margin: [0, 0, 0, 0],
                        image: '<?php echo $src; ?>',
                        width: 200,
                        height: 150,
                    });
                    
                    
                    
                    
                    
                    
                           
         } }
        
            
        ]
    } );
} );


     </script>
    
<!-- Export a Table to PDF - END -->

</div>
    <?php } else { echo '<title>Record Not Found</title>'; } ?>
</body>
</html>