Jelajahi Sumber

Add leads and sales orders value charts to dashboard

Alan Lam 2 tahun lalu
induk
melakukan
1685b7c390
5 mengubah file dengan 137 tambahan dan 1 penghapusan
  1. 67 0
      controllers/api.php
  2. 5 1
      controllers/dashboard.php
  3. 46 0
      public/js/dashboard.js
  4. 17 0
      views/dashboard.php
  5. 2 0
      views/header.php

+ 67 - 0
controllers/api.php

@@ -4,7 +4,10 @@ namespace App;
 
 
 use App\Constraint\Constraint;
 use App\Constraint\Constraint;
 use App\Constraint\HasTextConstraint;
 use App\Constraint\HasTextConstraint;
+use App\Constraint\MoreThanOrEqualsConstraint;
 use App\Constraint\StartsWithConstraint;
 use App\Constraint\StartsWithConstraint;
+use App\Enums\DocumentTypes;
+use Carbon\CarbonImmutable;
 use Exception;
 use Exception;
 use RuntimeException;
 use RuntimeException;
 
 
@@ -19,6 +22,70 @@ header("Content-Type: application/json");
 try {
 try {
 	switch($action) {
 	switch($action) {
 
 
+		case "get-leads-chart-data":
+			$dt_min = CarbonImmutable::now("Europe/London")->startOfMonth()->subMonths(12);
+
+			$labels = [];
+			$data = [];
+
+			for($i = 0; $i <= 12; $i++) {
+				$dt = $dt_min->addMonthsNoOverflow($i);
+				$label = $dt->format("M y");
+				$labels[] = $label;
+				$data[$label] = 0;
+			}
+
+			$leads = Sale::query([
+				"dts_created" => new MoreThanOrEqualsConstraint($dt_min->utc()),
+				"dts_deleted" => null
+			]);
+
+			foreach($leads as $lead) {
+				$dt_created = $lead->getDateTimeCreated()->setTimezone($_ENV["TIMEZONE"]);
+				$label = $dt_created->format("M y");
+				$data[$label]++;
+			}
+
+			JSON::success([
+				"labels" => $labels,
+				"data" => array_values($data),
+			]);
+
+			break;
+
+		case "get-sales-orders-value-chart-data":
+			$dt_min = CarbonImmutable::now("Europe/London")->startOfMonth()->subMonths(12);
+
+			$labels = [];
+			$data = [];
+
+			for($i = 0; $i <= 12; $i++) {
+				$dt = $dt_min->addMonthsNoOverflow($i);
+				$label = $dt->format("M y");
+				$labels[] = $label;
+				$data[$label] = "0.00";
+			}
+
+			$sales_orders = Document::query([
+				"type" => DocumentTypes::SALES_ORDER,
+				"currency" => "GBP",
+				"dts_created" => new MoreThanOrEqualsConstraint($dt_min->utc()),
+				"dts_deleted" => null
+			]);
+
+			foreach($sales_orders as $sales_order) {
+				$dt_created = $sales_order->getDateTimeCreated()->setTimezone($_ENV["TIMEZONE"]);
+				$label = $dt_created->format("M y");
+				$data[$label] = bcadd($data[$label], $sales_order->getSubtotal(), 2);
+			}
+
+			JSON::success([
+				"labels" => $labels,
+				"data" => array_values($data),
+			]);
+
+			break;
+
 		case "suggest-sku":
 		case "suggest-sku":
 			$supplier_id = $request->int("supplier_id");
 			$supplier_id = $request->int("supplier_id");
 			$product_type = $request->int("product_type");
 			$product_type = $request->int("product_type");

+ 5 - 1
controllers/dashboard.php

@@ -1,4 +1,6 @@
-<?php /** @noinspection \PhpUnhandledExceptionInspection */
+<?php /** @noinspection PhpUnhandledExceptionInspection */
+
+/** @noinspection \PhpUnhandledExceptionInspection */
 
 
 namespace App;
 namespace App;
 
 
@@ -71,6 +73,8 @@ $cards[] = [
 	"addText" => __("Add Product"),
 	"addText" => __("Add Product"),
 ];
 ];
 
 
+$core->addScript("js/dashboard.js");
+
 View::include("header");
 View::include("header");
 View::include("dashboard", compact("cards"));
 View::include("dashboard", compact("cards"));
 View::include("footer");
 View::include("footer");

+ 46 - 0
public/js/dashboard.js

@@ -0,0 +1,46 @@
+class Dashboard {
+	constructor() {
+		this.initLeadsChart();
+		this.initSalesOrdersValueChart();
+	}
+
+	initLeadsChart() {
+		const ctx = document.getElementById("leads-chart");
+
+		$.post("/api/get-leads-chart-data").then(response => {
+			this.initBarChart(ctx, response.labels, response.data, "Leads");
+		});
+	}
+
+	initSalesOrdersValueChart() {
+		const ctx = document.getElementById("sales-orders-value-chart");
+
+		$.post("/api/get-sales-orders-value-chart-data").then(response => {
+			this.initBarChart(ctx, response.labels, response.data, "Sales Orders Value (GBP)");
+		});
+	}
+
+	initBarChart(ctx, labels, data, datasetLabel) {
+		new Chart(ctx, {
+			type: 'bar',
+			data: {
+				labels: labels,
+				datasets: [{
+					label: datasetLabel,
+					data: data,
+					borderWidth: 1
+				}]
+			},
+			options: {
+				maintainAspectRatio: false,
+				scales: {
+					y: {
+						beginAtZero: true
+					}
+				}
+			}
+		});
+	}
+}
+
+$(() => new Dashboard());

+ 17 - 0
views/dashboard.php

@@ -35,3 +35,20 @@
 		?>
 		?>
 	</div>
 	</div>
 </section>
 </section>
+
+<section class="mb-4">
+	<h3><?=htmlspecialchars(__("Charts"))?></h3>
+
+	<div class="row">
+		<div class="col-lg-6">
+			<div style="position: relative; min-height: 384px; height: auto; width: 100%;">
+				<canvas id="leads-chart"></canvas>
+			</div>
+		</div>
+		<div class="col-lg-6">
+			<div style="position: relative; min-height: 384px; height: auto; width: 100%;">
+				<canvas id="sales-orders-value-chart"></canvas>
+			</div>
+		</div>
+	</div>
+</section>

+ 2 - 0
views/header.php

@@ -35,6 +35,8 @@ $google_api_key = $_ENV["GOOGLE_API_KEY"] ?? null;
 
 
 	<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.6.16/dist/sweetalert2.all.min.js" integrity="sha256-b+fVEXPbYTCRvyFMiFsP3TAaUr7X3fbvPaPg5JqwsDw=" crossorigin="anonymous"></script>
 	<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11.6.16/dist/sweetalert2.all.min.js" integrity="sha256-b+fVEXPbYTCRvyFMiFsP3TAaUr7X3fbvPaPg5JqwsDw=" crossorigin="anonymous"></script>
 
 
+	<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.3/dist/chart.umd.min.js"></script>
+
 	<?php
 	<?php
 	if($google_api_key) {
 	if($google_api_key) {
 		?>
 		?>