PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/sale/mysql/discount.php

https://gitlab.com/alexprowars/bitrix
PHP | 233 lines | 190 code | 27 blank | 16 comment | 46 complexity | 7ad7e7c3cc8356ee2fd559d81283f81c MD5 | raw file
  1. <?
  2. require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/sale/general/discount.php');
  3. class CSaleDiscount extends CAllSaleDiscount
  4. {
  5. /**
  6. * @param array $arOrder
  7. * @param array $arFilter
  8. * @param bool|array $arGroupBy
  9. * @param bool|array $arNavStartParams
  10. * @param array $arSelectFields
  11. * @return bool|CDBResult|mixed
  12. */
  13. public static function GetList($arOrder = array(), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
  14. {
  15. global $DB;
  16. if (!is_array($arOrder) && !is_array($arFilter))
  17. {
  18. $arOrder = (string)($arOrder);
  19. $arFilter = (string)($arFilter);
  20. if ($arOrder !== '' && $arFilter !== '')
  21. $arOrder = array($arOrder => $arFilter);
  22. else
  23. $arOrder = array();
  24. if (is_array($arGroupBy))
  25. $arFilter = $arGroupBy;
  26. else
  27. $arFilter = array();
  28. if (isset($arFilter["PRICE"]))
  29. {
  30. $valTmp = $arFilter["PRICE"];
  31. unset($arFilter["PRICE"]);
  32. $arFilter["<=PRICE_FROM"] = $valTmp;
  33. $arFilter[">=PRICE_TO"] = $valTmp;
  34. }
  35. $arGroupBy = false;
  36. }
  37. $arFields = array(
  38. "ID" => array("FIELD" => "D.ID", "TYPE" => "int"),
  39. "XML_ID" => array("FIELD" => "D.XML_ID", "TYPE" => "string"),
  40. "LID" => array("FIELD" => "D.LID", "TYPE" => "string"),
  41. "SITE_ID" => array("FIELD" => "D.LID", "TYPE" => "string"),
  42. "NAME" => array("FIELD" => "D.NAME", "TYPE" => "string"),
  43. "PRICE_FROM" => array("FIELD" => "D.PRICE_FROM", "TYPE" => "double", "WHERE" => array("CSaleDiscount", "PrepareCurrency4Where")),
  44. "PRICE_TO" => array("FIELD" => "D.PRICE_TO", "TYPE" => "double", "WHERE" => array("CSaleDiscount", "PrepareCurrency4Where")),
  45. "CURRENCY" => array("FIELD" => "D.CURRENCY", "TYPE" => "string"),
  46. "DISCOUNT_VALUE" => array("FIELD" => "D.DISCOUNT_VALUE", "TYPE" => "double"),
  47. "DISCOUNT_TYPE" => array("FIELD" => "D.DISCOUNT_TYPE", "TYPE" => "char"),
  48. "ACTIVE" => array("FIELD" => "D.ACTIVE", "TYPE" => "char"),
  49. "SORT" => array("FIELD" => "D.SORT", "TYPE" => "int"),
  50. "ACTIVE_FROM" => array("FIELD" => "D.ACTIVE_FROM", "TYPE" => "datetime"),
  51. "ACTIVE_TO" => array("FIELD" => "D.ACTIVE_TO", "TYPE" => "datetime"),
  52. "TIMESTAMP_X" => array("FIELD" => "D.TIMESTAMP_X", "TYPE" => "datetime"),
  53. "MODIFIED_BY" => array("FIELD" => "D.MODIFIED_BY", "TYPE" => "int"),
  54. "DATE_CREATE" => array("FIELD" => "D.DATE_CREATE", "TYPE" => "datetime"),
  55. "CREATED_BY" => array("FIELD" => "D.CREATED_BY", "TYPE" => "int"),
  56. "PRIORITY" => array("FIELD" => "D.PRIORITY", "TYPE" => "int"),
  57. "LAST_DISCOUNT" => array("FIELD" => "D.LAST_DISCOUNT", "TYPE" => "char"),
  58. "LAST_LEVEL_DISCOUNT" => array("FIELD" => "D.LAST_LEVEL_DISCOUNT", "TYPE" => "char"),
  59. "VERSION" => array("FIELD" => "D.VERSION", "TYPE" => "int"),
  60. "CONDITIONS" => array("FIELD" => "D.CONDITIONS", "TYPE" => "string"),
  61. "UNPACK" => array("FIELD" => "D.UNPACK", "TYPE" => "string"),
  62. "APPLICATION" => array("FIELD" => "D.APPLICATION", "TYPE" => "string"),
  63. "ACTIONS" => array("FIELD" => "D.ACTIONS", "TYPE" => "string"),
  64. "PRESET_ID" => array("FIELD" => "D.PRESET_ID", "TYPE" => "string"),
  65. "USE_COUPONS" => array("FIELD" => "D.USE_COUPONS", "TYPE" => "char"),
  66. "USER_GROUPS" => array("FIELD" => "DG.GROUP_ID", "TYPE" => "int","FROM" => "LEFT JOIN b_sale_discount_group DG ON (D.ID = DG.DISCOUNT_ID)")
  67. );
  68. if (empty($arSelectFields))
  69. $arSelectFields = array('ID','LID','SITE_ID','PRICE_FROM','PRICE_TO','CURRENCY','DISCOUNT_VALUE','DISCOUNT_TYPE','ACTIVE','SORT','ACTIVE_FROM','ACTIVE_TO','PRIORITY','LAST_DISCOUNT', 'LAST_LEVEL_DISCOUNT','VERSION','NAME');
  70. elseif (is_array($arSelectFields) && in_array('*',$arSelectFields))
  71. $arSelectFields = array('ID','LID','SITE_ID','PRICE_FROM','PRICE_TO','CURRENCY','DISCOUNT_VALUE','DISCOUNT_TYPE','ACTIVE','SORT','ACTIVE_FROM','ACTIVE_TO','PRIORITY','LAST_DISCOUNT', 'LAST_LEVEL_DISCOUNT','VERSION','NAME');
  72. $arSqls = CSaleOrder::PrepareSql($arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields);
  73. $arSqls["SELECT"] = str_replace("%%_DISTINCT_%%", '', $arSqls["SELECT"]);
  74. if (empty($arGroupBy) && is_array($arGroupBy))
  75. {
  76. $strSql = "select ".$arSqls["SELECT"]." from b_sale_discount D ".$arSqls["FROM"];
  77. if (!empty($arSqls["WHERE"]))
  78. $strSql .= " where ".$arSqls["WHERE"];
  79. if (!empty($arSqls["GROUPBY"]))
  80. $strSql .= " group by ".$arSqls["GROUPBY"];
  81. $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  82. if ($arRes = $dbRes->Fetch())
  83. return $arRes["CNT"];
  84. else
  85. return false;
  86. }
  87. $strSql = "select ".$arSqls["SELECT"]." from b_sale_discount D ".$arSqls["FROM"];
  88. if (!empty($arSqls["WHERE"]))
  89. $strSql .= " where ".$arSqls["WHERE"];
  90. if (!empty($arSqls["GROUPBY"]))
  91. $strSql .= " group by ".$arSqls["GROUPBY"];
  92. if (!empty($arSqls["ORDERBY"]))
  93. $strSql .= " order by ".$arSqls["ORDERBY"];
  94. $intTopCount = 0;
  95. $boolNavStartParams = (!empty($arNavStartParams) && is_array($arNavStartParams));
  96. if ($boolNavStartParams && isset($arNavStartParams['nTopCount']))
  97. {
  98. $intTopCount = (int)$arNavStartParams["nTopCount"];
  99. }
  100. if ($boolNavStartParams && $intTopCount <= 0)
  101. {
  102. $strSql_tmp = "select COUNT('x') as CNT from b_sale_discount D ".$arSqls["FROM"];
  103. if (!empty($arSqls["WHERE"]))
  104. $strSql_tmp .= " where ".$arSqls["WHERE"];
  105. if (!empty($arSqls["GROUPBY"]))
  106. $strSql_tmp .= " group by ".$arSqls["GROUPBY"];
  107. $dbRes = $DB->Query($strSql_tmp, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  108. $cnt = 0;
  109. if (empty($arSqls["GROUPBY"]))
  110. {
  111. if ($arRes = $dbRes->Fetch())
  112. $cnt = $arRes["CNT"];
  113. }
  114. else
  115. {
  116. $cnt = $dbRes->SelectedRowsCount();
  117. }
  118. $dbRes = new CDBResult();
  119. $dbRes->NavQuery($strSql, $cnt, $arNavStartParams);
  120. }
  121. else
  122. {
  123. if ($boolNavStartParams && $intTopCount > 0)
  124. {
  125. $strSql .= " limit ".$intTopCount;
  126. }
  127. $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  128. }
  129. return $dbRes;
  130. }
  131. /**
  132. * @param array $arOrder
  133. * @param array $arFilter
  134. * @param bool|array $arGroupBy
  135. * @param bool|array $arNavStartParams
  136. * @param array $arSelectFields
  137. * @return bool|CDBResult|mixed
  138. */
  139. public static function GetDiscountGroupList($arOrder = array(), $arFilter = array(), $arGroupBy = false, $arNavStartParams = false, $arSelectFields = array())
  140. {
  141. global $DB;
  142. $arFields = array(
  143. "ID" => array("FIELD" => "DG.ID", "TYPE" => "int"),
  144. "DISCOUNT_ID" => array("FIELD" => "DG.DISCOUNT_ID", "TYPE" => "int"),
  145. "GROUP_ID" => array("FIELD" => "DG.GROUP_ID", "TYPE" => "int"),
  146. );
  147. $arSqls = CSaleOrder::PrepareSql($arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields);
  148. $arSqls["SELECT"] = str_replace("%%_DISTINCT_%%", "", $arSqls["SELECT"]);
  149. if (empty($arGroupBy) && is_array($arGroupBy))
  150. {
  151. $strSql = "select ".$arSqls["SELECT"]." from b_sale_discount_group DG ".$arSqls["FROM"];
  152. if (!empty($arSqls["WHERE"]))
  153. $strSql .= " where ".$arSqls["WHERE"];
  154. if (!empty($arSqls["GROUPBY"]))
  155. $strSql .= " group by ".$arSqls["GROUPBY"];
  156. $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  157. if ($arRes = $dbRes->Fetch())
  158. return $arRes["CNT"];
  159. else
  160. return false;
  161. }
  162. $strSql = "select ".$arSqls["SELECT"]." from b_sale_discount_group DG ".$arSqls["FROM"];
  163. if (!empty($arSqls["WHERE"]))
  164. $strSql .= " where ".$arSqls["WHERE"];
  165. if (!empty($arSqls["GROUPBY"]))
  166. $strSql .= " group by ".$arSqls["GROUPBY"];
  167. if (!empty($arSqls["ORDERBY"]))
  168. $strSql .= " order by ".$arSqls["ORDERBY"];
  169. $intTopCount = 0;
  170. $boolNavStartParams = (!empty($arNavStartParams) && is_array($arNavStartParams));
  171. if ($boolNavStartParams && array_key_exists('nTopCount', $arNavStartParams))
  172. {
  173. $intTopCount = intval($arNavStartParams["nTopCount"]);
  174. }
  175. if ($boolNavStartParams && 0 >= $intTopCount)
  176. {
  177. $strSql_tmp = "select COUNT('x') as CNT from b_sale_discount_group DG ".$arSqls["FROM"];
  178. if (!empty($arSqls["WHERE"]))
  179. $strSql_tmp .= " where ".$arSqls["WHERE"];
  180. if (!empty($arSqls["GROUPBY"]))
  181. $strSql_tmp .= " group by ".$arSqls["GROUPBY"];
  182. $dbRes = $DB->Query($strSql_tmp, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  183. $cnt = 0;
  184. if (empty($arSqls["GROUPBY"]))
  185. {
  186. if ($arRes = $dbRes->Fetch())
  187. $cnt = $arRes["CNT"];
  188. }
  189. else
  190. {
  191. $cnt = $dbRes->SelectedRowsCount();
  192. }
  193. $dbRes = new CDBResult();
  194. $dbRes->NavQuery($strSql, $cnt, $arNavStartParams);
  195. }
  196. else
  197. {
  198. if ($boolNavStartParams && 0 < $intTopCount)
  199. {
  200. $strSql .= " limit ".$intTopCount;
  201. }
  202. $dbRes = $DB->Query($strSql, false, "File: ".__FILE__."<br>Line: ".__LINE__);
  203. }
  204. return $dbRes;
  205. }
  206. }