PageRenderTime 138ms CodeModel.GetById 38ms app.highlight 87ms RepoModel.GetById 2ms app.codeStats 0ms

/htdocs/expensereport/card.php

https://github.com/atm-maximep/dolibarr
PHP | 2085 lines | 1643 code | 248 blank | 194 comment | 336 complexity | c3f215c18eaa4b1dac4479f8ee921f2e MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1<?php
   2/* Copyright (C) 2003      Rodolphe Quiedeville <rodolphe@quiedeville.org>
   3 * Copyright (C) 2004-2008 Laurent Destailleur  <eldy@users.sourceforge.net>
   4 * Copyright (C) 2005-2009 Regis Houssin        <regis@dolibarr.fr>
   5 * Copyright (C) 2015      Alexandre Spangaro   <alexandre.spangaro@gmail.com>
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License as published by
   9 * the Free Software Foundation; either version 3 of the License, or
  10 * (at your option) any later version.
  11 *
  12 * This program is distributed in the hope that it will be useful,
  13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15 * GNU General Public License for more details.
  16 *
  17 * You should have received a copy of the GNU General Public License
  18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19 */
  20
  21/**
  22 *  \file       	htdocs/expensereport/card.php
  23 *  \ingroup    	expensereport
  24 *  \brief      	Page for trip and expense report card
  25 */
  26
  27$res=0;
  28require '../main.inc.php';
  29require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';
  30require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
  31require_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
  32require_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php';
  33require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
  34require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php';
  35require_once DOL_DOCUMENT_ROOT . '/core/lib/expensereport.lib.php';
  36require_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php';
  37require_once DOL_DOCUMENT_ROOT . '/core/modules/expensereport/modules_expensereport.php';
  38require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
  39require_once DOL_DOCUMENT_ROOT . '/compta/paiement/class/paiement.class.php';
  40require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
  41
  42$langs->load("trips");
  43$langs->load("bills");
  44$langs->load("mails");
  45
  46$action=GETPOST('action');
  47$cancel=GETPOST('cancel');
  48$date_start = dol_mktime(0, 0, 0, GETPOST('date_debutmonth'), GETPOST('date_debutday'), GETPOST('date_debutyear'));
  49$date_end = dol_mktime(0, 0, 0, GETPOST('date_finmonth'), GETPOST('date_finday'), GETPOST('date_finyear'));
  50$date = dol_mktime(0, 0, 0, GETPOST('datemonth'), GETPOST('dateday'), GETPOST('dateyear'));
  51$fk_projet=GETPOST('fk_projet');
  52$ref=GETPOST("ref",'alpha');
  53
  54// If socid provided by ajax company selector
  55if (! empty($_REQUEST['socid_id']))
  56{
  57	$_GET['socid'] = $_GET['socid_id'];
  58	$_POST['socid'] = $_POST['socid_id'];
  59	$_REQUEST['socid'] = $_REQUEST['socid_id'];
  60}
  61
  62// Security check
  63$id=GETPOST("id",'int');
  64if ($user->societe_id) $socid=$user->societe_id;
  65$result = restrictedArea($user, 'expensereport', 0, 'expensereport');
  66
  67
  68// Hack to use expensereport dir
  69$rootfordata = DOL_DATA_ROOT;
  70$rootforuser = DOL_DATA_ROOT;
  71// If multicompany module is enabled, we redefine the root of data
  72if (! empty($conf->multicompany->enabled) && ! empty($conf->entity) && $conf->entity > 1)
  73{
  74	$rootfordata.='/'.$conf->entity;
  75}
  76$conf->expensereport->dir_output = $rootfordata.'/expensereport';
  77
  78// Define $urlwithroot
  79$urlwithouturlroot=preg_replace('/'.preg_quote(DOL_URL_ROOT,'/').'$/i','',trim($dolibarr_main_url_root));
  80$urlwithroot=$urlwithouturlroot.DOL_URL_ROOT;		// This is to use external domain name found into config file
  81//$urlwithroot=DOL_MAIN_URL_ROOT;					// This is to use same domain name than current
  82
  83// PDF
  84$hidedetails = (GETPOST('hidedetails', 'int') ? GETPOST('hidedetails', 'int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0));
  85$hidedesc = (GETPOST('hidedesc', 'int') ? GETPOST('hidedesc', 'int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0));
  86$hideref = (GETPOST('hideref', 'int') ? GETPOST('hideref', 'int') : (! empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0));
  87
  88
  89
  90/*
  91 * Actions
  92 */
  93
  94if ($cancel) $action='';
  95
  96if ($action == 'confirm_delete' && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->supprimer)
  97{
  98	$object = new ExpenseReport($db);
  99	$result=$object->delete($id, $user);
 100	if ($result >= 0)
 101	{
 102		header("Location: index.php");
 103		exit;
 104	}
 105	else
 106	{
 107		setEventMessages($object->error, $object->errors, 'errors');
 108	}
 109}
 110
 111if ($action == 'add' && $user->rights->expensereport->creer)
 112{
 113	$object = new ExpenseReport($db);
 114
 115	$object->date_debut = $date_start;
 116	$object->date_fin = $date_end;
 117
 118	$object->fk_statut = 1;
 119	$object->fk_c_paiement				= GETPOST('fk_c_paiement','int');
 120	$object->fk_user_validator			= GETPOST('fk_user_validator','int');
 121	$object->note_public				= GETPOST('note_public');
 122	$object->note_private				= GETPOST('note_private');
 123
 124	if ($object->periode_existe($user,$object->date_debut,$object->date_fin))
 125	{
 126		$error++;
 127		setEventMessage($langs->trans("ErrorDoubleDeclaration"),'errors');
 128		$action='create';
 129	}
 130
 131	if (! $error)
 132	{
 133		$db->begin();
 134
 135		$id = $object->create($user);
 136
 137		if ($id > 0)
 138		{
 139			$db->commit();
 140			Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 141			exit;
 142		}
 143		else
 144		{
 145			setEventMessages($object->error, $object->errors, 'errors');
 146			$db->rollback();
 147			$action='create';
 148		}
 149	}
 150}
 151
 152if ($action == 'update' && $user->rights->expensereport->creer)
 153{
 154	$object = new ExpenseReport($db);
 155	$object->fetch($id);
 156
 157	$object->date_debut = $date_start;
 158	$object->date_fin = $date_end;
 159
 160	if($object->fk_statut < 3)
 161	{
 162		$object->fk_user_validator = GETPOST('fk_user_validator','int');
 163	}
 164
 165	$object->fk_c_paiement = GETPOST('fk_c_paiement','int');
 166	$object->note_public = GETPOST('note_public');
 167	$object->note_private = GETPOST('note_private');
 168
 169	$result = $object->update($user);
 170	if ($result > 0)
 171	{
 172		header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_POST['id']);
 173		exit;
 174	}
 175	else
 176	{
 177		setEventMessages($object->error, $object->errors, 'errors');
 178	}
 179}
 180
 181if ($action == "confirm_save" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->creer)
 182{
 183	$object = new ExpenseReport($db);
 184	$object->fetch($id);
 185	$result = $object->setValidate($user);
 186
 187	if ($result > 0)
 188	{
 189		// Define output language
 190		if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
 191		{
 192			$outputlangs = $langs;
 193			$newlang = '';
 194			if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
 195			if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
 196			if (! empty($newlang)) {
 197				$outputlangs = new Translate("", $conf);
 198				$outputlangs->setDefaultLang($newlang);
 199			}
 200			$model=$object->modelpdf;
 201			$ret = $object->fetch($id); // Reload to get new records
 202
 203			$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
 204		}
 205	}
 206
 207	if ($result > 0 && $object->fk_user_validator > 0)
 208	{
 209		$langs->load("mails");
 210
 211		// TO
 212		$destinataire = new User($db);
 213		$destinataire->fetch($object->fk_user_validator);
 214		$emailTo = $destinataire->email;
 215
 216		// FROM
 217		$expediteur = new User($db);
 218		$expediteur->fetch($object->fk_user_author);
 219		$emailFrom = $expediteur->email;
 220
 221		if ($emailTo && $emailFrom)
 222		{
 223			// SUBJECT
 224			$subject = $langs->trans("ExpenseReportWaitingForApproval");
 225
 226			// CONTENT
 227			$link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
 228			$message = $langs->trans("ExpenseReportWaitingForApprovalMessage", $expediteur->getFullName($langs), get_date_range($object->date_debut,$object->date_fin,'',$langs), $link);
 229
 230			// Rebuild pdf
 231			/*
 232			$object->setDocModel($user,"");
 233			$resultPDF = expensereport_pdf_create($db,$id,'',"",$langs);
 234
 235			if($resultPDF):
 236			// ATTACHMENT
 237			$filename=array(); $filedir=array(); $mimetype=array();
 238			array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
 239			array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref).".pdf");
 240			array_push($mimetype,"application/pdf");
 241			*/
 242
 243			// PREPARE SEND
 244			$mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename);
 245
 246			if ($mailfile)
 247			{
 248				// SEND
 249				$result=$mailfile->sendfile();
 250				if ($result)
 251				{
 252					$mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
 253					setEventMessage($mesg);
 254					header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 255					exit;
 256				}
 257				else
 258				{
 259					$langs->load("other");
 260					if ($mailfile->error)
 261					{
 262						$mesg='';
 263						$mesg.=$langs->trans('ErrorFailedToSendMail',$from,$sendto);
 264						$mesg.='<br>'.$mailfile->error;
 265						setEventMessage($mesg,'errors');
 266					}
 267					else
 268					{
 269						setEventMessage('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', 'warnings');
 270					}
 271				}
 272			}
 273			else
 274			{
 275				setEventMessages($mailfile->error,$mailfile->errors,'errors');
 276				$action='';
 277			}
 278		}
 279		else
 280		{
 281			setEventMessage($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), 'warnings');
 282			$action='';
 283		}
 284	}
 285	else
 286	{
 287		setEventMessages($object->error, $object->errors, 'errors');
 288	}
 289}
 290
 291if ($action == "confirm_save_from_refuse" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->creer)
 292{
 293	$object = new ExpenseReport($db);
 294	$object->fetch($id);
 295	$result = $object->set_save_from_refuse($user);
 296
 297	if ($result > 0)
 298	{
 299		// Define output language
 300		if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
 301		{
 302			$outputlangs = $langs;
 303			$newlang = '';
 304			if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
 305			if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
 306			if (! empty($newlang)) {
 307				$outputlangs = new Translate("", $conf);
 308				$outputlangs->setDefaultLang($newlang);
 309			}
 310			$model=$object->modelpdf;
 311			$ret = $object->fetch($id); // Reload to get new records
 312
 313			$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
 314		}
 315	}
 316
 317	if ($result > 0)
 318	{
 319		if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
 320		{
 321			// Send mail
 322
 323			// TO
 324			$destinataire = new User($db);
 325			$destinataire->fetch($object->fk_user_validator);
 326			$emailTo = $destinataire->email;
 327
 328			if ($emailTo)
 329			{
 330				// FROM
 331				$expediteur = new User($db);
 332				$expediteur->fetch($object->fk_user_author);
 333				$emailFrom = $expediteur->email;
 334
 335				// SUBJECT
 336				$subject = "' ERP - Note de frais à re-approuver";
 337
 338				// CONTENT
 339				$dateRefusEx = explode(" ",$object->date_refuse);
 340
 341				$message = "Bonjour {$destinataire->firstname},\n\n";
 342				$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";
 343				$message.= "L'auteur vient de modifier la note de frais, veuillez trouver la nouvelle version en pièce jointe.\n";
 344				$message.= "- Déclarant : {$expediteur->firstname} {$expediteur->lastname}\n";
 345				$message.= "- Période : du {$object->date_debut} au {$object->date_fin}\n";
 346				$message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
 347				$message.= "Bien cordialement,\n' SI";
 348
 349				// Génération du pdf avant attachement
 350				$object->setDocModel($user,"");
 351				$resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
 352
 353				if($resultPDF)
 354				{
 355					// ATTACHMENT
 356					$filename=array(); $filedir=array(); $mimetype=array();
 357					array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
 358					array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref_number).".pdf");
 359					array_push($mimetype,"application/pdf");
 360
 361					// PREPARE SEND
 362					$mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename);
 363
 364					if (! $mailfile->error)
 365					{
 366						// SEND
 367						$result=$mailfile->sendfile();
 368						if ($result)
 369						{
 370							Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 371							exit;
 372						}
 373						else
 374						{
 375							$mesg=$mailfile->error;
 376						}
 377						// END - Send mail
 378					}
 379					else
 380					{
 381						dol_print_error($db,$resultPDF);
 382						exit;
 383					}
 384				}
 385			}
 386		}
 387	}
 388	else
 389	{
 390		setEventMessages($object->error, $object->errors, 'errors');
 391	}
 392}
 393
 394// Approve
 395if ($action == "confirm_approve" && GETPOST("confirm") == "yes" && $id > 0 && $user->rights->expensereport->approve)
 396{
 397	$object = new ExpenseReport($db);
 398	$object->fetch($id);
 399
 400	$result = $object->setApproved($user);
 401
 402	if ($result > 0)
 403	{
 404		// Define output language
 405		if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
 406		{
 407			$outputlangs = $langs;
 408			$newlang = '';
 409			if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
 410			if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
 411			if (! empty($newlang)) {
 412				$outputlangs = new Translate("", $conf);
 413				$outputlangs->setDefaultLang($newlang);
 414			}
 415			$model=$object->modelpdf;
 416			$ret = $object->fetch($id); // Reload to get new records
 417
 418			$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
 419		}
 420	}
 421
 422	if ($result > 0)
 423	{
 424		if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
 425		{
 426			// Send mail
 427
 428			// TO
 429			$destinataire = new User($db);
 430			$destinataire->fetch($object->fk_user_author);
 431			$emailTo = $destinataire->email;
 432
 433			// CC
 434			$emailCC = $conf->global->NDF_CC_EMAILS;
 435
 436			// FROM
 437			$expediteur = new User($db);
 438			$expediteur->fetch($object->fk_user_valid);
 439			$emailFrom = $expediteur->email;
 440
 441			// SUBJECT
 442			$subject = "' ERP - Note de frais validée";
 443
 444			// CONTENT
 445			$message = "Bonjour {$destinataire->firstname},\n\n";
 446			$message.= "Votre note de frais \"{$object->ref}\" vient d'être approuvé!\n";
 447			$message.= "- Approbateur : {$expediteur->firstname} {$expediteur->lastname}\n";
 448			$message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
 449			$message.= "Bien cordialement,\n' SI";
 450
 451			// Génération du pdf avant attachement
 452			$object->setDocModel($user,"");
 453			$resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
 454
 455			if($resultPDF):
 456				// ATTACHMENT
 457				$filename=array(); $filedir=array(); $mimetype=array();
 458				array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
 459				array_push($filedir, $conf->expensereport->dir_output.
 460					"/".
 461					dol_sanitizeFileName($object->ref) .
 462					"/".
 463					dol_sanitizeFileName($object->ref).
 464					".pdf"
 465					);
 466				array_push($mimetype,"application/pdf");
 467
 468				// PREPARE SEND
 469				$mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message,$filedir,$mimetype,$filename,$emailCC);
 470
 471				if(!$mailfile->error):
 472
 473					// SEND
 474					$result=$mailfile->sendfile();
 475					if ($result):
 476						setEventMessage($langs->trans("MailSuccessfulySent",$emailFrom,$emailTo));
 477						Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 478						exit;
 479					else:
 480						setEventMessage($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo),'errors');
 481					endif;
 482
 483				else:
 484					setEventMessage($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo),'errors');
 485				endif;
 486				// END - Send mail
 487			else : // if ($resultPDF)
 488				dol_print_error($db,$resultPDF);
 489				exit;
 490			endif;
 491		}
 492	}
 493	else
 494	{
 495		setEventMessages($object->error, $object->errors, 'errors');
 496	}
 497}
 498
 499if ($action == "confirm_refuse" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->approve)
 500{
 501	$object = new ExpenseReport($db);
 502	$object->fetch($id);
 503
 504	$result = $object->setDeny($user,GETPOST('detail_refuse'));
 505
 506	if ($result > 0)
 507	{
 508		// Define output language
 509		if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
 510		{
 511			$outputlangs = $langs;
 512			$newlang = '';
 513			if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
 514			if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
 515			if (! empty($newlang)) {
 516				$outputlangs = new Translate("", $conf);
 517				$outputlangs->setDefaultLang($newlang);
 518			}
 519			$model=$object->modelpdf;
 520			$ret = $object->fetch($id); // Reload to get new records
 521
 522			$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
 523		}
 524	}
 525
 526	if ($result > 0)
 527	{
 528		if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
 529		{
 530			// Send mail
 531
 532			// TO
 533			$destinataire = new User($db);
 534			$destinataire->fetch($object->fk_user_author);
 535			$emailTo = $destinataire->email;
 536
 537			// FROM
 538			$expediteur = new User($db);
 539			$expediteur->fetch($object->fk_user_refuse);
 540			$emailFrom = $expediteur->email;
 541
 542			// SUBJECT
 543			$subject = "' ERP - Note de frais refusée";
 544
 545			// CONTENT
 546			$message = "Bonjour {$destinataire->firstname},\n\n";
 547			$message.= "Votre note de frais \"{$object->ref}\" vient d'être refusée.\n";
 548			$message.= "- Refuseur : {$expediteur->firstname} {$expediteur->lastname}\n";
 549			$message.= "- Motif de refus : {$_POST['detail_refuse']}\n";
 550			$message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
 551			$message.= "Bien cordialement,\n' SI";
 552
 553			// PREPARE SEND
 554			$mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
 555
 556			if(!$mailfile->error)
 557			{
 558				// SEND
 559				$result=$mailfile->sendfile();
 560				if ($result)
 561				{
 562					setEventMessage($langs->trans("MailSuccessfulySent",$emailFrom,$emailTo));
 563					Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 564					exit;
 565				}
 566				else
 567				{
 568					setEventMessage($langs->trans("ErrorFailedToSendMail",$emailFrom,$emailTo),'errors');
 569					$mesg="Impossible d'envoyer l'email.";
 570				}
 571				// END - Send mail
 572			}
 573		}
 574	}
 575	else
 576	{
 577		setEventMessage($object->error, $object->errors);
 578	}
 579}
 580
 581//var_dump($user->id == $object->fk_user_validator);exit;
 582if ($action == "confirm_cancel" && GETPOST('confirm')=="yes" && GETPOST('detail_cancel') && $id > 0 && $user->rights->expensereport->creer)
 583{
 584	$object = new ExpenseReport($db);
 585	$object->fetch($id);
 586
 587	if ($user->id == $object->fk_user_valid || $user->id == $object->fk_user_author)
 588	{
 589		$result = $object->set_cancel($user,GETPOST('detail_cancel'));
 590
 591		if ($result > 0)
 592		{
 593			// Define output language
 594			if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
 595			{
 596				$outputlangs = $langs;
 597				$newlang = '';
 598				if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
 599				if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
 600				if (! empty($newlang)) {
 601					$outputlangs = new Translate("", $conf);
 602					$outputlangs->setDefaultLang($newlang);
 603				}
 604				$model=$object->modelpdf;
 605				$ret = $object->fetch($id); // Reload to get new records
 606
 607				$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
 608			}
 609		}
 610
 611		if ($result > 0)
 612		{
 613			if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
 614			{
 615				// Send mail
 616
 617				// TO
 618				$destinataire = new User($db);
 619				$destinataire->fetch($object->fk_user_author);
 620				$emailTo = $destinataire->email;
 621
 622				// FROM
 623				$expediteur = new User($db);
 624				$expediteur->fetch($object->fk_user_cancel);
 625				$emailFrom = $expediteur->email;
 626
 627				// SUBJECT
 628				$subject = "' ERP - Note de frais annulée";
 629
 630				// CONTENT
 631				$message = "Bonjour {$destinataire->firstname},\n\n";
 632				$message.= "Votre note de frais \"{$object->ref}\" vient d'être annulée.\n";
 633				$message.= "- Annuleur : {$expediteur->firstname} {$expediteur->lastname}\n";
 634				$message.= "- Motif d'annulation : {$_POST['detail_cancel']}\n";
 635				$message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
 636				$message.= "Bien cordialement,\n' SI";
 637
 638				// PREPARE SEND
 639				$mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
 640
 641				if(!$mailfile->error)
 642				{
 643					// SEND
 644					$result=$mailfile->sendfile();
 645					if ($result)
 646					{
 647						header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 648						exit;
 649					}
 650					else
 651					{
 652						$mesg="Impossible d'envoyer l'email.";
 653					}
 654					// END - Send mail
 655				}
 656				else
 657				{
 658					setEventMessages($mail->error, $mail->errors, 'errors');
 659				}
 660			}
 661		}
 662		else
 663		{
 664			setEventMessages($object->error, $object->errors, 'errors');
 665		}
 666	}
 667}
 668
 669if ($action == "confirm_paid" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->to_paid)
 670{
 671	$object = new ExpenseReport($db);
 672	$object->fetch($id);
 673
 674	$result = $object->setPaid($user);
 675
 676	if ($result > 0)
 677	{
 678		// Define output language
 679		if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
 680		{
 681			$outputlangs = $langs;
 682			$newlang = '';
 683			if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
 684			if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
 685			if (! empty($newlang)) {
 686				$outputlangs = new Translate("", $conf);
 687				$outputlangs->setDefaultLang($newlang);
 688			}
 689			$model=$object->modelpdf;
 690			$ret = $object->fetch($id); // Reload to get new records
 691
 692			$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
 693		}
 694	}
 695
 696	if ($result > 0)
 697	{
 698		if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
 699		{
 700			// Send mail
 701
 702			// TO
 703			$destinataire = new User($db);
 704			$destinataire->fetch($object->fk_user_author);
 705			$emailTo = $destinataire->email;
 706
 707			// FROM
 708			$expediteur = new User($db);
 709			$expediteur->fetch($object->fk_user_paid);
 710			$emailFrom = $expediteur->email;
 711
 712			// SUBJECT
 713			$subject = "' ERP - Note de frais payée";
 714
 715			// CONTENT
 716			$message = "Bonjour {$destinataire->firstname},\n\n";
 717			$message.= "Votre note de frais \"{$object->ref}\" vient d'être payée.\n";
 718			$message.= "- Payeur : {$expediteur->firstname} {$expediteur->lastname}\n";
 719			$message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n";
 720			$message.= "Bien cordialement,\n' SI";
 721
 722			// Génération du pdf avant attachement
 723			$object->setDocModel($user,"");
 724			$resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
 725
 726			// PREPARE SEND
 727			$mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message);
 728
 729			if(!$mailfile->error):
 730
 731			// SEND
 732			$result=$mailfile->sendfile();
 733			if ($result):
 734			// Insert écriture dans le compte courant
 735			$idTrip 	= $id;
 736			$idAccount 	= 1;
 737
 738			$object = new ExpenseReport($db);
 739			$object->fetch($idTrip);
 740
 741			$datePaiement = explode("-",$object->date_paiement);
 742
 743			$dateop 	= dol_mktime(12,0,0,$datePaiement[1],$datePaiement[2],$datePaiement[0]);
 744			$operation	= $object->code_paiement;
 745			$label		= "Règlement ".$object->ref;
 746			$amount 	= - price2num($object->total_ttc);
 747			$num_chq	= '';
 748			$cat1		= '';
 749
 750			$user = new User($db);
 751			$user->fetch($object->fk_user_paid);
 752
 753			$acct=new Account($db,$idAccount);
 754			$insertid = $acct->addline($dateop, $operation, $label, $amount, $num_chq, $cat1, $user);
 755
 756			if ($insertid > 0):
 757			$sql = " UPDATE ".MAIN_DB_PREFIX."expensereport as d";
 758			$sql.= " SET integration_compta = 1, fk_bank_account = $idAccount";
 759			$sql.= " WHERE rowid = $idTrip";
 760			$resql=$db->query($sql);
 761			if($result):
 762			Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 763			exit;
 764			else:
 765			dol_print_error($db);
 766			endif;
 767			else:
 768			dol_print_error($db,$acct->error);
 769			endif;
 770			endif;
 771
 772			else:
 773
 774			$mesg="Impossible d'envoyer l'email.";
 775
 776			endif;
 777			// END - Send mail
 778		}
 779	}
 780	else
 781	{
 782		setEventMessages($object->error, $object->errors, 'errors');
 783	}
 784}
 785
 786if ($action == "confirm_brouillonner" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->creer)
 787{
 788	$object = new ExpenseReport($db);
 789	$object->fetch($id);
 790	if ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid)
 791	{
 792		$result = $object->setStatut(0);
 793
 794		if ($result > 0)
 795		{
 796			// Define output language
 797			if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
 798			{
 799				$outputlangs = $langs;
 800				$newlang = '';
 801				if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
 802				if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
 803				if (! empty($newlang)) {
 804					$outputlangs = new Translate("", $conf);
 805					$outputlangs->setDefaultLang($newlang);
 806				}
 807				$model=$object->modelpdf;
 808				$ret = $object->fetch($id); // Reload to get new records
 809
 810				$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
 811			}
 812		}
 813
 814		if ($result > 0)
 815		{
 816			header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 817			exit;
 818		}
 819		else
 820		{
 821			setEventMessages($object->error, $object->errors, 'errors');
 822		}
 823	}
 824	else
 825	{
 826		setEventMessages($langs->transnoentitiesnoconv("NOT_AUTHOR"), '', 'errors');
 827	}
 828}
 829
 830if ($action == 'set_paid')
 831{
 832	if ($object->set_paid($id) >= 0)
 833	{
 834		header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
 835		exit;
 836	}
 837    else {
 838	    setEventMessage($object->error, 'errors');
 839    }
 840}
 841
 842if ($action == "addline")
 843{
 844	$error = 0;
 845
 846	$db->begin();
 847
 848	$object_ligne = new ExpenseReportLine($db);
 849
 850	$object_ligne->comments = GETPOST('comments');
 851	$qty  = GETPOST('qty','int');
 852	if (empty($qty)) $qty=1;
 853	$object_ligne->qty = $qty;
 854
 855	$up=price2num(GETPOST('value_unit'),'MU');
 856	$object_ligne->value_unit = $up;
 857
 858	$object_ligne->date = $date;
 859
 860	$object_ligne->fk_c_type_fees = GETPOST('fk_c_type_fees');
 861
 862	$object_ligne->fk_c_tva = GETPOST('fk_c_tva');
 863	$object_ligne->vatrate = price2num(GETPOST('vatrate'));
 864
 865	$object_ligne->fk_projet = $fk_projet;
 866
 867	if (! GETPOST('fk_c_type_fees') > 0)
 868	{
 869		$error++;
 870		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type")),'errors');
 871		$action='';
 872	}
 873	if (GETPOST('vatrate') < 0 || GETPOST('vatrate') == '')
 874	{
 875		$error++;
 876		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Vat")),'errors');
 877		$action='';
 878	}
 879
 880    /* Projects are never required. To force them, check module forceproject
 881	if ($conf->projet->enabled)
 882	{
 883		if (empty($object_ligne->fk_projet) || $object_ligne->fk_projet==-1)
 884		{
 885			$error++;
 886			setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Project")), 'errors');
 887		}
 888	}*/
 889
 890	// Si aucune date n'est rentrée
 891	if (empty($object_ligne->date) || $object_ligne->date=="--")
 892	{
 893		$error++;
 894		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Date")), 'errors');
 895	}
 896	// Si aucun prix n'est rentré
 897	if($object_ligne->value_unit==0)
 898	{
 899		$error++;
 900		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("UP")), 'errors');
 901	}
 902
 903	// S'il y'a eu au moins une erreur
 904	if (! $error)
 905	{
 906		$object_ligne->fk_expensereport = $_POST['fk_expensereport'];
 907
 908		$type = 0;	// TODO What if service
 909		$tmp = calcul_price_total($qty, $up, 0, $vatrate, 0, 0, 0, 'TTC', 0, $type);
 910
 911		$object_ligne->vatrate = price2num(GETPOST('vatrate'));
 912		$object_ligne->total_ttc = $tmp[2];
 913		$object_ligne->total_ht = $tmp[0];
 914		$object_ligne->total_tva = $tmp[1];
 915
 916		$result = $object_ligne->insert();
 917		if ($result > 0)
 918		{
 919			$db->commit();
 920			header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
 921			exit;
 922		}
 923		else
 924		{
 925			dol_print_error($db,$object->error);
 926			$db->rollback();
 927		}
 928	}
 929
 930	$action='';
 931}
 932
 933if ($action == 'confirm_delete_line' && GETPOST("confirm") == "yes")
 934{
 935	$object = new ExpenseReport($db);
 936	$object->fetch($id);
 937
 938	$object_ligne = new ExpenseReportLine($db);
 939	$object_ligne->fetch(GETPOST("rowid"));
 940	$total_ht = $object_ligne->total_ht;
 941	$total_tva = $object_ligne->total_tva;
 942
 943	$result=$object->deleteline(GETPOST("rowid"));
 944	if ($result >= 0)
 945	{
 946		if ($result > 0)
 947		{
 948			// Define output language
 949			if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
 950			{
 951				$outputlangs = $langs;
 952				$newlang = '';
 953				if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
 954				if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
 955				if (! empty($newlang)) {
 956					$outputlangs = new Translate("", $conf);
 957					$outputlangs->setDefaultLang($newlang);
 958				}
 959				$model=$object->modelpdf;
 960				$ret = $object->fetch($id); // Reload to get new records
 961
 962				$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
 963			}
 964		}
 965
 966		$object->update_totaux_del($object_ligne->total_ht,$object_ligne->total_tva);
 967		header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_GET['id']);
 968		exit;
 969	}
 970	else
 971	{
 972		setEventMessages($object->error, $object->errors, 'errors');
 973	}
 974}
 975
 976if ($action == "updateligne" )
 977{
 978	$object = new ExpenseReport($db);
 979	$object->fetch($id);
 980
 981	$rowid = $_POST['rowid'];
 982	$type_fees_id = GETPOST('fk_c_type_fees');
 983	$object_ligne->fk_c_tva = GETPOST('fk_c_tva');
 984	$object_ligne->vatrate = price2num(GETPOST('vatrate'));
 985	$projet_id = $fk_projet;
 986	$comments = GETPOST('comments');
 987	$qty = GETPOST('qty');
 988	$value_unit = GETPOST('value_unit');
 989	$vatrate = GETPOST('vatrate');
 990
 991	if (! GETPOST('fk_c_type_fees') > 0)
 992	{
 993		$error++;
 994		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Type")),'errors');
 995		$action='';
 996	}
 997	if (GETPOST('vatrate') < 0 || GETPOST('vatrate') == '')
 998	{
 999		$error++;
1000		setEventMessage($langs->trans("ErrorFieldRequired",$langs->transnoentitiesnoconv("Vat")),'errors');
1001		$action='';
1002	}
1003
1004	if (! $error)
1005	{
1006		$result = $object->updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $id);
1007		if ($result >= 0)
1008		{
1009			if ($result > 0)
1010			{
1011				// Define output language
1012				if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
1013				{
1014					$outputlangs = $langs;
1015					$newlang = '';
1016					if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha');
1017					if ($conf->global->MAIN_MULTILANGS && empty($newlang))	$newlang = $object->thirdparty->default_lang;
1018					if (! empty($newlang)) {
1019						$outputlangs = new Translate("", $conf);
1020						$outputlangs->setDefaultLang($newlang);
1021					}
1022					$model=$object->modelpdf;
1023					$ret = $object->fetch($id); // Reload to get new records
1024
1025					$object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
1026				}
1027			}
1028
1029			$result = $object->recalculer($id);
1030
1031			header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
1032			exit;
1033		}
1034		else
1035		{
1036			setEventMessages($object->error, $object->errors, 'errors');
1037		}
1038	}
1039}
1040
1041
1042/*
1043 * Generate or regenerate the PDF document
1044 */
1045if ($action == 'builddoc')	// GET or POST
1046{
1047	$depl = new ExpenseReport($db, 0, $_GET['id']);
1048	$depl->fetch($id);
1049
1050	if ($_REQUEST['model'])
1051	{
1052		$depl->setDocModel($user, $_REQUEST['model']);
1053	}
1054
1055	$outputlangs = $langs;
1056	if (! empty($_REQUEST['lang_id']))
1057	{
1058		$outputlangs = new Translate("",$conf);
1059		$outputlangs->setDefaultLang($_REQUEST['lang_id']);
1060	}
1061	$result=expensereport_pdf_create($db, $depl, '', $depl->modelpdf, $outputlangs);
1062	if ($result <= 0)
1063	{
1064		setEventMessages($object->error, $object->errors, 'errors');
1065        $action='';
1066	}
1067}
1068
1069// Remove file in doc form
1070else if ($action == 'remove_file')
1071{
1072	$object = new ExpenseReport($db, 0, $_GET['id']);
1073	if ($object->fetch($id))
1074	{
1075		require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
1076
1077		$object->fetch_thirdparty();
1078
1079		$langs->load("other");
1080		$upload_dir = $conf->expensereport->dir_output;
1081		$file = $upload_dir . '/' . GETPOST('file');
1082		$ret=dol_delete_file($file,0,0,0,$object);
1083		if ($ret) setEventMessage($langs->trans("FileWasRemoved", GETPOST('urlfile')));
1084		else setEventMessage($langs->trans("ErrorFailToDeleteFile", GETPOST('urlfile')), 'errors');
1085		$action='';
1086	}
1087}
1088
1089
1090/*
1091 * View
1092 */
1093
1094llxHeader('', $langs->trans("ExpenseReport"));
1095
1096$form = new Form($db);
1097$formfile = new FormFile($db);
1098$formproject = new FormProjets($db);
1099$projecttmp = new Project($db);
1100
1101if (! empty($conf->global->DEPLACEMENT_TO_CLEAN))
1102{
1103	if(!empty($_GET['mesg']))
1104	{
1105		$text_mesg = explode(",",$_GET['mesg']);
1106
1107		foreach($text_mesg as $text)
1108		{
1109			$mesg.= "- ".$langs->trans($text)."<br />";
1110		}
1111
1112		print "<div class=\"error\" style=\"font-size:15px;background-color:#FFB3B3;\">";
1113		print $langs->trans("LINE_NOT_ADDED")."<br />";
1114		print $mesg;
1115		print "</div>";
1116	}
1117	else
1118	{
1119		if ($mesg) print "<div class=\"error\" style=\"font-size:16px;background-color:red;\">".$mesg."</div>";
1120	}
1121}
1122
1123
1124// Create
1125if ($action == 'create')
1126{
1127	print print_fiche_titre($langs->trans("NewTrip"));
1128
1129	print '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="create">';
1130	print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
1131	print '<input type="hidden" name="action" value="add">';
1132
1133	dol_fiche_head('');
1134
1135	print '<table class="border" width="100%">';
1136	print '<tbody>';
1137	print '<tr>';
1138	print '<td>'.$langs->trans("DateStart").'</td>';
1139	print '<td>';
1140	$form->select_date($date_start?$date_start:-1,'date_debut',0,0,0,'',1,1);
1141	print '</td>';
1142	print '</tr>';
1143	print '<tr>';
1144	print '<td>'.$langs->trans("DateEnd").'</td>';
1145	print '<td>';
1146	$form->select_date($date_end?$date_end:-1,'date_fin',0,0,0,'',1,1);
1147	print '</td>';
1148	print '</tr>';
1149	print '<tr>';
1150	print '<td>'.$langs->trans("VALIDATOR").'</td>';
1151	print '<td>';
1152	$object = new ExpenseReport($db);
1153	$include_users = $object->fetch_users_approver_expensereport();
1154	$s=$form->select_dolusers((GETPOST('fk_user_validator')?GETPOST('fk_user_validator'):$conf->global->EXPENSEREPORT_DEFAULT_VALIDATOR), "fk_user_validator", 1, "", 0, $include_users);
1155	print $form->textwithpicto($s, $langs->trans("AnyOtherInThisListCanValidate"));
1156	print '</td>';
1157	print '</tr>';
1158	if (! empty($conf->global->EXPENSEREPORT_ASK_PAYMENTMODE_ON_CREATION))
1159	{
1160		print '<tr>';
1161		print '<td>'.$langs->trans("ModePaiement").'</td>';
1162		print '<td>';
1163		$form->select_types_paiements(2,'fk_c_paiement');
1164		print '</td>';
1165		print '</tr>';
1166	}
1167
1168	// Public note
1169	print '<tr>';
1170	print '<td class="border" valign="top">' . $langs->trans('NotePublic') . '</td>';
1171	print '<td valign="top" colspan="2">';
1172
1173	$doleditor = new DolEditor('note_public', $note_public, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, 70);
1174	print $doleditor->Create(1);
1175	print '</td></tr>';
1176
1177	// Private note
1178	if (empty($user->societe_id)) {
1179		print '<tr>';
1180		print '<td class="border" valign="top">' . $langs->trans('NotePrivate') . '</td>';
1181		print '<td valign="top" colspan="2">';
1182
1183		$doleditor = new DolEditor('note_private', $note_private, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, 70);
1184		print $doleditor->Create(1);
1185		print '</td></tr>';
1186	}
1187
1188	print '<tbody>';
1189	print '</table>';
1190
1191	dol_fiche_end();
1192
1193	print '<div align="center">';
1194	print '<input type="submit" value="'.$langs->trans("AddTrip").'" name="bouton" class="button" />';
1195	print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="'.$langs->trans("Cancel").'" class="button" onclick="history.go(-1)" />';
1196	print '</div>';
1197
1198	print '</form>';
1199}
1200else
1201{
1202	if($id > 0 || $ref)
1203	{
1204		$object = new ExpenseReport($db);
1205		$result = $object->fetch($id, $ref);
1206
1207		if ($result > 0)
1208		{
1209			if ($object->fk_user_author != $user->id)
1210			{
1211				if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous))
1212				{
1213					print_fiche_titre($langs->trans('TripCard'));
1214
1215					print '<div class="tabBar">';
1216					print $langs->trans('NotUserRightToView');
1217					print '</div>';
1218
1219					$db->close();
1220
1221					llxFooter();
1222
1223					exit;
1224				}
1225			}
1226
1227			$head = expensereport_prepare_head($object);
1228
1229			if ($action == 'edit' && ($object->fk_statut < 3 || $object->fk_statut==99))
1230			{
1231				print "<form name='update' action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n";
1232				print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
1233				print '<input type="hidden" name="id" value="'.$id.'">';
1234
1235				dol_fiche_head($head, 'card', $langs->trans("TripCard"), 0, 'trip');
1236
1237				if($object->fk_statut==99)
1238				{
1239					print '<input type="hidden" name="action" value="updateFromRefuse">';
1240				}
1241				else
1242				{
1243					print '<input type="hidden" name="action" value="update">';
1244				}
1245
1246				print '<table class="border" style="width:100%;">';
1247
1248				$linkback = '<a href="'.DOL_URL_ROOT.'/expensereport/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
1249
1250            	// Ref
1251            	print '<tr><td width="25%">'.$langs->trans("Ref").'</td><td>';
1252            	print $form->showrefnav($object, 'ref', $linkback, 1, 'ref', 'ref', '');
1253            	print '</td></tr>';
1254
1255				print '<tr>';
1256				print '<td>'.$langs->trans("DateStart").'</td>';
1257				print '<td>';
1258				$form->select_date($object->date_debut,'date_debut');
1259				print '</td>';
1260				print '</tr>';
1261				print '<tr>';
1262				print '<td>'.$langs->trans("DateEnd").'</td>';
1263				print '<td>';
1264				$form->select_date($object->date_fin,'date_fin');
1265				print '</td>';
1266				print '</tr>';
1267
1268				if (! empty($conf->global->EXPENSEREPORT_ASK_PAYMENTMODE_ON_CREATION))
1269				{
1270					print '<tr>';
1271					print '<td>'.$langs->trans("ModePaiement").'</td>';
1272					print '<td>';
1273					$form->select_types_paiements($object->fk_c_paiement,'fk_c_paiement');
1274					print '</td>';
1275					print '</tr>';
1276				}
1277
1278				if($object->fk_statut<3)
1279				{
1280					print '<tr>';
1281					print '<td>'.$langs->trans("VALIDATOR").'</td>';	// Approbator
1282					print '<td>';
1283					$include_users = $object->fetch_users_approver_expensereport();
1284					$s=$form->select_dolusers($object->fk_user_validator,"fk_user_validator",1,"",0,$include_users);
1285					print $form->textwithpicto($s, $langs->trans("AnyOtherInThisListCanValidate"));
1286					print '</td>';
1287					print '</tr>';
1288				}
1289				else
1290				{
1291					print '<tr>';
1292					print '<td>'.$langs->trans("VALIDOR").'</td>';
1293					print '<td>';
1294					$userfee=new User($db);
1295					$userfee->fetch($object->fk_user_valid);
1296					print $userfee->getNomUrl(1);
1297					print '</td></tr>';
1298				}
1299
1300				print '<tr>';
1301				print '<td>'.$langs->trans("AUTHOR").'</td>';
1302				print '<td>';
1303				$userfee=new User($db);
1304				$userfee->fetch($object->fk_user_author);
1305				print $userfee->getNomUrl(1);
1306				print '</td></tr>';
1307				if ($object->fk_statut==6)
1308				{
1309					print '<tr>';
1310					print '<td>'.$langs->trans("AUTHORPAIEMENT").'</td>';
1311					print '<td>';
1312					$userfee=new User($db);
1313					$userfee->fetch($object->fk_user_paid);
1314					print $userfee->getNomUrl(1);
1315					print '</td></tr>';
1316
1317				}
1318
1319				// Public note
1320				print '<tr>';
1321				print '<td class="border" valign="top">' . $langs->trans('NotePublic') . '</td>';
1322				print '<td valign="top" colspan="2">';
1323
1324				$doleditor = new DolEditor('note_public', $object->note_public, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, 70);
1325				print $doleditor->Create(1);
1326				print '</td></tr>';
1327
1328				// Private note
1329				if (empty($user->societe_id)) {
1330					print '<tr>';
1331					print '<td class="border" valign="top">' . $langs->trans('NotePrivate') . '</td>';
1332					print '<td valign="top" colspan="2">';
1333
1334					$doleditor = new DolEditor('note_private', $object->note_private, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, 70);
1335					print $doleditor->Create(1);
1336					print '</td></tr>';
1337				}
1338
1339				print '</table>';
1340
1341				dol_fiche_end();
1342
1343				print '<div class="center">';
1344				print '<input type="submit" value="'.$langs->trans("Modify").'" name="bouton" class="button">';
1345				print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="'.$langs->trans("Cancel").'" class="button" onclick="history.go(-1)" />';
1346				print '</div>';
1347
1348				print '</form>';
1349			}
1350			else
1351			{
1352				dol_fiche_head($head, 'card', $langs->trans("TripCard"), 0, 'trip');
1353
1354				if ($action == 'save'):
1355				$ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("SaveTrip"),$langs->trans("ConfirmSaveTrip"),"confirm_save","","",1);
1356				if ($ret == 'html') print '<br>';
1357				endif;
1358
1359				if ($action == 'save_from_refuse'):
1360				$ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("SaveTrip"),$langs->trans("ConfirmSaveTrip"),"confirm_save_from_refuse","","",1);
1361				if ($ret == 'html') print '<br>';
1362				endif;
1363
1364				if ($action == 'delete'):
1365				$ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("DeleteTrip"),$langs->trans("ConfirmDeleteTrip"),"confirm_delete","","",1);
1366				if ($ret == 'html') print '<br>';
1367				endif;
1368
1369				if ($action == 'validate'):
1370				$ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("ValideTrip"),$langs->trans("ConfirmValideTrip"),"confirm_approve","","",1);
1371				if ($ret == 'html') print '<br>';
1372				endif;
1373
1374				if ($action == 'paid'):
1375				$ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("PaidTrip"),$langs->trans("ConfirmPaidTrip"),"confirm_paid","","",1);
1376				if ($ret == 'html') print '<br>';
1377				endif;
1378
1379				if ($action == 'cancel')
1380				{
1381					$array_input = array('text'=>$langs->trans("ConfirmCancelTrip"), array('type'=>"text",'label'=>$langs->trans("Comment"),'name'=>"detail_cancel",'size'=>"50",'value'=>""));
1382					$ret=$form->form_confirm($_SEVER["PHP_SELF"]."?id=".$id,$langs->trans("Cancel"),"","confirm_cancel",$array_input,"",1);
1383					if ($ret == 'html') print '<br>';
1384				}
1385
1386				if ($action == 'brouillonner'):
1387				$ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("BrouillonnerTrip"),$langs->trans("ConfirmBrouillonnerTrip"),"confirm_brouillonner","","",1);
1388				if ($ret == 'html') print '<br>';
1389				endif;
1390
1391				if ($action == 'refuse')		// Deny
1392				{
1393					$array_input = array('text'=>$langs->trans("ConfirmRefuseTrip"), array('type'=>"text",'label'=>$langs->trans("Comment"),'name'=>"detail_refuse",'size'=>"50",'value'=>""));
1394					$ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("Deny"),'',"confirm_refuse",$array_input,"yes",1);
1395					if ($ret == 'html') print '<br>';
1396				}
1397
1398				if ($action == 'delete_line')
1399				{
1400					$ret=$form->form_confirm($_SERVER["PHP_SELF"]."?id=".$id."&rowid=".GETPOST('rowid'),$langs->trans("DeleteLine"),$langs->trans("ConfirmDeleteLine"),"confirm_delete_line",'','yes',1);
1401					if ($ret == 'html') print '<br>';
1402				}
1403
1404				print '<table class="border" width="100%">';
1405
1406				$linkback = '<a href="'.DOL_URL_ROOT.'/expensereport/list.php'.(! empty($socid)?'?socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
1407
1408            	// Ref
1409            	print '<tr><td width="25%">'.$langs->trans("Ref").'</td><td colspan="2">';
1410            	print $form->showrefnav($object, 'ref', $linkback, 1, 'ref', 'ref', '');
1411            	print '</td></tr>';
1412
1413				print '<tr>';
1414				print '<td>'.$langs->trans("Period").'</td>';
1415				print '<td colspan="2">';
1416				print get_date_range($object->date_debut,$object->date_fin,'',$langs,0);
1417				print '</td>';
1418				print '</tr>';
1419				if (! empty($conf->global->EXPENSEREPORT_ASK_PAYMENTMODE_ON_CREATION))
1420				{
1421					print '<tr>';
1422					print '<td>'.$langs->trans("ModePaiement").'</td>';
1423					print '<td colspan="2">'.$object->libelle_paiement.'</td>';
1424					print '</tr>';
1425				}
1426				// Status
1427				print '<tr>';
1428				print '<td>'.$langs->trans("Statut").'</td>';
1429				print '<td colspan="2">'.$object->getLibStatut(4).'</td>';
1430				print '</tr>';
1431				print '<tr>';
1432				print '<td>'.$langs->trans("NotePublic").'</td>';
1433				print '<td colspan="2">'.$object->note_public.'</td>';
1434				print '</tr>';
1435				print '<tr>';
1436				print '<td>'.$langs->trans("NotePrivate").'</td>';
1437				print '<td colspan="2">'.$object->note_private.'</td>';
1438				print '</tr>';
1439				print '<tr>';
1440				print '<td>'.$langs->trans("AmountHT").'</td>';
1441				print '<td>'.price($object->total_ht).'</td>';
1442				print '<td rowspan="7" valign="top">';
1443				/*
1444				 * Payments
1445				 */
1446				$sql = "SELECT p.rowid, p.num_payment, p.datep as dp, p.amount,";
1447				$sql.= "c.code as type_code,c.libelle as payment_type";
1448				$sql.= " FROM ".MAIN_DB_PREFIX."payment_expensereport as p";
1449				$sql.= ", ".MAIN_DB_PREFIX."c_paiement as c ";
1450				$sql.= ", ".MAIN_DB_PREFIX."expensereport as e";
1451				$sql.= " WHERE e.rowid = '".$id."'";
1452				$sql.= " AND p.fk_expensereport = e.rowid";
1453				$sql.= " AND e.entity = ".$conf->entity;
1454				$sql.= " AND p.fk_typepayment = c.id";
1455				$sql.= " ORDER BY dp";
1456
1457				//print $sql;
1458				$resql = $db->query($sql);
1459				if ($resql)
1460				{
1461					$num = $db->num_rows($resql);
1462					$i = 0; $total = 0;
1463					print '<table class="nobordernopadding" width="100%">';
1464					print '<tr class="liste_titre">';
1465					print '<td>'.$langs->trans("RefPayment").'</td>';
1466					print '<td>'.$langs->trans("Date").'</td>';
1467					print '<td>'.$langs->trans("Type").'</td>';
1468					print '<td align="right">'.$langs->trans("Amount").'</td>';
1469					print '<td>&nbsp;</td>';
1470					print '</tr>';
1471
1472					$var=True;
1473					while ($i < $num)
1474					{
1475						$objp = $db->fetch_object($resql);
1476						$var=!$var;
1477						print "<tr ".$bc[$var]."><td>";
1478						print '<a href="'.DOL_URL_ROOT.'/expensereport/payment/card.php?id='.$objp->rowid.'">'.img_object($langs->trans("Payment"),"payment").' '.$objp->rowid.'</a></td>';
1479						print '<td>'.dol_print_date($db->jdate($objp->dp),'day')."</td>\n";
1480							$labeltype=$langs->trans("PaymentType".$object->type_code)!=("PaymentType".$object->type_code)?$langs->trans("PaymentType".$object->type_code):$object->fk_typepayment;
1481						print "<td>".$labeltype.' '.$object->num_payment."</td>\n";
1482						print '<td align="right">'.price($objp->amount)."</td><td>&nbsp;".$langs->trans("Currency".$conf->currency)."</td>\n";
1483						print "</tr>";
1484						$totalpaid += $objp->amount;
1485						$i++;
1486					}
1487
1488					if ($object->paid == 0)
1489					{
1490						print "<tr><td colspan=\"2\" align=\"right\">".$langs->trans("AlreadyPaid")." :</td><td align=\"right\"><b>".price($totalpaid)."</b></td><td>&nbsp;".$langs->trans("Currency".$conf->currency)."</td></tr>\n";
1491						print "<tr><td colspan=\"2\" align=\"right\">".$langs->trans("AmountExpected")." :</td><td align=\"right\" bgcolor=\"#d0d0d0\">".price($object->total_ttc)."</td><td bgcolor=\"#d0d0d0\">&nbsp;".$langs->trans("Currency".$conf->currency)."</td></tr>\n";
1492
1493						$remaintopay = $object->total_ttc - $totalpaid;
1494
1495						print "<tr><td colspan=\"2\" align=\"right\">".$langs->trans("RemainderToPay")." :</td>";
1496						print "<td align=\"right\" bgcolor=\"#f0f0f0\"><b>".price($remaintopay)."</b></td><td bgcolor=\"#f0f0f0\">&nbsp;".$langs->trans("Currency".$conf->currency)."</td></tr>\n";
1497					}
1498					print "</table>";
1499					$db->free($resql);
1500				}
1501				else
1502				{
1503					dol_print_error($db);
1504				}
1505				print "</td>";
1506	
1507				print '</tr>';
1508				print '<tr>';
1509				print '<td>'.$langs->trans("AmountVAT").'</td>';
1510				print '<td>'.price($object->total_tva).'</td>';
1511				print '</tr>';
1512				print '<tr>';
1513				print '<td>'.$langs->trans("AmountTTC").'</td>';
1514				print '<td>'.price($object->total_ttc).'</td>';
1515				print '</tr>';
1516
1517				// Author
1518				print '<tr>';
1519				print '<td>'.$langs->trans("AUTHOR").'</td>';
1520				print '<td>';
1521				if ($object->fk_user_author > 0)
1522				{
1523					$userauthor=new User($db);
1524					$result=$userauthor->fetch($object->fk_user_author);
1525					if ($result < 0) dol_print_error('',$userauthor->error);
1526					print $userauthor->getNomUrl(1);
1527				}
1528				print '</td></tr>';
1529
1530				print '<tr>';
1531				print '<td>'.$langs->trans("DATE_SAVE").'</td>';
1532				print '<td>'.dol_print_date($object->date_create,'dayhour').'</td></tr>';
1533				print '</tr>';
1534				if($object->fk_statut==6)
1535				{
1536					print '<tr>';
1537					print '<td>'.$langs->trans("AUTHORPAIEMENT").'</td>';
1538					print '<td>';
1539					$userfee=new User($db);
1540					$userfee->fetch($object->fk_user_paid);
1541					print $userfee->getNomUrl(1);
1542					print '</td></tr>';
1543					print '<tr>';
1544					print '<td>'.$langs->trans("DATE_PAIEMENT").'</td>';
1545					print '<td>'.$object->date_paiement.'</td></tr>';
1546					print '</tr>';
1547				}
1548
1549				// User to inform
1550				if($object->fk_statut<3)	// informed
1551				{
1552					print '<tr>';
1553					print '<td>'.$langs->trans("VALIDATOR").'</td>';
1554					print '<td>';
1555					if ($object->fk_user_validator > 0)
1556					{
1557						$userfee=new User($db);
1558						$userfee->fetch($object->fk_user_validator);
1559						print $userfee->getNomUrl(1);
1560						if (empty($userfee->email) || ! isValidEmail($userfee->email)) print img_warning($langs->trans("EmailNotValid"));
1561					}
1562					print '</td></tr>';
1563				}
1564				elseif($object->fk_statut==4)
1565				{
1566					print '<tr>';
1567					print '<td>'.$langs->trans("CANCEL_USER").'</span></td>';
1568					print '<td>';
1569					if ($object->fk_user_cancel > 0)
1570					{
1571						$userfee=new User($db);
1572						$userfee->fetch($object->fk_user_cancel);
1573						print $userfee->getNomUrl(1);
1574					}
1575					print '</td></tr>';
1576					print '<tr>';
1577					print '<td>'.$langs->trans("MOTIF_CANCEL").'</td>';
1578					print '<td>'.$object->detail_cancel.'</td></tr>';
1579					print '</tr>';
1580					print '<tr>';
1581					print '<td>'.$langs->trans("DATE_CANCEL").'</td>';
1582					print '<td>'.dol_print_date($object->date_cancel,'dayhour').'</td></tr>';
1583					print '</tr>';
1584				}
1585				else
1586				{
1587					print '<tr>';
1588					print '<td>'.$langs->trans("Approbator").'</td>';
1589					print '<td>';
1590					if ($object->fk_user_approve > 0)
1591					{
1592						$userapp=new User($db);
1593						$userapp->fetch($object->fk_user_approve);
1594						print $userapp->getNomUrl(1);
1595					}
1596					print '</td></tr>';
1597					print '<tr>';
1598					print '<td>'.$langs->trans("DateApprove").'</td>';
1599					print '<td>'.dol_print_date($object->date_approve,'dayhour').'</td></tr>';
1600					print '</tr>';
1601				}
1602
1603				if($object->fk_statut==99 || !empty($object->detail_refuse))
1604				{
1605					print '<tr>';
1606					print '<td>'.$langs->trans("REFUSEUR").'</td>';
1607					print '<td>';
1608					$userfee=new User($db);
1609					$userfee->fetch($object->fk_user_refuse);
1610					print $userfee->getNomUrl(1);
1611					print '</td></tr>';
1612					print '<tr>';
1613					print '<td>'.$langs->trans("DATE_REFUS").'</td>';
1614					print '<td>'.dol_print_date($object->date_refuse,'dayhour');
1615					if ($object->detail_refuse) print ' - '.$object->detail_refuse;
1616					print '</td>';
1617					print '</tr>';
1618				}
1619				print '</table>';
1620
1621				print '<br>';
1622
1623				// Fetch Lines of current expense …

Large files files are truncated, but you can click here to view the full file