<?php
session_start();
include 'db_connection.php';
//$user_id = $_SESSION["user_id"];
$receiptno = $_GET['receiptno'];
$orderinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM cash_out WHERE cashoutid='$receiptno'"));

$userid = $orderinfo['user_id'];
$bradnchid = $orderinfo['branch_id'];
$userinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileusers WHERE user_id='$userid'"));


?>

<!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: 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;
    }
    </style>
</head>

<body>
    <div class="container">
        <div style="width:100%;">
            <div style="text-align: center;">
                <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>
            </div>
        </div>
       
        <p style="margin-top: 10px; margin-bottom:5px;"><strong>SHIFT CLOSE  Operator: <?php echo $userinfo['fullname']; ?></strong></p>
        <p style="margin-bottom: 10px;"><?php echo $orderinfo['added_date']; ?></p>
       
        
        <table>
            <thead>
                
                
            </thead>
            <tbody>
            <tr><td colspan='4'><span style="text-align: center; font-size:14px; font-weight:600;">SALES:</span></td></tr>
                <?php
                $date = $orderinfo['added_date'];
                    $sales = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(paid_amount) as amount, user_id,branch_id FROM mobileorders WHERE user_id='$userid' AND branch_id='$bradnchid' AND DATE(create_at)=DATE('$date')"));


            
						echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>TOTAL</td>";
						echo "<td style='text-align: right;'>".number_format((float)$sales['amount'], 2)."</td>";
						echo "</tr>";



                    

                    echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;'></td>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='2'><strong>TOTAL SALES</strong></td>";
                        echo "<td style='text-align: right;'>".number_format((float)$sales['amount'], 2)."</td>";
                        echo "</tr>";
				?>


                
                <?php
                    $date = $orderinfo['added_date'];
                    $expenses = mysqli_query($conn, "SELECT * FROM mobileexpense WHERE user_id='$userid' AND branch_id='$bradnchid' AND DATE(add_date)=DATE('$date')");
                    $tot = 0;
                    if ($expenses->num_rows > 0) { ?>
                        <tr><td colspan='4'><span style="text-align: center; font-size:14px; font-weight:600;">EXPENSES:</span></td></tr>
                        <?php while($row = $expenses->fetch_assoc()) {
                            $tot = $tot + $row['amount'];
						echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>".$row['expname']."</td>";
						echo "<td style='text-align: right;'>".number_format((float)$row['amount'], 2)."</td>";
						echo "</tr>";

                        }
                        echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;'></td>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='2'><strong>TOTAL EXPENSES</strong></td>";
                        echo "<td style='text-align: right;'>".number_format((float)$tot, 2)."</td>";
                        echo "</tr>";
                    }

				?>
                <tr><td colspan='4'><span style="text-align: center; font-size:14px; font-weight:600;">CASH SUMMARY:</span></td></tr>
                <?php
                $date = $orderinfo['added_date'];
                    $cashin = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM cash_in_out WHERE user_id='$userid' AND branch_id='$bradnchid' AND DATE(added_date)=DATE('$date')"));
                    $sum = 0;
                    $sum5000 = $cashin['m5000']*5000;
                    $sum1000 = $cashin['m1000']*1000;
                    $sum500 = $cashin['m500']*500;
                    $sum100 = $cashin['m100']*100;
                    $sum50 = $cashin['m50']*50;
                    $sum20 = $cashin['m20']*20;
                    $coins = $cashin['coins'];
            
                    $sum =  $sum5000 + $sum1000 + $sum500 + $sum100 + $sum50 +  $sum20 + $coins;

            
						echo "<tr>";
						echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>OPENING CASH</td>";
						echo "<td style='text-align: right;'>".number_format((float)$sum, 2)."</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>TOTAL CASH-IN</td>";
						echo "<td style='text-align: right;'>".number_format((float)$sales['amount'], 2)."</td>";
                        echo "</tr>";
                        echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>TOTAL CASH-OUT</td>";
						echo "<td style='text-align: right;'>".number_format((float)$tot, 2)."</td>";
						echo "</tr>";

                        $tcash = ($sales['amount']+$sum)-$tot;

                        echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;'></td>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='2'><strong>CASHIER CASH BALANCE</strong></td>";
                        echo "<td style='text-align: right;'>".number_format((float)$tcash, 2)."</td>";
                        echo "</tr>";

                        $cashout = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM cash_out WHERE user_id='$userid' AND branch_id='$bradnchid' AND DATE(added_date)=DATE('$date')"));
                        $sum1 = 0;
                        $sum5000 = $cashout['m5000']*5000;
                        $sum1000 = $cashout['m1000']*1000;
                        $sum500 = $cashout['m500']*500;
                        $sum100 = $cashout['m100']*100;
                        $sum50 = $cashout['m50']*50;
                        $sum20 = $cashout['m20']*20;
                        $coins = $cashout['coins'];
                
                        $sum1 =  $sum5000 + $sum1000 + $sum500 + $sum100 + $sum50 +  $sum20 + $coins;

                        echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>CASH HANDED-OVER</td>";
						echo "<td style='text-align: right;'>".number_format((float)$sum1, 2)."</td>";
						echo "</tr>";


                        $ex = $tcash - $sum1;

                        echo "<tr>";
                        echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>CASH SHORTAGE(EXCESS)</td>";
						echo "<td style='text-align: right;'>".number_format((float)$ex, 2)."</td>";
						echo "</tr>";
				?>
                <tr><td colspan='4'><span style="text-align: center; font-size:14px; font-weight:600;">CREDIT SALES:</span></td></tr>
                 <?php 
                    $ccsales = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,user_id,branch_id,added_date FROM mobile_credit_payments WHERE user_id='$userid' AND branch_id='$bradnchid' AND DATE(added_date)=DATE('$date')"));

                    echo "<tr>";
                    echo "<td style='padding-left:25px;font-size: 12px;' colspan='3'>TOTAL CREDIT SALES</td>";
                    echo "<td style='text-align: right;'>".number_format((float)$ccsales['amount'], 2)."</td>";
                    echo "</tr>";
                 
                 
                 ?>   
            </tbody>
           
        </table>

        
    </div>
    <!-- JavaScript to trigger print functionality -->
    <script>
    window.onload = function() {
        window.print();
    };
    </script>
</body>

</html>

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

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