Qual: Make code simpler.

This commit is contained in:
Laurent Destailleur 2013-04-22 13:01:22 +02:00
parent 54040bed1d
commit c87c7703cd
2 changed files with 43 additions and 68 deletions

View File

@ -1803,7 +1803,7 @@ class Product extends CommonObject
$result=$this->del_sousproduit($id_pere, $id_fils);
if ($result < 0) return $result;
// Check not already father of id_pere (to avoid father -> child -> father links)
$sql = 'SELECT fk_product_pere from '.MAIN_DB_PREFIX.'product_association';
$sql .= ' WHERE fk_product_pere = '.$id_fils.' AND fk_product_fils = '.$id_pere;
@ -1852,11 +1852,11 @@ class Product extends CommonObject
{
if (! is_numeric($fk_parent)) $fk_parent=0;
if (! is_numeric($fk_child)) $fk_child=0;
$sql = "DELETE FROM ".MAIN_DB_PREFIX."product_association";
$sql.= " WHERE fk_product_pere = ".$fk_parent;
$sql.= " AND fk_product_fils = ".$fk_child;
dol_syslog(get_class($this).'::del_sousproduit sql='.$sql);
if (! $this->db->query($sql))
{
@ -2122,7 +2122,7 @@ class Product extends CommonObject
* Define value of this->res
*
* @param array $prod Products array
* @param string $compl_path Directory path
* @param string $compl_path Directory path of parents to add before
* @param int $multiply Because each sublevel must be multiplicated by parent nb
* @param int $level Init level
* @return void
@ -2132,7 +2132,8 @@ class Product extends CommonObject
global $conf,$langs;
$product = new Product($this->db);
foreach($prod as $nom_pere => $desc_pere)
//var_dump($prod);
foreach($prod as $id_product => $desc_pere) // nom_pere is 0 or id of sub_product
{
if (is_array($desc_pere)) // If this parent desc is an array, this is an array of childs
{
@ -2140,58 +2141,27 @@ class Product extends CommonObject
$nb=(! empty($desc_pere[1]) ? $desc_pere[1] :'');
$type=(! empty($desc_pere[2]) ? $desc_pere[2] :'');
$label=(! empty($desc_pere[3]) ? $desc_pere[3] :'');
if ($multiply < 1) $multiply=1;
if ($multiply)
{
//print "XXX ".$desc_pere[1]." multiply=".$multiply;
$img="";
$this->fetch($id);
$this->load_stock();
if ($this->stock_warehouse[1]->real < $this->seuil_stock_alerte)
{
$img=img_warning($langs->trans("StockTooLow"));
}
$this->res[]= array(
/* "<tr><td>&nbsp; &nbsp; &nbsp; ->
<a href=\"".DOL_URL_ROOT."/product/fiche.php?id=".$desc_pere[0]."\">".$compl_path.stripslashes($nom_pere)."
</a> (".$desc_pere[1].")</td><td align=\"center\"> ".($desc_pere[1]*$multiply)."</td><td>&nbsp</td><td>&nbsp</td>
<td align=\"center\">".$this->stock_entrepot[1]." ".$img."</td></tr>",
$desc_pere[0], // Id product
*/ 'id'=>$id, // Id product
'nb'=>$nb, // Nb of units that compose parent product
'nb_total'=>$nb*$multiply, // Nb of units for all nb of product
'stock'=>$this->stock_warehouse[1]->real, // Stock
'stock_alert'=>$this->seuil_stock_alerte, // Stock alert
'fullpath' => $compl_path.$label, // Label
'type'=>$type // Nb of units that compose parent product
);
}
else
{
$this->fetch($desc_pere[0]);
$this->load_stock();
$this->res[]= array(
/* $compl_path.$nom_pere." (".$desc_pere[1].")",
$desc_pere[0], // Id product
*/ 'id'=>$id, // Id product
//print "XXX We add id=".$id." - label=".$label." - nb=".$nb." - multiply=".$multiply." fullpath=".$compl_path.$label."\n";
$this->fetch($id);
$this->load_stock();
$this->res[]= array(
'id'=>$id, // Id product
'nb'=>$nb, // Nb of units that compose parent product
'nb_total'=>$nb, // Nb of units for all nb of product
'stock'=>$this->stock_warehouse[1]->real, // Stock
'nb_total'=>$nb*$multiply, // Nb of units for all nb of product
'stock'=>$this->stock_warehouse[1]->real, // Stock
'stock_alert'=>$this->seuil_stock_alerte, // Stock alert
'fullpath' => $compl_path.$nom_pere, // Label
'type'=>$type // Nb of units that compose parent product
);
}
}
else if($nom_pere != "0" && $nom_pere != "1")
{
$this->res[]= array($compl_path.$nom_pere,$desc_pere);
'fullpath' => $compl_path.$label, // Label
'type'=>$type // Nb of units that compose parent product
);
}
// Recursive call if child is an array
if (is_array($desc_pere[0]))
if (is_array($desc_pere['childs']))
{
$this ->fetch_prod_arbo($desc_pere[0], $nom_pere." -> ", $desc_pere[1]*$multiply, $level+1);
//print 'YYY We go down for '.$desc_pere[3]." -> \n";
$this ->fetch_prod_arbo($desc_pere['childs'], $compl_path.$desc_pere[3]." -> ", $desc_pere[1]*$multiply, $level+1);
}
}
}
@ -2228,11 +2198,12 @@ class Product extends CommonObject
$this->res = array();
if (isset($this->sousprods) && is_array($this->sousprods))
{
foreach($this->sousprods as $nom_pere => $desc_pere)
foreach($this->sousprods as $prod_name => $desc_product)
{
if (is_array($desc_pere)) $this->fetch_prod_arbo($desc_pere,"",$multiply);
if (is_array($desc_product)) $this->fetch_prod_arbo($desc_product,"",$multiply);
}
}
//var_dump($this->res);
return $this->res;
}
@ -2293,7 +2264,7 @@ class Product extends CommonObject
/**
* Return all parent products fo current product
* Return all direct parent products fo current product
*
* @return array prod
*/
@ -2324,19 +2295,19 @@ class Product extends CommonObject
}
/**
* Return childs of product with if fk_parent
* Return childs of product $id
*
* @param int $fk_parent Id of product to search childs of
* @param int $id Id of product to search childs of
* @return array Prod
*/
function getChildsArbo($fk_parent)
function getChildsArbo($id)
{
$sql = "SELECT p.rowid, p.label as label, pa.qty as qty, pa.fk_product_fils as id, p.fk_product_type";
$sql.= " FROM ".MAIN_DB_PREFIX."product as p";
$sql.= ", ".MAIN_DB_PREFIX."product_association as pa";
$sql.= " WHERE p.rowid = pa.fk_product_fils";
$sql.= " AND pa.fk_product_pere = ".$fk_parent;
$sql.= " AND pa.fk_product_fils != ".$fk_parent; // This should not happens, it is to avoid invinite loop if it happens
$sql.= " AND pa.fk_product_pere = ".$id;
$sql.= " AND pa.fk_product_fils != ".$id; // This should not happens, it is to avoid infinite loop if it happens
dol_syslog(get_class($this).'::getChildsArbo sql='.$sql);
$res = $this->db->query($sql);
@ -2351,7 +2322,7 @@ class Product extends CommonObject
$listofchilds=$this->getChildsArbo($rec['id']);
foreach($listofchilds as $keyChild => $valueChild)
{
$prods[$rec['rowid']][$keyChild] = $valueChild;
$prods[$rec['rowid']]['childs'][$keyChild] = $valueChild;
}
}
@ -2373,14 +2344,14 @@ class Product extends CommonObject
function get_sousproduits_arbo()
{
$parent = $this->getParent();
foreach($parent as $key => $value)
foreach($parent as $key => $value) // key=label, value[0]=id
{
foreach($this->getChildsArbo($value[0]) as $keyChild => $valueChild)
{
$parent[$key][$keyChild] = $valueChild;
}
}
foreach($parent as $key => $value)
foreach($parent as $key => $value) // key=label, value is array of childs
{
$this->sousprods[$key] = $value;
}
@ -2418,7 +2389,7 @@ class Product extends CommonObject
else if ($option == 'category')
{
$lien = '<a href="'.DOL_URL_ROOT.'/categories/categorie.php?id='.$this->id.'&type=0">';
}
}
else
{
$lien = '<a href="'.DOL_URL_ROOT.'/product/fiche.php?id='.$this->id.'">';

View File

@ -227,7 +227,7 @@ if ($id || $ref)
//print $value[0]; // This contains a tr line.
print '<tr>';
//print '<td>'.$productstatic->getNomUrl(1,'composition').' ('.$value['nb'].($value['nb_total'] > $value['nb']?'->'.$value['nb_total']:'').') &nbsp &nbsp</td>';
print '<td>'.$productstatic->getNomUrl(1,'composition').' ('.$value['nb_total'].') &nbsp &nbsp</td>';
print '<td>'.$productstatic->getNomUrl(1,'composition').' ('.$value['nb'].') &nbsp &nbsp</td>';
if (! empty($conf->stock->enabled)) print '<td>'.$langs->trans("Stock").' : <b>'.$productstatic->stock_reel.'</b></td>';
print '</tr>';
}
@ -292,12 +292,14 @@ if ($id || $ref)
// Number of subproducts
$prodsfather = $product->getFather(); //Parent Products
$product->get_sousproduits_arbo();
print '<tr><td>'.$langs->trans("AssociatedProductsNumber").'</td><td>'.count($product->get_arbo_each_prod()).'</td>';
$prods_arbo=$product->get_arbo_each_prod();
$nbofsubproducts=count($prods_arbo);
print '<tr><td>'.$langs->trans("AssociatedProductsNumber").'</td><td>';
print $form->textwithpicto($nbofsubproducts, $langs->trans('IfZeroItIsNotAVirtualProduct'));
print '</td>';
print '</tr>';
// List of subproducts
$prods_arbo = $product->get_arbo_each_prod();
//var_dump($prods_arbo);
if(count($prods_arbo) > 0)
{
print '<tr><td colspan="2">';
@ -322,8 +324,10 @@ if ($id || $ref)
print '</td></tr>';
}
// Number of parent products
print '<tr><td>'.$langs->trans("ParentProductsNumber").'</td><td>'.count($prodsfather).'</td>';
// Number of parent virtual products
print '<tr><td>'.$langs->trans("ParentProductsNumber").'</td><td>';
print $form->textwithpicto(count($prodsfather), $langs->trans('IfZeroItIsNotUsedByVirtualProduct'));
print '</td>';
if (count($prodsfather) > 0)
{