list.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. <?php
  2. /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
  3. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  4. * Copyright (C) 2014-2015 Ari Elbaz (elarifr) <github@accedinfo.com>
  5. * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
  6. * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>s
  7. * Copyright (C) 2016 Laurent Destailleur <eldy@users.sourceforge.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/accountancy/expensereport/list.php
  24. * \ingroup Advanced accountancy
  25. * \brief Ventilation page from expense reports
  26. */
  27. require '../../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT . '/expensereport/class/expensereport.class.php';
  29. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php';
  30. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
  31. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  32. require_once DOL_DOCUMENT_ROOT . '/core/class/html.formother.class.php';
  33. require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  34. // Load translation files required by the page
  35. $langs->loadLangs(array("bills","compta","accountancy","other","trips","productbatch"));
  36. $action=GETPOST('action','alpha');
  37. $massaction=GETPOST('massaction','alpha');
  38. $show_files=GETPOST('show_files','int');
  39. $confirm=GETPOST('confirm','alpha');
  40. $toselect = GETPOST('toselect', 'array');
  41. // Select Box
  42. $mesCasesCochees = GETPOST('toselect', 'array');
  43. // Search Getpost
  44. $search_expensereport = GETPOST('search_expensereport', 'alpha');
  45. $search_label = GETPOST('search_label', 'alpha');
  46. $search_desc = GETPOST('search_desc', 'alpha');
  47. $search_amount = GETPOST('search_amount', 'alpha');
  48. $search_account = GETPOST('search_account', 'alpha');
  49. $search_vat = GETPOST('search_vat', 'alpha');
  50. $search_day=GETPOST("search_day","int");
  51. $search_month=GETPOST("search_month","int");
  52. $search_year=GETPOST("search_year","int");
  53. $btn_ventil = GETPOST('ventil', 'alpha');
  54. // Load variable for pagination
  55. $limit = GETPOST('limit','int')?GETPOST('limit', 'int'):(empty($conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION)?$conf->liste_limit:$conf->global->ACCOUNTING_LIMIT_LIST_VENTILATION);
  56. $sortfield = GETPOST('sortfield', 'alpha');
  57. $sortorder = GETPOST('sortorder', 'alpha');
  58. $page = GETPOST('page','int');
  59. if (empty($page) || $page < 0) { $page = 0; }
  60. $offset = $limit * $page;
  61. $pageprev = $page - 1;
  62. $pagenext = $page + 1;
  63. if (! $sortfield)
  64. $sortfield = "erd.date, erd.rowid";
  65. if (! $sortorder) {
  66. if ($conf->global->ACCOUNTING_LIST_SORT_VENTILATION_TODO > 0) {
  67. $sortorder = "DESC";
  68. }
  69. }
  70. // Security check
  71. if ($user->societe_id > 0)
  72. accessforbidden();
  73. if (! $user->rights->accounting->bind->write)
  74. accessforbidden();
  75. $formaccounting = new FormAccounting($db);
  76. $accounting = new AccountingAccount($db);
  77. $chartaccountcode = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
  78. /*
  79. * Action
  80. */
  81. if (GETPOST('cancel','alpha')) { $action='list'; $massaction=''; }
  82. if (! GETPOST('confirmmassaction','alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') { $massaction=''; }
  83. // Purge search criteria
  84. if (GETPOST('button_removefilter_x','alpha') || GETPOST('button_removefilter.x','alpha') || GETPOST('button_removefilter','alpha')) // All test are required to be compatible with all browsers
  85. {
  86. $search_expensereport = '';
  87. $search_label = '';
  88. $search_desc = '';
  89. $search_amount = '';
  90. $search_account = '';
  91. $search_vat = '';
  92. $search_day = '';
  93. $search_month = '';
  94. $search_year = '';
  95. }
  96. // Mass actions
  97. $objectclass='ExpenseReport';
  98. $objectlabel='ExpenseReport';
  99. $permtoread = $user->rights->expensereport->read;
  100. $permtodelete = $user->rights->expensereport->delete;
  101. $uploaddir = $conf->expensereport->dir_output;
  102. include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
  103. if ($massaction == 'ventil') {
  104. $msg='';
  105. //print '<div><font color="red">' . $langs->trans("Processing") . '...</font></div>';
  106. if (! empty($mesCasesCochees)) {
  107. $msg = '<div>' . $langs->trans("SelectedLines") . ': '.count($mesCasesCochees).'</div>';
  108. $msg.='<div class="detail">';
  109. $mesCodesVentilChoisis = $codeventil;
  110. $cpt = 0;
  111. $ok=0;
  112. $ko=0;
  113. foreach ( $mesCasesCochees as $maLigneCochee ) {
  114. $maLigneCourante = explode("_", $maLigneCochee);
  115. $monId = $maLigneCourante[0];
  116. $monCompte = GETPOST('codeventil'.$monId);
  117. if ($monCompte <= 0)
  118. {
  119. $msg.= '<div><font color="red">' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' - ' . $langs->trans("NoAccountSelected") . '</font></div>';
  120. $ko++;
  121. }
  122. else
  123. {
  124. $sql = " UPDATE " . MAIN_DB_PREFIX . "expensereport_det";
  125. $sql .= " SET fk_code_ventilation = " . $monCompte;
  126. $sql .= " WHERE rowid = " . $monId;
  127. $accountventilated = new AccountingAccount($db);
  128. $accountventilated->fetch($monCompte, '');
  129. dol_syslog('accountancy/expensereport/list.php:: sql=' . $sql, LOG_DEBUG);
  130. if ($db->query($sql)) {
  131. $msg.= '<div><font color="green">' . $langs->trans("LineOfExpenseReport") . ' ' . $monId . ' - ' . $langs->trans("VentilatedinAccount") . ' : ' . length_accountg($accountventilated->account_number) . '</font></div>';
  132. $ok++;
  133. } else {
  134. $msg.= '<div><font color="red">' . $langs->trans("ErrorDB") . ' : ' . $langs->trans("Lineofinvoice") . ' ' . $monId . ' - ' . $langs->trans("NotVentilatedinAccount") . ' : ' . length_accountg($accountventilated->account_number) . '<br/> <pre>' . $sql . '</pre></font></div>';
  135. $ko++;
  136. }
  137. }
  138. $cpt++;
  139. }
  140. $msg.='</div>';
  141. $msg.= '<div>' . $langs->trans("EndProcessing") . '</div>';
  142. //} else {
  143. // setEventMessages($langs->trans("NoRecordSelected"), null, 'warnings');
  144. }
  145. }
  146. /*
  147. * View
  148. */
  149. $form = new Form($db);
  150. $formother = new FormOther($db);
  151. llxHeader('', $langs->trans("ExpenseReportsVentilation"));
  152. if (empty($chartaccountcode))
  153. {
  154. print $langs->trans("ErrorChartOfAccountSystemNotSelected");
  155. // End of page
  156. llxFooter();
  157. $db->close();
  158. exit;
  159. }
  160. // Expense report lines
  161. $sql = "SELECT er.ref, er.rowid as erid, er.date_debut,";
  162. $sql.= " erd.rowid, erd.fk_c_type_fees, erd.comments, erd.total_ht as price, erd.fk_code_ventilation, erd.tva_tx as tva_tx_line, erd.vat_src_code, erd.date,";
  163. $sql.= " f.id as type_fees_id, f.code as type_fees_code, f.label as type_fees_label, f.accountancy_code as code_buy,";
  164. $sql.= " aa.rowid as aarowid";
  165. $sql.= " FROM " . MAIN_DB_PREFIX . "expensereport as er";
  166. $sql.= " INNER JOIN " . MAIN_DB_PREFIX . "expensereport_det as erd ON er.rowid = erd.fk_expensereport";
  167. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "c_type_fees as f ON f.id = erd.fk_c_type_fees";
  168. $sql.= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON f.accountancy_code = aa.account_number AND aa.fk_pcg_version = '" . $chartaccountcode."' AND aa.entity = " . $conf->entity;
  169. $sql.= " WHERE er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.") AND erd.fk_code_ventilation <= 0";
  170. // Add search filter like
  171. if (strlen(trim($search_expensereport))) {
  172. $sql .= natural_search("er.ref",$search_expensereport);
  173. }
  174. if (strlen(trim($search_label))) {
  175. $sql .= natural_search("f.label",$search_label);
  176. }
  177. if (strlen(trim($search_desc))) {
  178. $sql .= natural_search("erd.comments",$search_desc);
  179. }
  180. if (strlen(trim($search_amount))) {
  181. $sql .= natural_search("erd.total_ht",$search_amount,1);
  182. }
  183. if (strlen(trim($search_account))) {
  184. $sql .= natural_search("aa.account_number",$search_account);
  185. }
  186. if (strlen(trim($search_vat))) {
  187. $sql .= natural_search("erd.tva_tx",$search_vat,1);
  188. }
  189. if ($search_month > 0)
  190. {
  191. if ($search_year > 0 && empty($search_day))
  192. $sql.= " AND erd.date BETWEEN '".$db->idate(dol_get_first_day($search_year,$search_month,false))."' AND '".$db->idate(dol_get_last_day($search_year,$search_month,false))."'";
  193. elseif ($search_year > 0 && ! empty($search_day))
  194. $sql.= " AND erd.date BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $search_month, $search_day, $search_year))."' AND '".$db->idate(dol_mktime(23, 59, 59, $search_month, $search_day, $search_year))."'";
  195. else
  196. $sql.= " AND date_format(erd.date, '%m') = '".$db->escape($search_month)."'";
  197. }
  198. elseif ($search_year > 0)
  199. {
  200. $sql.= " AND erd.date BETWEEN '".$db->idate(dol_get_first_day($search_year,1,false))."' AND '".$db->idate(dol_get_last_day($search_year,12,false))."'";
  201. }
  202. $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy
  203. $sql .= $db->order($sortfield, $sortorder);
  204. // Count total nb of records
  205. $nbtotalofrecords = '';
  206. if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST))
  207. {
  208. $result = $db->query($sql);
  209. $nbtotalofrecords = $db->num_rows($result);
  210. if (($page * $limit) > $nbtotalofrecords) // if total resultset is smaller then paging size (filtering), goto and load page 0
  211. {
  212. $page = 0;
  213. $offset = 0;
  214. }
  215. }
  216. $sql .= $db->plimit($limit + 1, $offset);
  217. dol_syslog('accountancy/expensereport/list.php');
  218. $result = $db->query($sql);
  219. if ($result) {
  220. $num_lines = $db->num_rows($result);
  221. $i = 0;
  222. $arrayofselected=is_array($toselect)?$toselect:array();
  223. $param='';
  224. if (! empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param.='&contextpage='.$contextpage;
  225. if ($limit > 0 && $limit != $conf->liste_limit) $param.='&limit='.$limit;
  226. if ($search_lineid) $param.='&search_lineid='.urlencode($search_lineid);
  227. if ($search_day) $param.='&search_day='.urlencode($search_day);
  228. if ($search_month) $param.='&search_month='.urlencode($search_month);
  229. if ($search_year) $param.='&search_year='.urlencode($search_year);
  230. if ($search_expensereport) $param.='&search_expensereport='.urlencode($search_expensereport);
  231. if ($search_label) $param.='&search_label='.urlencode($search_label);
  232. if ($search_desc) $param.='&search_desc='.urlencode($search_desc);
  233. if ($search_amount) $param.='&search_amount='.urlencode($search_amount);
  234. if ($search_vat) $param.='&search_vat='.urlencode($search_vat);
  235. $arrayofmassactions = array(
  236. 'ventil'=>$langs->trans("Ventilate")
  237. //'presend'=>$langs->trans("SendByMail"),
  238. //'builddoc'=>$langs->trans("PDFMerge"),
  239. );
  240. //if ($user->rights->mymodule->supprimer) $arrayofmassactions['predelete']=$langs->trans("Delete");
  241. //if (in_array($massaction, array('presend','predelete'))) $arrayofmassactions=array();
  242. $massactionbutton=$form->selectMassAction('ventil', $arrayofmassactions, 1);
  243. print '<form action="' . $_SERVER["PHP_SELF"] . '" method="post">' . "\n";
  244. print '<input type="hidden" name="action" value="ventil">';
  245. if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
  246. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  247. print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
  248. print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
  249. print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
  250. print '<input type="hidden" name="page" value="'.$page.'">';
  251. print_barre_liste($langs->trans("ExpenseReportLines"), $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num_lines, $nbtotalofrecords, 'title_accountancy', 0, '', '', $limit);
  252. print $langs->trans("DescVentilTodoExpenseReport") . '</br><br>';
  253. /*$topicmail="Information";
  254. $modelmail="project";
  255. $objecttmp=new Project($db);
  256. $trackid='prj'.$object->id;
  257. include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';*/
  258. if ($msg) print $msg.'<br>';
  259. $moreforfilter = '';
  260. print '<div class="div-table-responsive">';
  261. print '<table class="tagtable liste'.($moreforfilter?" listwithfilterbefore":"").'">'."\n";
  262. // We add search filter
  263. print '<tr class="liste_titre_filter">';
  264. print '<td class="liste_titre"></td>';
  265. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_expensereport" value="' . dol_escape_htmltag($search_expensereport) . '"></td>';
  266. print '<td class="liste_titre center">';
  267. if (! empty($conf->global->MAIN_LIST_FILTER_ON_DAY)) print '<input class="flat valignmiddle" type="text" size="1" maxlength="2" name="search_day" value="'.$search_day.'">';
  268. print '<input class="flat valignmiddle" type="text" size="1" maxlength="2" name="search_month" value="'.$search_month.'">';
  269. $formother->select_year($search_year,'search_year',1, 20, 5);
  270. print '</td>';
  271. print '<td class="liste_titre"><input type="text" class="flat maxwidth50" name="search_label" value="' . dol_escape_htmltag($search_label) . '"></td>';
  272. print '<td class="liste_titre"><input type="text" class="flat maxwidthonsmartphone" name="search_desc" value="' . dol_escape_htmltag($search_desc) . '"></td>';
  273. print '<td class="liste_titre" align="right"><input type="text" class="right flat maxwidth50" name="search_amount" value="' . dol_escape_htmltag($search_amount) . '"></td>';
  274. print '<td class="liste_titre" align="right"><input type="text" class="right flat maxwidth50" name="search_vat" placeholder="%" size="1" value="' . dol_escape_htmltag($search_vat) . '"></td>';
  275. print '<td class="liste_titre"></td>';
  276. print '<td class="liste_titre"></td>';
  277. print '<td align="center" class="liste_titre">';
  278. $searchpicto=$form->showFilterButtons();
  279. print $searchpicto;
  280. print '</td>';
  281. print '</tr>';
  282. print '<tr class="liste_titre">';
  283. print_liste_field_titre("LineId", $_SERVER["PHP_SELF"], "erd.rowid", "", $param, '', $sortfield, $sortorder);
  284. print_liste_field_titre("ExpenseReport", $_SERVER["PHP_SELF"], "er.ref", "", $param, '', $sortfield, $sortorder);
  285. print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "erd.date, erd.rowid", "", $param, 'align="center"', $sortfield, $sortorder);
  286. print_liste_field_titre("TypeFees", $_SERVER["PHP_SELF"], "f.label", "", $param, '', $sortfield, $sortorder);
  287. print_liste_field_titre("Description", $_SERVER["PHP_SELF"], "erd.comments", "", $param, '', $sortfield, $sortorder);
  288. print_liste_field_titre("Amount", $_SERVER["PHP_SELF"], "erd.total_ht", "", $param, 'align="right"', $sortfield, $sortorder);
  289. print_liste_field_titre("VATRate", $_SERVER["PHP_SELF"], "erd.tva_tx", "", $param, 'align="right"', $sortfield, $sortorder);
  290. print_liste_field_titre("AccountAccountingSuggest", '', '', '', '', 'align="center"', $sortfield, $sortorder);
  291. print_liste_field_titre("IntoAccount", '', '', '', '', 'align="center"', $sortfield, $sortorder);
  292. $checkpicto='';
  293. if ($massactionbutton) $checkpicto=$form->showCheckAddButtons('checkforselect', 1);
  294. print_liste_field_titre($checkpicto, '', '', '', '', 'align="center"');
  295. print "</tr>\n";
  296. $expensereport_static = new ExpenseReport($db);
  297. $form = new Form($db);
  298. while ( $i < min($num_lines, $limit) ) {
  299. $objp = $db->fetch_object($result);
  300. $objp->aarowid_suggest = '';
  301. $objp->aarowid_suggest = $objp->aarowid;
  302. $expensereport_static->ref = $objp->ref;
  303. $expensereport_static->id = $objp->erid;
  304. print '<tr class="oddeven">';
  305. // Line id
  306. print '<td>' . $objp->rowid . '</td>';
  307. // Ref Expense report
  308. print '<td>' . $expensereport_static->getNomUrl(1) . '</td>';
  309. // Date
  310. print '<td align="center">' . dol_print_date($db->jdate($objp->date), 'day') . '</td>';
  311. // Fees label
  312. print '<td>';
  313. print ($langs->trans($objp->type_fees_code) == $objp->type_fees_code ? $objp->type_fees_label : $langs->trans(($objp->type_fees_code)));
  314. print '</td>';
  315. // Fees description -- Can be null
  316. print '<td>';
  317. $text = dolGetFirstLineOfText(dol_string_nohtmltag($objp->comments));
  318. $trunclength = empty($conf->global->ACCOUNTING_LENGTH_DESCRIPTION) ? 32 : $conf->global->ACCOUNTING_LENGTH_DESCRIPTION;
  319. print $form->textwithtooltip(dol_trunc($text,$trunclength), $objp->comments);
  320. print '</td>';
  321. print '<td class="right">';
  322. print price($objp->price);
  323. print '</td>';
  324. // Vat rate
  325. print '<td class="right">';
  326. print vatrate($objp->tva_tx_line.($objp->vat_src_code?' ('.$objp->vat_src_code.')':''));
  327. print '</td>';
  328. // Current account
  329. print '<td align="center">';
  330. print length_accountg(html_entity_decode($objp->code_buy));
  331. print '</td>';
  332. // Suggested accounting account
  333. print '<td align="center">';
  334. print $formaccounting->select_account($objp->aarowid_suggest, 'codeventil'.$objp->rowid, 1, array(), 0, 0, 'codeventil maxwidth300 maxwidthonsmartphone', 'cachewithshowemptyone');
  335. print '</td>';
  336. print '<td align="center">';
  337. print '<input type="checkbox" class="flat checkforselect checkforselect'.$objp->rowid.'" name="toselect[]" value="' . $objp->rowid . "_" . $i . '"' . ($objp->aarowid ? "checked" : "") . '/>';
  338. print '</td>';
  339. print "</tr>";
  340. $i ++;
  341. }
  342. print '</table>';
  343. print "</div>";
  344. print '</form>';
  345. } else {
  346. print $db->error();
  347. }
  348. // Add code to auto check the box when we select an account
  349. print '<script type="text/javascript" language="javascript">
  350. jQuery(document).ready(function() {
  351. jQuery(".codeventil").change(function() {
  352. var s=$(this).attr("id").replace("codeventil", "")
  353. console.log(s+" "+$(this).val());
  354. if ($(this).val() == -1) jQuery(".checkforselect"+s).prop("checked", false);
  355. else jQuery(".checkforselect"+s).prop("checked", true);
  356. });
  357. });
  358. </script>';
  359. // End of page
  360. llxFooter();
  361. $db->close();