| 1 |
<?php |
| 2 |
/** |
| 3 |
* How an invoice's status is shown. |
| 4 |
* |
| 5 |
* The stored status is what the workflow set (draft, available, partial, |
| 6 |
* paid, cancelled…). "Overdue" is a condition — still owed after the due |
| 7 |
* date — that nothing writes, so it is derived here for display, and the |
| 8 |
* labels and badge colours live in one place. |
| 9 |
* |
| 10 |
* @package EasyInvoice\Helpers |
| 11 |
* @since 2.4.0 |
| 12 |
*/ |
| 13 |
|
| 14 |
namespace EasyInvoice\Helpers; |
| 15 |
|
| 16 |
if ( ! defined( 'ABSPATH' ) ) { |
| 17 |
exit; |
| 18 |
} |
| 19 |
|
| 20 |
class InvoiceStatus { |
| 21 |
|
| 22 |
/** Statuses on which money is still expected. */ |
| 23 |
const OPEN = [ 'available', 'unpaid', 'partial', 'sent', 'pending', 'overdue' ]; |
| 24 |
|
| 25 |
/** |
| 26 |
* Status key to show for an invoice: the stored one, or 'overdue' when an |
| 27 |
* open invoice is past its due date. |
| 28 |
* |
| 29 |
* @param string $status Stored status. |
| 30 |
* @param string|null $due_date Y-m-d (or anything strtotime reads). |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public static function displayKey( string $status, ?string $due_date ): string { |
| 34 |
$status = strtolower( trim( $status ) ); |
| 35 |
if ( in_array( $status, self::OPEN, true ) && $due_date ) { |
| 36 |
$due = strtotime( $due_date ); |
| 37 |
if ( $due && gmdate( 'Y-m-d', $due ) < gmdate( 'Y-m-d', current_time( 'timestamp' ) ) ) { |
| 38 |
return 'overdue'; |
| 39 |
} |
| 40 |
} |
| 41 |
return $status; |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* Same, from a model. |
| 46 |
* |
| 47 |
* @param object $invoice Invoice model. |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
public static function displayKeyFor( $invoice ): string { |
| 51 |
$status = is_callable( [ $invoice, 'getStatus' ] ) ? (string) $invoice->getStatus() : ''; |
| 52 |
$due = is_callable( [ $invoice, 'getDueDate' ] ) ? (string) $invoice->getDueDate() : ''; |
| 53 |
return self::displayKey( $status, $due ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Human label for a status key. |
| 58 |
* |
| 59 |
* @param string $key Status key. |
| 60 |
* @return string |
| 61 |
*/ |
| 62 |
public static function label( string $key ): string { |
| 63 |
$labels = [ |
| 64 |
'available' => __( 'Available', 'easy-invoice' ), |
| 65 |
'paid' => __( 'Paid', 'easy-invoice' ), |
| 66 |
'partial' => __( 'Partially paid', 'easy-invoice' ), |
| 67 |
'unpaid' => __( 'Unpaid', 'easy-invoice' ), |
| 68 |
'overdue' => __( 'Overdue', 'easy-invoice' ), |
| 69 |
'draft' => __( 'Draft', 'easy-invoice' ), |
| 70 |
'cancelled' => __( 'Cancelled', 'easy-invoice' ), |
| 71 |
'canceled' => __( 'Cancelled', 'easy-invoice' ), |
| 72 |
'sent' => __( 'Sent', 'easy-invoice' ), |
| 73 |
'pending' => __( 'Pending', 'easy-invoice' ), |
| 74 |
]; |
| 75 |
return $labels[ strtolower( $key ) ] ?? ucfirst( $key ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Tailwind badge classes for a status key. |
| 80 |
* |
| 81 |
* @param string $key Status key. |
| 82 |
* @return string |
| 83 |
*/ |
| 84 |
public static function badgeClass( string $key ): string { |
| 85 |
switch ( strtolower( $key ) ) { |
| 86 |
case 'paid': |
| 87 |
return 'bg-green-100 text-green-800 ring-1 ring-green-200'; |
| 88 |
case 'partial': |
| 89 |
case 'unpaid': |
| 90 |
case 'pending': |
| 91 |
return 'bg-yellow-100 text-yellow-800 ring-1 ring-yellow-200'; |
| 92 |
case 'overdue': |
| 93 |
return 'bg-red-100 text-red-800 ring-1 ring-red-200'; |
| 94 |
case 'available': |
| 95 |
case 'sent': |
| 96 |
return 'bg-blue-100 text-blue-800 ring-1 ring-blue-200'; |
| 97 |
default: |
| 98 |
return 'bg-gray-100 text-gray-800 ring-1 ring-gray-200'; |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|