<?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'];

$userinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE user_id='$userid'"));
$sql = "SELECT * FROM mobileorders WHERE order_id = '$receiptno'";
$s = 0;
$result = $conn->query($sql);
$discount = 0;
$dis_type = 0;
$paid_amount = 0;
$pay_option = "cash";
$markettotal = 0;
$marketprice = 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"];
        $pay_option = $row["pay_option"];
	}
}

$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: 58mm;
        margin: 0 auto;
        padding: 0px;
        box-sizing: border-box;
        border: 2px solid #000;
    }

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

    tbody{
        td{
            font-size: 11px;
            font-weight: 700;
        }
    }
    tbody{
        tr.itemsep{
           
        }
    }
    tfoot{
        td{
            font-size: 12px;
            
        }
    }
    th,
    td {
        padding: 2px;
        text-align: left;
        font-size: 12px;

    }

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

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

    .center {
        text-align: center;
    }
    .text1{
        position: relative;
        float: left;
    }
    .text1::after{
        position: absolute;
        left: 0;
        top: 14px;
        content:"";
        width: 102px;
        height: 5px;
        border-bottom: 2px solid;
    }
    .text1::before{
        position: absolute;
        left: 0;
        top: 18px;
        content:"";
        width: 115px;
        height: 5px;
        border-bottom: 2px solid;
    }
    @font-face {
    font-family: 'FMDerana';
    src: url('FMDeranax.woff2') format('woff2'),
        url('FMDeranax.woff') format('woff');
    font-weight: normal;
    font-style: normal;
    font-display: swap;
}
.sfont{
    font-family: 'FMDerana', Arial, sans-serif !important;
    font-size: 20px;
}
th{
    border-bottom: 1px solid #000 !important; 
    border-top: 1px solid #000  !important;
}
    </style>
</head>

<body>
    <div class="container">
        <div style="width:100%;">
            <div style="text-align: center; margin-bottom:40px;">
                 <b>
                    <p style="font-size:20px;">Eat More Resturant   </p>			
                    <p style="font-size:16px;">143/C,Kandewaththa Road,Minuwangoda,Galle</p>
                </b>
                <p style="margin: 0;">Tel : 077 324 1469</p>
                <b><p class="sfont" style="text-align: left; font-size:31px; margin-top:12px; margin-left:20px;">fydou foa blaukska</p></b>
                <b><p style="text-align: left; font-size:18px; margin-top: -8px; margin-left:20px; margin-botton:20px;"><span class="text1"></span><span class="sfont" style="font-size:31px; text-align: right; float:right;margin-right:20px;">wvqu  ñ,g</span></p></b>
            </div>
        </div>
        <hr>
        <b><p><span style="font-size: 20px;" class="sfont">ì,a wxlh</span><span style="margin-left: 30px;">: <?php echo $orid; ?></span></p></b>
        <p><b><span class="sfont" style="font-size: 20px;">Èkh</span></b> <span style="margin-left: 63px;"> : <?php 
        $dateTime = new DateTime($create_at);
        echo $dateTime->format('j-M-y   g:i:s A'); ?></span></p>
        <p style="margin-bottom: 10px; font-size: 13px;"><b><span class="sfont" style="font-size: 20px;">whleñ</span> </b> <span style="margin-left: 44px;"> : <?php echo $userinfo['fullname']; ?></span></p>
        
        
        
       
        <table>
            <thead>
                <!--<tr><th colspan='4'><span style="text-align: center; font-size:16px;">Item Name</span></th></tr>-->
                
                <tr style="border-bottom: 1px solid #000; border-top: 1px solid #000;">
                    
                    <th><span style="text-align: center; font-size:13px; font-weight: 800;">ප්‍රමාණය</span></th>
                    <th style="margin: 0; padding:0;"><span style="text-align: center; font-size:13px; display: inline-block; margin: 0; padding:0; font-weight: 800;">සදහන් මිල</span></th>
                    <th><span style="text-align: center; font-size:13px; display: inline-block; font-weight: 800;">අපේ මිල</span></th>
                    <th colspan="2" style="text-align: right; font-size:13px;"><span style="text-align: right; font-weight: 800;">එකතුව</span></th>
                </tr>
               
            </thead>
            <tbody>
                <?php
				// $total_amount = 0;
                // $dis=0;
                $total_amount = 0;
                $total_amount2 = 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'];
                            }
							$marketprice = $product['market_price'];
                           
						}
                        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;
                            
                        }
                        $markettotal = $markettotal + $marketprice*$pro_qty;
                        $total_amount += $product_price;
                        if($marketprice!=0){
                            $total_amount2 += $product_price;
                        }
                        $pro_price = number_format((float)$row["pro_price"], 2);
                        $product_price = number_format((float)$product_price, 2);
                        $marketprice = number_format((float)$marketprice, 2);
                        echo "<tr><td colspan='4'>$proname</td></tr>";
						echo "<tr class='itemsep'>";
                        echo "<td>$pro_qty</td>";
						echo "<td>$marketprice</td>";
						echo "<td>".$pro_price."</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;
               $t2 = $total_amount2+$discounted_amount;
               $s = $markettotal - $t2;
				?>
            </tbody>
            <tfoot>
                
                
                <tr>
                    <td colspan="5">
                        <hr>
                    </td>
                </tr>
                <tr style="line-height: 20px;">
                    <td colspan="4" style="font-size: 16px;"><b><span class="sfont" style="font-size:28px;">f.úh hq;= uqo,</span><span style="margin-left: 30px;">:</span></b></td>
                    
                    <td style="font-size: 16px; text-align:right;"><b><?php echo number_format((float)$t, 2); ?></b></td>
                </tr>
                <tr style="line-height: 8px;">
                    <td colspan="4"><b><span class="sfont" style="font-size:21px;">,nd ÿka uqo,</span><span style="margin-left: 82px;">:</span></b></td>
                    
                    <td style="text-align:right;"><b><?php if($pay_option=="cash"){ echo number_format((float)$paid_amount, 2); } ?></b></td>
                </tr>
                
                
              <!--  <tr>
                    <td colspan="3">වට්ටම්:</td>
                    <td></td>
                    <td><?php //echo number_format((float)$discount, 2); ?>%</td>
                </tr>-->
                <tr style="line-height: 20px;">
                    <td colspan="4"><b><span class="sfont" style="font-size:21px;">b;sß uqo,</span> <span style="margin-left: 102px;">:</span></b></td>
                    
                    <td style="text-align:right;"><b><?php echo abs(number_format((float)$balance_amount, 2)); ?></b></td>
                </tr>
                
                <!--<tr>
                    <td colspan="3">බිල් මුදල:</td>
                    <td>Rs</td>
                    <td><?php// echo number_format((float)$bill_amount, 2); ?></td>
                </tr>-->
                <?php 
                $s = $markettotal-$t2;
                    if ($s < 0) {
                        $s = 0;
                    }else{
                        $s = $s;
                    }
                
                ?>
                <tr style="line-height: 20px;">
                    <td colspan="4" style="font-size: 18px;"><b><span class="sfont" style="font-size:32px;">Tn ,enQ ,dNh</span><span style="margin-left: 20px; font-size: 14px;">:</span></b></td>
                   
                    <td style="font-size: 18px; text-align:right;"><b><?php echo number_format((float)$s, 2); ?></b></td>
                </tr>
                <tr style="border-top: 0; line-height: 20px;">
                    <td colspan="4"><span class="sfont" style="font-size:20px;">uq¿ NdKav ixLHdj</span> <span style="margin-left: 50px; font-size: 14px;">:</span></td>
                    <td style="text-align:right;"><?php echo $rowCount; ?></td>
                </tr>
            </tfoot>
        </table>

        <center>
            
            <hr>
            <p class="sfont" style="font-size: 20px;"><b>úiajdifhka /f.k hkak<b/></p>
            <p class="sfont" style="font-size: 22px;"><b>ia;=;shs - kej; tkak<b/></p>
            <p>Developed By Seopro Developer</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>