Fix several problem with hook printFieldListWhere

This commit is contained in:
Laurent Destailleur 2017-06-09 11:02:34 +02:00
parent bec6ad9fb3
commit 02781b41ca
4 changed files with 91 additions and 63 deletions

View File

@ -53,7 +53,7 @@ class box_produits extends ModeleBoxes
*/
function loadBox($max=5)
{
global $user, $langs, $db, $conf;
global $user, $langs, $db, $conf, $hookmanager;
$this->max=$max;
@ -69,6 +69,13 @@ class box_produits extends ModeleBoxes
$sql.= ' WHERE p.entity IN ('.getEntity($productstatic->element, 1).')';
if (empty($user->rights->produit->lire)) $sql.=' AND p.fk_product_type != 0';
if (empty($user->rights->service->lire)) $sql.=' AND p.fk_product_type != 1';
// Add where from hooks
if (is_object($hookmanager))
{
$parameters=array();
$reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
$sql.=$hookmanager->resPrint;
}
$sql.= $db->order('p.datec', 'DESC');
$sql.= $db->plimit($max, 0);

View File

@ -55,7 +55,7 @@ class box_produits_alerte_stock extends ModeleBoxes
*/
function loadBox($max=5)
{
global $user, $langs, $db, $conf;
global $user, $langs, $db, $conf, $hookmanager;
$this->max=$max;
@ -74,7 +74,14 @@ class box_produits_alerte_stock extends ModeleBoxes
$sql.= " AND p.tosell = 1 AND p.seuil_stock_alerte > 0";
if (empty($user->rights->produit->lire)) $sql.=' AND p.fk_product_type != 0';
if (empty($user->rights->service->lire)) $sql.=' AND p.fk_product_type != 1';
$sql.= " GROUP BY p.rowid, p.ref, p.label, p.price, p.price_base_type, p.price_ttc, p.fk_product_type, p.tms, p.tosell, p.tobuy, p.seuil_stock_alerte, p.entity";
// Add where from hooks
if (is_object($hookmanager))
{
$parameters=array();
$reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
$sql.=$hookmanager->resPrint;
}
$sql.= " GROUP BY p.rowid, p.ref, p.label, p.price, p.price_base_type, p.price_ttc, p.fk_product_type, p.tms, p.tosell, p.tobuy, p.seuil_stock_alerte, p.entity";
$sql.= " HAVING SUM(".$db->ifsql("s.reel IS NULL","0","s.reel").") < p.seuil_stock_alerte";
$sql.= $db->order('p.seuil_stock_alerte', 'DESC');
$sql.= $db->plimit($max, 0);

View File

@ -108,7 +108,7 @@ class Product extends CommonObject
var $localtax2_tx;
var $localtax1_type;
var $localtax2_type;
//! Stock real
var $stock_reel;
//! Stock virtual
@ -196,7 +196,7 @@ class Product extends CommonObject
public $fourn_pu;
public $fourn_price_base_type;
/**
* @deprecated
* @see ref_supplier
@ -306,7 +306,7 @@ class Product extends CommonObject
if (empty($this->localtax2_tx)) $this->localtax2_tx = 0;
if (empty($this->localtax1_type)) $this->localtax1_type = '0';
if (empty($this->localtax2_type)) $this->localtax2_type = '0';
if (empty($this->price)) $this->price = 0;
if (empty($this->price_min)) $this->price_min = 0;
@ -675,7 +675,7 @@ class Product extends CommonObject
$org->fetch($this->id);
$this->oldcopy=$org;
}
// Test if batch management is activated on existing product
// If yes, we create missing entries into product_batch
if ($this->hasbatch() && !$this->oldcopy->hasbatch())
@ -691,7 +691,7 @@ class Product extends CommonObject
$qty_batch = 0;
foreach ($ObjW->detail_batch as $detail) // Each lines of detail in product_batch of the current $ObjW = product_stock
{
if ($detail->batch == $valueforundefinedlot || $detail->batch == 'Undefined')
if ($detail->batch == $valueforundefinedlot || $detail->batch == 'Undefined')
{
// We discard this line, we will create it later
$sqlclean="DELETE FROM ".MAIN_DB_PREFIX."product_batch WHERE batch in('Undefined', '".$valueforundefinedlot."') AND fk_product_stock = ".$ObjW->id;
@ -703,7 +703,7 @@ class Product extends CommonObject
}
continue;
}
$qty_batch += $detail->qty;
}
// Quantities in batch details are not same as stock quantity,
@ -711,7 +711,7 @@ class Product extends CommonObject
if ($ObjW->real <> $qty_batch)
{
$ObjBatch = new Productbatch($this->db);
$ObjBatch->batch = $valueforundefinedlot;
$ObjBatch->batch = $valueforundefinedlot;
$ObjBatch->qty = ($ObjW->real - $qty_batch);
$ObjBatch->fk_product_stock = $ObjW->id;
@ -738,7 +738,7 @@ class Product extends CommonObject
$sql.= ", localtax2_tx = " . $this->localtax2_tx;
$sql.= ", localtax1_type = " . ($this->localtax1_type!=''?"'".$this->localtax1_type."'":"'0'");
$sql.= ", localtax2_type = " . ($this->localtax2_type!=''?"'".$this->localtax2_type."'":"'0'");
$sql.= ", barcode = ". (empty($this->barcode)?"null":"'".$this->db->escape($this->barcode)."'");
$sql.= ", fk_barcode_type = ". (empty($this->barcode_type)?"null":$this->db->escape($this->barcode_type));
@ -924,8 +924,8 @@ class Product extends CommonObject
if ($result < 0) { $error++; }
// End call triggers
}
// Delete from product_batch on product delete
// Delete from product_batch on product delete
if (! $error)
{
$sql = "DELETE FROM ".MAIN_DB_PREFIX.'product_batch';
@ -940,7 +940,7 @@ class Product extends CommonObject
$this->errors[] = $this->db->lasterror();
}
}
// Delete all child tables
if (! $error)
{
@ -1038,7 +1038,7 @@ class Product extends CommonObject
function setMultiLangs($user)
{
global $conf, $langs;
$langs_available = $langs->get_available_languages(DOL_DOCUMENT_ROOT, 0, 2);
$current_lang = $langs->getDefaultLang();
@ -1357,7 +1357,7 @@ class Product extends CommonObject
$sql.= " WHERE pfp.rowid = ".$prodfournprice;
if ($qty > 0) $sql.= " AND pfp.quantity <= ".$qty;
$sql.= " ORDER BY pfp.quantity DESC";
dol_syslog(get_class($this)."::get_buyprice first search by prodfournprice/qty", LOG_DEBUG);
$resql = $this->db->query($sql);
if ($resql)
@ -1487,7 +1487,7 @@ class Product extends CommonObject
// If multiprices are enabled, then we check if the current product is subject to price autogeneration
// Price will be modified ONLY when the first one is the one that is being modified
if (!empty($conf->global->PRODUIT_MULTIPRICES) && !$ignore_autogen && $this->price_autogen && ($level == 1))
if (!empty($conf->global->PRODUIT_MULTIPRICES) && !$ignore_autogen && $this->price_autogen && ($level == 1))
{
return $this->generateMultiprices($user, $newprice, $newpricebase, $newvat, $newnpr, $newpsq);
}
@ -1555,7 +1555,7 @@ class Product extends CommonObject
}
if (empty($localtax1)) $localtax1=0; // If = '' then = 0
if (empty($localtax2)) $localtax2=0; // If = '' then = 0
$this->db->begin();
// Ne pas mettre de quote sur les numeriques decimaux.
@ -1601,7 +1601,7 @@ class Product extends CommonObject
$this->localtax2_tx = $localtax2;
$this->localtax1_type = $localtaxtype1;
$this->localtax2_type = $localtaxtype2;
// Price by quantity
$this->price_by_qty = $newpsq;
@ -1702,7 +1702,7 @@ class Product extends CommonObject
$this->url = $obj->url;
$this->note_private = $obj->note_private;
$this->note = $obj->note_private; // deprecated
$this->type = $obj->fk_product_type;
$this->status = $obj->tosell;
$this->status_buy = $obj->tobuy;
@ -1726,7 +1726,7 @@ class Product extends CommonObject
$this->localtax2_tx = $obj->localtax2_tx;
$this->localtax1_type = $obj->localtax1_type;
$this->localtax2_type = $obj->localtax2_type;
$this->finished = $obj->finished;
$this->duration = $obj->duration;
$this->duration_value = substr($obj->duration,0,dol_strlen($obj->duration)-1);
@ -1773,7 +1773,7 @@ class Product extends CommonObject
// multilangs
if (! empty($conf->global->MAIN_MULTILANGS)) $this->getMultiLangs();
// Load multiprices array
if (! empty($conf->global->PRODUIT_MULTIPRICES))
{
@ -2006,29 +2006,29 @@ class Product extends CommonObject
$this->stats_commande['rows']=$obj->nb_rows;
$this->stats_commande['qty']=$obj->qty?$obj->qty:0;
// if it's a virtual product, maybe it is in order by extension
// if it's a virtual product, maybe it is in order by extension
if (! empty($conf->global->ORDER_ADD_ORDERS_WITH_PARENT_PROD_IF_INCDEC))
{
{
$TFather = $this->getFather();
if (is_array($TFather) && !empty($TFather)) {
foreach($TFather as &$fatherData) {
$pFather = new Product($this->db);
$pFather->id = $fatherData['id'];
$pFather->id = $fatherData['id'];
$qtyCoef = $fatherData['qty'];
if ($fatherData['incdec']) {
$pFather->load_stats_commande($socid, $filtrestatut);
$this->stats_commande['customers']+=$pFather->stats_commande['customers'];
$this->stats_commande['nb']+=$pFather->stats_commande['nb'];
$this->stats_commande['rows']+=$pFather->stats_commande['rows'];
$this->stats_commande['qty']+=$pFather->stats_commande['qty'] * $qtyCoef;
}
}
}
}
return 1;
}
else
@ -2432,7 +2432,7 @@ class Product extends CommonObject
$sql = "SELECT sum(d.qty), date_format(p.datep, '%Y%m')";
if ($mode == 'bynumber') $sql.= ", count(DISTINCT p.rowid)";
$sql.= " FROM ".MAIN_DB_PREFIX."propaldet as d, ".MAIN_DB_PREFIX."propal as p, ".MAIN_DB_PREFIX."societe as s";
if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as prod";
if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as prod";
if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE p.rowid = d.fk_propal";
if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
@ -2464,7 +2464,7 @@ class Product extends CommonObject
$sql = "SELECT sum(d.qty), date_format(p.date_valid, '%Y%m')";
if ($mode == 'bynumber') $sql.= ", count(DISTINCT p.rowid)";
$sql.= " FROM ".MAIN_DB_PREFIX."supplier_proposaldet as d, ".MAIN_DB_PREFIX."supplier_proposal as p, ".MAIN_DB_PREFIX."societe as s";
if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as prod";
if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as prod";
if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE p.rowid = d.fk_supplier_proposal";
if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
@ -2479,7 +2479,7 @@ class Product extends CommonObject
return $this->_get_stats($sql,$mode);
}
/**
* Return nb of units or orders in which product is included
*
@ -2526,7 +2526,7 @@ class Product extends CommonObject
$sql = "SELECT sum(d.qty), date_format(c.date_commande, '%Y%m')";
if ($mode == 'bynumber') $sql.= ", count(DISTINCT c.rowid)";
$sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as d, ".MAIN_DB_PREFIX."commande_fournisseur as c, ".MAIN_DB_PREFIX."societe as s";
if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as p";
if ($filteronproducttype >= 0) $sql.=", ".MAIN_DB_PREFIX."product as p";
if (!$user->rights->societe->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE c.rowid = d.fk_commande";
if ($this->id > 0) $sql.= " AND d.fk_product =".$this->id;
@ -2715,7 +2715,7 @@ class Product extends CommonObject
$now=dol_now();
dol_syslog(get_class($this)."::add_fournisseur id_fourn = ".$id_fourn." ref_fourn=".$ref_fourn." quantity=".$quantity, LOG_DEBUG);
if ($ref_fourn)
{
$sql = "SELECT rowid, fk_product";
@ -3104,7 +3104,7 @@ class Product extends CommonObject
function getChildsArbo($id, $firstlevelonly=0, $level=1)
{
global $alreadyfound;
$sql = "SELECT p.rowid, p.label as label, pa.qty as qty, pa.fk_product_fils as id, p.fk_product_type, pa.incdec";
$sql.= " FROM ".MAIN_DB_PREFIX."product as p";
$sql.= ", ".MAIN_DB_PREFIX."product_association as pa";
@ -3113,18 +3113,18 @@ class Product extends CommonObject
$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 id='.$id.' level='.$level, LOG_DEBUG);
if ($level == 1) $alreadyfound=array($id=>1); // We init array of found object to start of tree, so if we found it later (should not happened), we stop immediatly
// Protection against infinite loop
if ($level > 30) return array();
$res = $this->db->query($sql);
if ($res)
{
$prods = array();
while ($rec = $this->db->fetch_array($res))
{
if (! empty($alreadyfound[$rec['rowid']]))
if (! empty($alreadyfound[$rec['rowid']]))
{
dol_syslog(get_class($this).'::getChildsArbo the product id='.$rec['rowid'].' was already found at a higher level in tree. We discard to avoid infinite loop', LOG_WARNING);
continue;
@ -3194,7 +3194,7 @@ class Product extends CommonObject
$result='';
$newref=$this->ref;
if ($maxlength) $newref=dol_trunc($newref,$maxlength,'middle');
if ($this->type == Product::TYPE_PRODUCT) $label = '<u>' . $langs->trans("ShowProduct") . '</u>';
if ($this->type == Product::TYPE_SERVICE) $label = '<u>' . $langs->trans("ShowService") . '</u>';
if (! empty($this->ref))
@ -3217,13 +3217,13 @@ class Product extends CommonObject
{
//
}
if (! empty($this->entity))
if (! empty($this->entity))
{
$tmpphoto = $this->show_photos($conf->product->multidir_output[$this->entity],1,1,0,0,0,80);
if ($this->nbphoto > 0) $label .= '<br>' . $tmpphoto;
if ($this->nbphoto > 0) $label .= '<br>' . $tmpphoto;
}
$linkclose='';
if (empty($notooltip))
{
@ -3235,7 +3235,7 @@ class Product extends CommonObject
$linkclose.= ' title="'.dol_escape_htmltag($label, 1, 1).'"';
$linkclose.=' class="classfortooltip"';
if (! is_object($hookmanager))
{
include_once DOL_DOCUMENT_ROOT.'/core/class/hookmanager.class.php';
@ -3260,7 +3260,7 @@ class Product extends CommonObject
$linkstart = '<a href="'.$url.'"';
$linkstart.=$linkclose.'>';
$linkend='</a>';
if ($withpicto) {
if ($this->type == Product::TYPE_PRODUCT) $result.=($linkstart.img_object(($notooltip?'':$label), 'product', ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend.' ');
if ($this->type == Product::TYPE_SERVICE) $result.=($linkstart.img_object(($notooltip?'':$label), 'service', ($notooltip?'':'class="classfortooltip"'), 0, 0, $notooltip?0:1).$linkend.' ');
@ -3303,7 +3303,7 @@ class Product extends CommonObject
function LibStatut($status,$mode=0,$type=0)
{
global $conf, $langs;
$langs->load('products');
if (! empty($conf->productbatch->enabled)) $langs->load("productbatch");
@ -3473,14 +3473,14 @@ 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.
* This function need a lot of load. If you use it on list, use a cache to execute it one for each product id.
* If ENTREPOT_EXTRA_STATUS set, filtering on warehouse status possible.
*
* @param string $option '' = Load all stock info, also from closed and internal warehouses,
* 'nobatch' = Do not load batch information,
* @param string $option '' = Load all stock info, also from closed and internal warehouses,
* 'nobatch' = Do not load batch information,
* 'novirtual' = Do not load virtual stock,
* 'warehouseopen' = Load stock from open warehouses,
* 'warehouseclosed' = Load stock from closed warehouses,
* 'warehouseclosed' = Load stock from closed warehouses,
* 'warehouseinternal' = Load stock from warehouses for internal correct/transfer only
* @return int < 0 if KO, > 0 if OK
* @see load_virtual_stock, getBatchInfo
@ -3488,26 +3488,26 @@ class Product extends CommonObject
function load_stock($option='')
{
global $conf;
$this->stock_reel = 0;
$this->stock_warehouse = array();
$this->stock_theorique = 0;
$warehouseStatus = array();
if (preg_match('/warehouseclosed/', $option))
if (preg_match('/warehouseclosed/', $option))
{
$warehouseStatus[] = Entrepot::STATUS_CLOSED;
}
if (preg_match('/warehouseopen/', $option))
if (preg_match('/warehouseopen/', $option))
{
$warehouseStatus[] = Entrepot::STATUS_OPEN_ALL;
}
if (preg_match('/warehouseinternal/', $option))
if (preg_match('/warehouseinternal/', $option))
{
$warehouseStatus[] = Entrepot::STATUS_OPEN_INTERNAL;
}
$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";
@ -3537,7 +3537,7 @@ class Product extends CommonObject
}
$this->db->free($result);
if (! preg_match('/novirtual/', $option))
if (! preg_match('/novirtual/', $option))
{
$this->load_virtual_stock(); // This also load stats_commande_fournisseur, ...
}
@ -3553,7 +3553,7 @@ class Product extends CommonObject
/**
* Load value ->stock_theorique of a product. Property this->id must be defined.
* This function need a lot of load. If you use it on list, use a cache to execute it one for each product id.
* 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_stock, getBatchInfo
@ -3790,7 +3790,7 @@ class Product extends CommonObject
// Find name of thumb file
$photo_vignette=basename(getImageFileNameForSize($dir.$file, '_small'));
if (! dol_is_file($dirthumb.$photo_vignette)) $photo_vignette='';
// Get filesize of original file
$imgarray=dol_getImageSize($dir.$photo);
@ -3804,9 +3804,9 @@ class Product extends CommonObject
else if ($nbbyrow < 0) $return .= '<div class="inline-block">';
$return.= "\n";
$relativefile=preg_replace('/^\//', '', $pdir.$photo);
if (empty($nolink))
if (empty($nolink))
{
$urladvanced=getAdvancedPreviewUrl('product', $relativefile);
if ($urladvanced) $return.='<a href="'.$urladvanced.'">';
@ -3817,7 +3817,7 @@ class Product extends CommonObject
// Si fichier vignette disponible et image source trop grande, on utilise la vignette, sinon on utilise photo origine
$alt=$langs->transnoentitiesnoconv('File').': '.$relativefile;
$alt.=' - '.$langs->transnoentitiesnoconv('Size').': '.$imgarray['width'].'x'.$imgarray['height'];
if (empty($maxHeight) || $photo_vignette && $imgarray['height'] > $maxHeight)
{
$return.= '<!-- Show thumb -->';
@ -4017,7 +4017,7 @@ class Product extends CommonObject
*/
function load_state_board()
{
global $conf, $user;
global $conf, $user, $hookmanager;
$this->nb=array();
@ -4025,6 +4025,13 @@ class Product extends CommonObject
$sql.= " FROM ".MAIN_DB_PREFIX."product as p";
$sql.= ' WHERE p.entity IN ('.getEntity($this->element, 1).')';
$sql.= " AND p.fk_product_type <> 1";
// Add where from hooks
if (is_object($hookmanager))
{
$parameters=array();
$reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
$sql.=$hookmanager->resPrint;
}
$resql=$this->db->query($sql);
if ($resql)
@ -4350,7 +4357,7 @@ class Product extends CommonObject
return $user->rights->service;
}
}
/**
* Load information for tab info
*

View File

@ -56,7 +56,7 @@ class Service extends CommonObject
*/
function load_state_board()
{
global $conf, $user;
global $conf, $user, $hookmanager;
$this->nb=array();
@ -64,6 +64,13 @@ class Service extends CommonObject
$sql.= " FROM ".MAIN_DB_PREFIX."product as p";
$sql.= ' WHERE p.entity IN ('.getEntity('product', 1).')';
$sql.= " AND p.fk_product_type = 1";
// Add where from hooks
if (is_object($hookmanager))
{
$parameters=array();
$reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook
$sql.=$hookmanager->resPrint;
}
$resql=$this->db->query($sql);
if ($resql)