<?php
include('../db/cn.php');
ob_start();
// Fetch order details from the database
$orderId = isset($_GET['order_id']) ? mysqli_real_escape_string($connection, $_GET['order_id']) : null;
$orderDetails = [];
$outletName = $loginUser = $userRole = $outletAddress = $dateTime = $totalAmount = $paidBy = $customerName = $customerPhone = $discountedAmount = $discounted = $discountPer = $cashAmount = $changeAmount = $taxAmount = null;

if ($orderId) {
    $query = "SELECT * FROM log_user_sales WHERE order_id = '$orderId'";
    $result = mysqli_query($connection, $query);

    if ($result && mysqli_num_rows($result) > 0) {
        $orderDetails = mysqli_fetch_all($result, MYSQLI_ASSOC);
        $order = $orderDetails[0];
        $outletName = $order['outlet_name'];
        $loginUser = $order['login_user'];
        $userRole = $order['user_role'];
        $outletAddress = $order['outlet_address'];
        $dateTime = $order['date_time'];
        $totalAmount = $order['net_amount'];
        $paidBy = $order['paid_by'];
        $customerName = $order['customer_name'];
        $customerPhone = $order['customer_phone'];
        $discountedAmount = $order['discount_amount'];
        $discounted = $order['discounted'];
        $date = $order['date_time'];
        $discountPer = $order['discount_per'];
        $cashAmount = $order['cash_amount'];
        $changeAmount = $order['change_amount'];
        $taxAmount = $order['tax_amount'];
    }
}

$content = ob_get_clean(); // Capture HTML content

// Function to send data to the printer
function sendToPrinter($result) {
    $printerName = "EPSON_TM_T88V_Receipt"; // Change to your printer's name
    $handle = @fopen("USB", "w"); // LPT1 is commonly used for parallel port printers, change if necessary
    if (!$handle) {
        echo "Error opening printer on Windows. Check the printer port and connection.";
        return;
    }
    fwrite($handle, $result);
    fclose($handle);
    echo "";
}

// Call the print function
sendToPrinter($content);
?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Kashmir Bazar Receipt</title>
  <style>
    body {
      font-family: monospace;
      max-width: 380px;
      margin: auto;
  
      padding: 15px;
    }
    h2, h3, p {
      text-align: center;
      margin: 4px 0;
    }
    table {
      width: 100%;
      border-collapse: collapse;
      margin-top: 10px;
    }
    th, td {
      border-bottom: 1px dotted #000;
      padding: 3px;
      font-size: 13px;
    }
    .right {
      text-align: right;
    }
    .barcode {
      text-align: center;
      margin: 20px 0 10px;
    }
    .footer {
      text-align: center;
      font-size: 12px;
    }
     @media print {
         .no-print {
                display: none;
            }
     }
     
  </style>
</head>
<body>
  <h2>RECHNUNG-Receipt</h2>
  <h3>KASHMIR BAZAR</h3>
  <p>Hohenheimer Str. 72, 70184 Stuttgart St.</p>
  <p>ID: <?php echo htmlspecialchars($orderId); ?></p>
  <p><?php echo $date; ?></p>

  <table>
    <thead>
      <tr><td class="text-left " style="font-weight:700;">Product</td><th class="right"></th><th class="right"></th><th class="right">Euro</th><th class="right">Mwst%</th></tr>
    </thead>
    <tbody>
     <?php foreach ($orderDetails as $order): ?>
      <tr><td><?php echo htmlspecialchars($order['product_name']); ?></td>  <td class="right"><?php echo htmlspecialchars($order['product_quantity'].' x'); ?></td><td class="right"><?php echo htmlspecialchars($order['product_price']); ?></td><td class="right"><?php echo htmlspecialchars($order['product_price'] * $order['product_quantity']); ?></td><td class="right"><?php echo htmlspecialchars($order['wh_percentage']); ?></td></tr>
     <?php endforeach; ?>
    </tbody>
  </table>
<?php 
$rp = mysqli_query($connection,"select * from log_user_sales where order_id = '$orderId'");
$totalE = 0;
while($r = mysqli_fetch_array($rp)){
    $totalE += $r["net_amount"];
}
?>
  <table>
    <tr><td><b>Gesamte betrag:</b></td><td class="right"><?php  echo $totalE ;?> €</td></tr>
    <tr><td><b>Total EUR:</b></td><td class="right"><?php  echo $totalE ;?> €</td></tr>
  </table>

  <!--<table>-->
  <!--  <thead>-->
  <!--    <tr><th>BEZAHLT VON</th><th>BAR</th><th>BETRAG</th><th>BAR RETOUR</th></tr>-->
  <!--  </thead>-->
  <!--  <tbody>-->
  <!--    <tr><td>Bar gegeben</td><td>0.00 €</td><td>46.21 €</td><td>0.00 €</td></tr>-->
  <!--  </tbody>-->
  <!--</table>-->

<?php 
$s7 = mysqli_query($connection, "SELECT * FROM log_user_sales WHERE order_id = '$orderId' AND wh_percentage = '7'");
$total7 = 0;
while ($r7 = mysqli_fetch_array($s7)) {
    $total7 += $r7["net_amount"];
}
$tax7 = $total7 * 0.07;
$net7 = $total7 - $tax7;

$s19 = mysqli_query($connection, "SELECT * FROM log_user_sales WHERE order_id = '$orderId' AND wh_percentage = '19'");
$total19 = 0;
while ($r19 = mysqli_fetch_array($s19)) {
    $total19 += $r19["net_amount"];
}
$tax19 = $total19 * 0.19;
$net19 = $total19 - $tax19;
?>

<table>
  <tr>
    <td>7%</td>
    <td class="right"><?php echo number_format($net7, 2) ?> €</td>
    <td class="right"><?php echo number_format($tax7, 2) ?> €</td>
    <td class="right"><?php echo number_format($total7, 2) ?> €</td>
  </tr>
  <tr>
    <td>19%</td>
    <td class="right"><?php echo number_format($net19, 2) ?> €</td>
    <td class="right"><?php echo number_format($tax19, 2) ?> €</td>
    <td class="right"><?php echo number_format($total19, 2) ?> €</td>
  </tr>
</table>


  <div class="footer">
    <p>Mail: Info@kashmirbasar.online</p>
    <p>Tel: (+49 711 69908947)</p>
    <p>Website: <a href="https://www.kashmirbasar.online">https://www.kashmirbasar.online</a></p>
    <p>Business Timing: Mon-Sat 10 AM - 8 PM</p>
  </div>

  <div class="barcode">
  <img src="https://barcode.tec-it.com/barcode.ashx?data=<?php echo urlencode($orderId); ?>&code=Code128&translate-esc=false" alt="barcode">
  <p><?php echo htmlspecialchars($orderId); ?></p>
</div>


  <h3>Thanks for your visit!</h3>
  
<center>  
    <div class="text-center no-print">
        <button class="no-print" onclick="window.print()">Print</button>
      <a href="mainpos.php"> <button type="button" name="exit" class="no-print">Exit</button></a>
    </div>
</center>    
</body>
</html>
