|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Tools for Double class. |
|
4
|
|
|
* |
|
5
|
|
|
* @package App |
|
6
|
|
|
* |
|
7
|
|
|
* @copyright YetiForce S.A. |
|
8
|
|
|
* @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
9
|
|
|
* @author Tomasz Kur <[email protected]> |
|
10
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace App\Fields; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Double class. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Double |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var int User format without rounding */ |
|
21
|
|
|
public const FORMAT_USER_WITHOUT_ROUNDING = 0; |
|
22
|
|
|
/** @var int Rounds num to specified precision */ |
|
23
|
|
|
public const FORMAT_ROUND = 1; |
|
24
|
1924 |
|
/** @var int Truncate trailing zeros */ |
|
25
|
|
|
public const FORMAT_TRUNCATE_TRAILING_ZEROS = 2; |
|
26
|
1924 |
|
/** @var int Show digits up to precision */ |
|
27
|
1924 |
|
public const FORMAT_DIGITS_UP_TO_PRECISION = 4; |
|
28
|
2 |
|
|
|
29
|
|
|
/** |
|
30
|
1924 |
|
* Function to truncate zeros. |
|
31
|
1924 |
|
* |
|
32
|
324 |
|
* @param string $value |
|
33
|
324 |
|
* @param int $precision |
|
34
|
|
|
* |
|
35
|
1602 |
|
* @return string |
|
36
|
1602 |
|
*/ |
|
37
|
|
|
public static function truncateZeros(string $value, int $precision = 0) |
|
38
|
|
|
{ |
|
39
|
1924 |
|
$seperator = \App\User::getCurrentUserModel()->getDetail('currency_decimal_separator'); |
|
40
|
1924 |
|
if (false === strpos($value, $seperator)) { |
|
41
|
|
|
return $value; |
|
42
|
1924 |
|
} |
|
43
|
|
|
for ($i = \strlen($value) - 1; $i >= 0; --$i) { |
|
44
|
|
|
if ($value[$i] === $seperator) { |
|
45
|
|
|
--$i; |
|
46
|
|
|
break; |
|
47
|
|
|
} |
|
48
|
|
|
if (isset($value[$i - $precision]) && $value[$i - $precision] === $seperator) { |
|
49
|
|
|
break; |
|
50
|
|
|
} |
|
51
|
|
|
if ('0' !== $value[$i]) { |
|
52
|
|
|
break; |
|
53
|
3844 |
|
} |
|
54
|
|
|
} |
|
55
|
3844 |
|
if (-1 !== $i) { |
|
56
|
|
|
$value = substr($value, 0, $i + 1); |
|
57
|
|
|
} |
|
58
|
3844 |
|
|
|
59
|
3844 |
|
return $value; |
|
60
|
3841 |
|
} |
|
61
|
|
|
|
|
62
|
3844 |
|
/** |
|
63
|
|
|
* Function to display number in user format. |
|
64
|
3844 |
|
* |
|
65
|
3844 |
|
* @param string|null $value |
|
66
|
3844 |
|
* @param int $fix A bitmask of one or more of the mode flags |
|
67
|
1924 |
|
* |
|
68
|
1920 |
|
* @return string |
|
69
|
1600 |
|
*/ |
|
70
|
|
|
public static function formatToDisplay(?string $value, $fix = self::FORMAT_ROUND): string |
|
71
|
3844 |
|
{ |
|
72
|
|
|
if (empty($value)) { |
|
73
|
|
|
$value = 0; |
|
74
|
|
|
} |
|
75
|
|
|
$userModel = \App\User::getCurrentUserModel(); |
|
76
|
|
|
if ($fix & self::FORMAT_ROUND) { |
|
77
|
|
|
$value = number_format((float) $value, $userModel->getDetail('no_of_currency_decimals'), '.', ''); |
|
78
|
|
|
} |
|
79
|
|
|
[$integer, $decimal] = array_pad(explode('.', $value, 2), 2, false); |
|
80
|
|
|
|
|
81
|
13 |
|
$display = Integer::formatToDisplay($integer); |
|
82
|
|
|
$decimalSeperator = $userModel->getDetail('currency_decimal_separator'); |
|
83
|
13 |
|
if ($userModel->getDetail('truncate_trailing_zeros')) { |
|
84
|
11 |
|
$display = static::truncateZeros($display . $decimalSeperator . $decimal); |
|
85
|
|
|
} elseif ($fix & self::FORMAT_TRUNCATE_TRAILING_ZEROS) { |
|
86
|
4 |
|
$display = static::truncateZeros($display . $decimalSeperator . $decimal, ($fix & self::FORMAT_DIGITS_UP_TO_PRECISION) ? $userModel->getDetail('no_of_currency_decimals') : 0); |
|
87
|
4 |
|
} elseif ($decimal) { |
|
88
|
4 |
|
$display .= $decimalSeperator . $decimal; |
|
89
|
4 |
|
} |
|
90
|
4 |
|
|
|
91
|
4 |
|
return $display; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Convert number to format for database. |
|
96
|
|
|
* |
|
97
|
|
|
* @param string|null $value |
|
98
|
|
|
* |
|
99
|
|
|
* @return float |
|
100
|
|
|
*/ |
|
101
|
|
|
public static function formatToDb(?string $value): float |
|
102
|
|
|
{ |
|
103
|
|
|
if (empty($value)) { |
|
104
|
|
|
return 0; |
|
105
|
|
|
} |
|
106
|
|
|
$userModel = \App\User::getCurrentUserModel(); |
|
107
|
|
|
$decimalSeperator = $userModel->getDetail('currency_decimal_separator'); |
|
108
|
|
|
$groupSeperator = $userModel->getDetail('currency_grouping_separator'); |
|
109
|
|
|
$value = str_replace($groupSeperator, '', $value); |
|
110
|
|
|
$value = str_replace($decimalSeperator, '.', $value); |
|
111
|
|
|
return (float) preg_replace('/[^0-9\.-]/', '', $value); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|