Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
discount.php
1<?php
2namespace Bitrix\Catalog;
3
7
8Loc::loadMessages(__FILE__);
9
68class DiscountTable extends Main\Entity\DataManager
69{
70 const TYPE_DISCOUNT = 0;
72
73 const VALUE_TYPE_PERCENT = 'P';
74 const VALUE_TYPE_FIX = 'F';
75 const VALUE_TYPE_SALE = 'S';
76
80
84
88
92
93 const ACTUAL_VERSION = 2;
94 const OLD_VERSION = 1;
95
101 public static function getTableName()
102 {
103 return 'b_catalog_discount';
104 }
105
111 public static function getMap()
112 {
113 return array(
114 'ID' => new Main\Entity\IntegerField('ID', array(
115 'primary' => true,
116 'autocomplete' => true,
117 'title' => Loc::getMessage('DISCOUNT_ENTITY_ID_FIELD')
118 )),
119 'XML_ID' => new Main\Entity\StringField('XML_ID', array(
120 'validation' => array(__CLASS__, 'validateXmlId'),
121 'title' => Loc::getMessage('DISCOUNT_ENTITY_XML_ID_FIELD')
122 )),
123 'SITE_ID' => new Main\Entity\StringField('SITE_ID', array(
124 'required' => true,
125 'validation' => array(__CLASS__, 'validateSiteId'),
126 'title' => Loc::getMessage('DISCOUNT_ENTITY_SITE_ID_FIELD')
127 )),
128 'TYPE' => new Main\Entity\IntegerField('TYPE', array(
129 'required' => true,
130 'default_value' => self::TYPE_DISCOUNT,
131 'validation' => array(__CLASS__, 'validateType'),
132 'title' => Loc::getMessage('DISCOUNT_ENTITY_TYPE_FIELD')
133 )),
134 'ACTIVE' => new Main\Entity\BooleanField('ACTIVE', array(
135 'values' => array('N', 'Y'),
136 'default_value' => 'Y',
137 'title' => Loc::getMessage('DISCOUNT_ENTITY_ACTIVE_FIELD')
138 )),
139 'ACTIVE_FROM' => new Main\Entity\DatetimeField('ACTIVE_FROM', array(
140 'default_value' => null,
141 'title' => Loc::getMessage('DISCOUNT_ENTITY_ACTIVE_FROM_FIELD')
142 )),
143 'ACTIVE_TO' => new Main\Entity\DatetimeField('ACTIVE_TO', array(
144 'default_value' => null,
145 'title' => Loc::getMessage('DISCOUNT_ENTITY_ACTIVE_TO_FIELD')
146 )),
147 'RENEWAL' => new Main\Entity\BooleanField('RENEWAL', array(
148 'values' => array('N', 'Y'),
149 'default_value' => 'N',
150 'title' => Loc::getMessage('DISCOUNT_ENTITY_RENEWAL_FIELD')
151 )),
152 'NAME' => new Main\Entity\StringField('NAME', array(
153 'required' => true,
154 'validation' => array(__CLASS__, 'validateName'),
155 'title' => Loc::getMessage('DISCOUNT_ENTITY_NAME_FIELD')
156 )),
157 'SORT' => new Main\Entity\IntegerField('SORT', array(
158 'title' => Loc::getMessage('DISCOUNT_ENTITY_SORT_FIELD')
159 )),
160 'MAX_DISCOUNT' => new Main\Entity\FloatField('MAX_DISCOUNT', array(
161 'title' => Loc::getMessage('DISCOUNT_ENTITY_MAX_DISCOUNT_FIELD')
162 )),
163 'VALUE_TYPE' => new Main\Entity\EnumField('VALUE_TYPE', array(
164 'required' => true,
165 'values' => array(self::VALUE_TYPE_PERCENT, self::VALUE_TYPE_FIX, self::VALUE_TYPE_SALE),
166 'default_value' => self::VALUE_TYPE_PERCENT,
167 'title' => Loc::getMessage('DISCOUNT_ENTITY_VALUE_TYPE_FIELD')
168 )),
169 'VALUE' => new Main\Entity\FloatField('VALUE', array(
170 'required' => true,
171 'title' => Loc::getMessage('DISCOUNT_ENTITY_VALUE_FIELD')
172 )),
173 'CURRENCY' => new Main\Entity\StringField('CURRENCY', array(
174 'required' => true,
175 'validation' => array(__CLASS__, 'validateCurrency'),
176 'title' => Loc::getMessage('DISCOUNT_ENTITY_CURRENCY_FIELD')
177 )),
178 'TIMESTAMP_X' => new Main\Entity\DatetimeField('TIMESTAMP_X', array(
179 'required' => true,
180 'default_value' => function()
181 {
182 return new Main\Type\DateTime();
183 },
184 'title' => Loc::getMessage('DISCOUNT_ENTITY_TIMESTAMP_X_FIELD')
185 )),
186 'COUNT_PERIOD' => new Main\Entity\EnumField('COUNT_PERIOD', array(
187 'values' => array(self::COUNT_PERIOD_TYPE_ALL, self::COUNT_PERIOD_TYPE_INTERVAL, self::COUNT_PERIOD_TYPE_PERIOD),
188 'default_value' => self::COUNT_PERIOD_TYPE_ALL
189 )),
190 'COUNT_SIZE' => new Main\Entity\IntegerField('COUNT_SIZE', array(
191 'default_value' => 0
192 )),
193 'COUNT_TYPE' => new Main\Entity\EnumField('COUNT_TYPE', array(
194 'values' => array(self::COUNT_TYPE_SIZE_DAY, self::COUNT_TYPE_SIZE_MONTH, self::COUNT_TYPE_SIZE_YEAR),
195 'default_value' => self::COUNT_TYPE_SIZE_YEAR
196 )),
197 'COUNT_FROM' => new Main\Entity\DatetimeField('COUNT_FROM', array(
198 'default_value' => null
199 )),
200 'COUNT_TO' => new Main\Entity\DatetimeField('COUNT_TO', array(
201 'default_value' => null
202 )),
203 'ACTION_SIZE' => new Main\Entity\IntegerField('ACTION_SIZE', array(
204 'default_value' => 0
205 )),
206 'ACTION_TYPE' => new Main\Entity\EnumField('ACTION_TYPE', array(
207 'values' => array(self::ACTION_TYPE_SIZE_DAY, self::ACTION_TYPE_SIZE_MONTH, self::ACTION_TYPE_SIZE_YEAR),
208 'default_value' => self::ACTION_TYPE_SIZE_YEAR
209 )),
210 'MODIFIED_BY' => new Main\Entity\IntegerField('MODIFIED_BY', array(
211 'title' => Loc::getMessage('DISCOUNT_ENTITY_MODIFIED_BY_FIELD')
212 )),
213 'DATE_CREATE' => new Main\Entity\DatetimeField('DATE_CREATE', array(
214 'default_value' => null,
215 'title' => Loc::getMessage('DISCOUNT_ENTITY_DATE_CREATE_FIELD')
216 )),
217 'CREATED_BY' => new Main\Entity\IntegerField('CREATED_BY', array(
218 'title' => Loc::getMessage('DISCOUNT_ENTITY_CREATED_BY_FIELD')
219 )),
220 'PRIORITY' => new Main\Entity\IntegerField('PRIORITY', array(
221 'default_value' => 1,
222 'title' => Loc::getMessage('DISCOUNT_ENTITY_PRIORITY_FIELD')
223 )),
224 'LAST_DISCOUNT' => new Main\Entity\BooleanField('LAST_DISCOUNT', array(
225 'values' => array('N', 'Y'),
226 'default_value' => 'Y',
227 'title' => Loc::getMessage('DISCOUNT_ENTITY_LAST_DISCOUNT_FIELD')
228 )),
229 'VERSION' => new Main\Entity\EnumField('VERSION', array(
230 'values' => array(self::OLD_VERSION, self::ACTUAL_VERSION),
231 'default_value' => self::ACTUAL_VERSION
232 )),
233 'NOTES' => new Main\Entity\StringField('NOTES', array(
234 'validation' => array(__CLASS__, 'validateNotes'),
235 'title' => Loc::getMessage('DISCOUNT_ENTITY_NOTES_FIELD')
236 )),
237 'CONDITIONS' => new Main\Entity\TextField('CONDITIONS', array()),
238 'CONDITIONS_LIST' => new Main\Entity\TextField('CONDITIONS_LIST', array(
239 'serialized' => true,
240 'column_name' => 'CONDITIONS',
241 'title' => Loc::getMessage('DISCOUNT_ENTITY_CONDITIONS_LIST_FIELD')
242 )),
243 'UNPACK' => new Main\Entity\TextField('UNPACK', array()),
244 'USE_COUPONS' => new Main\Entity\BooleanField('USE_COUPONS', array(
245 'values' => array('N', 'Y'),
246 'default_value' => 'N',
247 'title' => Loc::getMessage('DISCOUNT_ENTITY_USE_COUPONS_FIELD')
248 )),
249 'SALE_ID' => new Main\Entity\IntegerField('SALE_ID'),
250 'CREATED_BY_USER' => new Main\Entity\ReferenceField(
251 'CREATED_BY_USER',
252 '\Bitrix\Main\User',
253 array('=this.CREATED_BY' => 'ref.ID')
254 ),
255 'MODIFIED_BY_USER' => new Main\Entity\ReferenceField(
256 'MODIFIED_BY_USER',
257 '\Bitrix\Main\User',
258 array('=this.MODIFIED_BY' => 'ref.ID')
259 ),
260 'SALE_DISCOUNT' => new Main\Entity\ReferenceField(
261 'SALE_DISCOUNT',
262 'Bitrix\Sale\Internals\DiscountTable',
263 array('=this.SALE_ID' => 'ref.ID')
264 )
265 );
266 }
272 public static function validateXmlId()
273 {
274 return array(
275 new Main\Entity\Validator\Length(null, 255),
276 );
277 }
283 public static function validateSiteId()
284 {
285 return array(
286 new Main\Entity\Validator\Length(null, 2),
287 );
288 }
294 public static function validateType()
295 {
296 return array(
297 array(__CLASS__, 'checkType')
298 );
299 }
305 public static function validateName()
306 {
307 return array(
308 new Main\Entity\Validator\Length(null, 255),
309 );
310 }
316 public static function validateCurrency()
317 {
318 return array(
319 new Main\Entity\Validator\Length(null, 3),
320 );
321 }
327 public static function validateNotes()
328 {
329 return array(
330 new Main\Entity\Validator\Length(null, 255),
331 );
332 }
333
343 public static function checkType($value, $primary, array $row, Main\Entity\Field $field)
344 {
345 if (
346 $value == self::TYPE_DISCOUNT
347 || $value == self::TYPE_DISCOUNT_SAVE
348 )
349 {
350 return true;
351 }
352 return Loc::getMessage('DISCOUNT_ENTITY_VALIDATOR_TYPE');
353 }
354
361 public static function add(array $data)
362 {
363 $result = new Main\Entity\AddResult();
364 $result->addError(new Main\Entity\EntityError(
365 Loc::getMessage('CATALOG_DISCOUNT_ENTITY_MESS_ADD_BLOCKED')
366 ));
367 return $result;
368 }
369
377 public static function update($primary, array $data)
378 {
379 $result = new Main\Entity\UpdateResult();
380 $result->addError(new Main\Entity\EntityError(
381 Loc::getMessage('CATALOG_DISCOUNT_ENTITY_MESS_UPDATE_BLOCKED')
382 ));
383 return $result;
384 }
385
392 public static function delete($primary)
393 {
394 $result = new Main\Entity\DeleteResult();
395 $result->addError(new Main\Entity\EntityError(
396 Loc::getMessage('CATALOG_DISCOUNT_ENTITY_MESS_DELETE_BLOCKED')
397 ));
398 return $result;
399 }
400
408 public static function convertCurrency(&$discount, $currency)
409 {
410 $currency = Currency\CurrencyManager::checkCurrencyID($currency);
411 if ($currency === false || empty($discount) || !is_array($discount))
412 return;
413 if (!isset($discount['VALUE_TYPE']) || !isset($discount['CURRENCY']) || $discount['CURRENCY'] == $currency)
414 return;
415
416 switch ($discount['VALUE_TYPE'])
417 {
420 $discount['VALUE'] = round(
421 \CCurrencyRates::convertCurrency($discount['VALUE'], $discount['CURRENCY'], $currency)
422 );
423 $discount['CURRENCY'] = $currency;
424 break;
426 if ($discount['MAX_DISCOUNT'] > 0)
427 $discount['MAX_DISCOUNT'] = round(
428 \CCurrencyRates::convertCurrency($discount['MAX_DISCOUNT'], $discount['CURRENCY'], $currency)
429 );
430 $discount['CURRENCY'] = $currency;
431 break;
432 }
433 }
434
442 public static function setUseCoupons($discountList, $use)
443 {
444 if (!is_array($discountList))
445 $discountList = array($discountList);
446 $use = (string)$use;
447 if ($use !== 'Y' && $use !== 'N')
448 return;
449 Main\Type\Collection::normalizeArrayValuesByInt($discountList);
450 if (empty($discountList))
451 return;
452 $conn = Main\Application::getConnection();
453 $helper = $conn->getSqlHelper();
454 $conn->queryExecute(
455 'update '.$helper->quote(self::getTableName()).
456 ' set '.$helper->quote('USE_COUPONS').' = \''.$use.'\' where '.
457 $helper->quote('ID').' in ('.implode(',', $discountList).')'
458 );
459 unset($helper, $conn);
460 }
461}
static setUseCoupons($discountList, $use)
Definition discount.php:442
static add(array $data)
Definition discount.php:361
static convertCurrency(&$discount, $currency)
Definition discount.php:408
static checkType($value, $primary, array $row, Main\Entity\Field $field)
Definition discount.php:343
static update($primary, array $data)
Definition discount.php:377
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29