<?php
	session_start();
	date_default_timezone_set('Africa/Mogadishu');
	include("library/conn.php");
	
	$role_rs = array();
	$get_role_mode = mysqli_query($conn, "SELECT * FROM user_role_privileges WHERE userID = '". $_SESSION['userIdd'] ."' AND roleName = 'customer_payments'") or die (mysqli_error($conn));
	if (mysqli_num_rows($get_role_mode) > 0) {
		$role_rs = mysqli_fetch_array($get_role_mode);
	} else {
		$role_rs = ['-1', '-1', '-1', '-1', '-1', '-1'];
	}

    // This is getting the start date of every thing during the day
    $start_date_time = (date("H") > 6) ? date("Y-m-d 06:00:00") : date('Y-m-d 06:00:00', strtotime("-1 days"));
    // Total amount for this user's transactions
    $total_balance = 0;

?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<?php include("library/head.php");?>
		<title>Orders of Today | <?php echo $_SESSION['systemName']; ?></title>
        <style>
			.modal { overflow: auto !important; }
		</style>
	</head>
	<body class="app sidebar-mini">

		<!-- Navbar-->
		<?php include("library/header.php");?>
		<!-- Sidebar menu-->
		<div class="app-sidebar__overlay" data-toggle="sidebar"></div>
		<?php include("library/sidebar.php");?>
		
		<main class="app-content">
			<div class="app-title">
				<div>
					<h4 class="text-primary"><i class="fa fa-list-ul fa-lg"></i> ORDERS OF TODAY</h4>
				</div>
				<ul class="app-breadcrumb breadcrumb">
					<li class="breadcrumb-item"><i class="fa fa-home"></i></li>
					<li class="breadcrumb-item"><a href="today-orders">Orders of Today</a></li>
				</ul>
			</div>

			<?php include("models/admin_config.php");?>
			<?php include("models/system_config.php");?>

			<div class="row">
            
                <div class="col-md-3">
				    <div class="tile">

                        <?php

                            if (($_SESSION['userTypeID'] == 'PS0001') || ($_SESSION['userTypeID'] == 'PS0002') || ($_SESSION['userTypeID'] == 'PS0003')) {

                                $getAllCashiers = mysqli_query($conn, "SELECT system_users.userID, employees.employeePhoto, system_users.Username 
                                FROM employees
                                JOIN system_users ON employees.employeeID = system_users.employeeID
                                WHERE employees.employeePosition = 'PS0002' OR employees.employeePosition = 'PS0003' OR employees.employeePosition = 'PS0004'");
                                if ($getAllCashiers) {
                                    if (mysqli_num_rows($getAllCashiers) > 0) { ?>
                                        <div class="row">
                                            <div class="col-md-12 pb-2">
                                                <h5 class="text-primary"><i class="fa fa-file-text"></i> Transactions of <?php echo date('d F Y', strtotime($start_date_time)); ?></h5>
                                            </div>
                                            <div class="col-md-12">
                                                <div id="accordion"><?php
                                                    $cashiers_grand_total = 0;
                                                    while($cashiers_rs = mysqli_fetch_array($getAllCashiers)) { 
                                                        if ($_SESSION['userIdd'] == $cashiers_rs[0]) {
                                                            ?><div class="card border-0">
                                                                <div class="card-header p-0 mb-1" id="heading<?php echo $cashiers_rs[0]; ?>">
                                                                    <h4 class="mb-0">
                                                                        <button class="btn btn-dark col-md-12 pt-2 pb-0 text-left shadow" data-toggle="collapse" data-target="#collapse<?php echo $cashiers_rs[0]; ?>" aria-expanded="true" aria-controls="collapse<?php echo $cashiers_rs[0]; ?>">
                                                                            <h3>
                                                                                <span class="text-left">
                                                                                    <img class="img-fluid rounded-circle pr-2" src="<?php echo $cashiers_rs[1]; ?> " width="40px" height="40px"/><?php echo $cashiers_rs[2]. " (You)"; ?>
                                                                                </span>
                                                                                <span class="pull-right pt-2">
                                                                                    <?php
                                                                                        $getCashierTotalPaid = mysqli_query($conn, "SELECT SUM(IFNULL(depositedAmount, 0)) FROM account_transactions WHERE registeredBy = '". $cashiers_rs[0] ."' AND registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."'");
                                                                                        if ($getCashierTotalPaid) {
                                                                                            if (mysqli_num_rows($getCashierTotalPaid) > 0) {
                                                                                                $cashier_total_rs = mysqli_fetch_array($getCashierTotalPaid);
                                                                                                echo "$". number_format($cashier_total_rs[0], 2);
                                                                                            }
                                                                                        }
                                                                                    ?>
                                                                                </span>
                                                                            </h3>
                                                                        </button>
                                                                    </h4>
                                                                </div>
                                                                <div id="collapse<?php echo $cashiers_rs[0]; ?>" class="collapse show" aria-labelledby="heading<?php echo $cashiers_rs[0]; ?>" data-parent="#accordion">
                                                                    <div class="card-body p-0">
                                                                    
                                                                        <div class="table-responsive">
                                                                            <table class="table table-hover table-bordered table-sm mb-4">
                                                                                <tbody>
                                                                                    <?php
                                                                                        
                                                                                        $cashier_total_balance = 0;

                                                                                        $getAccountsFirst = mysqli_query($conn, "SELECT payment_gateways.gatewayType, payment_gateways.gatewayLogo, payment_gateways.gatewayName, account_numbers.accountNoID, account_numbers.accountNoName,  account_numbers.accountNumber
                                                                                        FROM account_transactions
                                                                                        JOIN account_numbers ON account_transactions.accountNoID = account_numbers.accountNoID 
                                                                                        JOIN payment_gateways ON account_numbers.gatewayID = payment_gateways.gatewayID 
                                                                                        WHERE account_transactions.registeredBy = '". $cashiers_rs[0] ."' AND account_transactions.registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."' GROUP BY account_transactions.accountNoID");
                                                                                        if ($getAccountsFirst) {
                                                                                            if (mysqli_num_rows($getAccountsFirst) > 0) {
                                                                                                
                                                                                                while ($accounts_rs = mysqli_fetch_array($getAccountsFirst)) {
                                                                                                    ?><tr class="text-light bg-primary text-center" height="45px">
                                                                                                        <td class="align-middle text-left font-weight-bold" colspan="3">
                                                                                                            <?php
                                                                                                                if ($accounts_rs[0] == 1) {
                                                                                                                    ?><span class="font-weight-bold;"><img class="img-fluid" style="filter: brightness(0) invert(1);" src="<?php echo $accounts_rs[1]; ?>" width="25px"/> <?php echo $accounts_rs[2]; ?></span><?php
                                                                                                                } else if ($accounts_rs[0] == 2) {
                                                                                                                    ?><span class="font-weight-bold;"><img src="<?php echo $accounts_rs[1]; ?>" width="25px" style="filter: brightness(0) invert(1);"  /> <?php echo $accounts_rs[2]; ?> <?php echo $accounts_rs[4]; ?> (<?php echo $accounts_rs[5]; ?>)</span><?php
                                                                                                                } else if ($accounts_rs[0] == 3) {
                                                                                                                    ?><span class="font-weight-bold;"><img src="<?php echo $accounts_rs[1]; ?>" width="25px" style="filter: brightness(0) invert(1);"  /> <?php echo $accounts_rs[2]; ?> <?php echo $accounts_rs[4]; ?> (<?php echo $accounts_rs[5]; ?>)<?php
                                                                                                                }
                                                                                                            ?>
                                                                                                        </td>
                                                                                                    <tr><?php
                                                                                                    // Get the previous balance of this account
                                                                                                    $get_previous_balance = mysqli_query($conn, "SELECT ROUND(IFNULL(SUM(depositedAmount), 0), 2) - ROUND(IFNULL(SUM(withdrawalAmount), 0), 2) 'Openning Balance' FROM account_transactions WHERE accountNoID = '" . $accounts_rs[3] . "' AND registerDate < '". substr($start_date_time, 0, 10) ." 00:00' GROUP BY accountNoID") or die(mysqli_error($conn));
                                                                                                    if (mysqli_num_rows($get_previous_balance) > 0) {
                                                                                                        $prev_balance_rs = mysqli_fetch_array($get_previous_balance);
                                                                                                        $prev_balance_amount = $prev_balance_rs[0];
                                                                                                    } else {
                                                                                                        $prev_balance_amount = 0;
                                                                                                    } ?>
                                                                                                    <tr>
                                                                                                        <th class="align-middle text-right" colspan="2">Previous Balance</th>
                                                                                                        <th class="align-middle text-center text-primary" width="22%"><?php echo "$". number_format($prev_balance_amount, 2); ?></th>
                                                                                                    </tr>
                                                                                                    <tr class="text-light bg-dark text-center">
                                                                                                        <th class="align-middle" width="25%">Date</th>
                                                                                                        <th class="align-middle">Description</th>
                                                                                                        <th class="align-middle" width="25%">Amount</th>
                                                                                                    </tr><?php
                                                                                                    // Generate transactions of this account
                                                                                                    $cashier_account_total = 0;
                                                                                                    $generatedTransactions = mysqli_query($conn, "SELECT registerDate, transactionDescription, depositedAmount FROM account_transactions WHERE accountNoID = '". $accounts_rs[3] ."' AND depositedAmount > 0 AND registeredBy = '". $cashiers_rs[0] ."' AND registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."'");
                                                                                                    if ($generatedTransactions) {
                                                                                                        if (mysqli_num_rows($generatedTransactions) > 0) {
                                                                                                            while ($transacrions_rs = mysqli_fetch_array($generatedTransactions)) {
                                                                                                                ?><tr>
                                                                                                                    <td class="align-middle text-center font-weight-bold"><?php echo date('h:i A', strtotime($transacrions_rs[0])); ?></td>
                                                                                                                    <td class="align-middle text-left"><?php echo $transacrions_rs[1]; ?></td>
                                                                                                                    <td class="align-middle text-center text-success font-weight-bold"><?php echo "$". number_format($transacrions_rs[2], 2); ?></td>
                                                                                                                </tr><?php
                                                                                                                $cashier_account_total += $transacrions_rs[2];
                                                                                                                $cashier_total_balance += $transacrions_rs[2];
                                                                                                                $cashiers_grand_total += $transacrions_rs[2];
                                                                                                            }
                                                                                                        }
                                                                                                    } else {
                                                    
                                                                                                    } ?>
                                                                                                    <tr height="35px">
                                                                                                        <th class="align-middle text-right text-dark border-0" colspan="2">Total Balance</th>
                                                                                                        <th class="align-middle text-center text-primary border-0" width="22%"><?php echo "$". number_format($cashier_account_total, 2); ?></th>
                                                                                                    </tr><?php
                                                                                                }
                                                                                            }
                                                                                        } else {
                                                                                            
                                                                                        }
                                                                                    
                                                                                    ?>
                                                                                </tbody>
                                                                            </table>
                                                                            <table class="table table-bordered table-sm mb-5">
                                                                                <tr class="text-center text-light align-middle">
                                                                                    <th class="bg-primary pt-4 pb-3 border-0 rounded-left" width="50%"><h4>Total</h4></th>
                                                                                    <th class="bg-success pt-4 pb-3 border-0 rounded-right" width="50%"><h4><?php echo "$". number_format($cashier_total_balance, 2); ?></h4></th>
                                                                                </tr>
                                                                            </table>

                                                                        </div>
                                                                    
                                                                    </div>
                                                                </div>
                                                            </div><?php
                                                        } else {
                                                            ?><div class="card border-0">
                                                                <div class="card-header p-0 mb-1" id="heading<?php echo $cashiers_rs[0]; ?>">
                                                                    <h4 class="mb-0">
                                                                        <button class="btn btn-dark collapsed col-md-12 pt-2 pb-0 text-left shadow" data-toggle="collapse" data-target="#collapse<?php echo $cashiers_rs[0]; ?>" aria-expanded="false" aria-controls="collapse<?php echo $cashiers_rs[0]; ?>">
                                                                            <h3>
                                                                                <span class="text-left">
                                                                                    <img class="img-fluid rounded-circle pr-2" src="<?php echo $cashiers_rs[1]; ?> " width="40px" height="40px"/><?php echo $cashiers_rs[2]; ?>
                                                                                </span>
                                                                                <span class="pull-right pt-2">
                                                                                    <?php
                                                                                        $getCashierTotalPaid = mysqli_query($conn, "SELECT SUM(IFNULL(depositedAmount, 0)) FROM account_transactions WHERE registeredBy = '". $cashiers_rs[0] ."' AND registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."'");
                                                                                        if ($getCashierTotalPaid) {
                                                                                            if (mysqli_num_rows($getCashierTotalPaid) > 0) {
                                                                                                $cashier_total_rs = mysqli_fetch_array($getCashierTotalPaid);
                                                                                                echo "$". number_format($cashier_total_rs[0], 2);
                                                                                            }
                                                                                        }
                                                                                    ?>
                                                                                </span>
                                                                            </h3>
                                                                        </button>
                                                                    </h4>
                                                                </div>
                                                                <div id="collapse<?php echo $cashiers_rs[0]; ?>" class="collapse" aria-labelledby="heading<?php echo $cashiers_rs[0]; ?>" data-parent="#accordion">
                                                                    <div class="card-body p-0">
                                                                    
                                                                        <div class="table-responsive">
                                                                            <table class="table table-hover table-bordered table-sm mb-4">
                                                                                <tbody>
                                                                                    <?php
                                                                                        
                                                                                        $cashier_total_balance = 0;

                                                                                        $getAccountsFirst = mysqli_query($conn, "SELECT payment_gateways.gatewayType, payment_gateways.gatewayLogo, payment_gateways.gatewayName, account_numbers.accountNoID, account_numbers.accountNoName,  account_numbers.accountNumber
                                                                                        FROM account_transactions
                                                                                        JOIN account_numbers ON account_transactions.accountNoID = account_numbers.accountNoID 
                                                                                        JOIN payment_gateways ON account_numbers.gatewayID = payment_gateways.gatewayID 
                                                                                        WHERE account_transactions.registeredBy = '". $cashiers_rs[0] ."' AND account_transactions.registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."' GROUP BY account_transactions.accountNoID");
                                                                                        if ($getAccountsFirst) {
                                                                                            if (mysqli_num_rows($getAccountsFirst) > 0) {
                                                                                                while ($accounts_rs = mysqli_fetch_array($getAccountsFirst)) {
                                                                                                    ?><tr class="text-light bg-primary text-center" height="45px">
                                                                                                        <td class="align-middle text-left font-weight-bold" colspan="3">
                                                                                                            <?php
                                                                                                                if ($accounts_rs[0] == 1) {
                                                                                                                    ?><span class="font-weight-bold;"><img class="img-fluid" style="filter: brightness(0) invert(1);" src="<?php echo $accounts_rs[1]; ?>" width="25px"/> <?php echo $accounts_rs[2]; ?></span><?php
                                                                                                                } else if ($accounts_rs[0] == 2) {
                                                                                                                    ?><span class="font-weight-bold;"><img src="<?php echo $accounts_rs[1]; ?>" width="25px" style="filter: brightness(0) invert(1);"  /> <?php echo $accounts_rs[2]; ?> <?php echo $accounts_rs[4]; ?> (<?php echo $accounts_rs[5]; ?>)</span><?php
                                                                                                                } else if ($accounts_rs[0] == 3) {
                                                                                                                    ?><span class="font-weight-bold;"><img src="<?php echo $accounts_rs[1]; ?>" width="25px" style="filter: brightness(0) invert(1);"  /> <?php echo $accounts_rs[2]; ?> <?php echo $accounts_rs[4]; ?> (<?php echo $accounts_rs[5]; ?>)<?php
                                                                                                                }
                                                                                                            ?>
                                                                                                        </td>
                                                                                                    <tr><?php
                                                                                                    // Get the previous balance of this account
                                                                                                    $get_previous_balance = mysqli_query($conn, "SELECT ROUND(IFNULL(SUM(depositedAmount), 0), 2) - ROUND(IFNULL(SUM(withdrawalAmount), 0), 2) 'Openning Balance' FROM account_transactions WHERE accountNoID = '" . $accounts_rs[3] . "' AND registerDate < '". substr($start_date_time, 0, 10) ." 00:00' GROUP BY accountNoID") or die(mysqli_error($conn));
                                                                                                    if (mysqli_num_rows($get_previous_balance) > 0) {
                                                                                                        $prev_balance_rs = mysqli_fetch_array($get_previous_balance);
                                                                                                        $prev_balance_amount = $prev_balance_rs[0];
                                                                                                    } else {
                                                                                                        $prev_balance_amount = 0;
                                                                                                    } ?>
                                                                                                    <tr>
                                                                                                        <th class="align-middle text-right" colspan="2">Previous Balance</th>
                                                                                                        <th class="align-middle text-center text-primary" width="22%"><?php echo "$". number_format($prev_balance_amount, 2); ?></th>
                                                                                                    </tr>
                                                                                                    <tr class="text-light bg-dark text-center">
                                                                                                        <th class="align-middle" width="25%">Date</th>
                                                                                                        <th class="align-middle">Description</th>
                                                                                                        <th class="align-middle" width="25%">Amount</th>
                                                                                                    </tr><?php
                                                                                                    // Generate transactions of this account
                                                                                                    $cashier_account_total = 0;
                                                                                                    $generatedTransactions = mysqli_query($conn, "SELECT registerDate, transactionDescription, depositedAmount FROM account_transactions WHERE accountNoID = '". $accounts_rs[3] ."' AND depositedAmount > 0 AND registeredBy = '". $cashiers_rs[0] ."' AND registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."'");
                                                                                                    if ($generatedTransactions) {
                                                                                                        if (mysqli_num_rows($generatedTransactions) > 0) {
                                                                                                            while ($transacrions_rs = mysqli_fetch_array($generatedTransactions)) {
                                                                                                                ?><tr>
                                                                                                                    <td class="align-middle text-center font-weight-bold"><?php echo date('h:i A', strtotime($transacrions_rs[0])); ?></td>
                                                                                                                    <td class="align-middle text-left"><?php echo $transacrions_rs[1]; ?></td>
                                                                                                                    <td class="align-middle text-center text-success font-weight-bold"><?php echo "$". number_format($transacrions_rs[2], 2); ?></td>
                                                                                                                </tr><?php
                                                                                                                $cashier_account_total += $transacrions_rs[2];
                                                                                                                $cashier_total_balance += $transacrions_rs[2];
                                                                                                                $cashiers_grand_total += $transacrions_rs[2];
                                                                                                            }
                                                                                                        }
                                                                                                    } else {
                                                    
                                                                                                    } ?>
                                                                                                    <tr height="35px">
                                                                                                        <th class="align-middle text-right text-dark border-0" colspan="2">Total Balance</th>
                                                                                                        <th class="align-middle text-center text-primary border-0" width="22%"><?php echo "$". number_format($cashier_account_total, 2); ?></th>
                                                                                                    </tr><?php
                                                                                                }
                                                                                            }
                                                                                        } else {
                                                                                            
                                                                                        }
                                                                                    
                                                                                    ?>
                                                                                </tbody>
                                                                            </table>
                                                                            <table class="table table-bordered table-sm mb-5">
                                                                                <tr class="text-center text-light align-middle">
                                                                                    <th class="bg-primary pt-4 pb-3 border-0 rounded-left" width="50%"><h4>Total</h4></th>
                                                                                    <th class="bg-success pt-4 pb-3 border-0 rounded-right" width="50%"><h4><?php echo "$". number_format($cashier_total_balance, 2); ?></h4></th>
                                                                                </tr>
                                                                            </table>

                                                                        </div>
                                                                    
                                                                    </div>
                                                                </div>
                                                            </div><?php
                                                        }
                                                    } ?>
                                                </div>
                                            </div>
                                            <div class="col-md-12 mt-4">
                                                <table class="table table-bordered table-sm">
                                                    <tr class="text-center text-light align-middle">
                                                        <th class="bg-primary pt-4 pb-3 border-0 rounded-left" width="50%"><h4>Grand Total</h4></th>
                                                        <th class="bg-success pt-4 pb-3 border-0 rounded-right" width="50%"><h4><?php echo "$". number_format($cashiers_grand_total, 2); ?></h4></th>
                                                    </tr>
                                                </table>
                                            </div>
                                        </div><?php
                                    } else {

                                    }
                                } else {

                                }

                            } else {

                                ?><div class="row">
                                    <div class="col-md-12 pb-2">
                                        <h5 class="text-primary"><i class="fa fa-file-text"></i> My Transactions - <?php echo date('d F Y', strtotime($start_date_time)); ?></h5>
                                    </div>
                                    <div class="col-md-12">
                                        <div class="table-responsive">
                                            <table class="table table-hover table-bordered table-sm mb-4">
                                                <tbody>
                                                    <?php
                                                        
                                                        $getAccountsFirst = mysqli_query($conn, "SELECT payment_gateways.gatewayType, payment_gateways.gatewayLogo, payment_gateways.gatewayName, account_numbers.accountNoID, account_numbers.accountNoName,  account_numbers.accountNumber
                                                        FROM account_transactions
                                                        JOIN account_numbers ON account_transactions.accountNoID = account_numbers.accountNoID 
                                                        JOIN payment_gateways ON account_numbers.gatewayID = payment_gateways.gatewayID 
                                                        WHERE account_transactions.registeredBy = '". $_SESSION['userIdd'] ."' AND account_transactions.registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."' GROUP BY account_transactions.accountNoID");
                                                        if ($getAccountsFirst) {
                                                            if (mysqli_num_rows($getAccountsFirst) > 0) {
                                                                while ($accounts_rs = mysqli_fetch_array($getAccountsFirst)) {
                                                                    ?><tr class="text-light bg-primary text-center" height="45px">
                                                                        <td class="align-middle text-left font-weight-bold" colspan="3">
                                                                            <?php
                                                                                if ($accounts_rs[0] == 1) {
                                                                                    ?><span class="font-weight-bold;"><img class="img-fluid" style="filter: brightness(0) invert(1);" src="<?php echo $accounts_rs[1]; ?>" width="25px"/> <?php echo $accounts_rs[2]; ?></span><?php
                                                                                } else if ($accounts_rs[0] == 2) {
                                                                                    ?><span class="font-weight-bold;"><img src="<?php echo $accounts_rs[1]; ?>" width="25px" style="filter: brightness(0) invert(1);"  /> <?php echo $accounts_rs[2]; ?> <?php echo $accounts_rs[4]; ?> (<?php echo $accounts_rs[5]; ?>)</span><?php
                                                                                } else if ($accounts_rs[0] == 3) {
                                                                                    ?><span class="font-weight-bold;"><img src="<?php echo $accounts_rs[1]; ?>" width="25px" style="filter: brightness(0) invert(1);"  /> <?php echo $accounts_rs[2]; ?> <?php echo $accounts_rs[4]; ?> (<?php echo $accounts_rs[5]; ?>)<?php
                                                                                }
                                                                            ?>
                                                                        </td>
                                                                    <tr><?php
                                                                    // Get the previous balance of this account
                                                                    $get_previous_balance = mysqli_query($conn, "SELECT ROUND(IFNULL(SUM(depositedAmount), 0), 2) - ROUND(IFNULL(SUM(withdrawalAmount), 0), 2) 'Openning Balance' FROM account_transactions WHERE accountNoID = '" . $accounts_rs[3] . "' AND registerDate < '". substr($start_date_time, 0, 10) ." 00:00' GROUP BY accountNoID") or die(mysqli_error($conn));
                                                                    if (mysqli_num_rows($get_previous_balance) > 0) {
                                                                        $prev_balance_rs = mysqli_fetch_array($get_previous_balance);
                                                                        $prev_balance_amount = $prev_balance_rs[0];
                                                                    } else {
                                                                        $prev_balance_amount = 0;
                                                                    } ?>
                                                                    <tr>
                                                                        <th class="align-middle text-right" colspan="2">Previous Balance</th>
                                                                        <th class="align-middle text-center text-primary" width="22%"><?php echo "$". number_format($prev_balance_amount, 2); ?></th>
                                                                    </tr>
                                                                    <tr class="text-light bg-dark text-center">
                                                                        <th class="align-middle" width="25%">Date</th>
                                                                        <th class="align-middle">Description</th>
                                                                        <th class="align-middle" width="25%">Amount</th>
                                                                    </tr><?php
                                                                    // Generate transactions of this account
                                                                    $subtotal_balance = 0;
                                                                    $generatedTransactions = mysqli_query($conn, "SELECT registerDate, transactionDescription, depositedAmount FROM account_transactions WHERE accountNoID = '". $accounts_rs[3] ."' AND depositedAmount > 0 AND registeredBy = '". $_SESSION['userIdd'] ."' AND registerDate BETWEEN '". $start_date_time ."' AND '". date("Y-m-d H:i:s") ."'");
                                                                    if ($generatedTransactions) {
                                                                        if (mysqli_num_rows($generatedTransactions) > 0) {
                                                                            while ($transacrions_rs = mysqli_fetch_array($generatedTransactions)) {
                                                                                ?><tr>
                                                                                    <td class="align-middle text-center font-weight-bold"><?php echo date('h:i A', strtotime($transacrions_rs[0])); ?></td>
                                                                                    <td class="align-middle text-left"><?php echo $transacrions_rs[1]; ?></td>
                                                                                    <td class="align-middle text-center text-success font-weight-bold"><?php echo "$". number_format($transacrions_rs[2], 2); ?></td>
                                                                                </tr><?php
                                                                                $subtotal_balance += $transacrions_rs[2];
                                                                                $total_balance += $transacrions_rs[2];
                                                                            }
                                                                        }
                                                                    } else {
                    
                                                                    } ?>
                                                                    <tr height="35px">
                                                                        <th class="align-middle text-right text-dark border-0" colspan="2">Total Balance</th>
                                                                        <th class="align-middle text-center text-primary border-0" width="22%"><?php echo "$". number_format($subtotal_balance, 2); ?></th>
                                                                    </tr><?php
                                                                }
                                                            }
                                                        } else {
                                                            
                                                        }
                                                    
                                                    ?>
                                                </tbody>
                                            </table>
                                            <table class="table table-bordered table-sm mb-2">
                                                <tr class="text-center text-light align-middle">
                                                    <th class="bg-primary pt-4 pb-3 border-0 rounded-left" width="50%"><h4>Total</h4></th>
                                                    <th class="bg-success pt-4 pb-3 border-0 rounded-right" width="50%"><h4><?php echo "$". number_format($total_balance, 2); ?></h4></th>
                                                </tr>
                                            </table>

                                        </div>
                                    </div>
                                </div><?php

                            }

                        ?>

					</div>
				</div>

				<div class="col-md-9">
				    <div class="tile">
						<div class="tile-body">
						    <?php
						        if (($_SESSION['userTypeID'] == 'PS0001') || ($_SESSION['userTypeID'] == 'PS0002') || ($_SESSION['userTypeID'] == 'PS0003')) {
						            ?><form action="#" method="POST" id="searchDateBasedOrderForm">
                    				    <div class="row mt-1 mb-5 no-gutters align-items-center bg-primary p-3 rounded-lg">
                                            <div class="col-md-2">
                                                <h2 class="text-white pt-2">Search</h2>
                                            </div>
                                            <div class="col-md-2 pr-3">
                    							<label class="control-label text-white">Status</label><br>
                                                <select class="form-control" name="cmbOrderStatusSearchDateBasedOrders" id="cmbOrderStatusSearchDateBasedOrders" style="width: 100%;">
                                                    <option value="0">General (All)</option>
                                                    <option value="1">Full Paid</option>
                                                    <option value="2">Partial Paid</option>
                                                    <option value="3">Not Paid</option>
                    							</select>
                    						</div>
                                            <div class="col-md-3 pr-3">
                								<label class="control-label text-white">From Date</label>
                								<input class="form-control" type="date" name="txtFromDateSearchDateBasedOrders" id="txtFromDateSearchDateBasedOrders" />
                							</div>
                                            <div class="col-md-3 pr-3">
                								<label class="control-label text-white">To Date</label>
                								<input class="form-control" type="date" name="txtToDateSearchDateBasedOrders" id="txtToDateSearchDateBasedOrders" />
                							</div>
                							<div class="col-md-2">
                							    <label class="control-label">&nbsp;</label>
                								<button type="submit" class="btn btn-warning col-md-12" name="btnSearchDateBasedOrder" id="btnSearchDateBasedOrder"><i class="fa fa-search" style="font-size: 18px; margin-left: 5px; margin-bottom: 3px;"></i> Search</button>
                							</div>
                						</div>
                				    </form><?php
						        }
						    ?>

							<div class="table-responsive dateBasedOrdersSectionArea">
							    
                                <table class="table table-hover table-bordered table-sm display nowrap" id="ordersOfTodayTable">
                                    <thead>
                                        <tr class="text-light bg-primary text-center" style="height: 45px;">
                                            <th class="align-middle">Edit</th>
                                            <th class="align-middle">OrderID</th>
                                            <th class="align-middle">Customer Name</th>
                                            <th class="align-middle">Type</th>
                                            <th class="align-middle">Orderer</th>
                                            <th class="align-middle">Time</th>
                                            <th class="align-middle">Balance</th>
                                            <th class="align-middle">Action</th>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <?php
                                            
                                            $start_date_time = (date("H") > 6) ? date("Y-m-d 06:00:00") : date('Y-m-d 06:00:00', strtotime("-1 days"));
											if (($_SESSION['userTypeID'] == 'PS0001') || ($_SESSION['userTypeID'] == 'PS0002') || ($_SESSION['userTypeID'] == 'PS0003')) $fetchCriteria = "WHERE restaurant_orders.orderStatus != '4' AND restaurant_orders.registerDate BETWEEN '". $start_date_time."' AND '". date("Y-m-d H:i:s") ."'";
											else $fetchCriteria = "WHERE restaurant_orders.orderStatus != '4' AND (restaurant_orders.registeredBy = '". $_SESSION['userIdd'] ."' OR restaurant_orders.registeredBy IN (SELECT system_users.userID FROM system_users JOIN employees ON system_users.employeeID = employees.employeeID WHERE employees.workingAreaID = '". mysqli_real_escape_string($conn, $_SESSION['workingAreaID']) ."' AND employees.employeePosition = 'PS0008')) AND restaurant_orders.registerDate BETWEEN '". $start_date_time."' AND '". date("Y-m-d H:i:s") ."'";
                                            $getTodayOrdersList = mysqli_query($conn, "SELECT restaurant_orders.orderID, customers.customerName, order_types.orderTypeName, orderer_employee.employeePhoto, orderer_user.Username, restaurant_orders.registerDate, ROUND(IFNULL(restaurant_orders.totalAmount, 0), 2) 'OrderBalance', ROUND(IFNULL(restaurant_orders.totalAmount, 0) - IFNULL(SUM(customer_payments.paidAmount), 0), 2) 'UnpaidAmount', receiver_cashier.Username
                                            FROM restaurant_orders
                                            LEFT JOIN customers ON restaurant_orders.customerID = customers.customerID
                                            JOIN order_types ON restaurant_orders.orderTypeID = order_types.orderTypeID
											LEFT JOIN system_users orderer_user ON restaurant_orders.registeredBy = orderer_user.userID
											JOIN employees orderer_employee ON orderer_user.employeeID = orderer_employee.employeeID
                                            LEFT JOIN customer_payments ON restaurant_orders.orderID = customer_payments.orderID
                                            LEFT JOIN system_users receiver_cashier ON customer_payments.registeredBy = receiver_cashier.userID
											$fetchCriteria
                                            GROUP BY restaurant_orders.orderID
                                            ORDER BY restaurant_orders.registerDate DESC") or die(mysqli_error($conn));
                                            
                                            $count_total_orders = 0;
                                            $count_total_balance = 0;
                                            $count_total_due_balance = 0;
                                            while ($today_orders_rs = mysqli_fetch_array($getTodayOrdersList)) {?>
                                                <tr>
                                                    <td class="align-middle text-center">
                                                        <?php
                                                            if (($today_orders_rs[7] > 0) || ($today_orders_rs[2] == "Delivery")) { ?>
        														<span class="bg-primary text-white p-2 rounded-lg font-weight-bold btnEditOrderInformation" style="cursor: pointer;" data-id="<?php echo $today_orders_rs[0]; ?>" data-toggle="modal" data-target="#editRestaurantOrderModal" title="Edit Order"><i class="fa fa-pencil fa-lg"></i></span>
        														<span class="bg-success text-white p-2 rounded-lg font-weight-bold btnPrintOrderInformation" style="cursor: pointer;" data-id="<?php echo $today_orders_rs[0]; ?>" btn-type="Print" data-toggle="modal" data-target="#viewAndPrintOrderReceiptModal" title="Print Order"><i class="fa fa-print fa-lg"></i></span>
        														<span class="bg-danger text-white p-2 rounded-lg font-weight-bold btnCancelOrderInformation" style="cursor: pointer;" data-id="<?php echo $today_orders_rs[0]; ?>" title="Cancel Order"><i class="fa fa-times fa-lg"></i></span>
        													<?php } else if (($today_orders_rs[7] <= 0) && ($today_orders_rs[2] == "Seat In")) { ?>
        														<span class="bg-success text-white p-2 rounded-lg font-weight-bold btnPrintOrderInformation" style="cursor: pointer;" data-id="<?php echo $today_orders_rs[0]; ?>" btn-type="View" data-toggle="modal" data-target="#viewAndPrintOrderReceiptModal" title="View Order"><i class="fa fa-eye fa-lg"></i> View</span>
        													<?php } else if (($today_orders_rs[7] <= 0) && ($today_orders_rs[2] == "Take Away")) { ?>
            												    <span class="bg-success text-white p-2 rounded-lg font-weight-bold btnPrintOrderInformation" style="cursor: pointer;" data-id="<?php echo $today_orders_rs[0]; ?>" btn-type="View" data-toggle="modal" data-target="#viewAndPrintOrderReceiptModal" title="View Order"><i class="fa fa-eye fa-lg"></i></span>
            													<span class="bg-danger text-white p-2 rounded-lg font-weight-bold btnCancelOrderInformation" style="cursor: pointer;" data-id="<?php echo $today_orders_rs[0]; ?>" title="Cancel Order"><i class="fa fa-times fa-lg"></i></span>
                                                            <?php }
                                                        ?>
                                                        
														
													</td>
                                                    <td class="align-middle text-center"><span class="text-primary font-weight-bold"><?php echo $today_orders_rs[0]; ?></td>
                                                    <td class="align-middle text-left"><?php echo $today_orders_rs[1]; ?></td>
                                                    <td class="align-middle text-left">
                                                        <?php 
                                                            if ($today_orders_rs[2] == "Seat In") {
                                                                ?><h6 class="text-success"><i class='fa fa-hotel'></i> <?php echo $today_orders_rs[2]; ?></h6><?php
                                                            } else if ($today_orders_rs[2] == "Take Away") {
                                                                ?><h6 class="text-primary"><i class='fa fa-shopping-basket'></i> <?php echo $today_orders_rs[2]; ?></h6><?php
                                                            } else if ($today_orders_rs[2] == "Delivery") {
                                                                ?><h6 class="text-danger"><i class='fa fa-motorcycle'></i> <?php echo $today_orders_rs[2]; ?></h6><?php
                                                            }
                                                        ?>
                                                    </td>
													<td class="align-middle text-left"><img class="img-fluid rounded-circle pr-2" src="<?php echo $today_orders_rs[3]; ?> " width="40px" height="40px"/><?php echo $today_orders_rs[4]; ?></td>
													<td class="align-middle text-center font-weight-bold"><?php echo date('h:i A', strtotime($today_orders_rs[5])); ?></td>
                                                    <td class="align-middle text-center"><h6 class="text-success"><?php echo "$". number_format($today_orders_rs[6], 2); ?></h6></td>
                                                    <td class="align-middle text-center">
                                                        <?php
                                                            if ($today_orders_rs[7] > 0) {
                                                                ?><button class="btn btn-lg btn-danger col-md-12 pt-3 pb-3 text-left font-weight-bold btnPuyNowCustomerPayment" id="<?php echo "Update". $today_orders_rs[0]; ?>" data-id="<?php echo $today_orders_rs[0]; ?>" data-toggle="modal" data-target="#registerCustomerPaymentModal">
                                                                    <span class="text-light font-weight-normal" style="font-size: 12px; display: block;">Due: <b><?php echo "$". number_format($today_orders_rs[7], 2); ?></b></span>
                                                                    <i class="fa fa-credit-card"></i> Pay Now
                                                                </button><?php
                                                            } else if ($today_orders_rs[7] <= 0) {
                                                                ?><button class="btn btn-lg btn-success col-md-12 pt-3 pb-3 text-left font-weight-bold">
                                                                    <span class="font-weight-normal" style="font-size: 12px; display: block;">Cashier: <b><?php echo $today_orders_rs[8]; ?></b></span>
                                                                    <i class="fa fa-check"></i> Full Paid
                                                                </button><?php
                                                            }
                                                        
                                                        ?>
                                                        
                                                    </td>
                                                </tr><?php
                                                $count_total_balance += $today_orders_rs[6];
                                                $count_total_due_balance += $today_orders_rs[7];
                                                $count_total_orders++;
                                            }
                                        ?>
                                    </tbody>
                                     <tfoot>
                                        <tr height="50px">
                                            <th class="align-middle text-right" colspan="4"><h5 style="display: inline;">Total Orders</h5></th>
                                            <th class="align-middle text-center"><h5 class="text-primary" style="display: inline;"><?php echo $count_total_orders; ?></h5></th>
                                            <th class="align-middle text-center"><h5 style="display: inline;">Total</h5></th>
                                            <th class="align-middle text-center"><h5 class="text-success" style="display: inline;"><?php echo "$". number_format($count_total_balance, 2); ?></h5></th>
                                            <th class="align-middle text-center"><h5 class="text-danger" style="display: inline;"><?php echo "$". number_format($count_total_due_balance, 2); ?></h5></th>
                                        </tr>
                                    </tfoot>
                                </table>

							</div>
						</div>
					</div>
				</div>
			</div>
		</main>
	
		<!-- Essential javascripts for application to work-->
		<?php include("library/footer.php");?>	    	
		<?php include("library/scripts.php"); ?>
		<script type="text/javascript"> 
		    $("#ordersOfTodayTable").DataTable({"pageLength": 100, "order": []})
		  //  $("#cmbCustomerIddEditOrderInformation").select2({ tags: false, dropdownParent: $("#editRestaurantOrderModal") })
		    $("#cmbOrderStatusSearchDateBasedOrders").select2({ tags: false })
		    $("#cmbDepositAccountRegisterCustomerPayment0").select2({ tags: false, dropdownParent: $("#registerCustomerPaymentModal") })
		</script>
		
	</body>
</html>