Optimize code and performance of module variant
This commit is contained in:
parent
d5b10a18b7
commit
6fef6ac2fb
@ -102,10 +102,13 @@ function product_prepare_head($object)
|
||||
|
||||
$prodcomb = new ProductCombination($db);
|
||||
|
||||
if ($prodcomb->fetchByFkProductChild($object->id) == -1) {
|
||||
if ($prodcomb->fetchByFkProductChild($object->id) == -1)
|
||||
{
|
||||
$head[$h][0] = DOL_URL_ROOT."/variants/combinations.php?id=".$object->id;
|
||||
$head[$h][1] = $langs->trans('ProductCombinations');
|
||||
$head[$h][2] = 'combinations';
|
||||
$nbVariant = $prodcomb->countNbOfCombinationForFkProductParent($object->id);
|
||||
if ($nbVariant > 0) $head[$h][1].= ' <span class="badge">'.$nbVariant.'</span>';
|
||||
}
|
||||
|
||||
$h++;
|
||||
|
||||
@ -288,7 +288,8 @@ DoNotRemovePreviousCombinations=Do not remove previous variants
|
||||
UsePercentageVariations=Use percentage variations
|
||||
PercentageVariation=Percentage variation
|
||||
ErrorDeletingGeneratedProducts=There was an error while trying to delete existing product variants
|
||||
NbProducts=No. of products
|
||||
NbOfDifferentValues=Nb of different values
|
||||
NbProducts=Nb. of products
|
||||
ParentProduct=Parent product
|
||||
HideChildProducts=Hide child products
|
||||
ConfirmCloneProductCombinations=Would you like to copy all the product variant to the product with the given reference?
|
||||
|
||||
@ -36,6 +36,7 @@ if ($prodattr->fetch($id) < 1) {
|
||||
exit();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Actions
|
||||
*/
|
||||
@ -110,6 +111,11 @@ if ($confirm == 'yes') {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* View
|
||||
*/
|
||||
|
||||
$langs->load('products');
|
||||
|
||||
$title = $langs->trans('ProductAttributeName', dol_htmlentities($prodattr->label));
|
||||
|
||||
@ -73,9 +73,7 @@ class ProductAttribute
|
||||
return -1;
|
||||
}
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT.'/variants/lib/product_variants.lib.php';
|
||||
|
||||
$sql = "SELECT rowid, ref, label, rang FROM ".MAIN_DB_PREFIX."product_attribute WHERE rowid = ".(int) $id." AND entity IN (".getProductEntities($this->db).")";
|
||||
$sql = "SELECT rowid, ref, label, rang FROM ".MAIN_DB_PREFIX."product_attribute WHERE rowid = ".(int) $id." AND entity IN (".getEntity('product', 1).")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
@ -100,11 +98,9 @@ class ProductAttribute
|
||||
*/
|
||||
public function fetchAll()
|
||||
{
|
||||
require_once DOL_DOCUMENT_ROOT.'/variants/lib/product_variants.lib.php';
|
||||
|
||||
$return = array();
|
||||
|
||||
$sql = 'SELECT rowid, ref, label, rang FROM '.MAIN_DB_PREFIX."product_attribute WHERE entity IN (".getProductEntities($this->db).')';
|
||||
$sql = 'SELECT rowid, ref, label, rang FROM '.MAIN_DB_PREFIX."product_attribute WHERE entity IN (".getEntity('product', 1).')';
|
||||
$sql .= $this->db->order('rang', 'asc');
|
||||
$query = $this->db->query($sql);
|
||||
if ($query)
|
||||
@ -183,6 +179,21 @@ class ProductAttribute
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of values for this attribute
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function countChildValues()
|
||||
{
|
||||
$sql = "SELECT COUNT(*) count FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE fk_product_attribute = ".(int) $this->id;
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
$result = $this->db->fetch_object($query);
|
||||
|
||||
return $result->count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of products that are using this attribute
|
||||
*
|
||||
@ -190,10 +201,8 @@ class ProductAttribute
|
||||
*/
|
||||
public function countChildProducts()
|
||||
{
|
||||
require_once __DIR__.'/../lib/product_variants.lib.php';
|
||||
|
||||
$sql = "SELECT COUNT(*) count FROM ".MAIN_DB_PREFIX."product_attribute_combination2val pac2v
|
||||
LEFT JOIN ".MAIN_DB_PREFIX."product_attribute_combination pac ON pac2v.fk_prod_combination = pac.rowid WHERE pac2v.fk_prod_attr = ".(int) $this->id." AND pac.entity IN (".getProductEntities($this->db).")";
|
||||
LEFT JOIN ".MAIN_DB_PREFIX."product_attribute_combination pac ON pac2v.fk_prod_combination = pac.rowid WHERE pac2v.fk_prod_attr = ".(int) $this->id." AND pac.entity IN (".getEntity('product', 1).")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
@ -202,6 +211,7 @@ class ProductAttribute
|
||||
return $result->count;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reorders the order of the variants.
|
||||
* This is an internal function used by moveLine function
|
||||
|
||||
@ -68,9 +68,7 @@ class ProductAttributeValue
|
||||
*/
|
||||
public function fetch($valueid)
|
||||
{
|
||||
require_once __DIR__.'/../lib/product_variants.lib.php';
|
||||
|
||||
$sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $valueid." AND entity IN (".getProductEntities($this->db).")";
|
||||
$sql = "SELECT rowid, fk_product_attribute, ref, value FROM ".MAIN_DB_PREFIX."product_attribute_value WHERE rowid = ".(int) $valueid." AND entity IN (".getEntity('product', 1).")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
@ -101,8 +99,6 @@ class ProductAttributeValue
|
||||
*/
|
||||
public function fetchAllByProductAttribute($prodattr_id, $only_used = false)
|
||||
{
|
||||
require_once __DIR__.'/../lib/product_variants.lib.php';
|
||||
|
||||
$return = array();
|
||||
|
||||
$sql = 'SELECT ';
|
||||
|
||||
@ -86,9 +86,7 @@ class ProductCombination
|
||||
*/
|
||||
public function fetch($rowid)
|
||||
{
|
||||
require_once __DIR__.'/../lib/product_variants.lib.php';
|
||||
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $rowid." AND entity IN (".getProductEntities($this->db).")";
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE rowid = ".(int) $rowid." AND entity IN (".getEntity('product', 1).")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
@ -120,9 +118,7 @@ class ProductCombination
|
||||
*/
|
||||
public function fetchByFkProductChild($fk_child)
|
||||
{
|
||||
require_once __DIR__.'/../lib/product_variants.lib.php';
|
||||
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_child = ".(int) $fk_child." AND entity IN (".getProductEntities($this->db).")";
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_child = ".(int) $fk_child." AND entity IN (".getEntity('product', 1).")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
@ -154,9 +150,7 @@ class ProductCombination
|
||||
*/
|
||||
public function fetchAllByFkProductParent($fk_product_parent)
|
||||
{
|
||||
require_once __DIR__.'/../lib/product_variants.lib.php';
|
||||
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".(int) $fk_product_parent." AND entity IN (".getProductEntities($this->db).")";
|
||||
$sql = "SELECT rowid, fk_product_parent, fk_product_child, variation_price, variation_price_percentage, variation_weight FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".(int) $fk_product_parent." AND entity IN (".getEntity('product', 1).")";
|
||||
|
||||
$query = $this->db->query($sql);
|
||||
|
||||
@ -182,6 +176,26 @@ class ProductCombination
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves all product combinations by the product parent row id
|
||||
*
|
||||
* @param int $fk_product_parent Id of parent product
|
||||
* @return int Nb of record
|
||||
*/
|
||||
public function countNbOfCombinationForFkProductParent($fk_product_parent)
|
||||
{
|
||||
$nb = 0;
|
||||
$sql = "SELECT count(rowid) as nb FROM ".MAIN_DB_PREFIX."product_attribute_combination WHERE fk_product_parent = ".(int) $fk_product_parent." AND entity IN (".getEntity('product', 1).")";
|
||||
|
||||
$resql = $this->db->query($sql);
|
||||
if ($resql) {
|
||||
$obj = $this->db->fetch_object($resql);
|
||||
if ($obj) $nb = $obj->nb;
|
||||
}
|
||||
|
||||
return $nb;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a product attribute combination
|
||||
*
|
||||
|
||||
@ -52,6 +52,7 @@ if ($id > 0 || $ref)
|
||||
$object->fetch($id, $ref);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* Actions
|
||||
*/
|
||||
|
||||
@ -1,31 +0,0 @@
|
||||
<?php
|
||||
|
||||
/* Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Returns the entity of the products.
|
||||
* @param DoliDB $db Database handler
|
||||
* @return mixed
|
||||
*/
|
||||
function getProductEntities(DoliDB $db)
|
||||
{
|
||||
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
|
||||
|
||||
$product = new Product($db);
|
||||
|
||||
return getEntity($product->element, 1);
|
||||
}
|
||||
@ -18,7 +18,7 @@
|
||||
require '../main.inc.php';
|
||||
require DOL_DOCUMENT_ROOT.'/variants/class/ProductAttribute.class.php';
|
||||
|
||||
$rowid = GETPOST('rowid');
|
||||
$id = GETPOST('id');
|
||||
$action = GETPOST('action');
|
||||
$object = new ProductAttribute($db);
|
||||
|
||||
@ -109,14 +109,16 @@ $forcereloadpage=empty($conf->global->MAIN_FORCE_RELOAD_PAGE)?0:1;
|
||||
<tr class="liste_titre nodrag nodrop">
|
||||
<th class="liste_titre"><?php print $langs->trans('Ref') ?></th>
|
||||
<th class="liste_titre"><?php print $langs->trans('Label') ?></th>
|
||||
<th class="liste_titre"><?php print $langs->trans('NbProducts') ?></th>
|
||||
<th class="liste_titre" align="right"><?php print $langs->trans('NbOfDifferentValues') ?></th>
|
||||
<th class="liste_titre" align="right"><?php print $langs->trans('NbProducts') ?></th>
|
||||
<th class="liste_titre" colspan="2"></th>
|
||||
</tr>
|
||||
<?php foreach ($variants as $key => $attribute): ?>
|
||||
<tr id="row-<?php echo $attribute->id ?>" <?php echo $bcdd[$var] ?>>
|
||||
<td><a href="card.php?id=<?php echo $attribute->id ?>"><?php echo dol_htmlentities($attribute->ref) ?></a></td>
|
||||
<td><a href="card.php?id=<?php echo $attribute->id ?>"><?php echo dol_htmlentities($attribute->label) ?></a></td>
|
||||
<td><?php echo $attribute->countChildProducts() ?></td>
|
||||
<td align="right"><?php echo $attribute->countChildValues() ?></td>
|
||||
<td align="right"><?php echo $attribute->countChildProducts() ?></td>
|
||||
<td style="text-align: right">
|
||||
<a href="card.php?id=<?php echo $attribute->id ?>&action=edit"><?php echo img_edit() ?></a>
|
||||
<a href="card.php?id=<?php echo $attribute->id ?>&action=delete"><?php echo img_delete() ?></a>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user