PageRenderTime 26ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/catalog/lib/rounding.php

https://gitlab.com/alexprowars/bitrix
PHP | 410 lines | 230 code | 33 blank | 147 comment | 23 complexity | 3758eda46370d351180797df3bac57ee MD5 | raw file
  1. <?php
  2. namespace Bitrix\Catalog;
  3. use Bitrix\Main,
  4. Bitrix\Main\Localization\Loc;
  5. Loc::loadMessages(__FILE__);
  6. /**
  7. * Class RoundingTable
  8. *
  9. * Fields:
  10. * <ul>
  11. * <li> ID int mandatory
  12. * <li> CATALOG_GROUP_ID int mandatory
  13. * <li> PRICE double mandatory
  14. * <li> ROUND_TYPE int mandatory
  15. * <li> ROUND_PRECISION double mandatory
  16. * <li> CREATED_BY int optional
  17. * <li> DATE_CREATE datetime optional
  18. * <li> MODIFIED_BY int optional
  19. * <li> TIMESTAMP_X datetime optional
  20. * <li> CREATED_BY_USER reference to {@link \Bitrix\Main\UserTable}
  21. * <li> MODIFIED_BY_USER reference to {@link \Bitrix\Main\UserTable}
  22. * </ul>
  23. *
  24. * @package Bitrix\Catalog
  25. *
  26. * DO NOT WRITE ANYTHING BELOW THIS
  27. *
  28. * <<< ORMENTITYANNOTATION
  29. * @method static EO_Rounding_Query query()
  30. * @method static EO_Rounding_Result getByPrimary($primary, array $parameters = array())
  31. * @method static EO_Rounding_Result getById($id)
  32. * @method static EO_Rounding_Result getList(array $parameters = array())
  33. * @method static EO_Rounding_Entity getEntity()
  34. * @method static \Bitrix\Catalog\EO_Rounding createObject($setDefaultValues = true)
  35. * @method static \Bitrix\Catalog\EO_Rounding_Collection createCollection()
  36. * @method static \Bitrix\Catalog\EO_Rounding wakeUpObject($row)
  37. * @method static \Bitrix\Catalog\EO_Rounding_Collection wakeUpCollection($rows)
  38. */
  39. class RoundingTable extends Main\Entity\DataManager
  40. {
  41. const ROUND_MATH = 0x0001;
  42. const ROUND_UP = 0x0002;
  43. const ROUND_DOWN = 0x0004;
  44. /** @var int clear rounding cache flag */
  45. protected static $clearCache = 0;
  46. /** @var array price type list for clear */
  47. protected static $priceTypeIds = array();
  48. /**
  49. * Returns DB table name for entity.
  50. *
  51. * @return string
  52. */
  53. public static function getTableName()
  54. {
  55. return 'b_catalog_rounding';
  56. }
  57. /**
  58. * Returns entity map definition.
  59. *
  60. * @return array
  61. */
  62. public static function getMap()
  63. {
  64. return array(
  65. 'ID' => new Main\Entity\IntegerField('ID', array(
  66. 'primary' => true,
  67. 'autocomplete' => true,
  68. 'title' => Loc::getMessage('ROUNDING_ENTITY_ID_FIELD')
  69. )),
  70. 'CATALOG_GROUP_ID' => new Main\Entity\IntegerField('CATALOG_GROUP_ID', array(
  71. 'required' => true,
  72. 'title' => Loc::getMessage('ROUNDING_ENTITY_CATALOG_GROUP_ID_FIELD')
  73. )),
  74. 'PRICE' => new Main\Entity\FloatField('PRICE', array(
  75. 'required' => true,
  76. 'title' => Loc::getMessage('ROUNDING_ENTITY_PRICE_FIELD')
  77. )),
  78. 'ROUND_TYPE' => new Main\Entity\EnumField('ROUND_TYPE', array(
  79. 'required' => true,
  80. 'values' => array(self::ROUND_MATH, self::ROUND_UP, self::ROUND_DOWN),
  81. 'title' => Loc::getMessage('ROUNDING_ENTITY_ROUND_TYPE_FIELD')
  82. )),
  83. 'ROUND_PRECISION' => new Main\Entity\FloatField('ROUND_PRECISION', array(
  84. 'required' => true,
  85. 'title' => Loc::getMessage('ROUNDING_ENTITY_ROUND_PRECISION_FIELD')
  86. )),
  87. 'CREATED_BY' => new Main\Entity\IntegerField('CREATED_BY', array(
  88. 'title' => Loc::getMessage('ROUNDING_ENTITY_CREATED_BY_FIELD')
  89. )),
  90. 'DATE_CREATE' => new Main\Entity\DatetimeField('DATE_CREATE', array(
  91. 'title' => Loc::getMessage('ROUNDING_ENTITY_DATE_CREATE_FIELD')
  92. )),
  93. 'MODIFIED_BY' => new Main\Entity\IntegerField('MODIFIED_BY', array(
  94. 'title' => Loc::getMessage('ROUNDING_ENTITY_MODIFIED_BY_FIELD')
  95. )),
  96. 'DATE_MODIFY' => new Main\Entity\DatetimeField('DATE_MODIFY', array(
  97. 'title' => Loc::getMessage('ROUNDING_ENTITY_TIMESTAMP_X_FIELD')
  98. )),
  99. 'CREATED_BY_USER' => new Main\Entity\ReferenceField(
  100. 'CREATED_BY_USER',
  101. '\Bitrix\Main\User',
  102. array('=this.CREATED_BY' => 'ref.ID'),
  103. array('join_type' => 'LEFT')
  104. ),
  105. 'MODIFIED_BY_USER' => new Main\Entity\ReferenceField(
  106. 'MODIFIED_BY_USER',
  107. '\Bitrix\Main\User',
  108. array('=this.MODIFIED_BY' => 'ref.ID'),
  109. array('join_type' => 'LEFT')
  110. )
  111. );
  112. }
  113. /**
  114. * Default onBeforeAdd handler. Absolutely necessary.
  115. *
  116. * @param Main\Entity\Event $event Current data for add.
  117. * @return Main\Entity\EventResult
  118. */
  119. public static function onBeforeAdd(Main\Entity\Event $event)
  120. {
  121. $result = new Main\Entity\EventResult;
  122. $data = $event->getParameter('fields');
  123. $modifyFieldList = array();
  124. static::setUserId($modifyFieldList, $data, array('CREATED_BY', 'MODIFIED_BY'));
  125. static::setTimestamp($modifyFieldList, $data, array('DATE_CREATE', 'DATE_MODIFY'));
  126. if (!empty($modifyFieldList))
  127. $result->modifyFields($modifyFieldList);
  128. unset($modifyFieldList);
  129. return $result;
  130. }
  131. /**
  132. * Default onAfterAdd handler. Absolutely necessary.
  133. *
  134. * @param Main\Entity\Event $event Current data for add.
  135. * @return void
  136. */
  137. public static function onAfterAdd(Main\Entity\Event $event)
  138. {
  139. if (!static::isAllowedClearCache())
  140. return;
  141. $data = $event->getParameter('fields');
  142. self::$priceTypeIds[$data['CATALOG_GROUP_ID']] = $data['CATALOG_GROUP_ID'];
  143. unset($data);
  144. static::clearCache();
  145. }
  146. /**
  147. * Default onBeforeUpdate handler. Absolutely necessary.
  148. *
  149. * @param Main\Entity\Event $event Current data for update.
  150. * @return Main\Entity\EventResult
  151. */
  152. public static function onBeforeUpdate(Main\Entity\Event $event)
  153. {
  154. $result = new Main\Entity\EventResult;
  155. $data = $event->getParameter('fields');
  156. $modifyFieldList = array();
  157. static::setUserId($modifyFieldList, $data, array('MODIFIED_BY'));
  158. static::setTimestamp($modifyFieldList, $data, array('DATE_MODIFY'));
  159. if (!empty($modifyFieldList))
  160. $result->modifyFields($modifyFieldList);
  161. unset($modifyFieldList);
  162. return $result;
  163. }
  164. /**
  165. * Default onUpdate handler. Absolutely necessary.
  166. *
  167. * @param Main\Entity\Event $event Current data for update.
  168. * @return void
  169. */
  170. public static function onUpdate(Main\Entity\Event $event)
  171. {
  172. if (!static::isAllowedClearCache())
  173. return;
  174. $data = $event->getParameter('fields');
  175. $rule = static::getList(array(
  176. 'select' => array('ID', 'CATALOG_GROUP_ID'),
  177. 'filter' => array('=ID' => $event->getParameter('id'))
  178. ))->fetch();
  179. if (!empty($rule))
  180. {
  181. self::$priceTypeIds[$rule['CATALOG_GROUP_ID']] = $rule['CATALOG_GROUP_ID'];
  182. if (isset($data['CATALOG_GROUP_ID']))
  183. self::$priceTypeIds[$data['CATALOG_GROUP_ID']] = $data['CATALOG_GROUP_ID'];
  184. }
  185. unset($rule, $data);
  186. }
  187. /**
  188. * Default onAfterUpdate handler. Absolutely necessary.
  189. *
  190. * @param Main\Entity\Event $event Current data for update.
  191. * @return void
  192. */
  193. public static function onAfterUpdate(Main\Entity\Event $event)
  194. {
  195. static::clearCache();
  196. }
  197. /**
  198. * Default onDelete handler. Absolutely necessary.
  199. *
  200. * @param Main\Entity\Event $event Current data for delete.
  201. * @return void
  202. */
  203. public static function onDelete(Main\Entity\Event $event)
  204. {
  205. if (!static::isAllowedClearCache())
  206. return;
  207. $rule = static::getList(array(
  208. 'select' => array('ID', 'CATALOG_GROUP_ID'),
  209. 'filter' => array('=ID' => $event->getParameter('id'))
  210. ))->fetch();
  211. if (!empty($rule))
  212. self::$priceTypeIds[$rule['CATALOG_GROUP_ID']] = $rule['CATALOG_GROUP_ID'];
  213. unset($rule);
  214. }
  215. /**
  216. * Default onAfterDelete handler. Absolutely necessary.
  217. *
  218. * @param Main\Entity\Event $event Current data for delete.
  219. * @return void
  220. */
  221. public static function onAfterDelete(Main\Entity\Event $event)
  222. {
  223. static::clearCache();
  224. }
  225. /**
  226. * Returns current allow mode for cache clearing.
  227. *
  228. * @return bool
  229. */
  230. public static function isAllowedClearCache()
  231. {
  232. return (self::$clearCache >= 0);
  233. }
  234. /**
  235. * Allow clear cache after multiuse add/update/delete.
  236. *
  237. * @return void
  238. */
  239. public static function allowClearCache()
  240. {
  241. self::$clearCache++;
  242. }
  243. /**
  244. * Disallow clear cache before multiuse add/update/delete.
  245. *
  246. * @return void
  247. */
  248. public static function disallowClearCache()
  249. {
  250. self::$clearCache--;
  251. }
  252. /**
  253. * Clear price type ids.
  254. *
  255. * @return void
  256. */
  257. public static function clearPriceTypeIds()
  258. {
  259. self::$priceTypeIds = array();
  260. }
  261. /**
  262. * Set price type list for cache clearing.
  263. *
  264. * @param string|int|array $priceTypes Price types for cache clearing.
  265. * @return void
  266. */
  267. public static function setPriceTypeIds($priceTypes)
  268. {
  269. if (!is_array($priceTypes))
  270. $priceTypes = array($priceTypes => $priceTypes);
  271. if (!empty($priceTypes) && is_array($priceTypes))
  272. self::$priceTypeIds = (empty(self::$priceTypeIds) ? $priceTypes : array_merge(self::$priceTypeIds, $priceTypes));
  273. }
  274. /**
  275. * Clear managed cache.
  276. *
  277. * @return void
  278. */
  279. public static function clearCache()
  280. {
  281. if (!static::isAllowedClearCache() || empty(self::$priceTypeIds))
  282. return;
  283. foreach (self::$priceTypeIds as $priceType)
  284. Product\Price::clearRoundRulesCache($priceType);
  285. unset($priceType);
  286. static::clearPriceTypeIds();
  287. }
  288. /**
  289. * Delete rules by price type.
  290. *
  291. * @param string|int $priceType Price type id.
  292. * @return void
  293. */
  294. public static function deleteByPriceType($priceType)
  295. {
  296. $priceType = (int)$priceType;
  297. if ($priceType <= 0)
  298. return;
  299. $conn = Main\Application::getConnection();
  300. $helper = $conn->getSqlHelper();
  301. $conn->queryExecute(
  302. 'delete from '.$helper->quote(self::getTableName()).' where '.$helper->quote('CATALOG_GROUP_ID').' = '.$priceType
  303. );
  304. unset($helper, $conn);
  305. Product\Price::clearRoundRulesCache($priceType);
  306. }
  307. /**
  308. * Return round types.
  309. *
  310. * @param bool $full Get types with description.
  311. * @return array
  312. */
  313. public static function getRoundTypes($full = false)
  314. {
  315. $full = ($full === true);
  316. if ($full)
  317. {
  318. return array(
  319. self::ROUND_MATH => Loc::getMessage('ROUNDING_TYPE_ROUND_MATH'),
  320. self::ROUND_UP => Loc::getMessage('ROUNDING_TYPE_ROUND_UP'),
  321. self::ROUND_DOWN => Loc::getMessage('ROUNDING_TYPE_ROUND_DOWN')
  322. );
  323. }
  324. return array(
  325. self::ROUND_MATH,
  326. self::ROUND_UP,
  327. self::ROUND_DOWN
  328. );
  329. }
  330. /**
  331. * Fill user id fields.
  332. *
  333. * @param array &$result Modified data for add/update discount.
  334. * @param array $data Current data for add/update discount.
  335. * @param array $keys List with checked keys (userId info).
  336. * @return void
  337. */
  338. protected static function setUserId(array &$result, array $data, array $keys)
  339. {
  340. static $currentUserID = false;
  341. if ($currentUserID === false)
  342. {
  343. global $USER;
  344. /** @noinspection PhpMethodOrClassCallIsNotCaseSensitiveInspection */
  345. $currentUserID = (isset($USER) && $USER instanceof \CUser ? (int)$USER->getID() : null);
  346. }
  347. foreach ($keys as $index)
  348. {
  349. $setField = true;
  350. if (array_key_exists($index, $data))
  351. $setField = ($data[$index] !== null && (int)$data[$index] <= 0);
  352. if ($setField)
  353. $result[$index] = $currentUserID;
  354. }
  355. unset($index);
  356. }
  357. /**
  358. * Fill datetime fields.
  359. *
  360. * @param array &$result Modified data for add/update discount.
  361. * @param array $data Current data for add/update discount.
  362. * @param array $keys List with checked keys (datetime info).
  363. * @return void
  364. */
  365. protected static function setTimestamp(array &$result, array $data, array $keys)
  366. {
  367. foreach ($keys as $index)
  368. {
  369. $setField = true;
  370. if (array_key_exists($index, $data))
  371. $setField = ($data[$index] !== null && !is_object($data[$index]));
  372. if ($setField)
  373. $result[$index] = new Main\Type\DateTime();
  374. }
  375. unset($index);
  376. }
  377. }