New: Quelques amliorations sur la gestion des stocks.

This commit is contained in:
Laurent Destailleur 2008-03-31 05:56:45 +00:00
parent e4ac9de490
commit 10dfe57420
7 changed files with 222 additions and 184 deletions

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
* Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
*
@ -17,13 +17,12 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
/**
\defgroup produit Module produit
\brief Module pour g<EFBFBD>rer le suivi de produits pr<EFBFBD>d<EFBFBD>finis
\brief Module pour gerer le suivi de produits predefinis
\version $Id$
*/
/**

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2007 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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
@ -15,13 +15,12 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
*/
/**
\defgroup stock Module stock
\brief Module pour g<EFBFBD>rer la tenue de stocks produits
\defgroup stock Module stock
\brief Module pour gerer la tenue de stocks produits
\version $Id$
*/
/**
@ -88,7 +87,7 @@ class modStock extends DolibarrModules
$this->rights[0][5] = '';
$this->rights[1][0] = 1002;
$this->rights[1][1] = 'Cr<EFBFBD>er/Modifier les stocks';
$this->rights[1][1] = 'Creer/Modifier les stocks';
$this->rights[1][2] = 'w';
$this->rights[1][3] = 0;
$this->rights[1][4] = 'creer';
@ -109,17 +108,31 @@ class modStock extends DolibarrModules
$this->rights[3][5] = 'lire';
$this->rights[4][0] = 1005;
$this->rights[4][1] = 'Cr<EFBFBD>er/modifier mouvements de stocks';
$this->rights[4][1] = 'Creer/modifier mouvements de stocks';
$this->rights[4][2] = 'w';
$this->rights[4][3] = 0;
$this->rights[4][4] = 'mouvement';
$this->rights[4][5] = 'creer';
// Exports
//--------
$r=0;
$r++;
$this->export_code[$r]=$this->rights_class.'_'.$r;
$this->export_label[$r]="ProductsOrServices"; // Translation key (used only if key ExportDataset_xxx_z not found)
$this->export_permission[$r]=array(array("produit","export"));
$this->export_fields_array[$r]=array('p.rowid'=>"Id",'p.ref'=>"Ref",'p.fk_product_type'=>"Type",'p.label'=>"Label",'p.description'=>"Description",'p.note'=>"Note",'p.price'=>"Price",'p.tva_tx'=>'VAT','p.envente'=>"OnSell",'p.duration'=>"Duration",'p.datec'=>'DateCreation','p.tms'=>'DateModification');
$this->export_entities_array[$r]=array('p.rowid'=>"product",'p.ref'=>"product",'p.fk_product_type'=>"product",'p.label'=>"product",'p.description'=>"product",'p.note'=>"product",'p.price'=>"product",'p.tva_tx'=>'product','p.envente'=>"product",'p.duration'=>"product",'p.datec'=>'product','p.tms'=>'product');
$this->export_alias_array[$r]=array('p.rowid'=>"id",'p.ref'=>"ref",'p.fk_product_type'=>"type",'p.label'=>"label",'p.description'=>"description",'p.note'=>"note",'p.price'=>"price",'p.tva_tx'=>'vat','p.envente'=>"onsell",'p.duration'=>"duration",'p.datec'=>'datecreation','p.tms'=>'datemodification');
$this->export_sql_start[$r]='SELECT DISTINCT ';
$this->export_sql_end[$r] =' FROM '.MAIN_DB_PREFIX.'product as p';
}
/**
* \brief Fonction appel<EFBFBD>e lors de l'activation du module. Ins<EFBFBD>re en base les constantes, boites, permissions du module.
* D<EFBFBD>finit <EFBFBD>galement les r<EFBFBD>pertoires de donn<EFBFBD>es <EFBFBD> cr<EFBFBD>er pour ce module.
* \brief Fonction appelee lors de l'activation du module. Insere en base les constantes, boites, permissions du module.
* Definit egalement les repertoires de donnees a creer pour ce module.
*/
function init()
{

View File

@ -1978,107 +1978,138 @@ class Product extends CommonObject
return $langs->trans('Unknown');
}
/**
* \brief Entre un nombre de piece du produit en stock dans un entrep<EFBFBD>t
* \param id_entrepot id de l'entrepot
* \param nbpiece nombre de pieces
*/
function create_stock($id_entrepot, $nbpiece)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_stock ";
$sql .= " (fk_product, fk_entrepot, reel)";
$sql .= " VALUES ($this->id, $id_entrepot, $nbpiece)";
if ($this->db->query($sql) )
{
return 1;
}
else
{
dolibarr_print_error($this->db);
return -1;
}
}
/**
* \brief Ajuste le stock d'un entrep<EFBFBD>t pour le produit <EFBFBD> une valeure donn<EFBFBD>e
* \param user utilisateur qui demande l'ajustement
* \param id_entrepot id de l'entrepot
* \param nbpiece nombre de pieces
* \param mouvement 0 = ajout, 1 = suppression
*/
function correct_stock($user, $id_entrepot, $nbpiece, $mouvement)
{
if ($id_entrepot)
{
$sql = "SELECT count(*) FROM ".MAIN_DB_PREFIX."product_stock ";
$sql .= " WHERE fk_product = $this->id AND fk_entrepot = $id_entrepot";
if ($this->db->query($sql) )
{
$row = $this->db->fetch_row(0);
if ($row[0] > 0)
{
return $this->ajust_stock($user, $id_entrepot, $nbpiece, $mouvement);
}
else
{
return $this->create_stock($id_entrepot, $nbpiece);
}
}
else
{
dolibarr_print_error($this->db);
$this->db->rollback();
return -1;
}
}
}
/**
* \brief Augmente ou r<EFBFBD>duit la valeur de stock pour le produit
* \param user utilisateur qui demande l'ajustement
* \param id_entrepot id de l'entrepot
* \param nbpiece nombre de pieces
* \param mouvement 0 = ajout, 1 = suppression
*/
function ajust_stock($user, $id_entrepot, $nbpiece, $mouvement)
{
$op[0] = "+" . trim($nbpiece);
$op[1] = "-" . trim($nbpiece);
if ($this->db->begin())
{
$sql = "UPDATE ".MAIN_DB_PREFIX."product_stock ";
$sql .= " SET reel = reel ".$op[$mouvement];
$sql .= " WHERE fk_product = $this->id AND fk_entrepot = $id_entrepot";
/**
* \brief Entre un nombre de piece du produit en stock dans un entrep<EFBFBD>t
* \param id_entrepot id de l'entrepot
* \param nbpiece nombre de pieces
*/
function create_stock($id_entrepot, $nbpiece)
{
global $user;
$op[0] = "+".trim($nbpiece);
$op[1] = "-".trim($nbpiece);
$mouvement=0; // We add pieces
$this->db->begin();
if ($this->db->query($sql) )
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."stock_mouvement (datem, fk_product, fk_entrepot, value, type_mouvement, fk_user_author)";
$sql .= " VALUES (now(), $this->id, $id_entrepot, ".$op[$mouvement].", 0, $user->id)";
if ($this->db->query($sql) )
{
$this->db->commit();
return 1;
}
else
{
dolibarr_print_error($this->db);
$this->db->rollback();
return -2;
}
}
else
{
dolibarr_print_error($this->db);
$this->db->rollback();
return -1;
}
}
}
$sql = "INSERT INTO ".MAIN_DB_PREFIX."product_stock ";
$sql .= " (fk_product, fk_entrepot, reel)";
$sql .= " VALUES ($this->id, $id_entrepot, $nbpiece)";
dolibarr_syslog("Product::create_stock sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."stock_mouvement (datem, fk_product, fk_entrepot, value, type_mouvement, fk_user_author)";
$sql .= " VALUES (now(), ".$this->id.", ".$id_entrepot.", ".$nbpiece.", 0, ".$user->id.")";
dolibarr_syslog("Product::create_stock sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$this->db->commit();
return 1;
}
else
{
dolibarr_print_error($this->db);
$this->db->rollback();
return -2;
}
}
else
{
dolibarr_print_error($this->db);
return -1;
}
}
/**
* \brief Ajuste le stock d'un entrepot pour le produit a une valeure donnee
* \param user utilisateur qui demande l'ajustement
* \param id_entrepot id de l'entrepot
* \param nbpiece nombre de pieces
* \param mouvement 0 = ajout, 1 = suppression
*/
function correct_stock($user, $id_entrepot, $nbpiece, $mouvement)
{
if ($id_entrepot)
{
$sql = "SELECT count(*) as nb FROM ".MAIN_DB_PREFIX."product_stock";
$sql .= " WHERE fk_product = ".$this->id." AND fk_entrepot = ".$id_entrepot;
$resql=$this->db->query($sql);
if ($resql)
{
$row = $this->db->fetch_object($resql);
if ($row->nb > 0)
{
// Record already exists, we make an update
return $this->ajust_stock($user, $id_entrepot, $nbpiece, $mouvement);
}
else
{
// Record not yet available, we make an insert
return $this->create_stock($id_entrepot, $nbpiece);
}
}
else
{
dolibarr_print_error($this->db);
$this->db->rollback();
return -1;
}
}
}
/**
* \brief Augmente ou r<EFBFBD>duit la valeur de stock pour le produit
* \param user utilisateur qui demande l'ajustement
* \param id_entrepot id de l'entrepot
* \param nbpiece nombre de pieces
* \param mouvement 0 = ajout, 1 = suppression
*/
function ajust_stock($user, $id_entrepot, $nbpiece, $mouvement)
{
$op[0] = "+".trim($nbpiece);
$op[1] = "-".trim($nbpiece);
$this->db->begin();
$sql = "UPDATE ".MAIN_DB_PREFIX."product_stock";
$sql.= " SET reel = reel ".$op[$mouvement];
$sql.= " WHERE fk_product = ".$this->id." AND fk_entrepot = ".$id_entrepot;
dolibarr_syslog("Product::ajust_stock sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$sql = "INSERT INTO ".MAIN_DB_PREFIX."stock_mouvement (datem, fk_product, fk_entrepot, value, type_mouvement, fk_user_author)";
$sql .= " VALUES (now(), ".$this->id.", ".$id_entrepot.", ".$op[$mouvement].", 0, ".$user->id.")";
dolibarr_syslog("Product::ajust_stock sql=".$sql);
$resql=$this->db->query($sql);
if ($resql)
{
$this->db->commit();
return 1;
}
else
{
dolibarr_print_error($this->db);
$this->db->rollback();
return -2;
}
}
else
{
dolibarr_print_error($this->db);
$this->db->rollback();
return -1;
}
}
/**
* \brief Augmente ou r<EFBFBD>duit le nombre de piece en commande a expedier

View File

@ -16,16 +16,13 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*/
/**
\file htdocs/product/stock/entrepot.class.php
\ingroup stock
\brief Fichier de la classe de gestion des entrepots
\version $Revision$
\version $Id$
*/
@ -64,60 +61,60 @@ class Entrepot
$this->statuts[1] = $langs->trans("Opened");
}
/*
* \brief Creation d'un entrepot en base
* \param Objet user qui crée l'entrepot
*/
function create($user)
{
// Si libelle non defini, erreur
if ($this->libelle == '') {
$this->error = "Libellé obligatoire";
return 0;
}
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."entrepot (datec, fk_user_author)";
$sql .= " VALUES (now(),".$user->id.")";
$result=$this->db->query($sql);
if ($result)
{
$id = $this->db->last_insert_id(MAIN_DB_PREFIX."entrepot");
if ($id > 0)
{
$this->id = $id;
if ( $this->update($id, $user) > 0)
{
$this->db->commit();
return $id;
}
else
{
dolibarr_syslog("Entrepot::Create return -3");
$this->db->rollback();
return -3;
}
}
else {
$this->error="Failed to get insert id";
dolibarr_syslog("Entrepot::Create return -2");
return -2;
/*
* \brief Creation d'un entrepot en base
* \param Objet user qui crée l'entrepot
*/
function create($user)
{
// Si libelle non defini, erreur
if ($this->libelle == '')
{
$this->error = "ErrorFieldRequired";
return 0;
}
$this->db->begin();
$sql = "INSERT INTO ".MAIN_DB_PREFIX."entrepot (datec, fk_user_author, label)";
$sql .= " VALUES (now(),".$user->id.",'".addslashes($this->libelle)."')";
dolibarr_syslog("Entrepot::create sql=".$sql);
$result=$this->db->query($sql);
if ($result)
{
$id = $this->db->last_insert_id(MAIN_DB_PREFIX."entrepot");
if ($id > 0)
{
$this->id = $id;
if ( $this->update($id, $user) > 0)
{
$this->db->commit();
return $id;
}
else
{
dolibarr_syslog("Entrepot::Create return -3");
$this->db->rollback();
return -3;
}
}
else {
$this->error="Failed to get insert id";
dolibarr_syslog("Entrepot::Create return -2");
return -2;
}
}
else
{
$this->error=$this->db->error();
dolibarr_syslog("Entrepot::Create Error ".$this->db->error());
$this->db->rollback();
return -1;
}
}
}
else
{
$this->error="Failed to insert warehouse";
dolibarr_syslog("Entrepot::Create return -1");
dolibarr_syslog("Entrepot::Create ".$this->error);
dolibarr_syslog("Entrepot::Create ".$this->db->error());
$this->db->rollback();
return -1;
}
}
/*
* \brief Mise a jour des information d'un entrepot

View File

@ -1,6 +1,6 @@
<?php
/* Copyright (C) 2001-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2006 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2006 Regis Houssin <regis@dolibarr.fr>
*
* This program is free software; you can redistribute it and/or modify
@ -16,16 +16,13 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*/
/**
\file htdocs/product/stock/mouvement.php
\ingroup stock
\brief Page liste des mouvements de stocks
\version $Revision$
\version $Id$
*/
require("./pre.inc.php");
@ -147,14 +144,16 @@ if ($resql)
$objp = $db->fetch_object($resql);
$var=!$var;
print "<tr $bc[$var]>";
print '<td>'.dolibarr_print_date($objp->datem).'</td>';
print '<td>'.dolibarr_print_date($objp->datem,'dayhour').'</td>';
print "<td><a href=\"../fiche.php?id=$objp->rowid\">";
print img_object($langs->trans("ShowProduct"),"product").' '.$objp->produit;
print "</a></td>\n";
print '<td><a href="fiche.php?id='.$objp->entrepot_id.'">';
print img_object($langs->trans("ShowWarehouse"),"stock").' '.$objp->stock;
print "</a></td>\n";
print '<td align="right">'.$objp->value.'</td>';
print '<td align="right">';
if ($objp->value > 0) print '+';
print $objp->value.'</td>';
print "</tr>\n";
$i++;
}

View File

@ -18,16 +18,13 @@
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*
* $Id$
* $Source$
*/
/**
\file htdocs/product/stock/product.php
\ingroup product
\brief Page de la fiche stock d'un produit
\version $Revision$
\version $Id$
*/
require("./pre.inc.php");
@ -45,6 +42,9 @@ if (! $user->rights->produit->lire || ! $product->type == 0 || ! $conf->stock->e
accessforbidden();
}
/**
* Actions
*/
if ($_POST["action"] == "create_stock")
{
@ -55,9 +55,8 @@ if ($_POST["action"] == "create_stock")
if ($_POST["action"] == "correct_stock" && $_POST["cancel"] <> $langs->trans("Cancel"))
{
if (is_numeric($_POST["nbpiece"]))
if (is_numeric($_POST["nbpiece"]))
{
$product = new Product($db);
$product->id = $_GET["id"];
$product->correct_stock($user,

View File

@ -90,7 +90,7 @@ if ($result)
{
$entrepot=new Entrepot($db);
$total = 0;
$var=True;
$var=false;
while ($i < min($num,$limit))
{
$objp = $db->fetch_object($result);