expensereport.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. <?php
  2. /* Copyright (C) 2003-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2015 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
  5. * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
  6. * Copyright (C) 2005-2014 Regis Houssin <regis.houssin@inodbox.com>
  7. * Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
  8. * Copyright (C) 2011-2013 Juanjo Menent <jmenent@2byte.es>
  9. * Copyright (C) 2011-2018 Philippe Grand <philippe.grand@atoo-net.com>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/admin/expensereport.php
  26. * \ingroup expensereport
  27. * \brief Setup page of module ExpenseReport
  28. */
  29. require '../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/expensereport.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array('admin', 'errors', 'trips', 'other'));
  36. if (!$user->admin) {
  37. accessforbidden();
  38. }
  39. $action = GETPOST('action', 'aZ09');
  40. $value = GETPOST('value', 'alpha');
  41. $modulepart = GETPOST('modulepart', 'aZ09'); // Used by actions_setmoduleoptions.inc.php
  42. $label = GETPOST('label', 'alpha');
  43. $scandir = GETPOST('scan_dir', 'alpha');
  44. $type = 'expensereport';
  45. /*
  46. * Actions
  47. */
  48. include DOL_DOCUMENT_ROOT.'/core/actions_setmoduleoptions.inc.php';
  49. if ($action == 'updateMask') {
  50. $maskconst = GETPOST('maskconst', 'alpha');
  51. $maskvalue = GETPOST('maskvalue', 'alpha');
  52. if ($maskconst) {
  53. $res = dolibarr_set_const($db, $maskconst, $maskvalue, 'chaine', 0, '', $conf->entity);
  54. }
  55. if (!($res > 0)) {
  56. $error++;
  57. }
  58. if (!$error) {
  59. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  60. } else {
  61. setEventMessages($langs->trans("Error"), null, 'errors');
  62. }
  63. } elseif ($action == 'specimen') { // For fiche expensereport
  64. $modele = GETPOST('module', 'alpha');
  65. $expensespecimen = new ExpenseReport($db);
  66. $expensespecimen->initAsSpecimen();
  67. $expensespecimen->status = 0; // Force statut draft to show watermark
  68. // Search template files
  69. $file = ''; $classname = ''; $filefound = 0;
  70. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  71. foreach ($dirmodels as $reldir) {
  72. $file = dol_buildpath($reldir."core/modules/expensereport/doc/pdf_".$modele.".modules.php", 0);
  73. if (file_exists($file)) {
  74. $filefound = 1;
  75. $classname = "pdf_".$modele;
  76. break;
  77. }
  78. }
  79. if ($filefound) {
  80. require_once $file;
  81. $module = new $classname($db);
  82. if ($module->write_file($expensespecimen, $langs) > 0) {
  83. header("Location: ".DOL_URL_ROOT."/document.php?modulepart=expensereport&file=SPECIMEN.pdf");
  84. return;
  85. } else {
  86. setEventMessages($module->error, $module->errors, 'errors');
  87. dol_syslog($module->error, LOG_ERR);
  88. }
  89. } else {
  90. setEventMessages($langs->trans("ErrorModuleNotFound"), null, 'errors');
  91. dol_syslog($langs->trans("ErrorModuleNotFound"), LOG_ERR);
  92. }
  93. } elseif ($action == 'set') {
  94. // Activate a model
  95. $ret = addDocumentModel($value, $type, $label, $scandir);
  96. if ($ret > 0 && empty($conf->global->EXPENSEREPORT_ADDON_PDF)) {
  97. dolibarr_set_const($db, 'EXPENSEREPORT_ADDON_PDF', $value, 'chaine', 0, '', $conf->entity);
  98. }
  99. } elseif ($action == 'del') {
  100. $ret = delDocumentModel($value, $type);
  101. if ($ret > 0) {
  102. if ($conf->global->EXPENSEREPORT_ADDON_PDF == "$value") {
  103. dolibarr_del_const($db, 'EXPENSEREPORT_ADDON_PDF', $conf->entity);
  104. }
  105. }
  106. } elseif ($action == 'setdoc') {
  107. // Set default model
  108. if (dolibarr_set_const($db, "EXPENSEREPORT_ADDON_PDF", $value, 'chaine', 0, '', $conf->entity)) {
  109. // La constante qui a ete lue en avant du nouveau set
  110. // on passe donc par une variable pour avoir un affichage coherent
  111. $conf->global->EXPENSEREPORT_ADDON_PDF = $value;
  112. }
  113. // On active le modele
  114. $ret = delDocumentModel($value, $type);
  115. if ($ret > 0) {
  116. $ret = addDocumentModel($value, $type, $label, $scandir);
  117. }
  118. } elseif ($action == 'setmod') {
  119. // TODO Verifier si module numerotation choisi peut etre active
  120. // par appel methode canBeActivated
  121. dolibarr_set_const($db, "EXPENSEREPORT_ADDON", $value, 'chaine', 0, '', $conf->entity);
  122. } elseif ($action == 'setoptions') {
  123. $db->begin();
  124. $freetext = GETPOST('EXPENSEREPORT_FREE_TEXT', 'restricthtml'); // No alpha here, we want exact string
  125. $res1 = dolibarr_set_const($db, "EXPENSEREPORT_FREE_TEXT", $freetext, 'chaine', 0, '', $conf->entity);
  126. $draft = GETPOST('EXPENSEREPORT_DRAFT_WATERMARK', 'alpha');
  127. $res2 = dolibarr_set_const($db, "EXPENSEREPORT_DRAFT_WATERMARK", trim($draft), 'chaine', 0, '', $conf->entity);
  128. $res3 = 0;
  129. if (!empty($conf->project->enabled) && GETPOSTISSET('EXPENSEREPORT_PROJECT_IS_REQUIRED')) { // Option may not be provided
  130. $res3 = dolibarr_set_const($db, 'EXPENSEREPORT_PROJECT_IS_REQUIRED', GETPOST('EXPENSEREPORT_PROJECT_IS_REQUIRED', 'int'), 'chaine', 0, '', $conf->entity);
  131. }
  132. $dates = GETPOST('EXPENSEREPORT_PREFILL_DATES_WITH_CURRENT_MONTH', 'int');
  133. $res4 = dolibarr_set_const($db, 'EXPENSEREPORT_PREFILL_DATES_WITH_CURRENT_MONTH', intval($dates), 'chaine', 0, '', $conf->entity);
  134. $amounts = GETPOST('EXPENSEREPORT_FORCE_LINE_AMOUNTS_INCLUDING_TAXES_ONLY', 'int');
  135. $res5 = dolibarr_set_const($db, 'EXPENSEREPORT_FORCE_LINE_AMOUNTS_INCLUDING_TAXES_ONLY', intval($amounts), 'chaine', 0, '', $conf->entity);
  136. if (!($res1 > 0) || !($res2 > 0) || !($res3 >= 0) || !($res4 >0) || !($res5 >0)) {
  137. $error++;
  138. }
  139. if (!$error) {
  140. $db->commit();
  141. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  142. } else {
  143. $db->rollback();
  144. setEventMessages($langs->trans("Error"), null, 'errors');
  145. }
  146. }
  147. /*
  148. * View
  149. */
  150. $dirmodels = array_merge(array('/'), (array) $conf->modules_parts['models']);
  151. llxHeader('', $langs->trans("ExpenseReportsSetup"));
  152. $form = new Form($db);
  153. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  154. print load_fiche_titre($langs->trans("ExpenseReportsSetup"), $linkback, 'title_setup');
  155. $head = expensereport_admin_prepare_head();
  156. print dol_get_fiche_head($head, 'expensereport', $langs->trans("ExpenseReports"), -1, 'trip');
  157. /*
  158. * Expense report numbering model
  159. */
  160. print load_fiche_titre($langs->trans("ExpenseReportNumberingModules"), '', '');
  161. print '<table class="noborder centpercent">';
  162. print '<tr class="liste_titre">';
  163. print '<td>'.$langs->trans("Name").'</td>';
  164. print '<td>'.$langs->trans("Description").'</td>';
  165. print '<td class="nowrap">'.$langs->trans("Example").'</td>';
  166. print '<td class="center" width="60">'.$langs->trans("Status").'</td>';
  167. print '<td class="center" width="16">'.$langs->trans("ShortInfo").'</td>';
  168. print '</tr>'."\n";
  169. clearstatcache();
  170. foreach ($dirmodels as $reldir) {
  171. $dir = dol_buildpath($reldir."core/modules/expensereport/");
  172. if (is_dir($dir)) {
  173. $handle = opendir($dir);
  174. if (is_resource($handle)) {
  175. while (($file = readdir($handle)) !== false) {
  176. if (substr($file, 0, 18) == 'mod_expensereport_' && substr($file, dol_strlen($file) - 3, 3) == 'php') {
  177. $file = substr($file, 0, dol_strlen($file) - 4);
  178. require_once $dir.$file.'.php';
  179. $module = new $file($db);
  180. // Show modules according to features level
  181. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  182. continue;
  183. }
  184. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  185. continue;
  186. }
  187. if ($module->isEnabled()) {
  188. print '<tr class="oddeven"><td>'.$module->nom."</td><td>\n";
  189. print $module->info();
  190. print '</td>';
  191. // Show example of numbering model
  192. print '<td class="nowrap">';
  193. $tmp = $module->getExample();
  194. if (preg_match('/^Error/', $tmp)) {
  195. $langs->load("errors");
  196. print '<div class="error">'.$langs->trans($tmp).'</div>';
  197. } elseif ($tmp == 'NotConfigured') {
  198. print '<span class="opacitymedium">'.$langs->trans($tmp).'</span>';
  199. } else {
  200. print $tmp;
  201. }
  202. print '</td>'."\n";
  203. print '<td class="center">';
  204. if ($conf->global->EXPENSEREPORT_ADDON == $file) {
  205. print img_picto($langs->trans("Activated"), 'switch_on');
  206. } else {
  207. print '<a class="reposition" href="'.$_SERVER["PHP_SELF"].'?action=setmod&token='.newToken().'&value='.urlencode($file).'">';
  208. print img_picto($langs->trans("Disabled"), 'switch_off');
  209. print '</a>';
  210. }
  211. print '</td>';
  212. $exp = new ExpenseReport($db);
  213. $exp->initAsSpecimen();
  214. // Info
  215. $htmltooltip = '';
  216. $htmltooltip .= ''.$langs->trans("Version").': <b>'.$module->getVersion().'</b><br>';
  217. $nextval = $module->getNextValue($exp);
  218. if ("$nextval" != $langs->trans("NotAvailable")) { // Keep " on nextval
  219. $htmltooltip .= ''.$langs->trans("NextValue").': ';
  220. if ($nextval) {
  221. if (preg_match('/^Error/', $nextval) || $nextval == 'NotConfigured') {
  222. $nextval = $langs->trans($nextval);
  223. }
  224. $htmltooltip .= $nextval.'<br>';
  225. } else {
  226. $htmltooltip .= $langs->trans($module->error).'<br>';
  227. }
  228. }
  229. print '<td class="center">';
  230. print $form->textwithpicto('', $htmltooltip, 1, 0);
  231. print '</td>';
  232. print "</tr>\n";
  233. }
  234. }
  235. }
  236. closedir($handle);
  237. }
  238. }
  239. }
  240. print "</table><br>\n";
  241. /*
  242. * Documents models for ExpenseReport
  243. */
  244. print load_fiche_titre($langs->trans("TemplatePDFExpenseReports"), '', '');
  245. // Defini tableau def des modeles
  246. $type = 'expensereport';
  247. $def = array();
  248. $sql = "SELECT nom";
  249. $sql .= " FROM ".MAIN_DB_PREFIX."document_model";
  250. $sql .= " WHERE type = '".$db->escape($type)."'";
  251. $sql .= " AND entity = ".$conf->entity;
  252. $resql = $db->query($sql);
  253. if ($resql) {
  254. $i = 0;
  255. $num_rows = $db->num_rows($resql);
  256. while ($i < $num_rows) {
  257. $array = $db->fetch_array($resql);
  258. array_push($def, $array[0]);
  259. $i++;
  260. }
  261. } else {
  262. dol_print_error($db);
  263. }
  264. print '<table class="noborder centpercent">';
  265. print '<tr class="liste_titre">';
  266. print '<td>'.$langs->trans("Name").'</td>';
  267. print '<td>'.$langs->trans("Description").'</td>';
  268. print '<td class="center" width="60">'.$langs->trans("Status")."</td>\n";
  269. print '<td class="center" width="60">'.$langs->trans("Default")."</td>\n";
  270. print '<td class="center" width="80">'.$langs->trans("ShortInfo").'</td>';
  271. print '<td class="center" width="80">'.$langs->trans("Preview").'</td>';
  272. print "</tr>\n";
  273. clearstatcache();
  274. foreach ($dirmodels as $reldir) {
  275. $dir = dol_buildpath($reldir."core/modules/expensereport/doc");
  276. if (is_dir($dir)) {
  277. $handle = opendir($dir);
  278. if (is_resource($handle)) {
  279. while (($file = readdir($handle)) !== false) {
  280. $filelist[] = $file;
  281. }
  282. closedir($handle);
  283. arsort($filelist);
  284. foreach ($filelist as $file) {
  285. if (preg_match('/\.modules\.php$/i', $file) && preg_match('/^(pdf_|doc_)/', $file)) {
  286. if (file_exists($dir.'/'.$file)) {
  287. $name = substr($file, 4, dol_strlen($file) - 16);
  288. $classname = substr($file, 0, dol_strlen($file) - 12);
  289. require_once $dir.'/'.$file;
  290. $module = new $classname($db);
  291. $modulequalified = 1;
  292. if ($module->version == 'development' && $conf->global->MAIN_FEATURES_LEVEL < 2) {
  293. $modulequalified = 0;
  294. }
  295. if ($module->version == 'experimental' && $conf->global->MAIN_FEATURES_LEVEL < 1) {
  296. $modulequalified = 0;
  297. }
  298. if ($modulequalified) {
  299. print '<tr class="oddeven"><td width="100">';
  300. print (empty($module->name) ? $name : $module->name);
  301. print "</td><td>\n";
  302. if (method_exists($module, 'info')) {
  303. print $module->info($langs);
  304. } else {
  305. print $module->description;
  306. }
  307. print '</td>';
  308. // Active
  309. if (in_array($name, $def)) {
  310. print '<td class="center">'."\n";
  311. print '<a href="'.$_SERVER["PHP_SELF"].'?action=del&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'">';
  312. print img_picto($langs->trans("Enabled"), 'switch_on');
  313. print '</a>';
  314. print "</td>";
  315. } else {
  316. print '<td class="center">'."\n";
  317. print '<a href="'.$_SERVER["PHP_SELF"].'?action=set&token='.newToken().'&value='.urlencode($name).'&scan_dir='.urlencode($module->scandir).'&label='.urlencode($module->name).'">'.img_picto($langs->trans("Disabled"), 'switch_off').'</a>';
  318. print "</td>";
  319. }
  320. // Default
  321. print '<td class="center">';
  322. if ($conf->global->EXPENSEREPORT_ADDON_PDF == "$name") {
  323. print img_picto($langs->trans("Default"), 'on');
  324. } else {
  325. print '<a href="'.$_SERVER["PHP_SELF"].'?action=setdoc&token='.newToken().'&value='.$name.'&scan_dir='.$module->scandir.'&label='.urlencode($module->name).'" alt="'.$langs->trans("Default").'">'.img_picto($langs->trans("Disabled"), 'off').'</a>';
  326. }
  327. print '</td>';
  328. // Info
  329. $htmltooltip = ''.$langs->trans("Name").': '.$module->name;
  330. $htmltooltip .= '<br>'.$langs->trans("Type").': '.($module->type ? $module->type : $langs->trans("Unknown"));
  331. $htmltooltip .= '<br>'.$langs->trans("Width").'/'.$langs->trans("Height").': '.$module->page_largeur.'/'.$module->page_hauteur;
  332. $htmltooltip .= '<br><br><u>'.$langs->trans("FeaturesSupported").':</u>';
  333. $htmltooltip .= '<br>'.$langs->trans("Logo").': '.yn($module->option_logo, 1, 1);
  334. $htmltooltip .= '<br>'.$langs->trans("PaymentMode").': '.yn($module->option_modereg, 1, 1);
  335. $htmltooltip .= '<br>'.$langs->trans("PaymentConditions").': '.yn($module->option_condreg, 1, 1);
  336. $htmltooltip .= '<br>'.$langs->trans("MultiLanguage").': '.yn($module->option_multilang, 1, 1);
  337. $htmltooltip .= '<br>'.$langs->trans("WatermarkOnDraftOrders").': '.yn($module->option_draft_watermark, 1, 1);
  338. print '<td class="center">';
  339. print $form->textwithpicto('', $htmltooltip, -1, 0);
  340. print '</td>';
  341. // Preview
  342. print '<td class="center">';
  343. if ($module->type == 'pdf') {
  344. print '<a href="'.$_SERVER["PHP_SELF"].'?action=specimen&module='.$name.'">'.img_object($langs->trans("Preview"), 'pdf').'</a>';
  345. } else {
  346. print img_object($langs->trans("PreviewNotAvailable"), 'generic');
  347. }
  348. print '</td>';
  349. print '</tr>';
  350. }
  351. }
  352. }
  353. }
  354. }
  355. }
  356. }
  357. print '</table>';
  358. print '<br>';
  359. /*
  360. * Other options
  361. */
  362. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">';
  363. print '<input type="hidden" name="token" value="'.newToken().'">';
  364. print '<input type="hidden" name="action" value="setoptions">';
  365. print load_fiche_titre($langs->trans("OtherOptions"), '', '');
  366. print '<table class="noborder centpercent">';
  367. print '<tr class="liste_titre">';
  368. print '<td>'.$langs->trans("Parameter").'</td>';
  369. print '<td class="center" width="60"></td>';
  370. print "</tr>\n";
  371. $substitutionarray = pdf_getSubstitutionArray($langs, null, null, 2);
  372. $substitutionarray['__(AnyTranslationKey)__'] = $langs->trans("Translation");
  373. $htmltext = '<i>'.$langs->trans("AvailableVariables").':<br>';
  374. foreach ($substitutionarray as $key => $val) {
  375. $htmltext .= $key.'<br>';
  376. }
  377. $htmltext .= '</i>';
  378. print '<tr class="oddeven"><td colspan="2">';
  379. print $form->textwithpicto($langs->trans("FreeLegalTextOnExpenseReports"), $langs->trans("AddCRIfTooLong").'<br><br>'.$htmltext, 1, 'help', '', 0, 2, 'freetexttooltip').'<br>';
  380. $variablename = 'EXPENSEREPORT_FREE_TEXT';
  381. if (empty($conf->global->PDF_ALLOW_HTML_FOR_FREE_TEXT)) {
  382. print '<textarea name="'.$variablename.'" class="flat" cols="120">'.getDolGlobalString($variablename).'</textarea>';
  383. } else {
  384. include_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  385. $doleditor = new DolEditor($variablename, getDolGlobalString($variablename), '', 80, 'dolibarr_notes');
  386. print $doleditor->Create();
  387. }
  388. print '</td></tr>'."\n";
  389. // Use draft Watermark
  390. print '<tr class="oddeven"><td colspan="2">';
  391. print $form->textwithpicto($langs->trans("WatermarkOnDraftExpenseReports"), $htmltext, 1, 'help', '', 0, 2, 'watermarktooltip').'<br>';
  392. print '<input class="flat minwidth200" type="text" name="EXPENSEREPORT_DRAFT_WATERMARK" value="'.dol_escape_htmltag(getDolGlobalString('EXPENSEREPORT_DRAFT_WATERMARK')).'">';
  393. print '</td></tr>'."\n";
  394. if (!empty($conf->project->enabled)) {
  395. print '<tr class="oddeven"><td>';
  396. print $langs->trans('ProjectIsRequiredOnExpenseReports');
  397. print '</td><td class="right">';
  398. print $form->selectyesno('EXPENSEREPORT_PROJECT_IS_REQUIRED', empty($conf->global->EXPENSEREPORT_PROJECT_IS_REQUIRED) ? 0 : 1, 1);
  399. print '</td></tr>';
  400. }
  401. print '<tr class="oddeven"><td>';
  402. print $langs->trans('PrefillExpenseReportDatesWithCurrentMonth');
  403. print '</td><td class="right">';
  404. print $form->selectyesno('EXPENSEREPORT_PREFILL_DATES_WITH_CURRENT_MONTH', empty($conf->global->EXPENSEREPORT_PREFILL_DATES_WITH_CURRENT_MONTH) ? 0 : 1, 1);
  405. print '</td></tr>';
  406. print '<tr class="oddeven"><td>';
  407. print $langs->trans('ForceExpenseReportsLineAmountsIncludingTaxesOnly');
  408. print '</td><td class="right">';
  409. print $form->selectyesno('EXPENSEREPORT_FORCE_LINE_AMOUNTS_INCLUDING_TAXES_ONLY', empty($conf->global->EXPENSEREPORT_FORCE_LINE_AMOUNTS_INCLUDING_TAXES_ONLY) ? 0 : 1, 1);
  410. print '</td></tr>';
  411. print '</table>';
  412. print $form->buttonsSaveCancel("Save", '');
  413. print '</form>';
  414. print dol_get_fiche_end();
  415. // End of page
  416. llxFooter();
  417. $db->close();