diff --git a/htdocs/product/stock/replenish.php b/htdocs/product/stock/replenish.php index 946a6bb7305..00c514a899f 100644 --- a/htdocs/product/stock/replenish.php +++ b/htdocs/product/stock/replenish.php @@ -40,10 +40,10 @@ $result=restrictedArea($user,'produit|service'); function ordered($product_id) { global $db; $sql = 'SELECT DISTINCT cfd.fk_product, SUM(cfd.qty) from '; - $sql .= MAIN_DB_PREFIX.'commande_fournisseurdet as cfd LEFT JOIN '; - $sql .= MAIN_DB_PREFIX.'commande_fournisseur as cf ON '; - $sql .= 'cfd.fk_commande = cf.rowid WHERE cf.source = 42 '; - $sql .= 'AND cf.fk_statut < 5 AND cfd.fk_product = '.$product_id; + $sql .= MAIN_DB_PREFIX . 'commande_fournisseurdet as cfd '; + $sql .= 'LEFT JOIN ' . MAIN_DB_PREFIX . 'commande_fournisseur as cf'; + $sql .= ' ON cfd.fk_commande = cf.rowid WHERE cf.source = 42 '; + $sql .= 'AND cf.fk_statut < 5 AND cfd.fk_product = ' . $product_id; $sql .= ' GROUP BY cfd.fk_product'; $resql = $db->query($sql); @@ -51,7 +51,7 @@ function ordered($product_id) { $exists = $db->num_rows($resql); if($exists) { $obj = $db->fetch_array($resql); - return $obj['SUM(cfd.qty)'].' '.img_picto('','tick'); + return $obj['SUM(cfd.qty)'] . ' ' . img_picto('','tick'); } else { return img_picto('', 'stcomm-1'); @@ -60,52 +60,50 @@ function ordered($product_id) { else { $error=$db->lasterror(); dol_print_error($db); - dol_syslog("replenish.php: ".$error, LOG_ERROR); + dol_syslog('replenish.php: ' . $error, LOG_ERROR); return $langs->trans('error'); } } -$action=GETPOST('action','alpha'); -$sref=GETPOST("sref"); -$snom=GETPOST("snom"); -$sall=GETPOST("sall"); -$type=GETPOST("type","int"); -$sbarcode=GETPOST("sbarcode"); -$catid=GETPOST('catid','int'); -$tobuy = GETPOST("tobuy"); +$action = GETPOST('action','alpha'); +$sref = GETPOST('sref'); +$snom = GETPOST('snom'); +$sall = GETPOST('sall'); +$type = GETPOST('type','int'); +$sbarcode = GETPOST('sbarcode'); +$catid = GETPOST('catid','int'); +$tobuy = GETPOST('tobuy'); -$sortfield = GETPOST("sortfield",'alpha'); -$sortorder = GETPOST("sortorder",'alpha'); -$page = GETPOST("page",'int'); -if (! $sortfield) $sortfield="stock_physique"; -if (! $sortorder) $sortorder="ASC"; +$sortfield = GETPOST('sortfield','alpha'); +$sortorder = GETPOST('sortorder','alpha'); +$page = GETPOST('page','int'); +if (! $sortfield) $sortfield = 'stock_physique'; +if (! $sortorder) $sortorder = 'ASC'; $limit = $conf->liste_limit; $offset = $limit * $page ; // Load sale and categ filters -$search_sale = GETPOST("search_sale"); -$search_categ = GETPOST("search_categ"); +$search_sale = GETPOST('search_sale'); +$search_categ = GETPOST('search_categ'); -// Get object canvas (By default, this is not defined, so standard usage of dolibarr) -//$object->getCanvas($id); -$canvas=GETPOST("canvas"); -$objcanvas=''; -if (! empty($canvas)) -{ - require_once DOL_DOCUMENT_ROOT.'/core/class/canvas.class.php'; +// Get object canvas +//(By default, this is not defined, so standard usage of dolibarr) +$canvas = GETPOST('canvas'); +$objcanvas = ''; +if (! empty($canvas)) { + require_once DOL_DOCUMENT_ROOT . '/core/class/canvas.class.php'; $objcanvas = new Canvas($db,$action); - $objcanvas->getCanvas('product','list',$canvas); + $objcanvas->getCanvas('product', 'list', $canvas); } -if (! empty($_POST["button_removefilter_x"])) -{ - $sref=""; - $snom=""; - $sall=""; - $search_sale=""; - $search_categ=""; - $type=""; - $catid=''; +if (! empty($_POST['button_removefilter_x'])) { + $sref = ''; + $snom = ''; + $sall = ''; + $search_sale = ''; + $search_categ = ''; + $type = ''; + $catid = ''; } @@ -115,20 +113,21 @@ if (! empty($_POST["button_removefilter_x"])) */ //orders creation -if($action == 'order'){ +if($action == 'order') { $linecount = GETPOST('linecount', 'int'); - if($linecount > 0){ + if($linecount > 0) { $suppliers = array(); for($i = 0; $i < $linecount; $i++) { - if(GETPOST($i, 'alpha') === 'on' && GETPOST('fourn'.$i, 'int') > 0) { //one line + if(GETPOST($i, 'alpha') === 'on' + && GETPOST('fourn' . $i, 'int') > 0) { //one line $supplierpriceid = GETPOST('fourn'.$i, 'int'); //get all the parameters needed to create a line $qty = GETPOST('tobuy'.$i, 'int'); $desc = GETPOST('desc'.$i, 'alpha'); - $sql = 'Select fk_product, fk_soc, ref_fourn'; - $sql .= ', tva_tx, unitprice'; - $sql .= ' from '.MAIN_DB_PREFIX.'product_fournisseur_price'; - $sql .= ' where rowid = '.$supplierpriceid; + $sql = 'SELECT fk_product, fk_soc, ref_fourn'; + $sql .= ', tva_tx, unitprice FROM '; + $sql .= MAIN_DB_PREFIX . 'product_fournisseur_price'; + $sql .= ' WHERE rowid = ' . $supplierpriceid; $resql = $db->query($sql); if($resql && $db->num_rows($resql) > 0) { //might need some value checks @@ -140,7 +139,8 @@ if($action == 'order'){ $line->tva_tx = $obj->tva_tx; $line->subprice = $obj->unitprice; $line->total_ht = $obj->unitprice * $qty; - $line->total_tva = $line->total_ht * $line->tva_tx / 100; + $tva = $line->tva_tx / 100; + $line->total_tva = $line->total_ht * $tva; $line->total_ttc = $line->total_ht + $line->total_tva; $line->ref_fourn = $obj->ref_fourn; $suppliers[$obj->fk_soc]['lines'][] = $line; @@ -148,7 +148,7 @@ if($action == 'order'){ else { $error=$db->lasterror(); dol_print_error($db); - dol_syslog("replenish.php: ".$error, LOG_ERROR); + dol_syslog('replenish.php: '.$error, LOG_ERROR); } $db->free($resql); } @@ -157,12 +157,12 @@ if($action == 'order'){ $i = 0; $orders = array(); $suppliersid = array_keys($suppliers); - foreach($suppliers as $supplier){ + foreach($suppliers as $supplier) { $order = new CommandeFournisseur($db); $order->socid = $suppliersid[$i]; - //little trick to know which orders have been generated this way + //trick to know which orders have been generated this way $order->source = 42; - foreach($supplier['lines'] as $line){ + foreach($supplier['lines'] as $line) { $order->lines[] = $line; } $id = $order->create($user); @@ -180,163 +180,254 @@ if($action == 'order'){ $htmlother=new FormOther($db); -$title=$langs->trans("Replenishment"); +$title=$langs->trans('Replenishment'); -$sql = 'SELECT p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type,'; -$sql.= ' p.fk_product_type, p.tms as datem,'; -$sql.= ' p.duration, p.tobuy, p.seuil_stock_alerte,'; -$sql.= ' SUM(s.reel) as stock_physique'; +$sql = 'SELECT p.rowid, p.ref, p.label, p.barcode, p.price'; +$sql .= ', p.price_ttc, p.price_base_type,p.fk_product_type'; +$sql .= ', p.tms as datem, p.duration, p.tobuy, p.seuil_stock_alerte,'; +$sql .= ' SUM(s.reel) as stock_physique'; $sql .= ', p.desiredstock'; -$sql.= ' FROM ('.MAIN_DB_PREFIX.'product as p'; -// We'll need this table joined to the select in order to filter by categ -if ($search_categ) $sql.= ", ".MAIN_DB_PREFIX."categorie_product as cp"; -$sql .= ') LEFT JOIN '.MAIN_DB_PREFIX.'product_fournisseur_price as pf on p.rowid = pf.fk_product'; -$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as s on p.rowid = s.fk_product'; +$sql .= ' FROM (' . MAIN_DB_PREFIX . 'product as p'; +// need this table joined to the select in order to filter by categ +if ($search_categ) { + $sql.= ", " . MAIN_DB_PREFIX . "categorie_product as cp"; +} +$sql .= ') LEFT JOIN ' . MAIN_DB_PREFIX . 'product_fournisseur_price as pf'; +$sql .= ' ON p.rowid = pf.fk_product'; +$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product_stock as s'; +$sql .= ' ON p.rowid = s.fk_product'; -$sql.= " WHERE p.entity IN (".getEntity('product', 1).")"; -if ($search_categ) $sql.= " AND p.rowid = cp.fk_product"; // Join for the needed table to filter by categ -if ($sall) -{ - $sql.= " AND (p.ref LIKE '%".$db->escape($sall)."%' OR p.label LIKE '%".$db->escape($sall)."%' OR p.description LIKE '%".$db->escape($sall)."%' OR p.note LIKE '%".$db->escape($sall)."%')"; +$sql.= ' WHERE p.entity IN (' . getEntity("product", 1) . ')'; +if ($search_categ) { // Join for the needed table to filter by categ + $sql .= ' AND p.rowid = cp.fk_product'; +} +if ($sall) { + $sql .= ' AND (p.ref LIKE "%'.$db->escape($sall).'%" '; + $sql .= 'OR p.label LIKE "%'.$db->escape($sall).'%" '; + $sql .= 'OR p.description LIKE "%'.$db->escape($sall).'%" '; + $sql .= 'OR p.note LIKE "%'.$db->escape($sall).'%")'; } // if the type is not 1, we show all products (type = 0,2,3) -if (dol_strlen($type)) -{ - if ($type==1) - { - $sql.= " AND p.fk_product_type = '1'"; +if (dol_strlen($type)) { + if ($type == 1) { + $sql .= ' AND p.fk_product_type = 1'; } - else - { - $sql.= " AND p.fk_product_type <> '1'"; + else { + $sql .= ' AND p.fk_product_type != 1'; } } -if ($sref) $sql.= " AND p.ref LIKE '%".$sref."%'"; -if ($sbarcode) $sql.= " AND p.barcode LIKE '%".$sbarcode."%'"; -if ($snom) $sql.= " AND p.label LIKE '%".$db->escape($snom)."%'"; - -$sql.= " AND p.tobuy = 1"; - -if (! empty($canvas)) -{ - $sql.= " AND p.canvas = '".$db->escape($canvas)."'"; +if ($sref) { + $sql .= ' AND p.ref LIKE "%' . $sref . '%"'; } -if($catid) -{ - $sql.= " AND cp.fk_categorie = ".$catid; +if ($sbarcode) { + $sql .= ' AND p.barcode LIKE "%' . $sbarcode . '%"'; +} +if ($snom) { + $sql .= ' AND p.label LIKE "%' . $db->escape($snom) . '%"'; } - $sql.= " AND p.rowid = pf.fk_product"; +$sql .= ' AND p.tobuy = 1'; + +if (!empty($canvas)) { + $sql .= ' AND p.canvas = "' . $db->escape($canvas) . '"'; +} +if($catid) { + $sql .= ' AND cp.fk_categorie = ' . $catid; +} + + $sql .= ' AND p.rowid = pf.fk_product'; // Insert categ filter -if ($search_categ) -{ - $sql .= " AND cp.fk_categorie = ".$db->escape($search_categ); +if ($search_categ) { + $sql .= ' AND cp.fk_categorie = ' . $db->escape($search_categ); } -$sql.= " GROUP BY p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type,"; -$sql.= " p.fk_product_type, p.tms,"; -$sql.= " p.duration, p.tobuy, p.seuil_stock_alerte"; -$sql .= ", p.desiredstock"; -$sql.= ' HAVING p.desiredstock > SUM(s.reel) or SUM(s.reel) is NULL'; -$sql.= $db->order($sortfield,$sortorder); -$sql.= $db->plimit($limit + 1, $offset); +$sql .= ' GROUP BY p.rowid, p.ref, p.label, p.barcode, p.price'; +$sql .= ', p.price_ttc, p.price_base_type,p.fk_product_type, p.tms'; +$sql .= ', p.duration, p.tobuy, p.seuil_stock_alerte'; +$sql .= ', p.desiredstock'; +$sql .= ' HAVING p.desiredstock > SUM(s.reel) or SUM(s.reel) is NULL'; +$sql .= $db->order($sortfield,$sortorder); +$sql .= $db->plimit($limit + 1, $offset); $resql = $db->query($sql); -if ($resql) -{ +if ($resql) { $num = $db->num_rows($resql); - $i = 0; - - if ($num == 1 && ($sall or $snom or $sref)) - { + if ($num == 1 && ($sall or $snom or $sref)) { $objp = $db->fetch_object($resql); - header("Location: fiche.php?id=$objp->rowid"); + header('Location: ../fiche.php?id=' . $objp->rowid); exit; } - $helpurl=''; - $helpurl='EN:Module_Stocks_En|FR:Module_Stock|ES:Módulo_Stocks'; + $helpurl = 'EN:Module_Stocks_En|FR:Module_Stock|'; + $helpurl .= 'ES:Módulo_Stocks'; $texte = $langs->trans('Replenishment'); - llxHeader("",$title,$helpurl,$texte); + llxHeader('', $title, $helpurl, $texte); - if ($sref || $snom || $sall || GETPOST('search')) - { - print_barre_liste($texte, $page, "replenish.php", "&sref=".$sref."&snom=".$snom."&sall=".$sall, $sortfield, $sortorder,'',$num); + if ($sref || $snom || $sall || GETPOST('search')) { + print_barre_liste($texte, + $page, + 'replenish.php', + '&sref=' . $sref . '&snom=' . $snom . '&sall=' . $sall, + $sortfield, + $sortorder, + '', + $num + ); } - else - { - print_barre_liste($texte, $page, "replenish.php", "&sref=$sref&snom=$snom&fourn_id=$fourn_id".(isset($type)?"&type=$type":""), $sortfield, $sortorder,'',$num); + else { + print_barre_liste($texte, + $page, + 'replenish.php', + '&sref=$sref&snom=$snom&fourn_id=$fourn_id' . (isset($type)?'&type=$type':''), + $sortfield, + $sortorder, + '', + $num); } - if (! empty($catid)) - { - print "