checkbox + improve order detection

This commit is contained in:
Cédric Salvador 2013-07-04 18:57:20 +02:00
parent c09b05a05f
commit 6bf95db95e
3 changed files with 207 additions and 97 deletions

View File

@ -0,0 +1,106 @@
<?php
/*
* Copyright (C) 2013 Cédric Salvador <csalvador@gpcsolutions.fr>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \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');
}
}
?>

View File

@ -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.formother.class.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.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 DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
require_once './lib/replenishment.lib.php';
$langs->load("products"); $langs->load("products");
$langs->load("stocks"); $langs->load("stocks");
@ -39,34 +40,7 @@ if ($user->societe_id) {
$result=restrictedArea($user,'produit|service'); $result=restrictedArea($user,'produit|service');
//checks if a product has been ordered //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'); $action = GETPOST('action','alpha');
$sref = GETPOST('sref', 'alpha'); $sref = GETPOST('sref', 'alpha');
@ -94,6 +68,7 @@ $offset = $limit * $page ;
*/ */
//orders creation //orders creation
//could go in the lib
if ($action == 'order') { if ($action == 'order') {
$linecount = GETPOST('linecount', 'int'); $linecount = GETPOST('linecount', 'int');
unset($_POST['linecount']); unset($_POST['linecount']);
@ -280,7 +255,7 @@ if ($resql) {
// Lignes des titres // Lignes des titres
echo '<tr class="liste_titre">', echo '<tr class="liste_titre">',
'<td>&nbsp;</td>'; '<td><input type="checkbox" onClick="toggle(this)" /></td>';
print_liste_field_titre($langs->trans('Ref'), print_liste_field_titre($langs->trans('Ref'),
'replenish.php', 'replenish.php',
'p.ref', 'p.ref',
@ -415,28 +390,7 @@ if ($resql) {
$prod->ref = $objp->ref; $prod->ref = $objp->ref;
$prod->id = $objp->rowid; $prod->id = $objp->rowid;
$prod->type = $objp->fk_product_type; $prod->type = $objp->fk_product_type;
echo '<tr ' . $bc[$var] . '>', $ordered = ordered($prod->id);
'<td><input type="checkbox" name="' . $i . '"></td>',
'<td class="nowrap">',
$prod->getNomUrl(1, '', 16),
'</td>',
'<td>' . $objp->label . '</td>',
'<input type="hidden" name="desc' . $i . '" value="' . $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 '<td align="center">',
$duration,
'</td>';
}
if (!$objp->stock_physique) { if (!$objp->stock_physique) {
$objp->stock_physique = 0; $objp->stock_physique = 0;
@ -466,6 +420,38 @@ if ($resql) {
//depending on conf, use either physical stock or //depending on conf, use either physical stock or
//virtual stock to compute the stock to buy value //virtual stock to compute the stock to buy value
$stocktobuy = $objp->desiredstock - $stock; $stocktobuy = $objp->desiredstock - $stock;
if($ordered) {
$picto = img_picto('','tick');
if($ordered >= $stocktobuy) {
$disabled = 'disabled="disabled"';
}
} else {
$picto = img_picto('', 'stcomm-1');
}
echo '<tr ' . $bc[$var] . '>',
'<td><input type="checkbox" class="check" name="' . $i . '"' . $disabled . '></td>',
'<td class="nowrap">',
$prod->getNomUrl(1, '', 16),
'</td>',
'<td>' . $objp->label . '</td>',
'<input type="hidden" name="desc' . $i . '" value="' . $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 '<td align="center">',
$duration,
'</td>';
}
echo '<td align="right">' . $objp->desiredstock . '</td>', echo '<td align="right">' . $objp->desiredstock . '</td>',
'<td align="right">', '<td align="right">',
$stock, $stock,
@ -473,7 +459,7 @@ if ($resql) {
'<td align="right">', $warning, $stocktobuy , '</td>', '<td align="right">', $warning, $stocktobuy , '</td>',
'<input type="hidden" name="tobuy' . $i . '" value="' . $stocktobuy . '" >', '<input type="hidden" name="tobuy' . $i . '" value="' . $stocktobuy . '" >',
'<td align="right">', '<td align="right">',
ordered($prod->id), $ordered, ' ', $picto,
'</td>', '</td>',
'<td align="right">', '<td align="right">',
$form->select_product_fourn_price($prod->id, $form->select_product_fourn_price($prod->id,
@ -529,7 +515,15 @@ if ($resql) {
} }
$db->free($resql); $db->free($resql);
echo ' <script type="text/javascript">
function toggle(source) {
checkboxes = document.getElementsByClassName("check");
for(var i=0; i < checkboxes.length;i++) {
if(!checkboxes[i].disabled) {
checkboxes[i].checked = source.checked;
}
}
} </script>';
} else { } else {
dol_print_error($db); dol_print_error($db);
} }

View File

@ -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.'/core/class/html.form.class.php';
require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.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 DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php';
require_once './lib/replenishment.lib.php';
$langs->load("products"); $langs->load("products");
$langs->load("stocks"); $langs->load("stocks");
@ -90,7 +91,14 @@ $sql .= 'ON cf.fk_user_author = u.rowid';
$sql .= ' WHERE cf.fk_soc = s.rowid '; $sql .= ' WHERE cf.fk_soc = s.rowid ';
$sql .= ' AND cf.entity = ' . $conf->entity; $sql .= ' AND cf.entity = ' . $conf->entity;
$sql .= ' AND cf.source = 42'; $sql .= ' AND cf.source = 42';
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'; $sql .= ' AND cf.fk_statut < 5';
}
if (!$user->rights->societe->client->voir && !$socid) { if (!$user->rights->societe->client->voir && !$socid) {
$sql .= ' AND s.rowid = sc.fk_soc AND sc.fk_user = ' . $user->id; $sql .= ' AND s.rowid = sc.fk_soc AND sc.fk_user = ' . $user->id;
@ -149,11 +157,11 @@ if ($socid) {
if (GETPOST('statut', 'int')) { if (GETPOST('statut', 'int')) {
$sql .= ' AND fk_statut = ' . 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 .= ' ORDER BY ' . $sortfield . ' ' . $sortorder . ' ';
$sql .= $db->plimit($conf->liste_limit+1, $offset); $sql .= $db->plimit($conf->liste_limit+1, $offset);
$resql = $db->query($sql); $resql = $db->query($sql);
if ($resql) { if ($resql) {
$num = $db->num_rows($resql); $num = $db->num_rows($resql);
$i = 0; $i = 0;
@ -256,6 +264,7 @@ if ($resql) {
$obj = $db->fetch_object($resql); $obj = $db->fetch_object($resql);
$var = !$var; $var = !$var;
if(!dispatched($obj->rowid)) {
$href = DOL_URL_ROOT . '/fourn/commande/fiche.php?id=' . $obj->rowid; $href = DOL_URL_ROOT . '/fourn/commande/fiche.php?id=' . $obj->rowid;
echo '<tr ' . $bc[$var] . '>', echo '<tr ' . $bc[$var] . '>',
// Ref // Ref
@ -300,6 +309,7 @@ if ($resql) {
$commandestatic->LibStatut($obj->fk_statut, 5), $commandestatic->LibStatut($obj->fk_statut, 5),
'</td>', '</td>',
'</tr>'; '</tr>';
}
$i++; $i++;
} }
echo '</table>', echo '</table>',