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 // Rename directory if dir was a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->ref)) if (preg_match('/^[\(]?PROV/i', $this->ref))
{ {
// On renomme repertoire facture ($this->ref = ancienne ref, $num = nouvelle ref) // Rename of object directory ($this->ref = old ref, $num = new ref)
// afin de ne pas perdre les fichiers attaches // to not lose the linked files
$facref = dol_sanitizeFileName($this->ref); $facref = dol_sanitizeFileName($this->ref);
$snumfa = dol_sanitizeFileName($num); $snumfa = dol_sanitizeFileName($num);
$dirsource = $conf->facture->dir_output.'/'.$facref; $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 * @param User $user Objet User
* @return int <0 if KO, >0 if OK * @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'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
global $langs, $conf; global $langs, $conf;
$now=dol_now();
$error=0; $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 $this->db->begin();
$result=$soc->set_as_client();
$this->fetch_thirdparty();
// A contract is validated so we can move thirdparty to status customer
$result=$this->thirdparty->set_as_client();
// Define new ref // Define new ref
if (! $error && (preg_match('/^[\(]?PROV/i', $this->ref))) if (! $error && (preg_match('/^[\(]?PROV/i', $this->ref)))
{ {
$num = $this->getNextNumRef($soc); $num = $this->getNextNumRef($this->thirdparty);
} }
else else
{ {
$num = $this->ref; $num = $this->ref;
} }
$sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$num."', statut = 1"; if ($num)
$sql .= " WHERE rowid = ".$this->id . " AND statut = 0";
$resql = $this->db->query($sql);
if ($resql)
{ {
// Appel des triggers $sql = "UPDATE ".MAIN_DB_PREFIX."contrat SET ref = '".$num."', statut = 1";
include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php'; //$sql.= ", fk_user_valid = ".$user->id.", date_valid = '".$this->db->idate($now)."'";
$interface=new Interfaces($this->db); $sql .= " WHERE rowid = ".$this->id . " AND statut = 0";
$result=$interface->run_triggers('CONTRACT_VALIDATE',$this,$user,$langs,$conf);
if ($result < 0) { $error++; $this->errors=$interface->errors; } dol_syslog(get_class($this)."::validate sql=".$sql);
// Fin appel triggers $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) if (! $error)
{ {
$this->oldref = '';
// Rename directory if dir was a temporary ref // Rename directory if dir was a temporary ref
if (preg_match('/^[\(]?PROV/i', $this->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 // to not lose the linked files
$facref = dol_sanitizeFileName($this->ref); $facref = dol_sanitizeFileName($this->ref);
$snumfa = dol_sanitizeFileName($num); $snumfa = dol_sanitizeFileName($num);
@ -333,25 +342,53 @@ class Contrat extends CommonObject
if (@rename($dirsource, $dirdest)) if (@rename($dirsource, $dirdest))
{ {
$this->oldref = $facref;
dol_syslog("Rename ok"); dol_syslog("Rename ok");
// Deleting old PDF in new rep // Deleting old PDF in new rep
dol_delete_file($conf->contract->dir_output.'/'.$snumfa.'/'.$facref.'*.*'); 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 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; return -1;
} }
} }