<?php
session_start();

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

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

      $frdate = $_GET['from_date']; $frmdates =date("j-M-Y",strtotime($frdate)); 
      $todate =$_GET['to_date']; $tomdates = date("j-M-Y",strtotime($todate));
      
         $billNo = isset($_GET['bill_no']) ? $_GET['bill_no'] : '';
         $billDate = isset($_GET['bill_date']) ? $_GET['bill_date'] : '';

          $preBalance = isset($_GET['previous_bal']) ? $_GET['previous_bal'] : '';
                  $details = isset($_GET['details']) ? $_GET['details'] : '';
      
      
       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>");
       ?>
    <?php } ?>
</div>


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



?>


 <div class="row">
    <div class="col-md-4">
    <?php if($billNo) { ?>
    <b>Bill No <?php echo $billNo; ?></b><br>
   
    
    <?php }  ?>   
    <?php if($details) { ?>
     <b>Details: <?php echo $details;  ?></b>
     <?php }?>
    </div>
    <div class="col-md-4">
        
    </div>
    
    <div class="col-md-4">
        <?php if($billDate) { ?>
    <b>Date <?php echo $billDate; ?></b>
    
    <?php }  ?>   
    </div>
         
</div> 
    
<table id="example" class="table table-striped table-bordered dt-responsive nowrap" style="width:100%">                  
                        

                  <thead>
                      
                  <tr>
                     <th class="text-center">S No</th> 
                    <th class="text-center">SM No</th>
                     <th class="text-center">Date</th>
                    <!--<th>Vendor</th>-->
                    <th class="text-center">Slip No</th>
                    <th class="text-center">Vehicle</th>
                    <th class="text-center">Item</th>
                    <th class="text-center">Qty</th>
                    <th class="text-center">Measurement</th>
                    <th class="text-center">Rate</th>
                    <th class="text-center">Amount</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 `shipment` as s    WHERE s.vendor_id ='".$vendor."' AND s.s_date BETWEEN '".$fromdate."' AND '".$todate."' AND s.s_status='1' ORDER BY s.s_date ASC, s.sh_id ASC");

      
      
      
       while($row = mysqli_fetch_assoc($result)) {
       $no++;    
      $v = $row['measurement_total'];
    
      $amount = $row['ship_total'];
      $cost =$row['allmcost_total'];
      $profitcost =  $amount - $cost;
      
      $totalcost +=$cost;
      $alltotalship +=$amount;
      $totalprofit +=$profitcost;
       ?>
                  <tr>
                      <td class="text-center"><?php echo $no; ?></td>
                      <td class="text-center"><?php echo $row['order_num']; ?></td>
                       <td class="text-center">
                     <?php $ordate = $row['s_date']; echo date("d/m/Y", strtotime($ordate));
                     //$ordates =date("M-j-Y",strtotime($ordate)); echo $ordates; ?></td>
                      <td class="text-center"><?php echo  $row['slip_no']; ?></td>
                       <td class="text-center"><?php echo  $row['measure_id']; ?></td>
                      <!--<td><h5><b><?php echo $row['vendor_id']; ?></b></h5></td>-->
                      <td class="text-center"><?php echo  $row['material_id']; ?></td>
                      <td class="text-center"><?php echo  $row['qty']; ?></td>
                      <td class="text-center"><?php echo $v; ?></td>
                      <td class="text-center"><?php echo  $row['s_rate']; ?></td>
                      <td class="text-center"><?php echo  $row['ship_total']; ?></td>
                     
                       
                      </tr>
                     
 <?php } ?>
                  </tbody>
              <tfoot>
                  <tr>
                    <th></th>
                    <th></th>
                    <th></th>
                     <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th></th>
                    <th class="text-center"><?php echo $alltotalship; ?>
   <?php if($preBalance){ $totalbillAmount = $preBalance + $alltotalship; } ?>
                    </th>
                    
                    </tr>
                  </tfoot>
                </table>
                 </tbody>
    </table>
    
      <div class="row">
        <div class="col-md-4">
            
        </div>
        
        <div class="col-md-4">
            
        </div>
        
        <div class="col-md-4">
         
          <?php
    

    // Use logical OR instead of bitwise OR
    if ($preBalance) { ?>
        <b>Previous Balance: <?php echo $preBalance ?: '0'; ?></b><br>
        <b>Total Bill Amount: <?php echo $totalbillAmount ?: $alltotalship; ?></b>
    <?php } ?>
        </div>
    </div>
<br><br>
   
</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 = [45, 30, 50, 90];
                    
                    //doc.pageMargins = [45, 20, 50, 0];
                  doc.pageMargins = [45, 30, 50, 90];
                    doc.defaultStyle.fontSize = 6.4;
                    doc.styles.tableHeader.fontSize = 7;
                    doc.styles.title.fontSize = 10;
                  
                    var table = doc.content[1].table;

// Set the widths of the columns here
table.widths = ['4%', '10%', '10%', '10%','10%', '10%', '10%', '12%', '10%', '14%'];

var totalshipment = '<?php echo $alltotalship; ?>'; // Similarly for totalShipment




                        // Assigning PHP variables to JavaScript, ensuring they are numeric
    var billNo = '<?php echo $billNo; ?>';
    var billDate = '<?php echo $billDate; ?>';
         var details = '<?php echo $details; ?>'; // Similarly for totalBillAmount

    var preBalance = '<?php echo $preBalance; ?>';  // Make sure this is treated as a number
    var totalshipment = '<?php echo $alltotalship; ?>'; // Similarly for totalShipment
    var totalBillAmount = '<?php echo $totalbillAmount; ?>'; // Similarly for totalBillAmount

                    // Set custom header content
var headerContent = [
    [
        { text: billNo ? 'Bill No: ' + billNo : '', alignment: 'left', fontSize: 11,italics: true, bold: true },
        { text: billDate ? '<?php echo date("j-M-Y", strtotime($billDate)); ?>' : '', alignment: 'right',italics: true, fontSize: 11, bold: true }
    ],
    [
        { text: details ? details : '', alignment: 'left', fontSize: 9, bold: true, italics: true, colSpan: 2 }
    ]
];

                    // Add the header to the PDF
                    doc.content.unshift({
                        margin: [0, 0, 0, 10], // Adjust margin for the header
                        table: {
                            widths: ['50%', '50%'], // Split into two columns (left and right)
                            body: headerContent
                        },
                        layout: 'noBorders' // Ensure no borders around the header
                    });

// 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';
    });
}


// Assuming columnIndex is the index of the column that should not break across pages
var columnIndex = 0; // Change this to the actual index of the column

// Set the pageBreak property for the entire column
table.body.forEach(function(row) {
    row[columnIndex].pageBreak = 'avoid';
});
                    
                    // 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 = 5; // Adjust the font size as needed
    });
}
                  
                    
                    
                 <?php if(!$_GET['hide_logo']){ ?> 
    doc.content.splice(0, 0, {
        alignment: 'center',
        margin: [0, 0, 0, 0],
        image: '<?php echo $src; ?>',
        width: 200,
        height: 150,
    });
    
    
         // Add the "Previous Balance" and "Total Bill Amount" at the bottom right
                    if (preBalance) {
                        doc.content.push({
                            margin: [0, 10, 0, 0], // Add margin for the bottom
                            table: {
                                widths: ['50%', '50%'], // Split into two columns: left for labels, right for values
                                body: [
                                     [
                                        { text: 'Previous Balance:', alignment: 'left', fontSize: 10, bold: true },
                        { text: preBalance || 0, alignment: 'right', fontSize: 10, bold: true }
                                    ],
                                    [
                                        { text: 'Total Bill Amount:', alignment: 'left', fontSize: 10, bold: true },
                        { text: totalBillAmount || totalshipment || 0, alignment: 'right', fontSize: 10, bold: true }
                                    ]
                                ]
                            },
                            layout: {
                                // Adding thicker borders to the rows
                                hLineWidth: function(i, node) { 
                                    if (i === 0 || i === node.table.body.length) {
                                        return 0; // No border for the top and bottom of the table
                                    }
                                    // Make the borders of the specific rows thicker
                                    if (i === 0 || i === 1) { // First and second row for "Previous Balance" and "Total Bill Amount"
                                        return 2; // Thicker borders for the rows with labels and values
                                    } 
                                    return 0.5; // Default border thickness
                                },
                                vLineWidth: function(i) { 
                                    return 0.5; // Vertical border thickness
                                },
                                hLineColor: function(i, node) { 
                                    // Change color for thicker borders
                                    if (i === 0 || i === 1) {
                                        return '#000000'; // Darker color for thicker borders
                                    }
                                    return '#d3d3d3'; // Lighter color for normal rows
                                },
                                vLineColor: function(i) { 
                                    return '#d3d3d3'; // Vertical border color
                                },
                                paddingLeft: function(i) { return 10; }, // Left padding
                                paddingRight: function(i) { return 10; }, // Right padding
                                paddingTop: function(i) { return 5; }, // Top padding
                                paddingBottom: function(i) { return 5; } // Bottom padding
                            }
                        });
                    }
                    
                    
    doc.footer = function (currentPage, pageCount) {
    // Only add the footer on the last page with minimal top margin
    if (currentPage === pageCount) {
        return {
             text: 'Office shop # 1: Sheikh Plaza Near Lieda Office Nazar Chowrangi Hite HuB, Balochistan.',
            alignment: 'center',
            fontSize: 10, // Adjust the font size as needed
            margin: [0, 5, 0, 5] // Minimal margin to avoid taking too much space
        };
    } else {
        // Return nothing on pages other than the last page
        return null;
    }
};
    
   
    
    
<?php } else { ?>
    // Adjust the margin of surrounding content to simulate the space where the logo would be
    doc.content[0].margin[1] += 160; // Increase the top margin by 10 units (adjustable)
<?php } ?>
                    
                    
                           
         } }
        
            
        ]
    } );
} );
</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;'
                    
//                     // Default page margins
//                     doc.pageMargins = [45, 30, 50, 90];
//                     doc.defaultStyle.fontSize = 4;
//                     doc.styles.tableHeader.fontSize = 4;
//                     doc.styles.title.fontSize = 10;
                  
//                     var table = doc.content[1].table;

//                     // Set the widths of the columns here
//                      table.widths = ['4%', '10%', '10%', '10%','10%', '10%', '10%', '12%', '10%', '14%'];
//                       //table.widths = ['auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto', 'auto'];

//                     var totalshipment = '<?php echo $alltotalship; ?>';

//                     // Assigning PHP variables to JavaScript
//                     var billNo = '<?php echo $billNo; ?>';
//                     var billDate = '<?php echo $billDate; ?>';
//                     var details = '<?php echo $details; ?>';
//                     var preBalance = '<?php echo $preBalance; ?>';
//                     var totalshipment = '<?php echo $alltotalship; ?>';
//                     var totalBillAmount = '<?php echo $totalbillAmount; ?>';

//                     // Set custom header content
//                     // var headerContent = [
//                     //     [
//                     //         { text: billNo ? 'Bill No: ' + billNo : '', alignment: 'left', fontSize: 11, italics: true, bold: true },
//                     //         { text: billDate ? '<?php echo date("j-M-Y", strtotime($billDate)); ?>' : '', alignment: 'right', italics: true, fontSize: 11, bold: true }
//                     //     ],
//                     //     [
//                     //         { text: details ? details : '', alignment: 'left', fontSize: 9, bold: true, italics: true, colSpan: 2 }
//                     //     ]
//                     // ];
                    
//                     // Set custom header content - Date ko thoda upar kiya (apni jagah par)
// var headerContent = [
//     [
//         { 
//             text: billNo ? 'Bill No: ' + billNo : '', 
//             alignment: 'left', 
//             fontSize: 11, 
//             italics: true, 
//             bold: true 
//         },
//         { 
//             text: billDate ? '<?php echo date("j-M-Y", strtotime($billDate)); ?>' : '', 
//             alignment: 'right', 
//             italics: true, 
//             fontSize: 11, 
//             bold: true,
//             margin: [0, -16, 0, 0]  // Sirf yahan margin add kiya - upar jayega
//         }
//     ],
//     [
//         { 
//             text: details ? details : '', 
//             alignment: 'left', 
//             fontSize: 9, 
//             bold: true, 
//             italics: true, 
//             colSpan: 2 
//         }
//     ]
// ];

//                     // Add the header to the PDF
//                     doc.content.unshift({
//                         margin: [0, 0, 0, 10],
//                         table: {
//                             widths: ['50%', '50%'],
//                             body: headerContent
//                         },
//                         layout: 'noBorders'
//                     });

//                     // 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';
//                         });
//                     }

//                     // Set the pageBreak property for the entire column
//                     var columnIndex = 0;
//                     table.body.forEach(function(row) {
//                         if(row[columnIndex]) {
//                             row[columnIndex].pageBreak = 'avoid';
//                         }
//                     });
                    
//                     // 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 = 5;
//                         });
//                     }
                    
//                     <?php if(!$_GET['hide_logo']){ ?> 
//                         // JAB LOGO DIKHE
//                         doc.content.splice(0, 0, {
//                             alignment: 'center',
//                             margin: [0, 0, 0, 0],
//                             image: '<?php echo $src; ?>',
//                             width: 200,
//                             height: 150,
//                         });
                        
//                         // Add space after logo
//                         doc.content.splice(1, 0, {
//                             text: ' ',
//                             margin: [0, 0, 0, 5]
//                         });
                        
//                     <?php } else { ?>
//                         // JAB LOGO HIDE HO - SPACE WAISA HI RAHEGA
//                         // Logo ki jagah blank space add karo
//                         doc.content.splice(0, 0, {
//                             text: ' ',
//                             alignment: 'center',
//                             margin: [0, 0, 0, 150]  // Logo ki height jitna margin
//                         });
                        
//                         // Uske baad wahi 5 margin ka space (jo logo ke baad tha)
//                         doc.content.splice(1, 0, {
//                             text: ' ',
//                             margin: [0, 0, 0, 5]
//                         });
                        
//                         // Top margin change mat karo - wahi 30 rakho
//                         doc.pageMargins = [45, 30, 50, 90];
                        
//                     <?php } ?>

//                     // Add the "Previous Balance" and "Total Bill Amount" at the bottom right (ALWAYS SHOW)
//                     if (preBalance) {
//                         doc.content.push({
//                             margin: [0, 10, 0, 0],
//                             table: {
//                                 widths: ['50%', '50%'],
//                                 body: [
//                                     [
//                                         { text: 'Previous Balance:', alignment: 'left', fontSize: 10, bold: true },
//                                         { text: preBalance || 0, alignment: 'right', fontSize: 10, bold: true }
//                                     ],
//                                     [
//                                         { text: 'Total Bill Amount:', alignment: 'left', fontSize: 10, bold: true },
//                                         { text: totalBillAmount || totalshipment || 0, alignment: 'right', fontSize: 10, bold: true }
//                                     ]
//                                 ]
//                             },
//                             layout: {
//                                 hLineWidth: function(i, node) { 
//                                     if (i === 0 || i === node.table.body.length) {
//                                         return 0;
//                                     }
//                                     if (i === 0 || i === 1) {
//                                         return 2;
//                                     } 
//                                     return 0.5;
//                                 },
//                                 vLineWidth: function(i) { 
//                                     return 0.5;
//                                 },
//                                 hLineColor: function(i, node) { 
//                                     if (i === 0 || i === 1) {
//                                         return '#000000';
//                                     }
//                                     return '#d3d3d3';
//                                 },
//                                 vLineColor: function(i) { 
//                                     return '#d3d3d3';
//                                 },
//                                 paddingLeft: function(i) { return 10; },
//                                 paddingRight: function(i) { return 10; },
//                                 paddingTop: function(i) { return 5; },
//                                 paddingBottom: function(i) { return 5; }
//                             }
//                         });
//                     }
                    
//                     // FOOTER ADDRESS - ALWAYS SHOWS ON LAST PAGE
//                     doc.footer = function (currentPage, pageCount) {
//                         if (currentPage === pageCount) {
//                             return {
//                                 text: 'Office: Shop # 1, Sheikh Plaza Near Lieda Office Nazar Chowrangi Hite HuB , Balochistan.',
//                                 alignment: 'center',
//                                 fontSize: 10,
//                                 margin: [0, 5, 0, 5]
//                             };
//                         } else {
//                             return null;
//                         }
//                     };
                    
//          } }
        
            
//         ]
//     } );
// } );
</script>

</div>

</body>
</html>