| 1 |
<?php |
| 2 |
/** |
| 3 |
* Credit notes for one invoice. |
| 4 |
* |
| 5 |
* Receives from CreditNoteController::maybeRenderPage(): |
| 6 |
* $invoice, $invoice_id, $remaining, $credited, $notes, $notice |
| 7 |
* |
| 8 |
* @package Easy_Invoice |
| 9 |
*/ |
| 10 |
|
| 11 |
use EasyInvoice\Controllers\CreditNoteController; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
$total = (float) $invoice->getTotal(); |
| 18 |
$ei_money = new \EasyInvoice\Helpers\InvoiceFormatter( $invoice ); // the invoice's own currency |
| 19 |
$invoices_url = admin_url( 'admin.php?page=easy-invoice-all' ); |
| 20 |
$fully = $remaining <= 0.001; |
| 21 |
?> |
| 22 |
|
| 23 |
<div class="p-8"> |
| 24 |
|
| 25 |
<div class="ei-app-page-header bg-white border-b border-gray-200 px-6 easy-invoice-admin-page-header"> |
| 26 |
<div class="flex items-center justify-between flex-wrap gap-4"> |
| 27 |
<div> |
| 28 |
<h1 class="text-2xl font-bold text-gray-900"> |
| 29 |
<?php |
| 30 |
printf( |
| 31 |
/* translators: %s: invoice number. */ |
| 32 |
esc_html__( 'Credit note for %s', 'easy-invoice' ), |
| 33 |
esc_html( (string) $invoice->getNumber() ) |
| 34 |
); |
| 35 |
?> |
| 36 |
</h1> |
| 37 |
<p class="mt-1 text-sm text-gray-500"> |
| 38 |
<?php esc_html_e( 'An issued invoice cannot be edited or deleted — it is a tax record. A credit note leaves it standing and records the correction against it.', 'easy-invoice' ); ?> |
| 39 |
</p> |
| 40 |
</div> |
| 41 |
<a href="<?php echo esc_url( $invoices_url ); ?>" class="text-sm font-medium text-indigo-600 hover:text-indigo-800"> |
| 42 |
<?php esc_html_e( 'Back to invoices', 'easy-invoice' ); ?> |
| 43 |
</a> |
| 44 |
</div> |
| 45 |
</div> |
| 46 |
|
| 47 |
<?php if ( '' !== $notice ) : ?> |
| 48 |
<div class="mt-6 rounded-md bg-blue-50 border border-blue-200 p-4"> |
| 49 |
<p class="text-sm text-blue-900"><?php echo esc_html( $notice ); ?></p> |
| 50 |
</div> |
| 51 |
<?php endif; ?> |
| 52 |
|
| 53 |
<div class="mt-6 grid grid-cols-1 lg:grid-cols-3 gap-6 items-start"> |
| 54 |
|
| 55 |
<!-- Position --> |
| 56 |
<div class="bg-white rounded-lg border border-gray-200 p-6"> |
| 57 |
<h2 class="text-xs font-semibold uppercase tracking-wide text-gray-500"><?php esc_html_e( 'Position', 'easy-invoice' ); ?></h2> |
| 58 |
<dl class="mt-3 space-y-2 text-sm"> |
| 59 |
<div class="flex justify-between"> |
| 60 |
<dt class="text-gray-500"><?php esc_html_e( 'Invoiced', 'easy-invoice' ); ?></dt> |
| 61 |
<dd class="text-gray-900" style="font-variant-numeric: tabular-nums;"><?php echo esc_html( $ei_money->format( $total ) ); ?></dd> |
| 62 |
</div> |
| 63 |
<div class="flex justify-between"> |
| 64 |
<dt class="text-gray-500"><?php esc_html_e( 'Credited', 'easy-invoice' ); ?></dt> |
| 65 |
<dd class="text-gray-900" style="font-variant-numeric: tabular-nums;"><?php echo esc_html( $ei_money->format( $credited ) ); ?></dd> |
| 66 |
</div> |
| 67 |
<div class="flex justify-between border-t border-gray-100 pt-2 font-semibold"> |
| 68 |
<dt class="text-gray-900"><?php esc_html_e( 'Still creditable', 'easy-invoice' ); ?></dt> |
| 69 |
<dd class="text-gray-900" style="font-variant-numeric: tabular-nums;"><?php echo esc_html( $ei_money->format( max( 0, $remaining ) ) ); ?></dd> |
| 70 |
</div> |
| 71 |
</dl> |
| 72 |
</div> |
| 73 |
|
| 74 |
<!-- Issue --> |
| 75 |
<div class="bg-white rounded-lg border border-gray-200 p-6 lg:col-span-2"> |
| 76 |
<h2 class="text-lg font-semibold text-gray-900"><?php esc_html_e( 'Issue a credit note', 'easy-invoice' ); ?></h2> |
| 77 |
|
| 78 |
<?php if ( $fully ) : ?> |
| 79 |
<p class="mt-2 text-sm text-gray-600"> |
| 80 |
<?php esc_html_e( 'This invoice has been credited in full. There is nothing left to credit.', 'easy-invoice' ); ?> |
| 81 |
</p> |
| 82 |
<?php elseif ( empty( $can_issue ) ) : ?> |
| 83 |
<p class="mt-2 text-sm text-gray-600"> |
| 84 |
<?php esc_html_e( 'Your role can view credit notes but not issue them.', 'easy-invoice' ); ?> |
| 85 |
</p> |
| 86 |
<?php else : ?> |
| 87 |
<form method="post" action="<?php echo esc_url( admin_url( 'admin-post.php' ) ); ?>" class="mt-4"> |
| 88 |
<input type="hidden" name="action" value="<?php echo esc_attr( CreditNoteController::ACTION_CREATE ); ?>"> |
| 89 |
<input type="hidden" name="invoice_id" value="<?php echo esc_attr( (string) $invoice_id ); ?>"> |
| 90 |
<?php wp_nonce_field( CreditNoteController::ACTION_CREATE ); ?> |
| 91 |
|
| 92 |
<div class="grid grid-cols-1 sm:grid-cols-3 gap-4"> |
| 93 |
<div> |
| 94 |
<label for="ei-cn-amount" class="block text-sm font-medium text-gray-700"><?php esc_html_e( 'Amount', 'easy-invoice' ); ?></label> |
| 95 |
<input type="text" inputmode="decimal" id="ei-cn-amount" name="amount" |
| 96 |
placeholder="<?php echo esc_attr( number_format_i18n( max( 0, $remaining ), 2 ) ); ?>" |
| 97 |
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm text-sm"> |
| 98 |
<p class="mt-1 text-xs text-gray-500"> |
| 99 |
<?php esc_html_e( 'Leave empty to credit the whole invoice.', 'easy-invoice' ); ?> |
| 100 |
</p> |
| 101 |
</div> |
| 102 |
<div class="sm:col-span-2"> |
| 103 |
<label for="ei-cn-reason" class="block text-sm font-medium text-gray-700"><?php esc_html_e( 'Reason', 'easy-invoice' ); ?></label> |
| 104 |
<input type="text" id="ei-cn-reason" name="reason" |
| 105 |
placeholder="<?php esc_attr_e( 'e.g. Item returned, billed in error', 'easy-invoice' ); ?>" |
| 106 |
class="mt-1 block w-full rounded-md border-gray-300 shadow-sm text-sm"> |
| 107 |
<p class="mt-1 text-xs text-gray-500"> |
| 108 |
<?php esc_html_e( 'Appears on the credit note. Worth writing — it is what your accountant reads a year from now.', 'easy-invoice' ); ?> |
| 109 |
</p> |
| 110 |
</div> |
| 111 |
</div> |
| 112 |
|
| 113 |
<button type="submit" class="mt-4 inline-flex items-center px-3 py-1.5 text-sm font-medium rounded-md bg-indigo-600 text-white hover:bg-indigo-700"> |
| 114 |
<?php esc_html_e( 'Issue credit note', 'easy-invoice' ); ?> |
| 115 |
</button> |
| 116 |
</form> |
| 117 |
<?php endif; ?> |
| 118 |
</div> |
| 119 |
</div> |
| 120 |
|
| 121 |
<?php if ( ! empty( $notes ) ) : ?> |
| 122 |
<div class="mt-6 bg-white rounded-lg border border-gray-200 p-6"> |
| 123 |
<h2 class="text-lg font-semibold text-gray-900"><?php esc_html_e( 'Credit notes against this invoice', 'easy-invoice' ); ?></h2> |
| 124 |
<div class="mt-4 overflow-x-auto"> |
| 125 |
<table class="min-w-full divide-y divide-gray-200 text-sm"> |
| 126 |
<thead> |
| 127 |
<tr class="text-left text-xs uppercase tracking-wide text-gray-500"> |
| 128 |
<th class="py-2 pr-4 font-semibold"><?php esc_html_e( 'Number', 'easy-invoice' ); ?></th> |
| 129 |
<th class="py-2 px-4 font-semibold"><?php esc_html_e( 'Date', 'easy-invoice' ); ?></th> |
| 130 |
<th class="py-2 px-4 font-semibold"><?php esc_html_e( 'Reason', 'easy-invoice' ); ?></th> |
| 131 |
<th class="py-2 px-4 font-semibold text-right"><?php esc_html_e( 'Amount', 'easy-invoice' ); ?></th> |
| 132 |
<th class="py-2 pl-4 font-semibold text-right"><?php esc_html_e( 'PDF', 'easy-invoice' ); ?></th> |
| 133 |
</tr> |
| 134 |
</thead> |
| 135 |
<tbody class="divide-y divide-gray-100"> |
| 136 |
<?php foreach ( $notes as $credit_id ) : ?> |
| 137 |
<?php $note = new \EasyInvoice\Models\Invoice( get_post( $credit_id ) ); ?> |
| 138 |
<tr> |
| 139 |
<td class="py-2.5 pr-4 font-medium text-gray-900"><?php echo esc_html( (string) $note->getNumber() ); ?></td> |
| 140 |
<td class="py-2.5 px-4 text-gray-700"><?php echo esc_html( (string) $note->getIssueDate() ); ?></td> |
| 141 |
<td class="py-2.5 px-4 text-gray-500"> |
| 142 |
<?php echo esc_html( (string) get_post_meta( $credit_id, \EasyInvoice\Services\CreditNote::META_REASON, true ) ); ?> |
| 143 |
</td> |
| 144 |
<td class="py-2.5 px-4 text-right text-gray-900" style="font-variant-numeric: tabular-nums;"> |
| 145 |
<?php echo esc_html( $ei_money->format( (float) $note->getTotal() ) ); ?> |
| 146 |
</td> |
| 147 |
<td class="py-2.5 pl-4 text-right"> |
| 148 |
<a href="<?php echo esc_url( CreditNoteController::downloadUrl( (int) $credit_id ) ); ?>" |
| 149 |
class="text-indigo-600 hover:text-indigo-800 font-medium"><?php esc_html_e( 'Download', 'easy-invoice' ); ?></a> |
| 150 |
<?php |
| 151 |
/** |
| 152 |
* Extra actions for one credit note. |
| 153 |
* |
| 154 |
* The seam the E-Invoicing addon uses to offer the |
| 155 |
* structured formats here, the same way it adds them |
| 156 |
* to invoice rows. A credit note is a document under |
| 157 |
* EN 16931 in its own right (type code 381), so it |
| 158 |
* needs the same treatment as the invoice it corrects. |
| 159 |
* |
| 160 |
* @param array $actions Rendered anchors, keyed. |
| 161 |
* @param int $credit_id Credit note ID. |
| 162 |
*/ |
| 163 |
$cn_actions = (array) apply_filters( 'easy_invoice_credit_note_actions', [], (int) $credit_id ); |
| 164 |
foreach ( $cn_actions as $cn_action ) { |
| 165 |
echo '<span class="ml-3">' . wp_kses_post( $cn_action ) . '</span>'; |
| 166 |
} |
| 167 |
?> |
| 168 |
</td> |
| 169 |
</tr> |
| 170 |
<?php endforeach; ?> |
| 171 |
</tbody> |
| 172 |
</table> |
| 173 |
</div> |
| 174 |
</div> |
| 175 |
<?php endif; ?> |
| 176 |
|
| 177 |
</div> |
| 178 |
|