card.php 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488
  1. <?php
  2. /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2015-2017 Alexandre Spangaro <aspangaro@zendsi.com>
  6. * Copyright (C) 2017 Ferran Marcet <fmarcet@2byte.es>
  7. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/expensereport/card.php
  24. * \ingroup expensereport
  25. * \brief Page for trip and expense report card
  26. */
  27. $res=0;
  28. require '../main.inc.php';
  29. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formfile.class.php';
  30. require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
  31. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formmail.class.php';
  32. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formprojet.class.php';
  33. require_once DOL_DOCUMENT_ROOT . '/projet/class/project.class.php';
  34. require_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php';
  35. require_once DOL_DOCUMENT_ROOT . '/core/lib/expensereport.lib.php';
  36. require_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php';
  37. require_once DOL_DOCUMENT_ROOT . '/core/modules/expensereport/modules_expensereport.php';
  38. require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
  39. require_once DOL_DOCUMENT_ROOT . '/expensereport/class/paymentexpensereport.class.php';
  40. require_once DOL_DOCUMENT_ROOT . '/core/class/doleditor.class.php';
  41. require_once DOL_DOCUMENT_ROOT . '/core/class/extrafields.class.php';
  42. if (! empty($conf->accounting->enabled)) {
  43. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php';
  44. }
  45. // Load translation files required by the page
  46. $langs->loadLangs(array("trips","bills","mails"));
  47. $action=GETPOST('action','aZ09');
  48. $cancel=GETPOST('cancel','alpha');
  49. $confirm = GETPOST('confirm', 'alpha');
  50. $date_start = dol_mktime(0, 0, 0, GETPOST('date_debutmonth','int'), GETPOST('date_debutday','int'), GETPOST('date_debutyear','int'));
  51. $date_end = dol_mktime(0, 0, 0, GETPOST('date_finmonth','int'), GETPOST('date_finday','int'), GETPOST('date_finyear','int'));
  52. $date = dol_mktime(0, 0, 0, GETPOST('datemonth','int'), GETPOST('dateday','int'), GETPOST('dateyear','int'));
  53. $fk_projet=GETPOST('fk_projet','int');
  54. $vatrate=GETPOST('vatrate','alpha');
  55. $ref=GETPOST("ref",'alpha');
  56. $comments=GETPOST('comments','none');
  57. $fk_c_type_fees=GETPOST('fk_c_type_fees','int');
  58. $socid = GETPOST('socid','int')?GETPOST('socid','int'):GETPOST('socid_id','int');
  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. $object=new ExpenseReport($db);
  81. $extrafields = new ExtraFields($db);
  82. // fetch optionals attributes and labels
  83. $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
  84. // Load object
  85. include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once
  86. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  87. $hookmanager->initHooks(array('expensereportcard','globalcard'));
  88. $permissionnote = $user->rights->expensereport->creer; // Used by the include of actions_setnotes.inc.php
  89. $permissiondellink = $user->rights->expensereport->creer; // Used by the include of actions_dellink.inc.php
  90. $permissionedit = $user->rights->expensereport->creer; // Used by the include of actions_lineupdown.inc.php
  91. /*
  92. * Actions
  93. */
  94. $parameters = array('socid' => $socid);
  95. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  96. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  97. if (empty($reshook))
  98. {
  99. if ($cancel)
  100. {
  101. if (! empty($backtopage))
  102. {
  103. header("Location: ".$backtopage);
  104. exit;
  105. }
  106. $action='';
  107. $fk_projet='';
  108. $date_start='';
  109. $date_end='';
  110. $date='';
  111. $comments='';
  112. $vatrate='';
  113. $value_unit='';
  114. $qty=1;
  115. $fk_c_type_fees=-1;
  116. }
  117. include DOL_DOCUMENT_ROOT.'/core/actions_setnotes.inc.php'; // Must be include, not include_once
  118. include DOL_DOCUMENT_ROOT.'/core/actions_dellink.inc.php'; // Must be include, not include_once
  119. include DOL_DOCUMENT_ROOT.'/core/actions_lineupdown.inc.php'; // Must be include, not include_once
  120. // Action clone object
  121. if ($action == 'confirm_clone' && $confirm == 'yes' && $user->rights->expensereport->creer)
  122. {
  123. if (1==0 && ! GETPOST('clone_content','alpha') && ! GETPOST('clone_receivers','alpha'))
  124. {
  125. setEventMessages($langs->trans("NoCloneOptionsSpecified"), null, 'errors');
  126. }
  127. else
  128. {
  129. if ($object->id > 0)
  130. {
  131. // Because createFromClone modifies the object, we must clone it so that we can restore it later if it fails
  132. $orig = clone $object;
  133. $result=$object->createFromClone(GETPOST('fk_user_author','int'));
  134. if ($result > 0)
  135. {
  136. header("Location: ".$_SERVER['PHP_SELF'].'?id='.$result);
  137. exit;
  138. }
  139. else
  140. {
  141. setEventMessages($object->error, $object->errors, 'errors');
  142. $object = $orig;
  143. $action='';
  144. }
  145. }
  146. }
  147. }
  148. if ($action == 'confirm_delete' && GETPOST("confirm",'alpha') == "yes" && $id > 0 && $user->rights->expensereport->supprimer)
  149. {
  150. $object = new ExpenseReport($db);
  151. $result = $object->fetch($id);
  152. $result = $object->delete($user);
  153. if ($result >= 0)
  154. {
  155. header("Location: index.php");
  156. exit;
  157. }
  158. else
  159. {
  160. setEventMessages($object->error, $object->errors, 'errors');
  161. }
  162. }
  163. if ($action == 'add' && $user->rights->expensereport->creer)
  164. {
  165. $object = new ExpenseReport($db);
  166. $object->date_debut = $date_start;
  167. $object->date_fin = $date_end;
  168. $object->fk_user_author = GETPOST('fk_user_author','int');
  169. if (! ($object->fk_user_author > 0)) $object->fk_user_author = $user->id;
  170. $fuser=new User($db);
  171. $fuser->fetch($object->fk_user_author);
  172. $object->fk_statut = 1;
  173. $object->fk_c_paiement = GETPOST('fk_c_paiement','int');
  174. $object->fk_user_validator = GETPOST('fk_user_validator','int');
  175. $object->note_public = GETPOST('note_public','none');
  176. $object->note_private = GETPOST('note_private','none');
  177. // Fill array 'array_options' with data from add form
  178. if (! $error)
  179. {
  180. $ret = $extrafields->setOptionalsFromPost($extralabels, $object);
  181. if ($ret < 0) $error++;
  182. }
  183. if (empty($conf->global->EXPENSEREPORT_ALLOW_OVERLAPPING_PERIODS) && $object->periode_existe($fuser,$object->date_debut,$object->date_fin))
  184. {
  185. $error++;
  186. setEventMessages($langs->trans("ErrorDoubleDeclaration"), null, 'errors');
  187. $action='create';
  188. }
  189. if (! $error)
  190. {
  191. $db->begin();
  192. $id = $object->create($user);
  193. if ($id <= 0)
  194. {
  195. $error++;
  196. }
  197. if (! $error)
  198. {
  199. $db->commit();
  200. Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  201. exit;
  202. }
  203. else
  204. {
  205. setEventMessages($object->error, $object->errors, 'errors');
  206. $db->rollback();
  207. $action='create';
  208. }
  209. }
  210. }
  211. if ($action == 'update' && $user->rights->expensereport->creer)
  212. {
  213. $object = new ExpenseReport($db);
  214. $object->fetch($id);
  215. $object->date_debut = $date_start;
  216. $object->date_fin = $date_end;
  217. if($object->fk_statut < 3)
  218. {
  219. $object->fk_user_validator = GETPOST('fk_user_validator','int');
  220. }
  221. $object->fk_c_paiement = GETPOST('fk_c_paiement','int');
  222. $object->note_public = GETPOST('note_public','none');
  223. $object->note_private = GETPOST('note_private','none');
  224. $object->fk_user_modif = $user->id;
  225. $result = $object->update($user);
  226. if ($result > 0)
  227. {
  228. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_POST['id']);
  229. exit;
  230. }
  231. else
  232. {
  233. setEventMessages($object->error, $object->errors, 'errors');
  234. }
  235. }
  236. if ($action == 'update_extras')
  237. {
  238. $object->oldcopy = dol_clone($object);
  239. // Fill array 'array_options' with data from update form
  240. $extralabels = $extrafields->fetch_name_optionals_label($object->table_element);
  241. $ret = $extrafields->setOptionalsFromPost($extralabels, $object, GETPOST('attribute', 'none'));
  242. if ($ret < 0) $error++;
  243. if (! $error)
  244. {
  245. // Actions on extra fields
  246. $result = $object->insertExtraFields('EXPENSEREPORT_MODIFY');
  247. if ($result < 0)
  248. {
  249. setEventMessages($object->error, $object->errors, 'errors');
  250. $error++;
  251. }
  252. }
  253. if ($error)
  254. $action = 'edit_extras';
  255. }
  256. if ($action == "confirm_validate" && GETPOST("confirm", 'alpha') == "yes" && $id > 0 && $user->rights->expensereport->creer)
  257. {
  258. $error = 0;
  259. $db->begin();
  260. $object = new ExpenseReport($db);
  261. $object->fetch($id);
  262. $result = $object->setValidate($user);
  263. if ($result >= 0)
  264. {
  265. // Define output language
  266. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  267. {
  268. $outputlangs = $langs;
  269. $newlang = '';
  270. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  271. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  272. if (! empty($newlang)) {
  273. $outputlangs = new Translate("", $conf);
  274. $outputlangs->setDefaultLang($newlang);
  275. }
  276. $model=$object->modelpdf;
  277. $ret = $object->fetch($id); // Reload to get new records
  278. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  279. }
  280. }
  281. else
  282. {
  283. setEventMessages($object->error, $object->errors, 'errors');
  284. $error++;
  285. }
  286. if (! $error && $result > 0 && $object->fk_user_validator > 0)
  287. {
  288. $langs->load("mails");
  289. // TO
  290. $destinataire = new User($db);
  291. $destinataire->fetch($object->fk_user_validator);
  292. $emailTo = $destinataire->email;
  293. // FROM
  294. $expediteur = new User($db);
  295. $expediteur->fetch($object->fk_user_author);
  296. $emailFrom = $expediteur->email;
  297. if ($emailTo && $emailFrom)
  298. {
  299. $filename=array(); $filedir=array(); $mimetype=array();
  300. // SUBJECT
  301. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  302. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  303. $subject = $societeName." - ".$langs->transnoentities("ExpenseReportWaitingForApproval");
  304. // CONTENT
  305. $link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
  306. $message = $langs->transnoentities("ExpenseReportWaitingForApprovalMessage", $expediteur->getFullName($langs), get_date_range($object->date_debut,$object->date_fin,'',$langs), $link);
  307. // Rebuild pdf
  308. /*
  309. $object->setDocModel($user,"");
  310. $resultPDF = expensereport_pdf_create($db,$id,'',"",$langs);
  311. if($resultPDF):
  312. // ATTACHMENT
  313. array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
  314. array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref).".pdf");
  315. array_push($mimetype,"application/pdf");
  316. */
  317. // PREPARE SEND
  318. $mailfile = new CMailFile($subject, $emailTo, $emailFrom, $message, $filedir, $mimetype, $filename, '', '', 0, -1);
  319. if ($mailfile)
  320. {
  321. // SEND
  322. $result=$mailfile->sendfile();
  323. if ($result)
  324. {
  325. $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
  326. setEventMessages($mesg, null, 'mesgs');
  327. }
  328. else
  329. {
  330. $langs->load("other");
  331. if ($mailfile->error)
  332. {
  333. $mesg='';
  334. $mesg.=$langs->trans('ErrorFailedToSendMail', $emailFrom, $emailTo);
  335. $mesg.='<br>'.$mailfile->error;
  336. setEventMessages($mesg, null, 'errors');
  337. }
  338. else
  339. {
  340. setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'warnings');
  341. }
  342. }
  343. }
  344. else
  345. {
  346. setEventMessages($mailfile->error,$mailfile->errors,'errors');
  347. $action='';
  348. }
  349. }
  350. else
  351. {
  352. setEventMessages($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), null, 'warnings');
  353. $action='';
  354. }
  355. }
  356. if (! $error)
  357. {
  358. $db->commit();
  359. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  360. exit;
  361. }
  362. else
  363. {
  364. $db->rollback();
  365. }
  366. }
  367. if ($action == "confirm_save_from_refuse" && GETPOST("confirm", 'alpha') == "yes" && $id > 0 && $user->rights->expensereport->creer)
  368. {
  369. $object = new ExpenseReport($db);
  370. $object->fetch($id);
  371. $result = $object->set_save_from_refuse($user);
  372. if ($result > 0)
  373. {
  374. // Define output language
  375. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  376. {
  377. $outputlangs = $langs;
  378. $newlang = '';
  379. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  380. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  381. if (! empty($newlang)) {
  382. $outputlangs = new Translate("", $conf);
  383. $outputlangs->setDefaultLang($newlang);
  384. }
  385. $model=$object->modelpdf;
  386. $ret = $object->fetch($id); // Reload to get new records
  387. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  388. }
  389. }
  390. if ($result > 0)
  391. {
  392. // Send mail
  393. // TO
  394. $destinataire = new User($db);
  395. $destinataire->fetch($object->fk_user_validator);
  396. $emailTo = $destinataire->email;
  397. // FROM
  398. $expediteur = new User($db);
  399. $expediteur->fetch($object->fk_user_author);
  400. $emailFrom = $expediteur->email;
  401. if ($emailFrom && $emailTo)
  402. {
  403. $filename=array(); $filedir=array(); $mimetype=array();
  404. // SUBJECT
  405. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  406. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  407. $subject = $societeName." - ".$langs->transnoentities("ExpenseReportWaitingForReApproval");
  408. // CONTENT
  409. $link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
  410. $dateRefusEx = explode(" ",$object->date_refuse);
  411. $message = $langs->transnoentities("ExpenseReportWaitingForReApprovalMessage", $dateRefusEx[0], $object->detail_refuse, $expediteur->getFullName($langs), $link);
  412. // Rebuild pdf
  413. /*
  414. $object->setDocModel($user,"");
  415. $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
  416. if($resultPDF)
  417. {
  418. // ATTACHMENT
  419. $filename=array(); $filedir=array(); $mimetype=array();
  420. array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
  421. array_push($filedir,$conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref) . "/" . dol_sanitizeFileName($object->ref_number).".pdf");
  422. array_push($mimetype,"application/pdf");
  423. }
  424. */
  425. // PREPARE SEND
  426. $mailfile = new CMailFile($subject, $emailTo, $emailFrom, $message, $filedir, $mimetype, $filename, '', '', 0, -1);
  427. if ($mailfile)
  428. {
  429. // SEND
  430. $result=$mailfile->sendfile();
  431. if ($result)
  432. {
  433. $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
  434. setEventMessages($mesg, null, 'mesgs');
  435. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  436. exit;
  437. }
  438. else
  439. {
  440. $langs->load("other");
  441. if ($mailfile->error)
  442. {
  443. $mesg='';
  444. $mesg.=$langs->trans('ErrorFailedToSendMail', $emailFrom, $emailTo);
  445. $mesg.='<br>'.$mailfile->error;
  446. setEventMessages($mesg, null, 'errors');
  447. }
  448. else
  449. {
  450. setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'warnings');
  451. }
  452. }
  453. }
  454. else
  455. {
  456. setEventMessages($mailfile->error,$mailfile->errors,'errors');
  457. $action='';
  458. }
  459. }
  460. else
  461. {
  462. setEventMessages($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), null, 'warnings');
  463. $action='';
  464. }
  465. }
  466. else
  467. {
  468. setEventMessages($object->error, $object->errors, 'errors');
  469. }
  470. }
  471. // Approve
  472. if ($action == "confirm_approve" && GETPOST("confirm", 'alpha') == "yes" && $id > 0 && $user->rights->expensereport->approve)
  473. {
  474. $object = new ExpenseReport($db);
  475. $object->fetch($id);
  476. $result = $object->setApproved($user);
  477. if ($result > 0)
  478. {
  479. // Define output language
  480. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  481. {
  482. $outputlangs = $langs;
  483. $newlang = '';
  484. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  485. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  486. if (! empty($newlang)) {
  487. $outputlangs = new Translate("", $conf);
  488. $outputlangs->setDefaultLang($newlang);
  489. }
  490. $model=$object->modelpdf;
  491. $ret = $object->fetch($id); // Reload to get new records
  492. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  493. }
  494. }
  495. if ($result > 0)
  496. {
  497. // Send mail
  498. // TO
  499. $destinataire = new User($db);
  500. $destinataire->fetch($object->fk_user_author);
  501. $emailTo = $destinataire->email;
  502. // CC
  503. $emailCC = $conf->global->NDF_CC_EMAILS;
  504. if (empty($emailTo)) $emailTo=$emailCC;
  505. // FROM
  506. $expediteur = new User($db);
  507. $expediteur->fetch($object->fk_user_approve > 0 ? $object->fk_user_approve : $object->fk_user_validator);
  508. $emailFrom = $expediteur->email;
  509. if ($emailFrom && $emailTo)
  510. {
  511. $filename=array(); $filedir=array(); $mimetype=array();
  512. // SUBJECT
  513. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  514. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  515. $subject = $societeName." - ".$langs->transnoentities("ExpenseReportApproved");
  516. // CONTENT
  517. $link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
  518. $message = $langs->transnoentities("ExpenseReportApprovedMessage", $object->ref, $destinataire->getFullName($langs), $expediteur->getFullName($langs), $link);
  519. // Rebuilt pdf
  520. /*
  521. $object->setDocModel($user,"");
  522. $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
  523. if($resultPDF
  524. {
  525. // ATTACHMENT
  526. $filename=array(); $filedir=array(); $mimetype=array();
  527. array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
  528. array_push($filedir, $conf->expensereport->dir_output."/".dol_sanitizeFileName($object->ref)."/".dol_sanitizeFileName($object->ref).".pdf");
  529. array_push($mimetype,"application/pdf");
  530. }
  531. */
  532. $mailfile = new CMailFile($subject, $emailTo, $emailFrom, $message, $filedir, $mimetype, $filename, '', '', 0, -1);
  533. if ($mailfile)
  534. {
  535. // SEND
  536. $result=$mailfile->sendfile();
  537. if ($result)
  538. {
  539. $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
  540. setEventMessages($mesg, null, 'mesgs');
  541. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  542. exit;
  543. }
  544. else
  545. {
  546. $langs->load("other");
  547. if ($mailfile->error)
  548. {
  549. $mesg='';
  550. $mesg.=$langs->trans('ErrorFailedToSendMail', $emailFrom, $emailTo);
  551. $mesg.='<br>'.$mailfile->error;
  552. setEventMessages($mesg, null, 'errors');
  553. }
  554. else
  555. {
  556. setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'warnings');
  557. }
  558. }
  559. }
  560. else
  561. {
  562. setEventMessages($mailfile->error,$mailfile->errors,'errors');
  563. $action='';
  564. }
  565. }
  566. else
  567. {
  568. setEventMessages($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), null, 'warnings');
  569. $action='';
  570. }
  571. }
  572. else
  573. {
  574. setEventMessages($langs->trans("FailedtoSetToApprove"), null, 'warnings');
  575. $action='';
  576. }
  577. }
  578. else
  579. {
  580. setEventMessages($object->error, $object->errors, 'errors');
  581. }
  582. if ($action == "confirm_refuse" && GETPOST('confirm', 'alpha')=="yes" && $id > 0 && $user->rights->expensereport->approve)
  583. {
  584. $object = new ExpenseReport($db);
  585. $object->fetch($id);
  586. $result = $object->setDeny($user, GETPOST('detail_refuse', 'alpha'));
  587. if ($result > 0)
  588. {
  589. // Define output language
  590. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  591. {
  592. $outputlangs = $langs;
  593. $newlang = '';
  594. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  595. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  596. if (! empty($newlang)) {
  597. $outputlangs = new Translate("", $conf);
  598. $outputlangs->setDefaultLang($newlang);
  599. }
  600. $model=$object->modelpdf;
  601. $ret = $object->fetch($id); // Reload to get new records
  602. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  603. }
  604. }
  605. if ($result > 0)
  606. {
  607. // Send mail
  608. // TO
  609. $destinataire = new User($db);
  610. $destinataire->fetch($object->fk_user_author);
  611. $emailTo = $destinataire->email;
  612. // FROM
  613. $expediteur = new User($db);
  614. $expediteur->fetch($object->fk_user_refuse);
  615. $emailFrom = $expediteur->email;
  616. if ($emailFrom && $emailTo)
  617. {
  618. $filename=array(); $filedir=array(); $mimetype=array();
  619. // SUBJECT
  620. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  621. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  622. $subject = $societeName." - ".$langs->transnoentities("ExpenseReportRefused");
  623. // CONTENT
  624. $link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
  625. $message = $langs->transnoentities("ExpenseReportRefusedMessage", $object->ref, $destinataire->getFullName($langs), $expediteur->getFullName($langs), $_POST['detail_refuse'], $link);
  626. // Rebuilt pdf
  627. /*
  628. $object->setDocModel($user,"");
  629. $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
  630. if($resultPDF
  631. {
  632. // ATTACHMENT
  633. $filename=array(); $filedir=array(); $mimetype=array();
  634. array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
  635. array_push($filedir, $conf->expensereport->dir_output."/".dol_sanitizeFileName($object->ref)."/".dol_sanitizeFileName($object->ref).".pdf");
  636. array_push($mimetype,"application/pdf");
  637. }
  638. */
  639. // PREPARE SEND
  640. $mailfile = new CMailFile($subject, $emailTo, $emailFrom, $message, $filedir, $mimetype, $filename, '', '', 0, -1);
  641. if ($mailfile)
  642. {
  643. // SEND
  644. $result=$mailfile->sendfile();
  645. if ($result)
  646. {
  647. $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
  648. setEventMessages($mesg, null, 'mesgs');
  649. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  650. exit;
  651. }
  652. else
  653. {
  654. $langs->load("other");
  655. if ($mailfile->error)
  656. {
  657. $mesg='';
  658. $mesg.=$langs->trans('ErrorFailedToSendMail', $emailFrom, $emailTo);
  659. $mesg.='<br>'.$mailfile->error;
  660. setEventMessages($mesg, null, 'errors');
  661. }
  662. else
  663. {
  664. setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'warnings');
  665. }
  666. }
  667. }
  668. else
  669. {
  670. setEventMessages($mailfile->error,$mailfile->errors,'errors');
  671. $action='';
  672. }
  673. }
  674. else
  675. {
  676. setEventMessages($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), null, 'warnings');
  677. $action='';
  678. }
  679. }
  680. else
  681. {
  682. setEventMessages($langs->trans("FailedtoSetToDeny"), null, 'warnings');
  683. $action='';
  684. }
  685. }
  686. else
  687. {
  688. setEventMessages($object->error, $object->errors, 'errors');
  689. }
  690. //var_dump($user->id == $object->fk_user_validator);exit;
  691. if ($action == "confirm_cancel" && GETPOST('confirm', 'alpha')=="yes" && $id > 0 && $user->rights->expensereport->creer)
  692. {
  693. if (! GETPOST('detail_cancel', 'alpha'))
  694. {
  695. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Comment")), null, 'errors');
  696. }
  697. else
  698. {
  699. $object = new ExpenseReport($db);
  700. $object->fetch($id);
  701. if ($user->id == $object->fk_user_valid || $user->id == $object->fk_user_author)
  702. {
  703. $result = $object->set_cancel($user, GETPOST('detail_cancel', 'alpha'));
  704. if ($result > 0)
  705. {
  706. // Define output language
  707. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  708. {
  709. $outputlangs = $langs;
  710. $newlang = '';
  711. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  712. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  713. if (! empty($newlang)) {
  714. $outputlangs = new Translate("", $conf);
  715. $outputlangs->setDefaultLang($newlang);
  716. }
  717. $model=$object->modelpdf;
  718. $ret = $object->fetch($id); // Reload to get new records
  719. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  720. }
  721. }
  722. if ($result > 0)
  723. {
  724. // Send mail
  725. // TO
  726. $destinataire = new User($db);
  727. $destinataire->fetch($object->fk_user_author);
  728. $emailTo = $destinataire->email;
  729. // FROM
  730. $expediteur = new User($db);
  731. $expediteur->fetch($object->fk_user_cancel);
  732. $emailFrom = $expediteur->email;
  733. if ($emailFrom && $emailTo)
  734. {
  735. $filename=array(); $filedir=array(); $mimetype=array();
  736. // SUBJECT
  737. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  738. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  739. $subject = $societeName." - ".$langs->transnoentities("ExpenseReportCanceled");
  740. // CONTENT
  741. $link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
  742. $message = $langs->transnoentities("ExpenseReportCanceledMessage", $object->ref, $destinataire->getFullName($langs), $expediteur->getFullName($langs), GETPOST('detail_cancel','alpha'), $link);
  743. // Rebuilt pdf
  744. /*
  745. $object->setDocModel($user,"");
  746. $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
  747. if($resultPDF
  748. {
  749. // ATTACHMENT
  750. $filename=array(); $filedir=array(); $mimetype=array();
  751. array_push($filename,dol_sanitizeFileName($object->ref).".pdf");
  752. array_push($filedir, $conf->expensereport->dir_output."/".dol_sanitizeFileName($object->ref)."/".dol_sanitizeFileName($object->ref).".pdf");
  753. array_push($mimetype,"application/pdf");
  754. }
  755. */
  756. // PREPARE SEND
  757. $mailfile = new CMailFile($subject, $emailTo, $emailFrom, $message, $filedir, $mimetype, $filename, '', '', 0, -1);
  758. if ($mailfile)
  759. {
  760. // SEND
  761. $result=$mailfile->sendfile();
  762. if ($result)
  763. {
  764. $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
  765. setEventMessages($mesg, null, 'mesgs');
  766. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  767. exit;
  768. }
  769. else
  770. {
  771. $langs->load("other");
  772. if ($mailfile->error)
  773. {
  774. $mesg='';
  775. $mesg.=$langs->trans('ErrorFailedToSendMail', $emailFrom, $emailTo);
  776. $mesg.='<br>'.$mailfile->error;
  777. setEventMessages($mesg, null, 'errors');
  778. }
  779. else
  780. {
  781. setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'warnings');
  782. }
  783. }
  784. }
  785. else
  786. {
  787. setEventMessages($mailfile->error,$mailfile->errors,'errors');
  788. $action='';
  789. }
  790. }
  791. else
  792. {
  793. setEventMessages($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), null, 'warnings');
  794. $action='';
  795. }
  796. }
  797. else
  798. {
  799. setEventMessages($langs->trans("FailedToSetToCancel"), null, 'warnings');
  800. $action='';
  801. }
  802. }
  803. else
  804. {
  805. setEventMessages($object->error, $object->errors, 'errors');
  806. }
  807. }
  808. }
  809. if ($action == "confirm_brouillonner" && GETPOST('confirm', 'alpha')=="yes" && $id > 0 && $user->rights->expensereport->creer)
  810. {
  811. $object = new ExpenseReport($db);
  812. $object->fetch($id);
  813. if ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid)
  814. {
  815. $result = $object->setStatut(0);
  816. if ($result > 0)
  817. {
  818. // Define output language
  819. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  820. {
  821. $outputlangs = $langs;
  822. $newlang = '';
  823. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  824. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  825. if (! empty($newlang)) {
  826. $outputlangs = new Translate("", $conf);
  827. $outputlangs->setDefaultLang($newlang);
  828. }
  829. $model=$object->modelpdf;
  830. $ret = $object->fetch($id); // Reload to get new records
  831. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  832. }
  833. }
  834. if ($result > 0)
  835. {
  836. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  837. exit;
  838. }
  839. else
  840. {
  841. setEventMessages($object->error, $object->errors, 'errors');
  842. }
  843. }
  844. else
  845. {
  846. setEventMessages("NOT_AUTHOR", '', 'errors');
  847. }
  848. }
  849. if ($action == 'set_paid' && $id > 0 && $user->rights->expensereport->to_paid)
  850. {
  851. $object = new ExpenseReport($db);
  852. $object->fetch($id);
  853. $result = $object->set_paid($id, $user);
  854. if ($result > 0)
  855. {
  856. // Define output language
  857. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  858. {
  859. $outputlangs = $langs;
  860. $newlang = '';
  861. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  862. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  863. if (! empty($newlang)) {
  864. $outputlangs = new Translate("", $conf);
  865. $outputlangs->setDefaultLang($newlang);
  866. }
  867. $model=$object->modelpdf;
  868. $ret = $object->fetch($id); // Reload to get new records
  869. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  870. }
  871. }
  872. if ($result > 0)
  873. {
  874. // Send mail
  875. // TO
  876. $destinataire = new User($db);
  877. $destinataire->fetch($object->fk_user_author);
  878. $emailTo = $destinataire->email;
  879. // FROM
  880. $expediteur = new User($db);
  881. $expediteur->fetch($user->id);
  882. $emailFrom = $expediteur->email;
  883. if ($emailFrom && $emailTo)
  884. {
  885. $filename=array(); $filedir=array(); $mimetype=array();
  886. // SUBJECT
  887. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  888. if (! empty($conf->global->MAIN_APPLICATION_TITLE)) $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  889. $subject = $societeName." - ".$langs->transnoentities("ExpenseReportPaid");
  890. // CONTENT
  891. $link = $urlwithroot.'/expensereport/card.php?id='.$object->id;
  892. $message = $langs->transnoentities("ExpenseReportPaidMessage", $object->ref, $destinataire->getFullName($langs), $expediteur->getFullName($langs), $link);
  893. // Generate pdf before attachment
  894. $object->setDocModel($user,"");
  895. $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs);
  896. // PREPARE SEND
  897. $mailfile = new CMailFile($subject, $emailTo, $emailFrom, $message, $filedir, $mimetype, $filename, '', '', 0, -1);
  898. if ($mailfile)
  899. {
  900. // SEND
  901. $result=$mailfile->sendfile();
  902. if ($result)
  903. {
  904. $mesg=$langs->trans('MailSuccessfulySent',$mailfile->getValidAddress($emailFrom,2),$mailfile->getValidAddress($emailTo,2));
  905. setEventMessages($mesg, null, 'mesgs');
  906. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  907. exit;
  908. }
  909. else
  910. {
  911. $langs->load("other");
  912. if ($mailfile->error)
  913. {
  914. $mesg='';
  915. $mesg.=$langs->trans('ErrorFailedToSendMail', $emailFrom, $emailTo);
  916. $mesg.='<br>'.$mailfile->error;
  917. setEventMessages($mesg, null, 'errors');
  918. }
  919. else
  920. {
  921. setEventMessages('No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS', null, 'warnings');
  922. }
  923. }
  924. }
  925. else
  926. {
  927. setEventMessages($mailfile->error,$mailfile->errors,'errors');
  928. $action='';
  929. }
  930. }
  931. else
  932. {
  933. setEventMessages($langs->trans("NoEmailSentBadSenderOrRecipientEmail"), null, 'warnings');
  934. $action='';
  935. }
  936. }
  937. else
  938. {
  939. setEventMessages($langs->trans("FailedToSetPaid"), null, 'warnings');
  940. $action='';
  941. }
  942. }
  943. else
  944. {
  945. setEventMessages($object->error, $object->errors, 'errors');
  946. }
  947. if ($action == "addline" && $user->rights->expensereport->creer)
  948. {
  949. $error = 0;
  950. // if VAT is not used in Dolibarr, set VAT rate to 0 because VAT rate is necessary.
  951. if (empty($vatrate)) $vatrate = "0.000";
  952. $vatrate = price2num($vatrate);
  953. $value_unit=price2num(GETPOST('value_unit', 'alpha'),'MU');
  954. $fk_c_exp_tax_cat = GETPOST('fk_c_exp_tax_cat', 'int');
  955. $qty = GETPOST('qty','int');
  956. if (empty($qty)) $qty=1;
  957. if (! $fk_c_type_fees > 0)
  958. {
  959. $error++;
  960. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
  961. $action='';
  962. }
  963. if ($vatrate < 0 || $vatrate == '')
  964. {
  965. $error++;
  966. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("VAT")), null, 'errors');
  967. $action='';
  968. }
  969. /* Projects are never required. To force them, check module forceproject
  970. if ($conf->projet->enabled)
  971. {
  972. if (empty($object_ligne->fk_projet) || $object_ligne->fk_projet==-1)
  973. {
  974. $error++;
  975. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Project")), null, 'errors');
  976. }
  977. }*/
  978. // Si aucune date n'est rentrée
  979. if (empty($date) || $date=="--")
  980. {
  981. $error++;
  982. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors');
  983. }
  984. // Si aucun prix n'est rentré
  985. if ($value_unit==0)
  986. {
  987. $error++;
  988. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PriceUTTC")), null, 'errors');
  989. }
  990. // Warning if date out of range
  991. if ($date < $object->date_debut || $date > ($object->date_fin + (24 * 3600 - 1)))
  992. {
  993. $langs->load("errors");
  994. setEventMessages($langs->trans("WarningDateOfLineMustBeInExpenseReportRange"), null, 'warnings');
  995. }
  996. // S'il y'a eu au moins une erreur
  997. if (! $error)
  998. {
  999. $type = 0; // TODO What if service ? We should take the type product/service from the type of expense report llx_c_type_fees
  1000. // Insert line
  1001. $result = $object->addline($qty,$value_unit,$fk_c_type_fees,$vatrate,$date,$comments,$fk_projet,$fk_c_exp_tax_cat,$type);
  1002. if ($result > 0) {
  1003. $ret = $object->fetch($object->id); // Reload to get new records
  1004. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  1005. // Define output language
  1006. $outputlangs = $langs;
  1007. $newlang = GETPOST('lang_id', 'alpha');
  1008. if (! empty($conf->global->MAIN_MULTILANGS) && empty($newlang))
  1009. $newlang = $object->thirdparty->default_lang;
  1010. if (! empty($newlang)) {
  1011. $outputlangs = new Translate("", $conf);
  1012. $outputlangs->setDefaultLang($newlang);
  1013. }
  1014. $object->generateDocument($object->modelpdf, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1015. }
  1016. unset($qty);
  1017. unset($value_unit);
  1018. unset($vatrate);
  1019. unset($comments);
  1020. unset($fk_c_type_fees);
  1021. unset($fk_projet);
  1022. unset($date);
  1023. } else {
  1024. setEventMessages($object->error, $object->errors, 'errors');
  1025. }
  1026. }
  1027. $action='';
  1028. }
  1029. if ($action == 'confirm_delete_line' && GETPOST("confirm", 'alpha') == "yes" && $user->rights->expensereport->creer)
  1030. {
  1031. $object = new ExpenseReport($db);
  1032. $object->fetch($id);
  1033. $object_ligne = new ExpenseReportLine($db);
  1034. $object_ligne->fetch(GETPOST("rowid", 'int'));
  1035. $total_ht = $object_ligne->total_ht;
  1036. $total_tva = $object_ligne->total_tva;
  1037. $result=$object->deleteline(GETPOST("rowid", 'int'), $user);
  1038. if ($result >= 0)
  1039. {
  1040. if ($result > 0)
  1041. {
  1042. // Define output language
  1043. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  1044. {
  1045. $outputlangs = $langs;
  1046. $newlang = '';
  1047. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  1048. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  1049. if (! empty($newlang)) {
  1050. $outputlangs = new Translate("", $conf);
  1051. $outputlangs->setDefaultLang($newlang);
  1052. }
  1053. $model=$object->modelpdf;
  1054. $ret = $object->fetch($id); // Reload to get new records
  1055. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1056. }
  1057. }
  1058. $object->update_totaux_del($object_ligne->total_ht,$object_ligne->total_tva);
  1059. header("Location: ".$_SERVER["PHP_SELF"]."?id=".$_GET['id']);
  1060. exit;
  1061. }
  1062. else
  1063. {
  1064. setEventMessages($object->error, $object->errors, 'errors');
  1065. }
  1066. }
  1067. if ($action == "updateline" && $user->rights->expensereport->creer)
  1068. {
  1069. $object = new ExpenseReport($db);
  1070. $object->fetch($id);
  1071. $rowid = $_POST['rowid'];
  1072. $type_fees_id = GETPOST('fk_c_type_fees', 'int');
  1073. $fk_c_exp_tax_cat = GETPOST('fk_c_exp_tax_cat', 'int');
  1074. $projet_id = $fk_projet;
  1075. $comments = GETPOST('comments', 'none');
  1076. $qty = GETPOST('qty', 'int');
  1077. $value_unit = price2num(GETPOST('value_unit', 'alpha'), 'MU');
  1078. $vatrate = GETPOST('vatrate', 'alpha');
  1079. // if VAT is not used in Dolibarr, set VAT rate to 0 because VAT rate is necessary.
  1080. if (empty($vatrate)) $vatrate = "0.000";
  1081. $vatrate = price2num($vatrate);
  1082. if (! GETPOST('fk_c_type_fees', 'int') > 0)
  1083. {
  1084. $error++;
  1085. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Type")), null, 'errors');
  1086. $action='';
  1087. }
  1088. if ((int) $vatrate < 0 || $vatrate == '')
  1089. {
  1090. $error++;
  1091. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Vat")), null, 'errors');
  1092. $action='';
  1093. }
  1094. // Warning if date out of range
  1095. if ($date < $object->date_debut || $date > ($object->date_fin + (24 * 3600 - 1)))
  1096. {
  1097. $langs->load("errors");
  1098. setEventMessages($langs->trans("WarningDateOfLineMustBeInExpenseReportRange"), null, 'warnings');
  1099. }
  1100. if (! $error)
  1101. {
  1102. // TODO Use update method of ExpenseReportLine
  1103. $result = $object->updateline($rowid, $type_fees_id, $projet_id, $vatrate, $comments, $qty, $value_unit, $date, $id, $fk_c_exp_tax_cat);
  1104. if ($result >= 0)
  1105. {
  1106. if ($result > 0)
  1107. {
  1108. // Define output language
  1109. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE))
  1110. {
  1111. $outputlangs = $langs;
  1112. $newlang = '';
  1113. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id','aZ09')) $newlang = GETPOST('lang_id','aZ09');
  1114. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang;
  1115. if (! empty($newlang)) {
  1116. $outputlangs = new Translate("", $conf);
  1117. $outputlangs->setDefaultLang($newlang);
  1118. }
  1119. $model=$object->modelpdf;
  1120. $ret = $object->fetch($id); // Reload to get new records
  1121. $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  1122. }
  1123. }
  1124. $result = $object->recalculer($id);
  1125. //header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id);
  1126. //exit;
  1127. }
  1128. else
  1129. {
  1130. setEventMessages($object->error, $object->errors, 'errors');
  1131. }
  1132. }
  1133. }
  1134. // Actions when printing a doc from card
  1135. include DOL_DOCUMENT_ROOT.'/core/actions_printing.inc.php';
  1136. // Actions to send emails
  1137. $trigger_name='EXPENSEREPORT_SENTBYMAIL';
  1138. $autocopy='MAIN_MAIL_AUTOCOPY_EXPENSEREPORT_TO';
  1139. $trackid='exp'.$object->id;
  1140. include DOL_DOCUMENT_ROOT.'/core/actions_sendmails.inc.php';
  1141. // Actions to build doc
  1142. $upload_dir = $conf->expensereport->dir_output;
  1143. $permissioncreate = $user->rights->expensereport->creer;
  1144. include DOL_DOCUMENT_ROOT.'/core/actions_builddoc.inc.php';
  1145. }
  1146. /*
  1147. * View
  1148. */
  1149. $title=$langs->trans("ExpenseReport") . " - " . $langs->trans("Card");
  1150. $helpurl="EN:Module_Expense_Reports";
  1151. llxHeader("",$title,$helpurl);
  1152. $form = new Form($db);
  1153. $formfile = new FormFile($db);
  1154. $formproject = new FormProjets($db);
  1155. $projecttmp = new Project($db);
  1156. $paymentexpensereportstatic=new PaymentExpenseReport($db);
  1157. $bankaccountstatic = new Account($db);
  1158. // Create
  1159. if ($action == 'create')
  1160. {
  1161. print load_fiche_titre($langs->trans("NewTrip"));
  1162. print '<form action="'.$_SERVER['PHP_SELF'].'" method="post" name="create">';
  1163. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1164. print '<input type="hidden" name="action" value="add">';
  1165. dol_fiche_head('');
  1166. print '<table class="border" width="100%">';
  1167. print '<tbody>';
  1168. // Date start
  1169. print '<tr>';
  1170. print '<td class="titlefieldcreate fieldrequired">'.$langs->trans("DateStart").'</td>';
  1171. print '<td>';
  1172. print $form->selectDate($date_start?$date_start:-1, 'date_debut', 0, 0, 0, '', 1, 1);
  1173. print '</td>';
  1174. print '</tr>';
  1175. // Date end
  1176. print '<tr>';
  1177. print '<td class="fieldrequired">'.$langs->trans("DateEnd").'</td>';
  1178. print '<td>';
  1179. print $form->selectDate($date_end?$date_end:-1, 'date_fin', 0, 0, 0, '', 1, 1);
  1180. print '</td>';
  1181. print '</tr>';
  1182. // User for expense report
  1183. print '<tr>';
  1184. print '<td class="fieldrequired">'.$langs->trans("User").'</td>';
  1185. print '<td>';
  1186. $defaultselectuser=$user->id;
  1187. if (GETPOST('fk_user_author', 'int') > 0) $defaultselectuser=GETPOST('fk_user_author', 'int');
  1188. $include_users = 'hierarchyme';
  1189. if (! empty($conf->global->MAIN_USE_ADVANCED_PERMS) && ! empty($user->rights->expensereport->writeall_advance)) $include_users=array();
  1190. $s=$form->select_dolusers($defaultselectuser, "fk_user_author", 0, "", 0, $include_users, '', '0,'.$conf->entity);
  1191. print $s;
  1192. print '</td>';
  1193. print '</tr>';
  1194. // Approver
  1195. print '<tr>';
  1196. print '<td>'.$langs->trans("VALIDATOR").'</td>';
  1197. print '<td>';
  1198. $object = new ExpenseReport($db);
  1199. $include_users = $object->fetch_users_approver_expensereport();
  1200. if (empty($include_users)) print img_warning().' '.$langs->trans("NobodyHasPermissionToValidateExpenseReport");
  1201. else
  1202. {
  1203. $defaultselectuser=$user->fk_user; // Will work only if supervisor has permission to approve so is inside include_users
  1204. if (! empty($conf->global->EXPENSEREPORT_DEFAULT_VALIDATOR)) $defaultselectuser=$conf->global->EXPENSEREPORT_DEFAULT_VALIDATOR; // Can force default approver
  1205. if (GETPOST('fk_user_validator', 'int') > 0) $defaultselectuser=GETPOST('fk_user_validator', 'int');
  1206. $s=$form->select_dolusers($defaultselectuser, "fk_user_validator", 1, "", ((empty($defaultselectuser) || empty($conf->global->EXPENSEREPORT_DEFAULT_VALIDATOR_UNCHANGEABLE))?0:1), $include_users);
  1207. print $form->textwithpicto($s, $langs->trans("AnyOtherInThisListCanValidate"));
  1208. }
  1209. print '</td>';
  1210. print '</tr>';
  1211. // Payment mode
  1212. if (! empty($conf->global->EXPENSEREPORT_ASK_PAYMENTMODE_ON_CREATION))
  1213. {
  1214. print '<tr>';
  1215. print '<td>'.$langs->trans("ModePaiement").'</td>';
  1216. print '<td>';
  1217. $form->select_types_paiements(2,'fk_c_paiement');
  1218. print '</td>';
  1219. print '</tr>';
  1220. }
  1221. // Public note
  1222. print '<tr>';
  1223. print '<td class="tdtop">' . $langs->trans('NotePublic') . '</td>';
  1224. print '<td>';
  1225. $doleditor = new DolEditor('note_public', $note_public, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, '90%');
  1226. print $doleditor->Create(1);
  1227. print '</td></tr>';
  1228. // Private note
  1229. if (empty($user->societe_id)) {
  1230. print '<tr>';
  1231. print '<td class="tdtop">' . $langs->trans('NotePrivate') . '</td>';
  1232. print '<td>';
  1233. $doleditor = new DolEditor('note_private', $note_private, '', 80, 'dolibarr_notes', 'In', 0, false, true, ROWS_3, '90%');
  1234. print $doleditor->Create(1);
  1235. print '</td></tr>';
  1236. }
  1237. // Other attributes
  1238. $parameters = array('colspan' => ' colspan="3"');
  1239. $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by
  1240. print $hookmanager->resPrint;
  1241. if (empty($reshook)) {
  1242. print $object->showOptionals($extrafields, 'edit');
  1243. }
  1244. print '<tbody>';
  1245. print '</table>';
  1246. dol_fiche_end();
  1247. print '<div class="center">';
  1248. print '<input type="submit" value="'.$langs->trans("AddTrip").'" name="bouton" class="button" />';
  1249. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="'.$langs->trans("Cancel").'" class="button" onclick="history.go(-1)" />';
  1250. print '</div>';
  1251. print '</form>';
  1252. }
  1253. else
  1254. {
  1255. if($id > 0 || $ref)
  1256. {
  1257. $result = $object->fetch($id, $ref);
  1258. $res = $object->fetch_optionals();
  1259. if ($result > 0)
  1260. {
  1261. if (! in_array($object->fk_user_author, $user->getAllChildIds(1)))
  1262. {
  1263. if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)
  1264. && (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || empty($user->rights->expensereport->writeall_advance)))
  1265. {
  1266. print load_fiche_titre($langs->trans('TripCard'));
  1267. print '<div class="tabBar">';
  1268. print $langs->trans('NotUserRightToView');
  1269. print '</div>';
  1270. // End of page
  1271. llxFooter();
  1272. $db->close();
  1273. exit;
  1274. }
  1275. }
  1276. $head = expensereport_prepare_head($object);
  1277. if ($action == 'edit' && ($object->fk_statut < 3 || $object->fk_statut==99))
  1278. {
  1279. print "<form name='update' action=\"".$_SERVER['PHP_SELF']."\" method=\"post\">\n";
  1280. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1281. print '<input type="hidden" name="id" value="'.$id.'">';
  1282. dol_fiche_head($head, 'card', $langs->trans("ExpenseReport"), 0, 'trip');
  1283. if($object->fk_statut==99)
  1284. {
  1285. print '<input type="hidden" name="action" value="updateFromRefuse">';
  1286. }
  1287. else
  1288. {
  1289. print '<input type="hidden" name="action" value="update">';
  1290. }
  1291. $linkback = '<a href="'.DOL_URL_ROOT.'/expensereport/list.php?restore_lastsearch_values=1'.(! empty($socid)?'&socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
  1292. print '<table class="border" style="width:100%;">';
  1293. print '<tr>';
  1294. print '<td>'.$langs->trans("User").'</td>';
  1295. print '<td>';
  1296. $userfee=new User($db);
  1297. if ($object->fk_user_author > 0)
  1298. {
  1299. $userfee->fetch($object->fk_user_author);
  1300. print $userfee->getNomUrl(-1);
  1301. }
  1302. print '</td></tr>';
  1303. // Ref
  1304. print '<tr><td class="titlefieldcreate">'.$langs->trans("Ref").'</td><td>';
  1305. print $form->showrefnav($object, 'ref', $linkback, 1, 'ref', 'ref', '');
  1306. print '</td></tr>';
  1307. print '<tr>';
  1308. print '<td>'.$langs->trans("DateStart").'</td>';
  1309. print '<td>';
  1310. print $form->selectDate($object->date_debut, 'date_debut');
  1311. print '</td>';
  1312. print '</tr>';
  1313. print '<tr>';
  1314. print '<td>'.$langs->trans("DateEnd").'</td>';
  1315. print '<td>';
  1316. print $form->selectDate($object->date_fin, 'date_fin');
  1317. print '</td>';
  1318. print '</tr>';
  1319. if (! empty($conf->global->EXPENSEREPORT_ASK_PAYMENTMODE_ON_CREATION))
  1320. {
  1321. print '<tr>';
  1322. print '<td>'.$langs->trans("ModePaiement").'</td>';
  1323. print '<td>';
  1324. $form->select_types_paiements($object->fk_c_paiement,'fk_c_paiement');
  1325. print '</td>';
  1326. print '</tr>';
  1327. }
  1328. if($object->fk_statut<3)
  1329. {
  1330. print '<tr>';
  1331. print '<td>'.$langs->trans("VALIDATOR").'</td>'; // Approbator
  1332. print '<td>';
  1333. $include_users = $object->fetch_users_approver_expensereport();
  1334. $s=$form->select_dolusers($object->fk_user_validator,"fk_user_validator",1,"",0,$include_users);
  1335. print $form->textwithpicto($s, $langs->trans("AnyOtherInThisListCanValidate"));
  1336. print '</td>';
  1337. print '</tr>';
  1338. }
  1339. else
  1340. {
  1341. print '<tr>';
  1342. print '<td>'.$langs->trans("VALIDOR").'</td>';
  1343. print '<td>';
  1344. $userfee=new User($db);
  1345. $userfee->fetch($object->fk_user_valid);
  1346. print $userfee->getNomUrl(-1);
  1347. print '</td></tr>';
  1348. }
  1349. if ($object->fk_statut==6)
  1350. {
  1351. print '<tr>';
  1352. print '<td>'.$langs->trans("AUTHORPAIEMENT").'</td>';
  1353. print '<td>';
  1354. $userfee=new User($db);
  1355. $userfee->fetch($user->id);
  1356. print $userfee->getNomUrl(-1);
  1357. print '</td></tr>';
  1358. }
  1359. // Other attributes
  1360. //$cols = 3;
  1361. //include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_edit.tpl.php';
  1362. print '</table>';
  1363. dol_fiche_end();
  1364. print '<div class="center">';
  1365. print '<input type="submit" value="'.$langs->trans("Modify").'" name="bouton" class="button">';
  1366. print '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="button" value="'.$langs->trans("Cancel").'" class="button" onclick="history.go(-1)" />';
  1367. print '</div>';
  1368. print '</form>';
  1369. }
  1370. else
  1371. {
  1372. dol_fiche_head($head, 'card', $langs->trans("ExpenseReport"), -1, 'trip');
  1373. // Clone confirmation
  1374. if ($action == 'clone') {
  1375. // Create an array for form
  1376. $criteriaforfilter='hierarchyme';
  1377. if (! empty($user->rights->expensereport->readall)) $criteriaforfilter='';
  1378. $formquestion = array(
  1379. 'text' => '',
  1380. array('type' => 'other','name' => 'fk_user_author','label' => $langs->trans("SelectTargetUser"),'value' => $form->select_dolusers((GETPOST('fk_user_author', 'int')> 0 ? GETPOST('fk_user_author', 'int') : $user->id), 'fk_user_author', 0, null, 0, $criteriaforfilter))
  1381. );
  1382. // Paiement incomplet. On demande si motif = escompte ou autre
  1383. $formconfirm = $form->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $object->id, $langs->trans('CloneExpenseReport'), $langs->trans('ConfirmCloneExpenseReport', $object->ref), 'confirm_clone', $formquestion, 'yes', 1);
  1384. }
  1385. if ($action == 'save')
  1386. {
  1387. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("SaveTrip"),$langs->trans("ConfirmSaveTrip"),"confirm_validate","","",1);
  1388. }
  1389. if ($action == 'save_from_refuse')
  1390. {
  1391. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("SaveTrip"),$langs->trans("ConfirmSaveTrip"),"confirm_save_from_refuse","","",1);
  1392. }
  1393. if ($action == 'delete')
  1394. {
  1395. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("DeleteTrip"),$langs->trans("ConfirmDeleteTrip"),"confirm_delete","","",1);
  1396. }
  1397. if ($action == 'validate')
  1398. {
  1399. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("ValideTrip"),$langs->trans("ConfirmValideTrip"),"confirm_approve","","",1);
  1400. }
  1401. if ($action == 'paid')
  1402. {
  1403. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("PaidTrip"),$langs->trans("ConfirmPaidTrip"),"confirm_paid","","",1);
  1404. }
  1405. if ($action == 'cancel')
  1406. {
  1407. $array_input = array('text'=>$langs->trans("ConfirmCancelTrip"), array('type'=>"text",'label'=>'<strong>'.$langs->trans("Comment").'</strong>','name'=>"detail_cancel",'size'=>"50",'value'=>""));
  1408. $formconfirm=$form->formconfirm($_SEVER["PHP_SELF"]."?id=".$id,$langs->trans("Cancel"),"","confirm_cancel",$array_input,"",1);
  1409. }
  1410. if ($action == 'brouillonner')
  1411. {
  1412. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("BrouillonnerTrip"),$langs->trans("ConfirmBrouillonnerTrip"),"confirm_brouillonner","","",1);
  1413. }
  1414. if ($action == 'refuse') // Deny
  1415. {
  1416. $array_input = array('text'=>$langs->trans("ConfirmRefuseTrip"), array('type'=>"text",'label'=>$langs->trans("Comment"),'name'=>"detail_refuse",'size'=>"50",'value'=>""));
  1417. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id,$langs->trans("Deny"),'',"confirm_refuse",$array_input,"yes",1);
  1418. }
  1419. if ($action == 'delete_line')
  1420. {
  1421. $formconfirm=$form->formconfirm($_SERVER["PHP_SELF"]."?id=".$id."&rowid=".GETPOST('rowid','int'),$langs->trans("DeleteLine"),$langs->trans("ConfirmDeleteLine"),"confirm_delete_line",'','yes',1);
  1422. }
  1423. // Print form confirm
  1424. print $formconfirm;
  1425. // Expense report card
  1426. $linkback = '<a href="'.DOL_URL_ROOT.'/expensereport/list.php?restore_lastsearch_values=1'.(! empty($socid)?'&socid='.$socid:'').'">'.$langs->trans("BackToList").'</a>';
  1427. $morehtmlref='<div class="refidno">';
  1428. /*
  1429. // Ref customer
  1430. $morehtmlref.=$form->editfieldkey("RefCustomer", 'ref_client', $object->ref_client, $object, $user->rights->commande->creer, 'string', '', 0, 1);
  1431. $morehtmlref.=$form->editfieldval("RefCustomer", 'ref_client', $object->ref_client, $object, $user->rights->commande->creer, 'string', '', null, null, '', 1);
  1432. // Thirdparty
  1433. $morehtmlref.='<br>'.$langs->trans('ThirdParty') . ' : ' . $soc->getNomUrl(1);
  1434. // Project
  1435. if (! empty($conf->projet->enabled))
  1436. {
  1437. $langs->load("projects");
  1438. $morehtmlref.='<br>'.$langs->trans('Project') . ' ';
  1439. if ($user->rights->commande->creer)
  1440. {
  1441. if ($action != 'classify')
  1442. $morehtmlref.='<a href="' . $_SERVER['PHP_SELF'] . '?action=classify&amp;id=' . $object->id . '">' . img_edit($langs->transnoentitiesnoconv('SetProject')) . '</a> : ';
  1443. if ($action == 'classify') {
  1444. //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1);
  1445. $morehtmlref.='<form method="post" action="'.$_SERVER['PHP_SELF'].'?id='.$object->id.'">';
  1446. $morehtmlref.='<input type="hidden" name="action" value="classin">';
  1447. $morehtmlref.='<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1448. $morehtmlref.=$formproject->select_projects($object->socid, $object->fk_project, 'projectid', $maxlength, 0, 1, 0, 1, 0, 0, '', 1);
  1449. $morehtmlref.='<input type="submit" class="button valignmiddle" value="'.$langs->trans("Modify").'">';
  1450. $morehtmlref.='</form>';
  1451. } else {
  1452. $morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'none', 0, 0, 0, 1);
  1453. }
  1454. } else {
  1455. if (! empty($object->fk_project)) {
  1456. $proj = new Project($db);
  1457. $proj->fetch($object->fk_project);
  1458. $morehtmlref.='<a href="'.DOL_URL_ROOT.'/projet/card.php?id=' . $object->fk_project . '" title="' . $langs->trans('ShowProject') . '">';
  1459. $morehtmlref.=$proj->ref;
  1460. $morehtmlref.='</a>';
  1461. } else {
  1462. $morehtmlref.='';
  1463. }
  1464. }
  1465. }*/
  1466. $morehtmlref.='</div>';
  1467. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref);
  1468. print '<div class="fichecenter">';
  1469. print '<div class="fichehalfleft">';
  1470. print '<div class="underbanner clearboth"></div>';
  1471. print '<table class="border centpercent">';
  1472. // Author
  1473. print '<tr>';
  1474. print '<td class="titlefield">'.$langs->trans("User").'</td>';
  1475. print '<td>';
  1476. if ($object->fk_user_author > 0)
  1477. {
  1478. $userauthor=new User($db);
  1479. $result=$userauthor->fetch($object->fk_user_author);
  1480. if ($result < 0) dol_print_error('',$userauthor->error);
  1481. elseif ($result > 0) print $userauthor->getNomUrl(-1);
  1482. }
  1483. print '</td></tr>';
  1484. print '<tr>';
  1485. print '<td class="titlefield">'.$langs->trans("Period").'</td>';
  1486. print '<td>';
  1487. print get_date_range($object->date_debut,$object->date_fin,'day',$langs,0);
  1488. print '</td>';
  1489. print '</tr>';
  1490. if (! empty($conf->global->EXPENSEREPORT_ASK_PAYMENTMODE_ON_CREATION))
  1491. {
  1492. print '<tr>';
  1493. print '<td>'.$langs->trans("ModePaiement").'</td>';
  1494. print '<td>'.$object->libelle_paiement.'</td>';
  1495. print '</tr>';
  1496. }
  1497. // Validation date
  1498. print '<tr>';
  1499. print '<td>'.$langs->trans("DATE_SAVE").'</td>';
  1500. print '<td>'.dol_print_date($object->date_valid,'dayhour');
  1501. if ($object->status == 2 && $object->hasDelay('toapprove')) print ' '.img_warning($langs->trans("Late"));
  1502. if ($object->status == 5 && $object->hasDelay('topay')) print ' '.img_warning($langs->trans("Late"));
  1503. print '</td></tr>';
  1504. print '</tr>';
  1505. // User to inform for approval
  1506. if ($object->fk_statut < 3) // informed
  1507. {
  1508. print '<tr>';
  1509. print '<td>'.$langs->trans("VALIDATOR").'</td>'; // approver
  1510. print '<td>';
  1511. if ($object->fk_user_validator > 0)
  1512. {
  1513. $userfee=new User($db);
  1514. $result = $userfee->fetch($object->fk_user_validator);
  1515. if ($result > 0) print $userfee->getNomUrl(-1);
  1516. if (empty($userfee->email) || ! isValidEmail($userfee->email))
  1517. {
  1518. $langs->load("errors");
  1519. print img_warning($langs->trans("ErrorBadEMail", $userfee->email));
  1520. }
  1521. }
  1522. print '</td></tr>';
  1523. }
  1524. elseif($object->fk_statut == 4)
  1525. {
  1526. print '<tr>';
  1527. print '<td>'.$langs->trans("CANCEL_USER").'</span></td>';
  1528. print '<td>';
  1529. if ($object->fk_user_cancel > 0)
  1530. {
  1531. $userfee=new User($db);
  1532. $result = $userfee->fetch($object->fk_user_cancel);
  1533. if ($result > 0) print $userfee->getNomUrl(-1);
  1534. }
  1535. print '</td></tr>';
  1536. print '<tr>';
  1537. print '<td>'.$langs->trans("MOTIF_CANCEL").'</td>';
  1538. print '<td>'.$object->detail_cancel.'</td></tr>';
  1539. print '</tr>';
  1540. print '<tr>';
  1541. print '<td>'.$langs->trans("DATE_CANCEL").'</td>';
  1542. print '<td>'.dol_print_date($object->date_cancel,'dayhour').'</td></tr>';
  1543. print '</tr>';
  1544. }
  1545. else
  1546. {
  1547. print '<tr>';
  1548. print '<td>'.$langs->trans("ApprovedBy").'</td>';
  1549. print '<td>';
  1550. if ($object->fk_user_approve > 0)
  1551. {
  1552. $userapp=new User($db);
  1553. $result = $userapp->fetch($object->fk_user_approve);
  1554. if ($result > 0) print $userapp->getNomUrl(-1);
  1555. }
  1556. print '</td></tr>';
  1557. print '<tr>';
  1558. print '<td>'.$langs->trans("DateApprove").'</td>';
  1559. print '<td>'.dol_print_date($object->date_approve,'dayhour').'</td></tr>';
  1560. print '</tr>';
  1561. }
  1562. if ($object->fk_statut==99 || !empty($object->detail_refuse))
  1563. {
  1564. print '<tr>';
  1565. print '<td>'.$langs->trans("REFUSEUR").'</td>';
  1566. print '<td>';
  1567. $userfee=new User($db);
  1568. $result = $userfee->fetch($object->fk_user_refuse);
  1569. if ($result > 0) print $userfee->getNomUrl(-1);
  1570. print '</td></tr>';
  1571. print '<tr>';
  1572. print '<td>'.$langs->trans("DATE_REFUS").'</td>';
  1573. print '<td>'.dol_print_date($object->date_refuse,'dayhour');
  1574. if ($object->detail_refuse) print ' - '.$object->detail_refuse;
  1575. print '</td>';
  1576. print '</tr>';
  1577. }
  1578. if($object->fk_statut==6)
  1579. {
  1580. /* TODO this fields are not yet filled
  1581. print '<tr>';
  1582. print '<td>'.$langs->trans("AUTHORPAIEMENT").'</td>';
  1583. print '<td>';
  1584. $userfee=new User($db);
  1585. $userfee->fetch($object->fk_user_paid);
  1586. print $userfee->getNomUrl(-1);
  1587. print '</td></tr>';
  1588. print '<tr>';
  1589. print '<td>'.$langs->trans("DATE_PAIEMENT").'</td>';
  1590. print '<td>'.$object->date_paiement.'</td></tr>';
  1591. print '</tr>';
  1592. */
  1593. }
  1594. // Other attributes
  1595. $cols = 2;
  1596. include DOL_DOCUMENT_ROOT . '/core/tpl/extrafields_view.tpl.php';
  1597. print '</table>';
  1598. print '</div>';
  1599. print '<div class="fichehalfright">';
  1600. print '<div class="ficheaddleft">';
  1601. print '<div class="underbanner clearboth"></div>';
  1602. print '<table class="border centpercent">';
  1603. // Amount
  1604. print '<tr>';
  1605. print '<td class="titlefieldmiddle">'.$langs->trans("AmountHT").'</td>';
  1606. print '<td class="nowrap amountcard">'.price($object->total_ht, 1, '', 1, - 1, - 1, $conf->currency).'</td>';
  1607. $rowspan = 5;
  1608. if ($object->fk_statut < 3) $rowspan++;
  1609. elseif($object->fk_statut == 4) $rowspan+=2;
  1610. else $rowspan+=2;
  1611. if ($object->fk_statut==99 || !empty($object->detail_refuse)) $rowspan+=2;
  1612. if($object->fk_statut==6) $rowspan+=2;
  1613. print "</td>";
  1614. print '</tr>';
  1615. print '<tr>';
  1616. print '<td>'.$langs->trans("AmountVAT").'</td>';
  1617. print '<td class="nowrap amountcard">'.price($object->total_tva, 1, '', 1, - 1, - 1, $conf->currency).'</td>';
  1618. print '</tr>';
  1619. print '<tr>';
  1620. print '<td>'.$langs->trans("AmountTTC").'</td>';
  1621. print '<td class="nowrap amountcard">'.price($object->total_ttc, 1, '', 1, - 1, - 1, $conf->currency).'</td>';
  1622. print '</tr>';
  1623. // List of payments already done
  1624. $nbcols = 3;
  1625. if (! empty($conf->banque->enabled)) {
  1626. $nbrows ++;
  1627. $nbcols ++;
  1628. }
  1629. print '<table class="noborder paymenttable" width="100%">';
  1630. print '<tr class="liste_titre">';
  1631. print '<td class="liste_titre">' . $langs->trans('Payments') . '</td>';
  1632. print '<td class="liste_titre">' . $langs->trans('Date') . '</td>';
  1633. print '<td class="liste_titre">' . $langs->trans('Type') . '</td>';
  1634. if (! empty($conf->banque->enabled)) {
  1635. print '<td class="liste_titre" align="right">' . $langs->trans('BankAccount') . '</td>';
  1636. }
  1637. print '<td class="liste_titre" align="right">' . $langs->trans('Amount') . '</td>';
  1638. print '<td class="liste_titre" width="18">&nbsp;</td>';
  1639. print '</tr>';
  1640. // Payments already done (from payment on this expensereport)
  1641. $sql = "SELECT p.rowid, p.num_payment, p.datep as dp, p.amount, p.fk_bank,";
  1642. $sql.= "c.code as p_code, c.libelle as payment_type,";
  1643. $sql.= "ba.rowid as baid, ba.ref as baref, ba.label, ba.number as banumber, ba.account_number, ba.fk_accountancy_journal";
  1644. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as e, ".MAIN_DB_PREFIX."payment_expensereport as p";
  1645. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as c ON p.fk_typepayment = c.id";
  1646. $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON p.fk_bank = b.rowid';
  1647. $sql.= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank_account as ba ON b.fk_account = ba.rowid';
  1648. $sql.= " WHERE e.rowid = '".$id."'";
  1649. $sql.= " AND p.fk_expensereport = e.rowid";
  1650. $sql.= ' AND e.entity IN ('.getEntity('expensereport').')';
  1651. $sql.= " ORDER BY dp";
  1652. $resql = $db->query($sql);
  1653. if ($resql)
  1654. {
  1655. $num = $db->num_rows($resql);
  1656. $i = 0; $total = 0;
  1657. while ($i < $num)
  1658. {
  1659. $objp = $db->fetch_object($resql);
  1660. $paymentexpensereportstatic->id = $objp->rowid;
  1661. $paymentexpensereportstatic->datepaye = $db->jdate($objp->dp);
  1662. $paymentexpensereportstatic->ref = $objp->rowid;
  1663. $paymentexpensereportstatic->num_paiement = $objp->num_paiement;
  1664. $paymentexpensereportstatic->payment_code = $objp->payment_code;
  1665. print '<tr class="oddseven">';
  1666. print '<td>';
  1667. print $paymentexpensereportstatic->getNomUrl(1);
  1668. print '</td>';
  1669. print '<td>'.dol_print_date($db->jdate($objp->dp),'day')."</td>\n";
  1670. $labeltype=$langs->trans("PaymentType".$objp->p_code)!=("PaymentType".$objp->p_code)?$langs->trans("PaymentType".$objp->p_code):$objp->payment_type;
  1671. print "<td>".$labeltype.' '.$objp->num_payment."</td>\n";
  1672. if (! empty($conf->banque->enabled))
  1673. {
  1674. $bankaccountstatic->id = $objp->baid;
  1675. $bankaccountstatic->ref = $objp->baref;
  1676. $bankaccountstatic->label = $objp->baref;
  1677. $bankaccountstatic->number = $objp->banumber;
  1678. if (! empty($conf->accounting->enabled)) {
  1679. $bankaccountstatic->account_number = $objp->account_number;
  1680. $accountingjournal = new AccountingJournal($db);
  1681. $accountingjournal->fetch($objp->fk_accountancy_journal);
  1682. $bankaccountstatic->accountancy_journal = $accountingjournal->getNomUrl(0,1,1,'',1);
  1683. }
  1684. print '<td align="right">';
  1685. if ($bankaccountstatic->id)
  1686. print $bankaccountstatic->getNomUrl(1, 'transactions');
  1687. print '</td>';
  1688. }
  1689. print '<td align="right">'.price($objp->amount)."</td>";
  1690. print '<td></td>';
  1691. print "</tr>";
  1692. $totalpaid += $objp->amount;
  1693. $i++;
  1694. }
  1695. $totalpaid = price2num($totalpaid); // Round $totalpaid to fix floating problem after addition into loop
  1696. $remaintopay = price2num($object->total_ttc - $totalpaid);
  1697. $resteapayeraffiche = $remaintopay;
  1698. $cssforamountpaymentcomplete = 'amountpaymentcomplete';
  1699. if ($object->status == ExpenseReport::STATUS_REFUSED)
  1700. {
  1701. $cssforamountpaymentcomplete = 'amountpaymentneutral';
  1702. $resteapayeraffiche = 0;
  1703. }
  1704. elseif ($object->paid == 0)
  1705. {
  1706. $cssforamountpaymentcomplete = 'amountpaymentneutral';
  1707. }
  1708. print '<tr><td colspan="' . $nbcols . '" align="right">'.$langs->trans("AlreadyPaid").':</td><td align="right">'.price($totalpaid).'</td><td></td></tr>';
  1709. print '<tr><td colspan="' . $nbcols . '" align="right">'.$langs->trans("AmountExpected").':</td><td align="right">'.price($object->total_ttc).'</td><td></td></tr>';
  1710. print '<tr><td colspan="' . $nbcols . '" align="right">'.$langs->trans("RemainderToPay").':</td>';
  1711. print '<td align="right"'.($resteapayeraffiche?' class="amountremaintopay"':(' class="'.$cssforamountpaymentcomplete.'"')).'>'.price($resteapayeraffiche).'</td><td></td></tr>';
  1712. $db->free($resql);
  1713. }
  1714. else
  1715. {
  1716. dol_print_error($db);
  1717. }
  1718. print "</table>";
  1719. print '</div>';
  1720. print '</div>';
  1721. print '</div>';
  1722. print '<div class="clearboth"></div><br>';
  1723. print '<div style="clear: both;"></div>';
  1724. $actiontouse='updateline';
  1725. if (($object->fk_statut==0 || $object->fk_statut==99) && $action != 'editline') $actiontouse='addline';
  1726. print '<form name="expensereport" action="'.$_SERVER["PHP_SELF"].'" method="post">';
  1727. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  1728. print '<input type="hidden" name="action" value="'.$actiontouse.'">';
  1729. print '<input type="hidden" name="id" value="'.$object->id.'">';
  1730. print '<input type="hidden" name="fk_expensereport" value="'.$object->id.'" />';
  1731. print '<div class="div-table-responsive-no-min">';
  1732. print '<table id="tablelines" class="noborder" width="100%">';
  1733. if (!empty($object->lines))
  1734. {
  1735. $i = 0;$total = 0;
  1736. print '<tr class="liste_titre">';
  1737. print '<td style="text-align:center;">'.$langs->trans('LineNb').'</td>';
  1738. //print '<td style="text-align:center;">'.$langs->trans('Piece').'</td>';
  1739. print '<td style="text-align:center;">'.$langs->trans('Date').'</td>';
  1740. if (! empty($conf->projet->enabled)) print '<td class="minwidth100imp">'.$langs->trans('Project').'</td>';
  1741. if (!empty($conf->global->MAIN_USE_EXPENSE_IK)) print '<td>'.$langs->trans('CarCategory').'</td>';
  1742. print '<td style="text-align:center;">'.$langs->trans('Type').'</td>';
  1743. print '<td style="text-align:left;">'.$langs->trans('Description').'</td>';
  1744. print '<td style="text-align:right;">'.$langs->trans('VAT').'</td>';
  1745. print '<td style="text-align:right;">'.$langs->trans('PriceUTTC').'</td>';
  1746. print '<td style="text-align:right;">'.$langs->trans('Qty').'</td>';
  1747. if ($action != 'editline')
  1748. {
  1749. print '<td style="text-align:right;">'.$langs->trans('AmountHT').'</td>';
  1750. print '<td style="text-align:right;">'.$langs->trans('AmountTTC').'</td>';
  1751. }
  1752. // Ajout des boutons de modification/suppression
  1753. if (($object->fk_statut < 2 || $object->fk_statut == 99) && $user->rights->expensereport->creer)
  1754. {
  1755. print '<td style="text-align:right;"></td>';
  1756. }
  1757. print '</tr>';
  1758. foreach ($object->lines as &$line)
  1759. {
  1760. $numline = $i + 1;
  1761. if ($action != 'editline' || $line->rowid != GETPOST('rowid', 'int'))
  1762. {
  1763. print '<tr class="oddeven">';
  1764. print '<td style="text-align:center;">';
  1765. print $numline;
  1766. print '</td>';
  1767. /*print '<td style="text-align:center;">';
  1768. print img_picto($langs->trans("Document"), "object_generic");
  1769. print ' <span>'.$piece_comptable.'</span>';
  1770. print '</td>';*/
  1771. print '<td style="text-align:center;">'.dol_print_date($db->jdate($line->date), 'day').'</td>';
  1772. if (! empty($conf->projet->enabled))
  1773. {
  1774. print '<td>';
  1775. if ($line->fk_projet > 0)
  1776. {
  1777. $projecttmp->id=$line->fk_projet;
  1778. $projecttmp->ref=$line->projet_ref;
  1779. print $projecttmp->getNomUrl(1);
  1780. }
  1781. print '</td>';
  1782. }
  1783. if (!empty($conf->global->MAIN_USE_EXPENSE_IK))
  1784. {
  1785. print '<td class="fk_c_exp_tax_cat">';
  1786. print dol_getIdFromCode($db, $line->fk_c_exp_tax_cat, 'c_exp_tax_cat', 'rowid', 'label');
  1787. print '</td>';
  1788. }
  1789. print '<td class="center">';
  1790. $labeltype = ($langs->trans(($line->type_fees_code)) == $line->type_fees_code ? $line->type_fees_libelle : $langs->trans($line->type_fees_code));
  1791. print $labeltype;
  1792. print '</td>';
  1793. print '<td style="text-align:left;">'.dol_escape_htmltag($line->comments).'</td>';
  1794. print '<td style="text-align:right;">'.vatrate($line->vatrate,true).'</td>';
  1795. print '<td style="text-align:right;">'.price($line->value_unit).'</td>';
  1796. print '<td style="text-align:right;">'.dol_escape_htmltag($line->qty).'</td>';
  1797. if ($action != 'editline')
  1798. {
  1799. print '<td style="text-align:right;">'.price($line->total_ht).'</td>';
  1800. print '<td style="text-align:right;">'.price($line->total_ttc).'</td>';
  1801. }
  1802. // Ajout des boutons de modification/suppression
  1803. if (($object->fk_statut < 2 || $object->fk_statut == 99) && $user->rights->expensereport->creer)
  1804. {
  1805. print '<td style="text-align:right;" class="nowrap">';
  1806. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=editline&amp;rowid='.$line->rowid.'#'.$line->rowid.'">';
  1807. print img_edit();
  1808. print '</a> &nbsp; ';
  1809. print '<a href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=delete_line&amp;rowid='.$line->rowid.'">';
  1810. print img_delete();
  1811. print '</a>';
  1812. print '</td>';
  1813. }
  1814. print '</tr>';
  1815. }
  1816. if ($action == 'editline' && $line->rowid == GETPOST('rowid', 'int'))
  1817. {
  1818. print '<tr class="oddeven">';
  1819. print '<td></td>';
  1820. // Select date
  1821. print '<td class="center">';
  1822. print $form->selectDate($line->date,'date');
  1823. print '</td>';
  1824. // Select project
  1825. if (! empty($conf->projet->enabled))
  1826. {
  1827. print '<td>';
  1828. $formproject->select_projects(-1, $line->fk_projet,'fk_projet', 0, 0, 1, 1, 0, 0, 0, '', 0, 0, 'maxwidth300');
  1829. print '</td>';
  1830. }
  1831. if (!empty($conf->global->MAIN_USE_EXPENSE_IK))
  1832. {
  1833. print '<td class="fk_c_exp_tax_cat">';
  1834. $params = array('fk_expense' => $object->id, 'fk_expense_det' => $line->rowid, 'date' => $line->dates);
  1835. print $form->selectExpenseCategories($line->fk_c_exp_tax_cat, 'fk_c_exp_tax_cat', 1, array(), 'fk_c_type_fees', $userauthor->default_c_exp_tax_cat, $params);
  1836. print '</td>';
  1837. }
  1838. // Select type
  1839. print '<td class="center">';
  1840. select_type_fees_id($line->fk_c_type_fees,'fk_c_type_fees');
  1841. print '</td>';
  1842. // Add comments
  1843. print '<td>';
  1844. print '<textarea name="comments" class="flat_ndf centpercent">'.dol_escape_htmltag($line->comments).'</textarea>';
  1845. print '</td>';
  1846. // VAT
  1847. print '<td style="text-align:right;">';
  1848. print $form->load_tva('vatrate', (isset($_POST["vatrate"])?$_POST["vatrate"]:$line->vatrate), $mysoc, '');
  1849. print '</td>';
  1850. // Unit price
  1851. print '<td style="text-align:right;">';
  1852. print '<input type="text" min="0" class="maxwidth100" name="value_unit" value="'.dol_escape_htmltag($line->value_unit).'" />';
  1853. print '</td>';
  1854. // Quantity
  1855. print '<td style="text-align:right;">';
  1856. print '<input type="number" min="0" class="maxwidth100" name="qty" value="'.dol_escape_htmltag($line->qty).'" />';
  1857. print '</td>';
  1858. if ($action != 'editline')
  1859. {
  1860. print '<td style="text-align:right;">'.$langs->trans('AmountHT').'</td>';
  1861. print '<td style="text-align:right;">'.$langs->trans('AmountTTC').'</td>';
  1862. }
  1863. print '<td style="text-align:center;">';
  1864. print '<input type="hidden" name="rowid" value="'.$line->rowid.'">';
  1865. print '<input type="submit" class="button" name="save" value="'.$langs->trans('Save').'">';
  1866. print '<br><input type="submit" class="button" name="cancel" value="'.$langs->trans('Cancel').'">';
  1867. print '</td>';
  1868. }
  1869. $i++;
  1870. }
  1871. }
  1872. // Add a line
  1873. if (($object->fk_statut==0 || $object->fk_statut==99) && $action != 'editline' && $user->rights->expensereport->creer)
  1874. {
  1875. print '<tr class="liste_titre">';
  1876. print '<td></td>';
  1877. print '<td align="center">'.$langs->trans('Date').'</td>';
  1878. if (! empty($conf->projet->enabled)) print '<td class="minwidth100imp">'.$langs->trans('Project').'</td>';
  1879. if (!empty($conf->global->MAIN_USE_EXPENSE_IK)) print '<td>'.$langs->trans('CarCategory').'</td>';
  1880. print '<td align="center">'.$langs->trans('Type').'</td>';
  1881. print '<td>'.$langs->trans('Description').'</td>';
  1882. print '<td align="right">'.$langs->trans('VAT').'</td>';
  1883. print '<td align="right">'.$langs->trans('PriceUTTC').'</td>';
  1884. print '<td align="right">'.$langs->trans('Qty').'</td>';
  1885. print '<td colspan="3"></td>';
  1886. print '</tr>';
  1887. print '<tr class="oddeven">';
  1888. print '<td></td>';
  1889. // Select date
  1890. print '<td align="center">';
  1891. print $form->selectDate($date?$date:-1, 'date');
  1892. print '</td>';
  1893. // Select project
  1894. if (! empty($conf->projet->enabled))
  1895. {
  1896. print '<td>';
  1897. $formproject->select_projects(-1, $fk_projet, 'fk_projet', 0, 0, 1, 1, 0, 0, 0, '', 0, 0, 'maxwidth300');
  1898. print '</td>';
  1899. }
  1900. if (!empty($conf->global->MAIN_USE_EXPENSE_IK))
  1901. {
  1902. print '<td class="fk_c_exp_tax_cat">';
  1903. $params = array('fk_expense' => $object->id);
  1904. print $form->selectExpenseCategories('', 'fk_c_exp_tax_cat', 1, array(), 'fk_c_type_fees', $userauthor->default_c_exp_tax_cat, $params);
  1905. print '</td>';
  1906. }
  1907. // Select type
  1908. print '<td align="center">';
  1909. select_type_fees_id($fk_c_type_fees,'fk_c_type_fees',1);
  1910. print '</td>';
  1911. // Add comments
  1912. print '<td>';
  1913. print '<textarea class="flat_ndf centpercent" name="comments">'.dol_escape_htmltag($comments).'</textarea>';
  1914. print '</td>';
  1915. // Select VAT
  1916. print '<td align="right">';
  1917. $defaultvat=-1;
  1918. if (! empty($conf->global->EXPENSEREPORT_NO_DEFAULT_VAT)) $conf->global->MAIN_VAT_DEFAULT_IF_AUTODETECT_FAILS = 'none';
  1919. print $form->load_tva('vatrate', ($vatrate!=''?$vatrate:$defaultvat), $mysoc, '', 0, 0, '', false, 1);
  1920. print '</td>';
  1921. // Unit price
  1922. print '<td align="right">';
  1923. print '<input type="text" class="right maxwidth50" name="value_unit" value="'.dol_escape_htmltag($value_unit).'">';
  1924. print '</td>';
  1925. // Quantity
  1926. print '<td align="right">';
  1927. print '<input type="text" min="0" class="right maxwidth50" name="qty" value="'.dol_escape_htmltag($qty?$qty:1).'">'; // We must be able to enter decimal qty
  1928. print '</td>';
  1929. if ($action != 'editline')
  1930. {
  1931. print '<td align="right"></td>';
  1932. print '<td align="right"></td>';
  1933. }
  1934. print '<td align="center"><input type="submit" value="'.$langs->trans("Add").'" name="bouton" class="button"></td>';
  1935. print '</tr>';
  1936. } // Fin si c'est payé/validé
  1937. print '</table>';
  1938. print '</div>';
  1939. print '</form>';
  1940. dol_fiche_end();
  1941. } // end edit or not edit
  1942. } // end of if result
  1943. else
  1944. {
  1945. dol_print_error($db);
  1946. }
  1947. } //fin si id > 0
  1948. }
  1949. /*
  1950. * Barre d'actions
  1951. */
  1952. print '<div class="tabsAction">';
  1953. if ($action != 'create' && $action != 'edit')
  1954. {
  1955. $object = new ExpenseReport($db);
  1956. $object->fetch($id, $ref);
  1957. // Send
  1958. if ($object->fk_statut > ExpenseReport::STATUS_DRAFT) {
  1959. //if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) || $user->rights->expensereport->expensereport_advance->send)) {
  1960. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id=' . $object->id . '&action=presend&mode=init#formmailbeforetitle">' . $langs->trans('SendMail') . '</a></div>';
  1961. //} else
  1962. // print '<div class="inline-block divButAction"><a class="butActionRefused" href="#">' . $langs->trans('SendMail') . '</a></div>';
  1963. }
  1964. /* Si l'état est "Brouillon"
  1965. * ET user à droit "creer/supprimer"
  1966. * ET fk_user_author == user courant
  1967. * Afficher : "Enregistrer" / "Modifier" / "Supprimer"
  1968. */
  1969. if ($user->rights->expensereport->creer && $object->fk_statut==0)
  1970. {
  1971. if (in_array($object->fk_user_author, $user->getAllChildIds(1)) || !empty($user->rights->expensereport->writeall_advance))
  1972. {
  1973. // Modify
  1974. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&id='.$object->id.'">'.$langs->trans('Modify').'</a></div>';
  1975. // Validate
  1976. if (count($object->lines) > 0)
  1977. {
  1978. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=save&id='.$object->id.'">'.$langs->trans('ValidateAndSubmit').'</a></div>';
  1979. }
  1980. }
  1981. }
  1982. /* Si l'état est "Refusée"
  1983. * ET user à droit "creer/supprimer"
  1984. * ET fk_user_author == user courant
  1985. * Afficher : "Enregistrer" / "Modifier" / "Supprimer"
  1986. */
  1987. if($user->rights->expensereport->creer && $object->fk_statut==99)
  1988. {
  1989. if ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid)
  1990. {
  1991. // Modify
  1992. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=edit&id='.$object->id.'">'.$langs->trans('Modify').'</a></div>';
  1993. // Brouillonner (le statut refusée est identique à brouillon)
  1994. //print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=brouillonner&id='.$id.'">'.$langs->trans('BROUILLONNER').'</a>';
  1995. // Enregistrer depuis le statut "Refusée"
  1996. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=save_from_refuse&id='.$object->id.'">'.$langs->trans('ValidateAndSubmit').'</a></div>';
  1997. }
  1998. }
  1999. if ($user->rights->expensereport->to_paid && $object->fk_statut==5)
  2000. {
  2001. if ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid)
  2002. {
  2003. // Brouillonner
  2004. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=brouillonner&id='.$object->id.'">'.$langs->trans('SetToDraft').'</a></div>';
  2005. }
  2006. }
  2007. /* Si l'état est "En attente d'approbation"
  2008. * ET user à droit de "approve"
  2009. * ET fk_user_validator == user courant
  2010. * Afficher : "Valider" / "Refuser" / "Supprimer"
  2011. */
  2012. if ($object->fk_statut == 2)
  2013. {
  2014. if (in_array($object->fk_user_author, $user->getAllChildIds(1)))
  2015. {
  2016. // Brouillonner
  2017. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=brouillonner&id='.$object->id.'">'.$langs->trans('SetToDraft').'</a></div>';
  2018. }
  2019. }
  2020. if ($user->rights->expensereport->approve && $object->fk_statut == 2)
  2021. {
  2022. //if($object->fk_user_validator==$user->id)
  2023. //{
  2024. // Validate
  2025. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=validate&id='.$object->id.'">'.$langs->trans('Approve').'</a></div>';
  2026. // Deny
  2027. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=refuse&id='.$object->id.'">'.$langs->trans('Deny').'</a></div>';
  2028. //}
  2029. if ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid)
  2030. {
  2031. // Cancel
  2032. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=cancel&id='.$object->id.'">'.$langs->trans('Cancel').'</a></div>';
  2033. }
  2034. }
  2035. // If status is Appoved
  2036. // --------------------
  2037. if ($user->rights->expensereport->approve && $object->fk_statut == 5)
  2038. {
  2039. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=refuse&id='.$object->id.'">'.$langs->trans('Deny').'</a></div>';
  2040. }
  2041. // If bank module is used
  2042. if ($user->rights->expensereport->to_paid && ! empty($conf->banque->enabled) && $object->fk_statut == 5)
  2043. {
  2044. // Pay
  2045. if ($remaintopay == 0)
  2046. {
  2047. print '<div class="inline-block divButAction"><span class="butActionRefused" title="' . $langs->trans("DisabledBecauseRemainderToPayIsZero") . '">' . $langs->trans('DoPayment') . '</span></div>';
  2048. }
  2049. else
  2050. {
  2051. 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>';
  2052. }
  2053. }
  2054. // If bank module is not used
  2055. if (($user->rights->expensereport->to_paid || empty($conf->banque->enabled)) && $object->fk_statut == 5)
  2056. {
  2057. //if ((round($remaintopay) == 0 || empty($conf->banque->enabled)) && $object->paid == 0)
  2058. if ($object->paid == 0)
  2059. {
  2060. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER["PHP_SELF"] . '?id='.$object->id.'&action=set_paid">'.$langs->trans("ClassifyPaid")."</a></div>";
  2061. }
  2062. }
  2063. if ($user->rights->expensereport->creer && ($user->id == $object->fk_user_author || $user->id == $object->fk_user_valid) && $object->fk_statut == 5)
  2064. {
  2065. // Cancel
  2066. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=cancel&id='.$object->id.'">'.$langs->trans('Cancel').'</a></div>';
  2067. }
  2068. // TODO Replace this. It should be SetUnpaid and should go back to status unpaid not canceled.
  2069. if (($user->rights->expensereport->approve || $user->rights->expensereport->to_paid) && $object->fk_statut == 6)
  2070. {
  2071. // Cancel
  2072. print '<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=cancel&id='.$object->id.'">'.$langs->trans('Cancel').'</a></div>';
  2073. }
  2074. // Clone
  2075. if ($user->rights->expensereport->creer) {
  2076. print '<div class="inline-block divButAction"><a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?id=' . $object->id . '&amp;action=clone">' . $langs->trans("ToClone") . '</a></div>';
  2077. }
  2078. /* If draft, validated, cancel, and user can create, he can always delete its card before it is approved */
  2079. if ($user->rights->expensereport->creer && $user->id == $object->fk_user_author && $object->fk_statut <= 4)
  2080. {
  2081. // Delete
  2082. print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?action=delete&id='.$object->id.'">'.$langs->trans('Delete').'</a></div>';
  2083. }
  2084. else if($user->rights->expensereport->supprimer && $object->fk_statut != 6)
  2085. {
  2086. // Delete
  2087. print '<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER["PHP_SELF"].'?action=delete&id='.$object->id.'">'.$langs->trans('Delete').'</a></div>';
  2088. }
  2089. $parameters = array();
  2090. $reshook = $hookmanager->executeHooks('addMoreActionsButtons', $parameters, $object, $action); // Note that $action and $object may have been
  2091. }
  2092. print '</div>';
  2093. //$conf->global->DOL_URL_ROOT_DOCUMENT_PHP=dol_buildpath('/expensereport/documentwrapper.php',1);
  2094. // Select mail models is same action as presend
  2095. if (GETPOST('modelselected', 'alpha')) {
  2096. $action = 'presend';
  2097. }
  2098. if ($action != 'presend')
  2099. {
  2100. /*
  2101. * Generate documents
  2102. */
  2103. print '<div class="fichecenter"><div class="fichehalfleft">';
  2104. print '<a name="builddoc"></a>'; // ancre
  2105. if($user->rights->expensereport->creer && $action != 'create' && $action != 'edit')
  2106. {
  2107. $filename = dol_sanitizeFileName($object->ref);
  2108. $filedir = $conf->expensereport->dir_output . "/" . dol_sanitizeFileName($object->ref);
  2109. $urlsource = $_SERVER["PHP_SELF"]."?id=".$object->id;
  2110. $genallowed = $user->rights->expensereport->creer;
  2111. $delallowed = $user->rights->expensereport->creer;
  2112. $var = true;
  2113. print $formfile->showdocuments('expensereport', $filename, $filedir, $urlsource, $genallowed, $delallowed);
  2114. $somethingshown = $formfile->numoffiles;
  2115. }
  2116. if ($action != 'create' && $action != 'edit' && ($id || $ref))
  2117. {
  2118. $linktoelem = $form->showLinkToObjectBlock($object, null, array('expensereport'));
  2119. $somethingshown = $form->showLinkedObjectBlock($object, $linktoelem);
  2120. }
  2121. print '</div><div class="fichehalfright"><div class="ficheaddleft">';
  2122. // List of actions on element
  2123. include_once DOL_DOCUMENT_ROOT . '/core/class/html.formactions.class.php';
  2124. $formactions = new FormActions($db);
  2125. $somethingshown = $formactions->showactions($object, 'expensereport', null);
  2126. print '</div></div></div>';
  2127. }
  2128. // Presend form
  2129. $modelmail='expensereport';
  2130. $defaulttopic='SendExpenseReportRef';
  2131. $diroutput = $conf->expensereport->dir_output;
  2132. $trackid = 'exp'.$object->id;
  2133. include DOL_DOCUMENT_ROOT.'/core/tpl/card_presend.tpl.php';
  2134. llxFooter();
  2135. $db->close();