Skip to content

Commit

Permalink
NEW: Add global PRODUCT_STOCK_LIST_SHOW_WITH_COMPILED_PHYSICAL_STOCK …
Browse files Browse the repository at this point in the history
…for this is based on compiled physical stock, not the sum of product stocks on existing links.

Because products without a warehouse no longer appear.
  • Loading branch information
kkhelifa-opendsi committed Feb 14, 2024
1 parent 21098da commit defc418
Showing 1 changed file with 59 additions and 39 deletions.
98 changes: 59 additions & 39 deletions htdocs/product/reassort.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,11 @@
$sql = 'SELECT p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type, p.entity,';
$sql .= ' p.fk_product_type, p.tms as datem,';
$sql .= ' p.duration, p.tosell as statut, p.tobuy, p.seuil_stock_alerte, p.desiredstock,';
$sql .= ' SUM(s.reel) as stock_physique';
if (getDolGlobalString('PRODUCT_STOCK_LIST_SHOW_WITH_COMPILED_PHYSICAL_STOCK')) {
$sql .= ' p.stock as stock_physique';
} else {
$sql .= ' SUM(s.reel) as stock_physique';
}
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
$sql .= ', u.short_label as unit_short';
}
Expand All @@ -150,7 +154,9 @@
$reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters, $object); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager->resPrint;
$sql .= ' FROM '.MAIN_DB_PREFIX.'product as p';
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product_stock as s ON p.rowid = s.fk_product';
if (!getDolGlobalString('PRODUCT_STOCK_LIST_SHOW_WITH_COMPILED_PHYSICAL_STOCK')) {
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'product_stock as s ON p.rowid = s.fk_product';
}
if (getDolGlobalString('PRODUCT_USE_UNITS')) {
$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_units as u on p.fk_unit = u.rowid';
}
Expand All @@ -175,43 +181,45 @@
}
$sql .= ")";
}
if (!getDolGlobalString('PRODUCT_STOCK_LIST_SHOW_VIRTUAL_WITH_NO_PHYSICAL')) {
$sql .= " AND EXISTS (SELECT e.rowid FROM ".MAIN_DB_PREFIX."entrepot as e WHERE e.rowid = s.fk_entrepot AND e.entity IN (".getEntity('stock')."))";
} else {
$sql .= " AND
if (!getDolGlobalString('PRODUCT_STOCK_LIST_SHOW_WITH_COMPILED_PHYSICAL_STOCK')) {
if (!getDolGlobalString('PRODUCT_STOCK_LIST_SHOW_VIRTUAL_WITH_NO_PHYSICAL')) {
$sql .= " AND EXISTS (SELECT e.rowid FROM " . MAIN_DB_PREFIX . "entrepot as e WHERE e.rowid = s.fk_entrepot AND e.entity IN (" . getEntity('stock') . "))";
} else {
$sql .= " AND
(
EXISTS
(SELECT e.rowid
FROM ".MAIN_DB_PREFIX."entrepot as e
WHERE e.rowid = s.fk_entrepot AND e.entity IN (".getEntity('stock')."))
EXISTS
(SELECT e.rowid
FROM " . MAIN_DB_PREFIX . "entrepot as e
WHERE e.rowid = s.fk_entrepot AND e.entity IN (" . getEntity('stock') . "))
OR (
SELECT SUM(cd1.qty) as qty
FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as cd1
LEFT JOIN ".MAIN_DB_PREFIX."commande_fournisseur as c1
ON c1.rowid = cd1.fk_commande
SELECT SUM(cd1.qty) as qty
FROM " . MAIN_DB_PREFIX . "commande_fournisseurdet as cd1
LEFT JOIN " . MAIN_DB_PREFIX . "commande_fournisseur as c1
ON c1.rowid = cd1.fk_commande
WHERE c1.entity IN (1) AND cd1.fk_product = p.rowid AND c1.fk_statut in (3,4) AND cd1.qty <> 0
) IS NOT NULL
OR (
SELECT SUM(cd2.qty) as qty
FROM ".MAIN_DB_PREFIX."commandedet as cd2
LEFT JOIN ".MAIN_DB_PREFIX."commande as c2 ON c2.rowid = cd2.fk_commande
SELECT SUM(cd2.qty) as qty
FROM " . MAIN_DB_PREFIX . "commandedet as cd2
LEFT JOIN " . MAIN_DB_PREFIX . "commande as c2 ON c2.rowid = cd2.fk_commande
WHERE c2.entity IN (1) AND cd2.fk_product = p.rowid AND c2.fk_statut in (1,2) AND cd2.qty <> 0
) IS NOT NULL
OR (
SELECT SUM(ed3.qty) as qty
FROM ".MAIN_DB_PREFIX."expeditiondet as ed3
LEFT JOIN ".MAIN_DB_PREFIX."expedition as e3 ON e3.rowid = ed3.fk_expedition
LEFT JOIN ".MAIN_DB_PREFIX."commandedet as cd3 ON ed3.fk_origin_line = cd3.rowid
LEFT JOIN ".MAIN_DB_PREFIX."commande as c3 ON c3.rowid = cd3.fk_commande
SELECT SUM(ed3.qty) as qty
FROM " . MAIN_DB_PREFIX . "expeditiondet as ed3
LEFT JOIN " . MAIN_DB_PREFIX . "expedition as e3 ON e3.rowid = ed3.fk_expedition
LEFT JOIN " . MAIN_DB_PREFIX . "commandedet as cd3 ON ed3.fk_origin_line = cd3.rowid
LEFT JOIN " . MAIN_DB_PREFIX . "commande as c3 ON c3.rowid = cd3.fk_commande
WHERE e3.entity IN (1) AND cd3.fk_product = p.rowid AND c3.fk_statut IN (1,2) AND e3.fk_statut IN (1,2) AND ed3.qty <> 0
) IS NOT NULL
OR (
SELECT SUM(mp4.qty) as qty
FROM ".MAIN_DB_PREFIX."mrp_production as mp4
LEFT JOIN ".MAIN_DB_PREFIX."mrp_mo as m4 ON m4.rowid = mp4.fk_mo AND m4.entity IN (1) AND m4.status IN (1,2)
SELECT SUM(mp4.qty) as qty
FROM " . MAIN_DB_PREFIX . "mrp_production as mp4
LEFT JOIN " . MAIN_DB_PREFIX . "mrp_mo as m4 ON m4.rowid = mp4.fk_mo AND m4.entity IN (1) AND m4.status IN (1,2)
WHERE mp4.fk_product = p.rowid AND mp4.qty <> 0
) IS NOT NULL
) ";
}
}
if ($sall) {
$sql .= natural_search(array('p.ref', 'p.label', 'p.description', 'p.note'), $sall);
Expand Down Expand Up @@ -245,32 +253,44 @@
if ($fourn_id > 0) {
$sql .= " AND p.rowid = pf.fk_product AND pf.fk_soc = ".((int) $fourn_id);
}
if (getDolGlobalString('PRODUCT_STOCK_LIST_SHOW_WITH_COMPILED_PHYSICAL_STOCK')) {
if ($search_toolowstock) {
$sql .= " AND p.stock < p.seuil_stock_alerte";
}
if ($search_stock_physique != '') {
$sql .= natural_search('p.stock', $search_stock_physique, 1, 1);
}
}
// Add where from hooks
$parameters = array();
$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters, $object); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager->resPrint;
$sql .= " GROUP BY p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type, p.entity,";
$sql .= " p.fk_product_type, p.tms, p.duration, p.tosell, p.tobuy, p.seuil_stock_alerte, p.desiredstock";
if (!getDolGlobalString('PRODUCT_STOCK_LIST_SHOW_WITH_COMPILED_PHYSICAL_STOCK')) {
$sql .= " GROUP BY p.rowid, p.ref, p.label, p.barcode, p.price, p.price_ttc, p.price_base_type, p.entity,";
$sql .= " p.fk_product_type, p.tms, p.duration, p.tosell, p.tobuy, p.seuil_stock_alerte, p.desiredstock";
}

// Add GROUP BY from hooks
$parameters = array();
$reshook = $hookmanager->executeHooks('printFieldListGroupBy', $parameters, $object); // Note that $action and $object may have been modified by hook
$sql .= $hookmanager->resPrint;

$sql_having = '';
if ($search_toolowstock) {
$sql_having .= " HAVING SUM(".$db->ifsql('s.reel IS NULL', '0', 's.reel').") < p.seuil_stock_alerte";
}
if ($search_stock_physique != '') {
//$natural_search_physique = natural_search('HAVING SUM(' . $db->ifsql('s.reel IS NULL', '0', 's.reel') . ')', $search_stock_physique, 1, 1);
$natural_search_physique = natural_search('SUM(' . $db->ifsql('s.reel IS NULL', '0', 's.reel') . ')', $search_stock_physique, 1, 1);
$natural_search_physique = " " . substr($natural_search_physique, 1, -1); // remove first "(" and last ")" characters
if (!empty($sql_having)) {
$sql_having .= " AND";
} else {
$sql_having .= " HAVING";
if (!getDolGlobalString('PRODUCT_STOCK_LIST_SHOW_WITH_COMPILED_PHYSICAL_STOCK')) {
if ($search_toolowstock) {
$sql_having .= " HAVING SUM(" . $db->ifsql('s.reel IS NULL', '0', 's.reel') . ") < p.seuil_stock_alerte";
}
if ($search_stock_physique != '') {
//$natural_search_physique = natural_search('HAVING SUM(' . $db->ifsql('s.reel IS NULL', '0', 's.reel') . ')', $search_stock_physique, 1, 1);
$natural_search_physique = natural_search('SUM(' . $db->ifsql('s.reel IS NULL', '0', 's.reel') . ')', $search_stock_physique, 1, 1);
$natural_search_physique = " " . substr($natural_search_physique, 1, -1); // remove first "(" and last ")" characters
if (!empty($sql_having)) {
$sql_having .= " AND";
} else {
$sql_having .= " HAVING";
}
$sql_having .= $natural_search_physique;
}
$sql_having .= $natural_search_physique;
}

// Add HAVING from hooks
Expand Down

0 comments on commit defc418

Please sign in to comment.