PluginProbe
Easy Invoice – Invoice Generator, PDF Quotes & Payments / 2.3.7
Easy Invoice – Invoice Generator, PDF Quotes & Payments v2.3.7
2.4.0 2.4.1 2.3.8 2.3.7 2.3.6 2.3.5 2.3.4 2.3.3 2.3.2 2.3.1 2.2.0 2.1.21 2.1.20 2.1.19 2.1.18 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.13 2.1.14 2.1.15 2.1.16 2.1.2 All 57 releases
easy-invoice / includes / Traits / CurrencyTrait.php

CurrencyTrait.php in Easy Invoice – Invoice Generator, PDF Quotes & Payments 2.3.7, at includes/Traits/CurrencyTrait.php

88 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Currency Trait
4 *
5 * @package Easy_Invoice
6 * @subpackage Traits
7 */
8
9 namespace EasyInvoice\Traits;
10
11 use EasyInvoice\Controllers\SettingsController;
12 use EasyInvoice\Helpers\CurrencyHelper;
13
14 /**
15 * CurrencyTrait contains methods for handling currency formatting
16 */
17 trait CurrencyTrait {
18 /**
19 * Format currency amount
20 *
21 * @param float $amount Amount to format
22 * @param mixed $invoice Invoice object or null (to use invoice-specific currency settings)
23 * @param string $currency_symbol Fallback currency symbol
24 * @return string Formatted amount
25 */
26 protected function formatCurrency($amount, $invoice = null, $currency_symbol = '$') {
27 $settings_controller = new SettingsController();
28 $settings = $settings_controller->getSettings();
29
30 // Get currency settings - prioritize invoice-specific settings over global settings
31 $currency_code = null;
32 $currency_position = null;
33
34 if ($invoice && method_exists($invoice, 'getCurrencyCode') && method_exists($invoice, 'getCurrencyPosition')) {
35 $currency_code = $invoice->getCurrencyCode();
36 $currency_position = $invoice->getCurrencyPosition();
37 }
38
39 // If invoice doesn't have currency settings or they're empty, use global settings
40 if (empty($currency_code) || $currency_code === 'global') {
41 $currency_code = $settings['easy_invoice_currency_code'] ?? 'USD';
42 }
43
44 if (empty($currency_position) || $currency_position === 'global') {
45 $currency_position = $settings['easy_invoice_currency_position'] ?? 'before';
46 }
47
48 // Ensure currency code is uppercase for consistent lookup
49 $currency_code = strtoupper($currency_code);
50
51 // Get currency symbol from CurrencyHelper based on currency code
52 $symbol = CurrencyHelper::getCurrencySymbol($currency_code);
53
54 // Get currency symbol type setting
55 $currency_symbol_type = $settings['easy_invoice_currency_symbol_type'] ?? 'symbol';
56
57 // Use currency code instead of symbol if setting is 'code'
58 $display_symbol = ($currency_symbol_type === 'code') ? $currency_code : $symbol;
59
60 // Get other formatting settings from global settings
61 $thousands_separator = $settings['easy_invoice_thousands_separator'] ?? ',';
62 $decimal_separator = $settings['easy_invoice_decimal_separator'] ?? '.';
63 $precision = intval($settings['easy_invoice_decimal_precision'] ?? 2);
64
65 $formatted = number_format(
66 floatval($amount),
67 $precision,
68 $decimal_separator,
69 $thousands_separator
70 );
71
72 // Handle different currency positions
73 switch ($currency_position) {
74 case 'left':
75 case 'before':
76 return $display_symbol . $formatted;
77 case 'right':
78 case 'after':
79 return $formatted . $display_symbol;
80 case 'left_space':
81 return $display_symbol . ' ' . $formatted;
82 case 'right_space':
83 return $formatted . ' ' . $display_symbol;
84 default:
85 return $display_symbol . $formatted; // Default to left
86 }
87 }
88 }