| 1 |
<?php |
| 2 |
/** |
| 3 |
* Money formatting helpers for templates. |
| 4 |
* |
| 5 |
* One way to print an amount everywhere — symbol or code, its position, |
| 6 |
* separators and precision from Settings → Currency, or from the document |
| 7 |
* when it carries its own currency. |
| 8 |
* |
| 9 |
* @package EasyInvoice |
| 10 |
* @since 2.4.0 |
| 11 |
*/ |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
if ( ! function_exists( 'easy_invoice_format_money' ) ) { |
| 18 |
/** |
| 19 |
* Format an amount in the document's currency, or the site's. |
| 20 |
* |
| 21 |
* @param float|int|string $amount Amount. |
| 22 |
* @param object|null $document Invoice or quote model, when there is one. |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
function easy_invoice_format_money( $amount, $document = null, string $currency_code = '' ): string { |
| 26 |
if ( '' !== $currency_code && ! $document ) { |
| 27 |
// A bare currency (a dashboard total in EUR on a USD site): the |
| 28 |
// formatter reads the code and position off a document, so hand |
| 29 |
// it a stand-in carrying just those. |
| 30 |
$document = new class( strtoupper( $currency_code ) ) { |
| 31 |
/** @var string */ |
| 32 |
private $code; |
| 33 |
public function __construct( string $code ) { $this->code = $code; } |
| 34 |
public function getCurrencyCode(): string { return $this->code; } |
| 35 |
public function getCurrencyPosition(): string { return 'global'; } |
| 36 |
}; |
| 37 |
} |
| 38 |
$formatter = ( $document && is_callable( [ $document, 'getTotal' ] ) && strpos( get_class( $document ), 'Quote' ) !== false ) |
| 39 |
? new \EasyInvoice\Helpers\QuoteFormatter( $document ) |
| 40 |
: new \EasyInvoice\Helpers\InvoiceFormatter( $document ); |
| 41 |
return (string) $formatter->format( (float) $amount ); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
if ( ! function_exists( 'easy_invoice_money_format_spec' ) ) { |
| 46 |
/** |
| 47 |
* The pieces a script needs to format money the same way as PHP. |
| 48 |
* |
| 49 |
* @param object|null $document Invoice model, when there is one. |
| 50 |
* @return array{symbol:string,position:string,thousands:string,decimal:string,precision:int} |
| 51 |
*/ |
| 52 |
function easy_invoice_money_format_spec( $document = null ): array { |
| 53 |
$code = $document && is_callable( [ $document, 'getCurrencyCode' ] ) ? (string) $document->getCurrencyCode() : ''; |
| 54 |
$position = $document && is_callable( [ $document, 'getCurrencyPosition' ] ) ? (string) $document->getCurrencyPosition() : ''; |
| 55 |
if ( '' === $code || 'global' === $code ) { |
| 56 |
$code = (string) get_option( 'easy_invoice_currency_code', 'USD' ); |
| 57 |
} |
| 58 |
if ( '' === $position || 'global' === $position ) { |
| 59 |
$position = (string) get_option( 'easy_invoice_currency_position', 'before' ); |
| 60 |
} |
| 61 |
$code = strtoupper( $code ); |
| 62 |
$symbol = 'code' === get_option( 'easy_invoice_currency_symbol_type', 'symbol' ) |
| 63 |
? $code |
| 64 |
: \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol( $code ); |
| 65 |
return [ |
| 66 |
'symbol' => $symbol, |
| 67 |
'position' => $position, |
| 68 |
'thousands' => (string) get_option( 'easy_invoice_thousands_separator', ',' ), |
| 69 |
'decimal' => (string) get_option( 'easy_invoice_decimal_separator', '.' ), |
| 70 |
'precision' => (int) get_option( 'easy_invoice_decimal_precision', 2 ), |
| 71 |
]; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
if ( ! function_exists( 'easy_invoice_round_money' ) ) { |
| 76 |
/** |
| 77 |
* Round a money amount to cents, half away from zero — the same rule |
| 78 |
* the builder's JavaScript uses, so a line, its subtotal and the |
| 79 |
* total printed on the document always add up. |
| 80 |
* |
| 81 |
* @param float|int|string $amount |
| 82 |
* @return float |
| 83 |
*/ |
| 84 |
function easy_invoice_round_money( $amount ): float { |
| 85 |
return round( (float) $amount, 2 ); |
| 86 |
} |
| 87 |
} |
| 88 |
|