<?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);
$tltdiscount=0;
$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"];
		$tltdiscount = $row["discount"];
        $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: 128mm;
            margin: 1px 1px 1px 1px;
            padding: 1px 1px 1px 1px;
            box-sizing: border-box;
        }

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

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

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

        th,
        td {
            padding: 5px;
            font-size: 9px;

        }

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

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

        .center {
            text-align: center;
        }
        td,th{
            padding-top: 2px;
            padding-bottom: 2px;
        }
    </style>
</head>

<body>
    <div class="container">
        <div style="width:100%;border-bottom:2px solid #000000;margin-bottom:5px;padding-bottom:5px;">
            <div style="text-align: center;">
                <b>
                    <p style="font-size:24px;font-weight:600;">Payroll</p>
                    <p style="font-size:14px;font-weight:600;">Pasyala Road,Mirigama</p>
                </b>
                <p style="font-size:14px;margin: 0;font-weight:600;">Tel : 071 513 2796</p>
            </div>
        </div>	
		<table>
		<tr>
        <td colspan="7" style='font-size:11px;font-weight:600;'>Bill No: <?php echo $orid; ?></td>
        <td colspan="5" style='font-size:11px;'></td>
        <td style='font-size:11px; text-align:right;font-weight:600;'>Cashier: <?php

                                                if ($orderinfo['user_id'] == "0") {
                                                    echo "Admin";
                                                } else {

                                                    $userinfo4 = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE user_id='" . $orderinfo['user_id'] . "'"));
                                                    echo $userinfo4['fullname'];
                                                }
                                                ?></td>
        </tr>
		<tr>
        <td colspan="7" style='font-size:11px;font-weight:600;'>Customer: <?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'];
                                                }
                                                ?></td>
        <td colspan="5" style='font-size:11px;font-weight:600;'></td>
        <td style='font-size:11px; text-align:right;font-weight:600;'>Method : <?php echo $pay_option; ?></td>
        </tr>
		<tr>
        <td colspan="7" style='font-size:11px;font-weight:600;'>Date: <?php echo date_format(date_create($create_at),"M d, Y"); ?></td>
		<td colspan="5" style='font-size:11px;font-weight:600;'></td>
        <td style='font-size:11px; text-align:right;font-weight:600;'>Time: <?php echo date_format(date_create($create_at),"h:i:s A"); ?></td>
        </tr>
		</table>
			
        <table style="margin-top:5px;">
            <thead>
				<tr style="background-color:#000000;color:#ffffff;font-size:12px;">
					<th colspan='5'><span style="text-align: left; font-size:12px;font-weight:600;white-space: nowrap;color:#ffffff;">භාණ්ඩයේ නම</span></th>
                    <th colspan='2'><span style="text-align:left;font-size:12px;font-weight:600;white-space: nowrap;color:#ffffff;">අපේ මිල</span></th>
					<th><span style="text-align:center;font-size:12px;font-weight:600;white-space: nowrap;color:#ffffff;">වෙළද මිල</span></th>
                    <th><span style="text-align:center;font-size:12px;font-weight:600;white-space: nowrap;color:#ffffff;">ප්‍රමාණය</span></th>
                    <th><span style="text-align:center;font-size:12px;font-weight:600;white-space: nowrap;color:#ffffff;">වට්ටම</span></th>
                    <th><span style="text-align: right; font-size:12px;font-weight:600;color:#ffffff;">එකතුව</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;
                $profit1tot = 0;
				$profit1tot44 = 0;
				$totalprofit1tot = 0;
                if ($result->num_rows > 0) {
                    $distot = 0;
                    $marketprice = 0;
                    
                    
                    while ($row = $result->fetch_assoc()) {
                        $profit1 = 0;
                        $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($product['market_price']!= 0){
                                $marketprice = $product['market_price'];
                            }else{
                                $marketprice = $row['market_price2'];
                            }

                            
                        }   
                        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;
                                }
                                
                            }
                        }

                        $product_price =  $product_price ;

                        $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 colspan='5' style='font-size:12px;'><strong>$proname</strong></td>";
                        echo "<td colspan='2' style='text-align: center; font-size:12px;font-weight:600;'>" .  $pro_price. "</td>";
						echo "<td style='text-align: center; font-size:12px;font-weight:600;'>" .   number_format((float)$marketprice, 2) . "</td>";
                        echo "<td style='text-align: center; font-size:12px;font-weight:600;'>" . $pro_qty. "</td>";
                        echo "<td style='text-align: center; font-size:12px;font-weight:600;'>" . $pro_discount . $dismark . "</td>";

                        echo "<td style='text-align: right; font-size:12px;font-weight:600;'>" . $product_price. "</td>";
                        echo "</tr>";
                        $total_discount_amount += ($pro_discount * $pro_qty);
                        if($product['market_price']==0){
                            $profit1 = (float)$row['market_price2'] - (float)$row["pro_price"];
                        }else{
                            $profit1 = (float)$product['market_price'] - (float)$row["pro_price"];
                        }

                        $profit1 = $profit1 * $pro_qty;
                        $profit1tot44 = $profit1tot44 + $profit1;
                        
                    }
                }
                $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;
 
                 }

                  $tot4 =  $t - $dis2;

                  //$profit1tot =  $profit1tot + $dtotl;

                  //$profit1tot =  $profit1tot + $dis2;
				  
				  $totalprofit1tot =  $profit1tot44 + $tltdiscount + $dis2;
				  
                ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="11">
                        <hr>
                    </td>
                </tr>
                <tr>
                    <td colspan="10" style='font-size:14px;font-weight:600;'>මුළු භාණ්ඩ සංඛ්‍යාව:</td>
                    <td style='font-size:12px;font-weight:600;'><?php echo $rowCount;   ?></td>
                </tr>
                <tr>
                    <td colspan="11">
                        <hr>
                    </td>
                </tr>
                <tr>
                    <td colspan="9" style="font-size: 15px;font-weight:600;"><strong>මුළු එකතුව:</strong></td>
                    <td style="font-size: 15px;font-weight:600;"><strong>Rs.</strong></td>
                    <td style="font-size: 15px;font-weight:600;text-align:right;"><strong><?php echo number_format((float)$amount, 2); ?></strong></td>
                </tr>
                <tr>
                    <td colspan="9" style='font-size:14px;font-weight:600;'>භාණ්ඩ වට්ටම්:</td>
                    <td style='font-size:14px;font-weight:600;'>Rs.</td>
                    <td style='font-size:14px;font-weight:600;text-align:right;'><?php echo number_format((float)$tltdiscount, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="9" style='font-size:14px;font-weight:600;'>බිල් වට්ටම්:</td>
                    <td style='font-size:14px;font-weight:600;'>Rs.</td>
                    <td style='font-size:14px;font-weight:600;text-align:right;'><?php echo number_format((float)$dis2, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="9" style='font-size:14px;font-weight:600;'>බිල් මුදල:</td>
                    <td style='font-size:14px;font-weight:600;'>Rs.</td>
                    <td style='font-size:14px;font-weight:600;text-align:right;'><?php echo number_format((float)$amount, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="9" style='font-size:14px;font-weight:600;'>ගෙවූ මුදල:</td>
                    <td style='font-size:14px;font-weight:600;'>Rs.</td>
                    <td style='font-size:14px;font-weight:600;text-align:right;'><?php echo number_format((float)$paid_amount, 2); ?></td>
                </tr>
                <tr>
                    <td colspan="9" style='font-size:14px;font-weight:600;'>ඉතිරි මුදල:</td>
                    <td style='font-size:14px;font-weight:600;'>Rs.</td>
                    <td style='font-size:14px;font-weight:600;text-align:right;'><?php 
                    $balance_amount = abs($paid_amount-$amount);
                    
                    echo number_format((float)$balance_amount, 2); ?></td>
                </tr>
                <?php if ($pay_option == "credit") { ?>


                    <tr>
                        <td colspan="9" style='font-size:14px;font-weight:600;'>Credit Balance:</td>
                        <td style='font-size:14px;font-weight:600;'>Rs.</td>
                        <td style='font-size:14px;font-weight:600;text-align:right;'><?php
                                                    $userinfo5 = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobilecustomers WHERE coid='" . $orderinfo['customer'] . "'"));
                                                    echo number_format((float)$userinfo5['credit_limit'] - $userinfo5['ccredit'], 2); ?></td>
                    </tr>
                <?php } ?>


                <tr style="border:1px solid #000;">
                    <td colspan="12" style="font-weight:600;font-size: 14px;text-align:center;">ඔබ ලැබූ ලාභය : Rs.<?php echo number_format((float)$totalprofit1tot, 2); ?></td>
                </tr>
            </tfoot>
        </table>

        <center>
            <br>
            <p style="font-size:14px;font-weight:600;">ස්තුතියි.නැවත එන්න!!</p>
            <p style="font-size:14px;font-weight:600;">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();
?>