// StockManagement : Interface/Factory/Module

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@9126 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
bMancone
2011-10-07 09:56:55 +00:00
parent 14542fea44
commit 92b032e8dd
4 changed files with 321 additions and 0 deletions
+72
View File
@@ -0,0 +1,72 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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
*/
/**
* @since 1.5.0
*/
class StockCore extends ObjectModel
{
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 $fieldsSize = array();
protected $fieldsValidate = array(
'id_product' => 'isUnsignedId',
'id_product_attribute' => 'isUnsignedId',
'physical_quantity' => 'isUnsignedInt',
'usable_quantity' => 'isInt',
'price_te' => 'isPrice');
protected $table = 'stock';
protected $identifier = 'id_stock';
public function getFields()
{
$this->validateFields();
$fields['id_product'] = (int)$this->id_product;
$fields['id_product_attribute'] = (int)$this->id_product_attribute;
$fields['physical_quantity'] = (int)$this->physical_quantity;
$fields['usable_quantity'] = (int)$this->usable_quantity;
$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));
}
}
+79
View File
@@ -0,0 +1,79 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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
*/
/*
* StockManagerFactory : factory of stock manager
* @since 1.5.0
*/
class StockManagerFactoryCore
{
/**
* @var $stock_manager : instance of the current StockManager.
*/
protected static $stock_manager;
/**
* Returns a StockManager that corresponds to the Product/Warehouse
*
* @return StockManagerInterface
*/
public static function getManager()
{
if (!isset(StockManagerFactory::$stock_manager))
{
$stock_manager = StockManagerFactory::execHookStockManagerFactory();
if (!($stock_manager instanceof StockManagerInterface))
$stock_manager = new StockManager();
StockManagerFactory::$stock_manager = $stock_manager;
}
return StockManagerFactory::$stock_manager;
}
/**
* Looks for a StockManager in the modules list.
*
* @return StockManagerInterface
*/
public static function execHookStockManagerFactory()
{
$module_infos = Hook::getModulesHookFrom(Hook::getIdByName('stockManager'));
$stock_manager = false;
foreach ($modules_infos as $module_infos)
{
$module_instance = Module::getInstanceByName($modules_infos['name']);
if (is_callable(array($module_instance, 'hookStockManager')))
$stock_manager = $module_instance->hookStockManager();
if ($stock_manager)
break;
}
return $stock_manager;
}
}
+111
View File
@@ -0,0 +1,111 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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
*/
/**
* StockManagerInterface : defines a way to manage stock
* @since 1.5.0
*/
interface StockManagerInterface
{
/**
* Checks if the StockManager is available
*
* @return StockManagerInterface
*/
public static function isAvailable();
/**
* For a given product, adds a given quantity
*
* @param int $id_product
* @param int id_product_attribute
* @param int $id_warehouse
* @param int $quantity
* @param int $id_stock_mouvement_reason
* @param float $price_te
* @param bool $is_usable
* @param int $id_supplier_order Optionnal
* @return bool
*/
public function addProduct($id_product, $id_product_attribute, $id_warehouse, $quantity, $id_stock_mouvement_reason, $price_te, $is_usable, $id_supplier_order = null);
/**
* For a given product, removes a given quantity
*
* @param int $id_product
* @param int id_product_attribute
* @param int $id_warehouse
* @param int $quantity
* @param int $id_stock_mouvement_reason
* @param int $id_order Optionnal
* @return bool
*/
public function removeProduct($id_product, $id_product_attribute, $id_warehouse, $quantity, $id_stock_mouvement_reason, $id_order = null);
/**
* For a given product, returns its physical quantity
*
* @param int $id_product
* @param int $id_product_attribute
* @param int $id_warehouse Optionnal
* @param bool $usable false default - in this case we retrieve all physical quantities, otherwise we retrieve physical quantities flagged as usable
* @return int
*/
public function getProductPhysicalQuantities($id_product, $id_product_attribute, $id_warehouse = null, $usable = false);
/**
* For a given product, returns its real quantity
* Real quantity : (physical_qty + supplier_orders_qty - client_orders_qty)
* If $usable is defined, real quantity: usable_qty + supplier_orders_qty - client_orders_qty
*
* @param int $id_product
* @param int $id_product_attribute
* @param bool $usable false by default
* @param int $id_warehouse Optionnal
* @return int
*/
public function getProductRealQuantities($id_product, $id_product_attribute, $id_warehouse = null, $usable = false);
/**
* For a given product, transfers quantities between two warehouses
* By default, it manages usable quantities
* It is also possible to transfer a usable quantity from warehouse 1 in an unusable quantity to warehouse 2
* It is also possible to transfer a usable quantity from warehouse 1 in an unusable quantity to warehouse 1
*
* @param int $id_product
* @param int $id_product_attribute
* @param int $quantity
* @param int $id_warehouse_from
* @param int $id_warehouse_to
* @param bool $usable_from true by default
* @param bool $usable_to true by default
* @return bool
*/
public function transferBetweenWarehouses($id_product, $id_product_attribute, $quantity, $id_warehouse_from, $id_warehouse_to, $usable_from = true, $usable_to = true);
}
+59
View File
@@ -0,0 +1,59 @@
<?php
/*
* 2007-2011 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <contact@prestashop.com>
* @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
*/
/**
* @since 1.5.0
*/
abstract class StockManagerModule extends Module
{
public $stock_manager_class;
public function install()
{
return (parent::install() && $this->registerHook('stockManager') );
}
public function hookStockManager()
{
$class_file = _PS_MODULE_DIR_.'/'.$this->name.'/'.$this->stock_manager_class.'.php';
if (!isset($this->stock_manager_class) || !file_exists($class_file))
die(Tools::displayError('Incorrect Stock Manager class ['.$this->stock_manager_class.']'));
require_once($class_file);
if (!class_exists($this->stock_manager_class))
die(Tools::displayError('Stock Manager class not found ['.$this->stock_manager_class.']'));
$class = $this->stock_manager_class;
if (call_user_func(array($class, 'isAvailable')))
return new $class();
return false;
}
}