lines.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <?php
  2. /* Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2022 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2014-2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  5. * Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2014 Juanjo Menent <jmenent@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 <https://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/accountancy/expensereport/lines.php
  23. * \ingroup Accountancy (Double entries)
  24. * \brief Page of detail of the lines of ventilation of expense reports
  25. */
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
  31. require_once DOL_DOCUMENT_ROOT.'/user/class/user.class.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array("compta", "bills", "other", "accountancy", "trips", "productbatch", "hrm"));
  36. $optioncss = GETPOST('optioncss', 'aZ'); // Option for the css output (always '' except when 'print')
  37. $account_parent = GETPOST('account_parent', 'int');
  38. $changeaccount = GETPOST('changeaccount');
  39. // Search Getpost
  40. $search_login = GETPOST('search_login', 'alpha');
  41. $search_expensereport = GETPOST('search_expensereport', 'alpha');
  42. $search_label = GETPOST('search_label', 'alpha');
  43. $search_desc = GETPOST('search_desc', 'alpha');
  44. $search_amount = GETPOST('search_amount', 'alpha');
  45. $search_account = GETPOST('search_account', 'alpha');
  46. $search_vat = GETPOST('search_vat', 'alpha');
  47. $search_date_startday = GETPOST('search_date_startday', 'int');
  48. $search_date_startmonth = GETPOST('search_date_startmonth', 'int');
  49. $search_date_startyear = GETPOST('search_date_startyear', 'int');
  50. $search_date_endday = GETPOST('search_date_endday', 'int');
  51. $search_date_endmonth = GETPOST('search_date_endmonth', 'int');
  52. $search_date_endyear = GETPOST('search_date_endyear', 'int');
  53. $search_date_start = dol_mktime(0, 0, 0, $search_date_startmonth, $search_date_startday, $search_date_startyear); // Use tzserver
  54. $search_date_end = dol_mktime(23, 59, 59, $search_date_endmonth, $search_date_endday, $search_date_endyear);
  55. // Load variable for pagination
  56. $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : (empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION) ? $conf->liste_limit : $conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  57. $sortfield = GETPOST('sortfield', 'aZ09comma');
  58. $sortorder = GETPOST('sortorder', 'aZ09comma');
  59. $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
  60. if (empty($page) || $page < 0) {
  61. $page = 0;
  62. }
  63. $offset = $limit * $page;
  64. $pageprev = $page - 1;
  65. $pagenext = $page + 1;
  66. if (!$sortfield) {
  67. $sortfield = "erd.date, erd.rowid";
  68. }
  69. if (!$sortorder) {
  70. if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_DONE > 0) {
  71. $sortorder = "DESC";
  72. }
  73. }
  74. // Security check
  75. if (!isModEnabled('accounting')) {
  76. accessforbidden();
  77. }
  78. if ($user->socid > 0) {
  79. accessforbidden();
  80. }
  81. if (empty($user->rights->accounting->mouvements->lire)) {
  82. accessforbidden();
  83. }
  84. $formaccounting = new FormAccounting($db);
  85. /*
  86. * Actions
  87. */
  88. // Purge search criteria
  89. if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // Both test are required to be compatible with all browsers
  90. $search_login = '';
  91. $search_expensereport = '';
  92. $search_label = '';
  93. $search_desc = '';
  94. $search_amount = '';
  95. $search_account = '';
  96. $search_vat = '';
  97. $search_date_startday = '';
  98. $search_date_startmonth = '';
  99. $search_date_startyear = '';
  100. $search_date_endday = '';
  101. $search_date_endmonth = '';
  102. $search_date_endyear = '';
  103. $search_date_start = '';
  104. $search_date_end = '';
  105. }
  106. if (is_array($changeaccount) && count($changeaccount) > 0 && $user->rights->accounting->bind->write) {
  107. $error = 0;
  108. if (!(GETPOST('account_parent', 'int') >= 0)) {
  109. $error++;
  110. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Account")), null, 'errors');
  111. }
  112. if (!$error) {
  113. $db->begin();
  114. $sql1 = "UPDATE ".MAIN_DB_PREFIX."expensereport_det as erd";
  115. $sql1 .= " SET erd.fk_code_ventilation=".(GETPOST('account_parent', 'int') > 0 ? GETPOST('account_parent', 'int') : '0');
  116. $sql1 .= ' WHERE erd.rowid IN ('.$db->sanitize(implode(',', $changeaccount)).')';
  117. dol_syslog('accountancy/expensereport/lines.php::changeaccount sql= '.$sql1);
  118. $resql1 = $db->query($sql1);
  119. if (!$resql1) {
  120. $error++;
  121. setEventMessages($db->lasterror(), null, 'errors');
  122. }
  123. if (!$error) {
  124. $db->commit();
  125. setEventMessages($langs->trans("Save"), null, 'mesgs');
  126. } else {
  127. $db->rollback();
  128. setEventMessages($db->lasterror(), null, 'errors');
  129. }
  130. $account_parent = ''; // Protection to avoid to mass apply it a second time
  131. }
  132. }
  133. /*
  134. * View
  135. */
  136. $form = new Form($db);
  137. $formother = new FormOther($db);
  138. llxHeader('', $langs->trans("ExpenseReportsVentilation").' - '.$langs->trans("Dispatched"));
  139. print '<script type="text/javascript">
  140. $(function () {
  141. $(\'#select-all\').click(function(event) {
  142. // Iterate each checkbox
  143. $(\':checkbox\').each(function() {
  144. this.checked = true;
  145. });
  146. });
  147. $(\'#unselect-all\').click(function(event) {
  148. // Iterate each checkbox
  149. $(\':checkbox\').each(function() {
  150. this.checked = false;
  151. });
  152. });
  153. });
  154. </script>';
  155. /*
  156. * Expense reports lines
  157. */
  158. $sql = "SELECT er.ref, er.rowid as erid,";
  159. $sql .= " erd.rowid, erd.fk_c_type_fees, erd.comments, erd.total_ht, erd.fk_code_ventilation, erd.tva_tx, erd.vat_src_code, erd.date,";
  160. $sql .= " f.id as type_fees_id, f.code as type_fees_code, f.label as type_fees_label,";
  161. $sql .= " u.rowid as userid, u.login, u.lastname, u.firstname, u.email, u.gender, u.employee, u.photo, u.statut,";
  162. $sql .= " aa.label, aa.labelshort, aa.account_number";
  163. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as er";
  164. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."expensereport_det as erd ON er.rowid = erd.fk_expensereport";
  165. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
  166. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_type_fees as f ON f.id = erd.fk_c_type_fees";
  167. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."user as u ON u.rowid = er.fk_user_author";
  168. $sql .= " WHERE erd.fk_code_ventilation > 0";
  169. $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
  170. $sql .= " AND er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.")";
  171. // Add search filter like
  172. if (strlen(trim($search_login))) {
  173. $sql .= natural_search("u.login", $search_login);
  174. }
  175. if (strlen(trim($search_expensereport))) {
  176. $sql .= natural_search("er.ref", $search_expensereport);
  177. }
  178. if (strlen(trim($search_label))) {
  179. $sql .= natural_search("f.label", $search_label);
  180. }
  181. if (strlen(trim($search_desc))) {
  182. $sql .= natural_search("er.comments", $search_desc);
  183. }
  184. if (strlen(trim($search_amount))) {
  185. $sql .= natural_search("erd.total_ht", $search_amount, 1);
  186. }
  187. if (strlen(trim($search_account))) {
  188. $sql .= natural_search("aa.account_number", $search_account);
  189. }
  190. if (strlen(trim($search_vat))) {
  191. $sql .= natural_search("erd.tva_tx", price2num($search_vat), 1);
  192. }
  193. if ($search_date_start) {
  194. $sql .= " AND erd.date >= '".$db->idate($search_date_start)."'";
  195. }
  196. if ($search_date_end) {
  197. $sql .= " AND erd.date <= '".$db->idate($search_date_end)."'";
  198. }
  199. $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
  200. $sql .= $db->order($sortfield, $sortorder);
  201. // Count total nb of records
  202. $nbtotalofrecords = '';
  203. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
  204. $result = $db->query($sql);
  205. $nbtotalofrecords = $db->num_rows($result);
  206. if (($page * $limit) > $nbtotalofrecords) { // if total resultset is smaller then paging size (filtering), goto and load page 0
  207. $page = 0;
  208. $offset = 0;
  209. }
  210. }
  211. $sql .= $db->plimit($limit + 1, $offset);
  212. dol_syslog("accountancy/expensereport/lines.php", LOG_DEBUG);
  213. $result = $db->query($sql);
  214. if ($result) {
  215. $num_lines = $db->num_rows($result);
  216. $i = 0;
  217. $param = '';
  218. if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
  219. $param .= '&contextpage='.urlencode($contextpage);
  220. }
  221. if ($limit > 0 && $limit != $conf->liste_limit) {
  222. $param .= '&limit='.urlencode($limit);
  223. }
  224. if ($search_login) {
  225. $param .= '&search_login='.urlencode($search_login);
  226. }
  227. if ($search_expensereport) {
  228. $param .= "&search_expensereport=".urlencode($search_expensereport);
  229. }
  230. if ($search_label) {
  231. $param .= "&search_label=".urlencode($search_label);
  232. }
  233. if ($search_desc) {
  234. $param .= "&search_desc=".urlencode($search_desc);
  235. }
  236. if ($search_account) {
  237. $param .= "&search_account=".urlencode($search_account);
  238. }
  239. if ($search_vat) {
  240. $param .= "&search_vat=".urlencode($search_vat);
  241. }
  242. if ($search_date_startday) {
  243. $param .= '&search_date_startday='.urlencode($search_date_startday);
  244. }
  245. if ($search_date_startmonth) {
  246. $param .= '&search_date_startmonth='.urlencode($search_date_startmonth);
  247. }
  248. if ($search_date_startyear) {
  249. $param .= '&search_date_startyear='.urlencode($search_date_startyear);
  250. }
  251. if ($search_date_endday) {
  252. $param .= '&search_date_endday='.urlencode($search_date_endday);
  253. }
  254. if ($search_date_endmonth) {
  255. $param .= '&search_date_endmonth='.urlencode($search_date_endmonth);
  256. }
  257. if ($search_date_endyear) {
  258. $param .= '&search_date_endyear='.urlencode($search_date_endyear);
  259. }
  260. print '<form action="'.$_SERVER["PHP_SELF"].'" method="post">'."\n";
  261. print '<input type="hidden" name="action" value="ventil">';
  262. if ($optioncss != '') {
  263. print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  264. }
  265. print '<input type="hidden" name="token" value="'.newToken().'">';
  266. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  267. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  268. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  269. print '<input type="hidden" name="page" value="'.$page.'">';
  270. print_barre_liste($langs->trans("ExpenseReportLinesDone"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num_lines, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
  271. print '<span class="opacitymedium">'.$langs->trans("DescVentilDoneExpenseReport").'</span><br>';
  272. print '<br><div class="inline-block divButAction paddingbottom">'.$langs->trans("ChangeAccount").' ';
  273. print $formaccounting->select_account($account_parent, 'account_parent', 2, array(), 0, 0, 'maxwidth300 maxwidthonsmartphone valignmiddle');
  274. print '<input type="submit" class="button small valignmiddle" value="'.$langs->trans("ChangeBinding").'"/></div>';
  275. $moreforfilter = '';
  276. print '<div class="div-table-responsive">';
  277. print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
  278. print '<tr class="liste_titre_filter">';
  279. print '<td class="liste_titre"><input type="text" name="search_login" class="maxwidth50" value="'.$search_login.'"></td>';
  280. print '<td class="liste_titre"></td>';
  281. print '<td><input type="text" class="flat maxwidth50" name="search_expensereport" value="'.dol_escape_htmltag($search_expensereport).'"></td>';
  282. if (!empty($conf->global->ACCOUNTANCY_USE_EXPENSE_REPORT_VALIDATION_DATE)) {
  283. print '<td class="liste_titre"></td>';
  284. }
  285. print '<td class="liste_titre center">';
  286. print '<div class="nowrap">';
  287. print $form->selectDate($search_date_start ? $search_date_start : -1, 'search_date_start', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('From'));
  288. print '</div>';
  289. print '<div class="nowrap">';
  290. print $form->selectDate($search_date_end ? $search_date_end : -1, 'search_date_end', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('to'));
  291. print '</div>';
  292. print '</td>';
  293. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_label" value="'.dol_escape_htmltag($search_label).'"></td>';
  294. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_desc" value="'.dol_escape_htmltag($search_desc).'"></td>';
  295. print '<td class="liste_titre right"><input type="text" class="flat maxwidth50" name="search_amount" value="'.dol_escape_htmltag($search_amount).'"></td>';
  296. print '<td class="liste_titre center"><input type="text" class="flat maxwidth50" name="search_vat" size="1" placeholder="%" value="'.dol_escape_htmltag($search_vat).'"></td>';
  297. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_account" value="'.dol_escape_htmltag($search_account).'"></td>';
  298. print '<td class="liste_titre center">';
  299. $searchpicto = $form->showFilterButtons();
  300. print $searchpicto;
  301. print '</td>';
  302. print "</tr>\n";
  303. print '<tr class="liste_titre">';
  304. print_liste_field_titre("Employees", $_SERVER['PHP_SELF'], "u.login", $param, "", "", $sortfield, $sortorder);
  305. print_liste_field_titre("LineId", $_SERVER["PHP_SELF"], "erd.rowid", "", $param, '', $sortfield, $sortorder);
  306. print_liste_field_titre("ExpenseReport", $_SERVER["PHP_SELF"], "er.ref", "", $param, '', $sortfield, $sortorder);
  307. if (!empty($conf->global->ACCOUNTANCY_USE_EXPENSE_REPORT_VALIDATION_DATE)) {
  308. print_liste_field_titre("DateValidation", $_SERVER["PHP_SELF"], "er.date_valid", "", $param, '', $sortfield, $sortorder, 'center ');
  309. }
  310. print_liste_field_titre("DateOfLine", $_SERVER["PHP_SELF"], "erd.date, erd.rowid", "", $param, '', $sortfield, $sortorder, 'center ');
  311. print_liste_field_titre("TypeFees", $_SERVER["PHP_SELF"], "f.label", "", $param, '', $sortfield, $sortorder);
  312. print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "erd.comments", "", $param, '', $sortfield, $sortorder);
  313. print_liste_field_titre("Amount", $_SERVER["PHP_SELF"], "erd.total_ht", "", $param, '', $sortfield, $sortorder, 'right ');
  314. print_liste_field_titre("VATRate", $_SERVER["PHP_SELF"], "erd.tva_tx", "", $param, '', $sortfield, $sortorder, 'center ');
  315. print_liste_field_titre("AccountAccounting", $_SERVER["PHP_SELF"], "aa.account_number", "", $param, '', $sortfield, $sortorder);
  316. $checkpicto = $form->showCheckAddButtons();
  317. print_liste_field_titre($checkpicto, '', '', '', '', '', '', '', 'center ');
  318. print "</tr>\n";
  319. $expensereportstatic = new ExpenseReport($db);
  320. $accountingaccountstatic = new AccountingAccount($db);
  321. $userstatic = new User($db);
  322. $i = 0;
  323. while ($i < min($num_lines, $limit)) {
  324. $objp = $db->fetch_object($result);
  325. $expensereportstatic->ref = $objp->ref;
  326. $expensereportstatic->id = $objp->erid;
  327. $userstatic->id = $objp->userid;
  328. $userstatic->ref = $objp->label;
  329. $userstatic->login = $objp->login;
  330. $userstatic->statut = $objp->statut;
  331. $userstatic->email = $objp->email;
  332. $userstatic->gender = $objp->gender;
  333. $userstatic->firstname = $objp->firstname;
  334. $userstatic->lastname = $objp->lastname;
  335. $userstatic->employee = $objp->employee;
  336. $userstatic->photo = $objp->photo;
  337. $accountingaccountstatic->rowid = $objp->fk_compte;
  338. $accountingaccountstatic->label = $objp->label;
  339. $accountingaccountstatic->labelshort = $objp->labelshort;
  340. $accountingaccountstatic->account_number = $objp->account_number;
  341. print '<tr class="oddeven">';
  342. // Login
  343. print '<td class="nowraponall">';
  344. print $userstatic->getNomUrl(-1, '', 0, 0, 24, 1, 'login', '', 1);
  345. print '</td>';
  346. // Line id
  347. print '<td>'.$objp->rowid.'</td>';
  348. // Ref Expense report
  349. print '<td>'.$expensereportstatic->getNomUrl(1).'</td>';
  350. // Date validation
  351. if (!empty($conf->global->ACCOUNTANCY_USE_EXPENSE_REPORT_VALIDATION_DATE)) {
  352. print '<td class="center">'.dol_print_date($db->jdate($objp->date_valid), 'day').'</td>';
  353. }
  354. print '<td class="center">'.dol_print_date($db->jdate($objp->date), 'day').'</td>';
  355. // Fees label
  356. print '<td class="tdoverflow">'.($langs->trans($objp->type_fees_code) == $objp->type_fees_code ? $objp->type_fees_label : $langs->trans(($objp->type_fees_code))).'</td>';
  357. // Fees description -- Can be null
  358. print '<td>';
  359. $text = dolGetFirstLineOfText(dol_string_nohtmltag($objp->comments, 1));
  360. $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION) ? 32 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION;
  361. print $form->textwithtooltip(dol_trunc($text, $trunclength), $objp->comments);
  362. print '</td>';
  363. // Amount without taxes
  364. print '<td class="right nowraponall amount">'.price($objp->total_ht).'</td>';
  365. // Vat rate
  366. print '<td class="center">'.vatrate($objp->tva_tx.($objp->vat_src_code ? ' ('.$objp->vat_src_code.')' : '')).'</td>';
  367. // Accounting account affected
  368. print '<td>';
  369. print $accountingaccountstatic->getNomUrl(0, 1, 1, '', 1);
  370. print ' <a class="editfielda reposition marginleftonly marginrightonly" href="./card.php?id='.$objp->rowid.'&backtopage='.urlencode($_SERVER["PHP_SELF"].($param ? '?'.$param : '')).'">';
  371. print img_edit();
  372. print '</a></td>';
  373. print '<td class="center"><input type="checkbox" class="checkforaction" name="changeaccount[]" value="'.$objp->rowid.'"/></td>';
  374. print "</tr>";
  375. $i++;
  376. }
  377. if ($num_lines == 0) {
  378. $colspan=10;
  379. if (!empty($conf->global->ACCOUNTANCY_USE_EXPENSE_REPORT_VALIDATION_DATE)) {
  380. $colspan++;
  381. }
  382. print '<tr><td colspan="'.$colspan.'"><span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span></td></tr>';
  383. }
  384. print "</table>";
  385. print "</div>";
  386. if ($nbtotalofrecords > $limit) {
  387. print_barre_liste('', $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num_lines, $nbtotalofrecords, '', 0, '', '', $limit, 1);
  388. }
  389. print '</form>';
  390. } else {
  391. print $db->lasterror();
  392. }
  393. // End of page
  394. llxFooter();
  395. $db->close();