card.php 64 KB

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