/classes/stock/Stock.php

https://gitlab.com/staging06/myproject · PHP · 180 lines · 96 code · 25 blank · 59 comment · 10 complexity · e34f44c3a971aae505fc32ff2b723111 MD5 · raw file

  1. <?php
  2. /*
  3. * 2007-2015 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-2015 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. * Represents the products kept in warehouses
  28. *
  29. * @since 1.5.0
  30. */
  31. class StockCore extends ObjectModel
  32. {
  33. /** @var int identifier of the warehouse */
  34. public $id_warehouse;
  35. /** @var int identifier of the product */
  36. public $id_product;
  37. /** @var int identifier of the product attribute if necessary */
  38. public $id_product_attribute;
  39. /** @var string Product reference */
  40. public $reference;
  41. /** @var int Product EAN13 */
  42. public $ean13;
  43. /** @var string UPC */
  44. public $upc;
  45. /** @var int the physical quantity in stock for the current product in the current warehouse */
  46. public $physical_quantity;
  47. /** @var int the usable quantity (for sale) of the current physical quantity */
  48. public $usable_quantity;
  49. /** @var int the unit price without tax forthe current product */
  50. public $price_te;
  51. /**
  52. * @see ObjectModel::$definition
  53. */
  54. public static $definition = array(
  55. 'table' => 'stock',
  56. 'primary' => 'id_stock',
  57. 'fields' => array(
  58. 'id_warehouse' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  59. 'id_product' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  60. 'id_product_attribute' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId', 'required' => true),
  61. 'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference'),
  62. 'ean13' => array('type' => self::TYPE_STRING, 'validate' => 'isEan13'),
  63. 'upc' => array('type' => self::TYPE_STRING, 'validate' => 'isUpc'),
  64. 'physical_quantity' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt', 'required' => true),
  65. 'usable_quantity' => array('type' => self::TYPE_INT, 'validate' => 'isInt', 'required' => true),
  66. 'price_te' => array('type' => self::TYPE_FLOAT, 'validate' => 'isPrice', 'required' => true),
  67. ),
  68. );
  69. /**
  70. * @see ObjectModel::$webserviceParameters
  71. */
  72. protected $webserviceParameters = array(
  73. 'fields' => array(
  74. 'id_warehouse' => array('xlink_resource' => 'warehouses'),
  75. 'id_product' => array('xlink_resource' => 'products'),
  76. 'id_product_attribute' => array('xlink_resource' => 'combinations'),
  77. 'real_quantity' => array('getter' => 'getWsRealQuantity', 'setter' => false),
  78. ),
  79. 'hidden_fields' => array(
  80. ),
  81. );
  82. /**
  83. * @see ObjectModel::update()
  84. */
  85. public function update($null_values = false)
  86. {
  87. $this->getProductInformations();
  88. return parent::update($null_values);
  89. }
  90. /**
  91. * @see ObjectModel::add()
  92. */
  93. public function add($autodate = true, $null_values = false)
  94. {
  95. $this->getProductInformations();
  96. return parent::add($autodate, $null_values);
  97. }
  98. /**
  99. * Gets reference, ean13 and upc of the current product
  100. * Stores it in stock for stock_mvt integrity and history purposes
  101. */
  102. protected function getProductInformations()
  103. {
  104. // if combinations
  105. if ((int)$this->id_product_attribute > 0) {
  106. $query = new DbQuery();
  107. $query->select('reference, ean13, upc');
  108. $query->from('product_attribute');
  109. $query->where('id_product = '.(int)$this->id_product);
  110. $query->where('id_product_attribute = '.(int)$this->id_product_attribute);
  111. $rows = Db::getInstance()->executeS($query);
  112. if (!is_array($rows)) {
  113. return;
  114. }
  115. foreach ($rows as $row) {
  116. $this->reference = $row['reference'];
  117. $this->ean13 = $row['ean13'];
  118. $this->upc = $row['upc'];
  119. }
  120. } else {
  121. // else, simple product
  122. $product = new Product((int)$this->id_product);
  123. if (Validate::isLoadedObject($product)) {
  124. $this->reference = $product->reference;
  125. $this->ean13 = $product->ean13;
  126. $this->upc = $product->upc;
  127. }
  128. }
  129. }
  130. /**
  131. * Webservice : used to get the real quantity of a product
  132. */
  133. public function getWsRealQuantity()
  134. {
  135. $manager = StockManagerFactory::getManager();
  136. $quantity = $manager->getProductRealQuantities($this->id_product, $this->id_product_attribute, $this->id_warehouse, true);
  137. return $quantity;
  138. }
  139. public static function deleteStockByIds($id_product = null, $id_product_attribute = null)
  140. {
  141. if (!$id_product || !$id_product_attribute) {
  142. return false;
  143. }
  144. return Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'stock WHERE `id_product` = '.(int)$id_product.' AND `id_product_attribute` = '.(int)$id_product_attribute);
  145. }
  146. public static function productIsPresentInStock($id_product = 0, $id_product_attribute = 0, $id_warehouse = 0)
  147. {
  148. if (!(int)$id_product && !is_int($id_product_attribute) && !(int)$id_warehouse) {
  149. return false;
  150. }
  151. $result = Db::getInstance()->executeS('SELECT `id_stock` FROM '._DB_PREFIX_.'stock
  152. WHERE `id_warehouse` = '.(int)$id_warehouse.' AND `id_product` = '.(int)$id_product.((int)$id_product_attribute ? ' AND `id_product_attribute` = '.$id_product_attribute : ''));
  153. return (is_array($result) && !empty($result) ? true : false);
  154. }
  155. }