diff --git a/htdocs/product/stock/lib/replenishment.lib.php b/htdocs/product/stock/lib/replenishment.lib.php
new file mode 100644
index 00000000000..0283c538d53
--- /dev/null
+++ b/htdocs/product/stock/lib/replenishment.lib.php
@@ -0,0 +1,106 @@
+
+ *
+ * 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 .
+ */
+
+/**
+ * \file htdocs/product/stock/replenishment.lib.php
+ * \ingroup produit
+ * \brief Contains functions used in replenish.php and replenishorders.php
+ */
+
+function dispatched($order_id)
+{
+ global $db;
+ $sql = 'SELECT fk_product, SUM(qty) from llx_commande_fournisseur_dispatch';
+ $sql .= ' WHERE fk_commande = ' . $order_id . ' GROUP BY fk_product';
+ $sql .= ' ORDER by fk_product';
+ $resql = $db->query($sql);
+ $dispatched = array();
+ $ordered = array();
+ if($resql && $db->num_rows($resql)) {
+ while($res = $db->fetch_object($resql))
+ $dispatched[] = $res;
+ }
+ $sql = 'SELECT fk_product, SUM(qty) from llx_commande_fournisseurdet';
+ $sql .= ' WHERE fk_commande = ' . $order_id . ' GROUP BY fk_product';
+ $sql .= ' ORDER by fk_product';
+ $resql = $db->query($sql);
+ if($resql && $db->num_rows($resql)) {
+ while($res = $db->fetch_object($resql))
+ $ordered[] = $res;
+ }
+ return $dispatched == $ordered;
+}
+
+function dispatchedOrders()
+{
+ global $db;
+ $sql = 'SELECT rowid FROM ' . MAIN_DB_PREFIX . 'commande_fournisseur';
+ $resql = $db->query($sql);
+ $res = array();
+ if ($resql && $db->num_rows($resql) > 0) {
+ while ($obj = $db->fetch_object($resql)) {
+ if (dispatched($obj->rowid)) {
+ $res[] = $obj->rowid;
+ }
+ }
+ }
+ if ($res) {
+ $res = '(' . implode(',', $res) . ')';
+ } else {
+ //hack to make sure ordered SQL request won't syntax error
+ $res = '(0)';
+ }
+ return $res;
+}
+
+function ordered($product_id)
+{
+ global $db, $langs, $conf;
+ $sql = 'SELECT DISTINCT cfd.fk_product, SUM(cfd.qty) FROM';
+ $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';
+ if ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) {
+ $sql .= ' cf.fk_statut < 3';
+ } else if ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) {
+ $sql .= ' cf.fk_statut < 6 AND cf.rowid NOT IN ' . dispatchedOrders();
+ } else {
+ $sql .= ' cf.fk_statut < 5';
+ }
+ $sql .= ' AND cfd.fk_product = ' . $product_id;
+ $sql .= ' GROUP BY cfd.fk_product';
+
+ $resql = $db->query($sql);
+ if ($resql) {
+ $exists = $db->num_rows($resql);
+ if ($exists) {
+ $obj = $db->fetch_array($resql);
+ return $obj['SUM(cfd.qty)']; //. ' ' . img_picto('','tick');
+ } else {
+ return null;//img_picto('', 'stcomm-1');
+ }
+ } else {
+ $error = $db->lasterror();
+ dol_print_error($db);
+ dol_syslog('replenish.php: ' . $error, LOG_ERROR);
+
+ return $langs->trans('error');
+ }
+}
+
+?>
diff --git a/htdocs/product/stock/replenish.php b/htdocs/product/stock/replenish.php
index dcc628e784f..621c1b08c92 100644
--- a/htdocs/product/stock/replenish.php
+++ b/htdocs/product/stock/replenish.php
@@ -27,6 +27,7 @@ require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
+require_once './lib/replenishment.lib.php';
$langs->load("products");
$langs->load("stocks");
@@ -39,34 +40,7 @@ if ($user->societe_id) {
$result=restrictedArea($user,'produit|service');
//checks if a product has been ordered
-function ordered($product_id)
-{
- global $db;
- $sql = 'SELECT DISTINCT cfd.fk_product, SUM(cfd.qty) from ';
- $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);
- if ($resql) {
- $exists = $db->num_rows($resql);
- if ($exists) {
- $obj = $db->fetch_array($resql);
-
- return $obj['SUM(cfd.qty)'] . ' ' . img_picto('','tick');
- } else {
- return img_picto('', 'stcomm-1');
- }
- } else {
- $error = $db->lasterror();
- dol_print_error($db);
- dol_syslog('replenish.php: ' . $error, LOG_ERROR);
-
- return $langs->trans('error');
- }
-}
$action = GETPOST('action','alpha');
$sref = GETPOST('sref', 'alpha');
@@ -94,6 +68,7 @@ $offset = $limit * $page ;
*/
//orders creation
+//could go in the lib
if ($action == 'order') {
$linecount = GETPOST('linecount', 'int');
unset($_POST['linecount']);
@@ -280,7 +255,7 @@ if ($resql) {
// Lignes des titres
echo '
',
- '| | ';
+ ' | ';
print_liste_field_titre($langs->trans('Ref'),
'replenish.php',
'p.ref',
@@ -415,28 +390,7 @@ if ($resql) {
$prod->ref = $objp->ref;
$prod->id = $objp->rowid;
$prod->type = $objp->fk_product_type;
- echo '
',
- ' | ',
- '',
- $prod->getNomUrl(1, '', 16),
- ' | ',
- '' . $objp->label . ' | ',
- '';
-
- if (!empty($conf->service->enabled) && $type == 1) {
- if (preg_match('/([0-9]+)y/i', $objp->duration, $regs)) {
- $duration = $regs[1] . ' ' . $langs->trans('DurationYear');
- } elseif (preg_match('/([0-9]+)m/i', $objp->duration, $regs)) {
- $duration = $regs[1] . ' ' . $langs->trans('DurationMonth');
- } elseif (preg_match('/([0-9]+)d/i', $objp->duration, $regs)) {
- $duration = $regs[1] . ' ' . $langs->trans('DurationDay');
- } else {
- $duration = $objp->duration;
- }
- echo '',
- $duration,
- ' | ';
- }
+ $ordered = ordered($prod->id);
if (!$objp->stock_physique) {
$objp->stock_physique = 0;
@@ -466,6 +420,38 @@ if ($resql) {
//depending on conf, use either physical stock or
//virtual stock to compute the stock to buy value
$stocktobuy = $objp->desiredstock - $stock;
+
+ if($ordered) {
+ $picto = img_picto('','tick');
+ if($ordered >= $stocktobuy) {
+ $disabled = 'disabled="disabled"';
+ }
+ } else {
+ $picto = img_picto('', 'stcomm-1');
+ }
+ echo '
',
+ ' | ',
+ '',
+ $prod->getNomUrl(1, '', 16),
+ ' | ',
+ '' . $objp->label . ' | ',
+ '';
+
+ if (!empty($conf->service->enabled) && $type == 1) {
+ if (preg_match('/([0-9]+)y/i', $objp->duration, $regs)) {
+ $duration = $regs[1] . ' ' . $langs->trans('DurationYear');
+ } elseif (preg_match('/([0-9]+)m/i', $objp->duration, $regs)) {
+ $duration = $regs[1] . ' ' . $langs->trans('DurationMonth');
+ } elseif (preg_match('/([0-9]+)d/i', $objp->duration, $regs)) {
+ $duration = $regs[1] . ' ' . $langs->trans('DurationDay');
+ } else {
+ $duration = $objp->duration;
+ }
+ echo '',
+ $duration,
+ ' | ';
+ }
+
echo '' . $objp->desiredstock . ' | ',
'',
$stock,
@@ -473,7 +459,7 @@ if ($resql) {
' | ', $warning, $stocktobuy , ' | ',
'',
'',
- ordered($prod->id),
+ $ordered, ' ', $picto,
' | ',
'',
$form->select_product_fourn_price($prod->id,
@@ -529,7 +515,15 @@ if ($resql) {
}
$db->free($resql);
-
+echo ' ';
} else {
dol_print_error($db);
}
diff --git a/htdocs/product/stock/replenishorders.php b/htdocs/product/stock/replenishorders.php
index b79af87421c..d004ef47884 100644
--- a/htdocs/product/stock/replenishorders.php
+++ b/htdocs/product/stock/replenishorders.php
@@ -27,6 +27,7 @@ require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php';
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
+require_once './lib/replenishment.lib.php';
$langs->load("products");
$langs->load("stocks");
@@ -75,7 +76,7 @@ if (!$sortfield) {
$offset = $conf->liste_limit * $page ;
$sql = 'SELECT s.rowid as socid, s.nom, cf.date_creation as dc,';
-$sql .= ' cf.rowid,cf.ref, cf.fk_statut, cf.total_ttc';
+$sql .= ' cf.rowid, cf.ref, cf.fk_statut, cf.total_ttc';
$sql .= ", cf.fk_user_author, u.login";
$sql .= ' FROM (' . MAIN_DB_PREFIX . 'societe as s,';
$sql .= ' ' . MAIN_DB_PREFIX . 'commande_fournisseur as cf';
@@ -90,7 +91,14 @@ $sql .= 'ON cf.fk_user_author = u.rowid';
$sql .= ' WHERE cf.fk_soc = s.rowid ';
$sql .= ' AND cf.entity = ' . $conf->entity;
$sql .= ' AND cf.source = 42';
-$sql .= ' AND cf.fk_statut < 5';
+
+if ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER) {
+ $sql .= ' AND cf.fk_statut < 3';
+} else if ($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER) {
+ $sql .= ' AND cf.fk_statut < 6';
+} else {
+ $sql .= ' AND cf.fk_statut < 5';
+}
if (!$user->rights->societe->client->voir && !$socid) {
$sql .= ' AND s.rowid = sc.fk_soc AND sc.fk_user = ' . $user->id;
@@ -149,11 +157,11 @@ if ($socid) {
if (GETPOST('statut', 'int')) {
$sql .= ' AND fk_statut = ' . GETPOST('statut', 'int');
}
-
+$sql .= ' GROUP BY cf.rowid, cf.ref, cf.date_creation, cf.fk_statut';
+$sql .= ', cf.total_ttc, cf.fk_user_author, u.login, s.rowid, s.nom';
$sql .= ' ORDER BY ' . $sortfield . ' ' . $sortorder . ' ';
$sql .= $db->plimit($conf->liste_limit+1, $offset);
$resql = $db->query($sql);
-
if ($resql) {
$num = $db->num_rows($resql);
$i = 0;
@@ -256,50 +264,52 @@ if ($resql) {
$obj = $db->fetch_object($resql);
$var = !$var;
- $href = DOL_URL_ROOT . '/fourn/commande/fiche.php?id=' . $obj->rowid;
- echo ' |
',
- // Ref
- '| ',
- '',
- img_object($langs->trans('ShowOrder'), 'order') . ' ' . $obj->ref,
- ' | ';
+ if(!dispatched($obj->rowid)) {
+ $href = DOL_URL_ROOT . '/fourn/commande/fiche.php?id=' . $obj->rowid;
+ echo '
',
+ // Ref
+ '| ',
+ '',
+ img_object($langs->trans('ShowOrder'), 'order') . ' ' . $obj->ref,
+ ' | ';
- // Company
- $href = DOL_URL_ROOT . '/fourn/fiche.php?socid=' . $obj->socid;
- echo '',
- '',
- img_object($langs->trans('ShowCompany'), 'company'), ' ',
- $obj->nom . ' | ';
+ // Company
+ $href = DOL_URL_ROOT . '/fourn/fiche.php?socid=' . $obj->socid;
+ echo '',
+ '',
+ img_object($langs->trans('ShowCompany'), 'company'), ' ',
+ $obj->nom . ' | ';
- // Author
- $userstatic->id = $obj->fk_user_author;
- $userstatic->login = $obj->login;
- if ($userstatic->id) {
- $txt = $userstatic->getLoginUrl(1);
- } else {
- $txt = ' ';
+ // Author
+ $userstatic->id = $obj->fk_user_author;
+ $userstatic->login = $obj->login;
+ if ($userstatic->id) {
+ $txt = $userstatic->getLoginUrl(1);
+ } else {
+ $txt = ' ';
+ }
+ echo '',
+ $txt,
+ ' | ',
+ // Amount
+ '',
+ price($obj->total_ttc),
+ ' | ';
+ // Date
+ if ($obj->dc) {
+ $date = dol_print_date($db->jdate($obj->dc), 'day');
+ } else {
+ $date = '-';
+ }
+ echo '',
+ $date,
+ ' | ',
+ // Statut
+ '',
+ $commandestatic->LibStatut($obj->fk_statut, 5),
+ ' | ',
+ '
';
}
- echo '',
- $txt,
- ' | ',
- // Amount
- '',
- price($obj->total_ttc),
- ' | ';
- // Date
- if ($obj->dc) {
- $date = dol_print_date($db->jdate($obj->dc), 'day');
- } else {
- $date = '-';
- }
- echo '',
- $date,
- ' | ',
- // Statut
- '',
- $commandestatic->LibStatut($obj->fk_statut, 5),
- ' | ',
- '';
$i++;
}
echo '',