|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Inventory Tax Field Class. |
|
5
|
|
|
* |
|
6
|
|
|
* @package InventoryField |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright YetiForce S.A. |
|
9
|
|
|
* @license YetiForce Public License 5.0 (licenses/LicenseEN.txt or yetiforce.com) |
|
10
|
|
|
* @author Mariusz Krzaczkowski <[email protected]> |
|
11
|
|
|
* @author Radosław Skrzypczak <[email protected]> |
|
12
|
|
|
*/ |
|
13
|
|
|
class Vtiger_Tax_InventoryField extends Vtiger_Basic_InventoryField |
|
14
|
|
|
{ |
|
15
|
|
|
protected $type = 'Tax'; |
|
16
|
|
|
protected $defaultLabel = 'LBL_TAX'; |
|
17
|
|
|
protected $defaultValue = 0; |
|
18
|
|
|
protected $columnName = 'tax'; |
|
19
|
|
|
protected $dbType = 'decimal(28,8) DEFAULT 0'; |
|
20
|
|
|
protected $customColumn = [ |
|
21
|
|
|
'taxparam' => 'string' |
|
22
|
|
|
]; |
|
23
|
|
|
protected $summationValue = true; |
|
24
|
|
|
protected $maximumLength = '99999999999999999999'; |
|
25
|
|
|
protected $customMaximumLength = [ |
|
26
|
|
|
'taxparam' => 255 |
|
27
|
|
|
]; |
|
28
|
|
|
protected $purifyType = \App\Purifier::NUMBER; |
|
29
|
|
|
protected $customPurifyType = [ |
|
30
|
|
|
'taxparam' => \App\Purifier::TEXT |
|
31
|
|
|
]; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array List of shared fields |
|
34
|
|
|
*/ |
|
35
|
|
|
public $shared = ['taxparam' => 'tax_percent']; |
|
36
|
|
|
|
|
37
|
|
|
/** {@inheritdoc} */ |
|
38
|
|
|
public function getDisplayValue($value, array $rowData = [], bool $rawText = false) |
|
39
|
|
|
{ |
|
40
|
|
|
$value = \App\Fields\Currency::formatToDisplay($value, null, true); |
|
41
|
|
|
if (isset($rowData['currency']) && $currencySymbol = \App\Fields\Currency::getById($rowData['currency'])['currency_symbol'] ?? '') { |
|
42
|
|
|
$value = \CurrencyField::appendCurrencySymbol($value, $currencySymbol); |
|
|
|
|
|
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $value; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** {@inheritdoc} */ |
|
49
|
|
|
public function getEditValue(array $itemData, string $column = '') |
|
50
|
|
|
{ |
|
51
|
|
|
$value = parent::getEditValue($itemData, $column); |
|
52
|
|
|
if (!$column || $column === $this->getColumnName()) { |
|
53
|
|
|
$value = \App\Fields\Currency::formatToDisplay($value, null, true); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
2 |
|
return $value; |
|
57
|
|
|
} |
|
58
|
2 |
|
|
|
59
|
2 |
|
public function getClassName($data) |
|
60
|
2 |
|
{ |
|
61
|
2 |
|
if (\count($data) > 0 && 0 == $data[0]['taxmode']) { |
|
62
|
2 |
|
return 'hide'; |
|
63
|
2 |
|
} |
|
64
|
|
|
return ''; |
|
65
|
|
|
} |
|
66
|
1 |
|
|
|
67
|
|
|
/** {@inheritdoc} */ |
|
68
|
2 |
|
public function getDBValue($value, ?string $name = '') |
|
69
|
|
|
{ |
|
70
|
|
|
if ($name !== $this->getColumnName()) { |
|
71
|
|
|
$valid = $value ? \App\Json::decode($value) : []; |
|
72
|
|
|
if (isset($valid['individualTax'])) { |
|
73
|
|
|
$valid['individualTax'] = $valid['individualTax'] ?? 0; |
|
74
|
2 |
|
$valid['globalTax'] = $valid['globalTax'] ?? 0; |
|
75
|
|
|
$value = \App\Json::encode($valid); |
|
76
|
2 |
|
} |
|
77
|
2 |
|
} else { |
|
78
|
|
|
$value = App\Fields\Double::formatToDb($value); |
|
79
|
|
|
} |
|
80
|
|
|
return $value; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
2 |
|
/** {@inheritdoc} */ |
|
84
|
|
|
public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null) |
|
85
|
|
|
{ |
|
86
|
2 |
|
if ($columnName === $this->getColumnName()) { |
|
87
|
|
|
if ($isUserFormat) { |
|
88
|
|
|
$value = $this->getDBValue($value, $columnName); |
|
89
|
2 |
|
if (null !== $originalValue) { |
|
90
|
2 |
|
$originalValue = $this->getDBValue($originalValue, $columnName); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
2 |
|
if (!is_numeric($value)) { |
|
94
|
|
|
throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||$value", 406); |
|
95
|
|
|
} |
|
96
|
|
|
if ($this->maximumLength < $value || -$this->maximumLength > $value) { |
|
97
|
2 |
|
throw new \App\Exceptions\Security("ERR_VALUE_IS_TOO_LONG||$columnName||$value", 406); |
|
98
|
|
|
} |
|
99
|
|
|
if (null !== $originalValue && !\App\Validator::floatIsEqualUserCurrencyDecimals($value, $originalValue)) { |
|
100
|
|
|
throw new \App\Exceptions\Security('ERR_ILLEGAL_FIELD_VALUE||' . ($columnName ?? $this->getColumnName()) . "||{$this->getModuleName()}||$value($originalValue)", 406); |
|
101
|
|
|
} |
|
102
|
|
|
} else { |
|
103
|
|
|
if (App\TextUtils::getTextLength($value) > $this->customMaximumLength[$columnName]) { |
|
104
|
|
|
$module = $this->getModuleName(); |
|
105
|
|
|
throw new \App\Exceptions\Security("ERR_VALUE_IS_TOO_LONG||$columnName||$module||$value", 406); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* Get configuration parameters for taxes. |
|
112
|
|
|
* |
|
113
|
|
|
* @param string $taxParam String parameters json encode |
|
114
|
|
|
* @param float $net |
|
115
|
|
|
* @param array $return |
|
116
|
|
|
* |
|
117
|
|
|
* @return array |
|
118
|
|
|
*/ |
|
119
|
|
|
public function getTaxParam(string $taxParam, float $net, array $return = []): array |
|
120
|
|
|
{ |
|
121
|
|
|
$taxParam = json_decode($taxParam, true); |
|
122
|
|
|
if (empty($taxParam)) { |
|
123
|
|
|
return $return; |
|
124
|
|
|
} |
|
125
|
|
|
if (\is_string($taxParam['aggregationType'])) { |
|
126
|
|
|
$taxParam['aggregationType'] = [$taxParam['aggregationType']]; |
|
127
|
|
|
} |
|
128
|
|
|
if (isset($taxParam['aggregationType'])) { |
|
129
|
|
|
foreach ($taxParam['aggregationType'] as $aggregationType) { |
|
130
|
|
|
$percent = (string) ($taxParam[$aggregationType . 'Tax'] ?? 0); |
|
131
|
|
|
if (!isset($return[$percent])) { |
|
132
|
|
|
$return[$percent] = 0; |
|
133
|
|
|
} |
|
134
|
|
|
$return[$percent] += $net * ($percent / 100); |
|
135
|
2 |
|
} |
|
136
|
|
|
ksort($return); |
|
137
|
2 |
|
} |
|
138
|
2 |
|
|
|
139
|
2 |
|
return $return; |
|
140
|
2 |
|
} |
|
141
|
2 |
|
|
|
142
|
2 |
|
/** {@inheritdoc} */ |
|
143
|
|
|
public function getValueForSave(array $item, bool $userFormat = false, string $column = null) |
|
144
|
|
|
{ |
|
145
|
2 |
|
if ($column === $this->getColumnName() || null === $column) { |
|
146
|
|
|
$value = 0.0; |
|
147
|
2 |
|
if (!\App\Json::isEmpty($item['taxparam'] ?? '') && ($taxesConfig = \Vtiger_Inventory_Model::getTaxesConfig())) { |
|
148
|
|
|
$taxParam = \App\Json::decode($item['taxparam']); |
|
149
|
|
|
$netPrice = static::getInstance($this->getModuleName(), 'NetPrice')->getValueForSave($item, $userFormat); |
|
150
|
|
|
$value = $this->getTaxValue($taxParam, $netPrice, (int) $taxesConfig['aggregation']); |
|
151
|
|
|
} |
|
152
|
|
|
} else { |
|
153
|
|
|
$value = $userFormat ? $this->getDBValue($item[$column]) : $item[$column]; |
|
154
|
|
|
} |
|
155
|
|
|
return $value; |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
2 |
|
* Calculate the tax value. |
|
160
|
|
|
* |
|
161
|
2 |
|
* @param array $taxParam |
|
162
|
2 |
|
* @param float $netPrice |
|
163
|
2 |
|
* @param string $mode 0-can not be combined, 1-summary, 2-cascade |
|
164
|
2 |
|
* |
|
165
|
|
|
* @return float |
|
166
|
2 |
|
*/ |
|
167
|
2 |
|
public function getTaxValue(array $taxParam, float $netPrice, int $mode): float |
|
168
|
2 |
|
{ |
|
169
|
2 |
|
$value = 0.0; |
|
170
|
|
|
if ($taxParam) { |
|
|
|
|
|
|
171
|
|
|
$types = $taxParam['aggregationType']; |
|
172
|
|
|
if (!\is_array($types)) { |
|
173
|
|
|
$types = [$types]; |
|
174
|
2 |
|
} |
|
175
|
|
|
foreach ($types as $type) { |
|
176
|
|
|
$taxValue = $netPrice * $taxParam["{$type}Tax"] / 100.00; |
|
177
|
|
|
$value += $taxValue; |
|
178
|
|
|
if (2 === $mode) { |
|
179
|
|
|
$netPrice += $taxValue; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
|
183
|
|
|
return $value; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** {@inheritdoc} */ |
|
187
|
|
|
public function compare($value, $prevValue, string $column): bool |
|
188
|
|
|
{ |
|
189
|
|
|
return $column === $this->getColumnName() ? \App\Validator::floatIsEqual((float) $value, (float) $prevValue, 8) : parent::compare($value, $prevValue, $column); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|