From ddfb60d69a475c1bdefea93dc7ea51bc9d8b6325 Mon Sep 17 00:00:00 2001 From: dSevere Date: Tue, 22 Nov 2011 12:39:30 +0000 Subject: [PATCH] // Stock Available : bug fix --- .../themes/template/products/quantities.tpl | 51 ++++------------ classes/Product.php | 58 ++----------------- classes/stock/StockAvailable.php | 48 +++++++++++++++ controllers/admin/AdminProductsController.php | 30 +++------- 4 files changed, 72 insertions(+), 115 deletions(-) diff --git a/admin-dev/themes/template/products/quantities.tpl b/admin-dev/themes/template/products/quantities.tpl index 6263f54b6..bf38b7016 100644 --- a/admin-dev/themes/template/products/quantities.tpl +++ b/admin-dev/themes/template/products/quantities.tpl @@ -53,6 +53,7 @@ } }); }; + var refreshQtyAvaibilityForm = function() { if ($('#depends_on_stock_0').attr('checked')) @@ -66,17 +67,20 @@ $('.available_quantity').find('span').show(); } }; + $('.depends_on_stock').click(function(e) { refreshQtyAvaibilityForm(); - ajaxCall( { actionQty: 'depends_on_stock', value: $(this).attr('value') } ); + ajaxCall( { actionQty: 'depends_on_stock', value: $(this).val() } ); if($(this).val() == 0) $('.available_quantity input').trigger('change'); }); + $('.available_quantity').find('input').change(function(e) { ajaxCall( { actionQty: 'set_qty', id_product_attribute: $(this).parent().attr('id').split('_')[1], value: $(this).val() } ); }); + $('.available_quantity').find('input').click(function(e) { if(typeof(this.intervalId) != 'undefined') @@ -91,50 +95,18 @@ } }, 500, this, $(this).val()) }); + $('.out_of_stock').click(function(e) { + refreshQtyAvaibilityForm(); ajaxCall( { actionQty: 'out_of_stock', value: $(this).val() } ); }); + refreshQtyAvaibilityForm(); -
-

8. {l s='Quantities'}

- - - - - - -
- {l s='Available stock in warehouses'} -
-
-

{l s='There is %s quantities available in stock for this product'|sprintf:$total_quantity}

- - - - - - -
- - - - - - - - - {foreach from=$attributes item=attribute} - - - - - {/foreach} - -
{l s='Quantity'}{l s='Designation'}
{$physical_quantity[$attribute['id_product_attribute']]}{$product_designation[$attribute['id_product_attribute']]}
-
+

{l s='This interface ...'}

+
@@ -219,5 +191,4 @@ -
-
\ No newline at end of file + \ No newline at end of file diff --git a/classes/Product.php b/classes/Product.php index 42e854a31..0e44eca32 100644 --- a/classes/Product.php +++ b/classes/Product.php @@ -334,8 +334,8 @@ class ProductCore extends ObjectModel // By default, the product quantity correspond to the available quantity to sell in the current shop $this->quantity = StockAvailable::getQuantityAvailableByProduct($id_product, 0, Context::getContext()->shop->getID()); - $this->out_of_stock = $this->getOutOfStock(); - $this->depends_on_stock = $this->getDependsOnStock(); + $this->out_of_stock = StockAvailable::outOfStock($this->id); + $this->depends_on_stock = StockAvailable::dependsOnStock($this->id); if ($this->id_category_default) $this->category = Category::getLinkRewrite((int)$this->id_category_default, (int)$id_lang); @@ -944,7 +944,7 @@ class ProductCore extends ObjectModel return false; //Try to set available quantitiy if product quantity not depend on stock - $depend_on_stock = $this->getDependsOnStock(); + $depend_on_stock = StockAvailable::dependsOnStock($this->id); if (!$depend_on_stock) if (!StockAvailable::updateQuantity($this->id, $id_product_attribute, $quantity)) @@ -954,7 +954,7 @@ class ProductCore extends ObjectModel $stock_available->id_product_attribute = (int)$id_product_attribute; $stock_available->id_shop = (int)$context->shop->getID(); $stock_available->quantity = (int)$quantity; - $stock_available->out_of_stock = $this->getOutOfStock(); + $stock_available->out_of_stock = StockAvailable::outOfStock($this->id); $stock_available->depends_on_stock = 0; $stock_available->save(); } @@ -2314,54 +2314,6 @@ class ProductCore extends ObjectModel return $sql; } - /** - * Get the value of "out of stock" of current product - * "ouf of stock" allow to order product without stock - * - * @since 1.5.0 - * @param Context $context - * @return int - */ - public function getOutOfStock(Context $context = null) - { - if (!$this->id) - return 0; - if (!$context) - $context = Context::getContext(); - - $id_shop = $context->shop->getID(true); - $sql = 'SELECT out_of_stock - FROM '._DB_PREFIX_.'stock_available - WHERE id_product = '.$this->id.' - AND id_product_attribute = 0'. - $context->shop->addSqlRestriction(); - return (int)Db::getInstance()->getValue($sql); - } - - /** - * Get the value of "depends on stock" of current product - * "depends on stock" allow managing quantity available manually - * - * @since 1.5.0 - * @param Context $context - * @return int - */ - public function getDependsOnStock(Context $context = null) - { - if (!$this->id) - return 0; - if (!$context) - $context = Context::getContext(); - - $id_shop = $context->shop->getID(true); - $sql = 'SELECT depends_on_stock - FROM '._DB_PREFIX_.'stock_available - WHERE id_product = '.$this->id.' - AND id_product_attribute = 0'. - $context->shop->addSqlRestriction(); - return (int)Db::getInstance()->getValue($sql); - } - /** * @deprecated since 1.5.0 * @@ -2458,7 +2410,7 @@ class ProductCore extends ObjectModel if (Pack::isPack((int)$this->id) && !Pack::isInStock((int)$this->id)) return false; - if ($this->isAvailableWhenOutOfStock($this->getOutOfStock())) + if ($this->isAvailableWhenOutOfStock(StockAvailable::outOfStock($this->id))) return true; if (isset($this->id_product_attribute)) diff --git a/classes/stock/StockAvailable.php b/classes/stock/StockAvailable.php index 7d1979801..f5ee3fda1 100644 --- a/classes/stock/StockAvailable.php +++ b/classes/stock/StockAvailable.php @@ -191,6 +191,31 @@ class StockAvailableCore extends ObjectModel 'id_product = '.(int)$id_product.' AND id_shop = '.(int)$id_shop ); + $existing_id = self::getStockAvailableIdByProductId((int)$id_product, 0, (int)$id_shop); + + if ($existing_id > 0) + { + Db::getInstance()->autoExecute( + _DB_PREFIX_.'stock_available', + array('depends_on_stock' => (bool)$depends_on_stock), + 'UPDATE', + 'id_product = '.(int)$id_product.' AND id_product_attribute = 0 AND id_shop = '.(int)$id_shop + ); + } + else + { + Db::getInstance()->autoExecute( + _DB_PREFIX_.'stock_available', + array( + 'depends_on_stock' => (bool)$depends_on_stock, + 'id_product' => (int)$id_product, + 'id_product_attribute' => 0, + 'id_shop' => (int)$id_shop + ), + 'INSERT' + ); + } + // depends on stock.. hence synchronizes if ($depends_on_stock) StockAvailable::synchronize($id_product); @@ -354,6 +379,29 @@ class StockAvailableCore extends ObjectModel $query->select('depends_on_stock'); $query->from('stock_available'); $query->where('id_product = '.(int)$id_product); + $query->where('id_product_attribute = 0'); + $query->where('id_shop = '.(int)$id_shop); + + return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); + } + + /** + * For a given product, get its "out of stock" flag + * + * @param int $id_product + * @param int $id_shop Optional : gets context if null @see Context::getContext() + * @return bool : depends on stock @see $depends_on_stock + */ + public function outOfStock($id_product, $id_shop = null) + { + if (is_null($id_shop)) + $id_shop = Context::getContext()->shop->getID(true); + + $query = new DbQuery(); + $query->select('out_of_stock'); + $query->from('stock_available'); + $query->where('id_product = '.(int)$id_product); + $query->where('id_product_attribute = 0'); $query->where('id_shop = '.(int)$id_shop); return (bool)Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue($query); diff --git a/controllers/admin/AdminProductsController.php b/controllers/admin/AdminProductsController.php index d75e44904..2b4f23e7e 100644 --- a/controllers/admin/AdminProductsController.php +++ b/controllers/admin/AdminProductsController.php @@ -929,7 +929,7 @@ class AdminProductsController extends AdminController $obj = new Image($result['success']['id_image']); $json = array( 'status' => 'ok', - 'id'=>$obj->id, + 'id'=>$obj->id, 'path' => $obj->getExistingImgPath(), 'position' => $obj->position, 'cover' => $obj->cover, @@ -939,12 +939,12 @@ class AdminProductsController extends AdminController else die(Tools::jsonEncode($result)); } - + public function ajaxPreProcess() { $this->action = Tools::getValue('action'); } - + public function ajaxProcessUpdateProductImageShopAsso() { if (($id_image = $_GET['id_image']) && ($id_shop = (int)$_GET['id_shop'])) @@ -953,7 +953,7 @@ class AdminProductsController extends AdminController else die(Db::getInstance()->execute('DELETE FROM '._DB_PREFIX_.'image_shop WHERE `id_image`='.(int)$id_image.' && `id_shop`='.(int)$id_shop)); } - + public function ajaxProcessUpdateImagePosition() { if ($json = Tools::getValue('json')) @@ -969,7 +969,7 @@ class AdminProductsController extends AdminController } exit(); } - + public function ajaxProcessUpdateCover() { if ($this->action == 'UpdateCover') @@ -980,7 +980,7 @@ class AdminProductsController extends AdminController $img->update(); } } - + public function ajaxProcessDeleteProductImage() { /* Delete product image */ @@ -3048,36 +3048,22 @@ class AdminProductsController extends AdminController 'attribute_designation' => '' ); - // Get physical quantities & available quantities - $stock_manager = StockManagerFactory::getManager(); - $total_quantity = 0; - $physical_quantity = array(); $available_quantity = array(); $product_designation = array(); foreach ($attributes as $attribute) { - $physical_quantity[$attribute['id_product_attribute']] = (int)$stock_manager->getProductPhysicalQuantities( - (int)$obj->id, - (int)$attribute['id_product_attribute'] - ); - - $total_quantity += $physical_quantity[$attribute['id_product_attribute']]; - // Get available quantity for the current product attribute in the current shop - $available_quantity[$attribute['id_product_attribute']] = StockAvailable::getStockAvailableForProduct( + $available_quantity[$attribute['id_product_attribute']] = StockAvailable::getQuantityAvailableByProduct( (int)$obj->id, $attribute['id_product_attribute'] ); - // Get all product designation $product_designation[$attribute['id_product_attribute']] = rtrim($obj->name[$this->context->language->id].' - '.$attribute['attribute_designation'], ' - '); } $data->assign(array( 'attributes' => $attributes, - 'total_quantity' => $total_quantity, - 'physical_quantity' => $physical_quantity, 'available_quantity' => $available_quantity, 'product_designation' => $product_designation, 'product' => $this->object, @@ -3279,7 +3265,7 @@ class AdminProductsController extends AdminController '; $json = array( 'status' => 'ok', - 'id'=>$image_obj->id, + 'id'=>$image_obj->id, 'path' => _THEME_PROD_DIR_.$img_path.'.jpg', 'path_small' => _THEME_PROD_DIR_.$img_path.'-small.jpg', 'position' => $image['position'],