From 39bc4d5f02b7e6650397a77c95294df6d5fa0f92 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 27 Apr 2016 14:19:06 +0200 Subject: [PATCH] Optimize load_stock and load_stock_virtual --- htdocs/commande/list.php | 2 +- htdocs/product/class/product.class.php | 21 ++++++++++++++------- htdocs/product/list.php | 4 ++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/htdocs/commande/list.php b/htdocs/commande/list.php index b99d5a1d1f0..26c8a5beef9 100644 --- a/htdocs/commande/list.php +++ b/htdocs/commande/list.php @@ -716,7 +716,7 @@ if ($resql) // Get local and virtual stock and store it into cache if (empty($productstat_cache[$generic_commande->lines[$lig]->fk_product])) { $generic_product->load_stock(); - $generic_product->load_virtual_stock(); + //$generic_product->load_virtual_stock(); Already included into load_stock $productstat_cache[$generic_commande->lines[$lig]->fk_product]['stock_reel'] = $generic_product->stock_reel; $productstat_cachevirtual[$generic_commande->lines[$lig]->fk_product]['stock_reel'] = $generic_product->stock_theorique; } else { diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 1ac77af56c1..685a29cb8e1 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -109,8 +109,10 @@ class Product extends CommonObject var $localtax1_type; var $localtax2_type; - //! Stock + //! Stock real var $stock_reel; + //! Stock virtual + var $stock_theorique; //! Cost price var $cost_price; //! Average price value for product entry into stock (PMP) @@ -3341,14 +3343,16 @@ class Product extends CommonObject * Load information about stock of a product into stock_reel, stock_warehouse[] (including stock_warehouse[idwarehouse]->detail_batch for batch products) * This function need a lot of load. If you use it on list, use a cache to execute it one for each product id. * - * @return int < 0 if KO, > 0 if OK - * @see load_virtual_stock, getBatchInfo + * @param string $option '', 'nobatch' = Do not load batch information, 'novirtual' = Do not load virtual stock + * @return int < 0 if KO, > 0 if OK + * @see load_virtual_stock, getBatchInfo */ - function load_stock() + function load_stock($option='') { $this->stock_reel = 0; $this->stock_warehouse = array(); - + $this->stock_theorique = 0; + $sql = "SELECT ps.rowid, ps.reel, ps.fk_entrepot"; $sql.= " FROM ".MAIN_DB_PREFIX."product_stock as ps"; $sql.= ", ".MAIN_DB_PREFIX."entrepot as w"; @@ -3370,14 +3374,17 @@ class Product extends CommonObject $this->stock_warehouse[$row->fk_entrepot] = new stdClass(); $this->stock_warehouse[$row->fk_entrepot]->real = $row->reel; $this->stock_warehouse[$row->fk_entrepot]->id = $row->rowid; - if ($this->hasbatch()) $this->stock_warehouse[$row->fk_entrepot]->detail_batch=Productbatch::findAll($this->db,$row->rowid,1); + if ((! preg_match('/nobatch/', $option)) && $this->hasbatch()) $this->stock_warehouse[$row->fk_entrepot]->detail_batch=Productbatch::findAll($this->db,$row->rowid,1); $this->stock_reel+=$row->reel; $i++; } } $this->db->free($result); - $this->load_virtual_stock(); // This also load stats_commande_fournisseur, ... + if (! preg_match('/novirtual/', $option)) + { + $this->load_virtual_stock(); // This also load stats_commande_fournisseur, ... + } return 1; } diff --git a/htdocs/product/list.php b/htdocs/product/list.php index c111c8553cb..1df8e667569 100644 --- a/htdocs/product/list.php +++ b/htdocs/product/list.php @@ -683,10 +683,10 @@ else if (! empty($conf->stock->enabled) && $user->rights->stock->lire && $type != 1) // To optimize call of load_stock { - if ($objp->fk_product_type != 1) + if ($objp->fk_product_type != 1) // Not a service { $product_static->id = $objp->rowid; - $product_static->load_stock(); + $product_static->load_stock('nobatch'); // Load stock_reel + stock_warehouse + batch detail. This also call load_virtual_stock() } }