PageRenderTime 38ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

/classes/Pack.php

https://github.com/cokarmando/PrestaShop
PHP | 275 lines | 175 code | 31 blank | 69 comment | 21 complexity | fe0300003b8372722d2d47382020e004 MD5 | raw file
  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 license@prestashop.com 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 <contact@prestashop.com>
  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. class PackCore extends Product
  27. {
  28. protected static $cachePackItems = array();
  29. protected static $cacheIsPack = array();
  30. protected static $cacheIsPacked = array();
  31. /**
  32. * Is product a pack?
  33. *
  34. * @static
  35. * @param $id_product
  36. * @return bool
  37. */
  38. public static function isPack($id_product)
  39. {
  40. if (!Pack::isFeatureActive())
  41. return false;
  42. if (!$id_product)
  43. return false;
  44. if (!array_key_exists($id_product, self::$cacheIsPack))
  45. {
  46. $result = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'pack WHERE id_product_pack = '.(int)$id_product);
  47. self::$cacheIsPack[$id_product] = ($result > 0);
  48. }
  49. return self::$cacheIsPack[$id_product];
  50. }
  51. /**
  52. * Is product in a pack?
  53. *
  54. * @static
  55. * @param $id_product
  56. * @return bool
  57. */
  58. public static function isPacked($id_product)
  59. {
  60. if (!Pack::isFeatureActive())
  61. return false;
  62. if (!array_key_exists($id_product, self::$cacheIsPacked))
  63. {
  64. $result = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'pack WHERE id_product_item = '.(int)$id_product);
  65. self::$cacheIsPacked[$id_product] = ($result > 0);
  66. }
  67. return self::$cacheIsPacked[$id_product];
  68. }
  69. public static function noPackPrice($id_product)
  70. {
  71. $sum = 0;
  72. $price_display_method = !self::$_taxCalculationMethod;
  73. $items = Pack::getItems($id_product, Configuration::get('PS_LANG_DEFAULT'));
  74. foreach ($items as $item)
  75. $sum += $item->getPrice($price_display_method) * $item->pack_quantity;
  76. return $sum;
  77. }
  78. public static function getItems($id_product, $id_lang)
  79. {
  80. if (!Pack::isFeatureActive())
  81. return array();
  82. if (array_key_exists($id_product, self::$cachePackItems))
  83. return self::$cachePackItems[$id_product];
  84. $result = Db::getInstance()->executeS('SELECT id_product_item, quantity FROM '._DB_PREFIX_.'pack where id_product_pack = '.(int)$id_product);
  85. $array_result = array();
  86. foreach ($result as $row)
  87. {
  88. $p = new Product($row['id_product_item'], false, $id_lang);
  89. $p->loadStockData();
  90. $p->pack_quantity = $row['quantity'];
  91. $array_result[] = $p;
  92. }
  93. self::$cachePackItems[$id_product] = $array_result;
  94. return self::$cachePackItems[$id_product];
  95. }
  96. public static function isInStock($id_product)
  97. {
  98. if (!Pack::isFeatureActive())
  99. return true;
  100. $items = Pack::getItems((int)$id_product, Configuration::get('PS_LANG_DEFAULT'));
  101. foreach ($items as $item)
  102. {
  103. // Updated for 1.5.0
  104. if (Product::getQuantity($item->id) < $item->pack_quantity && !$item->isAvailableWhenOutOfStock((int)$item->out_of_stock))
  105. return false;
  106. }
  107. return true;
  108. }
  109. public static function getItemTable($id_product, $id_lang, $full = false)
  110. {
  111. if (!Pack::isFeatureActive())
  112. return array();
  113. $sql = 'SELECT p.*, product_shop.*, pl.*, MAX(image_shop.`id_image`) id_image, il.`legend`, cl.`name` AS category_default, a.quantity AS pack_quantity, product_shop.`id_category_default`, a.id_product_pack
  114. FROM `'._DB_PREFIX_.'pack` a
  115. LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = a.id_product_item
  116. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  117. ON p.id_product = pl.id_product
  118. AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
  119. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`)'.
  120. Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
  121. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
  122. '.Shop::addSqlAssociation('product', 'p').'
  123. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
  124. ON product_shop.`id_category_default` = cl.`id_category`
  125. AND cl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').'
  126. WHERE product_shop.`id_shop` = '.(int)Context::getContext()->shop->id.'
  127. AND a.`id_product_pack` = '.(int)$id_product.'
  128. GROUP BY product_shop.id_product';
  129. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  130. foreach ($result as &$line)
  131. $line = Product::getTaxesInformations($line);
  132. if (!$full)
  133. return $result;
  134. $array_result = array();
  135. foreach ($result as $prow)
  136. if (!Pack::isPack($prow['id_product']))
  137. $array_result[] = Product::getProductProperties($id_lang, $prow);
  138. return $array_result;
  139. }
  140. public static function getPacksTable($id_product, $id_lang, $full = false, $limit = null)
  141. {
  142. if (!Pack::isFeatureActive())
  143. return array();
  144. $packs = Db::getInstance()->getValue('
  145. SELECT GROUP_CONCAT(a.`id_product_pack`)
  146. FROM `'._DB_PREFIX_.'pack` a
  147. WHERE a.`id_product_item` = '.(int)$id_product);
  148. if (!(int)$packs)
  149. return array();
  150. $sql = '
  151. SELECT p.*, product_shop.*, pl.*, MAX(image_shop.`id_image`) id_image, il.`legend`
  152. FROM `'._DB_PREFIX_.'product` p
  153. NATURAL LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  154. '.Shop::addSqlAssociation('product', 'p').'
  155. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product`)'.
  156. Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
  157. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
  158. WHERE pl.`id_lang` = '.(int)$id_lang.'
  159. '.Shop::addSqlRestrictionOnLang('pl').'
  160. AND p.`id_product` IN ('.$packs.')
  161. GROUP BY product_shop.id_product';
  162. if ($limit)
  163. $sql .= ' LIMIT '.(int)$limit;
  164. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
  165. if (!$full)
  166. return $result;
  167. $array_result = array();
  168. foreach ($result as $row)
  169. if (!Pack::isPacked($row['id_product']))
  170. $array_result[] = Product::getProductProperties($id_lang, $row);
  171. return $array_result;
  172. }
  173. public static function deleteItems($id_product)
  174. {
  175. return Db::getInstance()->update('product', array('cache_is_pack' => 0), 'id_product = '.(int)$id_product) &&
  176. Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'pack` WHERE `id_product_pack` = '.(int)$id_product) &&
  177. Configuration::updateGlobalValue('PS_PACK_FEATURE_ACTIVE', Pack::isCurrentlyUsed());
  178. }
  179. /**
  180. * Add an item to the pack
  181. *
  182. * @param integer $id_product
  183. * @param integer $id_item
  184. * @param integer $qty
  185. * @return boolean true if everything was fine
  186. */
  187. public static function addItem($id_product, $id_item, $qty)
  188. {
  189. return Db::getInstance()->update('product', array('cache_is_pack' => 1), 'id_product = '.(int)$id_product) &&
  190. Db::getInstance()->insert('pack', array('id_product_pack' => (int)$id_product, 'id_product_item' => (int)$id_item, 'quantity' => (int)$qty)) &&
  191. Configuration::updateGlobalValue('PS_PACK_FEATURE_ACTIVE', '1');
  192. }
  193. public static function duplicate($id_product_old, $id_product_new)
  194. {
  195. Db::getInstance()->execute('INSERT INTO '._DB_PREFIX_.'pack (id_product_pack, id_product_item, quantity)
  196. (SELECT '.(int)$id_product_new.', id_product_item, quantity FROM '._DB_PREFIX_.'pack WHERE id_product_pack = '.(int)$id_product_old.')');
  197. // If return query result, a non-pack product will return false
  198. return true;
  199. }
  200. /**
  201. * This method is allow to know if a feature is used or active
  202. * @since 1.5.0.1
  203. * @return bool
  204. */
  205. public static function isFeatureActive()
  206. {
  207. return Configuration::get('PS_PACK_FEATURE_ACTIVE');
  208. }
  209. /**
  210. * This method is allow to know if a Pack entity is currently used
  211. * @since 1.5.0
  212. * @param $table
  213. * @param $has_active_column
  214. * @return bool
  215. */
  216. public static function isCurrentlyUsed($table = null, $has_active_column = false)
  217. {
  218. // We dont't use the parent method because the identifier isn't id_pack
  219. return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  220. SELECT `id_product_pack`
  221. FROM `'._DB_PREFIX_.'pack`
  222. ');
  223. }
  224. /**
  225. * For a given pack, tells if it has at least one product using the advanced stock management
  226. *
  227. * @param int $id_product id_pack
  228. * @return bool
  229. */
  230. public static function usesAdvancedStockManagement($id_product)
  231. {
  232. if (!Pack::isPack($id_product))
  233. return false;
  234. $products = Pack::getItems($id_product, Configuration::get('PS_LANG_DEFAULT'));
  235. foreach ($products as $product)
  236. {
  237. // if one product uses the advanced stock management
  238. if ($product->advanced_stock_management == 1)
  239. return true;
  240. }
  241. // not used
  242. return false;
  243. }
  244. }