PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.2.0
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.2.0
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Controllers / DashboardController.php

DashboardController.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.2.0, at includes/Controllers/DashboardController.php

329 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 // Calculate revenue for each month by currency
283 foreach ($payments as $payment) {
284 try {
285 $payment_date = get_post_meta($payment->ID, '_payment_date', true);
286 if (!$payment_date) {
287 continue;
288 }
289
290 $payment_date_obj = new \DateTime($payment_date);
291 if ($payment_date_obj >= $start_date && $payment_date_obj <= $current_date) {
292 $month = $payment_date_obj->format('M Y');
293 $payment_amount = get_post_meta($payment->ID, '_amount', true);
294
295 if (!is_numeric($payment_amount)) {
296 continue;
297 }
298
299 // Get currency information from payment
300 $currency_code = get_post_meta($payment->ID, '_currency', true);
301 if (empty($currency_code) || $currency_code === 'global') {
302 $currency_code = $global_currency;
303 }
304 $currency_code = strtoupper($currency_code);
305
306 $currency_symbol = \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol($currency_code);
307
308 // Initialize currency for this month if not exists
309 if (!isset($monthly_revenue[$month][$currency_code])) {
310 $monthly_revenue[$month][$currency_code] = [
311 'amount' => 0,
312 'symbol' => $currency_symbol
313 ];
314 }
315
316 $monthly_revenue[$month][$currency_code]['amount'] += $payment_amount;
317 }
318 } catch (\Exception $e) {
319 // Log the error and continue with the next payment
320 continue;
321 }
322 }
323 return $monthly_revenue;
324 } catch (\Exception $e) {
325 // Log the error and return empty monthly revenue
326 return $monthly_revenue;
327 }
328 }
329 }