<?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: 80mm;
        margin: 0 auto;
        padding: 10px;
        box-sizing: border-box;
    }

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

    tbody{
        td{
            font-size: 12px;
           
        }
    }
    tbody{
        tr.itemsep{
           
        }
    }
    tfoot{
        td{
            font-size: 12px;
        }
    }
    th,
    td {
        padding: 5px;
        text-align: left;
        font-size: 10px;

    }

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

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

    .center {
        text-align: center;
    }
    .text1{
        position: relative;
        float: left;
    }
    .text1::after{
        position: absolute;
        left: 0;
        top: 10px;
        content:"";
        width: 90px;
        height: 5px;
        border-bottom: 2px solid;
    }
    .text1::before{
        position: absolute;
        left: 0;
        top: 15px;
        content:"";
        width: 100px;
        height: 5px;
        border-bottom: 2px solid;
    }
    @font-face {
    font-family: 'Dl-Hansika';
    src: url('Dl-Hansika.woff2') format('woff2'),
        url('Dl-Hansika.woff') format('woff');
    font-weight: 100;
    font-style: normal;
    font-display: swap;
}

    </style>
</head>

<body>
    <div class="container">
        <div style="width:100%;">
            <div style="text-align: center; margin-bottom:40px;">
                <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>
				
                <b><p style="text-align: left; font-size:18px; margin-top:12px; margin-left:20px; font-family: Dl-Hansika, sans-serif;">හොදම දේ ඉක්මනින්</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 style="text-align: right; float:right;margin-right:20px;">අඩු මිලට</span></p></b>
            </div>
        </div>
        <hr>
        <b><p>බිල් අංකය: <?php echo $orid; ?></p></b>
        <p><b>දිනය:</b> <?php 
        $dateTime = new DateTime($create_at);
        echo $dateTime->format('j-M-y g:i:s A'); ?></p>
        <p style="margin-bottom: 10px;"><b>අයකැමි:</b> <?php echo $userinfo['fullname']; ?></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:10px;">ප්‍රමාණය</span></th>
                    <th style="margin: 0; padding:0;"><span style="text-align: center; font-size:10px; display: inline-block; margin: 0; padding:0;">සදහන් මිල</span></th>
                    <th><span style="text-align: center; font-size:10px; display: inline-block;">අපේ මිල</span></th>
                    <th colspan="2" style="text-align: center; font-size:10px;"><span style="text-align: center;">එකතුව</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: center;'>$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="border-top: 0;">
                    <td colspan="4">මුළු භාණ්ඩ සංඛ්‍යාව:</td>
                    <td><?php echo $rowCount; ?></td>
                </tr>
                <tr>
                    <td colspan="5">
                        <hr>
                    </td>
                </tr>
                <tr>
                    <td colspan="3" style="font-size: 16px;"><b>ගෙවිය යුතු මුදල:</b></td>
                    <td style="font-size: 16px;"><b>Rs</b></td>
                    <td style="font-size: 16px;"><b><?php echo number_format((float)$t, 2); ?></b></td>
                </tr>
                <tr>
                    <td colspan="3"><b>ලබා දුන් මුදල:</b></td>
                    <td><b>Rs</b></td>
                    <td><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>
                    <td colspan="3"><b>ඉතිරි මුදල:</b></td>
                    <td><b>Rs</b></td>
                    <td><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="">
                    <td colspan="3" style="font-size: 18px;"><b>ඔබ ලැබූ ලාභය:</b></td>
                    <td style="font-size: 18px;"><b>Rs</b></td>
                    <td style="font-size: 18px;"><b><?php echo number_format((float)$s, 2); ?></b></td>
                </tr>

            </tfoot>
        </table>

        <center>
            <br>
            <hr>
            <p style="font-size: 16px;"><b>විස්වාසයෙන් රැගෙන යන්න<b/></p>
            <p style="font-size: 16px;"><b>ස්තුතියි - නැවත එන්න<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>