code = $code; } public function getCurrencyCode(): string { return $this->code; } public function getCurrencyPosition(): string { return 'global'; } }; } $formatter = ( $document && is_callable( [ $document, 'getTotal' ] ) && strpos( get_class( $document ), 'Quote' ) !== false ) ? new \EasyInvoice\Helpers\QuoteFormatter( $document ) : new \EasyInvoice\Helpers\InvoiceFormatter( $document ); return (string) $formatter->format( (float) $amount ); } } if ( ! function_exists( 'easy_invoice_money_format_spec' ) ) { /** * The pieces a script needs to format money the same way as PHP. * * @param object|null $document Invoice model, when there is one. * @return array{symbol:string,position:string,thousands:string,decimal:string,precision:int} */ function easy_invoice_money_format_spec( $document = null ): array { $code = $document && is_callable( [ $document, 'getCurrencyCode' ] ) ? (string) $document->getCurrencyCode() : ''; $position = $document && is_callable( [ $document, 'getCurrencyPosition' ] ) ? (string) $document->getCurrencyPosition() : ''; if ( '' === $code || 'global' === $code ) { $code = (string) get_option( 'easy_invoice_currency_code', 'USD' ); } if ( '' === $position || 'global' === $position ) { $position = (string) get_option( 'easy_invoice_currency_position', 'before' ); } $code = strtoupper( $code ); $symbol = 'code' === get_option( 'easy_invoice_currency_symbol_type', 'symbol' ) ? $code : \EasyInvoice\Helpers\CurrencyHelper::getCurrencySymbol( $code ); return [ 'symbol' => $symbol, 'position' => $position, 'thousands' => (string) get_option( 'easy_invoice_thousands_separator', ',' ), 'decimal' => (string) get_option( 'easy_invoice_decimal_separator', '.' ), 'precision' => (int) get_option( 'easy_invoice_decimal_precision', 2 ), ]; } } if ( ! function_exists( 'easy_invoice_round_money' ) ) { /** * Round a money amount to cents, half away from zero — the same rule * the builder's JavaScript uses, so a line, its subtotal and the * total printed on the document always add up. * * @param float|int|string $amount * @return float */ function easy_invoice_round_money( $amount ): float { return round( (float) $amount, 2 ); } }