PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/Combination.php

https://bitbucket.org/enurkov/prestashop
PHP | 281 lines | 179 code | 47 blank | 55 comment | 10 complexity | fa79ff847908245580c3cc3fac4107d2 MD5 | raw file
  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. * @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 CombinationCore extends ObjectModel
  27. {
  28. public $id_product;
  29. public $reference;
  30. public $supplier_reference;
  31. public $location;
  32. public $ean13;
  33. public $upc;
  34. public $wholesale_price;
  35. public $price;
  36. public $unit_price_impact;
  37. public $ecotax;
  38. public $minimal_quantity = 1;
  39. public $quantity;
  40. public $weight;
  41. public $default_on;
  42. public $available_date = '0000-00-00';
  43. /**
  44. * @see ObjectModel::$definition
  45. */
  46. public static $definition = array(
  47. 'table' => 'product_attribute',
  48. 'primary' => 'id_product_attribute',
  49. 'fields' => array(
  50. 'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  51. 'location' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName', 'size' => 64),
  52. 'ean13' => array('type' => self::TYPE_STRING, 'validate' => 'isEan13', 'size' => 13),
  53. 'upc' => array('type' => self::TYPE_STRING, 'validate' => 'isUpc', 'size' => 12),
  54. 'quantity' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'size' => 10),
  55. 'reference' => array('type' => self::TYPE_STRING, 'size' => 32),
  56. 'supplier_reference' => array('type' => self::TYPE_STRING, 'size' => 32),
  57. /* Shop fields */
  58. 'wholesale_price' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice', 'size' => 27),
  59. 'price' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isNegativePrice', 'size' => 20),
  60. 'ecotax' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isPrice', 'size' => 20),
  61. 'weight' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isFloat'),
  62. 'unit_price_impact' => array('type' => self::TYPE_FLOAT, 'shop' => true, 'validate' => 'isNegativePrice', 'size' => 20),
  63. 'minimal_quantity' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isUnsignedId', 'required' => true),
  64. 'default_on' => array('type' => self::TYPE_INT, 'shop' => true, 'validate' => 'isBool'),
  65. 'available_date' => array('type' => self::TYPE_DATE, 'shop' => true, 'validate' => 'isDateFormat'),
  66. ),
  67. );
  68. protected $webserviceParameters = array(
  69. 'objectNodeName' => 'combination',
  70. 'objectsNodeName' => 'combinations',
  71. 'fields' => array(
  72. 'id_product' => array('required' => true, 'xlink_resource'=> 'products'),
  73. ),
  74. 'associations' => array(
  75. 'product_option_values' => array('resource' => 'product_option_value'),
  76. 'images' => array('resource' => 'image'),
  77. ),
  78. );
  79. public function delete()
  80. {
  81. if (!parent::delete())
  82. return false;
  83. // Removes the product from StockAvailable, for the current shop
  84. StockAvailable::removeProductFromStockAvailable((int)$this->id_product, (int)$this->id);
  85. if (!$this->hasMultishopEntries() && !$this->deleteAssociations())
  86. return false;
  87. return true;
  88. }
  89. public function add($autodate = true, $null_values = false)
  90. {
  91. if (!parent::add($autodate, $null_values))
  92. return false;
  93. $product = new Product((int)$this->id_product);
  94. if ($product->getType() == Product::PTYPE_VIRTUAL)
  95. StockAvailable::setProductOutOfStock((int)$this->id_product, 1, null, (int)$this->id);
  96. else
  97. StockAvailable::setProductOutOfStock((int)$this->id_product, StockAvailable::outOfStock((int)$this->id_product), null, $this->id);
  98. SpecificPriceRule::applyAllRules(array((int)$this->id_product));
  99. return true;
  100. }
  101. public function deleteAssociations()
  102. {
  103. $result = Db::getInstance()->delete('product_attribute_combination', '`id_product_attribute` = '.(int)$this->id);
  104. $result &= Db::getInstance()->delete('cart_product', '`id_product_attribute` = '.(int)$this->id);
  105. return $result;
  106. }
  107. public function setAttributes($ids_attribute)
  108. {
  109. if ($this->deleteAssociations())
  110. {
  111. $sql_values = array();
  112. foreach ($ids_attribute as $value)
  113. $sql_values[] = '('.(int)$value.', '.(int)$this->id.')';
  114. $result = Db::getInstance()->execute('
  115. INSERT INTO `'._DB_PREFIX_.'product_attribute_combination` (`id_attribute`, `id_product_attribute`)
  116. VALUES '.implode(',', $sql_values)
  117. );
  118. return $result;
  119. }
  120. return false;
  121. }
  122. public function setWsProductOptionValues($values)
  123. {
  124. $ids_attributes = array();
  125. foreach ($values as $value)
  126. $ids_attributes[] = $value['id'];
  127. return $this->setAttributes($ids_attributes);
  128. }
  129. public function getWsProductOptionValues()
  130. {
  131. $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  132. SELECT a.id_attribute AS id
  133. FROM `'._DB_PREFIX_.'product_attribute_combination` a
  134. '.Shop::addSqlAssociation('attribute', 'a').'
  135. WHERE a.id_product_attribute = '.(int)$this->id);
  136. return $result;
  137. }
  138. public function getWsImages()
  139. {
  140. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  141. SELECT a.`id_image` as id
  142. FROM `'._DB_PREFIX_.'product_attribute_image` a
  143. '.Shop::addSqlAssociation('product_attribute', 'a').'
  144. WHERE a.`id_product_attribute` = '.(int)$this->id.'
  145. ');
  146. }
  147. public function setImages($ids_image)
  148. {
  149. if (Db::getInstance()->execute('
  150. DELETE FROM `'._DB_PREFIX_.'product_attribute_image`
  151. WHERE `id_product_attribute` = '.(int)$this->id) === false)
  152. return false;
  153. $sql_values = array();
  154. foreach ($ids_image as $value)
  155. $sql_values[] = '('.(int)$this->id.', '.(int)$value.')';
  156. Db::getInstance()->execute('
  157. INSERT INTO `'._DB_PREFIX_.'product_attribute_image` (`id_product_attribute`, `id_image`)
  158. VALUES '.implode(',', $sql_values)
  159. );
  160. return true;
  161. }
  162. public function setWsImages($values)
  163. {
  164. $ids_images = array();
  165. foreach ($values as $value)
  166. $ids_images[] = (int)$value['id'];
  167. return $this->setImages($ids_images);
  168. }
  169. public function getAttributesName($id_lang)
  170. {
  171. return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
  172. SELECT al.*
  173. FROM '._DB_PREFIX_.'product_attribute_combination pac
  174. JOIN '._DB_PREFIX_.'attribute_lang al ON (pac.id_attribute = al.id_attribute AND al.id_lang='.(int)$id_lang.')
  175. WHERE pac.id_product_attribute='.(int)$this->id);
  176. }
  177. /**
  178. * This method is allow to know if a feature is active
  179. * @since 1.5.0.1
  180. * @return bool
  181. */
  182. public static function isFeatureActive()
  183. {
  184. static $feature_active = null;
  185. if ($feature_active === null)
  186. $feature_active = Configuration::get('PS_COMBINATION_FEATURE_ACTIVE');
  187. return $feature_active;
  188. }
  189. /**
  190. * This method is allow to know if a Combination entity is currently used
  191. * @since 1.5.0.1
  192. * @param $table
  193. * @param $has_active_column
  194. * @return bool
  195. */
  196. public static function isCurrentlyUsed($table = null, $has_active_column = false)
  197. {
  198. return parent::isCurrentlyUsed('product_attribute');
  199. }
  200. /**
  201. * For a given product_attribute reference, returns the corresponding id
  202. *
  203. * @param int $id_product
  204. * @param string $reference
  205. * @return int id
  206. */
  207. public static function getIdByReference($id_product, $reference)
  208. {
  209. if (empty($reference))
  210. return 0;
  211. $query = new DbQuery();
  212. $query->select('pa.id_product_attribute');
  213. $query->from('product_attribute', 'pa');
  214. $query->where('pa.reference LIKE \'%'.pSQL($reference).'%\'');
  215. $query->where('pa.id_product = '.(int)$id_product);
  216. return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query);
  217. }
  218. /**
  219. * Retrive the price of combination
  220. *
  221. * @since 1.5.0
  222. * @param int $id_product_attribute
  223. * @return float mixed
  224. */
  225. public static function getPrice($id_product_attribute)
  226. {
  227. return Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
  228. SELECT product_attribute_shop.`price`
  229. FROM `'._DB_PREFIX_.'product_attribute` pa
  230. '.Shop::addSqlAssociation('product_attribute', 'pa').'
  231. WHERE pa.`id_product_attribute` = '.(int)$id_product_attribute
  232. );
  233. }
  234. }