card.php 67 KB

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