Fix: Can not validate an order if there is a discount

Qual: Uniformize code for stock decrease
This commit is contained in:
Laurent Destailleur 2009-05-07 21:38:56 +00:00
parent 81a5393e70
commit 14afe7a82f
6 changed files with 546 additions and 504 deletions

View File

@ -205,8 +205,8 @@ class Commande extends CommonObject
/** /**
* \brief Validate order * \brief Validate order
* \param user Utilisateur qui valide * \param user User making status change
* \return int <=0 si ko, >0 si ok * \return int <=0 if OK, >0 if KO
*/ */
function valid($user) function valid($user)
{ {
@ -217,12 +217,14 @@ class Commande extends CommonObject
// Protection // Protection
if ($this->statut == 1) if ($this->statut == 1)
{ {
dol_syslog("Commande::valid no draft status", LOG_WARNING);
return 0; return 0;
} }
if (! $user->rights->commande->valider) if (! $user->rights->commande->valider)
{ {
$this->error='Permission denied'; $this->error='Permission denied';
dol_syslog("Commande::valid ".$this->error, LOG_ERR);
return -1; return -1;
} }
@ -231,13 +233,12 @@ class Commande extends CommonObject
// Definition du nom de module de numerotation de commande // Definition du nom de module de numerotation de commande
$soc = new Societe($this->db); $soc = new Societe($this->db);
$soc->fetch($this->socid); $soc->fetch($this->socid);
$num=$this->getNextNumRef($soc);
// Class of company linked to order // Class of company linked to order
$result=$soc->set_as_client(); $result=$soc->set_as_client();
// check if temporary number // Define new ref
if (eregi('^\(PROV', $this->ref)) if (! $error && (eregi('^\(PROV', $this->ref) || eregi('^PROV', $this->ref)))
{ {
$num = $this->getNextNumRef($soc); $num = $this->getNextNumRef($soc);
} }
@ -246,22 +247,53 @@ class Commande extends CommonObject
$num = $this->ref; $num = $this->ref;
} }
// Validate
$sql = "UPDATE ".MAIN_DB_PREFIX."commande"; $sql = "UPDATE ".MAIN_DB_PREFIX."commande";
$sql.= " SET ref = '".$num."'"; $sql.= " SET ref = '".$num."'";
$sql.= ", fk_statut = 1"; $sql.= ", fk_statut = 1";
$sql.= ", date_valid=".$this->db->idate(mktime()); $sql.= ", date_valid=".$this->db->idate(mktime());
$sql.= ", fk_user_valid = ".$user->id; $sql.= ", fk_user_valid = ".$user->id;
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
$sql.= " AND fk_statut = 0";
dol_syslog("Commande::valid() sql=".$sql);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if (! $resql)
{ {
// On efface le repertoire de pdf provisoire dol_syslog("Commande::valid() Echec update - 10 - sql=".$sql, LOG_ERR);
if (eregi('^\(PROV', $this->ref)) dol_print_error($this->db);
$error++;
}
if (! $error)
{ {
// On renomme repertoire facture ($this->ref = ancienne ref, $numfa = nouvelle ref) // If stock is incremented on validate order, we must increment it
// afin de ne pas perdre les fichiers attach<63>s if ($result >= 0 && $conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1)
{
require_once(DOL_DOCUMENT_ROOT."/product/stock/mouvementstock.class.php");
// Loop on each line
for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
{
if ($this->lignes[$i]->fk_product > 0 && $this->lignes[$i]->product_type == 0)
{
$mouvP = new MouvementStock($this->db);
// We decrement stock of product (and sub-products)
$entrepot_id = "1"; // TODO ajouter possibilité de choisir l'entrepot
// TODO Add price of product in method or '' to update PMP
$result=$mouvP->livraison($user, $this->lignes[$i]->fk_product, $entrepot_id, $this->lignes[$i]->qty);
if ($result < 0) { $error++; }
}
}
}
}
if (! $error)
{
// Rename directory if dir was a temporary ref
if (eregi('^\(PROV', $this->ref) || eregi('^PROV', $this->ref))
{
// On renomme repertoire ($this->ref = ancienne ref, $numfa = nouvelle ref)
// afin de ne pas perdre les fichiers attaches
$comref = dol_sanitizeFileName($this->ref); $comref = dol_sanitizeFileName($this->ref);
$snum = dol_sanitizeFileName($num); $snum = dol_sanitizeFileName($num);
$dirsource = $conf->commande->dir_output.'/'.$comref; $dirsource = $conf->commande->dir_output.'/'.$comref;
@ -278,24 +310,15 @@ class Commande extends CommonObject
} }
} }
} }
// If stock is incremented on validate order, we must increment it
if ($result >= 0 && $conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_VALIDATE_ORDER == 1)
{
require_once(DOL_DOCUMENT_ROOT."/product/stock/mouvementstock.class.php");
for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
{
$mouvP = new MouvementStock($this->db);
// We decrement stock of product (and sub-products)
$entrepot_id = "1"; //Todo: ajouter possibilite de choisir l'entrepot
$result=$mouvP->livraison($user, $this->lignes[$i]->fk_product, $entrepot_id, $this->lignes[$i]->qty);
if ($result < 0) { $error++; }
}
} }
if ($error == 0) // Set new ref
if (! $error)
{
$this->ref = $num;
}
if (! $error)
{ {
// Appel des triggers // Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
@ -305,7 +328,7 @@ class Commande extends CommonObject
// Fin appel triggers // Fin appel triggers
} }
if ($error == 0) if (! $error)
{ {
$this->db->commit(); $this->db->commit();
return 1; return 1;
@ -317,13 +340,6 @@ class Commande extends CommonObject
return -1; return -1;
} }
} }
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}
/** /**
* \brief Set draft status * \brief Set draft status

View File

@ -1,6 +1,6 @@
<?php <?php
/* Copyright (C) 2003-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org> /* Copyright (C) 2003-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2004-2008 Laurent Destailleur <eldy@users.sourceforge.net> * Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr> * Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
@ -77,6 +77,8 @@ print "</form></table><br>\n";
/* /*
* Commandes brouillons * Commandes brouillons
*/ */
if ($conf->commande->enabled)
{
$sql = "SELECT c.rowid, c.ref, s.nom, s.rowid as socid"; $sql = "SELECT c.rowid, c.ref, s.nom, s.rowid as socid";
$sql.= " FROM ".MAIN_DB_PREFIX."commande as c"; $sql.= " FROM ".MAIN_DB_PREFIX."commande as c";
$sql.= ", ".MAIN_DB_PREFIX."societe as s"; $sql.= ", ".MAIN_DB_PREFIX."societe as s";
@ -89,14 +91,14 @@ if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.
if ( $db->query($sql) ) if ( $db->query($sql) )
{ {
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td colspan="2">'.$langs->trans("DraftOrders").'</td></tr>';
$langs->load("orders"); $langs->load("orders");
$num = $db->num_rows(); $num = $db->num_rows();
if ($num) if ($num)
{ {
$i = 0; $i = 0;
print '<table class="noborder" width="100%">';
print '<tr class="liste_titre">';
print '<td colspan="2">'.$langs->trans("DraftOrders").'</td></tr>';
$var = True; $var = True;
while ($i < $num) while ($i < $num)
{ {
@ -108,6 +110,7 @@ if ( $db->query($sql) )
print '<td><a href="'.DOL_URL_ROOT.'/comm/fiche.php?socid='.$obj->socid.'">'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($obj->nom,24).'</a></td></tr>'; print '<td><a href="'.DOL_URL_ROOT.'/comm/fiche.php?socid='.$obj->socid.'">'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($obj->nom,24).'</a></td></tr>';
$i++; $i++;
} }
}
print "</table><br>"; print "</table><br>";
} }
} }
@ -115,6 +118,8 @@ if ( $db->query($sql) )
/* /*
* Commandes à traiter * Commandes à traiter
*/ */
if ($conf->commande->enabled)
{
$sql = "SELECT c.rowid, c.ref, s.nom, s.rowid as socid"; $sql = "SELECT c.rowid, c.ref, s.nom, s.rowid as socid";
$sql.=" FROM ".MAIN_DB_PREFIX."commande as c"; $sql.=" FROM ".MAIN_DB_PREFIX."commande as c";
$sql.= ", ".MAIN_DB_PREFIX."societe as s"; $sql.= ", ".MAIN_DB_PREFIX."societe as s";
@ -171,7 +176,7 @@ if ( $db->query($sql) )
print "</table><br>"; print "</table><br>";
} }
}
print '</td><td valign="top" width="70%" class="notopnoleftnoright">'; print '</td><td valign="top" width="70%" class="notopnoleftnoright">';
@ -179,6 +184,8 @@ print '</td><td valign="top" width="70%" class="notopnoleftnoright">';
/* /*
* Commandes en cours * Commandes en cours
*/ */
if ($conf->commande->enabled)
{
$sql = "SELECT c.rowid, c.ref, c.fk_statut, c.facture, s.nom, s.rowid as socid"; $sql = "SELECT c.rowid, c.ref, c.fk_statut, c.facture, s.nom, s.rowid as socid";
$sql.= " FROM ".MAIN_DB_PREFIX."commande as c"; $sql.= " FROM ".MAIN_DB_PREFIX."commande as c";
$sql.= ", ".MAIN_DB_PREFIX."societe as s"; $sql.= ", ".MAIN_DB_PREFIX."societe as s";
@ -238,6 +245,7 @@ if ( $db->query($sql) )
} }
print "</table><br>"; print "</table><br>";
} }
}
/* /*
* Dernières commandes traitées * Dernières commandes traitées

View File

@ -340,7 +340,7 @@ class Expedition extends CommonObject
} }
/** /**
* \brief Valide l'expedition, et met a jour le stock si stock géré * \brief Validate object and update stock if option enabled
* \param user Objet de l'utilisateur qui valide * \param user Objet de l'utilisateur qui valide
* \return int * \return int
*/ */
@ -348,63 +348,57 @@ class Expedition extends CommonObject
{ {
global $conf; global $conf;
require_once DOL_DOCUMENT_ROOT ."/product/stock/mouvementstock.class.php";
dol_syslog("Expedition::valid"); dol_syslog("Expedition::valid");
// Protection
if ($this->statut)
{
dol_syslog("Expedition::valid no draft status", LOG_WARNING);
return 0;
}
if (! $user->rights->expedition->valider)
{
$this->error='Permission denied';
dol_syslog("Expedition::valid ".$this->error, LOG_ERR);
return -1;
}
$this->db->begin(); $this->db->begin();
$error = 0; // Define new ref
$provref = $this->ref; $num = "EXP".$this->id;
if ($user->rights->expedition->valider)
{
$this->ref = "EXP".$this->id;
// Tester si non dejà au statut validé. Si oui, on arrete afin d'éviter
// de décrémenter 2 fois le stock.
$sql = "SELECT ref";
$sql.= " FROM ".MAIN_DB_PREFIX."expedition";
$sql.= " WHERE ref='".$this->ref."'";
$sql.= " AND entity = ".$conf->entity;
$sql.= " AND fk_statut <> '0'";
$resql=$this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
if ($num > 0)
{
dol_syslog("Expedition::valid already validated", LOG_WARNING);
$this->db->rollback();
return 0;
}
}
// Validate
$sql = "UPDATE ".MAIN_DB_PREFIX."expedition"; $sql = "UPDATE ".MAIN_DB_PREFIX."expedition";
$sql.= " SET ref='".$this->ref."'"; $sql.= " SET ref='".$num."'";
$sql.= ", fk_statut = 1"; $sql.= ", fk_statut = 1";
$sql.= ", date_valid = ".$this->db->idate(mktime()); $sql.= ", date_valid = ".$this->db->idate(mktime());
$sql.= ", fk_user_valid = ".$user->id; $sql.= ", fk_user_valid = ".$user->id;
$sql.= " WHERE rowid = ".$this->id; $sql.= " WHERE rowid = ".$this->id;
$sql.= " AND fk_statut = 0";
dol_syslog("Expedition::valid update expedition sql=".$sql); dol_syslog("Expedition::valid update expedition sql=".$sql);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if (! $resql)
{
dol_syslog("Expedition::valid() Echec update - 10 - sql=".$sql, LOG_ERR);
dol_print_error($this->db);
$error++;
}
if (! $error)
{ {
// If stock increment is done on sending (recommanded choice) // If stock increment is done on sending (recommanded choice)
if ($conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_SHIPMENT) if ($result >= 0 && $conf->stock->enabled && $conf->global->STOCK_CALCULATE_ON_SHIPMENT)
{ {
/* require_once DOL_DOCUMENT_ROOT ."/product/stock/mouvementstock.class.php";
* Enregistrement d'un mouvement de stock pour chaque produit de l'expedition
*/ // Loop on each product line to add a stock movement
$sql = "SELECT cd.fk_product, ed.qty, ed.fk_entrepot"; $sql = "SELECT cd.fk_product, ed.qty, ed.fk_entrepot";
$sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd"; $sql.= " FROM ".MAIN_DB_PREFIX."commandedet as cd";
$sql.= ", ".MAIN_DB_PREFIX."expeditiondet as ed"; $sql.= ", ".MAIN_DB_PREFIX."expeditiondet as ed";
$sql.= " WHERE ed.fk_expedition = ".$this->id; $sql.= " WHERE ed.fk_expedition = ".$this->id;
$sql.= " AND cd.rowid = ed.fk_origin_line"; $sql.= " AND cd.rowid = ed.fk_origin_line";
dol_syslog("Expedition::valid select details sql=".$sql); dol_syslog("Expedition::valid select details sql=".$sql);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if ($resql)
@ -413,23 +407,21 @@ class Expedition extends CommonObject
$i=0; $i=0;
while($i < $num) while($i < $num)
{ {
dol_syslog("Expedition::valid movment nb ".$i); dol_syslog("Expedition::valid movment index ".$i);
$obj = $this->db->fetch_object($resql); $obj = $this->db->fetch_object($resql);
$mouvS = new MouvementStock($this->db); if ($this->lignes[$i]->fk_product > 0 && $this->lignes[$i]->product_type == 0)
$result=$mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty);
if ($result < 0)
{ {
$this->db->rollback(); $mouvS = new MouvementStock($this->db);
$this->error=$this->db->error()." - sql=$sql"; // We decrement stock of product (and sub-products)
dol_syslog("Expedition::valid ".$this->error, LOG_ERR); $entrepot_id = "1"; // TODO ajouter possibilité de choisir l'entrepot
return -3; // TODO Add price of product in method or '' to update PMP
$result=$mouvS->livraison($user, $obj->fk_product, $obj->fk_entrepot, $obj->qty);
if ($result < 0) { $error++; }
} }
$i++; $i++;
} }
} }
else else
{ {
@ -439,9 +431,12 @@ class Expedition extends CommonObject
return -2; return -2;
} }
} }
}
if (! $error)
{
// On efface le répertoire de pdf provisoire // On efface le répertoire de pdf provisoire
$expeditionref = dol_sanitizeFileName($provref); $expeditionref = dol_sanitizeFileName($this->ref);
if ($conf->expedition->dir_output) if ($conf->expedition->dir_output)
{ {
$dir = $conf->expedition->dir_output . "/" . $expeditionref; $dir = $conf->expedition->dir_output . "/" . $expeditionref;
@ -461,28 +456,36 @@ class Expedition extends CommonObject
} }
} }
} }
}
else
{
$this->db->rollback();
$this->error=$this->db->error();
dol_syslog("Expedition::valid ".$this->error, LOG_ERR);
return -1;
}
}
else
{
$this->db->rollback();
$this->error="Non autorise";
dol_syslog("Expedition::valid ".$this->error, LOG_ERR);
return -1;
} }
// Set new ref
if (! $error)
{
$this->ref = $num;
}
if (! $error)
{
// Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('ORDER_SHIPPING',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
if (! $error)
{
$this->db->commit(); $this->db->commit();
//dol_syslog("Expedition::valid commit");
return 1; return 1;
} }
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}
/** /**

View File

@ -139,7 +139,7 @@ if ($_GET["action"] == 'create_delivery' && $conf->livraison_bon->enabled && $us
} }
} }
if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user->rights->expedition->valider) if ($_REQUEST["action"] == 'confirm_valid' && $_REQUEST["confirm"] == 'yes' && $user->rights->expedition->valider)
{ {
$expedition = new Expedition($db); $expedition = new Expedition($db);
$expedition->fetch($_GET["id"]); $expedition->fetch($_GET["id"]);
@ -147,7 +147,7 @@ if ($_POST["action"] == 'confirm_valid' && $_POST["confirm"] == 'yes' && $user->
//$expedition->PdfWrite(); //$expedition->PdfWrite();
} }
if ($_POST["action"] == 'confirm_delete' && $_POST["confirm"] == 'yes') if ($_REQUEST["action"] == 'confirm_delete' && $_REQUEST["confirm"] == 'yes')
{ {
if ($user->rights->expedition->supprimer ) if ($user->rights->expedition->supprimer )
{ {
@ -554,7 +554,7 @@ else
*/ */
if ($_GET["action"] == 'delete') if ($_GET["action"] == 'delete')
{ {
$ret=$html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('DeleteSending'),$langs->trans("ConfirmDeleteSending",$expedition->ref),'confirm_delete'); $ret=$html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('DeleteSending'),$langs->trans("ConfirmDeleteSending",$expedition->ref),'confirm_delete','',0,1);
if ($ret == 'html') print '<br>'; if ($ret == 'html') print '<br>';
} }
@ -564,7 +564,7 @@ else
*/ */
if ($_GET["action"] == 'valid') if ($_GET["action"] == 'valid')
{ {
$ret=$html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('ValidateSending'),$langs->trans("ConfirmValidateSending",$expedition->ref),'confirm_valid'); $ret=$html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('ValidateSending'),$langs->trans("ConfirmValidateSending",$expedition->ref),'confirm_valid','',0,1);
if ($ret == 'html') print '<br>'; if ($ret == 'html') print '<br>';
} }
/* /*
@ -573,7 +573,7 @@ else
*/ */
if ($_GET["action"] == 'annuler') if ($_GET["action"] == 'annuler')
{ {
$ret=$html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('CancelSending'),$langs->trans("ConfirmCancelSending",$expedition->ref),'confirm_cancel'); $ret=$html->form_confirm($_SERVER['PHP_SELF'].'?id='.$expedition->id,$langs->trans('CancelSending'),$langs->trans("ConfirmCancelSending",$expedition->ref),'confirm_cancel','',0,1);
if ($ret == 'html') print '<br>'; if ($ret == 'html') print '<br>';
} }
@ -812,8 +812,6 @@ else
{ {
print '<div class="tabsAction">'; print '<div class="tabsAction">';
if (! eregi('^(valid|delete)',$_REQUEST["action"]))
{
if ($expedition->statut == 0 && $num_prod > 0) if ($expedition->statut == 0 && $num_prod > 0)
{ {
if ($user->rights->expedition->valider) if ($user->rights->expedition->valider)
@ -835,7 +833,6 @@ else
{ {
print '<a class="butActionDelete" href="fiche.php?id='.$expedition->id.'&amp;action=delete">'.$langs->trans("Delete").'</a>'; print '<a class="butActionDelete" href="fiche.php?id='.$expedition->id.'&amp;action=delete">'.$langs->trans("Delete").'</a>';
} }
}
print '</div>'; print '</div>';
} }

View File

@ -1224,14 +1224,27 @@ class Facture extends CommonObject
global $conf,$langs; global $conf,$langs;
$error=0; $error=0;
if ($this->brouillon)
// Protection
if (! $this->brouillon)
{ {
dol_syslog("Facture::valid no draft status", LOG_WARNING);
return 0;
}
if (! $user->rights->commande->valider)
{
$this->error='Permission denied';
dol_syslog("Expedition::valid ".$this->error, LOG_ERR);
return -1;
}
$this->db->begin(); $this->db->begin();
$this->fetch_client(); $this->fetch_client();
$this->fetch_lines(); $this->fetch_lines();
// Verification paramètres // Check parameters
if ($this->type == 1) // si facture de remplacement if ($this->type == 1) // si facture de remplacement
{ {
// Controle que facture source connue // Controle que facture source connue
@ -1272,34 +1285,30 @@ class Facture extends CommonObject
} }
} }
// Define new ref
// on vérifie si la facture est en numérotation provisoire
$facref = substr($this->ref, 1, 4);
if ($force_number) if ($force_number)
{ {
$numfa = $force_number; $num = $force_number;
} }
else if ($facref == 'PROV') else if (eregi('^\(PROV', $this->ref) || eregi('^PROV', $this->ref))
{ {
$savdate=$this->date;
if ($conf->global->FAC_FORCE_DATE_VALIDATION) // If option enabled, we force invoice date if ($conf->global->FAC_FORCE_DATE_VALIDATION) // If option enabled, we force invoice date
{ {
$this->date=gmmktime(); $this->date=gmmktime();
$this->date_lim_reglement=$this->calculate_date_lim_reglement(); $this->date_lim_reglement=$this->calculate_date_lim_reglement();
} }
$numfa = $this->getNextNumRef($this->client); $num = $this->getNextNumRef($this->client);
} }
else else
{ {
$numfa = $this->ref; $num = $this->ref;
} }
$this->update_price(); $this->update_price();
// Validation de la facture // Validate
$sql = 'UPDATE '.MAIN_DB_PREFIX.'facture'; $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture';
$sql.= " SET facnumber='".$numfa."', fk_statut = 1, fk_user_valid = ".$user->id; $sql.= " SET facnumber='".$num."', fk_statut = 1, fk_user_valid = ".$user->id;
if ($conf->global->FAC_FORCE_DATE_VALIDATION) // If option enabled, we force invoice date if ($conf->global->FAC_FORCE_DATE_VALIDATION) // If option enabled, we force invoice date
{ {
$sql.= ', datef='.$this->db->idate($this->date); $sql.= ', datef='.$this->db->idate($this->date);
@ -1307,44 +1316,17 @@ class Facture extends CommonObject
} }
$sql.= ' WHERE rowid = '.$this->id; $sql.= ' WHERE rowid = '.$this->id;
dol_syslog("Facture::set_valid() sql=".$sql, LOG_DEBUG); dol_syslog("Facture::set_valid() sql=".$sql);
$resql=$this->db->query($sql); $resql=$this->db->query($sql);
if ($resql) if (! $resql)
{ {
$this->facnumber=$numfa; dol_syslog("Facture::set_valid() Echec update - 10 - sql=".$sql, LOG_ERR);
}
else
{
dol_syslog("Facture::set_valid() Echec update - 10 - sql=".$sql, LOG_DEBUG);
dol_print_error($this->db); dol_print_error($this->db);
$error++; $error++;
} }
// On vérifie si la facture était une provisoire // On vérifie si la facture était une provisoire
if ($facref == 'PROV') if (! $error && (eregi('^\(PROV', $this->ref) || eregi('^PROV', $this->ref)))
{
// On renomme repertoire facture ($this->ref = ancienne ref, $numfa = nouvelle ref)
// afin de ne pas perdre les fichiers attachés
$facref = dol_sanitizeFileName($this->ref);
$snumfa = dol_sanitizeFileName($numfa);
$dirsource = $conf->facture->dir_output.'/'.$facref;
$dirdest = $conf->facture->dir_output.'/'.$snumfa;
if (file_exists($dirsource))
{
dol_syslog("Facture::set_valid() rename dir ".$dirsource." into ".$dirdest);
if (@rename($dirsource, $dirdest))
{
dol_syslog("Rename ok");
// Suppression ancien fichier PDF dans nouveau rep
dol_delete_file($conf->facture->dir_output.'/'.$snumfa.'/'.$facref.'.*');
}
}
}
// On vérifie si la facture était une provisoire
if (! $error && $facref == 'PROV')
{ {
// La vérif qu'une remise n'est pas utilisée 2 fois est faite au moment de l'insertion de ligne // La vérif qu'une remise n'est pas utilisée 2 fois est faite au moment de l'insertion de ligne
} }
@ -1362,22 +1344,55 @@ class Facture extends CommonObject
// Loop on each line // Loop on each line
for ($i = 0 ; $i < sizeof($this->lignes) ; $i++) for ($i = 0 ; $i < sizeof($this->lignes) ; $i++)
{ {
if ($this->lignes[$i]->fk_product && $this->lignes[$i]->product_type == 0) if ($this->lignes[$i]->fk_product > 0 && $this->lignes[$i]->product_type == 0)
{ {
$mouvP = new MouvementStock($this->db); $mouvP = new MouvementStock($this->db);
// We decrease stock for product // We decrease stock for product
$entrepot_id = "1"; // TODO ajouter possibilité de choisir l'entrepot $entrepot_id = "1"; // TODO ajouter possibilité de choisir l'entrepot
// TODO Add price of product in method or '' to update PMP // TODO Add price of product in method or '' to update PMP
$result=$mouvP->livraison($user, $this->lignes[$i]->fk_product, $entrepot_id, $this->lignes[$i]->qty); $result=$mouvP->livraison($user, $this->lignes[$i]->fk_product, $entrepot_id, $this->lignes[$i]->qty);
if ($result < 0) { $error++; }
}
} }
} }
} }
$this->ref = $numfa; if (! $error)
{
// Rename directory if dir was a temporary ref
if (eregi('^\(PROV', $this->ref) || eregi('^PROV', $this->ref))
{
// On renomme repertoire facture ($this->ref = ancienne ref, $num = nouvelle ref)
// afin de ne pas perdre les fichiers attachés
$facref = dol_sanitizeFileName($this->ref);
$snumfa = dol_sanitizeFileName($num);
$dirsource = $conf->facture->dir_output.'/'.$facref;
$dirdest = $conf->facture->dir_output.'/'.$snumfa;
if (file_exists($dirsource))
{
dol_syslog("Facture::set_valid() rename dir ".$dirsource." into ".$dirdest);
if (@rename($dirsource, $dirdest))
{
dol_syslog("Rename ok");
// Suppression ancien fichier PDF dans nouveau rep
dol_delete_file($conf->facture->dir_output.'/'.$snumfa.'/'.$facref.'.*');
}
}
}
}
// Set new ref
if (! $error)
{
$this->ref = $num;
$this->facnumber=$num;
}
$this->use_webcal=($conf->global->PHPWEBCALENDAR_BILLSTATUS=='always'?1:0); $this->use_webcal=($conf->global->PHPWEBCALENDAR_BILLSTATUS=='always'?1:0);
if ($result > 0) // Trigger calls
if (! $error)
{ {
// Appel des triggers // Appel des triggers
include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php"); include_once(DOL_DOCUMENT_ROOT . "/interfaces.class.php");
@ -1385,23 +1400,19 @@ class Facture extends CommonObject
$result=$interface->run_triggers('BILL_VALIDATE',$this,$user,$langs,$conf); $result=$interface->run_triggers('BILL_VALIDATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; } if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers // Fin appel triggers
}
if (! $error)
{
$this->db->commit(); $this->db->commit();
return 1; return 1;
} }
else else
{ {
$this->db->rollback(); $this->db->rollback();
$this->error=$this->db->lasterror();
return -1; return -1;
} }
}
else
{
$this->db->rollback();
return -1;
}
}
} }
/** /**

View File

@ -19,10 +19,10 @@
*/ */
/** /**
\file htdocs/product/stock/mouvement.php * \file htdocs/product/stock/mouvement.php
\ingroup stock * \ingroup stock
\brief Page liste des mouvements de stocks * \brief Page liste des mouvements de stocks
\version $Id$ * \version $Id$
*/ */
require("./pre.inc.php"); require("./pre.inc.php");
@ -49,7 +49,7 @@ $form=new Form($db);
$sql = "SELECT p.rowid, p.label as produit,"; $sql = "SELECT p.rowid, p.label as produit,";
$sql.= " s.label as stock, s.rowid as entrepot_id,"; $sql.= " s.label as stock, s.rowid as entrepot_id,";
$sql.= " m.value, ".$db->pdate("m.datem")." as datem"; $sql.= " m.rowid as mid, m.value, m.datem";
$sql.= " FROM ".MAIN_DB_PREFIX."entrepot as s"; $sql.= " FROM ".MAIN_DB_PREFIX."entrepot as s";
$sql.= ", ".MAIN_DB_PREFIX."stock_mouvement as m"; $sql.= ", ".MAIN_DB_PREFIX."stock_mouvement as m";
$sql.= ", ".MAIN_DB_PREFIX."product as p"; $sql.= ", ".MAIN_DB_PREFIX."product as p";
@ -152,6 +152,7 @@ if ($resql)
print '<table class="noborder" width="100%">'; print '<table class="noborder" width="100%">';
print "<tr class=\"liste_titre\">"; print "<tr class=\"liste_titre\">";
//print_liste_field_titre($langs->trans("Id"),"mouvement.php", "m.rowid","",$param,"",$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Date"),"mouvement.php", "m.datem","",$param,"",$sortfield,$sortorder); print_liste_field_titre($langs->trans("Date"),"mouvement.php", "m.datem","",$param,"",$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Product"),"mouvement.php", "p.ref","",$param,"",$sortfield,$sortorder); print_liste_field_titre($langs->trans("Product"),"mouvement.php", "p.ref","",$param,"",$sortfield,$sortorder);
print_liste_field_titre($langs->trans("Warehouse"),"mouvement.php", "s.label","",$param,"",$sortfield,$sortorder); print_liste_field_titre($langs->trans("Warehouse"),"mouvement.php", "s.label","",$param,"",$sortfield,$sortorder);
@ -164,13 +165,19 @@ if ($resql)
$objp = $db->fetch_object($resql); $objp = $db->fetch_object($resql);
$var=!$var; $var=!$var;
print "<tr $bc[$var]>"; print "<tr $bc[$var]>";
print '<td>'.dol_print_date($objp->datem,'dayhour').'</td>'; // Id movement
//print '<td>'.$objp->mid.'</td>'; // This is primary not movement id
// Date
print '<td>'.dol_print_date($db->jdate($objp->datem),'dayhour').'</td>';
// Product
print "<td><a href=\"../fiche.php?id=$objp->rowid\">"; print "<td><a href=\"../fiche.php?id=$objp->rowid\">";
print img_object($langs->trans("ShowProduct"),"product").' '.$objp->produit; print img_object($langs->trans("ShowProduct"),"product").' '.$objp->produit;
print "</a></td>\n"; print "</a></td>\n";
// Warehouse
print '<td><a href="fiche.php?id='.$objp->entrepot_id.'">'; print '<td><a href="fiche.php?id='.$objp->entrepot_id.'">';
print img_object($langs->trans("ShowWarehouse"),"stock").' '.$objp->stock; print img_object($langs->trans("ShowWarehouse"),"stock").' '.$objp->stock;
print "</a></td>\n"; print "</a></td>\n";
// Value
print '<td align="right">'; print '<td align="right">';
if ($objp->value > 0) print '+'; if ($objp->value > 0) print '+';
print $objp->value.'</td>'; print $objp->value.'</td>';