Passed
Push — developer ( e47c33...9f9df1 )
by Radosław
19:35
created

Vtiger_Quantity_InventoryField::getEditValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 2
1
<?php
2
3
 /**
4
  * Inventory Quantity 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
14
 use App\Fields\Double;
15
16
class Vtiger_Quantity_InventoryField extends Vtiger_Basic_InventoryField
17
{
18
	protected $type = 'Quantity';
19
	protected $defaultLabel = 'LBL_QUANTITY';
20
	protected $defaultValue = '1';
21
	protected $columnName = 'qty';
22
	protected $dbType = 'decimal(25,3) DEFAULT 0';
23
	protected $maximumLength = '9999999999999999999999';
24
	protected $purifyType = \App\Purifier::NUMBER;
25
26
	/** {@inheritdoc} */
27
	public function getDisplayValue($value, array $rowData = [], bool $rawText = false)
28
	{
29
		return \App\Fields\Double::formatToDisplay($value, Double::FORMAT_TRUNCATE_TRAILING_ZEROS);
30
	}
31
32
	/** {@inheritdoc} */
33
	public function getEditValue(array $itemData, string $column = '')
34
	{
35
		$value = parent::getEditValue($itemData, $column);
36
		return \App\Fields\Double::formatToDisplay($value, Double::FORMAT_TRUNCATE_TRAILING_ZEROS);
37
	}
38
39
	/** {@inheritdoc} */
40
	public function getDBValue($value, ?string $name = '')
41
	{
42 2
		if (!isset($this->dbValue["{$value}"])) {
43
			$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...
44 2
		}
45 2
		return $this->dbValue["{$value}"];
46
	}
47 2
48
	/** {@inheritdoc} */
49
	public function validate($value, string $columnName, bool $isUserFormat, $originalValue = null)
50
	{
51
		if ($isUserFormat) {
52
			$value = $this->getDBValue($value, $columnName);
53 2
		}
54
		if (!is_numeric($value)) {
55 2
			throw new \App\Exceptions\Security("ERR_ILLEGAL_FIELD_VALUE||$columnName||$value", 406);
56
		}
57
		if ($this->maximumLength < $value || -$this->maximumLength > $value) {
58 2
			throw new \App\Exceptions\Security("ERR_VALUE_IS_TOO_LONG||$columnName||$value", 406);
59
		}
60
	}
61 2
62
	/** {@inheritdoc} */
63
	public function compare($value, $prevValue, string $column): bool
64 2
	{
65
		return \App\Validator::floatIsEqual((float) $value, (float) $prevValue, 3);
66
	}
67
}
68