Merge pull request #27008 from ATM-Consulting/FIX_17_trigger_BILL_SUPPLIER_VALIDATE_cannot_modify_newref

FIX: possible inconsistency between llx_ecm_files and file system when BILL_SUPPLIER_VALIDATES changes ref
This commit is contained in:
Laurent Destailleur 2023-12-12 01:06:06 +01:00 committed by GitHub
commit bde53b1706
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1872,19 +1872,18 @@ class FactureFournisseur extends CommonInvoice
// We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments
$oldref = dol_sanitizeFileName($this->ref);
$newref = dol_sanitizeFileName($num);
$dirsource = $conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$oldref;
$dirdest = $conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$newref;
$dirdest = $conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$this->newref;
if (!$error && file_exists($dirsource)) {
dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest);
if (@rename($dirsource, $dirdest)) {
dol_syslog("Rename ok");
// Rename docs starting with $oldref with $newref
$listoffiles = dol_dir_list($conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$newref, 'files', 1, '^'.preg_quote($oldref, '/'));
// Rename docs starting with $oldref with $this->newref
$listoffiles = dol_dir_list($conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2, 0, 0, $this, 'invoice_supplier').$this->newref, 'files', 1, '^'.preg_quote($oldref, '/'));
foreach ($listoffiles as $fileentry) {
$dirsource = $fileentry['name'];
$dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource);
$dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $this->newref, $dirsource);
$dirsource = $fileentry['path'].'/'.$dirsource;
$dirdest = $fileentry['path'].'/'.$dirdest;
@rename($dirsource, $dirdest);