From bb74a63d594cdc61d756c93e7f97ceeb1611a6a7 Mon Sep 17 00:00:00 2001 From: Regis Houssin Date: Sun, 5 Aug 2012 18:29:02 +0200 Subject: [PATCH] Fix: delete pdf if supplier invoice is deleted Fix: compatibility with external modules (minimum for 3.2) --- htdocs/core/class/fileupload.class.php | 15 ++-- .../fourn/class/fournisseur.facture.class.php | 77 ++++++++++++++----- htdocs/fourn/facture/document.php | 3 +- htdocs/fourn/facture/fiche.php | 5 +- 4 files changed, 74 insertions(+), 26 deletions(-) diff --git a/htdocs/core/class/fileupload.class.php b/htdocs/core/class/fileupload.class.php index 4f21bf968b9..50d874278c5 100644 --- a/htdocs/core/class/fileupload.class.php +++ b/htdocs/core/class/fileupload.class.php @@ -77,7 +77,7 @@ class FileUpload elseif ($element == 'order_supplier') { $pathname = 'fourn'; $filename='fournisseur.commande'; $dir_output=$conf->fournisseur->commande->dir_output; - } + } elseif ($element == 'invoice_supplier') { $pathname = 'fourn'; $filename='fournisseur.facture'; $dir_output=$conf->fournisseur->facture->dir_output; @@ -100,10 +100,15 @@ class FileUpload $object->fetch($fk_element); $object->fetch_thirdparty(); + $object_ref = dol_sanitizeFileName($object->ref); + if ($element == 'invoice_supplier') { + $object_ref = get_exdir($object->id, 2) . $object_ref; + } + $this->_options = array( 'script_url' => $_SERVER['PHP_SELF'], - 'upload_dir' => $dir_output . '/' . $object->ref . '/', - 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object->ref.'/', + 'upload_dir' => $dir_output . '/' . $object_ref . '/', + 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object_ref.'/', 'param_name' => 'files', // Set the following option to 'POST', if your server does not support // DELETE requests. This is a parameter sent to the client: @@ -136,8 +141,8 @@ class FileUpload ), */ 'thumbnail' => array( - 'upload_dir' => $dir_output . '/' . $object->ref . '/thumbs/', - 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object->ref.'/thumbs/', + 'upload_dir' => $dir_output . '/' . $object_ref . '/thumbs/', + 'upload_url' => DOL_URL_ROOT.'/document.php?modulepart='.$element.'&attachment=1&file=/'.$object_ref.'/thumbs/', 'max_width' => 80, 'max_height' => 80 ) diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index 6cdaa631da9..3c486ee596d 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -582,34 +582,73 @@ class FactureFournisseur extends Facture $this->db->begin(); $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det WHERE fk_facture_fourn = '.$rowid.';'; - dol_syslog("FactureFournisseur sql=".$sql, LOG_DEBUG); + dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG); $resql = $this->db->query($sql); if ($resql) { $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn WHERE rowid = '.$rowid; - dol_syslog("FactureFournisseur sql=".$sql, LOG_DEBUG); + dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG); $resql2 = $this->db->query($sql); - if (! $resql2) $error++; + if (! $resql2) { + $error++; + } + } + else { + $error++; + } - if (! $error) - { - $this->db->commit(); - return 1; - } - else - { - $this->db->rollback(); - $this->error=$this->db->lasterror(); - dol_syslog("FactureFournisseur::delete ".$this->error, LOG_ERR); - return -1; - } + if (! $error) + { + // Appel des triggers + include_once(DOL_DOCUMENT_ROOT . "/core/class/interfaces.class.php"); + $interface=new Interfaces($this->db); + $result=$interface->run_triggers('INVOICE_SUPPLIER_DELETE',$this,$user,$langs,$conf); + if ($result < 0) { + $error++; $this->errors=$interface->errors; + } + // Fin appel triggers + } + + if (! $error) + { + // We remove directory + if ($conf->fournisseur->facture->dir_output) + { + $ref = dol_sanitizeFileName($this->ref); + $dir = $conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2).$ref; + $file = $dir . "/" . $ref . ".pdf"; + if (file_exists($file)) + { + if (! dol_delete_file($file,0,0,0,$this)) // For triggers + { + $this->error='ErrorFailToDeleteFile'; + $error++; + } + } + if (file_exists($dir)) + { + $res=@dol_delete_dir_recursive($dir); + if (! $res) + { + $this->error='ErrorFailToDeleteDir'; + $error++; + } + } + } + } + + if (! $error) + { + dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG); + $this->db->commit(); + return 1; } else { - $this->db->rollback(); - $this->error=$this->db->lasterror(); - dol_syslog("FactureFournisseur::delete ".$this->error, LOG_ERR); - return -1; + $this->error=$this->db->lasterror(); + dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR); + $this->db->rollback(); + return -$error; } } diff --git a/htdocs/fourn/facture/document.php b/htdocs/fourn/facture/document.php index 5b2f02769b9..8b0fca864eb 100644 --- a/htdocs/fourn/facture/document.php +++ b/htdocs/fourn/facture/document.php @@ -119,11 +119,12 @@ if ($action=='delete') if ($facture->fetch($facid)) { $langs->load("other"); + $facture->fetch_thirdparty(); $ref=dol_sanitizeFileName($facture->ref); $upload_dir = $conf->fournisseur->facture->dir_output.'/'.get_exdir($facture->id,2).$ref; $file = $upload_dir . '/' . GETPOST('urlfile'); // Do not use urldecode here ($_GET and $_REQUEST are already decoded by PHP). - dol_delete_file($file); + dol_delete_file($file,0,0,0,$facture); $mesg = '
'.$langs->trans('FileWasRemoved',GETPOST('urlfile')).'
'; } } diff --git a/htdocs/fourn/facture/fiche.php b/htdocs/fourn/facture/fiche.php index 9cd107ac126..7816bd09455 100644 --- a/htdocs/fourn/facture/fiche.php +++ b/htdocs/fourn/facture/fiche.php @@ -119,6 +119,7 @@ elseif ($action == 'confirm_valid' && $confirm == 'yes' && $user->rights->fourni elseif ($action == 'confirm_delete' && $confirm == 'yes' && $user->rights->fournisseur->facture->supprimer) { $object->fetch($id); + $object->fetch_thirdparty(); $result=$object->delete($id); if ($result > 0) { @@ -791,6 +792,7 @@ elseif ($action == 'builddoc') { // Save modele used $object->fetch($id); + $object->fetch_thirdparty(); if ($_REQUEST['model']) { $object->setDocModel($user, $_REQUEST['model']); @@ -822,9 +824,10 @@ elseif ($action == 'remove_file') if ($object->fetch($id)) { + $object->fetch_thirdparty(); $upload_dir = $conf->fournisseur->facture->dir_output . "/"; $file = $upload_dir . '/' . GETPOST('file'); - dol_delete_file($file); + dol_delete_file($file,0,0,0,$object); $mesg = '
'.$langs->trans("FileWasRemoved",GETPOST('file')).'
'; } }