PageRenderTime 32ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/bitrix/modules/catalog/lib/discount.php

https://gitlab.com/Rad1calDreamer/honey
PHP | 426 lines | 284 code | 18 blank | 124 comment | 13 complexity | e18c3140d12af412c7afc5b5565b5829 MD5 | raw file
  1. <?php
  2. namespace Bitrix\Catalog;
  3. use Bitrix\Main;
  4. use Bitrix\Main\Localization\Loc;
  5. use Bitrix\Currency;
  6. Loc::loadMessages(__FILE__);
  7. /**
  8. * Class DiscountTable
  9. *
  10. * Fields:
  11. * <ul>
  12. * <li> ID int mandatory
  13. * <li> XML_ID string(255) optional
  14. * <li> SITE_ID string(2) mandatory
  15. * <li> TYPE int mandatory
  16. * <li> ACTIVE bool optional default 'Y'
  17. * <li> ACTIVE_FROM datetime optional
  18. * <li> ACTIVE_TO datetime optional
  19. * <li> RENEWAL bool optional default 'N'
  20. * <li> NAME string(255) optional
  21. * <li> MAX_USES int mandatory
  22. * <li> COUNT_USES int mandatory
  23. * <li> COUPON string(20) optional
  24. * <li> SORT int optional default 100
  25. * <li> MAX_DISCOUNT double optional
  26. * <li> VALUE_TYPE string(1) mandatory default 'P'
  27. * <li> VALUE double mandatory default 0.0000
  28. * <li> CURRENCY string(3) mandatory
  29. * <li> MIN_ORDER_SUM double optional default 0.0000
  30. * <li> TIMESTAMP_X datetime mandatory default 'CURRENT_TIMESTAMP'
  31. * <li> COUNT_PERIOD string(1) mandatory default 'U'
  32. * <li> COUNT_SIZE int mandatory
  33. * <li> COUNT_TYPE bool optional default 'Y'
  34. * <li> COUNT_FROM datetime optional
  35. * <li> COUNT_TO datetime optional
  36. * <li> ACTION_SIZE int mandatory
  37. * <li> ACTION_TYPE bool optional default 'Y'
  38. * <li> MODIFIED_BY int optional
  39. * <li> DATE_CREATE datetime optional
  40. * <li> CREATED_BY int optional
  41. * <li> PRIORITY int optional default 1
  42. * <li> LAST_DISCOUNT bool optional default 'Y'
  43. * <li> VERSION int optional default 1
  44. * <li> NOTES string(255) optional
  45. * <li> CONDITIONS string optional
  46. * <li> UNPACK string optional
  47. * </ul>
  48. *
  49. * @package Bitrix\Catalog
  50. **/
  51. class DiscountTable extends Main\Entity\DataManager
  52. {
  53. const TYPE_DISCOUNT = 0;
  54. const TYPE_DISCOUNT_SAVE = 1;
  55. const VALUE_TYPE_PERCENT = 'P';
  56. const VALUE_TYPE_FIX = 'F';
  57. const VALUE_TYPE_SALE = 'S';
  58. const COUNT_PERIOD_TYPE_ALL = 'U';
  59. const COUNT_PERIOD_TYPE_INTERVAL = 'D';
  60. const COUNT_PERIOD_TYPE_PERIOD = 'P';
  61. const COUNT_TYPE_SIZE_DAY = 'D';
  62. const COUNT_TYPE_SIZE_MONTH ='M';
  63. const COUNT_TYPE_SIZE_YEAR = 'Y';
  64. const ACTION_PERIOD_TYPE_ALL = 'U';
  65. const ACTION_PERIOD_TYPE_INTERVAL = 'D';
  66. const ACTION_PERIOD_TYPE_PERIOD = 'P';
  67. const ACTION_TYPE_SIZE_DAY = 'D';
  68. const ACTION_TYPE_SIZE_MONTH ='M';
  69. const ACTION_TYPE_SIZE_YEAR = 'Y';
  70. const ACTUAL_VERSION = 2;
  71. const OLD_VERSION = 1;
  72. /**
  73. * Returns DB table name for entity.
  74. *
  75. * @return string
  76. */
  77. public static function getTableName()
  78. {
  79. return 'b_catalog_discount';
  80. }
  81. /**
  82. * Returns entity map definition.
  83. *
  84. * @return array
  85. */
  86. public static function getMap()
  87. {
  88. return array(
  89. 'ID' => new Main\Entity\IntegerField('ID', array(
  90. 'primary' => true,
  91. 'autocomplete' => true,
  92. 'title' => Loc::getMessage('DISCOUNT_ENTITY_ID_FIELD')
  93. )),
  94. 'XML_ID' => new Main\Entity\StringField('XML_ID', array(
  95. 'validation' => array(__CLASS__, 'validateXmlId'),
  96. 'title' => Loc::getMessage('DISCOUNT_ENTITY_XML_ID_FIELD')
  97. )),
  98. 'SITE_ID' => new Main\Entity\StringField('SITE_ID', array(
  99. 'required' => true,
  100. 'validation' => array(__CLASS__, 'validateSiteId'),
  101. 'title' => Loc::getMessage('DISCOUNT_ENTITY_SITE_ID_FIELD')
  102. )),
  103. 'TYPE' => new Main\Entity\IntegerField('TYPE', array(
  104. 'required' => true,
  105. 'default_value' => self::TYPE_DISCOUNT,
  106. 'validation' => array(__CLASS__, 'validateType'),
  107. 'title' => Loc::getMessage('DISCOUNT_ENTITY_TYPE_FIELD')
  108. )),
  109. 'ACTIVE' => new Main\Entity\BooleanField('ACTIVE', array(
  110. 'values' => array('N', 'Y'),
  111. 'default_value' => 'Y',
  112. 'title' => Loc::getMessage('DISCOUNT_ENTITY_ACTIVE_FIELD')
  113. )),
  114. 'ACTIVE_FROM' => new Main\Entity\DatetimeField('ACTIVE_FROM', array(
  115. 'default_value' => null,
  116. 'title' => Loc::getMessage('DISCOUNT_ENTITY_ACTIVE_FROM_FIELD')
  117. )),
  118. 'ACTIVE_TO' => new Main\Entity\DatetimeField('ACTIVE_TO', array(
  119. 'default_value' => null,
  120. 'title' => Loc::getMessage('DISCOUNT_ENTITY_ACTIVE_TO_FIELD')
  121. )),
  122. 'RENEWAL' => new Main\Entity\BooleanField('RENEWAL', array(
  123. 'values' => array('N', 'Y'),
  124. 'default_value' => 'N',
  125. 'title' => Loc::getMessage('DISCOUNT_ENTITY_RENEWAL_FIELD')
  126. )),
  127. 'NAME' => new Main\Entity\StringField('NAME', array(
  128. 'required' => true,
  129. 'validation' => array(__CLASS__, 'validateName'),
  130. 'title' => Loc::getMessage('DISCOUNT_ENTITY_NAME_FIELD')
  131. )),
  132. 'MAX_USES' => new Main\Entity\IntegerField('MAX_USES', array(
  133. 'default_value' => 0
  134. )),
  135. 'COUNT_USES' => new Main\Entity\IntegerField('COUNT_USES', array(
  136. 'default_value' => 0
  137. )),
  138. 'COUPON' => new Main\Entity\StringField('COUPON', array(
  139. 'validation' => array(__CLASS__, 'validateCoupon')
  140. )),
  141. 'SORT' => new Main\Entity\IntegerField('SORT', array(
  142. 'title' => Loc::getMessage('DISCOUNT_ENTITY_SORT_FIELD')
  143. )),
  144. 'MAX_DISCOUNT' => new Main\Entity\FloatField('MAX_DISCOUNT', array(
  145. 'title' => Loc::getMessage('DISCOUNT_ENTITY_MAX_DISCOUNT_FIELD')
  146. )),
  147. 'VALUE_TYPE' => new Main\Entity\EnumField('VALUE_TYPE', array(
  148. 'required' => true,
  149. 'values' => array(self::VALUE_TYPE_PERCENT, self::VALUE_TYPE_FIX, self::VALUE_TYPE_SALE),
  150. 'default_value' => self::VALUE_TYPE_PERCENT,
  151. 'title' => Loc::getMessage('DISCOUNT_ENTITY_VALUE_TYPE_FIELD')
  152. )),
  153. 'VALUE' => new Main\Entity\FloatField('VALUE', array(
  154. 'required' => true,
  155. 'title' => Loc::getMessage('DISCOUNT_ENTITY_VALUE_FIELD')
  156. )),
  157. 'CURRENCY' => new Main\Entity\StringField('CURRENCY', array(
  158. 'required' => true,
  159. 'validation' => array(__CLASS__, 'validateCurrency'),
  160. 'title' => Loc::getMessage('DISCOUNT_ENTITY_CURRENCY_FIELD')
  161. )),
  162. 'MIN_ORDER_SUM' => new Main\Entity\FloatField('MIN_ORDER_SUM', array(
  163. 'default_value' => 0
  164. )),
  165. 'TIMESTAMP_X' => new Main\Entity\DatetimeField('TIMESTAMP_X', array(
  166. 'required' => true,
  167. 'default_value' => new Main\Type\DateTime(),
  168. 'title' => Loc::getMessage('DISCOUNT_ENTITY_TIMESTAMP_X_FIELD')
  169. )),
  170. 'COUNT_PERIOD' => new Main\Entity\EnumField('COUNT_PERIOD', array(
  171. 'values' => array(self::COUNT_PERIOD_TYPE_ALL, self::COUNT_PERIOD_TYPE_INTERVAL, self::COUNT_PERIOD_TYPE_PERIOD),
  172. 'default_value' => self::COUNT_PERIOD_TYPE_ALL
  173. )),
  174. 'COUNT_SIZE' => new Main\Entity\IntegerField('COUNT_SIZE', array(
  175. 'default_value' => 0
  176. )),
  177. 'COUNT_TYPE' => new Main\Entity\EnumField('COUNT_TYPE', array(
  178. 'values' => array(self::COUNT_TYPE_SIZE_DAY, self::COUNT_TYPE_SIZE_MONTH, self::COUNT_TYPE_SIZE_YEAR),
  179. 'default_value' => self::COUNT_TYPE_SIZE_YEAR
  180. )),
  181. 'COUNT_FROM' => new Main\Entity\DatetimeField('COUNT_FROM', array(
  182. 'default_value' => null
  183. )),
  184. 'COUNT_TO' => new Main\Entity\DatetimeField('COUNT_TO', array(
  185. 'default_value' => null
  186. )),
  187. 'ACTION_SIZE' => new Main\Entity\IntegerField('ACTION_SIZE', array(
  188. 'default_value' => 0
  189. )),
  190. 'ACTION_TYPE' => new Main\Entity\EnumField('ACTION_TYPE', array(
  191. 'values' => array(self::ACTION_TYPE_SIZE_DAY, self::ACTION_TYPE_SIZE_MONTH, self::ACTION_TYPE_SIZE_YEAR),
  192. 'default_value' => self::ACTION_TYPE_SIZE_YEAR
  193. )),
  194. 'MODIFIED_BY' => new Main\Entity\IntegerField('MODIFIED_BY', array(
  195. 'title' => Loc::getMessage('DISCOUNT_ENTITY_MODIFIED_BY_FIELD')
  196. )),
  197. 'DATE_CREATE' => new Main\Entity\DatetimeField('DATE_CREATE', array(
  198. 'default_value' => null,
  199. 'title' => Loc::getMessage('DISCOUNT_ENTITY_DATE_CREATE_FIELD')
  200. )),
  201. 'CREATED_BY' => new Main\Entity\IntegerField('CREATED_BY', array(
  202. 'title' => Loc::getMessage('DISCOUNT_ENTITY_CREATED_BY_FIELD')
  203. )),
  204. 'PRIORITY' => new Main\Entity\IntegerField('PRIORITY', array(
  205. 'default_value' => 1,
  206. 'title' => Loc::getMessage('DISCOUNT_ENTITY_PRIORITY_FIELD')
  207. )),
  208. 'LAST_DISCOUNT' => new Main\Entity\BooleanField('LAST_DISCOUNT', array(
  209. 'values' => array('N', 'Y'),
  210. 'default_value' => 'Y',
  211. 'title' => Loc::getMessage('DISCOUNT_ENTITY_LAST_DISCOUNT_FIELD')
  212. )),
  213. 'VERSION' => new Main\Entity\EnumField('VERSION', array(
  214. 'values' => array(self::OLD_VERSION, self::ACTUAL_VERSION),
  215. 'default_value' => self::ACTUAL_VERSION
  216. )),
  217. 'NOTES' => new Main\Entity\StringField('NOTES', array(
  218. 'validation' => array(__CLASS__, 'validateNotes'),
  219. 'title' => Loc::getMessage('DISCOUNT_ENTITY_NOTES_FIELD')
  220. )),
  221. 'CONDITIONS' => new Main\Entity\TextField('CONDITIONS', array()),
  222. 'CONDITIONS_LIST' => new Main\Entity\TextField('CONDITIONS_LIST', array(
  223. 'serialized' => true,
  224. 'column_name' => 'CONDITIONS',
  225. 'title' => Loc::getMessage('DISCOUNT_ENTITY_CONDITIONS_LIST_FIELD')
  226. )),
  227. 'UNPACK' => new Main\Entity\TextField('UNPACK', array()),
  228. 'CREATED_BY_USER' => new Main\Entity\ReferenceField(
  229. 'CREATED_BY_USER',
  230. 'Bitrix\Main\User',
  231. array('=this.CREATED_BY' => 'ref.ID')
  232. ),
  233. 'MODIFIED_BY_USER' => new Main\Entity\ReferenceField(
  234. 'MODIFIED_BY_USER',
  235. 'Bitrix\Main\User',
  236. array('=this.MODIFIED_BY' => 'ref.ID')
  237. )
  238. );
  239. }
  240. /**
  241. * Returns validators for XML_ID field.
  242. *
  243. * @return array
  244. */
  245. public static function validateXmlId()
  246. {
  247. return array(
  248. new Main\Entity\Validator\Length(null, 255),
  249. );
  250. }
  251. /**
  252. * Returns validators for SITE_ID field.
  253. *
  254. * @return array
  255. */
  256. public static function validateSiteId()
  257. {
  258. return array(
  259. new Main\Entity\Validator\Length(null, 2),
  260. );
  261. }
  262. /**
  263. * Returns validators for TYPE field.
  264. *
  265. * @return array
  266. */
  267. public static function validateType()
  268. {
  269. return array(
  270. array(__CLASS__, 'checkType')
  271. );
  272. }
  273. /**
  274. * Returns validators for NAME field.
  275. *
  276. * @return array
  277. */
  278. public static function validateName()
  279. {
  280. return array(
  281. new Main\Entity\Validator\Length(null, 255),
  282. );
  283. }
  284. /**
  285. * Returns validators for COUPON field.
  286. *
  287. * @return array
  288. */
  289. public static function validateCoupon()
  290. {
  291. return array(
  292. new Main\Entity\Validator\Length(null, 20),
  293. );
  294. }
  295. /**
  296. * Returns validators for CURRENCY field.
  297. *
  298. * @return array
  299. */
  300. public static function validateCurrency()
  301. {
  302. return array(
  303. new Main\Entity\Validator\Length(null, 3),
  304. );
  305. }
  306. /**
  307. * Returns validators for NOTES field.
  308. *
  309. * @return array
  310. */
  311. public static function validateNotes()
  312. {
  313. return array(
  314. new Main\Entity\Validator\Length(null, 255),
  315. );
  316. }
  317. /**
  318. * Check TYPE field.
  319. *
  320. * @param int $value Current field value.
  321. * @param int|array $primary Primary key.
  322. * @param array $row Current data.
  323. * @param Main\Entity\Field $field Field object.
  324. * @return bool|string
  325. */
  326. public static function checkType($value, $primary, array $row, Main\Entity\Field $field)
  327. {
  328. if (
  329. $value == self::TYPE_DISCOUNT
  330. || $value == self::TYPE_DISCOUNT_SAVE
  331. )
  332. {
  333. return true;
  334. }
  335. return Loc::getMessage('DISCOUNT_ENTITY_VALIDATOR_TYPE');
  336. }
  337. /**
  338. * Add discount.
  339. *
  340. * @param array $data Discount data.
  341. * @return Main\Entity\AddResult
  342. */
  343. public static function add(array $data)
  344. {
  345. $result = new Main\Entity\AddResult();
  346. $result->addError(new Main\Entity\EntityError(
  347. Loc::getMessage('CATALOG_DISCOUNT_ENTITY_MESS_ADD_BLOCKED')
  348. ));
  349. return $result;
  350. }
  351. /**
  352. * Updates discount by primary key.
  353. *
  354. * @param mixed $primary Discount primary key.
  355. * @param array $data Discount data.
  356. * @return Main\Entity\UpdateResult
  357. */
  358. public static function update($primary, array $data)
  359. {
  360. $result = new Main\Entity\UpdateResult();
  361. $result->addError(new Main\Entity\EntityError(
  362. Loc::getMessage('CATALOG_DISCOUNT_ENTITY_MESS_UPDATE_BLOCKED')
  363. ));
  364. return $result;
  365. }
  366. /**
  367. * Deletes discount by primary key.
  368. *
  369. * @param mixed $primary Discount primary key.
  370. * @return Main\Entity\DeleteResult
  371. */
  372. public static function delete($primary)
  373. {
  374. $result = new Main\Entity\DeleteResult();
  375. $result->addError(new Main\Entity\EntityError(
  376. Loc::getMessage('CATALOG_DISCOUNT_ENTITY_MESS_DELETE_BLOCKED')
  377. ));
  378. return $result;
  379. }
  380. /**
  381. * Convert discount data to other currency (sale currency).
  382. *
  383. * @param array &$discount Discout data.
  384. * @param string $currency New currency.
  385. * @return void
  386. */
  387. public static function convertCurrency(&$discount, $currency)
  388. {
  389. $currency = Currency\CurrencyManager::checkCurrencyID($currency);
  390. if ($currency === false || empty($discount) || !is_array($discount))
  391. return;
  392. if (!isset($discount['VALUE_TYPE']) || !isset($discount['CURRENCY']) || $discount['CURRENCY'] == $currency)
  393. return;
  394. switch ($discount['VALUE_TYPE'])
  395. {
  396. case self::VALUE_TYPE_FIX:
  397. case self::VALUE_TYPE_SALE:
  398. $discount['VALUE'] = \CCurrencyRates::convertCurrency($discount['VALUE'], $discount['CURRENCY'], $currency);
  399. $discount['CURRENCY'] = $currency;
  400. break;
  401. case self::VALUE_TYPE_PERCENT:
  402. if ($discount['MAX_DISCOUNT'] > 0)
  403. $discount['MAX_DISCOUNT'] = \CCurrencyRates::convertCurrency($discount['MAX_DISCOUNT'], $discount['CURRENCY'], $currency);
  404. $discount['CURRENCY'] = $currency;
  405. break;
  406. }
  407. }
  408. }