Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
calculation.php
1<?php
3
8
9Loc::loadMessages(__FILE__);
10
18{
20 const RESULT_MODE_RAW = 2;
21
22 protected static $config = array(
23 'CURRENCY' => null,
24 'PRECISION' => null,
25 'USE_DISCOUNTS' => true,
26 'RESULT_WITH_VAT' => true,
27 'RESULT_MODE' => self::RESULT_MODE_COMPONENT
28 );
29
30 private static $stack = array();
31
46 public static function setConfig(array $config)
47 {
48 $config = static::checkConfig($config);
49 if (empty($config))
50 return;
51
52 self::$config = array_merge(self::$config, $config);
53 }
54
60 public static function getConfig()
61 {
62 return self::$config;
63 }
64
70 public static function pushConfig()
71 {
72 array_push(self::$stack, static::getConfig());
73 }
74
80 public static function popConfig()
81 {
82 if (empty(self::$stack))
83 return;
84 static::setConfig(array_pop(self::$stack));
85 }
86
92 public static function getCurrency()
93 {
94 return (self::$config['CURRENCY'] !== null ? self::$config['CURRENCY'] : Currency\CurrencyManager::getBaseCurrency());
95 }
96
102 public static function getPrecision()
103 {
104 return (self::$config['PRECISION'] !== null ? self::$config['PRECISION'] : CATALOG_VALUE_PRECISION);
105 }
106
112 public static function isAllowedUseDiscounts()
113 {
114 return self::$config['USE_DISCOUNTS'];
115 }
116
122 public static function isIncludingVat()
123 {
124 return self::$config['RESULT_WITH_VAT'];
125 }
126
127 public static function getResultMode()
128 {
129 return self::$config['RESULT_MODE'];
130 }
131
132 public static function isComponentResultMode()
133 {
134 return self::$config['RESULT_MODE'] == self::RESULT_MODE_COMPONENT;
135 }
136
137 public static function isRawResultMode()
138 {
139 return self::$config['RESULT_MODE'] == self::RESULT_MODE_RAW;
140 }
141
148 public static function roundPrecision($value)
149 {
150 return round($value, static::getPrecision());
151 }
152
161 public static function compare($firstValue, $secondValue, $operator)
162 {
163 $firstValue = static::roundPrecision($firstValue);
164 $secondValue = static::roundPrecision($secondValue);
165
166 $result = false;
167 switch ($operator)
168 {
169 case '>':
170 $result = ($firstValue > $secondValue);
171 break;
172 case '>=':
173 $result = ($firstValue >= $secondValue);
174 break;
175 case '<':
176 $result = ($firstValue < $secondValue);
177 break;
178 case '<=':
179 $result = ($firstValue <= $secondValue);
180 break;
181 case '==':
182 $result = ($firstValue == $secondValue);
183 break;
184 case '!=':
185 $result = ($firstValue != $secondValue);
186 break;
187 }
188 return $result;
189 }
190
198 protected static function checkConfig(array $config)
199 {
200 $result = array();
201
202 $config = array_intersect_key($config, self::$config);
203 if (!empty($config))
204 {
205 foreach ($config as $field => $value)
206 {
207 $checked = true;
208 switch ($field)
209 {
210 case 'CURRENCY':
211 if ($value !== null)
212 {
213 $value = (string)$value;
214 $checked = Currency\CurrencyManager::isCurrencyExist($value);
215 }
216 break;
217 case 'PRECISION':
218 if ($value !== null)
219 {
220 $value = (int)$value;
221 $checked = ($value > 0);
222 }
223 break;
224 case 'USE_DISCOUNTS':
225 case 'RESULT_WITH_VAT':
226 $checked = is_bool($value);
227 break;
228 case 'RESULT_MODE':
229 $value = (int)$value;
230 $checked = ($value == self::RESULT_MODE_COMPONENT || $value == self::RESULT_MODE_RAW);
231 break;
232 default:
233 break;
234 }
235 if ($checked)
236 $result[$field] = $value;
237 }
238 unset($field, $value);
239 }
240
241 return $result;
242 }
243}
static compare($firstValue, $secondValue, $operator)
static loadMessages($file)
Definition loc.php:64