<?php
session_start();
include 'db_connection.php';
//$user_id = $_SESSION["user_id"];
$receiptno = $_GET['receiptno'];
$orderinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileorders WHERE order_id='$receiptno'"));
$userid = $orderinfo['user_id'];
$pay_option = $orderinfo['pay_option'];
$userinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE user_id='$userid'"));
$sql = "SELECT * FROM mobileorders WHERE order_id = '$receiptno'";

$result = $conn->query($sql);
$discount = 0;
$dis_type = 0;
$paid_amount = 0;
if ($result->num_rows > 0) {
    
	while ($row = $result->fetch_assoc()) {
		$paid_amount = $row["paid_amount"];
		$orid = $row["orid"];
		$create_at = $row["create_at"];
		$pay_option = $row["pay_option"];
		$amount = $row["amount"];
		$discount = $row["discount"];
        $dis_type = $row["dis_type"];
	}
}

$sql = "SELECT * FROM mobiletemporarycart WHERE orderid = '$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: 210mm;
        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 {
       
        height: auto;
        margin-right: 10px;
    }

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

    .center {
        text-align: center;
    }
    .bordertable  th, .bordertable td{
        border: 1px solid black;
        border-collapse: collapse;
    }
    .headerbg{
       position: relative;
    }
    .headerbg::before{
        background-image: url("images/headerbgg.png");
        width: 100%;
        height: 200px;
        position: absolute;
        top:0;
        left: 0;
        opacity: 0.2;
    content: "";
    background-size: cover;
    background-position: center;
       
    }
    </style>
</head>

<body>
    <div class="container">
        <div style="width:100%;">
            <div style="text-align: center;" class="headerbg">
                <img src="images/logo123.png" width="100%">
                <b>
                    
                    <p style="font-size:14px;">Pasyala Road,Mirigama.</p>
                    <p style="font-size:14px; margin: 0; margin-top:8px; margin-bottom:8px;">Tel : 071 513 2796(Whatsapp)<span style="margin-right: 20px;"></span>Email : seopro60@gmail.com
</p>
                </b>
               <!-- <div class="display:flex;padding-left:14%;padding-right:14%;">
                    <img src="images/bill1.jpg" style="width: 16%; height:60px;">
                    <img src="images/bill2.jpg" style="width: 16%; height:60px;">
                    <img src="images/bill3.jpg" style="width: 16%; height:60px;">
                    <img src="images/bill4.jpg" style="width: 16%; height:60px;">
                </div>-->
            </div>
        </div>
        <hr>
        <div style="width: 100%;">
        <div style="width: 100%;">
            <p style="font-size:16px; text-align: center; font-weight:600; margin-bottom:20px;">Invoive</p>
        </div>
        
            <div style="float:left; width:48%; margin-bottom:10px;">
                <table style="width: 100%;" class="bordertable">
                <tbody>
                    
                <tr><td style="width: 50%;"><p>Compnay Name  </p></td><td></td></tr>
                <tr><td style="width: 50%;"><p>Address  </p></td><td></td></tr>
                <tr><td style="width: 50%;"><p>Telephone No</td><td></td></tr>
                    
                </tbody>
                </table>
            </div>
            <div style="float:right; width:48%;">
                <table style="width: 100%;" class="bordertable">
                <tbody>
                <tr><td style="width: 50%;"><p>Invoice No </p></td><td><p><?php echo $orid; ?></p></td></tr>
                <tr><td style="width: 50%;"><p>Invoice Date </p></td><td><p><?php echo $create_at; ?></p></td></tr>
                <tr><td style="width: 50%;"><p>Received Date </p></td><td><p><?php echo $create_at; ?></p></td></tr>
                </tbody>
                </table>
            </div>
        </div>
        <div style="clear:both;">
        <hr>
        <table class="bordertable">
            <thead>
                <tr><th><span style="text-align: center; font-size:16px;">Item Name</span></th>
                
                    
                    <th><span style="text-align: center; font-size:16px;">Price</span></th>
                    <th><span style="text-align: center; font-size:16px;">Quantity</span></th>
                    <th><span style="text-align: center; font-size:16px;">Dis</span></th>
                    <th colspan="2" style="text-align: center; font-size:16px;"><span style="text-align: center;">Total</span></th>
                </tr>
            </thead>
            <tbody>
                <?php
				// $total_amount = 0;
                // $dis=0;
                $total_amount = 0;
$discounted_amount = 0;
$rowCount = 0;
				if ($result->num_rows > 0) {
                    $distot = 0;
					while ($row = $result->fetch_assoc()) {
						$rowCount = $rowCount + 1;
						$sql_products = "SELECT * FROM mobileproducts WHERE prid = " . $row['proid'];
						$result_products = $conn->query($sql_products);
						if ($result_products->num_rows > 0) {
							$product = $result_products->fetch_assoc();
                            if($row['pro_name']==null){
                                $proname = $product["proname"];
                            }else{
                                $proname = $row['pro_name'];
                            }
							
						}
                        if($row['discount_type']==1){
                            $dismark = "%";
                           
                        }else{
                            $dismark = "";
                           
                        }
						// $pro_price = number_format((float)$row["pro_price"], 2);
						// $pro_qty = $row["pro_qty"];
						// $pro_discount=number_format((float)$row["pro_discount"], 2);
						// $product_price = number_format((float)$pro_price * $pro_qty, 2);
						// $total_amount += $product_price;
                        //$pro_price = number_format((float)$row["pro_price"], 2);
                        $pro_price = $row["pro_price"];
                        $pro_qty = $row["pro_qty"];
                        $pro_discount = $row["pro_discount"];
                       
                        if($row['discount_type']==1){
                         //  $pdis =  ($pro_price * $pro_qty)/100*$pro_discount;

                           $pdis =  ($pro_price/100*$pro_discount)*$pro_qty;
                           // $product_price = number_format((float)($pro_price * $pro_qty)-$pdis, 2);
                           $product_price = ($pro_price * $pro_qty)-$pdis;
                            $distot +=  $pdis;
                        }else{
                            
                           // $product_price = number_format((float)($pro_price * $pro_qty)-$pro_discount,2);
                           
                            $product_price = ($pro_price * $pro_qty)-($pro_discount*$pro_qty);
                            $distot += $pro_discount* $pro_qty;
                            
                        }
                       
                        $total_amount += $product_price;
                        $pro_price = number_format((float)$row["pro_price"], 2);
                        $product_price = number_format((float)$product_price, 2);
                        echo "<tr><td>$proname</td>";
					
						echo "<td>$pro_price</td>";
						echo "<td>$pro_qty</td>";
						echo "<td>".$pro_discount.$dismark."</td>";
						echo "<td colspan='2'  style='text-align: right;'>$product_price</td>";
						echo "</tr>";
                        $total_discount_amount += ($pro_discount*$pro_qty);


                        

                       
                        
                        
					}
				}
               $discounted_amount = $total_discount_amount;
               
              //  if($dis_type==1){
              //      $discounted_amount = ($paid_amount/100)*$discount;
                //}else{
               //     $discounted_amount = $discount;
               // }
            if($dis_type==1){
                $discounted_amount = ($paid_amount/100)*$discount;
            }else{
                $discounted_amount = $discount;
                
            }  
              
               
                $bill_amount = $total_amount - $discounted_amount;
                $balance_amount = $paid_amount - $bill_amount;
                $discount = ($discounted_amount*100)/$total_amount;
               $t =  $total_amount+$discounted_amount;
				?>
            </tbody>
            <tfoot>
                
                <tr>
                    <td colspan="4">Total Item:</td>
                    <td style='text-align: right;'><?php echo $rowCount; ?></td>
                </tr>
               
                <tr>
                    <td colspan="3" style="font-size: 19px;">Total Amount:</td>
                    <td style="font-size: 19px;">Rs</td>
                    <td style="font-size: 19px; text-align: right;"><?php echo number_format((float)$t, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="3">Discount:</td>
                    <td></td>
                    <td style='text-align: right;'><?php echo number_format((float)$discount, 2); ?>%</td>
                </tr>
                <tr>
                    <td colspan="3">Discounted Amount:</td>
                    <td>Rs</td>
                    <td style='text-align: right;'><?php echo number_format((float)$distot, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="3">Bill Amount:</td>
                    <td>Rs</td>
                    <td style='text-align: right;'><?php echo number_format((float)$bill_amount, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="3">Paid Amount:</td>
                    <td>Rs</td>
                    <td style='text-align: right;'><?php echo number_format((float)$paid_amount, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="3">Balance Amount:</td>
                    <td>Rs</td>
                    <td style='text-align: right;'><?php echo number_format((float)$balance_amount, 2); ?></td>
                </tr>
            </tfoot>
        </table>
        <br>
        <br>
            <hr>
        <div style="display: flex; justify-content: space-between;margin-top:30px;">
            <div style="text-align: center;">
                <p>.............................................</p>
                <p>Prepared By</p>
            </div>
            <div style="text-align: center;">
                <p>.............................................</p>
                <p>Received By</p>
            </div>
            <div style="text-align: center;">
                <p>.............................................</p>
                <p>Date</p>
            </div>
        </div>
        <center>
            <br>
            <hr>
            <p style="margin-top:7px;">Payments to be made in favour of Pragash Trading Lanka</p>
            <p style="margin-top:7px;">Thank You & Come Again!</p>
            <p style="margin-top:7px;">Developed By Seopro Designer</p>
        </center>
    </div>
    <!-- JavaScript to trigger print functionality -->
    <script>
    window.onload = function() {
        window.print();
    };
    </script>
</body>

</html>

<?php
$conn->close();
?>
<script>
function redirectToPosPanel() {
    window.location.href = "/pos-panel.php";
}

window.onafterprint = redirectToPosPanel;
window.onload = function() {
  window.print();
    setTimeout(redirectToPosPanel, 2000);
};
</script>