card.php 84 KB

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