| 1 |
<?php |
| 2 |
/** |
| 3 |
* Client Controller Class |
| 4 |
* |
| 5 |
* @package Easy_Invoice |
| 6 |
* @subpackage Controllers |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace EasyInvoice\Controllers; |
| 10 |
|
| 11 |
use EasyInvoice\Providers\ClientServiceProvider; |
| 12 |
use EasyInvoice\Providers\InvoiceServiceProvider; |
| 13 |
use EasyInvoice\Providers\QuoteServiceProvider; |
| 14 |
use EasyInvoice\Constants\PagesSlugs; |
| 15 |
|
| 16 |
/** |
| 17 |
* ClientController handles all client-related functionality |
| 18 |
*/ |
| 19 |
class ClientController extends BaseController { |
| 20 |
/** |
| 21 |
* Initialize the controller |
| 22 |
*/ |
| 23 |
public function init() { |
| 24 |
// Register AJAX handlers for client management |
| 25 |
add_action('wp_ajax_easy_invoice_delete_client', array($this, 'deleteClient')); |
| 26 |
// Other client-related AJAX handlers can be added here |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Display method implementation |
| 31 |
* |
| 32 |
* @param array $args Display arguments |
| 33 |
*/ |
| 34 |
public function display(array $args = []) { |
| 35 |
$page = isset($args['page']) ? $args['page'] : ''; |
| 36 |
|
| 37 |
switch ($page) { |
| 38 |
case PagesSlugs::CLIENTS: |
| 39 |
$this->displayClientsPage(); |
| 40 |
break; |
| 41 |
|
| 42 |
case PagesSlugs::CLIENT_EDIT: |
| 43 |
$this->displayClientEditPage(); |
| 44 |
break; |
| 45 |
|
| 46 |
case PagesSlugs::CLIENT_VIEW: |
| 47 |
$this->displayClientViewPage(); |
| 48 |
break; |
| 49 |
|
| 50 |
default: |
| 51 |
$this->displayClientsPage(); |
| 52 |
break; |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Display clients page |
| 58 |
*/ |
| 59 |
protected function displayClientsPage() { |
| 60 |
// Pagination settings |
| 61 |
$per_page = 20; |
| 62 |
$current_page = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1; |
| 63 |
|
| 64 |
// Get repository |
| 65 |
$repository = ClientServiceProvider::getClientRepository(); |
| 66 |
|
| 67 |
// Get all clients first to calculate total |
| 68 |
$all_clients = $repository->all(); |
| 69 |
$total_clients = count($all_clients); |
| 70 |
$total_pages = ceil($total_clients / $per_page); |
| 71 |
|
| 72 |
// Get paginated clients |
| 73 |
$offset = ($current_page - 1) * $per_page; |
| 74 |
$clients = array_slice($all_clients, $offset, $per_page); |
| 75 |
|
| 76 |
// Initialize stats |
| 77 |
$stats = [ |
| 78 |
'total_clients' => $total_clients, |
| 79 |
'total_invoices' => 0, |
| 80 |
'total_revenue' => [], |
| 81 |
'pending_invoices' => 0 |
| 82 |
]; |
| 83 |
|
| 84 |
// Calculate total revenue from all clients' invoices |
| 85 |
$invoice_repository = InvoiceServiceProvider::getInvoiceRepository(); |
| 86 |
$all_invoices = $invoice_repository->all(); |
| 87 |
$revenue_by_currency = []; |
| 88 |
|
| 89 |
// First, get all currencies that exist in the system |
| 90 |
$all_currencies = []; |
| 91 |
|
| 92 |
foreach ($all_invoices as $invoice) { |
| 93 |
$currency_code = $invoice->getCurrencyCode(); |
| 94 |
|
| 95 |
// If currency is empty or "global", get the actual currency that was used |
| 96 |
if (empty($currency_code) || $currency_code === 'global') { |
| 97 |
// Get the actual currency from invoice meta |
| 98 |
$actual_currency = get_post_meta($invoice->getId(), '_easy_invoice_currency_code', true); |
| 99 |
$currency_code = !empty($actual_currency) ? $actual_currency : get_option('easy_invoice_currency_code', 'USD'); |
| 100 |
} |
| 101 |
|
| 102 |
// If currency is still "global", use the global setting |
| 103 |
if ($currency_code === 'global') { |
| 104 |
$currency_code = get_option('easy_invoice_currency_code', 'USD'); |
| 105 |
} |
| 106 |
|
| 107 |
// Normalize currency code to uppercase for consistent grouping |
| 108 |
$currency_code = strtoupper($currency_code); |
| 109 |
|
| 110 |
if (!empty($currency_code)) { |
| 111 |
$all_currencies[$currency_code] = \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol($currency_code); |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Initialize revenue for all currencies found |
| 116 |
foreach ($all_currencies as $currency_code => $currency_symbol) { |
| 117 |
$revenue_by_currency[$currency_code] = [ |
| 118 |
'amount' => 0, |
| 119 |
'symbol' => $currency_symbol, |
| 120 |
'currency_code' => $currency_code |
| 121 |
]; |
| 122 |
} |
| 123 |
|
| 124 |
// Now calculate revenue for paid invoices |
| 125 |
foreach ($all_invoices as $invoice) { |
| 126 |
if ($invoice->getStatus() === 'paid') { |
| 127 |
$invoice_total = $invoice->getTotal(); |
| 128 |
if (!is_numeric($invoice_total)) { |
| 129 |
continue; |
| 130 |
} |
| 131 |
|
| 132 |
// Get the actual currency from the invoice |
| 133 |
$currency_code = $invoice->getCurrencyCode(); |
| 134 |
|
| 135 |
// If currency is empty or "global", get the actual currency that was used |
| 136 |
if (empty($currency_code) || $currency_code === 'global') { |
| 137 |
// Get the actual currency from invoice meta |
| 138 |
$actual_currency = get_post_meta($invoice->getId(), '_easy_invoice_currency_code', true); |
| 139 |
$currency_code = !empty($actual_currency) ? $actual_currency : get_option('easy_invoice_currency_code', 'USD'); |
| 140 |
} |
| 141 |
|
| 142 |
// If currency is still "global", use the global setting |
| 143 |
if ($currency_code === 'global') { |
| 144 |
$currency_code = get_option('easy_invoice_currency_code', 'USD'); |
| 145 |
} |
| 146 |
|
| 147 |
// Normalize currency code to uppercase for consistent grouping |
| 148 |
$currency_code = strtoupper($currency_code); |
| 149 |
|
| 150 |
if (isset($revenue_by_currency[$currency_code])) { |
| 151 |
$revenue_by_currency[$currency_code]['amount'] += $invoice_total; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
$stats['total_revenue'] = $revenue_by_currency; |
| 157 |
$stats['total_invoices'] = count($all_invoices); |
| 158 |
$stats['pending_invoices'] = count($invoice_repository->findByStatus('pending')); |
| 159 |
|
| 160 |
// Prepare template data with pagination |
| 161 |
$template_data = [ |
| 162 |
'clients' => $clients, |
| 163 |
'stats' => $stats, |
| 164 |
'current_page' => $current_page, |
| 165 |
'per_page' => $per_page, |
| 166 |
'total_clients' => $total_clients, |
| 167 |
'total_pages' => $total_pages |
| 168 |
]; |
| 169 |
|
| 170 |
// Display the template |
| 171 |
$this->displayTemplate( |
| 172 |
EASY_INVOICE_PLUGIN_DIR . 'templates/clients-page.php', |
| 173 |
$template_data |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Display client edit page |
| 179 |
*/ |
| 180 |
protected function displayClientEditPage() { |
| 181 |
$check = $this->checkCapability(); |
| 182 |
if (is_wp_error($check)) { |
| 183 |
wp_die($check->get_error_message()); |
| 184 |
} |
| 185 |
|
| 186 |
$client_id = isset($_GET['client_id']) ? intval($_GET['client_id']) : 0; |
| 187 |
|
| 188 |
if ($client_id <= 0) { |
| 189 |
wp_die(__('Invalid client ID', 'easy-invoice')); |
| 190 |
} |
| 191 |
|
| 192 |
// Get client from repository |
| 193 |
$repository = ClientServiceProvider::getClientRepository(); |
| 194 |
$client = $repository->find($client_id); |
| 195 |
|
| 196 |
if (!$client) { |
| 197 |
wp_die(__('Client not found', 'easy-invoice')); |
| 198 |
} |
| 199 |
|
| 200 |
// Display the template |
| 201 |
$this->displayTemplate( |
| 202 |
EASY_INVOICE_PLUGIN_DIR . 'templates/client-edit-page.php', |
| 203 |
['client' => $client] |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* Display client view page |
| 209 |
*/ |
| 210 |
protected function displayClientViewPage() { |
| 211 |
$check = $this->checkCapability(); |
| 212 |
if (is_wp_error($check)) { |
| 213 |
wp_die($check->get_error_message()); |
| 214 |
} |
| 215 |
|
| 216 |
$client_id = isset($_GET['client_id']) ? intval($_GET['client_id']) : 0; |
| 217 |
|
| 218 |
if ($client_id <= 0) { |
| 219 |
wp_die(__('Invalid client ID', 'easy-invoice')); |
| 220 |
} |
| 221 |
|
| 222 |
// Get client from repository |
| 223 |
$repository = ClientServiceProvider::getClientRepository(); |
| 224 |
$client = $repository->find($client_id); |
| 225 |
|
| 226 |
if (!$client) { |
| 227 |
wp_die(__('Client not found', 'easy-invoice')); |
| 228 |
} |
| 229 |
|
| 230 |
// Get client invoices |
| 231 |
$invoices = []; |
| 232 |
$invoice_repository = InvoiceServiceProvider::getInvoiceRepository(); |
| 233 |
|
| 234 |
// Query invoices for this client |
| 235 |
$args = array( |
| 236 |
'post_type' => 'easy_invoice', |
| 237 |
'post_status' => 'publish', |
| 238 |
'meta_query' => array( |
| 239 |
array( |
| 240 |
'key' => '_easy_invoice_client_id', |
| 241 |
'value' => $client_id, |
| 242 |
'compare' => '=' |
| 243 |
) |
| 244 |
), |
| 245 |
'posts_per_page' => -1, |
| 246 |
'orderby' => 'date', |
| 247 |
'order' => 'DESC' |
| 248 |
); |
| 249 |
|
| 250 |
$invoice_posts = get_posts($args); |
| 251 |
|
| 252 |
foreach ($invoice_posts as $post) { |
| 253 |
$invoice = $invoice_repository->find($post->ID); |
| 254 |
if ($invoice) { |
| 255 |
$invoices[] = array( |
| 256 |
'id' => $invoice->getId(), |
| 257 |
'title' => $invoice->getTitle() ?: $invoice->getNumber(), |
| 258 |
'invoice_number' => $invoice->getNumber(), |
| 259 |
'date' => $invoice->getIssueDate(), |
| 260 |
'due_date' => $invoice->getDueDate(), |
| 261 |
'amount' => $invoice->getTotal(), |
| 262 |
'status' => $invoice->getStatus() |
| 263 |
); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
// Get client quotes |
| 268 |
$quotes = []; |
| 269 |
$quote_repository = QuoteServiceProvider::getQuoteRepository(); |
| 270 |
|
| 271 |
// Query quotes for this client |
| 272 |
$quote_args = array( |
| 273 |
'post_type' => 'easy_invoice_quote', |
| 274 |
'post_status' => 'publish', |
| 275 |
'meta_query' => array( |
| 276 |
array( |
| 277 |
'key' => '_easy_invoice_client_id', |
| 278 |
'value' => $client_id, |
| 279 |
'compare' => '=' |
| 280 |
) |
| 281 |
), |
| 282 |
'posts_per_page' => -1, |
| 283 |
'orderby' => 'date', |
| 284 |
'order' => 'DESC' |
| 285 |
); |
| 286 |
|
| 287 |
$quote_posts = get_posts($quote_args); |
| 288 |
|
| 289 |
foreach ($quote_posts as $post) { |
| 290 |
$quote = $quote_repository->find($post->ID); |
| 291 |
if ($quote) { |
| 292 |
$quotes[] = array( |
| 293 |
'id' => $quote->getId(), |
| 294 |
'title' => $quote->getTitle() ?: $quote->getNumber(), |
| 295 |
'quote_number' => $quote->getNumber(), |
| 296 |
'date' => $quote->getIssueDate(), |
| 297 |
'expiry_date' => $quote->getExpiryDate(), |
| 298 |
'amount' => $quote->getTotal(), |
| 299 |
'status' => $quote->getStatus() |
| 300 |
); |
| 301 |
} |
| 302 |
} |
| 303 |
|
| 304 |
// Get client role information |
| 305 |
$user = get_user_by('ID', $client_id); |
| 306 |
$client_role = $user ? $user->roles[0] ?? 'client' : 'client'; |
| 307 |
|
| 308 |
// Display the template |
| 309 |
$this->displayTemplate( |
| 310 |
EASY_INVOICE_PLUGIN_DIR . 'templates/client-view-page.php', |
| 311 |
[ |
| 312 |
'client' => $client, |
| 313 |
'invoices' => $invoices, |
| 314 |
'quotes' => $quotes, |
| 315 |
'client_role' => $client_role |
| 316 |
] |
| 317 |
); |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Get client stats |
| 322 |
* |
| 323 |
* @return array |
| 324 |
*/ |
| 325 |
public function getClientStats() { |
| 326 |
$repository = ClientServiceProvider::getClientRepository(); |
| 327 |
$clients = $repository->all(); |
| 328 |
|
| 329 |
return [ |
| 330 |
'total_clients' => count($clients) |
| 331 |
]; |
| 332 |
} |
| 333 |
|
| 334 |
/** |
| 335 |
* Delete a client |
| 336 |
*/ |
| 337 |
public function deleteClient() { |
| 338 |
if (!$this->handleAjaxSecurity($_POST['nonce'])) { |
| 339 |
return; |
| 340 |
} |
| 341 |
|
| 342 |
// Check client ID |
| 343 |
if (!isset($_POST['client_id']) || empty($_POST['client_id'])) { |
| 344 |
wp_send_json_error(array('message' => 'Invalid client ID')); |
| 345 |
} |
| 346 |
|
| 347 |
$client_id = intval($_POST['client_id']); |
| 348 |
|
| 349 |
// Delete client |
| 350 |
$repository = ClientServiceProvider::getClientRepository(); |
| 351 |
$result = $repository->delete($client_id); |
| 352 |
|
| 353 |
if ($result) { |
| 354 |
wp_send_json_success(array('message' => 'Client deleted successfully')); |
| 355 |
} else { |
| 356 |
wp_send_json_error(array('message' => 'Error deleting client')); |
| 357 |
} |
| 358 |
} |
| 359 |
} |