| Server IP : 13.126.101.145 / Your IP : 216.73.216.159 Web Server : Apache/2.4.52 (Ubuntu) System : Linux ip-11-115-0-196 6.8.0-1039-aws #41~22.04.1-Ubuntu SMP Thu Sep 11 10:54:48 UTC 2025 x86_64 User : www-data ( 33) PHP Version : 8.3.17 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : OFF | Sudo : ON | Pkexec : ON Directory : /var/www/html/rentals_updated/wp-content/plugins/custom-dashboard/ |
Upload File : |
<link rel="stylesheet" href="<?php echo plugin_dir_url( __FILE__ ); ?>/custom_style.css" />
<link rel="stylesheet" href="https://cdn.datatables.net/1.13.6/css/jquery.dataTables.min.css">
<script src="https://cdn.tailwindcss.com"></script>
<!-- Chart.js & DataLabels plugin -->
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script src="https://cdn.jsdelivr.net/npm/chartjs-plugin-datalabels"></script>
<!-- DataTables Scripts -->
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script src="https://cdn.datatables.net/1.13.6/js/jquery.dataTables.min.js"></script>
<style>
/* Remove default DataTable spacing */
.dataTables_wrapper {
padding: 0;
margin-top: 10px;
}
/* Search box styling */
.dataTables_wrapper .dataTables_filter {
float: right;
margin-bottom: 15px;
}
.dataTables_wrapper .dataTables_filter input {
border: 1px solid #ddd;
border-radius: 6px;
padding: 6px 10px;
margin-left: 5px;
}
/* Length dropdown */
.dataTables_wrapper .dataTables_length {
float: left;
margin-bottom: 15px;
}
.dataTables_wrapper .dataTables_length select {
border: 1px solid #ddd;
border-radius: 6px;
padding: 4px 6px;
}
/* Table look */
#sales_order_table {
border-collapse: collapse !important;
width: 100%;
}
#sales_order_table th, #sales_order_table td {
padding: 10px 12px;
border: 1px solid #eee;
text-align: left;
}
#sales_order_table thead th {
background: #f9f9f9;
font-weight: 600;
}
/* Pagination styling */
.dataTables_wrapper .dataTables_paginate {
margin-top: 15px;
}
.dataTables_wrapper .dataTables_paginate .paginate_button {
padding: 6px 12px;
border-radius: 6px;
border: 1px solid #ddd;
margin: 0 3px;
cursor: pointer;
}
.dataTables_wrapper .dataTables_paginate .paginate_button.current {
background: #0073aa;
color: #fff !important;
border-color: #0073aa;
}
</style>
<?php
global $wpdb;
$customer_count = "
SELECT COUNT(DISTINCT u.ID)
FROM {$wpdb->users} u
INNER JOIN {$wpdb->usermeta} um ON u.ID = um.user_id
INNER JOIN {$wpdb->postmeta} pm ON u.ID = pm.meta_value
INNER JOIN {$wpdb->posts} p ON pm.post_id = p.ID
WHERE um.meta_key = '{$wpdb->prefix}capabilities'
AND um.meta_value LIKE '%customer%'
AND pm.meta_key = '_customer_user'
AND p.post_type = 'shop_order'
";
$active_customer_count = $wpdb->get_var($customer_count);
?>
<div class="bg-[#f1f1f1] grid w-full">
<div class="bg-[#f1f1f1] relative">
<div class="w-full bg-[#f1f1f1] flex">
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6 w-full px-4">
<!-- Card Active Customers -->
<a href="<?php echo get_site_url() ?>/wp-admin/users.php?page=active-users" target="_blank"
class="bg-white rounded-lg p-6 flex flex-col">
<div class="flex items-start gap-4 flex-col">
<img class="w-12 h-12" src="/wp-content/uploads/2025/09/tu-fu-icon.png"
alt="icon" />
<div class="flex items-end justify-between w-full">
<div class="">
<div class="text-2xl font-bold text-[#212b36]"><?php echo $active_customer_count; ?>
</div>
<div class="text-sm text-[#637381] mt-1 font-medium">Active Customers</div>
</div>
<div class="flex items-center gap-1 text-sm text-[#45cb85] font-medium">
<span>3</span>
<img class="w-3 h-3" src="/wp-content/uploads/2025/09/up-arrow.png"
alt="up" />
</div>
</div>
</div>
</a>
<!-- Card Enquiry Raised -->
<a href="<?php echo get_site_url() ?>/wp-admin/admin.php?page=leads" target="_blank"
class="bg-white rounded-lg p-6 flex flex-col">
<div class="flex items-start gap-4 flex-col">
<img class="w-12 h-12" src="/wp-content/uploads/2025/09/enquiry-raised-icon.png"
alt="icon" />
<div class="flex items-end justify-between w-full">
<?php
global $wpdb;
$user = wp_get_current_user();
$user_roles = $user->roles;
// Get the user's region location from user meta
$user_region = get_user_meta($user->ID, 'ba_region_location', true);
// Ensure that user_region is set and not empty
if ($user_region && (in_array('rue_manager', $user_roles) )) {
$table_name = $wpdb->prefix . 'leads';
// Count leads where the region matches the user's region
$count_leads = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE region = %s",
$user_region
) );
} else {
$table_name = $wpdb->prefix . 'leads'; // Ensure the table name is defined
// If the user does not have the required roles or region is not set, count all leads
$count_leads = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
}
?>
<div class="">
<div class="text-2xl font-bold text-[#212b36]"><?php echo $count_leads; ?></div>
<div class="text-sm text-[#637381] mt-1 font-medium">Enquiry Raised</div>
</div>
<div class="flex items-center gap-1 text-sm text-[#45cb85] font-medium">
<span>4</span>
<img class="w-3 h-3" src="/wp-content/uploads/2025/09/up-arrow.png"
alt="up" />
</div>
</div>
</div>
</a>
<!-- Card RFQ -->
<a href="<?php echo get_site_url() ?>/wp-admin/admin.php?page=rfq-dashboard" target="_blank"
class="bg-white rounded-lg p-6 flex flex-col">
<div class="flex items-start gap-4 flex-col">
<img class="w-12 h-12" src="/wp-content/uploads/2025/09/rfq-icon.png"
alt="icon" />
<div class="flex items-end justify-between w-full">
<?php
global $wpdb;
$user = wp_get_current_user();
$user_roles = $user->roles;
// Get the user's region location from user meta
$user_region = get_user_meta($user->ID, 'ba_region_location', true);
// Ensure that user_region is set and not empty
if ($user_region && (in_array('rue_manager', $user_roles) )) {
$table_name = $wpdb->prefix . 'request_quote';
// Count RFQs where the region matches the user's region
$count_rfq = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE region = %s",
$user_region
) );
} else {
$table_name = $wpdb->prefix . 'request_quote'; // Ensure the table name is defined
// If the user does not have the required roles or region is not set, count all RFQs
$count_rfq = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
}
?>
<div class="">
<div class="text-2xl font-bold text-[#212b36]"><?php echo $count_rfq; ?></div>
<div class="text-sm text-[#637381] mt-1 font-medium">RFQ</div>
</div>
<div class="flex items-center gap-1 text-sm text-[#45cb85] font-medium">
<span>2</span>
<img class="w-3 h-3" src="/wp-content/uploads/2025/09/up-arrow.png"
alt="up" />
</div>
</div>
</div>
</a>
<!-- Card Standard Quote -->
<a href="<?php echo get_site_url() ?>/wp-admin/admin.php?page=stdratecard-dashboard" target="_blank"
class="bg-white rounded-lg p-6 flex flex-col">
<div class="flex items-start gap-4 flex-col">
<img class="w-12 h-12" src="/wp-content/uploads/2025/09/enquiry-raised-icon.png"
alt="icon" />
<div class="flex items-end justify-between w-full">
<?php
global $wpdb;
$user = wp_get_current_user();
$user_roles = $user->roles;
$table_name = $wpdb->prefix . 'download_std_ratecard';
// Get the user's region location from user meta
$user_region = get_user_meta($user->ID, 'ba_region_location', true);
// Ensure that user_region is set and not empty
if ($user_region && (in_array('rue_manager', $user_roles) )) {
// Count download_std_ratecard where the region matches the user's region
$count_src = $wpdb->get_var( $wpdb->prepare(
"SELECT COUNT(*) FROM $table_name WHERE region = %s",
$user_region
) );
} else {
// If the user does not have the required roles or region is not set, count all leads
$count_src = $wpdb->get_var( "SELECT COUNT(*) FROM $table_name" );
}
?>
<div class="">
<div class="text-2xl font-bold text-[#212b36]"><?php echo $count_src; ?></div>
<div class="text-sm text-[#637381] mt-1 font-medium">Standard Quote</div>
</div>
<div class="flex items-center gap-1 text-sm text-[#45cb85] font-medium">
<span>1</span>
<img class="w-3 h-3" src="/wp-content/uploads/2025/09/up-arrow.png"
alt="up" />
</div>
</div>
</div>
</a>
<!-- TU FU Section -->
<div class="bg-white rounded-lg p-6 flex flex-col" style="display: none">
<div class="flex items-start gap-4 flex-col">
<img class="w-12 h-12" src="/wp-content/uploads/2025/09/tu-fu-icon.png"
alt="icon" />
<div class="flex items-end justify-between w-full">
<?php
global $wpdb;
$table_assets = $wpdb->prefix . 'asset_equipment';
$table_revenue = $wpdb->prefix . 'archive_sales_data';
// ---------------- TU Count ----------------
// (Just counting assets — same as your recordsTotal style)
$count_tu = $wpdb->get_var("
SELECT COUNT(*) AS total_tu_entries
FROM (
SELECT
e.equipment_number
FROM $table_assets e
LEFT JOIN wp_contracts rc
ON e.equipment_number = rc.equipment_number
GROUP BY e.equipment_number
) AS grouped;
");
// ---------------- FU Count ----------------
// (Counting assets that have valid FU calculation)
$count_fu = $wpdb->get_var("
SELECT COUNT(*)
FROM (
SELECT a.asset_id
FROM $table_assets a
LEFT JOIN $table_revenue b ON a.asset_id = b.asset_id
GROUP BY a.asset_id, a.acq_cost, a.acq_date, a.equipment_number
) AS grouped;
");
?>
<div class="w-full">
<a href="<?php echo get_site_url() ?>/wp-admin/admin.php?page=rfm-time-utilization" target="_blank" class="flex items-center = justify-between w-full">
<div class="text-sm text-[#637381] mt-1 font-medium">TU - <span class="text-2xl font-bold text-[#212b36]"><?php echo $count_tu; ?></span></div>
<img src="/wp-content/uploads/2025/09/right-arrow.png" alt="right-arrow" />
</a>
<a href="<?php echo get_site_url() ?>/wp-admin/admin.php?page=rfm-financial-utilization" target="_blank" class="flex items-center justify-between w-full">
<div class="text-sm text-[#637381] mt-1 font-medium">FU - <span class="text-2xl font-bold text-[#212b36]"><?php echo $count_fu; ?></span></div>
<img src="/wp-content/uploads/2025/09/right-arrow.png" alt="right-arrow" />
</a>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="w-full bg-[#f1f1f1] flex pt-7">
<div class="grid grid-cols-1 lg:grid-cols-[40%_28%_28%] gap-6 w-full px-4">
<!-- Brand Fleet Report -->
<?php
// Get all terms from "pa_brand" attribute
$brands = get_terms([
'taxonomy' => 'pa_brand',
'hide_empty' => true,
]);
$brand_counts = [];
$total_products = 0;
foreach ($brands as $brand) {
$count = $brand->count; // Number of products in this brand
$brand_counts[$brand->name] = $count;
$total_products += $count;
}
// Prepare data
$brand_data = [];
foreach ($brand_counts as $name => $count) {
$brand_data[] = [
'label' => $name,
'count' => $count,
'percent' => $total_products > 0 ? round(($count / $total_products) * 100, 2) : 0,
];
}
// Sort descending by percent
usort($brand_data, function($a, $b) {
return $b['percent'] <=> $a['percent'];
});
?>
<div class="bg-white rounded-xl p-6 flex flex-col">
<!-- Header -->
<div class="flex items-center justify-between mb-6">
<h3 class="font-semibold text-black text-xl">Brand Fleet Report</h3>
<a href="<?php echo get_site_url() ?>/wp-admin/admin.php?page=operator-details" target="_blank" class="flex items-center gap-1 text-sm font-semibold text-[#212b36]">
View all
<img class="w-5 h-5"
src="https://c.animaapp.com/meqqgiwcw2b4OM/img/keyboard-backspace.svg" />
</a>
</div>
<div class="flex flex-col md:flex-row items-center justify-between gap-8">
<!-- Brand List -->
<div class="space-y-4 w-full md:w-1/2">
<?php
$colors = ["#4879f5", "#44d2f1", "#20c745", "#f4a74b", "#f45252", "#6f42c1", "#c14268", "#0818ff"];
?>
<?php foreach ($brand_data as $i => $brand): ?>
<div class="flex items-center justify-between">
<div class="flex items-center gap-2">
<div class="w-3 h-3 rounded-full" style="background-color: <?php echo $colors[$i % count($colors)]; ?>"></div>
<span class="text-sm font-medium text-[#212b36]">
<?php echo esc_html($brand['label']); ?>
</span>
</div>
<span class="text-sm font-medium text-[#637381]">
<?php echo $brand['count']; ?> (<?php echo $brand['percent']; ?>%)
</span>
</div>
<?php endforeach; ?>
</div>
<!-- Chart -->
<div class="flex justify-center items-center w-full md:w-1/2 relative">
<canvas id="brandFleetChart"></canvas>
<div class="absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2 text-sm font-bold text-[#637381]">
<?php echo $total_products; ?>
</div>
</div>
</div>
</div>
<script>
const brandData = <?php echo json_encode($brand_data); ?>;
const colors = ["#4879f5", "#44d2f1", "#20c745", "#f4a74b", "#f45252", "#6f42c1", "#c14268", "#0818ff"];
const datasets = brandData.map((brand, i) => {
const outer = 80; // outermost ring
const thickness = 10;
return {
label: brand.label,
data: [brand.percent, 100 - brand.percent],
backgroundColor: [colors[i % colors.length], "#f0f0f0"],
borderWidth: 0,
circumference: 360,
rotation: 0, // start from right
cutout: `${outer - (i+1) * thickness}%`,
radius: `${outer - i * thickness}%`
};
});
new Chart(document.getElementById("brandFleetChart"), {
type: "doughnut",
data: { datasets },
options: {
responsive: true,
plugins: {
layout: {
padding: 0
},
datalabels: {
color: "#fff",
font: { weight: "bold", size: 12 },
formatter: (value) => {
return value;
}
},
tooltip: {
callbacks: {
label: function(context) {
const brand = brandData[context.datasetIndex];
return `${brand.label}: ${brand.count} (${brand.percent}%)`;
}
}
},
legend: { display: false },
title: { display: false }
},
}
});
</script>
<!-- Fleet Status -->
<?php
global $wpdb;
// Recursive query to fetch all child categories under "Machine Rental"
$category_tree_query = "
WITH RECURSIVE category_tree AS (
SELECT term_id, parent
FROM wp_term_taxonomy
WHERE taxonomy = 'product_cat'
AND term_id = (SELECT term_id FROM wp_terms WHERE name = 'Machine Rental')
UNION ALL
SELECT tt.term_id, tt.parent
FROM wp_term_taxonomy tt
INNER JOIN category_tree ct ON tt.parent = ct.term_id
)
SELECT term_id FROM category_tree
";
$categories = $wpdb->get_col($category_tree_query);
// ================== FETCH DISTINCT STATUSES ==================
$statuses = $wpdb->get_col("
SELECT DISTINCT pm.meta_value
FROM wp_posts p
JOIN wp_term_relationships tr ON p.ID = tr.object_id
JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_postmeta pm ON p.ID = pm.post_id
WHERE p.post_type = 'product'
AND p.post_status != 'trash'
AND tt.term_id IN (" . implode(",", $categories) . ")
AND pm.meta_key = 'status'
");
// ================== COUNT PRODUCTS PER STATUS ==================
$status_counts = [];
$total_count = 0;
foreach ($statuses as $status) {
$count = (int)$wpdb->get_var($wpdb->prepare("
SELECT COUNT(DISTINCT p.ID)
FROM wp_posts p
JOIN wp_term_relationships tr ON p.ID = tr.object_id
JOIN wp_term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id
JOIN wp_postmeta pm1 ON p.ID = pm1.post_id
JOIN wp_postmeta pm2 ON p.ID = pm2.post_id
WHERE p.post_type = 'product'
AND p.post_status != 'trash'
AND tt.term_id IN (" . implode(",", $categories) . ")
AND pm1.meta_key = 'purpose_of_listing'
AND pm1.meta_value = 'renting'
AND pm2.meta_key = 'status'
AND pm2.meta_value = %s
", $status));
$status_counts[$status] = $count;
$total_count += $count;
}
// ================== CALCULATE PERCENTAGES ==================
$status_percentages = [];
foreach ($status_counts as $status => $count) {
$status_percentages[$status] = $total_count ? round(($count / $total_count) * 100, 2) : 0;
}
?>
<div class="bg-white rounded-xl p-6 flex flex-col">
<!-- Header -->
<div class="flex items-center justify-between mb-6">
<h3 class="font-semibold text-black text-xl">Fleet Status</h3>
<a href="<?php echo get_site_url() ?>/wp-admin/admin.php?page=operator-details" target="_blank"
class="flex items-center gap-1 text-sm font-semibold text-[#212b36]">
View all
<img class="w-5 h-5"
src="https://c.animaapp.com/meqqgiwcw2b4OM/img/keyboard-backspace.svg" />
</a>
</div>
<!-- Pie Chart -->
<div class="flex justify-center items-center">
<canvas id="fleetChart" width="200" height="200"></canvas>
</div>
<!-- Legend -->
<?php
arsort($status_percentages);
$sorted_status_counts = [];
foreach ($status_percentages as $status => $percentage) {
$sorted_status_counts[$status] = $status_counts[$status];
}
?>
<div class="flex flex-wrap gap-x-1 gap-y-3 justify-center mt-6">
<?php
$colors = ["#44d2f1", "#fb7800", "#ff3131", "#28a745", "#6f42c1","#c14268", "#0818ff", "#00ff1e", "#eeff07"];
$i = 0;
foreach ($sorted_status_counts as $status => $count): ?>
<div class="flex items-center gap-1">
<div class="w-3 h-3 rounded-full" style="background-color: <?php echo $colors[$i % count($colors)]; ?>;"></div>
<span class="text-xs font-medium text-[#212b36]"><?php echo ucfirst($status); ?>:</span>
<span class="text-xs font-medium text-[#637381]">
<?php echo $count; ?> (<?php echo $status_percentages[$status]; ?>%)
</span>
</div>
<?php $i++; endforeach; ?>
</div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function () {
const ctx = document.getElementById("fleetChart").getContext("2d");
const labels = <?php echo json_encode(array_keys($status_counts)); ?>;
const data = <?php echo json_encode(array_values($status_counts)); ?>;
const colors = ["#44d2f1", "#fb7800", "#ff3131", "#28a745", "#6f42c1","#c14268", "#0818ff", "#00ff1e", "#eeff07"];
new Chart(ctx, {
type: "pie",
data: {
labels: labels,
datasets: [{
data: data,
backgroundColor: colors.slice(0, labels.length),
borderWidth: 1,
borderColor: "#fff"
}]
},
options: {
responsive: false,
plugins: {
legend: { display: false }, // hide default legend
datalabels: {
color: "#fff",
font: { weight: "bold", size: 12 },
formatter: (value) => {
return value; // ✅ show only number (count), not name
}
},
tooltip: {
callbacks: {
label: function(context) {
let total = context.dataset.data.reduce((a, b) => a + b, 0);
let value = context.raw;
let percent = total ? ((value / total) * 100).toFixed(1) : 0;
return `${context.label}: ${value} (${percent}%)`;
}
}
}
}
},
plugins: [ChartDataLabels]
});
});
</script>
<!-- Contracts -->
<?php
// Fetch WooCommerce orders and count contracts
$today = strtotime(date("Y-m-d"));
$active = 0;
$expiring = 0;
$args = array(
'limit' => -1,
'status' => array_diff(wc_get_order_statuses(), array('wc-pending', 'wc-failed')),
);
$orders = wc_get_orders($args);
foreach ($orders as $order) {
$end_date = get_post_meta($order->get_id(), 'end_date', true);
if ($end_date) {
$end_ts = strtotime($end_date);
if ($end_ts >= $today) {
$days_left = ($end_ts - $today) / (60 * 60 * 24);
if ($days_left <= 3) {
$expiring++;
} else {
$active++;
}
}
}
}
$total = $active + $expiring;
?>
<div class="bg-white rounded-xl p-6 flex flex-col">
<!-- Header -->
<div class="flex items-center justify-between mb-6">
<h3 class="font-semibold text-black text-xl">Contracts</h3>
<a href="<?php echo get_site_url() ?>/wp-admin/admin.php?page=quotation-dashboard" target="_blank" class="flex items-center gap-1 text-sm font-semibold text-[#212b36]">
View all
<img class="w-5 h-5" src="https://c.animaapp.com/meqqgiwcw2b4OM/img/keyboard-backspace.svg" />
</a>
</div>
<!-- Donut Chart -->
<div class="flex justify-center">
<div class="w-[180px] h-[180px]">
<canvas id="contractsChart"></canvas>
</div>
</div>
<!-- Legend -->
<div class="flex justify-center gap-6 mt-6" id="contractsLegend"></div>
</div>
<script>
document.addEventListener("DOMContentLoaded", function() {
const ctx = document.getElementById('contractsChart');
if (!ctx) return; // prevent breaking other charts if canvas is missing
const data = {
labels: ['Active', 'Expiring'],
datasets: [{
data: [<?= $active ?>, <?= $expiring ?>],
backgroundColor: ['#20c745', '#ffbd2b'],
borderWidth: 0
}]
};
// Plugin only for THIS chart
const centerTextPlugin = {
id: 'centerTextContracts',
beforeDraw(chart) {
if (chart.canvas.id !== 'contractsChart') return; // only run for this chart
const {width, height, ctx} = chart;
ctx.save();
ctx.font = "bold 16px sans-serif";
ctx.fillStyle = "#000";
ctx.textBaseline = "middle";
const text = "Total <?= $total ?>";
const textX = Math.round((width - ctx.measureText(text).width) / 2);
const textY = height / 2;
ctx.fillText(text, textX, textY);
ctx.restore();
}
};
new Chart(ctx, {
type: 'doughnut',
data: data,
options: {
responsive: true,
cutout: '70%',
plugins: {
layout: {
padding: 0
},
legend: { display: false },
tooltip: { enabled: true }
}
},
plugins: [centerTextPlugin]
});
// Custom legend
const legendContainer = document.getElementById("contractsLegend");
if (legendContainer) {
const counts = [<?= $active ?>, <?= $expiring ?>];
const percentages = [
<?= $total ? round(($active/$total)*100) : 0 ?>,
<?= $total ? round(($expiring/$total)*100) : 0 ?>
];
legendContainer.innerHTML = `
<div class="flex items-center gap-2">
<div class="w-3 h-3 rounded-full bg-[#20c745]"></div>
<span class="text-xs font-medium text-[#212b36]">Active</span>
<span class="text-sm font-medium text-[#637381]">${counts[0]} (${percentages[0]}%)</span>
</div>
<div class="flex items-center gap-2">
<div class="w-3 h-3 rounded-full bg-[#ffbd2b]"></div>
<span class="text-xs font-medium text-[#212b36]">Expiring</span>
<span class="text-sm font-medium text-[#637381]">${counts[1]} (${percentages[1]}%)</span>
</div>
`;
}
});
</script>
</div>
</div>
<div class="w-full bg-[#f1f1f1] pt-7 px-4">
<div class="bg-white rounded-lg p-6 flex flex-col">
<!-- Header -->
<div class="flex items-center justify-between">
<h2 class="text-xl font-semibold text-gray-900">Sale of Equipments</h2>
</div>
<?php
global $wpdb;
// Get latest 20 completed orders
// $orders = $wpdb->get_results("
// SELECT p.ID as order_id, p.post_date as order_date
// FROM {$wpdb->prefix}posts p
// WHERE p.post_type = 'shop_order'
// AND p.post_status IN ('wc-completed', 'wc-processing')
// ORDER BY p.post_date DESC
// ");
$orders = $wpdb->get_results("
SELECT p.ID as order_id, p.post_date as order_date
FROM {$wpdb->prefix}posts p
WHERE p.post_type = 'shop_order'
AND p.post_status NOT IN ('wc-pending', 'wc-failed')
ORDER BY p.post_date DESC
");
?>
<div class="mt-5">
<table id="sales_order_table" class="min-w-full text-sm">
<thead class="bg-gray-100 text-gray-800 uppercase text-[13px] font-bold">
<tr>
<th class="px-6 py-3 text-left">Id</th>
<th class="px-6 py-3 text-left">Customers</th>
<th class="px-6 py-3 text-left">Brand Name</th>
<th class="px-6 py-3 text-left">Equipment</th>
<th class="px-6 py-3 text-left">Price</th>
<th class="px-6 py-3 text-left">Sold Date</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-200">
<?php if ($orders): ?>
<?php foreach ($orders as $order_obj): ?>
<?php
$order = wc_get_order($order_obj->order_id);
$customer_id = $order->get_user_id();
$customer_name = get_userdata($customer_id)->first_name;
// $customer_name = $order->get_formatted_billing_full_name();
$sold_date = date('d/m/Y', strtotime($order_obj->order_date));
foreach ($order->get_items() as $item) {
$product_id = $item->get_product_id();
$product = wc_get_product($product_id);
$equipment = $product ? $product->get_name() : $item->get_name();
$price = wc_price($item->get_total()); // item price
// Get Brand taxonomy (pa_brand)
$brands = wp_get_post_terms($product_id, 'pa_brand', ['fields' => 'names']);
$brand_name = $brands ? implode(', ', $brands) : '-';
?>
<tr class="hover:bg-gray-50">
<td><?php echo esc_html($order->id); ?></td>
<td class="px-6 py-4 font-medium text-gray-900"><?php echo esc_html($customer_name); ?></td>
<td class="px-6 py-4 text-[#0073aa] font-medium"><?php echo esc_html($brand_name); ?></td>
<td class="px-6 py-4 text-[#0073aa] font-medium"><?php echo esc_html($equipment); ?></td>
<td class="px-6 py-4 text-[#0073aa] font-medium"><?php echo wp_kses_post($price); ?></td>
<td class="px-6 py-4 text-gray-900 font-medium"><?php echo esc_html($sold_date); ?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
<?php else: ?>
<tr><td colspan="5" class="px-6 py-4">No Sales found.</td></tr>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
<script>
jQuery(document).ready(function($) {
$('#sales_order_table').DataTable({
"pageLength": 5,
"lengthMenu": [5, 10, 25, 50],
"ordering": true,
"searching": true,
"language": {
"search": "Search..."
},
"columnDefs": [
{ "orderable": true, "targets": [0,1,2,3,4] }
],
"dom": '<"top"l>ftip' // l = length (Show entries), f = filter, t = table, i = info, p = pagination
});
});
</script>
</div>
</div>
</div>