PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.3.4
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.3.4
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 / Traits / PaymentCalculationTrait.php

PaymentCalculationTrait.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.3.4, at includes/Traits/PaymentCalculationTrait.php

114 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EasyInvoice\Traits;
4
5 /**
6 * Trait for calculating payment totals
7 *
8 * This trait provides shared functionality for calculating total payments
9 * for invoices across different payment gateways.
10 */
11 trait PaymentCalculationTrait
12 {
13 /**
14 * Calculate total payments for a specific invoice
15 *
16 * @param int $invoice_id
17 * @return float
18 */
19 private function calculateTotalPaymentsForInvoice($invoice_id): float
20 {
21 // Get all payment records for this invoice
22 $payments = get_posts([
23 'post_type' => 'easy_invoice_payment',
24 'post_status' => 'publish',
25 'meta_query' => [
26 [
27 'key' => '_invoice_id',
28 'value' => $invoice_id,
29 'compare' => '='
30 ],
31 [
32 'key' => '_status',
33 'value' => 'completed',
34 'compare' => '='
35 ]
36 ],
37 'numberposts' => -1
38 ]);
39
40 $total_payments = 0.0;
41
42 foreach ($payments as $payment) {
43 $amount = get_post_meta($payment->ID, '_amount', true);
44 if ($amount && is_numeric($amount)) {
45 $total_payments += floatval($amount);
46 }
47 }
48
49 return $total_payments;
50 }
51
52 /**
53 * Check if invoice should be marked as paid based on total payments
54 *
55 * @param int $invoice_id
56 * @param \EasyInvoice\Models\Invoice $invoice
57 * @return bool
58 */
59 private function shouldMarkInvoiceAsPaid($invoice_id, $invoice): bool
60 {
61 $total_payments = $this->calculateTotalPaymentsForInvoice($invoice_id);
62 $invoice_total = $invoice->getTotal();
63
64 return $total_payments >= $invoice_total;
65 }
66
67 /**
68 * Update invoice status to paid only if total payments are sufficient
69 *
70 * @param int $invoice_id
71 * @param \EasyInvoice\Models\Invoice $invoice
72 * @param string $gateway_name
73 * @return bool True if status was updated, false otherwise
74 */
75 private function updateInvoiceStatusIfPaid($invoice_id, $invoice, $gateway_name = ''): bool
76 {
77 if ($this->shouldMarkInvoiceAsPaid($invoice_id, $invoice)) {
78 $invoice->setStatus('paid');
79 $invoice->save();
80
81 // Log the status update
82 if (method_exists($this, 'log')) {
83 $this->log("Invoice #$invoice_id status updated to 'paid' - payments complete", 'info');
84 } else {
85 error_log("Easy Invoice: Invoice #$invoice_id status updated to 'paid' - payments complete");
86 }
87
88 // Trigger hook when invoice is marked as paid (for email notifications, etc.)
89 // Get payment method and transaction ID from the most recent payment
90 $payment_method = get_post_meta($invoice_id, '_payment_method', true) ?: $gateway_name ?: 'online';
91 $transaction_id = get_post_meta($invoice_id, '_transaction_id', true) ?: '';
92 $payment_amount = $invoice->getTotal();
93
94 do_action('easy_invoice_payment_completed', $invoice_id, $invoice, [
95 'payment_method' => $payment_method,
96 'gateway_name' => $gateway_name,
97 'transaction_id' => $transaction_id,
98 'amount' => $payment_amount
99 ]);
100
101 return true;
102 } else {
103 // Log that status remains unchanged
104 if (method_exists($this, 'log')) {
105 $this->log("Invoice #$invoice_id status remains unchanged - partial payment received", 'info');
106 } else {
107 error_log("Easy Invoice: Invoice #$invoice_id status remains unchanged - partial payment received");
108 }
109
110 return false;
111 }
112 }
113 }
114