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

Vtiger_Margin_InventoryField::getValueForSave()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
/**
4
 * Inventory Margin 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_Margin_InventoryField extends Vtiger_Basic_InventoryField
14
{
15
	protected $type = 'Margin';
16
	protected $defaultLabel = 'LBL_MARGIN';
17
	protected $defaultValue = 0;
18
	protected $columnName = 'margin';
19
	protected $dbType = 'decimal(28,8) DEFAULT 0';
20
	protected $summationValue = true;
21
	protected $maximumLength = '99999999999999999999';
22
	protected $purifyType = \App\Purifier::NUMBER;
23
24
	/** {@inheritdoc} */
25
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
26
	{
27
		$value = \App\Fields\Double::formatToDisplay($value);
28
		if (isset($rowData['currency']) && $currencySymbol = \App\Fields\Currency::getById($rowData['currency'])['currency_symbol'] ?? '') {
29
			$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

29
			$value = \CurrencyField::appendCurrencySymbol(/** @scrutinizer ignore-type */ $value, $currencySymbol);
Loading history...
30
		}
31
32
		return $value;
33
	}
34
35
	/** {@inheritdoc} */
36
	public function getEditValue(array $itemData, string $column = '')
37
	{
38
		$value = parent::getEditValue($itemData, $column);
39
		return \App\Fields\Double::formatToDisplay($value, false);
40
	}
41
42
	/** {@inheritdoc} */
43 1
	public function getDBValue($value, ?string $name = '')
44
	{
45 1
		if (!isset($this->dbValue["{$value}"])) {
46 1
			$this->dbValue["{$value}"] = App\Fields\Double::formatToDb($value);
0 ignored issues
show
Bug Best Practice introduced by
The property dbValue does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
47
		}
48 1
		return $this->dbValue["{$value}"];
49
	}
50
51
	/** {@inheritdoc} */
52
	public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null)
53
	{
54 2
		if ($isUserFormat) {
55
			$value = $this->getDBValue($value, $columnName);
56 2
			if (null !== $originalValue) {
57
				$originalValue = $this->getDBValue($originalValue, $columnName);
58
			}
59
		}
60
		if (!is_numeric($value)) {
61
			throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||$value", 406);
62 2
		}
63
		if ($this->maximumLength < $value || -$this->maximumLength > $value) {
64
			throw new \App\Exceptions\Security("ERR_VALUE_IS_TOO_LONG||$columnName||$value", 406);
65 2
		}
66
		if (null !== $originalValue && !\App\Validator::floatIsEqualUserCurrencyDecimals($value, $originalValue)) {
67
			throw new \App\Exceptions\Security('ERR_ILLEGAL_FIELD_VALUE||' . ($columnName ?? $this->getColumnName()) . "||{$this->getModuleName()}||$value($originalValue)", 406);
68 2
		}
69
	}
70
71 2
	/** {@inheritdoc} */
72
	public function getValueForSave(array $item, bool $userFormat = false, string $column = null)
73
	{
74
		$quantity = static::getInstance($this->getModuleName(), 'Quantity')->getValueForSave($item, $userFormat);
75
		$purchase = static::getInstance($this->getModuleName(), 'Purchase')->getValueForSave($item, $userFormat);
76 2
		$netPrice = static::getInstance($this->getModuleName(), 'NetPrice')->getValueForSave($item, $userFormat);
77
		return $netPrice - ($purchase * $quantity);
78 2
	}
79 2
80 2
	/** {@inheritdoc} */
81 2
	public function compare($value, $prevValue, string $column): bool
82
	{
83
		return \App\Validator::floatIsEqual((float) $value, (float) $prevValue, 8);
84
	}
85
}
86