Kandy road, Pasyala.<?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'];
$customer = $orderinfo['customer'];
$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 {
            width: 80px;
            height: auto;
            margin-right: 10px;
        }

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

        .center {
            text-align: center;
        }
		thead {
  border-top: 1px solid black;   
  border-bottom: 1px solid black; 
}
		th,td {
			padding-top:10px;
			padding-bottom:10px;
		}
		tfoot{
			border-top: 1px solid #ccc;
		}
    </style>
</head>

<body>
    <div class="container">
        <div style="width:49%; float:left;margin-top:15px;margin-bottom:25px;">
            <div style="text-align: left; display:flex;">
				<div>
				<p style="font-size:24px;"><img style="padding: 25px 10px;background: #000;margin-right: 20px;" src="images/settings/999882.png" alt="#" width="100px"></p></div>
				<div>
                <p style="font-size:20px; margin-top:7px;">Eat More Resturant   </p>
                <p style="font-size:14px; margin-top:5px;">143/C,Kandewaththa Road,Minuwangoda,Galle</p>
                <p style="margin: 0; margin-top:5px;">Tel : 077 324 1469</p>
				</div>
            </div>
        </div>
		<div style="width:49%;float:right; text-align:right; margin-top:15px;margin-bottom:25px;"> 
			<p style="font-size:16px;margin-top:5px;">Order No</p>
			 <p style="font-size:16px;margin-top:5px;"><?php echo $orid; ?></p>
			<p style="font-size:16px;margin-top:5px;">Date</p>
			<p style="font-size:16px;margin-top:5px;"><?php echo $create_at; ?></p>
		</div>
        
       <div style="width:100%;clear:both; border-top:1px solid #ccc;padding: 15px 0px;">
		   
		<p style="margin-bottom:8px;">TO</p>
        <p style="font-size:16px;font-weight:600;"><?php

                                                if ($orderinfo['customer'] == "0") {
                                                    echo "Walk-in Customer";
                                                } else {

                                                    $userinfo4 = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobilecustomers WHERE coid='" . $orderinfo['customer'] . "'"));
                                                    echo $userinfo4['cname'];
                                                }
                                                ?>

        </p>
		</div>
       
        <table>
            <thead>
               
                <tr>
					<th><span style="text-align: center; font-size:15px;">DESCRIPTION</span></th>
                    <th><span style="text-align: center; font-size:15px;">RATE</span></th>
                    <th><span style="text-align: center; font-size:15px;">QTY</span></th>
                    <th colspan="2" style="text-align: right; font-size:15px;"><span style="text-align: center;">AMOUNT</span></th>
                </tr>
            </thead>
            <tbody>
                <?php
                // $total_amount = 0;
                // $dis=0;
                $total_amount = 0;
                $discounted_amount = 0;
                $rowCount = 0;
				$total_discount_amount = 0;
                $dtotl = 0;
                $taxvaltot = 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 = $row["pro_price"];
                        $pro_qty = $row["pro_qty"];
                        $pro_discount = $row["pro_discount"];
                        $pamount = 0;

                        if ($row['discount_type'] == 1) {

                            $pdis =  ($pro_price / 100 * $pro_discount) * $pro_qty;

                            $product_price = ($pro_price * $pro_qty) - $pdis;
                            $distot +=  $pdis;
                            $pamount = $pro_price * $pro_qty;
                        } else {

                            if($product["sellprice"]==$pro_price){

                                $product_price = ($pro_price * $pro_qty) - ($pro_discount * $pro_qty);
                                $distot += $pro_discount * $pro_qty;
                                $pamount = $pro_price * $pro_qty;
                            }else{
                                if($row['proid']==148){
                                    $product_price = ($pro_price * $pro_qty) - ($pro_discount * $pro_qty);
                                    $distot += $pro_discount * $pro_qty;
                                    $pamount = $pro_price * $pro_qty;
                                }else{
                                    $product_price = $pro_price - $pro_discount;
                                    $distot += $pro_discount;
                                    $pamount = $pro_price * $pro_qty;
                                }
                                
                            }
                        }

                        if ($row['tax_type'] == 1) {
                            $taxval1 = ($pamount/100) * $row['tax'];
                            $taxval1 = $taxval1 * $pro_qty;
                        }else{
                            $taxval1 = $row['tax'] * $pro_qty;
                        }
                        $taxx = 0;
                        if($row['tax_type'] == 1){
                            $taxx = $row['tax']."%";
                           
                        }else{
                            $taxx = $row['tax'];
                        }
						
                        $taxvaltot = $taxvaltot + $taxval1;

                        $product_price =  $product_price + $taxval1;

                        $dtotl = $dtotl + $distot;
                        $total_amount += $product_price;
                        $pro_price = number_format((float)$row["pro_price"], 2);
                        $product_price = number_format((float)$product_price, 2);
                        $pamount = number_format((float)$pamount, 2);
                        
                      
                        echo "<tr>";
						echo "<td style='font-size:15px;'>$proname</td>";
                        echo "<td style='font-size:15px;'>$pro_price</td>";
                        echo "<td style='font-size:15px;'>$pro_qty</td>";
                       
                       
                        echo "<td colspan='2'  style='text-align: right; font-size:16px;'>$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;
                }

                $bill_amount = $total_amount - $discounted_amount;
                $balance_amount = $paid_amount - $bill_amount;
                $discount = ($discounted_amount * 100) / $total_amount;
                $t =  $total_amount;

                $dis2 = 0;
                if($orderinfo['discount_type']==0){ 

                     $dis2 =  $orderinfo['all_discount'];
                 }else{
                     $disval = $orderinfo['all_discount'];
                     
                     $dis2 =  ($t/100)*$disval;
 
                 }
                 $taxx = 0;
                 if($orderinfo['tax_type']==0){
                    $taxx =  $orderinfo['tax'];

                    }else{

                    $taxx = ($t /100) * $orderinfo['tax'];

                    }

                    $tot4 =  $t - $dis2;

                  $taxx = $taxx + $taxvaltot;
                  
                    
                ?>
            </tbody>
            <tfoot>
                
                <tr>
                    <td colspan="3" style="font-size: 18px;"></td>
                    <td style="font-size: 18px;border-bottom: 1px solid #ccc;"><strong>Total</strong></td>
                    <td style="font-size: 18px; text-align:right;border-bottom: 1px solid #ccc;"><strong>LKR Rs <?php echo number_format((float)$t, 2); ?></strong></td>
                </tr>
               
            </tfoot>
        </table>
        <p style="font-size:12px;text-align:center"><img src="images/warranty.png" alt="#" width="25px"></p>
		<center>
        <br>
        <hr>
        <p>Thank You Come Again!</p>
        <p>Developed By Seopro Developer</p>
        </center>
    </div>
    <!-- JavaScript to trigger print functionality -->
    <script>
       window.onload = function() {
            window.onafterprint = function() {
                
               window.location.href = '/pos-panel.php';
            };
            window.print();
            setTimeout(function() {
                window.onafterprint = null;
            }, 1000);
        };
    </script>
</body>

</html>

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