PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/prestashop/classes/Pack.php

https://github.com/ebusiness-2012/Tribu-des-gones
PHP | 199 lines | 142 code | 20 blank | 37 comment | 11 complexity | a838674c698fdc32501eedf9b925b32b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /*
  3. * 2007-2011 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-2011 PrestaShop SA
  23. * @version Release: $Revision: 7403 $
  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. public static function isPack($id_product)
  33. {
  34. if (!array_key_exists($id_product, self::$cacheIsPack))
  35. {
  36. $result = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'pack WHERE id_product_pack = '.(int)($id_product));
  37. self::$cacheIsPack[$id_product] = ($result > 0);
  38. }
  39. return self::$cacheIsPack[$id_product];
  40. }
  41. public static function isPacked($id_product)
  42. {
  43. if (!array_key_exists($id_product, self::$cacheIsPacked))
  44. {
  45. $result = Db::getInstance()->getValue('SELECT COUNT(*) FROM '._DB_PREFIX_.'pack WHERE id_product_item = '.(int)($id_product));
  46. self::$cacheIsPacked[$id_product] = ($result > 0);
  47. }
  48. return self::$cacheIsPacked[$id_product];
  49. }
  50. public static function noPackPrice($id_product)
  51. {
  52. $sum = 0;
  53. $price_display_method = !self::$_taxCalculationMethod;
  54. $items = self::getItems($id_product, Configuration::get('PS_LANG_DEFAULT'));
  55. foreach ($items as $item)
  56. $sum += $item->getPrice($price_display_method) * $item->pack_quantity;
  57. return $sum;
  58. }
  59. public static function getItems($id_product, $id_lang)
  60. {
  61. if (array_key_exists($id_product, self::$cachePackItems))
  62. return self::$cachePackItems[$id_product];
  63. $result = Db::getInstance()->ExecuteS('SELECT id_product_item, quantity FROM '._DB_PREFIX_.'pack where id_product_pack = '.(int)($id_product));
  64. $arrayResult = array();
  65. foreach ($result AS $row)
  66. {
  67. $p = new Product($row['id_product_item'], false, (int)($id_lang));
  68. $p->pack_quantity = $row['quantity'];
  69. $arrayResult[] = $p;
  70. }
  71. self::$cachePackItems[$id_product] = $arrayResult;
  72. return self::$cachePackItems[$id_product];
  73. }
  74. public static function isInStock($id_product)
  75. {
  76. $items = self::getItems((int)($id_product), Configuration::get('PS_LANG_DEFAULT'));
  77. foreach ($items AS $item)
  78. if ($item->quantity < $item->pack_quantity AND !$item->isAvailableWhenOutOfStock((int)($item->out_of_stock)))
  79. return false;
  80. return true;
  81. }
  82. public static function getItemTable($id_product, $id_lang, $full = false)
  83. {
  84. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
  85. SELECT p.*, pl.*, i.`id_image`, il.`legend`, t.`rate`, cl.`name` AS category_default, a.quantity AS pack_quantity
  86. FROM `'._DB_PREFIX_.'pack` a
  87. LEFT JOIN `'._DB_PREFIX_.'product` p ON p.id_product = a.id_product_item
  88. LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.id_product = pl.id_product AND pl.`id_lang` = '.(int)($id_lang).')
  89. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
  90. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)($id_lang).')
  91. LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)($id_lang).')
  92. LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
  93. AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
  94. AND tr.`id_state` = 0)
  95. LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
  96. LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)($id_lang).')
  97. WHERE a.`id_product_pack` = '.(int)($id_product)
  98. );
  99. if (!$full)
  100. return $result;
  101. $arrayResult = array();
  102. foreach ($result as $row)
  103. if (!Pack::isPack($row['id_product']))
  104. $arrayResult[] = Product::getProductProperties($id_lang, $row);
  105. return $arrayResult;
  106. }
  107. public static function getPacksTable($id_product, $id_lang, $full = false, $limit = NULL)
  108. {
  109. $packs = Db::getInstance()->getValue('
  110. SELECT GROUP_CONCAT(a.`id_product_pack`)
  111. FROM `'._DB_PREFIX_.'pack` a
  112. WHERE a.`id_product_item` = '.(int)$id_product);
  113. if (!(int)$packs)
  114. return array();
  115. $sql = '
  116. SELECT p.*, pl.*, i.`id_image`, il.`legend`, t.`rate`
  117. FROM `'._DB_PREFIX_.'product` p
  118. NATURAL LEFT JOIN `'._DB_PREFIX_.'product_lang` pl
  119. LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
  120. LEFT JOIN `'._DB_PREFIX_.'image_lang` il ON (i.`id_image` = il.`id_image` AND il.`id_lang` = '.(int)$id_lang.')
  121. LEFT JOIN `'._DB_PREFIX_.'tax_rule` tr ON (p.`id_tax_rules_group` = tr.`id_tax_rules_group`
  122. AND tr.`id_country` = '.(int)Country::getDefaultCountryId().'
  123. AND tr.`id_state` = 0)
  124. LEFT JOIN `'._DB_PREFIX_.'tax` t ON (t.`id_tax` = tr.`id_tax`)
  125. LEFT JOIN `'._DB_PREFIX_.'tax_lang` tl ON (t.`id_tax` = tl.`id_tax` AND tl.`id_lang` = '.(int)$id_lang.')
  126. WHERE pl.`id_lang` = '.(int)$id_lang.'
  127. AND p.`id_product` IN ('.$packs.')';
  128. if ($limit)
  129. $sql .= ' LIMIT '.(int)$limit;
  130. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($sql);
  131. if (!$full)
  132. return $result;
  133. $arrayResult = array();
  134. foreach ($result as $row)
  135. if (!Pack::isPacked($row['id_product']))
  136. $arrayResult[] = Product::getProductProperties($id_lang, $row);
  137. return $arrayResult;
  138. }
  139. public static function deleteItems($id_product)
  140. {
  141. Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'product SET cache_is_pack = 0 WHERE id_product = '.(int)($id_product).' LIMIT 1');
  142. return Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'pack` WHERE `id_product_pack` = '.(int)($id_product));
  143. }
  144. /**
  145. * @deprecated
  146. */
  147. public static function addItems($id_product, $ids)
  148. {
  149. Tools::displayAsDeprecated();
  150. array_pop($ids);
  151. foreach ($ids as $id_product_item)
  152. {
  153. $idQty = explode('x', $id_product_item);
  154. if (!self::addItem($id_product, $idQty[1], $idQty[0]))
  155. return false;
  156. }
  157. return true;
  158. }
  159. /**
  160. * Add an item to the pack
  161. *
  162. * @param integer $id_product
  163. * @param integer $id_item
  164. * @param integer $qty
  165. * @return boolean true if everything was fine
  166. */
  167. public static function addItem($id_product, $id_item, $qty)
  168. {
  169. Db::getInstance()->Execute('UPDATE '._DB_PREFIX_.'product SET cache_is_pack = 1 WHERE id_product = '.(int)($id_product).' LIMIT 1');
  170. return Db::getInstance()->AutoExecute(_DB_PREFIX_.'pack', array('id_product_pack' => (int)($id_product), 'id_product_item' => (int)($id_item), 'quantity' => (int)($qty)), 'INSERT');
  171. }
  172. public static function duplicate($id_product_old, $id_product_new)
  173. {
  174. Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'pack (id_product_pack, id_product_item, quantity)
  175. (SELECT '.(int)($id_product_new).', id_product_item, quantity FROM '._DB_PREFIX_.'pack WHERE id_product_pack = '.(int)($id_product_old).')');
  176. // If return query result, a non-pack product will return false
  177. return true;
  178. }
  179. }