<?php 

include "config.php";

?>
<html>
<head>
  <title>Feenix</title>
  <!-- billslipticket.php build 2026-03-25 -->
  <style>
    th
    {
      font-size:24px;
    }
    span
    {
      font-size:20px;
    }
  </style>
</head>
<script>
  function enterkeypressed()
{
	//alert(event.keyCode);
	if(event.keyCode=='13'){ window.print(); }
}
</script>
<body onkeydown="enterkeypressed()" ontouchstart="window.print()" >
<table>
<?php 
$print = isset($_GET['print']) ? trim($_GET['print']) : '';
$debug = isset($_GET['debug']) && $_GET['debug'] == '1';
if ($print === '') {
  echo '<tr><td style="font-size:18px;"><b>Missing ticket number.</b><br>Open this page like <code>billslipticket.php?print=YOUR_TICKET_NO</code>.</td></tr>';
  echo '</table></body></html>';
  exit;
}

function h($value) {
  return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
}

function db_query($connect, $sql, $debug = false) {
  $result = mysqli_query($connect, $sql);
  if ($result === false) {
    echo '<tr><td style="font-size:18px;"><b>Database query failed.</b></td></tr>';
    if ($debug) {
      echo '<tr><td><pre style="white-space:pre-wrap;">' . h($sql) . "\n\n" . h(mysqli_error($connect)) . '</pre></td></tr>';
    }
    echo '</table></body></html>';
    exit;
  }
  return $result;
}

$printEsc = mysqli_real_escape_string($connect, $print);
$singleSql = "select * from ticket where (status is null or lower(status) <> 'deleted') and ticket = '$printEsc' limit 1";
$single = db_query($connect, $singleSql, $debug);
$singleshow = mysqli_fetch_array($single);
if (!$singleshow) {
  echo '<tr><td style="font-size:18px;"><b>Ticket not found:</b> ' . h($print) . '</td></tr>';
  echo '</table></body></html>';
  exit;
}

$customer = $singleshow['customer'];
$customerEsc = mysqli_real_escape_string($connect, $customer);
$cusSql = "select * from customerdetails where code = '$customerEsc' and (status is null or lower(status) <> 'deleted') order by id desc limit 1";
$cus = db_query($connect, $cusSql, $debug);
$cusshow = mysqli_fetch_array($cus);
if (!$cusshow) {
  $cusSql2 = "select * from customerdetails where code = '$customerEsc' order by id desc limit 1";
  $cus = db_query($connect, $cusSql2, $debug);
  $cusshow = mysqli_fetch_array($cus);
}

$product = $singleshow['product'];
$productEsc = mysqli_real_escape_string($connect, $product);
$proSql = "select * from productdetail where productcode = '$productEsc' and (status is null or lower(status) <> 'deleted') order by id desc limit 1";
$pro = db_query($connect, $proSql, $debug);
$proshow = mysqli_fetch_array($pro);
if (!$proshow) {
  $proSql2 = "select * from productdetail where productcode = '$productEsc' order by id desc limit 1";
  $pro = db_query($connect, $proSql2, $debug);
  $proshow = mysqli_fetch_array($pro);
}

$date = trim((string)$singleshow['editdate']);
if ($date === '') {
  $date = trim((string)$singleshow['date']);
}
$datefrom = '';
if ($date !== '') {
  $datefr = date_create($date);
  if ($datefr) {
    $datefrom = date_format($datefr,"d-m-Y");
  }
}
?>
 <?php 
               $proedit=db_query($connect,"select party from partydetail order by id desc limit 1", $debug);
               $proshowedi = mysqli_fetch_array($proedit);
               $party = $proshowedi ? $proshowedi['party'] : '';
               
               ?>
               <tr align="center"><th colspan="4"><?php echo h($party) ?></th></tr>
  <tr>
    <td colspan="4"><b>TO:<?php echo h($cusshow ? $cusshow['name'] : '') ?></td>
  <!-- <tr>
    <td colspan=4><b>Trip No:</b>
    <span><b><?php echo $singleshow['tripno'] ?></b></span></td>
  </tr> -->
  <tr>
    <td colspan=4><b>Ticket No:</b>
    <span><b><?php echo h($singleshow['ticket']) ?></b></span></td>
  </tr>
  <tr>
    <td colspan=4><b>vehicle No:</b>
    <span><b><?php echo h($singleshow['vehicleno']) ?></b></span></td>
  </tr>
  <tr>
    <td colspan=4><b><span><?php echo $datefrom ?></span>,</b>
    <span><b><?php echo h($singleshow['time']) ?></b></span></td>
  </tr>
  <tr>
    <td colspan=4>-------------------------------------</td>
  </tr>
  <tr>
    <td colspan=2><b>Chakka:</b>
    <span><b><?php echo h($singleshow['chakka']) ?></b></span></td><td colspan=2></td>
  </tr>
  <tr>
    <td colspan=2><b>Material:</b>
    <span><b><?php echo h($proshow ? $proshow['productname'] : '') ?></b></span></td><td colspan=2></td>
  </tr>
  <tr>
    <td colspan=2><b>Vehicle Capacity:</b>
    <span><b><?php echo h($singleshow['cft'])?></b></span></td>
  </tr>
  
   <tr>
    <td colspan=4>-------------------------------------</td>
  </tr>
  <tr>
    <td colspan=2><b>Empty Weight:</b>
    <span><b><?php echo h($singleshow['emptyweight'])?></b></span></td>
  </tr>
  <tr>
    <td colspan=4>-------------------------------------</td>
  </tr>
  <tr>
    <td colspan=4><b>Load Operator Sign</b>
   
  </tr>
  <tr>
    <td colspan=4>-------------------------------------</td>
  </tr>
  <tr>
    <td colspan=4 style="text-align:center">Thank You<td>
  </tr>
  <?php 
  ?>
</table>

</body>
</html>
