Passed
Push — developer ( 5a7d78...6a35f4 )
by Radosław
17:06
created

Vtiger_Tax_InventoryField::getTaxParam()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 15.2788

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 21
ccs 4
cts 11
cp 0.3636
rs 9.2222
c 0
b 0
f 0
cc 6
nc 5
nop 3
crap 15.2788
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);
0 ignored issues
show
Bug introduced by
$value of type string is incompatible with the type double|integer expected by parameter $currencyValue of CurrencyField::appendCurrencySymbol(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
			$value = \CurrencyField::appendCurrencySymbol(/** @scrutinizer ignore-type */ $value, $currencySymbol);
Loading history...
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $taxParam of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
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