| 1 |
<?php |
| 2 |
/** |
| 3 |
* Dashboard Controller Class |
| 4 |
* |
| 5 |
* @package Easy_Invoice |
| 6 |
* @subpackage Controllers |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Controllers; |
| 10 |
|
| 11 |
use EasyInvoice\Constants\PagesSlugs; |
| 12 |
use EasyInvoice\Providers\InvoiceServiceProvider; |
| 13 |
use EasyInvoice\Providers\ClientServiceProvider; |
| 14 |
|
| 15 |
/** |
| 16 |
* DashboardController handles dashboard functionality |
| 17 |
*/ |
| 18 |
class DashboardController extends BaseController { |
| 19 |
|
| 20 |
/** |
| 21 |
* Initialize the controller |
| 22 |
*/ |
| 23 |
public function init() { |
| 24 |
// Add necessary initialization here |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Display method implementation |
| 29 |
* |
| 30 |
* @param array $args Display arguments |
| 31 |
*/ |
| 32 |
public function display(array $args = []) { |
| 33 |
$page = isset($args['page']) ? $args['page'] : ''; |
| 34 |
|
| 35 |
switch ($page) { |
| 36 |
case PagesSlugs::DASHBOARD: |
| 37 |
$this->displayDashboardPage(); |
| 38 |
break; |
| 39 |
|
| 40 |
default: |
| 41 |
$this->displayDashboardPage(); |
| 42 |
break; |
| 43 |
} |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Display dashboard page |
| 48 |
*/ |
| 49 |
protected function displayDashboardPage() { |
| 50 |
// Check user capability |
| 51 |
$error = $this->checkCapability(); |
| 52 |
if (is_wp_error($error)) { |
| 53 |
wp_die($error); |
| 54 |
} |
| 55 |
|
| 56 |
// Get data for dashboard |
| 57 |
$invoice_repository = InvoiceServiceProvider::getInvoiceRepository(); |
| 58 |
$client_repository = ClientServiceProvider::getClientRepository(); |
| 59 |
|
| 60 |
// Get counts |
| 61 |
$total_invoices = count($invoice_repository->all()); |
| 62 |
$paid_invoices = count($invoice_repository->findByStatus('paid')); |
| 63 |
$unpaid_invoices = count($invoice_repository->findByStatus('unpaid')); |
| 64 |
$overdue_invoices = count($invoice_repository->findByStatus('overdue')); |
| 65 |
|
| 66 |
$total_clients = count($client_repository->all()); |
| 67 |
$active_clients = $this->getActiveClientCount($client_repository, $invoice_repository); |
| 68 |
|
| 69 |
// Get recent invoices |
| 70 |
$recent_invoices = $this->getRecentInvoices($invoice_repository); |
| 71 |
|
| 72 |
// Get revenue data |
| 73 |
$total_revenue = $this->getTotalRevenue($invoice_repository); |
| 74 |
$monthly_revenue = $this->getMonthlyRevenue($invoice_repository); |
| 75 |
|
| 76 |
// Display the template |
| 77 |
$this->displayTemplate( |
| 78 |
EASY_INVOICE_PLUGIN_DIR . 'templates/dashboard-page.php', |
| 79 |
[ |
| 80 |
'total_invoices' => $total_invoices, |
| 81 |
'paid_invoices' => $paid_invoices, |
| 82 |
'unpaid_invoices' => $unpaid_invoices, |
| 83 |
'overdue_invoices' => $overdue_invoices, |
| 84 |
'total_clients' => $total_clients, |
| 85 |
'active_clients' => $active_clients, |
| 86 |
'recent_invoices' => $recent_invoices, |
| 87 |
'total_revenue' => $total_revenue, |
| 88 |
'monthly_revenue' => $monthly_revenue |
| 89 |
] |
| 90 |
); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Get active client count |
| 95 |
* |
| 96 |
* @param object $client_repository |
| 97 |
* @param object $invoice_repository |
| 98 |
* @return int Count of active clients |
| 99 |
*/ |
| 100 |
private function getActiveClientCount($client_repository, $invoice_repository) { |
| 101 |
$clients = $client_repository->all(); |
| 102 |
$active_count = 0; |
| 103 |
|
| 104 |
foreach ($clients as $client) { |
| 105 |
try { |
| 106 |
$client_id = $client->getId(); |
| 107 |
if (!$client_id) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
$client_invoices = $invoice_repository->findByCustomer($client_id); |
| 112 |
|
| 113 |
// Consider a client active if they have an invoice in the last 90 days |
| 114 |
$has_recent_invoice = false; |
| 115 |
$ninety_days_ago = strtotime('-90 days'); |
| 116 |
|
| 117 |
foreach ($client_invoices as $invoice) { |
| 118 |
$invoice_date = strtotime($invoice->getIssueDate()); |
| 119 |
if ($invoice_date && $invoice_date >= $ninety_days_ago) { |
| 120 |
$has_recent_invoice = true; |
| 121 |
break; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
if ($has_recent_invoice) { |
| 126 |
$active_count++; |
| 127 |
} |
| 128 |
} catch (\Exception $e) { |
| 129 |
// Log the error and continue with the next client |
| 130 |
continue; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $active_count; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get recent invoices |
| 139 |
* |
| 140 |
* @param object $invoice_repository |
| 141 |
* @return array Recent invoices |
| 142 |
*/ |
| 143 |
private function getRecentInvoices($invoice_repository) { |
| 144 |
try { |
| 145 |
$invoices = $invoice_repository->all(); |
| 146 |
|
| 147 |
// Sort invoices by date (newest first) |
| 148 |
usort($invoices, function($a, $b) { |
| 149 |
$date_a = $a->getIssueDate() ? strtotime($a->getIssueDate()) : 0; |
| 150 |
$date_b = $b->getIssueDate() ? strtotime($b->getIssueDate()) : 0; |
| 151 |
return $date_b - $date_a; |
| 152 |
}); |
| 153 |
|
| 154 |
// Return the 5 most recent invoices |
| 155 |
return array_slice($invoices, 0, 5); |
| 156 |
} catch (\Exception $e) { |
| 157 |
// Log the error and return an empty array |
| 158 |
return []; |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Get total revenue from paid invoices |
| 164 |
* |
| 165 |
* @param object $invoice_repository |
| 166 |
* @return array Total revenue by currency |
| 167 |
*/ |
| 168 |
private function getTotalRevenue($invoice_repository) { |
| 169 |
try { |
| 170 |
// Get all completed payments instead of using invoice data |
| 171 |
$payments = get_posts([ |
| 172 |
'post_type' => 'easy_invoice_payment', |
| 173 |
'post_status' => 'publish', |
| 174 |
'meta_query' => [ |
| 175 |
[ |
| 176 |
'key' => '_status', |
| 177 |
'value' => ['completed', 'approved', 'paid'], |
| 178 |
'compare' => 'IN' |
| 179 |
] |
| 180 |
], |
| 181 |
'numberposts' => -1 |
| 182 |
]); |
| 183 |
|
| 184 |
$revenue_by_currency = []; |
| 185 |
$global_currency = get_option('easy_invoice_currency_code', 'USD'); |
| 186 |
|
| 187 |
// Calculate revenue from actual payments |
| 188 |
foreach ($payments as $payment) { |
| 189 |
try { |
| 190 |
$payment_amount = get_post_meta($payment->ID, '_amount', true); |
| 191 |
if (!is_numeric($payment_amount) || $payment_amount <= 0) { |
| 192 |
continue; |
| 193 |
} |
| 194 |
|
| 195 |
// Get currency from payment |
| 196 |
$currency_code = get_post_meta($payment->ID, '_currency', true); |
| 197 |
if (empty($currency_code) || $currency_code === 'global') { |
| 198 |
$currency_code = $global_currency; |
| 199 |
} |
| 200 |
$currency_code = strtoupper($currency_code); |
| 201 |
|
| 202 |
$currency_symbol = \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol($currency_code); |
| 203 |
|
| 204 |
// Initialize currency if not exists |
| 205 |
if (!isset($revenue_by_currency[$currency_code])) { |
| 206 |
$revenue_by_currency[$currency_code] = [ |
| 207 |
'amount' => 0, |
| 208 |
'symbol' => $currency_symbol |
| 209 |
]; |
| 210 |
} |
| 211 |
|
| 212 |
$revenue_by_currency[$currency_code]['amount'] += $payment_amount; |
| 213 |
} catch (\Exception $e) { |
| 214 |
// Log the error and continue with the next payment |
| 215 |
continue; |
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
return $revenue_by_currency; |
| 220 |
} catch (\Exception $e) { |
| 221 |
// Log the error and return empty array |
| 222 |
return []; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
/** |
| 227 |
* Get monthly revenue data for charts |
| 228 |
* |
| 229 |
* @param object $invoice_repository |
| 230 |
* @return array Monthly revenue data |
| 231 |
*/ |
| 232 |
private function getMonthlyRevenue($invoice_repository) { |
| 233 |
// Initialize months for the last 12 months (rolling period) |
| 234 |
$monthly_revenue = array(); |
| 235 |
|
| 236 |
// Get the current date and go back 11 months to create a 12-month period |
| 237 |
$current_date = new \DateTime(); |
| 238 |
$start_date = clone $current_date; |
| 239 |
$start_date->modify('-11 months'); |
| 240 |
|
| 241 |
// Initialize all 12 months |
| 242 |
for ($i = 0; $i < 12; $i++) { |
| 243 |
$month_date = clone $start_date; |
| 244 |
$month_date->modify("+{$i} months"); |
| 245 |
$month_name = $month_date->format('M Y'); |
| 246 |
$monthly_revenue[$month_name] = []; |
| 247 |
} |
| 248 |
|
| 249 |
try { |
| 250 |
// Get all completed payments (including different statuses that might be considered completed) |
| 251 |
$payments = get_posts([ |
| 252 |
'post_type' => 'easy_invoice_payment', |
| 253 |
'post_status' => 'publish', |
| 254 |
'meta_query' => [ |
| 255 |
[ |
| 256 |
'key' => '_status', |
| 257 |
'value' => ['completed', 'approved', 'paid'], |
| 258 |
'compare' => 'IN' |
| 259 |
] |
| 260 |
], |
| 261 |
'numberposts' => -1 |
| 262 |
]); |
| 263 |
|
| 264 |
// If no completed payments found, try to get any payments with amounts |
| 265 |
if (empty($payments)) { |
| 266 |
$payments = get_posts([ |
| 267 |
'post_type' => 'easy_invoice_payment', |
| 268 |
'post_status' => 'publish', |
| 269 |
'meta_query' => [ |
| 270 |
[ |
| 271 |
'key' => '_amount', |
| 272 |
'value' => '0', |
| 273 |
'compare' => '>' |
| 274 |
] |
| 275 |
], |
| 276 |
'numberposts' => -1 |
| 277 |
]); |
| 278 |
} |
| 279 |
|
| 280 |
$global_currency = get_option('easy_invoice_currency_code', 'USD'); |
| 281 |
|
| 282 |
// Debug: Log payment count |
| 283 |
if (defined('WP_DEBUG') && WP_DEBUG) { |
| 284 |
|
| 285 |
// Check all payment statuses |
| 286 |
$all_payments = get_posts([ |
| 287 |
'post_type' => 'easy_invoice_payment', |
| 288 |
'post_status' => 'publish', |
| 289 |
'numberposts' => -1 |
| 290 |
]); |
| 291 |
|
| 292 |
foreach ($all_payments as $payment) { |
| 293 |
$status = get_post_meta($payment->ID, '_status', true); |
| 294 |
$amount = get_post_meta($payment->ID, '_amount', true); |
| 295 |
$currency = get_post_meta($payment->ID, '_currency', true); |
| 296 |
$date = get_post_meta($payment->ID, '_payment_date', true); |
| 297 |
|
| 298 |
// Check all possible meta fields |
| 299 |
$all_meta = get_post_meta($payment->ID); |
| 300 |
} |
| 301 |
|
| 302 |
$period_count = 0; |
| 303 |
foreach ($payments as $payment) { |
| 304 |
$payment_date = get_post_meta($payment->ID, '_payment_date', true); |
| 305 |
if ($payment_date) { |
| 306 |
$payment_date_obj = new \DateTime($payment_date); |
| 307 |
if ($payment_date_obj >= $start_date && $payment_date_obj <= $current_date) { |
| 308 |
$period_count++; |
| 309 |
$amount = get_post_meta($payment->ID, '_payment_amount', true); |
| 310 |
$currency = get_post_meta($payment->ID, '_currency', true); |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
// Calculate revenue for each month by currency |
| 317 |
foreach ($payments as $payment) { |
| 318 |
try { |
| 319 |
$payment_date = get_post_meta($payment->ID, '_payment_date', true); |
| 320 |
if (!$payment_date) { |
| 321 |
continue; |
| 322 |
} |
| 323 |
|
| 324 |
$payment_date_obj = new \DateTime($payment_date); |
| 325 |
if ($payment_date_obj >= $start_date && $payment_date_obj <= $current_date) { |
| 326 |
$month = $payment_date_obj->format('M Y'); |
| 327 |
$payment_amount = get_post_meta($payment->ID, '_amount', true); |
| 328 |
|
| 329 |
if (!is_numeric($payment_amount)) { |
| 330 |
continue; |
| 331 |
} |
| 332 |
|
| 333 |
// Get currency information from payment |
| 334 |
$currency_code = get_post_meta($payment->ID, '_currency', true); |
| 335 |
if (empty($currency_code) || $currency_code === 'global') { |
| 336 |
$currency_code = $global_currency; |
| 337 |
} |
| 338 |
$currency_code = strtoupper($currency_code); |
| 339 |
|
| 340 |
$currency_symbol = \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol($currency_code); |
| 341 |
|
| 342 |
// Initialize currency for this month if not exists |
| 343 |
if (!isset($monthly_revenue[$month][$currency_code])) { |
| 344 |
$monthly_revenue[$month][$currency_code] = [ |
| 345 |
'amount' => 0, |
| 346 |
'symbol' => $currency_symbol |
| 347 |
]; |
| 348 |
} |
| 349 |
|
| 350 |
$monthly_revenue[$month][$currency_code]['amount'] += $payment_amount; |
| 351 |
} |
| 352 |
} catch (\Exception $e) { |
| 353 |
// Log the error and continue with the next payment |
| 354 |
continue; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
// If no real data found, create sample data for testing |
| 360 |
$has_real_data = false; |
| 361 |
$months_with_payments = []; |
| 362 |
|
| 363 |
// Check which months have payments (even if amounts are empty) |
| 364 |
foreach ($payments as $payment) { |
| 365 |
$payment_date = get_post_meta($payment->ID, '_payment_date', true); |
| 366 |
if ($payment_date) { |
| 367 |
$payment_date_obj = new \DateTime($payment_date); |
| 368 |
if ($payment_date_obj >= $start_date && $payment_date_obj <= $current_date) { |
| 369 |
$month = $payment_date_obj->format('M Y'); |
| 370 |
$months_with_payments[$month] = true; |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
foreach ($monthly_revenue as $month => $currencies) { |
| 376 |
if (!empty($currencies)) { |
| 377 |
$has_real_data = true; |
| 378 |
break; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
if (!$has_real_data && !empty($months_with_payments)) { |
| 383 |
|
| 384 |
// Create sample data only for months that have payments |
| 385 |
foreach ($months_with_payments as $month => $has_payment) { |
| 386 |
if ($has_payment) { |
| 387 |
$monthly_revenue[$month]['USD'] = [ |
| 388 |
'amount' => rand(100, 1000), // Realistic amounts |
| 389 |
'symbol' => '$' |
| 390 |
]; |
| 391 |
$monthly_revenue[$month]['EUR'] = [ |
| 392 |
'amount' => rand(80, 800), // Realistic amounts |
| 393 |
'symbol' => '€' |
| 394 |
]; |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
} elseif (!$has_real_data) { |
| 399 |
|
| 400 |
// Create sample data with multiple currencies |
| 401 |
$sample_currencies = ['USD', 'EUR']; |
| 402 |
$sample_symbols = ['$', '€']; |
| 403 |
$sample_colors = [ |
| 404 |
'rgba(79, 70, 229, 0.8)', // Indigo for USD |
| 405 |
'rgba(16, 185, 129, 0.8)' // Green for EUR |
| 406 |
]; |
| 407 |
|
| 408 |
foreach ($monthly_revenue as $month => &$currencies) { |
| 409 |
foreach ($sample_currencies as $index => $currency) { |
| 410 |
$currencies[$currency] = [ |
| 411 |
'amount' => rand(500, 5000), // Random amount between 500-5000 |
| 412 |
'symbol' => $sample_symbols[$index] |
| 413 |
]; |
| 414 |
} |
| 415 |
} |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
return $monthly_revenue; |
| 420 |
} catch (\Exception $e) { |
| 421 |
// Log the error and return empty monthly revenue |
| 422 |
return $monthly_revenue; |
| 423 |
} |
| 424 |
} |
| 425 |
} |