Guest User

Untitled

a guest
Oct 2nd, 2013
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.90 KB | None | 0 0
  1. <?php
  2. /*
  3. * 2007-2013 PrestaShop
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to [email protected] so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade PrestaShop to newer
  18. * versions in the future. If you wish to customize PrestaShop for your
  19. * needs please refer to http://www.prestashop.com for more information.
  20. *
  21. * @author PrestaShop SA <[email protected]>
  22. * @copyright 2007-2013 PrestaShop SA
  23. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  24. * International Registered Trademark & Property of PrestaShop SA
  25. */
  26.  
  27. class ProductSaleCore
  28. {
  29. /*
  30. ** Fill the `product_sale` SQL table with data from `order_detail`
  31. ** @return bool True on success
  32. */
  33. public static function fillProductSales()
  34. {
  35. $sql = 'REPLACE INTO '._DB_PREFIX_.'product_sale
  36. (`id_product`, `quantity`, `sale_nbr`, `date_upd`)
  37. SELECT od.product_id, COUNT(od.product_id), SUM(od.product_quantity), NOW()
  38. FROM '._DB_PREFIX_.'order_detail od GROUP BY od.product_id';
  39. return Db::getInstance()->execute($sql);
  40. }
  41.  
  42. /*
  43. ** Get number of actives products sold
  44. ** @return int number of actives products listed in product_sales
  45. */
  46. public static function getNbSales()
  47. {
  48. $sql = 'SELECT COUNT(ps.`id_product`) AS nb
  49. FROM `'._DB_PREFIX_.'product_sale` ps
  50. LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = ps.`id_product`
  51. '.Shop::addSqlAssociation('product', 'p', false).'
  52. WHERE product_shop.`active` = 1';
  53. return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($sql);
  54. }
  55.  
  56. /*
  57. ** Get required informations on best sales products
  58. **
  59. ** @param integer $id_lang Language id
  60. ** @param integer $page_number Start from (optional)
  61. ** @param integer $nb_products Number of products to return (optional)
  62. ** @return array from Product::getProductProperties
  63. */
  64. public static function getBestSales($id_lang, $page_number = 0, $nb_products = 10, $order_by = null, $order_way = null)
  65. {
  66. if ($page_number < 0) $page_number = 0;
  67. if ($nb_products < 1) $nb_products = 10;
  68. $final_order_by = $order_by;
  69. if (is_null($order_by) || $order_by == 'position' || $order_by == 'price') $order_by = 'sales';
  70. if (is_null($order_way) || $order_by == 'sales') $order_way == 'DESC';
  71. $groups = FrontController::getCurrentCustomerGroups();
  72. $sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
  73. $interval = Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20;
  74.  
  75. $sql = 'SELECT p.*, product_shop.*, stock.out_of_stock, IFNULL(stock.quantity, 0) as quantity,
  76. pl.`description`, pl.`description_short`, pl.`link_rewrite`, pl.`meta_description`,
  77. pl.`meta_keywords`, pl.`meta_title`, pl.`name`,
  78. m.`name` AS manufacturer_name, p.`id_manufacturer` as id_manufacturer,
  79. MAX(image_shop.`id_image`) id_image, il.`legend`,
  80. ps.`quantity` AS sales, t.`rate`, pl.`meta_keywords`, pl.`meta_title`, pl.`meta_description`,
  81. DATEDIFF(p.`date_add`, DATE_SUB(NOW(),
  82. INTERVAL '.$interval.' DAY)) > 0 AS new
  83. FROM `'._DB_PREFIX_.'product_sale` ps
  84. LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product`
  85. '.Shop::addSqlAssociation('product', 'p', false).'
  86. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  87. ON p.`id_product` = pl.`id_product`
  88. AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
  89. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`)'.
  90. Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
  91. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
  92. LEFT JOIN `'._DB_PREFIX_.'manufacturer` m ON (m.`id_manufacturer` = p.`id_manufacturer`)
  93. LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (product_shop.`id_tax_rules_group` = tr.`id_tax_rules_group`)
  94. AND tr.`id_country` = '.(int)Context::getContext()->country->id.'
  95. AND tr.`id_state` = 0
  96. LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
  97. '.Product::sqlStock('p').'
  98. WHERE product_shop.`active` = 1
  99. AND p.`visibility` != \'none\'
  100. AND p.`id_product` IN (
  101. SELECT cp.`id_product`
  102. FROM `'._DB_PREFIX_.'category_group` cg
  103. LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
  104. WHERE cg.`id_group` '.$sql_groups.'
  105. )
  106. GROUP BY product_shop.id_product
  107. ORDER BY `'.pSQL($order_by).'` '.pSQL($order_way).'
  108. LIMIT '.(int)($page_number * $nb_products).', '.(int)$nb_products;
  109.  
  110. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  111.  
  112. if ($final_order_by == 'price')
  113. Tools::orderbyPrice($result, $order_way);
  114. if (!$result)
  115. return false;
  116. return Product::getProductsProperties($id_lang, $result);
  117. }
  118.  
  119. /*
  120. ** Get required informations on best sales products
  121. **
  122. ** @param integer $id_lang Language id
  123. ** @param integer $page_number Start from (optional)
  124. ** @param integer $nb_products Number of products to return (optional)
  125. ** @return array keys : id_product, link_rewrite, name, id_image, legend, sales, ean13, upc, link
  126. */
  127. public static function getBestSalesLight($id_lang, $page_number = 0, $nb_products = 10, Context $context = null)
  128. {
  129. if (!$context)
  130. $context = Context::getContext();
  131. if ($page_number < 0) $page_number = 0;
  132. if ($nb_products < 1) $nb_products = 10;
  133.  
  134. $groups = FrontController::getCurrentCustomerGroups();
  135. $sql_groups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
  136.  
  137. $sql = 'SELECT p.id_product, pl.`link_rewrite`, pl.`name`, pl.`description_short`, MAX(image_shop.`id_image`) id_image, il.`legend`,
  138. ps.`quantity` AS sales, p.`ean13`, p.`upc`, cl.`link_rewrite` AS category
  139. FROM `'._DB_PREFIX_.'product_sale` ps
  140. LEFT JOIN `'._DB_PREFIX_.'product` p ON ps.`id_product` = p.`id_product`
  141. '.Shop::addSqlAssociation('product', 'p').'
  142. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  143. ON p.`id_product` = pl.`id_product`
  144. AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
  145. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`)'.
  146. Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
  147. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
  148. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
  149. ON cl.`id_category` = product_shop.`id_category_default`
  150. AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
  151. WHERE product_shop.`active` = 1
  152. AND p.`visibility` != \'none\'
  153. AND p.`id_product` IN (
  154. SELECT cp.`id_product`
  155. FROM `'._DB_PREFIX_.'category_group` cg
  156. LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_category` = cg.`id_category`)
  157. WHERE cg.`id_group` '.$sql_groups.'
  158. )
  159. GROUP BY product_shop.id_product
  160. ORDER BY sales DESC
  161. LIMIT '.(int)($page_number * $nb_products).', '.(int)$nb_products;
  162. if (!$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql))
  163. return false;
  164.  
  165. foreach ($result as &$row)
  166. {
  167. $row['link'] = $context->link->getProductLink($row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
  168. $row['id_image'] = Product::defineProductImage($row, $id_lang);
  169. }
  170. return $result;
  171. }
  172.  
  173. public static function addProductSale($product_id, $qty = 1)
  174. {
  175. return Db::getInstance()->execute('
  176. INSERT INTO '._DB_PREFIX_.'product_sale
  177. (`id_product`, `quantity`, `sale_nbr`, `date_upd`)
  178. VALUES ('.(int)$product_id.', '.(int)$qty.', 1, NOW())
  179. ON DUPLICATE KEY UPDATE `quantity` = `quantity` + '.(int)$qty.', `sale_nbr` = `sale_nbr` + 1, `date_upd` = NOW()');
  180. }
  181.  
  182. public static function getNbrSales($id_product)
  183. {
  184. $result = Db::getInstance()->getRow('SELECT `sale_nbr` FROM '._DB_PREFIX_.'product_sale WHERE `id_product` = '.(int)$id_product);
  185. if (!$result || empty($result) || !key_exists('sale_nbr', $result))
  186. return -1;
  187. return (int)$result['sale_nbr'];
  188. }
  189.  
  190. public static function removeProductSale($id_product, $qty = 1)
  191. {
  192. $total_sales = ProductSale::getNbrSales($id_product);
  193. if ($total_sales > 1)
  194. return Db::getInstance()->execute('
  195. UPDATE '._DB_PREFIX_.'product_sale
  196. SET `quantity` = `quantity` - '.(int)$qty.', `sale_nbr` = `sale_nbr` - 1, `date_upd` = NOW()
  197. WHERE `id_product` = '.(int)$id_product
  198. );
  199. elseif ($total_sales == 1)
  200. return Db::getInstance()->delete('product_sale', 'id_product = '.(int)$id_product);
  201. return true;
  202. }
  203. }
Advertisement
Add Comment
Please, Sign In to add comment