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

if (!$_SESSION['email']) {
    header('location:login.php');
}
?>
<?php
include '../includes/config.php';


if (isset($_GET['invoice_no']) && !empty($_GET['invoice_no'])) {
    $invoice_number = $_GET['invoice_no'];

    $invoice_number = intval($invoice_number);

    $stmt_customer = $conn->prepare("SELECT contacts.name, defective_sale.date_sale, defective_sale.sales_man, defective_sale.date_sale FROM contacts
    INNER JOIN defective_sale ON contacts.id = defective_sale.customer_id
    WHERE defective_sale.invoice = :invoice_number;");
    $stmt_customer->bindParam(':invoice_number', $invoice_number, PDO::PARAM_INT);
    $stmt_customer->execute();
    $row_customer = $stmt_customer->fetch(PDO::FETCH_ASSOC);

    if ($row_customer) {
        $stmt_products = $conn->prepare("SELECT 
        ss.*, 
        b.name AS brand_name
    FROM defective_sale ss
    JOIN products p ON ss.product_id = p.id
    JOIN brands b ON p.brand_id = b.id
    WHERE ss.invoice = :invoice_number;");
        $stmt_products->bindParam(':invoice_number', $invoice_number, PDO::PARAM_INT);
        $stmt_products->execute();
    } else {
        echo "No data found for the provided invoice number.";
        exit;
    }
} else {
    echo "No invoice number provided.";
    exit;
}

?>

<!DOCTYPE html>
<html>

<head>
    <title>Sales Receipt</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Crimson+Text&display=swap" rel="stylesheet">
<style>
@page {
    size: A4 portrait;
    margin: 0;
}

body {
    font-family: Consolas, Arial, sans-serif;
    background-color: #f5f5f5;
    padding: 0;
    margin: 0;
}

.container {
    width: 210mm;
    height: 297mm;
    margin: 0 auto;
    padding: 20mm;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
    border: 1px solid #000;
    border-radius: 5px;
    overflow: hidden;
    box-sizing: border-box;
}

h1, h2, h3 {
    text-align: center;
    color: #333;
    background-color: #f2f2f2;
    padding: 10px;
    margin: 0;
    border-bottom: 2px solid #000;
    font-weight: bold;
}

p {
    text-align: left;
    color: #333;
    background-color: #f2f2f2;
    padding: 5px;
    margin: 0;
    border-bottom: 1px solid #000;
     font-weight: bold;
       font-size: 14px;
}

.table-container {
    margin-top: 20px;
}

table {
    width: 100%;
    border-collapse: collapse;
    font-size: 14px;
}

table td, table th {
    padding: 10px;
    border: 1px solid #000;
    text-align: left;
     font-weight: bold;
       font-size: 14px;
}

table th {
    background-color: #f2f2f2;
    font-weight: bold;
      font-size: 14px;
}

.total {
    text-align: right;
    font-weight: bold;
    font-size: 16px;
    margin-top: 10px;
}

.logo {
    width: 100px;
    margin: 10px auto;
    display: block;
}

/* Print Styles */
@media print {
    @page {
        size: A4 portrait;
        margin: 0;
    }

    body, html {
        background: #fff;
        margin: 0;
        padding: 0;
    }

    .container {
        width: 100%;
        height: auto;
        padding: 10mm;
        box-shadow: none;
        border: none;
    }

    h1, h2, h3 {
        padding: 5px;
        border-bottom: 2px solid #000;
    }

    table td, table th {
        padding: 8px;
        font-size: 14px;
    }

    .total {
        font-size: 14px;
    }

    .logo {
        width: 80px;
    }

    #back-link, .no-print {
        display: none;
    }
}
</style>
</head>
<a href="../list_damage.php" id="back-link">Back List POS</a>
<body>
    <div class="container" id="data">
        <!-- <div class="logo">
        <img src="invoice_logo.jpg">
        </div> -->
        <h3 style="
        font-family: Consolas, Arial, sans-serif; 
        font-size: 28px; 
        font-weight:bold;
        display: flex; 
        align-items: center; 
        justify-content: center; 
        text-align: center; 
        margin: 0; 
        position: relative; 
        padding-left: 20px; /* Space for the logo */
    ">
        <!--    <img class="image_logo" src="logo.png" style="-->
        <!--    position: absolute; -->
        <!--    left: 0; -->
        <!--    width: 60px; -->
        <!--    top: 50%; -->
        <!--    padding-left: 5px;-->
        <!--    transform: translateY(-50%);-->
        <!--" alt="Logo">-->
            <div style="
            display: flex; 
            flex-direction: column; 
            align-items: center; 
            width: 100%;
        ">
                <span>Pen Grid</span>
            </div>
        </h3>

        <div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 10px;">
    
    <!-- Left Column: Customer Details -->
    <div style="flex: 1;">
<h1 style="font-size: 13px; margin: 0; display: flex; justify-content: space-between; align-items: center;">
    <span><?php echo date("H:i:s");  ?></span>
    <span><?php echo date("l d/m/Y", strtotime($row_customer['date_sale'])); ?></span>
</h1>       <p style="font-size: 16px; font-weight: bold; margin: 5px 0; color: red;">
    🔁 Replace Invoice #: <?php echo htmlspecialchars($invoice_number); ?>
</p>

               <!--<p style="font-size: 14px;"><b>SaleMan: <?php echo htmlspecialchars($row_customer['sales_man']); ?></b></p>-->

        <p style="font-size: 14px; margin: 2px 0;"><b>Customer Name: <?php echo htmlspecialchars($row_customer['name']); ?> </b></p>
        <!-- <p style="font-size: 14px;"><b>Customer Phone: <?php echo $row_customer['customer_phone']; ?></b></p> -->
    </div>

    <!-- Right Column: Company Details -->
    <div style="flex: 1; text-align: right;">
        <h4 style="font-size: 13px; font-weight: bold; margin: 0;">
            <!--Address: 419/420 Pia Main Boulevard--><br>
          Phone: 0324-5246625<br>
            
        </h4>
    </div>
</div>

     <table>
    <thead>
        <tr>
            <th>SR#</th> 
            <th>Item</th>
            <th>Brand Name</th>
            <th>Replace QTY</th>
            <th>Price</th>
            <!--<th>Ex.Value</th> -->
            <!--<th>S.Tax (%)</th> -->
            <!--<th>S.Tax Value</th> -->
            <th>Total</th> 
        </tr>
    </thead>

    <tbody>
        <?php
        $sr_number = 1;
        $total_qty = 0;
        $total_order_tax = 0;
        $total_amount_with_tax = 0;

        while ($row_product = $stmt_products->fetch(PDO::FETCH_ASSOC)) {
            $exclusive_value = floatval($row_product['total_amount']);
            $sales_tax_value = ($exclusive_value * $order_tax_percentage) / 100;
            $total_with_tax = $exclusive_value + $sales_tax_value;

            $total_order_tax += $sales_tax_value;
            $total_amount_with_tax += $total_with_tax;

            // Calculate total quantity
            $qty = floatval($row_product['sale_qty']);
            $total_qty += $qty;
        ?>
            <tr>
                <td><?php echo $sr_number++; ?></td>
                <td><?php echo $row_product['product_name']; ?></td>
                <td><?php echo $row_product['brand_name']; ?></td>
                <td><?php echo $row_product['sale_qty'] . " " . $row_product['unit']; ?></td>
                <td><?php echo number_format($row_product['price'], 2); ?></td>
                <td><?php echo number_format($total_with_tax, 2); ?></td>
            </tr>
        <?php } ?>
        <!-- Display total quantity row -->
       
       
    </tbody>
</table>


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

// Check if the invoice number is provided in the URL
if (isset($_GET['invoice_no']) && !empty($_GET['invoice_no'])) {
    $invoice_number = $_GET['invoice_no'];

    // Sanitize the input to prevent SQL injection
    $invoice_number = intval($invoice_number);

    // Get the customer details
    $stmt_customer = $conn->prepare("SELECT contacts.name FROM contacts
    INNER JOIN defective_sale ON contacts.id = defective_sale.customer_id
    WHERE defective_sale.invoice = :invoice_number;");
    $stmt_customer->bindParam(':invoice_number', $invoice_number, PDO::PARAM_INT);
    $stmt_customer->execute();
    $row_customer = $stmt_customer->fetch(PDO::FETCH_ASSOC);

    // Check if customer details are found for the given invoice
    if ($row_customer) {
        // Get the products in the bill
        $stmt_products = $conn->prepare("SELECT SUM(total_amount) as total_amount_sum, 
                                                SUM(net_cash) as net_cash_sum, 
                                                (discount) AS discount_sum, 
                                                SUM(order_tax) AS order_tax_sum, 
                                                SUM(sale_qty) AS Total_sale_qty
                                         FROM defective_sale
                                         WHERE defective_sale.invoice = :invoice_number");
        $stmt_products->bindParam(':invoice_number', $invoice_number, PDO::PARAM_INT);
        $stmt_products->execute();
        $row_products = $stmt_products->fetch(PDO::FETCH_ASSOC);
    } else {
        echo "No data found for the provided invoice number.";
        exit;
    }
} else {
    echo "No invoice number provided.";
    exit;
}
?>


<div class="total">
    Total Replace Qty: <?php echo htmlspecialchars($row_products['Total_sale_qty']); ?>
</div>
<!--<div class="total">
    Total Replace Amount: <?php echo htmlspecialchars($row_products['total_amount_sum']); ?>
</div>-->
<!--<div class="total">-->
<!--    Discount: <?php echo htmlspecialchars($row_products['discount_sum']); ?>-->
<!--</div>-->
<!-- <div class="total">
    Previous Balance: 0
</div> -->
<!-- <div class="total">
    Sale Tax : <?php echo htmlspecialchars($row_products['order_tax_sum']); ?>
</div> -->
<!--<div class="total">-->
<!--    Cash: <?php echo htmlspecialchars($row_products['net_cash_sum']); ?>-->
<!--</div>-->
<!--<div class="total">-->
<!--    Due Balance: -->
<!--        <?php echo htmlspecialchars(number_format( ($row_products['total_amount_sum'] + $row_products['order_tax_sum']) - ($row_products['net_cash_sum'] + $row_products['discount_sum']),2)); ?>-->
<!--</div>-->

        <!--<p style="align-items: center; text-align: center;">Goods Once Sold are not returnable-->
        <!--</p>-->
        <h2>Thank You</h2>
<!--        <p style="background-color: #fdfdfd; -->
<!--          border: 1px solid #ccc; -->
<!--          padding: 12px 18px; -->
<!--          border-radius: 6px; -->
<!--          font-weight: bold; -->
<!--          text-align: center; -->
<!--          color: #333; -->
<!--          font-family: Arial, sans-serif; -->
<!--          font-size: 15px;-->
<!--          margin: 20px 0;">-->
<!--  <strong style="color: #000;">Warranty:</strong><br>-->
<!--  <span style="display: inline-block; margin-top: 6px;">-->
<!--    <strong>Motor:</strong> <span style="color: #007700;">3 Years</span> &nbsp; | &nbsp; -->
<!--    <strong>Kitt:</strong> <span style="color: #990000;">1 Year</span>-->
<!--  </span>-->
<!--</p>-->
        <!-- <p style="text-align:center; font-size:14px; font-weight:bold;">(Developed by IT Genesis)<br>webisite:itgenesis.net & ph: +92 308 4926007</p> -->
        <button onclick="printContent()">Print</button>
    </div>
</body>
<script>
    function printContent() {
        var container = document.querySelector(".container");
        var printButton = document.querySelector("button");
        printButton.style.display = "none"; // Hide the button

        window.print(); 

        setTimeout(function() {
            window.location.href = '../pos.php';
        }, 500); 
    }
</script>

</html>