<?php
session_start();
include 'db_connection.php';
//$user_id = $_SESSION["user_id"];
$receiptno = $_GET['receiptno'];
$orderinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobile_purchase_order WHERE purchase_order_id ='$receiptno'"));

$suppler_id = $orderinfo['suppler_id'];
$totalamount = $orderinfo['total_amount'];


$sql = "SELECT * FROM purchase_order_items WHERE purchase_order_id = '$receiptno'";

$result = $conn->query($sql);

?>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Invoice</title>
    <style>
        @page {
            size: 10.4cm 10cm landscape;
            font-size: 15px;
        }

        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
        }

        .container {
            width: 88mm;
            margin: 0 auto;
            padding: 10px;
            box-sizing: border-box;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        tbody {
            td {
                font-size: 15px;
            }
        }

        tfoot {
            td {
                font-size: 15px;
            }
        }

        th,
        td {
            padding: 5px;
            text-align: left;
            font-size: 10px;

        }

        img {
            width: 80px;
            height: auto;
            margin-right: 10px;
        }

        p {
            font-size: 12px;
            margin: 0;
        }

        .center {
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="container">
        <div style="width:100%;">
            <div style="text-align: center;">
                <b>
					<p style="text-align:center;"><img src="images/poslogo.png" alt="#" width="44px"></p>
                    <p style="font-size:24px;">Payroll</p>
                    <p style="font-size:14px;">Pasyala Road,Mirigama</p>
                </b>
                <p style="margin: 0;">Tel : 071 513 2796</p>
				
            </div>
        </div>
        <hr>
        <p style="font-size:16px;">Receipt: <?php echo $receiptno; ?></p>
        <p style="font-size:16px;">Date: <?php echo $orderinfo['added_date']; ?></p>
        <p style="font-size:16px;">Suppler: <?php

                                               
                                                    $userinfo4 = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobilesuppliers WHERE spid='" . $orderinfo['suppler_id'] . "'"));
                                                    echo $userinfo4['spname'];
                                            
                                                ?>

        </p>
        <hr>
        <table>
            <thead>
                <tr>
                    <th colspan='3'><span style="text-align: center; font-size:17px;">Item Name</span></th>
                </tr>
                <tr>
                    <th><span style="text-align: center; font-size:17px;">Price</span></th>
                    <th><span style="text-align: center; font-size:17px;">Qty</span></th>
                    
                    <th colspan="2" style="text-align: center; font-size:17px;"><span style="text-align: center;">Total</span></th>
                </tr>
            </thead>
            <tbody>
                <?php
               
                if ($result->num_rows > 0) {
                    $tot = 0;
                    while ($row = $result->fetch_assoc()) {
                        $tot = $row['price']* $row['qty'];
                       $prid = $row['product_id'];
                       $prodductinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid='$prid'"));
                        echo "<tr style='font-size:16px;'><td colspan='4' style='font-size:15px;'><strong>".$prodductinfo['proname']."</strong></td></tr>";
                        echo "<tr>";
                        echo "<td style='font-size:15px;'>".$row['price']."</td>";
                        echo "<td style='font-size:15px;'>".$row['qty']."</td>";
                       
                        echo "<td colspan='2'  style='text-align: center; font-size:16px;'>".$tot."</td>";
                        echo "</tr>";
                        
                    }
                }
                
                  
                    
                ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="3">
                        <hr>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style='font-size:16px;'>Total Item:</td>
                    <td style='font-size:16px; text-align:center;'><?php echo $result->num_rows; ?></td>
                </tr>
                <tr>
                    <td colspan="3">
                        <hr>
                    </td>
                </tr>
                <tr>
                    <td colspan="1" style="font-size: 20px;"><strong>Total Amount:</strong></td>
                    <td style="font-size: 20px;"><strong>$</strong></td>
                    <td style="font-size: 20px; text-align:right;"><strong><?php echo number_format((float)$totalamount, 2); ?></strong></td>
                </tr>
                
                
            </tfoot>
        </table>

        <center>
            <br>
            <hr>
            <p>Thank You Come Again!</p>
            <p>Developed By Emarketing Solutions</p>
        </center>
    </div>
    <!-- JavaScript to trigger print functionality -->
    <script>
      window.onload = function() {
            window.onafterprint = function() {
                
               window.location.href = '/listpurchaseorder.php';
            };
            window.print();
            setTimeout(function() {
                window.onafterprint = null;
            }, 1000);
        };
    </script>
</body>

</html>

<?php
$conn->close();
?>