| 1 |
<?php |
| 2 |
/** |
| 3 |
* Edit Payment Template |
| 4 |
* |
| 5 |
* @package Easy_Invoice |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace EasyInvoice\Templates\Payments; |
| 9 |
|
| 10 |
use EasyInvoice\Models\Invoice; |
| 11 |
use EasyInvoice\Models\Payment; |
| 12 |
|
| 13 |
if (!isset($payment) || !($payment instanceof Payment)) { |
| 14 |
wp_die(__('Invalid payment', 'easy-invoice')); |
| 15 |
} |
| 16 |
?> |
| 17 |
|
| 18 |
<div class="p-8"> |
| 19 |
<div class="flex justify-between items-center mb-6"> |
| 20 |
<h1 class="text-2xl font-bold text-gray-900"> |
| 21 |
<?php _e('Edit Payment', 'easy-invoice'); ?> |
| 22 |
</h1> |
| 23 |
<a href="<?php echo admin_url('admin.php?page=easy-invoice-payments'); ?>" |
| 24 |
class="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"> |
| 25 |
<?php _e('Back to Payments', 'easy-invoice'); ?> |
| 26 |
</a> |
| 27 |
</div> |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
<div class="bg-white shadow overflow-hidden sm:rounded-lg"> |
| 32 |
<form method="post" action="<?php echo admin_url('admin-ajax.php'); ?>" id="edit-payment-form" class="space-y-6 p-6"> |
| 33 |
<?php wp_nonce_field('easy_invoice_payment', 'payment_nonce'); ?> |
| 34 |
<input type="hidden" name="action" value="easy_invoice_update_payment"> |
| 35 |
<input type="hidden" name="payment_id" value="<?php echo esc_attr($payment->getId()); ?>"> |
| 36 |
|
| 37 |
<div class="grid grid-cols-1 gap-6 sm:grid-cols-2"> |
| 38 |
<!-- Invoice Selection --> |
| 39 |
<div> |
| 40 |
<label for="invoice_id" class="block text-sm font-medium text-gray-700"> |
| 41 |
<?php _e('Invoice', 'easy-invoice'); ?> <span class="text-red-500">*</span> |
| 42 |
</label> |
| 43 |
<select name="invoice_id" id="invoice_id" required |
| 44 |
class="mt-1 block w-full pl-3 pr-10 py-2 text-base border border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md"> |
| 45 |
<option value=""><?php _e('Select Invoice', 'easy-invoice'); ?></option> |
| 46 |
<?php |
| 47 |
// Get current payment's invoice ID |
| 48 |
$current_invoice_id = $payment->getInvoiceId(); |
| 49 |
|
| 50 |
// Get unpaid invoices |
| 51 |
$unpaid_invoices = get_posts(array( |
| 52 |
'post_type' => 'easy_invoice', |
| 53 |
'posts_per_page' => -1, |
| 54 |
'post_status' => 'publish', |
| 55 |
'orderby' => 'date', |
| 56 |
'order' => 'DESC', |
| 57 |
'meta_query' => array( |
| 58 |
'relation' => 'OR', |
| 59 |
array( |
| 60 |
'key' => '_payment_status', |
| 61 |
'value' => 'unpaid', |
| 62 |
'compare' => '=' |
| 63 |
), |
| 64 |
array( |
| 65 |
'key' => '_payment_status', |
| 66 |
'compare' => 'NOT EXISTS' |
| 67 |
), |
| 68 |
array( |
| 69 |
'key' => '_payment_status', |
| 70 |
'value' => '', |
| 71 |
'compare' => '=' |
| 72 |
) |
| 73 |
) |
| 74 |
)); |
| 75 |
|
| 76 |
// Get current payment's invoice if it's not in unpaid list |
| 77 |
$current_invoice = null; |
| 78 |
if ($current_invoice_id) { |
| 79 |
$current_invoice = get_post($current_invoice_id); |
| 80 |
} |
| 81 |
|
| 82 |
// Combine unpaid invoices with current invoice |
| 83 |
$invoices = $unpaid_invoices; |
| 84 |
if ($current_invoice && $current_invoice->post_type === 'easy_invoice') { |
| 85 |
$invoices[] = $current_invoice; |
| 86 |
} |
| 87 |
|
| 88 |
foreach ($invoices as $post) { |
| 89 |
$invoice = new Invoice($post); |
| 90 |
$is_selected = ($invoice->getId() == $current_invoice_id); |
| 91 |
|
| 92 |
// Get invoice number - try different methods |
| 93 |
$invoice_number = $invoice->getNumber(); |
| 94 |
if (empty($invoice_number)) { |
| 95 |
$invoice_number = get_post_meta($invoice->getId(), '_easy_invoice_number', true); |
| 96 |
} |
| 97 |
if (empty($invoice_number)) { |
| 98 |
$invoice_number = 'INV-' . $invoice->getId(); |
| 99 |
} |
| 100 |
|
| 101 |
// Get total amount |
| 102 |
$total = $invoice->getTotal(); |
| 103 |
if (empty($total)) { |
| 104 |
$total = get_post_meta($invoice->getId(), '_easy_invoice_total', true); |
| 105 |
} |
| 106 |
if (empty($total)) { |
| 107 |
$total = 0; |
| 108 |
} |
| 109 |
|
| 110 |
// Get currency symbol |
| 111 |
$currency_symbol = $invoice->getCurrencySymbol(); |
| 112 |
if (empty($currency_symbol)) { |
| 113 |
$currency_symbol = get_option('easy_invoice_currency_symbol', '$'); |
| 114 |
} |
| 115 |
|
| 116 |
printf( |
| 117 |
'<option value="%d" %s>%s - %s</option>', |
| 118 |
$invoice->getId(), |
| 119 |
$is_selected ? 'selected="selected"' : '', |
| 120 |
esc_html($invoice_number), |
| 121 |
esc_html($currency_symbol . number_format($total, 2)) |
| 122 |
); |
| 123 |
} |
| 124 |
?> |
| 125 |
</select> |
| 126 |
</div> |
| 127 |
|
| 128 |
<!-- Payment Method --> |
| 129 |
<div> |
| 130 |
<label for="payment_method" class="block text-sm font-medium text-gray-700"> |
| 131 |
<?php _e('Payment Method', 'easy-invoice'); ?> <span class="text-red-500">*</span> |
| 132 |
</label> |
| 133 |
<select name="payment_method" id="payment_method" required |
| 134 |
class="mt-1 block w-full pl-3 pr-10 py-2 text-base border border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md"> |
| 135 |
<option value=""><?php _e('Select Payment Method', 'easy-invoice'); ?></option> |
| 136 |
<?php |
| 137 |
$current_payment_method = $payment->getPaymentMethod(); |
| 138 |
if (empty($current_payment_method)) { |
| 139 |
$current_payment_method = get_post_meta($payment->getId(), '_payment_method', true); |
| 140 |
} |
| 141 |
|
| 142 |
// Get available payment gateways (sorted) |
| 143 |
$gateway_manager = \EasyInvoice\EasyInvoice::getInstance()->getGatewayManager(); |
| 144 |
$enabled_gateways = $gateway_manager->getEnabledGateways(); |
| 145 |
|
| 146 |
// Show only enabled gateways and manual options |
| 147 |
$available_methods = [ |
| 148 |
'manual' => __('Manual', 'easy-invoice'), |
| 149 |
]; |
| 150 |
|
| 151 |
// Add enabled gateways only (already sorted) |
| 152 |
foreach ($enabled_gateways as $gateway_id => $gateway) { |
| 153 |
$available_methods[$gateway_id] = $gateway_manager->getGatewayDisplayName($gateway_id); |
| 154 |
} |
| 155 |
|
| 156 |
foreach ($available_methods as $method_id => $method_name) { |
| 157 |
$is_selected = ($method_id === $current_payment_method); |
| 158 |
printf( |
| 159 |
'<option value="%s" %s>%s</option>', |
| 160 |
esc_attr($method_id), |
| 161 |
$is_selected ? 'selected="selected"' : '', |
| 162 |
esc_html($method_name) |
| 163 |
); |
| 164 |
} |
| 165 |
?> |
| 166 |
</select> |
| 167 |
</div> |
| 168 |
|
| 169 |
<!-- Amount --> |
| 170 |
<div> |
| 171 |
<label for="amount" class="block text-sm font-medium text-gray-700"> |
| 172 |
<?php _e('Amount', 'easy-invoice'); ?> <span class="text-red-500">*</span> |
| 173 |
</label> |
| 174 |
<div class="mt-1 relative rounded-md shadow-sm"> |
| 175 |
<div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none"> |
| 176 |
<span class="text-gray-500 sm:text-sm"><?php echo get_option('easy_invoice_currency_symbol', '$'); ?></span> |
| 177 |
</div> |
| 178 |
<input type="number" name="amount" id="amount" step="0.01" required |
| 179 |
value="<?php echo esc_attr($payment->getAmount()); ?>" |
| 180 |
class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 pl-7 pr-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" |
| 181 |
placeholder="0.00"> |
| 182 |
</div> |
| 183 |
</div> |
| 184 |
|
| 185 |
<!-- Payment Date --> |
| 186 |
<div> |
| 187 |
<label for="payment_date" class="block text-sm font-medium text-gray-700"> |
| 188 |
<?php _e('Payment Date', 'easy-invoice'); ?> <span class="text-red-500">*</span> |
| 189 |
</label> |
| 190 |
<input type="date" name="payment_date" id="payment_date" |
| 191 |
value="<?php |
| 192 |
$payment_date = $payment->getPaymentDate(); |
| 193 |
if (empty($payment_date)) { |
| 194 |
$payment_date = get_post_meta($payment->getId(), '_payment_date', true); |
| 195 |
} |
| 196 |
if (empty($payment_date)) { |
| 197 |
$payment_date = date('Y-m-d'); |
| 198 |
} |
| 199 |
|
| 200 |
// Ensure date is in YYYY-MM-DD format for HTML date input |
| 201 |
if (!empty($payment_date)) { |
| 202 |
$timestamp = strtotime($payment_date); |
| 203 |
if ($timestamp) { |
| 204 |
$payment_date = date('Y-m-d', $timestamp); |
| 205 |
} else { |
| 206 |
$payment_date = date('Y-m-d'); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
echo esc_attr($payment_date); |
| 211 |
?>" required |
| 212 |
class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"> |
| 213 |
</div> |
| 214 |
|
| 215 |
<!-- Status --> |
| 216 |
<div> |
| 217 |
<label for="status" class="block text-sm font-medium text-gray-700"> |
| 218 |
<?php _e('Status', 'easy-invoice'); ?> <span class="text-red-500">*</span> |
| 219 |
</label> |
| 220 |
<select name="status" id="status" required |
| 221 |
class="mt-1 block w-full pl-3 pr-10 py-2 text-base border border-gray-300 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm rounded-md"> |
| 222 |
<option value="pending" <?php selected($payment->getStatus(), 'pending'); ?>><?php _e('Pending', 'easy-invoice'); ?></option> |
| 223 |
<option value="completed" <?php selected($payment->getStatus(), 'completed'); ?>><?php _e('Completed', 'easy-invoice'); ?></option> |
| 224 |
<option value="failed" <?php selected($payment->getStatus(), 'failed'); ?>><?php _e('Failed', 'easy-invoice'); ?></option> |
| 225 |
<option value="refunded" <?php selected($payment->getStatus(), 'refunded'); ?>><?php _e('Refunded', 'easy-invoice'); ?></option> |
| 226 |
</select> |
| 227 |
</div> |
| 228 |
|
| 229 |
<!-- Notes --> |
| 230 |
<div class="sm:col-span-2"> |
| 231 |
<label for="notes" class="block text-sm font-medium text-gray-700"> |
| 232 |
<?php _e('Notes', 'easy-invoice'); ?> |
| 233 |
</label> |
| 234 |
<div class="mt-1"> |
| 235 |
<textarea name="notes" id="notes" rows="3" |
| 236 |
class="mt-1 block w-full border border-gray-300 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm" |
| 237 |
placeholder="<?php _e('Add any additional notes about this payment...', 'easy-invoice'); ?>"><?php echo wp_kses_post($payment->getNotes()); ?></textarea> |
| 238 |
</div> |
| 239 |
</div> |
| 240 |
</div> |
| 241 |
|
| 242 |
<div class="pt-5"> |
| 243 |
<div class="flex justify-end"> |
| 244 |
<button type="submit" |
| 245 |
class="ml-3 inline-flex justify-center py-2 px-4 border border-transparent shadow-sm text-sm font-medium rounded-md text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"> |
| 246 |
<?php _e('Update Payment', 'easy-invoice'); ?> |
| 247 |
</button> |
| 248 |
</div> |
| 249 |
</div> |
| 250 |
</form> |
| 251 |
</div> |
| 252 |
</div> |
| 253 |
|
| 254 |
<script> |
| 255 |
jQuery(document).ready(function($) { |
| 256 |
// Form submission |
| 257 |
$('#edit-payment-form').on('submit', function(e) { |
| 258 |
e.preventDefault(); |
| 259 |
|
| 260 |
var formData = new FormData(this); |
| 261 |
|
| 262 |
$.ajax({ |
| 263 |
url: $(this).attr('action'), |
| 264 |
type: 'POST', |
| 265 |
data: formData, |
| 266 |
processData: false, |
| 267 |
contentType: false, |
| 268 |
success: function(response) { |
| 269 |
if (response.success) { |
| 270 |
EasyInvoiceToast.success(response.data.message || '<?php _e('Payment updated successfully', 'easy-invoice'); ?>'); |
| 271 |
} else { |
| 272 |
EasyInvoiceToast.error(response.data.message || '<?php _e('An error occurred', 'easy-invoice'); ?>'); |
| 273 |
} |
| 274 |
}, |
| 275 |
error: function() { |
| 276 |
EasyInvoiceToast.error('<?php _e('An error occurred', 'easy-invoice'); ?>'); |
| 277 |
} |
| 278 |
}); |
| 279 |
}); |
| 280 |
}); |
| 281 |
</script> |