diff --git a/classes/stock/Stock.php b/classes/stock/Stock.php new file mode 100644 index 000000000..39a6c1ad0 --- /dev/null +++ b/classes/stock/Stock.php @@ -0,0 +1,72 @@ + +* @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)); + } +} \ No newline at end of file diff --git a/classes/stock/StockManagerFactory.php b/classes/stock/StockManagerFactory.php new file mode 100644 index 000000000..6bbd85882 --- /dev/null +++ b/classes/stock/StockManagerFactory.php @@ -0,0 +1,79 @@ + +* @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; + } +} \ No newline at end of file diff --git a/classes/stock/StockManagerInterface.php b/classes/stock/StockManagerInterface.php new file mode 100644 index 000000000..9fbdf99e3 --- /dev/null +++ b/classes/stock/StockManagerInterface.php @@ -0,0 +1,111 @@ + +* @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); + +} \ No newline at end of file diff --git a/classes/stock/StockManagerModule.php b/classes/stock/StockManagerModule.php new file mode 100644 index 000000000..8ca037ea7 --- /dev/null +++ b/classes/stock/StockManagerModule.php @@ -0,0 +1,59 @@ + +* @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; + } +} +