<?php
include 'load.php';
if (isset($_SESSION['user_id'])) {
	$user_qury_02 = mysqli_query($conn, "SELECT * FROM tbl_user_branch WHERE user_id='" . $_SESSION['user_id'] . "'");
	$user_res = mysqli_fetch_array($user_qury_02);
	$user_branch = $user_res['branch_id'];
}
$branchid = $_SESSION['branch_id'];

$id2 = $user_res['branch_id'];
$query2 = $DB_con->prepare('SELECT branch_name FROM branch WHERE branch_id=' . $id2);
$query2->execute();
$result2 = $query2->fetch();
$branchName = ($result2['branch_name']) ? ' | ' . $result2['branch_name'] : '';

//$sql = "SELECT DATE_FORMAT(create_at,'%Y-%m') AS sale_month, SUM(amount) AS total_amount
	//				FROM mobileorders
	//				WHERE create_at >= DATE_FORMAT(CURRENT_DATE,'%Y-01-01')
	//				GROUP BY DATE_FORMAT(create_at,'%Y-%m') ";
if(isset($_POST['branch'])){
	$branch = $_POST['branch'];
	if($branch=="all"){
		$sql = "SELECT DATE_FORMAT(create_at, '%Y-%m-%d') AS sale_date, 
               SUM(amount) AS total_amount
        FROM mobileorders
        WHERE create_at >= DATE_FORMAT(CURRENT_DATE, '%Y-01-01')
        GROUP BY DATE_FORMAT(create_at, '%Y-%m-%d')
        ORDER BY sale_date ASC";
	}else{
		$sql = "SELECT DATE_FORMAT(create_at, '%Y-%m-%d') AS sale_date, 
               SUM(amount) AS total_amount
        FROM mobileorders
        WHERE create_at >= DATE_FORMAT(CURRENT_DATE, '%Y-01-01') AND branch_id = '$branch'
        GROUP BY DATE_FORMAT(create_at, '%Y-%m-%d')
        ORDER BY sale_date ASC";
	}
}else{
	$sql = "SELECT DATE_FORMAT(create_at, '%Y-%m-%d') AS sale_date, 
               SUM(amount) AS total_amount
        FROM mobileorders
        WHERE create_at >= DATE_FORMAT(CURRENT_DATE, '%Y-01-01') AND branch_id = '$branchid'
        GROUP BY DATE_FORMAT(create_at, '%Y-%m-%d')
        ORDER BY sale_date ASC";
}

//$sql = "SELECT DATE_FORMAT(create_at, '%Y-%m-%d') AS sale_date, 
  //             SUM(amount) AS total_amount
  //      FROM mobileorders
   //     WHERE create_at >= DATE_FORMAT(CURRENT_DATE, '%Y-01-01') AND branch_id = '$branchid'
   //     GROUP BY DATE_FORMAT(create_at, '%Y-%m-%d')
   //     ORDER BY sale_date ASC";



$result = $conn->query($sql);

$labels = [];
$data = [];
if ($result->num_rows > 0) {
	while ($row = $result->fetch_assoc()) {
		//$labels[] = date('F Y', strtotime($row['sale_month'] . '-01'));
		//$data[] = $row['total_amount'];
		$labels[] = date('d M Y', strtotime($row['sale_date'])); 
        $data[] = $row['total_amount'];
	}
}





?>

<!DOCTYPE html>
<html lang="en">

<head>
	<style>
		.card {
			background-color: #fff;
			border-radius: 10px;
			border: none;
			position: relative;
			margin-bottom: 15px;
			box-shadow: 0 0.46875rem 2.1875rem rgba(90, 97, 105, 0.1), 0 0.9375rem 1.40625rem rgba(90, 97, 105, 0.1), 0 0.25rem 0.53125rem rgba(90, 97, 105, 0.12), 0 0.125rem 0.1875rem rgba(90, 97, 105, 0.1);
		}

		.l-bg-cherry {
			background: linear-gradient(to right, #493240, #f09) !important;
			color: #fff;
		}

		.l-bg-blue-dark {
			background: linear-gradient(to right, #373b44, #4286f4) !important;
			color: #fff;
		}

		.l-bg-green-dark {
			background: linear-gradient(to right, #0a504a, #38ef7d) !important;
			color: #fff;
		}

		.l-bg-orange-dark {
			background: linear-gradient(to right, #a86008, #ffba56) !important;
			color: #fff;
		}

		.l-bg-purple-dark {
			background: linear-gradient(to right, #4b0082, #9932cc) !important;
			color: #fff;
		}

		.l-bg-yellow-dark {
			background: linear-gradient(to right, #ffd700, #ff8c00) !important;
			color: #fff;
		}

		.l-bg-red-dark {
			background: linear-gradient(to right, #8b0000, #ff6347) !important;
			color: #fff;
		}

		.l-bg-cyan-dark {
			background: linear-gradient(to right, #008b8b, #00ced1) !important;
			color: #fff;
		}

		.l-bg-magenta-dark {
			background: linear-gradient(to right, #8b008b, #ff69b4) !important;
			color: #fff;
		}

		.l-bg-green-light {
			background-color: #d4edda;
			/* Light green color */
		}


		.card .card-statistic-3 .card-icon-large .fas,
		.card .card-statistic-3 .card-icon-large .far,
		.card .card-statistic-3 .card-icon-large .fab,
		.card .card-statistic-3 .card-icon-large .fal {
			font-size: 110px;
		}

		.card .card-statistic-3 .card-icon {
			padding: 2rem;
			text-align: center;
			line-height: 50px;
			margin-left: 15px;
			color: #000;
			position: absolute;
			right: -5px;
			top: 20px;
			opacity: 0.1;
		}

		.l-bg-cyan {
			background: linear-gradient(135deg, #289cf5, #84c0ec) !important;
			color: #fff;
		}

		.l-bg-green {
			background: linear-gradient(135deg, #23bdb8 0%, #43e794 100%) !important;
			color: #fff;
		}

		.l-bg-orange {
			background: linear-gradient(to right, #f9900e, #ffba56) !important;
			color: #fff;
		}

		.l-bg-cyan {
			background: linear-gradient(135deg, #289cf5, #84c0ec) !important;
			color: #fff;
		}

		.l-bg-pink {
			background: linear-gradient(135deg, #ff4e50, #ff8a65) !important;
			color: #fff;
		}

		.l-bg-blue {
			background: linear-gradient(135deg, #3498db, #9b59b6) !important;
			color: #fff;
		}

		.l-bg-purple {
			background: linear-gradient(135deg, #82589F, #A8CABA) !important;
			color: #fff;
		}

		.l-bg-yellow {
			background: linear-gradient(to right, #f9d423, #ff4e50) !important;
			color: #fff;
		}
	</style>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=0">
	<meta name="description" content="POS - Payroll | Pos & Invetory System | Dashboard
">
	<meta name="keywords" content="Payroll | Pos & Invetory System | Dashboard
">
	<meta name="author" content="Payroll | Pos & Invetory System | Dashboard
">
	<meta name="robots" content="noindex, nofollow">
	<title>Home | Payroll | Pos & Invetory System | Dashboard
</title>
	<?php require_once 'headercss.php'; ?>
	<script src="assets/js/popper.min.js" integrity="sha384-XThWIDJf7H0ql8C6iGvrZo9v3/PJq2gN73WTW9aKdFm+0lw1Wkk8jKVz07trZTdW" crossorigin="anonymous">
	</script>
	<script src="assets/js/bootstrap.min.js" integrity="sha384-Wm1vFJWFazXGMpBLQXeRRJIXwu4qnp68vmyeSogB8IZBfF2Pmr3UHzqLeW/Z+TZ8" crossorigin="anonymous">
	</script>
</head>

<body>
	<div id="global-loader">
		<div class="whirly-loader"> </div>
	</div>
	<div class="main-wrapper">
		<?php require_once 'header.php'; ?>
		<?php require_once 'navbar.php'; ?>
		<div class="page-wrapper">
			<div class="content">
				<div class="row">
					<div class="col-lg-12  col-12 admin_title">
						<h2>Welcome to the POS Admin Panel <?= $branchName; ?></h2>
					</div>
					<div class="col-lg-12  col-12 mb-4">
						<form action="" method="post">
							<div class="row">
								<div class="col-lg-4  col-12" style="display: flex;">
									<select class="form-control" id="branch" name="branch" required>
									<option value="">Select Branch</option>
									<option value="all">All</option>
										<?php $bra = mysqli_query($conn, "SELECT * FROM branch WHERE status='1'"); 
										if ($bra->num_rows > 0) {
											while($row = $bra->fetch_assoc()) { 
												if($_POST['branch']==$row['branch_id']){ ?>
													<option value="<?php echo $row['branch_id']; ?>" selected><?php echo $row['branch_name']; ?></option>
												<?php } else{ ?>
													<option value="<?php echo $row['branch_id']; ?>"><?php echo $row['branch_name']; ?></option>
													<?php } 
										
										 } 
										
											} ?>


									</select>
									<button type="submit" class="btn btn-primary" name="filter">Filter</button>
								</div>
							</div>
							</form>
					</div>
					<!-- <div class="card bg-light"> -->
					<div class="row chart_details">
						<div class="col-lg-12 col-md-12 col-xl-12">
						<div class="row">
							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-orange-dark">
									<div class="card-statistic-3 p-4">
										<?php
										
										date_default_timezone_set("Asia/Colombo");
											
										//$startmonth = date("Y-m-01");
										//$endmonth = date("Y-m-30");
										//$currentmonth = date('F', strtotime($startmonth));

										//if ($branch_id == '1') {

										//$stmt = $DB_con->prepare("SELECT SUM(mo.amount) as totalsales FROM mobileorders mo WHERE mo.create_at BETWEEN '$startmonth 00:00:00' AND '$endmonth 23:59:59'");

										//} else {
											if(isset($_POST['branch'])){
												$branch = $_POST['branch'];
												if($branch=="all"){
													$branchestext = "";
												}else{
													$branchestext = "mo.branch_id='$branch'";
												}

											}else{
												$branchestext = "mo.branch_id='$branchid'";
											}
										$stmt = $DB_con->prepare("SELECT SUM(mo.amount) as totalsales FROM mobileorders mo WHERE $branchestext");

										//}
										
										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);

										$SumAmountView = $row['totalsales'];
										
										?>
										<div class="card-icon card-icon-large"><i class="fas fa-coins"></i> </div>
										<div class="">
											<h5 class="card-title mb-0">
											<h6>Total Sales</h6>
											</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php 
												$SumAmountView = $SumAmountView ?? 0;
												echo number_format($SumAmountView, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-cyan" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>
							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-cherry">
									<div class="card-statistic-3 p-4">
										<?php
										
										date_default_timezone_set("Asia/Colombo");
											
										$startmonth = date("Y-m-01");
										$endmonth = date("Y-m-30");
										$currentmonth = date('F', strtotime($startmonth));

										//if ($branch_id == '1') {

										//$stmt = $DB_con->prepare("SELECT SUM(mo.amount) as totalsales FROM mobileorders mo WHERE mo.create_at BETWEEN '$startmonth 00:00:00' AND '$endmonth 23:59:59'");

										//} else {
											if(isset($_POST['branch'])){
												$branch = $_POST['branch'];
												if($branch=="all"){
													$branchestext = "";
												}else{
													$branchestext = "AND mo.branch_id='$branch'";
												}

											}else{
												$branchestext = "AND mo.branch_id='$branchid'";
											}
										$stmt = $DB_con->prepare("SELECT SUM(mo.amount) as totalsales FROM mobileorders mo WHERE  mo.create_at BETWEEN '$startmonth 00:00:00' AND '$endmonth 23:59:59' $branchestext");

										//}
										
										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);

										$SumAmountView = $row['totalsales'];
										
										?>
										<div class="card-icon card-icon-large"><i class="fas fa-coins"></i> </div>
										<div class="">
											<h5 class="card-title mb-0">
											<h6>Total <?php echo $currentmonth; ?> Sales</h6>
											</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php 
												$SumAmountView = $SumAmountView ?? 0;
												echo number_format($SumAmountView, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-cyan" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-blue-dark">
									<div class="card-statistic-3 p-4">
									<?php
										
										date_default_timezone_set("Asia/Colombo");
										$today = date("Y-m-d");
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND mo.branch_id=:branch_id";
											}
										} else {
											$branchestext = "AND mo.branch_id=:branch_id";
										}
										//if ($branch_id == '1') {
										//	$stmt = $DB_con->prepare("SELECT SUM(mo.amount) as totalsales FROM mobileorders mo WHERE DATE(mo.create_at) = :today");
										//} else {
											$stmt = $DB_con->prepare("SELECT SUM(mo.amount) as totalsales FROM mobileorders mo WHERE DATE(mo.create_at) = :today $branchestext");
										//}
										
										$stmt->bindParam(':today', $today);
										if (!empty($branchestext)) {  
											if (isset($branch) && $branch != "all") {
												$stmt->bindParam(':branch_id', $branch);
											} else if (isset($branchid)) { 
												$stmt->bindParam(':branch_id', $branchid);
											}
										}
										
										
										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);
										
										$SumDailyAmountView = $row['totalsales'];

										?>
										<div class="card-icon card-icon-large"><i class="fas fa-shopping-cart"></i></div>
										<div class="">
										<h5 class="card-title mb-0">Total Daily Sales</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													Rs.<?php 
													$SumDailyAmountView = $SumDailyAmountView ?? 0;
													echo number_format($SumDailyAmountView, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-green" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-cherry">
									<div class="card-statistic-3 p-4">
									<?php
										
										date_default_timezone_set("Asia/Colombo");
										$yesterday = date("Y-m-d", strtotime("-1 day"));
										
										//if ($branch_id == '1') {

										//$stmt = $DB_con->prepare("SELECT SUM(mo.amount) as totalsales FROM mobileorders mo WHERE DATE(mo.create_at) = '$yesterday'");

										//} else {
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$branchestext = "AND mo.branch_id='$branch'";
												}
											} else {
												$branchestext = "AND mo.branch_id='$branchid'";
											}
										$stmt = $DB_con->prepare("SELECT SUM(mo.amount) as totalsales FROM mobileorders mo WHERE DATE(mo.create_at) = '$yesterday' $branchestext");

										//}
										
										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);

										$SumYesterdayAmountView = $row['totalsales'];
	
										?>
											
										<div class="card-icon card-icon-large"><i class="fas fa-shopping-cart"></i></div>
										<div class="">
											<h5 class="card-title mb-0">Total Yesterday Sales</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													Rs.<?php 
													$SumYesterdayAmountView = $SumYesterdayAmountView ?? 0;
													echo number_format($SumYesterdayAmountView, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-green" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>
<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-red-dark">
									<div class="card-statistic-3 p-4">
									<?php
											
											date_default_timezone_set("Asia/Colombo");
											//$startmonth = date("Y-m-01");
											//$endmonth = date("Y-m-30");
											//$currentmonth = date('F', strtotime($startmonth));
											
											$profit=0;
											$totalprofit=0;
											$marketprofit=0;

											$totalrowprofitamount=0;
											$totalrowmarketprofitamount=0;
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$branchestext = "AND mo.branch_id='$branch'";
												}
											} else {
												$branchestext = "AND mo.branch_id='$branchid'";
											}
										/*	if ($branch_id == '1') {
												$stmt = $DB_con->prepare("SELECT mo.order_id as moorderid,mo.orid,mo.customer,mo.pay_option,mo.amount as moamount,mo.paid_amount,mo.due_amount,mtc.pro_discount,mtc.proid,mtc.discount_type,mo.discount,mo.all_discount,mo.discount_type,mo.status as mostatus,mo.create_at,SUM(mo.discount) as totaldiscount,SUM(mo.discount) as totaldiscount,SUM(mtc.pro_price * mtc.pro_qty) as totalbillptotal,SUM(mp.price * mtc.pro_qty) as totalproducttotal,SUM(mp.market_price * mtc.pro_qty) as totalproductmarkettotal FROM mobileorders mo INNER JOIN 
mobiletemporarycart mtc ON mo.order_id=mtc.orderid INNER JOIN
mobileproducts mp ON mtc.proid=mp.prid
WHERE mo.create_at BETWEEN '$startmonth 00:00:00' AND '$endmonth 23:59:59' AND mtc.proid!='148' ORDER BY mo.create_at DESC");
											} else { */
												$stmt = $DB_con->prepare("SELECT mo.order_id as moorderid,mo.orid,mo.customer,mo.pay_option,mo.amount as moamount,mo.paid_amount,mo.due_amount,mtc.pro_discount,mtc.proid,mtc.discount_type,mo.discount,mo.all_discount,mo.discount_type,mo.status as mostatus,mo.create_at,SUM(mo.discount) as totaldiscount,SUM(mtc.pro_dis_price * mtc.pro_qty) as totalbillptotal,SUM(mp.price * mtc.pro_qty) as totalproducttotal FROM mobileorders mo INNER JOIN 
mobiletemporarycart mtc ON mo.order_id=mtc.orderid INNER JOIN
mobileproducts mp ON mtc.proid=mp.prid
WHERE mtc.proid!='148' $branchestext ORDER BY mo.create_at DESC");
											//}

										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);

										$profit=$row['totalbillptotal']-$row['totalproducttotal'];

										//$totalrowprofitamount=$totalrowprofitamount+$profit-$row['totaldiscount'];
										
										//$totalrowmarketprofitamount=$totalrowmarketprofitamount+$marketprofit-$row['totaldiscount'];
										
										$totalprofit=$profit;
										
										//$oooid = $row['moorderid'];

										//$orders = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(mobileorders.amount) as amount FROM mobileorders WHERE mobileorders.order_id='$oooid' and mobileorders.branch_id='$branchid'"));
										//$productcost = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(mobileproducts.price * mobiletemporarycart.pro_qty) as cost
														//FROM mobiletemporarycart
														//JOIN mobileproducts ON mobiletemporarycart.proid = mobileproducts.prid WHERE mobiletemporarycart.orderid='$oooid' and mobiletemporarycart.branch_id='$branchid'"));
										//$totalprofi = $orders['amount'] - $productcost['cost'];
										//$profitnormal = number_format($totalprofi, 2);
										//var_dump($totalprofi);

										?>
										<div class="card-icon card-icon-large"><i class="fas fa-ticket-alt"></i></div>
										<div class="">
											<h5 class="card-title mb-0">Total Profit(Normal)</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													Rs.<?php echo number_format($totalprofit, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-orange" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-green-dark">
									<div class="card-statistic-3 p-4">
									<?php
											
											date_default_timezone_set("Asia/Colombo");
											$startmonth = date("Y-m-01");
											$endmonth = date("Y-m-30");
											$currentmonth = date('F', strtotime($startmonth));
											
											$profit=0;
											$totalprofit=0;
											$marketprofit=0;

											$totalrowprofitamount=0;
											$totalrowmarketprofitamount=0;
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$branchestext = "AND mo.branch_id='$branch'";
												}
											} else {
												$branchestext = "AND mo.branch_id='$branchid'";
											}
										/*	if ($branch_id == '1') {
												$stmt = $DB_con->prepare("SELECT mo.order_id as moorderid,mo.orid,mo.customer,mo.pay_option,mo.amount as moamount,mo.paid_amount,mo.due_amount,mtc.pro_discount,mtc.proid,mtc.discount_type,mo.discount,mo.all_discount,mo.discount_type,mo.status as mostatus,mo.create_at,SUM(mo.discount) as totaldiscount,SUM(mo.discount) as totaldiscount,SUM(mtc.pro_price * mtc.pro_qty) as totalbillptotal,SUM(mp.price * mtc.pro_qty) as totalproducttotal,SUM(mp.market_price * mtc.pro_qty) as totalproductmarkettotal FROM mobileorders mo INNER JOIN 
mobiletemporarycart mtc ON mo.order_id=mtc.orderid INNER JOIN
mobileproducts mp ON mtc.proid=mp.prid
WHERE mo.create_at BETWEEN '$startmonth 00:00:00' AND '$endmonth 23:59:59' AND mtc.proid!='148' ORDER BY mo.create_at DESC");
											} else { */
												$stmt = $DB_con->prepare("SELECT mo.order_id as moorderid,mo.orid,mo.customer,mo.pay_option,mo.amount as moamount,mo.paid_amount,mo.due_amount,mtc.pro_discount,mtc.proid,mtc.discount_type,mo.discount,mo.all_discount,mo.discount_type,mo.status as mostatus,mo.create_at,SUM(mo.discount) as totaldiscount,SUM(mtc.pro_dis_price * mtc.pro_qty) as totalbillptotal,SUM(mp.price * mtc.pro_qty) as totalproducttotal FROM mobileorders mo INNER JOIN 
mobiletemporarycart mtc ON mo.order_id=mtc.orderid INNER JOIN
mobileproducts mp ON mtc.proid=mp.prid
WHERE mtc.proid!='148' $branchestext and mo.create_at BETWEEN '$startmonth 00:00:00' AND '$endmonth 23:59:59' ORDER BY mo.create_at DESC");
											//}

										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);

										$profit=$row['totalbillptotal']-$row['totalproducttotal'];

										//$totalrowprofitamount=$totalrowprofitamount+$profit-$row['totaldiscount'];
										
										//$totalrowmarketprofitamount=$totalrowmarketprofitamount+$marketprofit-$row['totaldiscount'];
										
										$totalprofit=$profit;
										
										//$oooid = $row['moorderid'];

										//$orders = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(mobileorders.amount) as amount FROM mobileorders WHERE mobileorders.order_id='$oooid' and mobileorders.branch_id='$branchid'"));
										//$productcost = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(mobileproducts.price * mobiletemporarycart.pro_qty) as cost
														//FROM mobiletemporarycart
														//JOIN mobileproducts ON mobiletemporarycart.proid = mobileproducts.prid WHERE mobiletemporarycart.orderid='$oooid' and mobiletemporarycart.branch_id='$branchid'"));
										//$totalprofi = $orders['amount'] - $productcost['cost'];
										//$profitnormal = number_format($totalprofi, 2);
										//var_dump($totalprofi);

										?>
										<div class="card-icon card-icon-large"><i class="fas fa-ticket-alt"></i></div>
										<div class="">
											<h5 class="card-title mb-0">Total  <?php echo $currentmonth; ?> Profit(Normal)</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													Rs.<?php echo number_format($totalprofit, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-orange" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-cyan-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-dollar-sign"></i></div>
										<div class="">
											<?php
											date_default_timezone_set("Asia/Colombo");
											$today = date("Y-m-d");

											$profit=0;
											$marketprofit=0;
											
											$totalrowprofitamount=0;
											$totalrowmarketprofitamount=0;
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$branchestext = "AND mo.branch_id='$branch'";
												}
											} else {
												$branchestext = "AND mo.branch_id='$branchid'";
											}

										/*	if ($branch_id == '1') {
												$stmt = $DB_con->prepare("SELECT mo.order_id as moorderid,mo.orid,mo.customer,mo.pay_option,mo.amount as moamount,mo.paid_amount,mo.due_amount,mtc.pro_discount,mtc.proid,mtc.discount_type,mo.discount,mo.all_discount,mo.discount_type,mo.status as mostatus,mo.create_at,SUM(mo.discount) as totaldiscount,SUM(mtc.pro_price * mtc.pro_qty) as totalbillptotal,SUM(mp.price * mtc.pro_qty) as totalproducttotal,SUM(mp.market_price * mtc.pro_qty) as totalproductmarkettotal FROM mobileorders mo INNER JOIN 
mobiletemporarycart mtc ON mo.order_id=mtc.orderid INNER JOIN
mobileproducts mp ON mtc.proid=mp.prid
WHERE DATE(mo.create_at) = '$today' AND mtc.proid!='148' ORDER BY mo.create_at DESC");
											} else { */
												$stmt = $DB_con->prepare("SELECT mo.order_id as moorderid,mo.orid,mo.customer,mo.pay_option,mo.amount as moamount,mo.paid_amount,mo.due_amount,mtc.pro_discount,mtc.proid,mtc.discount_type,mo.discount,mo.all_discount,mo.discount_type,mo.status as mostatus,mo.create_at,SUM(mo.discount) as totaldiscount,SUM(mtc.pro_dis_price * mtc.pro_qty) as totalbillptotal,SUM(mp.price * mtc.pro_qty) as totalproducttotal FROM mobileorders mo INNER JOIN 
mobiletemporarycart mtc ON mo.order_id=mtc.orderid INNER JOIN
mobileproducts mp ON mtc.proid=mp.prid
WHERE DATE(mo.create_at)='$today' AND mtc.proid!='148' $branchestext ORDER BY mo.create_at DESC");
										//	}

										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);
										
										
										$profit=$row['totalbillptotal']-$row['totalproducttotal'];

										//$totalrowprofitamount=$totalrowprofitamount+$profit-$row['totaldiscount'];
										
										//$totalrowmarketprofitamount=$totalrowmarketprofitamount+$marketprofit-$row['totaldiscount'];
										
										$totalprofit=$profit;
										
										//$oooid = $row['moorderid'];

										//$orders = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(mobileorders.amount) as amount FROM mobileorders WHERE mobileorders.order_id='$oooid' and mobileorders.branch_id='$branchid'"));
										//$productcost = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(mobileproducts.price * mobiletemporarycart.pro_qty) as cost
														//FROM mobiletemporarycart
														//JOIN mobileproducts ON mobiletemporarycart.proid = mobileproducts.prid WHERE mobiletemporarycart.orderid='$oooid' and mobiletemporarycart.branch_id='$branchid'"));
										//$totalprofi = $orders['amount'] - $productcost['cost'];
										//$profitnormal = number_format($totalprofi, 2);
										//var_dump($totalprofi);

										?>
										<h5 class="card-title mb-0">Total Daily Profit(Normal)</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format($totalprofit, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-cyan" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>
						
							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card  l-bg-orange-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-dollar-sign"></i></div>
										<div class="">
											<?php
											date_default_timezone_set("Asia/Colombo");
											$today = date("Y-m-d");
											$yesterday = date("Y-m-d", strtotime("-1 day"));

											$profit=0;
											$marketprofit=0;
											
											$totalrowprofitamount=0;
											$totalrowmarketprofitamount=0;
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$branchestext = "AND mo.branch_id='$branch'";
												}
											} else {
												$branchestext = "AND mo.branch_id='$branchid'";
											}
											/*if ($branch_id == '1') {
												$stmt = $DB_con->prepare("SELECT mo.order_id as moorderid,mo.orid,mo.customer,mo.pay_option,mo.amount as moamount,mo.paid_amount,mo.due_amount,mtc.pro_discount,mtc.proid,mtc.discount_type,mo.discount,mo.all_discount,mo.discount_type,mo.status as mostatus,mo.create_at,SUM(mo.discount) as totaldiscount,SUM(mtc.pro_price * mtc.pro_qty) as totalbillptotal,SUM(mp.price * mtc.pro_qty) as totalproducttotal,SUM(mp.market_price * mtc.pro_qty) as totalproductmarkettotal FROM mobileorders mo INNER JOIN 
mobiletemporarycart mtc ON mo.order_id=mtc.orderid INNER JOIN
mobileproducts mp ON mtc.proid=mp.prid
WHERE DATE(mo.create_at) = '$yesterday' AND mtc.proid!='148' ORDER BY mo.create_at DESC");
											} else { */
												$stmt = $DB_con->prepare("SELECT mo.order_id as moorderid,mo.orid,mo.customer,mo.pay_option,mo.amount as moamount,mo.paid_amount,mo.due_amount,mtc.pro_discount,mtc.proid,mtc.discount_type,mo.discount,mo.all_discount,mo.discount_type,mo.status as mostatus,mo.create_at,SUM(mo.discount) as totaldiscount,SUM(mtc.pro_dis_price * mtc.pro_qty) as totalbillptotal,SUM(mp.price * mtc.pro_qty) as totalproducttotal,SUM(mp.market_price * mtc.pro_qty) as totalproductmarkettotal FROM mobileorders mo INNER JOIN 
mobiletemporarycart mtc ON mo.order_id=mtc.orderid INNER JOIN
mobileproducts mp ON mtc.proid=mp.prid
WHERE DATE(mo.create_at)='$yesterday' AND mtc.proid!='148' $branchestext ORDER BY mo.create_at DESC");
										//	}

										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);
										
										$profit=$row['totalbillptotal']-$row['totalproducttotal'];
										
										//$marketprofit=$row['totalproductmarkettotal']-$row['totalproducttotal'];

										//$totalrowprofitamount=$totalrowprofitamount+$profit-$row['totaldiscount'];
										
										//$totalrowmarketprofitamount=$totalrowmarketprofitamount+$marketprofit-$row['totaldiscount'];
										
										$totalprofit=$profit;

										//$orders = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount FROM mobileorders WHERE DATE(create_at) = CURDATE() - INTERVAL 1 DAY AND branch_id='$branchid'"));
										//$productcost = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(mobileproducts.price * mobiletemporarycart.pro_qty) as cost
														//FROM mobiletemporarycart
														//JOIN mobileproducts ON mobiletemporarycart.proid = mobileproducts.prid WHERE DATE(mobiletemporarycart.added_date) = CURDATE() - INTERVAL 1 DAY AND mobiletemporarycart.branch_id='$branchid'"));
										//$totalprofi = $orders['amount'] - $productcost['cost'];

										?>
											<h5 class="card-title mb-0">Total Yesterday Profit(Normal)</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format($totalprofit, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-cyan" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>
							
							

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-purple-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-users"></i></div>
										<div class="">
											<?php
											
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$branchestext = "AND branch_id = :branch_id";
												}
											} else {
												
												$branchestext = "AND branch_id = :branch_id";
											}
											$query = 'SELECT COUNT(*) AS customers_count FROM mobilecustomers WHERE status = "1" ' . $branchestext;
											$stmt = $DB_con->prepare($query);
											
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$stmt->bindParam(':branch_id', $branch);
												}
											} else {
												
												$stmt->bindParam(':branch_id', $branchid);
											}
											//if (!empty($branchestext)) {
											//	$stmt->bindParam(':branch_id', $branch);
											//}
											$stmt->execute();
											$result = $stmt->fetch();
											$total_customers = $result['customers_count'];

											?>
											<h5 class="card-title mb-0">Customers</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													<?php echo $total_customers ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-pink" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-magenta-dark ">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-users"></i></i></div>
										<div class="">
											<?php
											
											$stmt = $DB_con->prepare('SELECT COUNT(*) AS suppliers_count FROM mobilesuppliers where status="1"');
											$stmt->execute();
											$result = $stmt->fetch();
											$total_suppliers = $result['suppliers_count'];

											?>
											<h5 class="card-title mb-0">Suppliers</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													<?php echo $total_suppliers ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-red-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
											<?php
											$stmt = $DB_con->prepare('SELECT COUNT(*) AS products_count FROM mobileproducts where status="1"');
											$stmt->execute();
											$result = $stmt->fetch();
											$total_products = $result['products_count'];

											?>
											<h5 class="card-title mb-0">Products</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													<?php echo $total_products ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-cyan-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-credit-card"></i></i></i></div>
										<div class="">
											<?php

											$stmt = $DB_con->prepare('SELECT COUNT(*) AS products_count FROM mobileproducts where status="1"');
											$stmt->execute();
											$result = $stmt->fetch();
											$total_products = $result['products_count'];

											?>
											<h5 class="card-title mb-0">Purchase Invoice</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												0
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-blue-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
											<?php
											
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$branchestext = "WHERE branch_id = :branch_id";
												}
											} else {
												
												$branchestext = "WHERE branch_id = :branch_id";
											}
											
											$query = 'SELECT sum(amount) AS amount FROM mobileexpense ' . $branchestext;
											$stmt = $DB_con->prepare($query);
											
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$stmt->bindParam(':branch_id', $branch);
												}
											} else {
												
												$stmt->bindParam(':branch_id', $branchid);
											}
											

											//$stmt = $DB_con->prepare('SELECT sum(amount) AS amount FROM mobileexpense '.$branchestext);
											//$stmt->bindParam(':branch_id', $branch_id, PDO::PARAM_INT);
											
											$stmt->execute();
											$result = $stmt->fetch();
											$amount = $result['amount'];

											?>
											<h5 class="card-title mb-0">All Expences</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format((float) $amount, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-green-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
											<?php
											date_default_timezone_set("Asia/Colombo");
											$today = date("Y-m-d");
											//$stmt = $DB_con->prepare('SELECT sum(amount) AS amount,add_date FROM mobileexpense WHERE DATE(add_date)="$today"');
											//$stmt->execute();
											///$result = $stmt->fetch();
											//$amount = $result['amount'];
											if (isset($_POST['branch'])) {
												$branch = $_POST['branch'];
												if ($branch == "all") {
													$branchestext = "";
												} else {
													$branchestext = "AND branch_id = '$branch'";
												}
											} else {
												
												$branchestext = "AND branch_id = '$branchid'";
											}
											
											$expen = "SELECT sum(amount) AS amount,add_date FROM mobileexpense WHERE DATE(add_date) = '$today' " . $branchestext;
											//$stmt = $DB_con->prepare($query);
											
											

											//$expen = " AND branch_id = '$branch_id'";
											$SDAresult = mysqli_query($conn, $expen);
											$amount = mysqli_fetch_assoc($SDAresult)['amount'];
											?>
											<h5 class="card-title mb-0">Daily Expences</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format((float) $amount, 2); ?>
												</h2>
											</div>
										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-orange-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
										<?php
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND branch_id = '$branch'";
											}
										} else {
											
											$branchestext = "AND branch_id = '$branchid'";
										}
										
											$result = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,pay_option FROM mobileorders WHERE pay_option='Cash' $branchestext"));
													

													 ?>
											<h5 class="card-title mb-0">Cash Payments Total</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													Rs.<?php echo number_format((float) $result['amount'], 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-magenta-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
										<?php
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND branch_id = '$branch'";
											}
										} else {
											
											$branchestext = "AND branch_id = '$branchid'";
										}
										$result = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,pay_option FROM mobileorders WHERE pay_option='Debit-Card' $branchestext"));
													

													 ?>
											<h5 class="card-title mb-0">Card Payments Total</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format((float) $result['amount'], 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>
							
							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-purple-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
										<?php
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND branch_id = '$branch'";
											}
										} else {
											
											$branchestext = "AND branch_id = '$branchid'";
										}
										$result = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,pay_option FROM mobileorders WHERE pay_option='banktransfer' $branchestext"));
													

													 ?>
											<h5 class="card-title mb-0">Bank Transfer Total</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format((float) $result['amount'], 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-red-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
										<?php
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND branch_id = '$branch'";
											}
										} else {
											
											$branchestext = "AND branch_id = '$branchid'";
										}
										$result = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,pay_option FROM mobileorders WHERE pay_option='cheques' $branchestext"));
													

													 ?>
											<h5 class="card-title mb-0">Cheques Total</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format((float) $result['amount'], 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>
							


							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-cyan-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
										<?php
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND branch_id = '$branch'";
											}
										} else {
											
											$branchestext = "AND branch_id = '$branchid'";
										}
											date_default_timezone_set("Asia/Colombo");
											$today = date("Y-m-d");
											$result = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,pay_option,create_at FROM mobileorders WHERE pay_option='Cash' AND DATE(create_at) = '$today' $branchestext"));
													

													 ?>
											<h5 class="card-title mb-0">Daily Cash Payments Total</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													Rs.<?php echo number_format((float) $result['amount'], 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-blue-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
										<?php
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND branch_id = '$branch'";
											}
										} else {
											
											$branchestext = "AND branch_id = '$branchid'";
										}
										$result = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,pay_option,create_at FROM mobileorders WHERE pay_option='Debit-Card' $branchestext"));
													

													 ?>
											<h5 class="card-title mb-0">Daily Card Payments Total</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format((float) $result['amount'], 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>
							
							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-cherry">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
										<?php
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND branch_id = '$branch'";
											}
										} else {
											
											$branchestext = "AND branch_id = '$branchid'";
										}
										$result = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,pay_option,create_at FROM mobileorders WHERE pay_option='banktransfer' AND DATE(create_at) = '$today' $branchestext"));
													

													 ?>
											<h5 class="card-title mb-0">Daily Bank Transfer Total</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format((float) $result['amount'], 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-green-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-money-bill"></i></div>
										<div class="">
										<?php
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND branch_id = '$branch'";
											}
										} else {
											
											$branchestext = "AND branch_id = '$branchid'";
										}
										$result = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(amount) as amount,pay_option,create_at FROM mobileorders WHERE pay_option='cheques' AND DATE(create_at) = '$today' $branchestext"));
													

													 ?>
											<h5 class="card-title mb-0">Daily Cheques Total</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php echo number_format((float) $result['amount'], 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-purple-dark">
									<div class="card-statistic-3 p-4">
										<div class="card-icon card-icon-large"><i class="fas fa-credit-card"></i></i></i></div>
										<div class="">
											<?php
												if (isset($_POST['branch'])) {
													$branch = $_POST['branch'];
													if ($branch == "all") {
														$branchestext = "";
													} else {
														$branchestext = "AND branch_id = '$branch'";
													}
												} else {
													
													$branchestext = "AND branch_id = '$branchid'";
												}
												 $tot = 0;
												 $result = mysqli_query($conn, "SELECT * FROM tbl_stock WHERE qty > '0' AND prid!='148' $branchestext");		
												 if ($result->num_rows > 0) {
													 while($row = $result->fetch_assoc()) { 
														
														 ?>
														
															 <?php 
															 $prid =  $row['prid'];
															 $qry = mysqli_query($conn, "SELECT * FROM mobileproducts WHERE prid='$prid'");		
															 $rw = mysqli_fetch_array($qry); 
															 $price = isset($rw['price']) ? $rw['price'] : 0;
															 $qty = isset($row['qty']) ? $row['qty'] : 0;
															 $tot = $tot + ( $price* $qty); ?>
		 
															
												   <?php }
												 }
											
											
											?>

											
											<h5 class="card-title mb-0">Daily Stock Valivation</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
													Rs.<?php echo $tot; ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12">
    <div class="card l-bg-red-dark">
        <div class="card-statistic-3 p-4">
            <?php
            date_default_timezone_set("Asia/Colombo");
            $today = date("Y-m-d");

            
           
           

           
			if (isset($_POST['branch'])) {
				$branch = $_POST['branch'];
				if ($branch == "all") {
					$branchestext = "";
				} else {
					$branchestext = "AND ci.branch_id=:branch_id";
				}
			} else {
				
				//$branchestext = "AND branch_id = '$branchid'";
				$branchestext = "AND ci.branch_id=:branch_id";
			}

           
            $stmt = $DB_con->prepare("SELECT * FROM cash_in_out ci WHERE DATE(ci.added_date) = :today $branchestext");
            $stmt->bindParam(':today', $today);

            if ($branchestext !== "") {
                $stmt->bindParam(':branch_id', $branch);
            }
			 
			if (isset($_POST['branch'])) {
				$branch = $_POST['branch'];
				if ($branch == "all") {
					$branchestext = "";
				} else {
					$stmt->bindParam(':branch_id', $branch);
				}
			} else {
				$stmt->bindParam(':branch_id', $branchid);
				//$branchestext = "AND branch_id = '$branchid'";
				//$branchestext = "AND ci.branch_id=:branch_id";
			}

            $stmt->execute();
			$sum5000 = 0;
			$sum1000 = 0;
			$sum500 = 0;
			$sum100 = 0;
			$sum50 = 0;
			$sum20 = 0;
			$coins = 0;
            if ($stmt->rowCount() > 0) {
                while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
					$sum5000 = ((int) ($row['m5000'] ?? 0)) * 5000;
					$sum1000 = ((int) ($row['m1000'] ?? 0)) * 1000;
					$sum500 = ((int) ($row['m500'] ?? 0)) * 500;
					$sum100 = ((int) ($row['m100'] ?? 0)) * 100;
					$sum50 = ((int) ($row['m50'] ?? 0)) * 50;
					$sum20 = ((int) ($row['m20'] ?? 0)) * 20;
					$coins = ((int) ($row['coins'] ?? 0));
                   
				}
            }
                    $sum     = $sum5000 + $sum1000 + $sum500 + $sum100 + $sum50 + $sum20 + $coins;
					$sum = $sum ?? 0; 
            ?>
                    <div class="card-icon card-icon-large">
                        <i class="fas fa-credit-card"></i>
                    </div>
                    <div>
                        <h5 class="card-title mb-0">Daily Drawer Amount</h5>
                    </div>
                    <div class="row align-items-center mb-2 d-flex">
                        <div class="col-12">
                            <h2 class="d-flex align-items-center mb-0">
                                Rs. <?php echo number_format($sum, 2); ?>
                            </h2>
                        </div>
                    </div>
                    <div class="progress mt-1" data-height="8" style="height: 8px;">
                        <div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
                    </div>
            <?php
                
            ?>
        </div>
    </div>
</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-orange-dark">
									<div class="card-statistic-3 p-4">
									<?php
										
										date_default_timezone_set("Asia/Colombo");
										$today = date("Y-m-d");
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND mi.branch_id=:branch_id";
											}
										} else {
											
											$branchestext = "AND mi.branch_id=:branch_id";
										}
										$stmt = $DB_con->prepare("SELECT sum(money) as money,added_date FROM money_in_out mi WHERE mi.in_out='1' $branchestext AND DATE(mi.added_date) = :today");
                                                                                                                                    
										$stmt->bindParam(':today', $today);
										//$stmt->bindParam(':branch_id', $branchid);
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$stmt->bindParam(':branch_id', $branch);
											}
										} else {
											$stmt->bindParam(':branch_id', $branchid);
											
											
										}
										$stmt->execute();

										if($stmt->rowCount() > 0)

										{

										while($row=$stmt->fetch(PDO::FETCH_ASSOC))

										{

										extract($row);

										?>
										<div class="card-icon card-icon-large"><i class="fas fa-credit-card"></i></i></i></div>
										<div class="">
											<h5 class="card-title mb-0">Daily Money In</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php 
												$amount = $row['money'] ?? 0;
												echo number_format($amount, 2); ?>
												</h2>
											</div>
										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>

							<div class="col-lg-4 col-sm-6 col-12 ">
								<div class="card l-bg-cyan-dark">
									<div class="card-statistic-3 p-4">
									<?php
										
										date_default_timezone_set("Asia/Colombo");
										$today = date("Y-m-d");
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$branchestext = "AND mi.branch_id=:branch_id";
											}
										} else {
											
											$branchestext = "AND mi.branch_id=:branch_id";
										}
										$stmt2 = $DB_con->prepare("SELECT sum(money) as money,added_date FROM money_in_out mi WHERE mi.in_out='0' $branchestext AND DATE(mi.added_date) = :today");
                                                                                                                                    
										$stmt2->bindParam(':today', $today);
										//$stmt2->bindParam(':branch_id', $branchid);
										if (isset($_POST['branch'])) {
											$branch = $_POST['branch'];
											if ($branch == "all") {
												$branchestext = "";
											} else {
												$stmt2->bindParam(':branch_id', $branch);
											}
										} else {
											$stmt2->bindParam(':branch_id', $branchid);
											
											
										}
										$stmt2->execute();

										if($stmt2->rowCount() > 0)

										{

										while($row2=$stmt2->fetch(PDO::FETCH_ASSOC))

										{

										extract($row2);

										?>

										<div class="card-icon card-icon-large"><i class="fas fa-credit-card"></i></i></i></div>
										<div class="">
											<h5 class="card-title mb-0">Daily Money Out</h5>
										</div>
										<div class="row align-items-center mb-2 d-flex">
											<div class="col-12">
												<h2 class="d-flex align-items-center mb-0">
												Rs.<?php 
												$amount = $row2['money'] ?? 0;
												echo number_format($amount, 2); ?>
												</h2>
											</div>

										</div>
										<div class="progress mt-1 " data-height="8" style="height: 8px;">
											<div class="progress-bar l-bg-blue" role="progressbar" data-width="25%" aria-valuenow="25" aria-valuemin="0" aria-valuemax="100" style="width: 25%;"></div>
										</div>
										<?php 
										} 
										} 
										?>
									</div>
								</div>
							</div>

					</div>
					</div>
					<div class="col-sm-12 col-md-12 col-xl-12">
							<div class="card">
								<h3>Sales Current Financial Year</h3>
								<div>
									<canvas id="myChart"></canvas>
								</div>
							</div>
						</div>
						<div class="col-sm-12 col-md-12 col-xl-12">
							<div class="card">
								<h3>Sales Current Financial Year</h3>
								<div>
									<canvas id="myChart1"></canvas>
								</div>
							</div>
						</div>
				</div>
				<?php require_once 'footerjs.php'; ?>
			</div>
		</div>
		<div class="modal fade" id="shortcutModal" tabindex="-1" aria-labelledby="shortcutModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<h5 class="modal-title" id="shortcutModalLabel">Shortcut Keys</h5>
						<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
					</div>
					<div class="modal-body">
						<table class="table">
							<thead>
								<tr>
									<th>#</th>
									<th>Operations</th>
									<th>Keyboard Shortcut</th>
								</tr>
							</thead>
							<tbody>

								<tr>
									<td>1</td>
									<td>Hold</td>
									<td>shift+d</td>
								</tr>
								<tr>
									<td>2</td>
									<td>Unhold</td>
									<td>shift+u</td>
								</tr>
								<tr>
									<td>3</td>
									<td>Cancel</td>
									<td>shift+c</td>
								</tr>
								<tr>
									<td>4</td>
									<td>Edit Discount</td>
									<td>shift+i</td>
								</tr>
								<tr>
									<td>5</td>
									<td>Add Payment</td>
									<td>shift+r</td>
								</tr>
								<tr>
									<td>6</td>
									<td>Product Quantity</td>
									<td>f2</td>
								</tr>
								<tr>
									<td>7</td>
									<td>Add New Product</td>
									<td>f4</td>
								</tr>
								<tr>
									<td>8</td>
									<td>PayBill</td>
									<td>Tab</td>
								</tr>
								<tr>
									<td>9</td>
									<td>Go To Checkout</td>
									<td>Tab</td>
								</tr>
							</tbody>
						</table>
					</div>
				</div>
			</div>
		</div>
		<script src="assets/js/chart.js"></script>
		<script>
			const labels = <?php echo json_encode($labels); ?>;
			const data = <?php echo json_encode($data); ?>;

			const ctx = document.getElementById('myChart');
			const ctx1 = document.getElementById('myChart1');

			new Chart(ctx, {
				type: 'line',
				data: {
					labels: labels,
					datasets: [{
						label: 'Sales',
						data: data,
						borderWidth: 1,
						backgroundColor: 'rgb(0, 128, 255)',
						borderColor: 'rgb(0, 51, 102)',
						hoverBackgroundColor: 'rgb(64, 191, 128)',
						hoverBorderColor: 'rgba(255, 99, 132, 1)'
					}]
				},
				options: {
					plugins: {
						legend: {
							labels: {
								font: {
									size: 15
								}
							}
						}
					},
					scales: {
						y: {
							beginAtZero: true
						}
					}
				}
			});
					

			new Chart(ctx1, {
				type: 'bar',
				data: {
					labels: labels,
					datasets: [{
						label: 'Sales',
						data: data,
						borderWidth: 1,
						backgroundColor: 'rgb(0, 128, 255)',
						borderColor: 'rgb(0, 51, 102)',
						hoverBackgroundColor: 'rgb(64, 191, 128)',
						hoverBorderColor: 'rgba(255, 99, 132, 1)'
					}]
				},
				options: {
					plugins: {
						legend: {
							labels: {
								font: {
									size: 15
								}
							}
						}
					},
					scales: {
						y: {
							beginAtZero: true
						}
					}
				}
			});
			
		</script>
	</div>
</body>

</html>