Fix: Validation of contract lose the new ref into trigger.

Fix: Validation of contract was not using transaction
Fix: Validation of contract did not renamed uploaded directory.
This commit is contained in:
Laurent Destailleur 2013-10-18 10:17:06 +02:00
parent a8f784083c
commit e5bc7d29d0
2 changed files with 82 additions and 45 deletions

View File

@ -1788,8 +1788,8 @@ class Facture extends CommonInvoice
// Rename directory if dir was a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref))
{
// On renomme repertoire facture ($this->ref = ancienne ref, $num = nouvelle ref)
// afin de ne pas perdre les fichiers attaches
// Rename of object directory ($this->ref = old ref, $num = new ref)
// to not lose the linked files
$facref = dol_sanitizeFileName($this->ref);
$snumfa = dol_sanitizeFileName($num);
$dirsource = $conf->facture->dir_output.'/'.$facref;

View File

@ -274,54 +274,63 @@ class Contrat extends CommonObject
}
/**
* Validate a contract
* Validate a contract
*
* @param User $user Objet User
* @return int <0 if KO, >0 if OK
* @param User $user Objet User
* @param string $force_number Reference to force on contract (not implemented yet)
* @return int <0 if KO, >0 if OK
*/
function validate($user)
function validate($user, $force_number='')
{
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
global $langs, $conf;
$now=dol_now();
$error=0;
dol_syslog(get_class($this).'::validate user='.$user->id.', force_number='.$force_number);
// Definition du nom de module de numerotation de commande
$soc = new Societe($this->db);
$soc->fetch($this->socid);
// Class of company linked to order
$result=$soc->set_as_client();
$this->db->begin();
$this->fetch_thirdparty();
// A contract is validated so we can move thirdparty to status customer
$result=$this->thirdparty->set_as_client();
// Define new ref
if (! $error && (preg_match('/^[\(]?PROV/i', $this->ref)))
{
$num = $this->getNextNumRef($soc);
$num = $this->getNextNumRef($this->thirdparty);
}
else
{
$num = $this->ref;
}
$sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$num."', statut = 1";
$sql .= " WHERE rowid = ".$this->id . " AND statut = 0";
$resql = $this->db->query($sql);
if ($resql)
if ($num)
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('CONTRACT_VALIDATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
$sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$num."', statut = 1";
//$sql.= ", fk_user_valid = ".$user->id.", date_valid = '".$this->db->idate($now)."'";
$sql .= " WHERE rowid = ".$this->id . " AND statut = 0";
dol_syslog(get_class($this)."::validate sql=".$sql);
$resql = $this->db->query($sql);
if (! $resql)
{
dol_syslog(get_class($this)."::validate Echec update - 10 - sql=".$sql, LOG_ERR);
dol_print_error($this->db);
$error++;
}
if (! $error)
{
$this->oldref = '';
// Rename directory if dir was a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref))
{
// Rename of propal directory ($this->ref = old ref, $num = new ref)
// Rename of object directory ($this->ref = old ref, $num = new ref)
// to not lose the linked files
$facref = dol_sanitizeFileName($this->ref);
$snumfa = dol_sanitizeFileName($num);
@ -333,25 +342,53 @@ class Contrat extends CommonObject
if (@rename($dirsource, $dirdest))
{
$this->oldref = $facref;
dol_syslog("Rename ok");
// Deleting old PDF in new rep
dol_delete_file($conf->contract->dir_output.'/'.$snumfa.'/'.$facref.'*.*');
}
}
}
return 1;
}
else
// Set new ref and define current statut
if (! $error)
{
return -1;
$this->ref = $num;
$this->statut=1;
$this->brouillon=0;
$this->date_validation=$now;
}
// Trigger calls
if (! $error)
{
// Appel des triggers
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
$interface=new Interfaces($this->db);
$result=$interface->run_triggers('CONTRACT_VALIDATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; }
// Fin appel triggers
}
}
else
{
$this->error=$this->db->error();
$error++;
}
if (! $error)
{
$this->db->commit();
return 1;
}
else
{
$this->db->rollback();
$this->error=$this->db->lasterror();
return -1;
}
}