|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @license LGPLv3, https://opensource.org/licenses/LGPL-3.0 |
|
5
|
|
|
* @copyright Aimeos (aimeos.org), 2016-2024 |
|
6
|
|
|
* @package MShop |
|
7
|
|
|
* @subpackage Price |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace Aimeos\MShop\Price\Item; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Basic methods for all price implementations |
|
16
|
|
|
* |
|
17
|
|
|
* @package MShop |
|
18
|
|
|
* @subpackage Price |
|
19
|
|
|
*/ |
|
20
|
|
|
abstract class Base |
|
21
|
|
|
extends \Aimeos\MShop\Common\Item\Base |
|
22
|
|
|
implements \Aimeos\MShop\Price\Item\Iface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* Compares the properties of the given price item with its own one. |
|
26
|
|
|
* |
|
27
|
|
|
* This method compare only the essential price properties: |
|
28
|
|
|
* * Value |
|
29
|
|
|
* * Costs |
|
30
|
|
|
* * Rebate |
|
31
|
|
|
* * Tax rate |
|
32
|
|
|
* * Tax flag |
|
33
|
|
|
* * Quantity |
|
34
|
|
|
* * Currency ID |
|
35
|
|
|
* |
|
36
|
|
|
* All other item properties are not compared. |
|
37
|
|
|
* |
|
38
|
|
|
* @param \Aimeos\MShop\Price\Item\Iface $price Price item to compare with |
|
39
|
|
|
* @return bool True if equal, false if not |
|
40
|
|
|
* @since 2014.09 |
|
41
|
|
|
*/ |
|
42
|
|
|
public function compare( \Aimeos\MShop\Price\Item\Iface $price ) : bool |
|
43
|
|
|
{ |
|
44
|
|
|
if( $this->getValue() === $price->getValue() |
|
45
|
|
|
&& $this->getCosts() === $price->getCosts() |
|
46
|
|
|
&& $this->getRebate() === $price->getRebate() |
|
47
|
|
|
&& $this->getTaxFlag() === $price->getTaxFlag() |
|
48
|
|
|
&& $this->getTaxRate() === $price->getTaxRate() |
|
49
|
|
|
&& $this->getTaxRates() === $price->getTaxRates() |
|
50
|
|
|
&& $this->getQuantity() === $price->getQuantity() |
|
51
|
|
|
&& $this->getCurrencyId() === $price->getCurrencyId() |
|
52
|
|
|
) { |
|
53
|
|
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Tests if the price is within the requirements. |
|
62
|
|
|
* |
|
63
|
|
|
* @param string|int|float|null $value Monetary value |
|
64
|
|
|
* @param int|null $precision Number of decimal digits, null for default value |
|
65
|
|
|
* @return string|null Sanitized monetary value |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function checkPrice( $value, int $precision = null ) : ?string |
|
68
|
|
|
{ |
|
69
|
|
|
if( $value != '' && !is_numeric( $value ) ) { |
|
70
|
|
|
throw new \Aimeos\MShop\Price\Exception( sprintf( 'Invalid characters in price "%1$s"', $value ) ); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return $this->formatNumber( $value !== '' ? $value : null, $precision ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Formats the money value. |
|
79
|
|
|
* |
|
80
|
|
|
* @param string|int|float|null $number Money value |
|
81
|
|
|
* @param int|null $precision Number of decimal digits, null for default value |
|
82
|
|
|
* @return string|null Formatted money value |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function formatNumber( $number, int $precision = null ) : ?string |
|
85
|
|
|
{ |
|
86
|
|
|
return $number !== null ? number_format( $number, $precision ?: $this->getPrecision(), '.', '' ) : null; |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|