PK

ADDRLIN : /home/anibklip/aelogifts.com/cms/
FLL :
Current File : //home/anibklip/aelogifts.com/cms/dashboard.php

<?php
$query = "SELECT * from members "; 
$query = mysqli_query($GLOBALS["conn"], $query) or die (mysqli_error($GLOBALS["conn"]));
$tot_members=mysqli_num_rows($query);

$query = "SELECT * from orders where payment_method ='cod'"; 
$query = mysqli_query($GLOBALS["conn"], $query) or die (mysqli_error($GLOBALS["conn"]));
$orders_cod=mysqli_num_rows($query);

$query = "SELECT * from orders where payment_method ='razorpay'"; 
$query = mysqli_query($GLOBALS["conn"], $query) or die (mysqli_error($GLOBALS["conn"]));
$orders_paid=mysqli_num_rows($query);

$query = "SELECT * from prd "; 
$query = mysqli_query($GLOBALS["conn"], $query) or die (mysqli_error($GLOBALS["conn"]));
$tot_products=mysqli_num_rows($query);

// Get monthly sales data for the last 12 months
$monthly_sales_query = "SELECT 
    DATE_FORMAT(dt, '%Y-%m') as month,
    COUNT(*) as total_orders,
    SUM(total) as total_sales,
    SUM(CASE WHEN payment_method = 'cod' THEN total ELSE 0 END) as cod_sales,
    SUM(CASE WHEN payment_method = 'razorpay' THEN total ELSE 0 END) as online_sales
FROM orders 
WHERE dt >= DATE_SUB(CURDATE(), INTERVAL 12 MONTH)
GROUP BY DATE_FORMAT(dt, '%Y-%m')
ORDER BY month ASC";

$monthly_sales_result = mysqli_query($GLOBALS["conn"], $monthly_sales_query) or die(mysqli_error($GLOBALS["conn"]));
$months = [];
$sales_data = [];
$cod_data = [];
$online_data = [];

while($row = mysqli_fetch_array($monthly_sales_result)) {
    $months[] = date('M Y', strtotime($row['month'].'-01'));
    $sales_data[] = round($row['total_sales'], 2);
    $cod_data[] = round($row['cod_sales'], 2);
    $online_data[] = round($row['online_sales'], 2);
}

?>
<div class="content-wrapper">
	<!-- Content Header (Page header) -->
	<div class="content-header">
		<div class="container-fluid">
			<div class="row mb-2">
				<div class="col-sm-6">
					<h1 class="m-0">Dashboard</h1>
				</div><!-- /.col -->
				<div class="col-sm-6">
					<ol class="breadcrumb float-sm-right">
						<li class="breadcrumb-item"><a href="#">Home</a></li>
						<li class="breadcrumb-item active">Dashboard v1</li>
					</ol>
				</div><!-- /.col -->
			</div><!-- /.row -->
		</div><!-- /.container-fluid -->
	</div>
	<!-- /.content-header -->

	<!-- Main content -->
	<section class="content">
		<div class="container-fluid">
			<!-- Small boxes (Stat box) -->
			<div class="row d-none1">
				<div class="col-lg-3 col-6">
					<!-- small box -->
					<div class="small-box bg-info">
						<div class="inner">
							<h3><?php echo $orders_cod;?></h3>

							<p>COD Orders</p>
						</div>
						<div class="icon">
							<i class="ion ion-bag"></i>
						</div>
						<a href="main.php?action=orders_view&payment_method=cod" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
					</div>
				</div>
				<!-- ./col -->
				<div class="col-lg-3 col-6">
					<!-- small box -->
					<div class="small-box bg-success">
						<div class="inner">
							<h3>
								<?php echo $orders_paid;?>
								<!-- <sup style="font-size: 20px">%</sup> -->
							</h3>

							<p>Prepaid Orders</p>
						</div>
						<div class="icon">
							<i class="ion ion-stats-bars"></i>
						</div>
						<a href="main.php?action=orders_view&payment_method=razorpay" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
					</div>
				</div>
				<!-- ./col -->
				<div class="col-lg-3 col-6">
					<!-- small box -->
					<div class="small-box bg-warning">
						<div class="inner">
							<h3><?php echo $tot_members;?></h3>

							<p>User Registrations</p>
						</div>
						<div class="icon">
							<i class="ion ion-person-add"></i>
						</div>
						<a href="main.php?action=users_view" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
					</div>
				</div>
				<!-- ./col -->
				<div class="col-lg-3 col-6">
					<!-- small box -->
					<div class="small-box bg-danger">
						<div class="inner">
							<h3><?php echo $tot_products;?></h3>

							<p>Total Products</p>
						</div>
						<div class="icon">
							<i class="ion ion-pie-graph"></i>
						</div>
						<a href="main.php?action=product_view" class="small-box-footer">More info <i class="fas fa-arrow-circle-right"></i></a>
					</div>
				</div>
				<!-- ./col -->
			</div>
			<!-- /.row -->

			<!-- Main row -->
			<div class="row d-none1">
				<!-- right col (We are only adding the ID to make the widgets sortable)-->
				<section class="col-lg-12">
					<!-- solid sales graph -->
					<div class="card bg-gradient-info">
						<div class="card-header border-0">
							<h3 class="card-title"> <i class="fas fa-chart-line mr-1"></i> Sales Analytics </h3>

							<div class="card-tools">
								<select class="custom-select custom-select-sm" id="salesPeriod" style="border: none;">
									<option value="monthly">Monthly View</option>
									<option value="quarterly">Quarterly View</option>
									<option value="yearly">Yearly View</option>
								</select>
							</div>
						</div>
						<div class="card-body">
							<canvas class="chart" id="line-chart" style="min-height: 250px; height: 250px; max-height: 250px; max-width: 100%;"></canvas>
						</div>
						<div class="card-footer bg-transparent">
							<div class="row">
								<div class="col-4 text-center">
									<p class="text-white mb-0">
										<span class="mr-2"><i class="fas fa-circle text-success"></i> Online</span>
										<span><?php echo "₹" . number_format(array_sum($online_data), 2); ?></span>
									</p>
								</div>
								<div class="col-4 text-center">
									<p class="text-white mb-0">
										<span class="mr-2"><i class="fas fa-circle text-warning"></i> COD</span>
										<span><?php echo "₹" . number_format(array_sum($cod_data), 2); ?></span>
									</p>
								</div>
								<div class="col-4 text-center">
									<p class="text-white mb-0">
										<span class="mr-2"><i class="fas fa-circle text-light"></i> Total</span>
										<span><?php echo "₹" . number_format(array_sum($sales_data), 2); ?></span>
									</p>
								</div>
							</div>
						</div>
					</div>
					<!-- /.card -->
				</section>
				<!-- right col -->
			</div>
			<!-- /.row (main row) -->
		</div><!-- /.container-fluid -->
	</section>
	<!-- /.content -->
</div>

<script>
$(function () {
  'use strict'

  var $salesChart = $('#line-chart');
  var salesChart;
  var chartData = {
    labels: <?php echo json_encode($months); ?>,
    originalData: {
      total: <?php echo json_encode($sales_data); ?>,
      cod: <?php echo json_encode($cod_data); ?>,
      online: <?php echo json_encode($online_data); ?>
    }
  };

  function initChart(period) {
    var labels = chartData.labels.slice();
    var totalData = chartData.originalData.total.slice();
    var codData = chartData.originalData.cod.slice();
    var onlineData = chartData.originalData.online.slice();
    
    if (period === 'quarterly') {
      var quarterlyData = {labels: [], total: [], cod: [], online: []};
      for (var i = 0; i < labels.length; i += 3) {
        var quarter = Math.floor(i/3) + 1;
        var year = labels[i].split(' ')[1];
        quarterlyData.labels.push('Q' + quarter + ' ' + year);
        quarterlyData.total.push(totalData.slice(i, i + 3).reduce((a, b) => a + b, 0));
        quarterlyData.cod.push(codData.slice(i, i + 3).reduce((a, b) => a + b, 0));
        quarterlyData.online.push(onlineData.slice(i, i + 3).reduce((a, b) => a + b, 0));
      }
      labels = quarterlyData.labels;
      totalData = quarterlyData.total;
      codData = quarterlyData.cod;
      onlineData = quarterlyData.online;
    } else if (period === 'yearly') {
      var yearlyData = {labels: [], total: [], cod: [], online: []};
      var currentYear = '';
      var yearIndex = -1;
      labels.forEach((label, i) => {
        var year = label.split(' ')[1];
        if (year !== currentYear) {
          currentYear = year;
          yearIndex++;
          yearlyData.labels.push(year);
          yearlyData.total[yearIndex] = totalData[i];
          yearlyData.cod[yearIndex] = codData[i];
          yearlyData.online[yearIndex] = onlineData[i];
        } else {
          yearlyData.total[yearIndex] += totalData[i];
          yearlyData.cod[yearIndex] += codData[i];
          yearlyData.online[yearIndex] += onlineData[i];
        }
      });
      labels = yearlyData.labels;
      totalData = yearlyData.total;
      codData = yearlyData.cod;
      onlineData = yearlyData.online;
    }

    if (salesChart) {
      salesChart.destroy();
    }

    var ctx = $salesChart[0].getContext('2d');
    salesChart = new Chart(ctx, {
      type: 'line',
      data: {
        labels: labels,
        datasets: [
          {
            label: 'Total Sales',
            data: totalData,
            fill: false,
            borderColor: '#efefef',
            pointBackgroundColor: '#efefef',
            pointBorderColor: '#efefef',
            pointRadius: 4,
            pointHoverRadius: 6,
            borderWidth: 2
          },
          {
            label: 'COD Sales',
            data: codData,
            fill: false,
            borderColor: '#ffc107',
            pointBackgroundColor: '#ffc107',
            pointBorderColor: '#ffc107',
            pointRadius: 4,
            pointHoverRadius: 6,
            borderWidth: 2
          },
          {
            label: 'Online Sales',
            data: onlineData,
            fill: false,
            borderColor: '#28a745',
            pointBackgroundColor: '#28a745',
            pointBorderColor: '#28a745',
            pointRadius: 4,
            pointHoverRadius: 6,
            borderWidth: 2
          }
        ]
      },
      options: {
        maintainAspectRatio: false,
        responsive: true,
        legend: {
          display: true,
          position: 'bottom',
          labels: {
            fontColor: '#efefef',
            boxWidth: 20
          }
        },
        scales: {
          yAxes: [{
            gridLines: {
              display: true,
              color: 'rgba(255, 255, 255, 0.1)',
              zeroLineColor: 'rgba(255, 255, 255, 0.25)'
            },
            ticks: {
              fontColor: '#efefef',
              callback: function(value) {
                return '₹' + value.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
              }
            }
          }],
          xAxes: [{
            gridLines: {
              display: false
            },
            ticks: {
              fontColor: '#efefef'
            }
          }]
        },
        tooltips: {
          callbacks: {
            label: function(tooltipItem, data) {
              var label = data.datasets[tooltipItem.datasetIndex].label || '';
              return label + ': ₹' + tooltipItem.yLabel.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
            }
          }
        }
      }
    });
  }

  $('#salesPeriod').on('change', function() {
    initChart($(this).val());
  });

  // Initialize with monthly view
  initChart('monthly');
});
</script>



PK 99