diff --git a/htdocs/core/class/html.formfile.class.php b/htdocs/core/class/html.formfile.class.php
index cb0fe8728b2d6c022ea383779093cecdb66f2fc0..6aac42331bc0161f89e17554d72e33f6e87378a7 100644
--- a/htdocs/core/class/html.formfile.class.php
+++ b/htdocs/core/class/html.formfile.class.php
@@ -578,11 +578,9 @@ class FormFile
 					$var=!$var;
 
 					// Define relative path for download link (depends on module)
-					$relativepath=$file["name"];								// Cas general
+					$relativepath=$file["name"];										// Cas general
 					if ($modulesubdir) $relativepath=$modulesubdir."/".$file["name"];	// Cas propal, facture...
-					// Autre cas
-					if ($modulepart == 'donation')            { $relativepath = get_exdir($modulesubdir,2,0,0,null,'donation').$file["name"]; }
-					if ($modulepart == 'export')              { $relativepath = $file["name"]; }
+					if ($modulepart == 'export') $relativepath = $file["name"];			// Other case
 
 					$out.= "<tr ".$bc[$var].">";
 
diff --git a/htdocs/core/lib/files.lib.php b/htdocs/core/lib/files.lib.php
index 7eb6d9c5dcf2c3afe9e93c0efe366b091061a78d..daae392cd393af2a443b385d43d713bb82e65e51 100644
--- a/htdocs/core/lib/files.lib.php
+++ b/htdocs/core/lib/files.lib.php
@@ -1399,7 +1399,7 @@ function dol_most_recent_file($dir,$regexfilter='',$excludefilter=array('(\.meta
 /**
  * Security check when accessing to a document (used by document.php, viewimage.php and webservices)
  *
- * @param	string	$modulepart			Module of document (module, module_user_temp, module_user or module_temp)
+ * @param	string	$modulepart			Module of document ('module', 'module_user_temp', 'module_user' or 'module_temp')
  * @param	string	$original_file		Relative path with filename
  * @param	string	$entity				Restrict onto entity
  * @param  	User	$fuser				User object (forced)
@@ -1817,7 +1817,7 @@ function dol_check_secure_access_document($modulepart,$original_file,$entity,$fu
 			$accessallowed=1;
 		}
 
-		$original_file=$conf->banque->dir_output.'/bordereau/'.get_exdir(basename($original_file,".pdf"),2,1,0,null,'cheque').$original_file;
+		$original_file=$conf->banque->dir_output.'/bordereau/'.$original_file;		// original_file should contains relative path so include the get_exdir result
 	}
 
 	// Wrapping for export module
diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php
index 01615391adb85e266c96f44fd890dd9db137e413..6d5f1fe7a65681bb6f572905d4ed0a8ee8c293a2 100644
--- a/htdocs/core/lib/functions.lib.php
+++ b/htdocs/core/lib/functions.lib.php
@@ -3713,13 +3713,16 @@ function yn($yesno, $case=1, $color=0)
  *  @param	string	$modulepart		Type of object ('invoice_supplier, 'donation', 'invoice', ...')
  *  @return	string					Dir to use ending. Example '' or '1/' or '1/2/'
  */
-function get_exdir($num,$level,$alpha=0,$withoutslash=0,$object=null,$modulepart='')
+function get_exdir($num,$level,$alpha,$withoutslash,$object,$modulepart)
 {
 	global $conf;
 
 	$path = '';
 
-	if (! empty($level) && in_array($modulepart, array('shipment', 'member','don','donation','supplier_invoice','invoice_supplier')))
+	// TODO if object is null, load it from id and modulepart.
+
+
+	if (! empty($level) && in_array($modulepart, array('cheque','user','category','shipment', 'member','don','donation','supplier_invoice','invoice_supplier')))
 	{
 		// This part should be removed once all code is using "get_exdir" to forge path, with all parameters provided
 		if (empty($alpha)) $num = preg_replace('/([^0-9])/i','',$num);
diff --git a/htdocs/core/modules/askpricesupplier/doc/pdf_aurore.modules.php b/htdocs/core/modules/askpricesupplier/doc/pdf_aurore.modules.php
index 6acbb1fc4d06ab483c0135dc8aebe01b2a472f8b..db021bcb89e7c914c6d00ec7fe49470fdb1acf9a 100644
--- a/htdocs/core/modules/askpricesupplier/doc/pdf_aurore.modules.php
+++ b/htdocs/core/modules/askpricesupplier/doc/pdf_aurore.modules.php
@@ -165,7 +165,7 @@ class pdf_aurore extends ModelePDFAskPriceSupplier
 				$objphoto = new Product($this->db);
 				$objphoto->fetch($object->lines[$i]->fk_product);
 
-				$pdir = get_exdir($object->lines[$i]->fk_product,2,0,0,$object,'product') . $object->lines[$i]->fk_product ."/photos/";
+				$pdir = get_exdir($object->lines[$i]->fk_product,2,0,0,$objphoto,'product') . $object->lines[$i]->fk_product ."/photos/";
 				$dir = $conf->product->dir_output.'/'.$pdir;
 
 				$realpath='';
diff --git a/htdocs/core/modules/dons/html_cerfafr.modules.php b/htdocs/core/modules/dons/html_cerfafr.modules.php
index 9da0d45db502cbb018aa9f8b77c7b10f1ae5fea8..69c862d916d6c3cfb6515206d1152d56d944abdd 100644
--- a/htdocs/core/modules/dons/html_cerfafr.modules.php
+++ b/htdocs/core/modules/dons/html_cerfafr.modules.php
@@ -105,7 +105,7 @@ class html_cerfafr extends ModeleDon
 			else
 			{
 				$donref = dol_sanitizeFileName($don->ref);
-				$dir = $conf->don->dir_output . "/" . get_exdir($donref,2,0,0,$don,'donation');
+				$dir = $conf->don->dir_output . "/" . get_exdir($donref,2,0,1,$don,'donation') . "/" . dol_sanitizeFileName($don->ref);
 				$file = $dir . "/" . $donref . ".html";
 			}
 
diff --git a/htdocs/don/card.php b/htdocs/don/card.php
index 2047c9b5f8f294b4f351481509a008dc3467d702..d066f3ee6fffd816ce84237b250c2b180c9fc07b 100644
--- a/htdocs/don/card.php
+++ b/htdocs/don/card.php
@@ -173,6 +173,10 @@ if ($action == 'add')
 			header("Location: index.php");
 			exit;
 		}
+		else
+		{
+			setEventMessages($object->error, $object->errors, 'errors');
+		}
 	}
 }
 if ($action == 'confirm_delete' && GETPOST("confirm") == "yes" && $user->rights->don->supprimer)
@@ -232,7 +236,7 @@ if ($action == 'set_paid')
 if ($action == 'builddoc')
 {
 	$object = new Don($db);
-	$object->fetch($id);
+	$result=$object->fetch($id);
 
 	// Save last template used to generate document
 	if (GETPOST('model')) $object->setDocModel($user, GETPOST('model','alpha'));
@@ -380,6 +384,7 @@ if (! empty($id) && $action == 'edit')
 		dol_print_error($db); exit;
 	}
 
+	$hselected='card';
 	$head = donation_prepare_head($object);
 	dol_fiche_head($head, $hselected, $langs->trans("Donation"), 0, 'generic');
 
@@ -507,6 +512,8 @@ if (! empty($id) && $action != 'edit')
 		dol_print_error($db); exit;
 	}
 
+	$hselected='card';
+
 	$head = donation_prepare_head($object);
 	dol_fiche_head($head, $hselected, $langs->trans("Donation"), 0, 'generic');
 
@@ -720,7 +727,7 @@ if (! empty($id) && $action != 'edit')
 	 * Documents generes
 	 */
 	$filename=dol_sanitizeFileName($object->id);
-	$filedir=$conf->don->dir_output . '/' . get_exdir($filename,2,0,0,$object,'donation');
+	$filedir=$conf->don->dir_output . '/' . get_exdir($filename,2,0,1,$object,'donation'). '/'. dol_sanitizeFileName($object->ref);
 	$urlsource=$_SERVER['PHP_SELF'].'?rowid='.$object->id;
 	//            $genallowed=($fac->statut == 1 && ($fac->paye == 0 || $user->admin) && $user->rights->facture->creer);
 	//            $delallowed=$user->rights->facture->supprimer;
@@ -730,7 +737,7 @@ if (! empty($id) && $action != 'edit')
 	$var=true;
 
 	print '<br>';
-	$formfile->show_documents('donation',$filename,$filedir,$urlsource,$genallowed,$delallowed);
+	$formfile->show_documents('donation',$filename,$filedir,$urlsource,$genallowed,$delallowed,$object->modelpdf);
 
 	print '</td><td>&nbsp;</td>';
 
diff --git a/htdocs/don/class/don.class.php b/htdocs/don/class/don.class.php
index 33706ddd841029020aec5a975c90db11893d7b79..e3910b6fe5187c92ca45ba5240fd9aa9641feaa7 100644
--- a/htdocs/don/class/don.class.php
+++ b/htdocs/don/class/don.class.php
@@ -60,9 +60,10 @@ class Don extends CommonObject
     var $note_private;
     var $note_public;
     var $statut;
-
+	var $modelpdf;
     var $projet;
 
+
     /**
      *  Constructor
      *
@@ -306,14 +307,14 @@ class Don extends CommonObject
      * @return  int  		        <0 if KO, id of created donation if OK
      * TODO    add numbering module for Ref
      */
-    function create($user, $notrigger)
+    function create($user, $notrigger=0)
     {
         global $conf, $langs;
-		
+
 		$error = 0;
 		$ret = 0;
         $now=dol_now();
-		
+
         // Clean parameters
         $this->address=($this->address>0?$this->address:$this->address);
         $this->zip=($this->zip>0?$this->zip:$this->zip);
@@ -391,7 +392,7 @@ class Don extends CommonObject
             $this->errno = $this->db->lasterrno();
             $error++;
         }
-		
+
 		// Update extrafield
         if (!$error) {
         	if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
@@ -403,7 +404,7 @@ class Don extends CommonObject
         		}
         	}
         }
-		
+
 		if (!$error && !empty($conf->global->MAIN_DISABLEDRAFTSTATUS))
         {
             $res = $this->setValid($user);
@@ -434,7 +435,7 @@ class Don extends CommonObject
         global $langs, $conf;
 
 		$error=0;
-		
+
         // Clean parameters
         $this->address=($this->address>0?$this->address:$this->address);
         $this->zip=($this->zip>0?$this->zip:$this->zip);
@@ -523,11 +524,11 @@ class Don extends CommonObject
     {
 		global $conf, $langs;
 		require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
-		
+
 		$error = 0;
-        
+
 		$this->db->begin();
-		
+
         // Delete donation
         if (! $error)
         {
@@ -601,7 +602,7 @@ class Don extends CommonObject
         $sql = "SELECT d.rowid, d.datec, d.tms as datem, d.datedon,";
         $sql.= " d.firstname, d.lastname, d.societe, d.amount, d.fk_statut, d.address, d.zip, d.town, ";
         $sql.= " d.fk_country, d.country as country_olddata, d.public, d.amount, d.fk_payment, d.paid, d.note_private, d.note_public, cp.libelle, d.email, d.phone, ";
-        $sql.= " d.phone_mobile, d.fk_projet,";
+        $sql.= " d.phone_mobile, d.fk_projet, d.model_pdf,";
         $sql.= " p.title as project_label,";
         $sql.= " c.code as country_code, c.label as country";
         $sql.= " FROM ".MAIN_DB_PREFIX."don as d";
@@ -651,12 +652,13 @@ class Don extends CommonObject
                 $this->public         = $obj->public;
                 $this->modepaymentid  = $obj->fk_payment;
                 $this->modepayment    = $obj->libelle;
-				$this->paid			  = $obj->paid;	
+				$this->paid			  = $obj->paid;
                 $this->amount         = $obj->amount;
                 $this->note_private	  = $obj->note_private;
                 $this->note_public	  = $obj->note_public;
+                $this->modelpdf       = $obj->model_pdf;
                 $this->commentaire    = $obj->note;	// deprecated
-				
+
 				// Retrieve all extrafield for thirdparty
                 // fetch optionals attributes and labels
                 require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php';
@@ -821,7 +823,7 @@ class Don extends CommonObject
         if ($withpicto != 2) $result.=$link.$this->id.$linkend;
         return $result;
     }
-	
+
 	/**
 	 * Information on record
 	 *
diff --git a/htdocs/fourn/facture/list.php b/htdocs/fourn/facture/list.php
index d546e796fa17bcb78694cd1ff203cfeb6068b658..010e9b95ae6a5e02eb749ffcae51b49624be7a5c 100644
--- a/htdocs/fourn/facture/list.php
+++ b/htdocs/fourn/facture/list.php
@@ -309,7 +309,7 @@ if ($resql)
 		$facturestatic->ref_supplier=$obj->ref_supplier;
 		print $facturestatic->getNomUrl(1);
 		$filename=dol_sanitizeFileName($obj->ref);
-		$filedir=$conf->fournisseur->facture->dir_output.'/'.get_exdir($obj->facid,2,0,0,$object,'invoice_supplier').dol_sanitizeFileName($object->ref?$obj->ref:$obj->facid);
+		$filedir=$conf->fournisseur->facture->dir_output.'/'.get_exdir($obj->facid,2,0,0,$facturestatic,'invoice_supplier').dol_sanitizeFileName($obj->ref);
 		print $formfile->getDocumentsLink('facture_fournisseur', $filename, $filedir);
 		print "</td>\n";
 
diff --git a/htdocs/install/mysql/migration/3.7.0-3.8.0.sql b/htdocs/install/mysql/migration/3.7.0-3.8.0.sql
index ea80e18b213dbc01d0d1b0dd6e09284c4ac2d363..1d5ba017aab2c2cf8ba9ecb95e70f6af13588ee9 100755
--- a/htdocs/install/mysql/migration/3.7.0-3.8.0.sql
+++ b/htdocs/install/mysql/migration/3.7.0-3.8.0.sql
@@ -19,7 +19,7 @@
 -- -- VMYSQL4.1 DELETE FROM llx_usergroup_user      WHERE fk_usergroup NOT IN (SELECT rowid from llx_usergroup);
 
 
-UPDATE TABLE llx_facture_fourn set ref=id where ref IS NULL;
+UPDATE llx_facture_fourn set ref=rowid where ref IS NULL;
 ALTER TABLE llx_facture_fourn MODIFY COLUMN ref varchar(255) NOT NULL;
 
 
@@ -451,6 +451,7 @@ ALTER TABLE llx_don ADD COLUMN fk_country integer NOT NULL after country;
 ALTER TABLE llx_don CHANGE COLUMN fk_paiement fk_payment integer;
 ALTER TABLE llx_don ADD COLUMN paid smallint default 0 NOT NULL after fk_payment;
 ALTER TABLE llx_don CHANGE COLUMN fk_don_projet fk_projet integer NULL;
+ALTER TABLE llx_don CHANGE COLUMN fk_project fk_projet integer NULL;
 
 create table llx_don_extrafields
 (