PageRenderTime 51ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/classes/Pack.php

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