diff --git a/classes/stock/Stock.php b/classes/stock/Stock.php index 39a6c1ad0..de5c3a094 100644 --- a/classes/stock/Stock.php +++ b/classes/stock/Stock.php @@ -30,21 +30,32 @@ */ class StockCore extends ObjectModel { + public $id_warehouse; public $id_product; public $id_product_attribute; public $physical_quantity; public $usable_quantity; public $price_te; - protected $fieldsRequired = array('id_product', 'id_product_attribute', 'physical_quantity', 'usable_quantity', 'price_te'); + protected $fieldsRequired = array( + 'id_warehouse', + 'id_product', + 'id_product_attribute', + 'physical_quantity', + 'usable_quantity', + 'price_te' + ); + protected $fieldsSize = array(); protected $fieldsValidate = array( + 'id_warehouse' => 'isUnsignedId', 'id_product' => 'isUnsignedId', 'id_product_attribute' => 'isUnsignedId', 'physical_quantity' => 'isUnsignedInt', 'usable_quantity' => 'isInt', - 'price_te' => 'isPrice'); + 'price_te' => 'isPrice' + ); protected $table = 'stock'; protected $identifier = 'id_stock'; @@ -52,6 +63,7 @@ class StockCore extends ObjectModel public function getFields() { $this->validateFields(); + $fields['id_warehouse'] = (int)$this->id_warehouse; $fields['id_product'] = (int)$this->id_product; $fields['id_product_attribute'] = (int)$this->id_product_attribute; $fields['physical_quantity'] = (int)$this->physical_quantity; @@ -59,14 +71,4 @@ class StockCore extends ObjectModel $fields['price_te'] = (float)$this->price_te; return $fields; } - - public static function getStockId($id_product, $id_product_attribute) - { - $query = array(); - $query['select'] = 's.id_stock'; - $query['from'] = _DB_PREFIX_.'stock s'; - $query['where'] = 'id_product = '.(int)$id_product.' AND id_product_attribute = '.(int)$id_product_attribute; - - return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS(Tools::buildQuery($query)); - } } \ No newline at end of file diff --git a/classes/stock/StockManagerModule.php b/classes/stock/StockManagerModule.php index 8ca037ea7..341c05aa6 100644 --- a/classes/stock/StockManagerModule.php +++ b/classes/stock/StockManagerModule.php @@ -55,5 +55,4 @@ abstract class StockManagerModule extends Module return false; } -} - +} \ No newline at end of file diff --git a/classes/stock/StockMvt.php b/classes/stock/StockMvt.php new file mode 100755 index 000000000..d66870631 --- /dev/null +++ b/classes/stock/StockMvt.php @@ -0,0 +1,194 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class StockMvtCore extends ObjectModel +{ + public $id; + public $date_add; + public $id_employee; + + /** + * @since 1.5.0 + * @var int + */ + public $physical_quantity; + + public $id_stock_mvt_reason; + public $id_order = null; + + /** + * @since 1.5.0 + * @var int + */ + public $sign; + + /** + * @since 1.5.0 + * @var int + */ + public $id_supplier_order = null; + + /** + * Last value of the Weighted-average method + * @since 1.5.0 + * @var float + */ + public $last_wa = null; + + /** + * Current value of the Weighted-average method + * @since 1.5.0 + * @var float + */ + public $current_wa = null; + + /** + * @since 1.5.0 + * @var float + */ + public $price_te; + + /** + * @deprecated since 1.5.0 + * @deprecated stock movement will not be updated anymore + */ + public $date_upd; + + /** + * @deprecated since 1.5.0 + * @see physical_quantity + * @var int + */ + public $quantity; + + protected $table = 'stock_mvt'; + protected $identifier = 'id_stock_mvt'; + + protected $fieldsRequired = array( + 'date_add', + 'id_employee', + 'id_stock', + 'physical_quantity', + 'id_stock_mvt_reason', + 'sign', + 'price_te' + ); + + protected $fieldsValidate = array( + 'date_add' => 'isDate', + 'id_employee' => 'isUnsignedId', + 'id_stock' => 'isUnsignedId', + 'physical_quantity' => 'isUnsignedInt', + 'id_stock_mvt_reason' => 'isUnsignedId', + 'id_order' => 'isUnsignedId', + 'sign' => 'isInt', + 'id_supplier_order' => 'isUnsignedId', + 'last_wa' => 'isPrice', + 'current_wa' => 'isPrice', + 'price_te' => 'isPrice' + ); + + protected $webserviceParameters = array( + 'objectsNodeName' => 'stock_movements', + 'objectNodeName' => 'stock_movement', + 'fields' => array( + 'id_employee' => array('xlink_resource'=> 'employees'), + 'id_stock' => array('xlink_resource'=> 'stock'), + 'id_stock_mvt_reason' => array('xlink_resource'=> 'stock_movement_reasons'), + 'id_order' => array('xlink_resource'=> 'orders') + ), + ); + + public function getFields() + { + $this->validateFields(); + $fields['date_add'] = pSQL($this->date_add); + $fields['id_employee'] = (int)$this->id_employee; + $fields['id_stock'] = (int)$this->id_stock; + $fields['physical_quantity'] = (int)$this->physical_quantity; + $fields['id_stock_mvt_reason'] = (int)$this->id_stock_mvt_reason; + $fields['id_order'] = (int)$this->id_order; + $fields['sign'] = (int)$this->sign; + $fields['id_supplider_order'] = (int)$this->id_supplier_order; + $fields['last_wa'] = (float)$this->last_wa; + $fields['current_wa'] = (float)$this->current_wa; + $fields['price_te'] = (float)$this->price_te; + return $fields; + } + + /** + * @deprecated since 1.5.0 + * Add missing stock movement (cmp the quantity of a products with all movements : if there is a difference, create a stock movement to correct it) + * + * @param int $id_employee + */ + public static function addMissingMvt($id_employee) + { + Tools::displayAsDeprecated(); + // Search missing stock movement on products without attributes + $sql = 'SELECT s.id_stock, (stock.quantity - SUM(IFNULL(sm.quantity, 0))) AS qty + FROM '._DB_PREFIX_.'product p + '.Product::sqlStock('p', null, true).' + LEFT JOIN '._DB_PREFIX_.'stock_mvt sm ON s.id_stock = sm.id_stock + WHERE ( + SELECT COUNT(*) FROM '._DB_PREFIX_.'stock s2 + WHERE s2.id_product = p.id_product + AND s2.id_product_attribute > 0 + ) = 0 + GROUP BY s.id_product, s.id_shop + HAVING qty <> 0'; + $products_without_attributes = Db::getInstance()->ExecuteS($sql); + + // Search missing stock movement on products with attributes + $sql = 'SELECT s.id_stock, (stock.quantity - SUM(IFNULL(sm.quantity, 0))) AS qty + FROM '._DB_PREFIX_.'product_attribute pa + '.Product::sqlStock('pa', 'pa', true).' + LEFT JOIN '._DB_PREFIX_.'stock_mvt sm ON s.id_stock = sm.id_stock + WHERE s.id_product_attribute > 0 + AND ( + SELECT COUNT(*) FROM '._DB_PREFIX_.'stock s2 + WHERE s2.id_product = pa.id_product + AND s2.id_product_attribute > 0 + ) > 0 + GROUP BY s.id_product_attribute + HAVING qty <> 0'; + $products_with_attributes = Db::getInstance()->ExecuteS($sql); + + // Add missing stock movements + $products = array_merge($products_without_attributes, $products_with_attributes); + if ($products) + foreach ($products as $product) + { + $mvt = new StockMvt(); + $mvt->id_stock = $product['id_stock']; + $mvt->id_employee = (int)$id_employee; + $mvt->quantity = $product['qty']; + $mvt->id_stock_mvt_reason = _STOCK_MOVEMENT_MISSING_REASON_; + $mvt->add(); + } + } +} \ No newline at end of file diff --git a/classes/stock/StockMvtReason.php b/classes/stock/StockMvtReason.php new file mode 100755 index 000000000..05b35cf94 --- /dev/null +++ b/classes/stock/StockMvtReason.php @@ -0,0 +1,70 @@ + +* @copyright 2007-2011 PrestaShop SA +* @version Release: $Revision$ +* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) +* International Registered Trademark & Property of PrestaShop SA +*/ + +class StockMvtReasonCore extends ObjectModel +{ + public $id; + public $name; + public $sign; + public $date_add; + public $date_upd; + protected $table = 'stock_mvt_reason'; + protected $identifier = 'id_stock_mvt_reason'; + + protected $fieldsRequiredLang = array('name'); + protected $fieldsSizeLang = array('name' => 255); + protected $fieldsValidateLang = array('name' => 'isGenericName'); + + protected $webserviceParameters = array( + 'objectsNodeName' => 'stock_movement_reasons', + 'objectNodeName' => 'stock_movement_reason', + ); + + public function getFields() + { + $this->validateFields(); + $fields['sign'] = (int)$this->sign; + $fields['date_add'] = pSQL($this->date_add); + $fields['date_upd'] = pSQL($this->date_upd); + return $fields; + } + + public function getTranslationsFieldsChild() + { + $this->validateFieldsLang(); + return $this->getTranslationsFields(array('name')); + } + + public static function getStockMvtReasons($id_lang) + { + $query = new DbQuery(); + $query->select('smrl.name, smr.id_stock_mvt_reason, smr.sign'); + $query->from('stock_mvt_reason smr'); + $query->leftjoin('stock_mvt_reason_lang smrl ON (smr.id_stock_mvt_reason = smrl.id_stock_mvt_reason AND smrl.id_lang='.(int)$id_lang.')'); + return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query); + } +} diff --git a/classes/stock/StockWarehouse.php b/classes/stock/StockWarehouse.php index 589bcdc54..43a7b59e4 100644 --- a/classes/stock/StockWarehouse.php +++ b/classes/stock/StockWarehouse.php @@ -36,15 +36,27 @@ class StockWarehouseCore extends ObjectModel public $id_employee; public $stock_management; - protected $fieldsRequired = array('id_address', 'reference', 'name', 'id_employee', 'stock_management'); - protected $fieldsSize = array('stock_management' => 32, 'reference' => 45, 'name' => 45); + protected $fieldsRequired = array( + 'id_address', + 'reference', + 'name', + 'id_employee', + 'stock_management' + ); + + protected $fieldsSize = array( + 'stock_management' => 32, + 'reference' => 45, + 'name' => 45 + ); protected $fieldsValidate = array( 'id_address' => 'isUnsignedId', 'reference' => 'isString', 'name' => 'isString', 'id_employee' => 'isUnsignedId', - 'stock_management' => 'isStockManagement'); + 'stock_management' => 'isStockManagement' + ); protected $table = 'warehouse'; protected $identifier = 'id_warehouse'; @@ -54,14 +66,16 @@ class StockWarehouseCore extends ObjectModel $this->validateFields(); $fields['id_address'] = (int)$this->id_addresse; $fields['reference'] = $this->reference; - $fields['name'] = $this->name; + $fields['name'] = pSQL($this->name); $fields['id_employee'] = (int)$this->id_employee; $fields['stock_management'] = $this->$stock_management; return $fields; } /** - * Get a list of shop ids linked to a warehouse + * For the current warehouse, gets its shops ids list + * + * @return array */ public function getIdShopList() { @@ -71,29 +85,25 @@ class StockWarehouseCore extends ObjectModel $query->where($this->identifier.' = '.(int)$this->id); return (int)Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS($query); } - /** - * Linked a list of shop ids to a warehouse - * - * @param $shopIdList List of shop ids to linked to the warehouse + * For the current warehouse, sets its shop ids list + * + * @param array $id_shop_list List of shop ids */ public function setIdShopList($id_shop_list) { - $row_to_instert = array(); + $row_to_insert = array(); foreach ($id_shop_list as $id_shop) - { - $row_to_instert = array( - $this->reference => $this->id, - 'id_shop' => $id_shop); - } + $row_to_insert = array($this->reference => $this->id, 'id_shop' => $id_shop); + Db::getInstance()->ExecuteS('DELETE INTO `warehouse_shop` ws WHERE ws.'.$this->identifier.' = '.(int)$this->id); - Db::getInstance()->autoExecute('warehouse_shop', $row_to_instert, 'INSERT'); + Db::getInstance()->autoExecute('warehouse_shop', $row_to_insert, 'INSERT'); } - + /** - * Check if a warehouse exists or not - * + * For a given warehouse, checks if it exists + * * @param $id_warehouse warehouse identifier */ public static function exists($id_warehouse)