diff --git a/htdocs/commande/card.php b/htdocs/commande/card.php
index 8b471fdcfe2..25e6606c16c 100644
--- a/htdocs/commande/card.php
+++ b/htdocs/commande/card.php
@@ -262,7 +262,6 @@ if (empty($reshook))
$object->location_incoterms = GETPOST('location_incoterms', 'alpha');
$object->multicurrency_code = GETPOST('multicurrency_code', 'alpha');
$object->multicurrency_tx = GETPOST('originmulticurrency_tx', 'int');
-
// Fill array 'array_options' with data from add form
if (! $error)
{
@@ -1212,7 +1211,6 @@ if (empty($reshook))
if (! $error)
{
// Actions on extra fields (by external module or standard code)
- // TODO le hook fait double emploi avec le trigger !!
$hookmanager->initHooks(array('orderdao'));
$parameters = array('id' => $object->id);
$reshook = $hookmanager->executeHooks('insertExtraFields', $parameters, $object, $action); // Note that $action and $object may have been modified by
diff --git a/htdocs/expensereport/card.php b/htdocs/expensereport/card.php
index 2d43faf016a..f0c21ebd3d8 100644
--- a/htdocs/expensereport/card.php
+++ b/htdocs/expensereport/card.php
@@ -38,6 +38,7 @@ require_once DOL_DOCUMENT_ROOT . '/core/modules/expensereport/modules_expenserep
require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
+require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
$langs->load("trips");
$langs->load("bills");
@@ -89,996 +90,1091 @@ $hidedesc = (GETPOST('hidedesc', 'int') ? GETPOST('hidedesc', 'int') : (! empty(
$hideref = (GETPOST('hideref', 'int') ? GETPOST('hideref', 'int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
+$object=new ExpenseReport($db);
+$extrafields = new ExtraFields($db);
+
+// fetch optionals attributes and labels
+$extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
+
+// Load object
+include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
+
+// Initialize technical object to manage hooks of thirdparties. Note that conf->hooks_modules contains array array
+$hookmanager->initHooks(array('expensereportcard','globalcard'));
+
+$permissionnote = $user->rights->expensereport->creer; // Used by the include of actions_setnotes.inc.php
+$permissiondellink = $user->rights->expensereport->creer; // Used by the include of actions_dellink.inc.php
+$permissionedit = $user->rights->expensereport->creer; // Used by the include of actions_lineupdown.inc.php
+
+
+
/*
* Actions
*/
-if ($cancel)
+$parameters = array('socid' => $socid);
+$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
+if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
+
+if (empty($reshook))
{
- $action='';
- $fk_projet='';
- $date_start='';
- $date_end='';
- $date='';
- $comments='';
- $vatrate='';
- $value_unit='';
- $qty=1;
- $fk_c_type_fees=-1;
-}
-
-if ($action == 'confirm_delete' && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->supprimer)
-{
- $object = new ExpenseReport($db);
- $result=$object->delete($id, $user);
- if ($result >= 0)
- {
- header("Location: index.php");
- exit;
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
-}
-
-if ($action == 'add' && $user->rights->expensereport->creer)
-{
- $object = new ExpenseReport($db);
-
- $object->date_debut = $date_start;
- $object->date_fin = $date_end;
-
- $object->fk_statut = 1;
- $object->fk_c_paiement = GETPOST('fk_c_paiement','int');
- $object->fk_user_validator = GETPOST('fk_user_validator','int');
- $object->note_public = GETPOST('note_public');
- $object->note_private = GETPOST('note_private');
-
- if ($object->periode_existe($user,$object->date_debut,$object->date_fin))
- {
- $error++;
- setEventMessages($langs->trans("ErrorDoubleDeclaration"), null, 'errors');
- $action='create';
- }
-
- if (! $error)
- {
- $db->begin();
-
- $id = $object->create($user);
-
- if ($id > 0)
- {
- $db->commit();
- Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- $db->rollback();
- $action='create';
- }
- }
-}
-
-if ($action == 'update' && $user->rights->expensereport->creer)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
-
- $object->date_debut = $date_start;
- $object->date_fin = $date_end;
-
- if($object->fk_statut < 3)
- {
- $object->fk_user_validator = GETPOST('fk_user_validator','int');
- }
-
- $object->fk_c_paiement = GETPOST('fk_c_paiement','int');
- $object->note_public = GETPOST('note_public');
- $object->note_private = GETPOST('note_private');
- $object->fk_user_modif = $user->id;
-
- $result = $object->update($user);
- if ($result > 0)
- {
- header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_POST['id']);
- exit;
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
-}
-
-if ($action == "confirm_validate" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->creer)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
- $result = $object->setValidate($user);
-
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- if ($result > 0 && $object->fk_user_validator > 0)
- {
- $langs->load("mails");
-
- // TO
- $destinataire = new User($db);
- $destinataire->fetch($object->fk_user_validator);
- $emailTo = $destinataire->email;
-
- // FROM
- $expediteur = new User($db);
- $expediteur->fetch($object->fk_user_author);
- $emailFrom = $expediteur->email;
-
- if ($emailTo && $emailFrom)
- {
- $filename=array(); $filedir=array(); $mimetype=array();
-
- // SUBJECT
- $subject = $langs->trans("ExpenseReportWaitingForApproval");
-
- // CONTENT
- $link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
- $message = $langs->trans("ExpenseReportWaitingForApprovalMessage", $expediteur->getFullName($langs), get_date_range($object->date_debut,$object->date_fin,'',$langs), $link);
-
- // Rebuild pdf
- /*
- $object->setDocModel($user,"");
- $resultPDF = expensereport_pdf_create($db,$id,'',"",$langs);
-
- if($resultPDF):
- // ATTACHMENT
- array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
- array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref).".pdf");
- array_push($mimetype,"application/pdf");
- */
-
- // PREPARE SEND
- $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename);
-
- if ($mailfile)
- {
- // SEND
- $result=$mailfile->sendfile();
- if ($result)
- {
- $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
- setEventMessages($mesg, null, 'mesgs');
- header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- }
- else
- {
- $langs->load("other");
- if ($mailfile->error)
- {
- $mesg='';
- $mesg.=$langs->trans('ErrorFailedToSendMail', $emailFrom, $emailTo);
- $mesg.='
'.$mailfile->error;
- setEventMessages($mesg, null, 'errors');
- }
- else
- {
- setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'warnings');
- }
- }
- }
- else
- {
- setEventMessages($mailfile->error,$mailfile->errors,'errors');
- $action='';
- }
- }
- else
- {
- setEventMessages($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), null, 'warnings');
- $action='';
- }
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
-}
-
-if ($action == "confirm_save_from_refuse" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->creer)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
- $result = $object->set_save_from_refuse($user);
-
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- if ($result > 0)
- {
- if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
- {
- // Send mail
-
- // TO
- $destinataire = new User($db);
- $destinataire->fetch($object->fk_user_validator);
- $emailTo = $destinataire->email;
-
- if ($emailTo)
- {
- // FROM
- $expediteur = new User($db);
- $expediteur->fetch($object->fk_user_author);
- $emailFrom = $expediteur->email;
-
- // SUBJECT
- $subject = "' ERP - Note de frais à re-approuver";
-
- // CONTENT
- $dateRefusEx = explode(" ",$object->date_refuse);
-
- $message = "Bonjour {$destinataire->firstname},\n\n";
- $message.= "Le {$dateRefusEx[0]} à {$dateRefusEx[1]} vous avez refusé d'approuver la note de frais \"{$object->ref}\". Vous aviez émis le motif suivant : {$object->detail_refuse}\n\n";
- $message.= "L'auteur vient de modifier la note de frais, veuillez trouver la nouvelle version en pièce jointe.\n";
- $message.= "- Déclarant : {$expediteur->firstname} {$expediteur->lastname}\n";
- $message.= "- Période : du {$object->date_debut} au {$object->date_fin}\n";
- $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
- $message.= "Bien cordialement,\n' SI";
-
- // Génération du pdf avant attachement
- $object->setDocModel($user,"");
- $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
-
- if($resultPDF)
- {
- // ATTACHMENT
- $filename=array(); $filedir=array(); $mimetype=array();
- array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
- array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref_number).".pdf");
- array_push($mimetype,"application/pdf");
-
- // PREPARE SEND
- $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename);
-
- if (! $mailfile->error)
- {
- // SEND
- $result=$mailfile->sendfile();
- if ($result)
- {
- Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- }
- else
- {
- $mesg=$mailfile->error;
- setEventMessages($mesg, null, 'errors');
- }
- // END - Send mail
- }
- else
- {
- dol_print_error($db,$resultPDF);
- exit;
- }
- }
- }
- }
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
-}
-
-// Approve
-if ($action == "confirm_approve" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->approve)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
-
- $result = $object->setApproved($user);
-
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- if ($result > 0)
- {
- if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
- {
- // Send mail
-
- // TO
- $destinataire = new User($db);
- $destinataire->fetch($object->fk_user_author);
- $emailTo = $destinataire->email;
-
- // CC
- $emailCC = $conf->global->NDF_CC_EMAILS;
-
- // FROM
- $expediteur = new User($db);
- $expediteur->fetch($object->fk_user_valid);
- $emailFrom = $expediteur->email;
-
- // SUBJECT
- $subject = "' ERP - Note de frais validée";
-
- // CONTENT
- $message = "Bonjour {$destinataire->firstname},\n\n";
- $message.= "Votre note de frais \"{$object->ref}\" vient d'être approuvé!\n";
- $message.= "- Approbateur : {$expediteur->firstname} {$expediteur->lastname}\n";
- $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
- $message.= "Bien cordialement,\n' SI";
-
- // Génération du pdf avant attachement
- $object->setDocModel($user,"");
- $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
-
- if($resultPDF):
- // ATTACHMENT
- $filename=array(); $filedir=array(); $mimetype=array();
- array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
- array_push($filedir, $conf->expensereport->dir_output.
- "/".
- dol_sanitizeFileName($object->ref) .
- "/".
- dol_sanitizeFileName($object->ref).
- ".pdf"
- );
- array_push($mimetype,"application/pdf");
-
- // PREPARE SEND
- $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename,$emailCC);
-
- if(!$mailfile->error):
-
- // SEND
- $result=$mailfile->sendfile();
- if ($result):
- setEventMessages($langs->trans("MailSuccessfulySent",$emailFrom,$emailTo), null, 'mesgs');
- Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- else:
- setEventMessages($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo), null, 'errors');
- endif;
-
- else:
- setEventMessages($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo), null, 'errors');
- endif;
- // END - Send mail
- else : // if ($resultPDF)
- dol_print_error($db,$resultPDF);
- exit;
- endif;
- }
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
-}
-
-if ($action == "confirm_refuse" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->approve)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
-
- $result = $object->setDeny($user,GETPOST('detail_refuse'));
-
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- if ($result > 0)
- {
- if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
- {
- // Send mail
-
- // TO
- $destinataire = new User($db);
- $destinataire->fetch($object->fk_user_author);
- $emailTo = $destinataire->email;
-
- // FROM
- $expediteur = new User($db);
- $expediteur->fetch($object->fk_user_refuse);
- $emailFrom = $expediteur->email;
-
- // SUBJECT
- $subject = "' ERP - Note de frais refusée";
-
- // CONTENT
- $message = "Bonjour {$destinataire->firstname},\n\n";
- $message.= "Votre note de frais \"{$object->ref}\" vient d'être refusée.\n";
- $message.= "- Refuseur : {$expediteur->firstname} {$expediteur->lastname}\n";
- $message.= "- Motif de refus : {$_POST['detail_refuse']}\n";
- $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
- $message.= "Bien cordialement,\n' SI";
-
- // PREPARE SEND
- $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
-
- if(!$mailfile->error)
- {
- // SEND
- $result=$mailfile->sendfile();
- if ($result)
- {
- setEventMessages($langs->trans("MailSuccessfulySent",$emailFrom,$emailTo), null, 'mesgs');
- Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- }
- else
- {
- setEventMessages($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo), null, 'errors');
- }
- // END - Send mail
- }
- }
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
-}
-
-//var_dump($user->id == $object->fk_user_validator);exit;
-if ($action == "confirm_cancel" && GETPOST('confirm')=="yes" && GETPOST('detail_cancel') && $id > 0 && $user->rights->expensereport->creer)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
-
- if ($user->id == $object->fk_user_valid || $user->id == $object->fk_user_author)
- {
- $result = $object->set_cancel($user,GETPOST('detail_cancel'));
-
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- if ($result > 0)
- {
- if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
- {
- // Send mail
-
- // TO
- $destinataire = new User($db);
- $destinataire->fetch($object->fk_user_author);
- $emailTo = $destinataire->email;
-
- // FROM
- $expediteur = new User($db);
- $expediteur->fetch($object->fk_user_cancel);
- $emailFrom = $expediteur->email;
-
- // SUBJECT
- $subject = "' ERP - Note de frais annulée";
-
- // CONTENT
- $message = "Bonjour {$destinataire->firstname},\n\n";
- $message.= "Votre note de frais \"{$object->ref}\" vient d'être annulée.\n";
- $message.= "- Annuleur : {$expediteur->firstname} {$expediteur->lastname}\n";
- $message.= "- Motif d'annulation : {$_POST['detail_cancel']}\n";
- $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
- $message.= "Bien cordialement,\n' SI";
-
- // PREPARE SEND
- $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
-
- if(!$mailfile->error)
- {
- // SEND
- $result=$mailfile->sendfile();
- if ($result)
- {
- header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- }
- else
- {
- $mesg="Impossible d'envoyer l'email.";
- setEventMessages($mesg, null, 'errors');
- }
- // END - Send mail
- }
- else
- {
- setEventMessages($mail->error, $mail->errors, 'errors');
- }
- }
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- }
- else
- {
- setEventMessages($langs->transnoentitiesnoconv("OnlyOwnerCanCancel"), '', 'errors'); // Should not happened
- }
-}
-
-if ($action == "confirm_brouillonner" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->creer)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
- if ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid)
- {
- $result = $object->setStatut(0);
-
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- if ($result > 0)
- {
- header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- }
- else
- {
- setEventMessages("NOT_AUTHOR", '', 'errors');
- }
-}
-
-if ($action == 'set_paid' && $id > 0 && $user->rights->expensereport->to_paid)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
-
- $result = $object->set_paid($id, $user);
-
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- if ($result > 0)
- {
- if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
- {
- // Send mail
-
- // TO
- $destinataire = new User($db);
- $destinataire->fetch($object->fk_user_author);
- $emailTo = $destinataire->email;
-
- // FROM
- $expediteur = new User($db);
- $expediteur->fetch($user->id);
- $emailFrom = $expediteur->email;
-
- // SUBJECT
- $subject = "'ERP - Note de frais payée";
-
- // CONTENT
- $message = "Bonjour {$destinataire->firstname},\n\n";
- $message.= "Votre note de frais \"{$object->ref}\" vient d'être payée.\n";
- $message.= "- Payeur : {$expediteur->firstname} {$expediteur->lastname}\n";
- $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
- $message.= "Bien cordialement,\n' SI";
-
- // Generate pdf before attachment
- $object->setDocModel($user,"");
- $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
-
- // PREPARE SEND
- $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
-
- if(!$mailfile->error):
-
- // SEND
- $result=$mailfile->sendfile();
- if ($result):
-
- // Retour
- if($result):
- Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- else:
- dol_print_error($db);
- endif;
-
- else:
- dol_print_error($db,$acct->error);
- endif;
-
- else:
- $mesg="Impossible d'envoyer l'email.";
- setEventMessages($mesg, null, 'errors');
- endif;
- // END - Send mail
- }
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
-}
-
-if ($action == "addline" && $user->rights->expensereport->creer)
-{
- $error = 0;
-
- $db->begin();
-
- $object_ligne = new ExpenseReportLine($db);
-
- $vatrate = GETPOST('vatrate');
- $object_ligne->comments = GETPOST('comments');
- $qty = GETPOST('qty','int');
- if (empty($qty)) $qty=1;
- $object_ligne->qty = $qty;
-
- $up=price2num(GETPOST('value_unit'),'MU');
- $object_ligne->value_unit = $up;
-
- $object_ligne->date = $date;
-
- $object_ligne->fk_c_type_fees = GETPOST('fk_c_type_fees');
-
- // if VAT is not used in Dolibarr, set VAT rate to 0 because VAT rate is necessary.
- if (empty($vatrate)) $vatrate = "0.000";
-
- $object_ligne->vatrate = price2num($vatrate);
-
- $object_ligne->fk_projet = $fk_projet;
-
-
- if (! GETPOST('fk_c_type_fees') > 0)
- {
- $error++;
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
- $action='';
- }
-
- if ($vatrate < 0 || $vatrate == '')
- {
- $error++;
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("VAT")), null, 'errors');
- $action='';
- }
-
- /* Projects are never required. To force them, check module forceproject
- if ($conf->projet->enabled)
- {
- if (empty($object_ligne->fk_projet) || $object_ligne->fk_projet==-1)
- {
- $error++;
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Project")), null, 'errors');
- }
- }*/
-
- // Si aucune date n'est rentrée
- if (empty($object_ligne->date) || $object_ligne->date=="--")
- {
- $error++;
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
- }
- // Si aucun prix n'est rentré
- if($object_ligne->value_unit==0)
- {
- $error++;
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PriceUTTC")), null, 'errors');
- }
-
- // S'il y'a eu au moins une erreur
- if (! $error)
- {
- $object_ligne->fk_expensereport = $_POST['fk_expensereport'];
-
- $type = 0; // TODO What if service ?
- $seller = ''; // seller is unknown
- $tmp = calcul_price_total($qty, $up, 0, $vatrate, 0, 0, 0, 'TTC', 0, $type, $seller);
-
- $object_ligne->vatrate = price2num($vatrate);
- $object_ligne->total_ttc = $tmp[2];
- $object_ligne->total_ht = $tmp[0];
- $object_ligne->total_tva = $tmp[1];
-
- $result = $object_ligne->insert();
- if ($result > 0)
- {
- $db->commit();
- header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- }
- else
- {
- dol_print_error($db,$object->error);
- $db->rollback();
- }
- }
-
- $action='';
-}
-
-if ($action == 'confirm_delete_line' && GETPOST("confirm") == "yes" && $user->rights->expensereport->creer)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
-
- $object_ligne = new ExpenseReportLine($db);
- $object_ligne->fetch(GETPOST("rowid"));
- $total_ht = $object_ligne->total_ht;
- $total_tva = $object_ligne->total_tva;
-
- $result=$object->deleteline(GETPOST("rowid"), $user);
- if ($result >= 0)
- {
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- $object->update_totaux_del($object_ligne->total_ht,$object_ligne->total_tva);
- header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_GET['id']);
- exit;
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
-}
-
-if ($action == "updateligne" && $user->rights->expensereport->creer)
-{
- $object = new ExpenseReport($db);
- $object->fetch($id);
-
- $rowid = $_POST['rowid'];
- $type_fees_id = GETPOST('fk_c_type_fees');
- $projet_id = $fk_projet;
- $comments = GETPOST('comments');
- $qty = GETPOST('qty');
- $value_unit = GETPOST('value_unit');
- $vatrate = GETPOST('vatrate');
-
- if (! GETPOST('fk_c_type_fees') > 0)
- {
- $error++;
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
- $action='';
- }
- if (GETPOST('vatrate') < 0 || GETPOST('vatrate') == '')
- {
- $error++;
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Vat")), null, 'errors');
- $action='';
- }
-
- if (! $error)
- {
- // TODO Use update method of ExpenseReportLine
- $result = $object->updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $id);
- if ($result >= 0)
- {
- if ($result > 0)
- {
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
- {
- $outputlangs = $langs;
- $newlang = '';
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
- if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
- if (! empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- $model=$object->modelpdf;
- $ret = $object->fetch($id); // Reload to get new records
-
- $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- }
- }
-
- $result = $object->recalculer($id);
-
- header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
- exit;
- }
- else
- {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- }
-}
-
-
-/*
- * Generate or regenerate the PDF document
- */
-if ($action == 'builddoc') // GET or POST
-{
- $depl = new ExpenseReport($db, 0, $_GET['id']);
- $depl->fetch($id);
-
- if ($_REQUEST['model'])
- {
- $depl->setDocModel($user, $_REQUEST['model']);
- }
-
- $outputlangs = $langs;
- if (! empty($_REQUEST['lang_id']))
- {
- $outputlangs = new Translate("",$conf);
- $outputlangs->setDefaultLang($_REQUEST['lang_id']);
- }
- $result=expensereport_pdf_create($db, $depl, '', $depl->modelpdf, $outputlangs);
- if ($result <= 0)
- {
- setEventMessages($object->error, $object->errors, 'errors');
+ if ($cancel)
+ {
$action='';
- }
-}
+ $fk_projet='';
+ $date_start='';
+ $date_end='';
+ $date='';
+ $comments='';
+ $vatrate='';
+ $value_unit='';
+ $qty=1;
+ $fk_c_type_fees=-1;
+ }
-// Remove file in doc form
-else if ($action == 'remove_file')
-{
- $object = new ExpenseReport($db, 0, $_GET['id']);
- if ($object->fetch($id))
- {
- require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
+ include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not include_once
- $object->fetch_thirdparty();
+ include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
- $langs->load("other");
- $upload_dir = $conf->expensereport->dir_output;
- $file = $upload_dir . '/' . GETPOST('file');
- $ret=dol_delete_file($file,0,0,0,$object);
- if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
- else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
- $action='';
- }
+ include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
+
+ // Action clone object
+ if ($action == 'confirm_clone' && $confirm == 'yes' && $user->rights->expensereport->creer)
+ {
+ if (1==0 && ! GETPOST('clone_content') && ! GETPOST('clone_receivers'))
+ {
+ setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
+ }
+ else
+ {
+ if ($object->id > 0)
+ {
+ // Because createFromClone modifies the object, we must clone it so that we can restore it later
+ $orig = clone $object;
+
+ $result=$object->createFromClone($socid);
+ if ($result > 0)
+ {
+ header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
+ exit;
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ $object = $orig;
+ $action='';
+ }
+ }
+ }
+ }
+
+ if ($action == 'confirm_delete' && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->supprimer)
+ {
+ $object = new ExpenseReport($db);
+ $result=$object->delete($id, $user);
+ if ($result >= 0)
+ {
+ header("Location: index.php");
+ exit;
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
+ if ($action == 'add' && $user->rights->expensereport->creer)
+ {
+ $object = new ExpenseReport($db);
+
+ $object->date_debut = $date_start;
+ $object->date_fin = $date_end;
+
+ $object->fk_statut = 1;
+ $object->fk_c_paiement = GETPOST('fk_c_paiement','int');
+ $object->fk_user_validator = GETPOST('fk_user_validator','int');
+ $object->note_public = GETPOST('note_public');
+ $object->note_private = GETPOST('note_private');
+ // Fill array 'array_options' with data from add form
+ if (! $error)
+ {
+ $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
+ if ($ret < 0) $error++;
+ }
+
+ if ($object->periode_existe($user,$object->date_debut,$object->date_fin))
+ {
+ $error++;
+ setEventMessages($langs->trans("ErrorDoubleDeclaration"), null, 'errors');
+ $action='create';
+ }
+
+ if (! $error)
+ {
+ $db->begin();
+
+ $id = $object->create($user);
+
+ if ($id > 0)
+ {
+ $db->commit();
+ Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ $db->rollback();
+ $action='create';
+ }
+ }
+ }
+
+ if ($action == 'update' && $user->rights->expensereport->creer)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+
+ $object->date_debut = $date_start;
+ $object->date_fin = $date_end;
+
+ if($object->fk_statut < 3)
+ {
+ $object->fk_user_validator = GETPOST('fk_user_validator','int');
+ }
+
+ $object->fk_c_paiement = GETPOST('fk_c_paiement','int');
+ $object->note_public = GETPOST('note_public');
+ $object->note_private = GETPOST('note_private');
+ $object->fk_user_modif = $user->id;
+
+ $result = $object->update($user);
+ if ($result > 0)
+ {
+ header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_POST['id']);
+ exit;
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
+ if ($action == 'update_extras')
+ {
+ // Fill array 'array_options' with data from update form
+ $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
+ $ret = $extrafields->setOptionalsFromPost($extralabels, $object, GETPOST('attribute'));
+ if ($ret < 0) $error++;
+
+ if (! $error)
+ {
+ // Actions on extra fields (by external module or standard code)
+ $hookmanager->initHooks(array('expensereportdao'));
+ $parameters = array('id' => $object->id);
+ $reshook = $hookmanager->executeHooks('insertExtraFields', $parameters, $object, $action); // Note that $action and $object may have been modified by
+ // some hooks
+ if (empty($reshook)) {
+ $result = $object->insertExtraFields();
+ if ($result < 0) {
+ $error++;
+ }
+ } else if ($reshook < 0)
+ $error++;
+ }
+
+ if ($error)
+ $action = 'edit_extras';
+ }
+
+ if ($action == "confirm_validate" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->creer)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+ $result = $object->setValidate($user);
+
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ if ($result > 0 && $object->fk_user_validator > 0)
+ {
+ $langs->load("mails");
+
+ // TO
+ $destinataire = new User($db);
+ $destinataire->fetch($object->fk_user_validator);
+ $emailTo = $destinataire->email;
+
+ // FROM
+ $expediteur = new User($db);
+ $expediteur->fetch($object->fk_user_author);
+ $emailFrom = $expediteur->email;
+
+ if ($emailTo && $emailFrom)
+ {
+ $filename=array(); $filedir=array(); $mimetype=array();
+
+ // SUBJECT
+ $subject = $langs->trans("ExpenseReportWaitingForApproval");
+
+ // CONTENT
+ $link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
+ $message = $langs->trans("ExpenseReportWaitingForApprovalMessage", $expediteur->getFullName($langs), get_date_range($object->date_debut,$object->date_fin,'',$langs), $link);
+
+ // Rebuild pdf
+ /*
+ $object->setDocModel($user,"");
+ $resultPDF = expensereport_pdf_create($db,$id,'',"",$langs);
+
+ if($resultPDF):
+ // ATTACHMENT
+ array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
+ array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref).".pdf");
+ array_push($mimetype,"application/pdf");
+ */
+
+ // PREPARE SEND
+ $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename);
+
+ if ($mailfile)
+ {
+ // SEND
+ $result=$mailfile->sendfile();
+ if ($result)
+ {
+ $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
+ setEventMessages($mesg, null, 'mesgs');
+ header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ }
+ else
+ {
+ $langs->load("other");
+ if ($mailfile->error)
+ {
+ $mesg='';
+ $mesg.=$langs->trans('ErrorFailedToSendMail', $emailFrom, $emailTo);
+ $mesg.='
'.$mailfile->error;
+ setEventMessages($mesg, null, 'errors');
+ }
+ else
+ {
+ setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'warnings');
+ }
+ }
+ }
+ else
+ {
+ setEventMessages($mailfile->error,$mailfile->errors,'errors');
+ $action='';
+ }
+ }
+ else
+ {
+ setEventMessages($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), null, 'warnings');
+ $action='';
+ }
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
+ if ($action == "confirm_save_from_refuse" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->creer)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+ $result = $object->set_save_from_refuse($user);
+
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ if ($result > 0)
+ {
+ if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
+ {
+ // Send mail
+
+ // TO
+ $destinataire = new User($db);
+ $destinataire->fetch($object->fk_user_validator);
+ $emailTo = $destinataire->email;
+
+ if ($emailTo)
+ {
+ // FROM
+ $expediteur = new User($db);
+ $expediteur->fetch($object->fk_user_author);
+ $emailFrom = $expediteur->email;
+
+ // SUBJECT
+ $subject = "' ERP - Note de frais à re-approuver";
+
+ // CONTENT
+ $dateRefusEx = explode(" ",$object->date_refuse);
+
+ $message = "Bonjour {$destinataire->firstname},\n\n";
+ $message.= "Le {$dateRefusEx[0]} à {$dateRefusEx[1]} vous avez refusé d'approuver la note de frais \"{$object->ref}\". Vous aviez émis le motif suivant : {$object->detail_refuse}\n\n";
+ $message.= "L'auteur vient de modifier la note de frais, veuillez trouver la nouvelle version en pièce jointe.\n";
+ $message.= "- Déclarant : {$expediteur->firstname} {$expediteur->lastname}\n";
+ $message.= "- Période : du {$object->date_debut} au {$object->date_fin}\n";
+ $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
+ $message.= "Bien cordialement,\n' SI";
+
+ // Génération du pdf avant attachement
+ $object->setDocModel($user,"");
+ $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
+
+ if($resultPDF)
+ {
+ // ATTACHMENT
+ $filename=array(); $filedir=array(); $mimetype=array();
+ array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
+ array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref_number).".pdf");
+ array_push($mimetype,"application/pdf");
+
+ // PREPARE SEND
+ $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename);
+
+ if (! $mailfile->error)
+ {
+ // SEND
+ $result=$mailfile->sendfile();
+ if ($result)
+ {
+ Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ }
+ else
+ {
+ $mesg=$mailfile->error;
+ setEventMessages($mesg, null, 'errors');
+ }
+ // END - Send mail
+ }
+ else
+ {
+ dol_print_error($db,$resultPDF);
+ exit;
+ }
+ }
+ }
+ }
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
+ // Approve
+ if ($action == "confirm_approve" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->approve)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+
+ $result = $object->setApproved($user);
+
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ if ($result > 0)
+ {
+ if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
+ {
+ // Send mail
+
+ // TO
+ $destinataire = new User($db);
+ $destinataire->fetch($object->fk_user_author);
+ $emailTo = $destinataire->email;
+
+ // CC
+ $emailCC = $conf->global->NDF_CC_EMAILS;
+
+ // FROM
+ $expediteur = new User($db);
+ $expediteur->fetch($object->fk_user_valid);
+ $emailFrom = $expediteur->email;
+
+ // SUBJECT
+ $subject = "' ERP - Note de frais validée";
+
+ // CONTENT
+ $message = "Bonjour {$destinataire->firstname},\n\n";
+ $message.= "Votre note de frais \"{$object->ref}\" vient d'être approuvé!\n";
+ $message.= "- Approbateur : {$expediteur->firstname} {$expediteur->lastname}\n";
+ $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
+ $message.= "Bien cordialement,\n' SI";
+
+ // Génération du pdf avant attachement
+ $object->setDocModel($user,"");
+ $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
+
+ if($resultPDF):
+ // ATTACHMENT
+ $filename=array(); $filedir=array(); $mimetype=array();
+ array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
+ array_push($filedir, $conf->expensereport->dir_output.
+ "/".
+ dol_sanitizeFileName($object->ref) .
+ "/".
+ dol_sanitizeFileName($object->ref).
+ ".pdf"
+ );
+ array_push($mimetype,"application/pdf");
+
+ // PREPARE SEND
+ $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename,$emailCC);
+
+ if(!$mailfile->error):
+
+ // SEND
+ $result=$mailfile->sendfile();
+ if ($result):
+ setEventMessages($langs->trans("MailSuccessfulySent",$emailFrom,$emailTo), null, 'mesgs');
+ Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ else:
+ setEventMessages($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo), null, 'errors');
+ endif;
+
+ else:
+ setEventMessages($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo), null, 'errors');
+ endif;
+ // END - Send mail
+ else : // if ($resultPDF)
+ dol_print_error($db,$resultPDF);
+ exit;
+ endif;
+ }
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
+ if ($action == "confirm_refuse" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->approve)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+
+ $result = $object->setDeny($user,GETPOST('detail_refuse'));
+
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ if ($result > 0)
+ {
+ if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
+ {
+ // Send mail
+
+ // TO
+ $destinataire = new User($db);
+ $destinataire->fetch($object->fk_user_author);
+ $emailTo = $destinataire->email;
+
+ // FROM
+ $expediteur = new User($db);
+ $expediteur->fetch($object->fk_user_refuse);
+ $emailFrom = $expediteur->email;
+
+ // SUBJECT
+ $subject = "' ERP - Note de frais refusée";
+
+ // CONTENT
+ $message = "Bonjour {$destinataire->firstname},\n\n";
+ $message.= "Votre note de frais \"{$object->ref}\" vient d'être refusée.\n";
+ $message.= "- Refuseur : {$expediteur->firstname} {$expediteur->lastname}\n";
+ $message.= "- Motif de refus : {$_POST['detail_refuse']}\n";
+ $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
+ $message.= "Bien cordialement,\n' SI";
+
+ // PREPARE SEND
+ $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
+
+ if(!$mailfile->error)
+ {
+ // SEND
+ $result=$mailfile->sendfile();
+ if ($result)
+ {
+ setEventMessages($langs->trans("MailSuccessfulySent",$emailFrom,$emailTo), null, 'mesgs');
+ Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ }
+ else
+ {
+ setEventMessages($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo), null, 'errors');
+ }
+ // END - Send mail
+ }
+ }
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
+ //var_dump($user->id == $object->fk_user_validator);exit;
+ if ($action == "confirm_cancel" && GETPOST('confirm')=="yes" && GETPOST('detail_cancel') && $id > 0 && $user->rights->expensereport->creer)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+
+ if ($user->id == $object->fk_user_valid || $user->id == $object->fk_user_author)
+ {
+ $result = $object->set_cancel($user,GETPOST('detail_cancel'));
+
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ if ($result > 0)
+ {
+ if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
+ {
+ // Send mail
+
+ // TO
+ $destinataire = new User($db);
+ $destinataire->fetch($object->fk_user_author);
+ $emailTo = $destinataire->email;
+
+ // FROM
+ $expediteur = new User($db);
+ $expediteur->fetch($object->fk_user_cancel);
+ $emailFrom = $expediteur->email;
+
+ // SUBJECT
+ $subject = "' ERP - Note de frais annulée";
+
+ // CONTENT
+ $message = "Bonjour {$destinataire->firstname},\n\n";
+ $message.= "Votre note de frais \"{$object->ref}\" vient d'être annulée.\n";
+ $message.= "- Annuleur : {$expediteur->firstname} {$expediteur->lastname}\n";
+ $message.= "- Motif d'annulation : {$_POST['detail_cancel']}\n";
+ $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
+ $message.= "Bien cordialement,\n' SI";
+
+ // PREPARE SEND
+ $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
+
+ if(!$mailfile->error)
+ {
+ // SEND
+ $result=$mailfile->sendfile();
+ if ($result)
+ {
+ header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ }
+ else
+ {
+ $mesg="Impossible d'envoyer l'email.";
+ setEventMessages($mesg, null, 'errors');
+ }
+ // END - Send mail
+ }
+ else
+ {
+ setEventMessages($mail->error, $mail->errors, 'errors');
+ }
+ }
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+ else
+ {
+ setEventMessages($langs->transnoentitiesnoconv("OnlyOwnerCanCancel"), '', 'errors'); // Should not happened
+ }
+ }
+
+ if ($action == "confirm_brouillonner" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->creer)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+ if ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid)
+ {
+ $result = $object->setStatut(0);
+
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ if ($result > 0)
+ {
+ header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+ else
+ {
+ setEventMessages("NOT_AUTHOR", '', 'errors');
+ }
+ }
+
+ if ($action == 'set_paid' && $id > 0 && $user->rights->expensereport->to_paid)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+
+ $result = $object->set_paid($id, $user);
+
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ if ($result > 0)
+ {
+ if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) // TODO Translate this so we can remove condition
+ {
+ // Send mail
+
+ // TO
+ $destinataire = new User($db);
+ $destinataire->fetch($object->fk_user_author);
+ $emailTo = $destinataire->email;
+
+ // FROM
+ $expediteur = new User($db);
+ $expediteur->fetch($user->id);
+ $emailFrom = $expediteur->email;
+
+ // SUBJECT
+ $subject = "'ERP - Note de frais payée";
+
+ // CONTENT
+ $message = "Bonjour {$destinataire->firstname},\n\n";
+ $message.= "Votre note de frais \"{$object->ref}\" vient d'être payée.\n";
+ $message.= "- Payeur : {$expediteur->firstname} {$expediteur->lastname}\n";
+ $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
+ $message.= "Bien cordialement,\n' SI";
+
+ // Generate pdf before attachment
+ $object->setDocModel($user,"");
+ $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
+
+ // PREPARE SEND
+ $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
+
+ if(!$mailfile->error):
+
+ // SEND
+ $result=$mailfile->sendfile();
+ if ($result):
+
+ // Retour
+ if($result):
+ Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ else:
+ dol_print_error($db);
+ endif;
+
+ else:
+ dol_print_error($db,$acct->error);
+ endif;
+
+ else:
+ $mesg="Impossible d'envoyer l'email.";
+ setEventMessages($mesg, null, 'errors');
+ endif;
+ // END - Send mail
+ }
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
+ if ($action == "addline" && $user->rights->expensereport->creer)
+ {
+ $error = 0;
+
+ $db->begin();
+
+ $object_ligne = new ExpenseReportLine($db);
+
+ $vatrate = GETPOST('vatrate');
+ $object_ligne->comments = GETPOST('comments');
+ $qty = GETPOST('qty','int');
+ if (empty($qty)) $qty=1;
+ $object_ligne->qty = $qty;
+
+ $up=price2num(GETPOST('value_unit'),'MU');
+ $object_ligne->value_unit = $up;
+
+ $object_ligne->date = $date;
+
+ $object_ligne->fk_c_type_fees = GETPOST('fk_c_type_fees');
+
+ // if VAT is not used in Dolibarr, set VAT rate to 0 because VAT rate is necessary.
+ if (empty($vatrate)) $vatrate = "0.000";
+
+ $object_ligne->vatrate = price2num($vatrate);
+
+ $object_ligne->fk_projet = $fk_projet;
+
+
+ if (! GETPOST('fk_c_type_fees') > 0)
+ {
+ $error++;
+ setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
+ $action='';
+ }
+
+ if ($vatrate < 0 || $vatrate == '')
+ {
+ $error++;
+ setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("VAT")), null, 'errors');
+ $action='';
+ }
+
+ /* Projects are never required. To force them, check module forceproject
+ if ($conf->projet->enabled)
+ {
+ if (empty($object_ligne->fk_projet) || $object_ligne->fk_projet==-1)
+ {
+ $error++;
+ setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Project")), null, 'errors');
+ }
+ }*/
+
+ // Si aucune date n'est rentrée
+ if (empty($object_ligne->date) || $object_ligne->date=="--")
+ {
+ $error++;
+ setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
+ }
+ // Si aucun prix n'est rentré
+ if($object_ligne->value_unit==0)
+ {
+ $error++;
+ setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PriceUTTC")), null, 'errors');
+ }
+
+ // S'il y'a eu au moins une erreur
+ if (! $error)
+ {
+ $object_ligne->fk_expensereport = $_POST['fk_expensereport'];
+
+ $type = 0; // TODO What if service ?
+ $seller = ''; // seller is unknown
+ $tmp = calcul_price_total($qty, $up, 0, $vatrate, 0, 0, 0, 'TTC', 0, $type, $seller);
+
+ $object_ligne->vatrate = price2num($vatrate);
+ $object_ligne->total_ttc = $tmp[2];
+ $object_ligne->total_ht = $tmp[0];
+ $object_ligne->total_tva = $tmp[1];
+
+ $result = $object_ligne->insert();
+ if ($result > 0)
+ {
+ $db->commit();
+ header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ }
+ else
+ {
+ dol_print_error($db,$object->error);
+ $db->rollback();
+ }
+ }
+
+ $action='';
+ }
+
+ if ($action == 'confirm_delete_line' && GETPOST("confirm") == "yes" && $user->rights->expensereport->creer)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+
+ $object_ligne = new ExpenseReportLine($db);
+ $object_ligne->fetch(GETPOST("rowid"));
+ $total_ht = $object_ligne->total_ht;
+ $total_tva = $object_ligne->total_tva;
+
+ $result=$object->deleteline(GETPOST("rowid"), $user);
+ if ($result >= 0)
+ {
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ $object->update_totaux_del($object_ligne->total_ht,$object_ligne->total_tva);
+ header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_GET['id']);
+ exit;
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+
+ if ($action == "updateligne" && $user->rights->expensereport->creer)
+ {
+ $object = new ExpenseReport($db);
+ $object->fetch($id);
+
+ $rowid = $_POST['rowid'];
+ $type_fees_id = GETPOST('fk_c_type_fees');
+ $projet_id = $fk_projet;
+ $comments = GETPOST('comments');
+ $qty = GETPOST('qty');
+ $value_unit = GETPOST('value_unit');
+ $vatrate = GETPOST('vatrate');
+
+ if (! GETPOST('fk_c_type_fees') > 0)
+ {
+ $error++;
+ setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
+ $action='';
+ }
+ if (GETPOST('vatrate') < 0 || GETPOST('vatrate') == '')
+ {
+ $error++;
+ setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Vat")), null, 'errors');
+ $action='';
+ }
+
+ if (! $error)
+ {
+ // TODO Use update method of ExpenseReportLine
+ $result = $object->updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $id);
+ if ($result >= 0)
+ {
+ if ($result > 0)
+ {
+ // Define output language
+ if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
+ {
+ $outputlangs = $langs;
+ $newlang = '';
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
+ if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
+ if (! empty($newlang)) {
+ $outputlangs = new Translate("", $conf);
+ $outputlangs->setDefaultLang($newlang);
+ }
+ $model=$object->modelpdf;
+ $ret = $object->fetch($id); // Reload to get new records
+
+ $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
+ }
+ }
+
+ $result = $object->recalculer($id);
+
+ header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
+ exit;
+ }
+ else
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ }
+ }
+ }
+
+
+ /*
+ * Generate or regenerate the PDF document
+ */
+ if ($action == 'builddoc') // GET or POST
+ {
+ $depl = new ExpenseReport($db, 0, $_GET['id']);
+ $depl->fetch($id);
+
+ if ($_REQUEST['model'])
+ {
+ $depl->setDocModel($user, $_REQUEST['model']);
+ }
+
+ $outputlangs = $langs;
+ if (! empty($_REQUEST['lang_id']))
+ {
+ $outputlangs = new Translate("",$conf);
+ $outputlangs->setDefaultLang($_REQUEST['lang_id']);
+ }
+ $result=expensereport_pdf_create($db, $depl, '', $depl->modelpdf, $outputlangs);
+ if ($result <= 0)
+ {
+ setEventMessages($object->error, $object->errors, 'errors');
+ $action='';
+ }
+ }
+
+ // Remove file in doc form
+ else if ($action == 'remove_file')
+ {
+ $object = new ExpenseReport($db, 0, $_GET['id']);
+ if ($object->fetch($id))
+ {
+ require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
+
+ $object->fetch_thirdparty();
+
+ $langs->load("other");
+ $upload_dir = $conf->expensereport->dir_output;
+ $file = $upload_dir . '/' . GETPOST('file');
+ $ret=dol_delete_file($file,0,0,0,$object);
+ if ($ret) setEventMessages($langs->trans("FileWasRemoved", GETPOST('urlfile')), null, 'mesgs');
+ else setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), null, 'errors');
+ $action='';
+ }
+ }
}
/*
* View
*/
+
$title=$langs->trans("ExpenseReport") . " - " . $langs->trans("Card");
$helpurl="EN:Module_Expense_Reports";
llxHeader("",$title,$helpurl);
@@ -1155,6 +1251,14 @@ if ($action == 'create')
print '';
}
+ // Other attributes
+ $parameters = array('colspan' => ' colspan="3"');
+ $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by
+ // hook
+ if (empty($reshook) && ! empty($extrafields->attribute_label)) {
+ print $object->showOptionals($extrafields, 'edit');
+ }
+
print '