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) 2004     	Eric Seigne          <eric.seigne@ryxeo.com>
5 * Copyright (C) 2005-2009	Regis Houssin        <regis.houssin@inodbox.com>
6 * Copyright (C) 2015       Alexandre Spangaro   <aspangaro@open-dsi.fr>
7 * Copyright (C) 2018       Ferran Marcet	     <fmarcet@2byte.es>
8 * Copyright (C) 2018       Charlene Benke       <charlie@patas-monkey.com>
9 * Copyright (C) 2019       Juanjo Menent		 <jmenent@2byte.es>
10 * Copyright (C) 2019-2021  Frédéric France      <frederic.france@netlogic.fr>
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
24 */
25
26/**
27 *	    \file       htdocs/expensereport/list.php
28 *      \ingroup    expensereport
29 *		\brief      list of expense reports
30 */
31
32require '../main.inc.php';
33require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/class/html.formfile.class.php';
37require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
38require_once DOL_DOCUMENT_ROOT.'/core/class/html.formexpensereport.class.php';
39require_once DOL_DOCUMENT_ROOT.'/core/lib/usergroups.lib.php';
40require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport_ik.class.php';
41
42// Load translation files required by the page
43$langs->loadLangs(array('companies', 'users', 'trips'));
44
45$action      = GETPOST('action', 'aZ09');
46$massaction  = GETPOST('massaction', 'alpha');
47$show_files  = GETPOST('show_files', 'int');
48$confirm     = GETPOST('confirm', 'alpha');
49$cancel      = GETPOST('cancel', 'alpha'); // We click on a Cancel button
50$toselect    = GETPOST('toselect', 'array');
51$contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'expensereportlist';
52
53$childids = $user->getAllChildIds(1);
54
55// Security check
56$socid = GETPOST('socid', 'int');
57if ($user->socid) {
58	$socid = $user->socid;
59}
60$result = restrictedArea($user, 'expensereport', '', '');
61$id = GETPOST('id', 'int');
62// If we are on the view of a specific user
63if ($id > 0) {
64	$canread = 0;
65	if ($id == $user->id) {
66		$canread = 1;
67	}
68	if (!empty($user->rights->expensereport->readall)) {
69		$canread = 1;
70	}
71	if (!empty($user->rights->expensereport->lire) && in_array($id, $childids)) {
72		$canread = 1;
73	}
74	if (!$canread) {
75		accessforbidden();
76	}
77}
78
79$diroutputmassaction = $conf->expensereport->dir_output.'/temp/massgeneration/'.$user->id;
80
81
82// Load variable for pagination
83$limit 		= GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
84$sortfield	= GETPOST('sortfield', 'aZ09comma');
85$sortorder	= GETPOST('sortorder', 'aZ09comma');
86$page 		= GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
87if (empty($page) || $page == -1) {
88	$page = 0;
89}     // If $page is not defined, or '' or -1
90$offset = $limit * $page;
91$pageprev = $page - 1;
92$pagenext = $page + 1;
93if (!$sortorder) {
94	$sortorder = "DESC";
95}
96if (!$sortfield) {
97	$sortfield = "d.date_debut";
98}
99
100
101$sall			= trim((GETPOST('search_all', 'alphanohtml') != '') ?GETPOST('search_all', 'alphanohtml') : GETPOST('sall', 'alphanohtml'));
102
103$search_ref			= GETPOST('search_ref', 'alpha');
104$search_user		= GETPOST('search_user', 'int');
105$search_amount_ht	= GETPOST('search_amount_ht', 'alpha');
106$search_amount_vat	= GETPOST('search_amount_vat', 'alpha');
107$search_amount_ttc	= GETPOST('search_amount_ttc', 'alpha');
108$search_status		= (GETPOST('search_status', 'intcomma') != '' ?GETPOST('search_status', 'intcomma') : GETPOST('statut', 'intcomma'));
109
110$search_date_startday		= GETPOST('search_date_startday', 'int');
111$search_date_startmonth		= GETPOST('search_date_startmonth', 'int');
112$search_date_startyear		= GETPOST('search_date_startyear', 'int');
113$search_date_startendday	= GETPOST('search_date_startendday', 'int');
114$search_date_startendmonth	= GETPOST('search_date_startendmonth', 'int');
115$search_date_startendyear	= GETPOST('search_date_startendyear', 'int');
116$search_date_start			= dol_mktime(0, 0, 0, $search_date_startmonth, $search_date_startday, $search_date_startyear);	// Use tzserver
117$search_date_startend		= dol_mktime(23, 59, 59, $search_date_startendmonth, $search_date_startendday, $search_date_startendyear);
118
119$search_date_endday			= GETPOST('search_date_endday', 'int');
120$search_date_endmonth		= GETPOST('search_date_endmonth', 'int');
121$search_date_endyear		= GETPOST('search_date_endyear', 'int');
122$search_date_endendday		= GETPOST('search_date_endendday', 'int');
123$search_date_endendmonth	= GETPOST('search_date_endendmonth', 'int');
124$search_date_endendyear		= GETPOST('search_date_endendyear', 'int');
125$search_date_end			= dol_mktime(0, 0, 0, $search_date_endmonth, $search_date_endday, $search_date_endyear);	// Use tzserver
126$search_date_endend			= dol_mktime(23, 59, 59, $search_date_endendmonth, $search_date_endendday, $search_date_endendyear);
127
128$optioncss    = GETPOST('optioncss', 'alpha');
129
130if ($search_status == '') {
131	$search_status = -1;
132}
133if ($search_user == '') {
134	$search_user = -1;
135}
136
137// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
138$object = new ExpenseReport($db);
139$hookmanager->initHooks(array('expensereportlist'));
140$extrafields = new ExtraFields($db);
141
142// fetch optionals attributes and labels
143$extrafields->fetch_name_optionals_label($object->table_element);
144
145$search_array_options = $extrafields->getOptionalsFromPost($object->table_element, '', 'search_');
146
147
148// List of fields to search into when doing a "search in all"
149$fieldstosearchall = array(
150	'd.ref'=>'Ref',
151	'd.note_public'=>"NotePublic",
152	'u.lastname'=>'EmployeeLastname',
153	'u.firstname'=>"EmployeeFirstname",
154	'u.login'=>"Login",
155);
156if (empty($user->socid)) {
157	$fieldstosearchall["d.note_private"] = "NotePrivate";
158}
159
160$arrayfields = array(
161	'd.ref'=>array('label'=>$langs->trans("Ref"), 'checked'=>1),
162	'user'=>array('label'=>$langs->trans("User"), 'checked'=>1),
163	'd.date_debut'=>array('label'=>$langs->trans("DateStart"), 'checked'=>1),
164	'd.date_fin'=>array('label'=>$langs->trans("DateEnd"), 'checked'=>1),
165	'd.date_valid'=>array('label'=>$langs->trans("DateValidation"), 'checked'=>1),
166	'd.date_approve'=>array('label'=>$langs->trans("DateApprove"), 'checked'=>1),
167	'd.total_ht'=>array('label'=>$langs->trans("AmountHT"), 'checked'=>1),
168	'd.total_vat'=>array('label'=>$langs->trans("AmountVAT"), 'checked'=>1),
169	'd.total_ttc'=>array('label'=>$langs->trans("AmountTTC"), 'checked'=>1),
170	'd.date_create'=>array('label'=>$langs->trans("DateCreation"), 'checked'=>0, 'position'=>500),
171	'd.tms'=>array('label'=>$langs->trans("DateModificationShort"), 'checked'=>0, 'position'=>500),
172	'd.fk_statut'=>array('label'=>$langs->trans("Status"), 'checked'=>1, 'position'=>1000),
173);
174// Extra fields
175include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_array_fields.tpl.php';
176
177$canedituser = (!empty($user->admin) || $user->rights->user->user->creer);
178
179$objectuser = new User($db);
180
181
182/*
183 * Actions
184 */
185
186if (GETPOST('cancel', 'alpha')) {
187	$action = 'list'; $massaction = '';
188}
189if (!GETPOST('confirmmassaction', 'alpha') && $massaction != 'presend' && $massaction != 'confirm_presend') {
190	$massaction = '';
191}
192
193$parameters = array('socid'=>$socid);
194$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
195if ($reshook < 0) {
196	setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
197}
198
199if (empty($reshook)) {
200	// Selection of new fields
201	include DOL_DOCUMENT_ROOT.'/core/actions_changeselectedfields.inc.php';
202
203	// Purge search criteria
204	if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // All tests are required to be compatible with all browsers
205		$search_ref = "";
206		$search_user = "";
207		$search_amount_ht = "";
208		$search_amount_vat = "";
209		$search_amount_ttc = "";
210		$search_status = "";
211		$search_date_startday = '';
212		$search_date_startmonth = '';
213		$search_date_startyear = '';
214		$search_date_startendday = '';
215		$search_date_startendmonth = '';
216		$search_date_startendyear = '';
217		$search_date_start = '';
218		$search_date_startend = '';
219		$search_date_endday = '';
220		$search_date_endmonth = '';
221		$search_date_endyear = '';
222		$search_date_endendday = '';
223		$search_date_endendmonth = '';
224		$search_date_endendyear = '';
225		$search_date_end = '';
226		$search_date_endend = '';
227		$toselect = '';
228		$search_array_options = array();
229	}
230	if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')
231		|| GETPOST('button_search_x', 'alpha') || GETPOST('button_search.x', 'alpha') || GETPOST('button_search', 'alpha')) {
232		$massaction = ''; // Protection to avoid mass action if we force a new search during a mass action confirmation
233	}
234
235	// Mass actions
236	$objectclass = 'ExpenseReport';
237	$objectlabel = 'ExpenseReport';
238	$permissiontoread = $user->rights->expensereport->lire;
239	$permissiontodelete = $user->rights->expensereport->supprimer;
240	$uploaddir = $conf->expensereport->dir_output;
241	include DOL_DOCUMENT_ROOT.'/core/actions_massactions.inc.php';
242}
243
244
245/*
246 * View
247 */
248
249$form = new Form($db);
250$formother = new FormOther($db);
251$formfile = new FormFile($db);
252$formexpensereport = new FormExpenseReport($db);
253
254$fuser = new User($db);
255
256$title = $langs->trans("ListOfTrips");
257llxHeader('', $title);
258
259$max_year = 5;
260$min_year = 10;
261
262// Get current user id
263$user_id = $user->id;
264
265if ($id > 0) {
266	// Charge utilisateur edite
267	$fuser->fetch($id, '', '', 1);
268	$fuser->getrights();
269	$user_id = $fuser->id;
270
271	$search_user = $user_id;
272}
273
274$sql = "SELECT d.rowid, d.ref, d.fk_user_author, d.total_ht, d.total_tva, d.total_ttc, d.fk_statut as status,";
275$sql .= " d.date_debut, d.date_fin, d.date_create, d.tms as date_modif, d.date_valid, d.date_approve, d.note_private, d.note_public,";
276$sql .= " u.rowid as id_user, u.firstname, u.lastname, u.login, u.email, u.statut, u.photo";
277// Add fields from extrafields
278if (!empty($extrafields->attributes[$object->table_element]['label'])) {
279	foreach ($extrafields->attributes[$object->table_element]['label'] as $key => $val) {
280		$sql .= ($extrafields->attributes[$object->table_element]['type'][$key] != 'separate' ? ", ef.".$key.' as options_'.$key : '');
281	}
282}
283// Add fields from hooks
284$parameters = array();
285$reshook = $hookmanager->executeHooks('printFieldListSelect', $parameters); // Note that $action and $object may have been modified by hook
286$sql .= $hookmanager->resPrint;
287$sql .= " FROM ".MAIN_DB_PREFIX."expensereport as d";
288if (is_array($extrafields->attributes[$object->table_element]['label']) && count($extrafields->attributes[$object->table_element]['label'])) {
289	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX.$object->table_element."_extrafields as ef on (d.rowid = ef.fk_object)";
290}
291$sql .= ", ".MAIN_DB_PREFIX."user as u";
292$sql .= " WHERE d.fk_user_author = u.rowid AND d.entity IN (".getEntity('expensereport').")";
293// Search all
294if (!empty($sall)) {
295	$sql .= natural_search(array_keys($fieldstosearchall), $sall);
296}
297// Ref
298if (!empty($search_ref)) {
299	$sql .= natural_search('d.ref', $search_ref);
300}
301// Date Start
302if ($search_date_start) {
303	$sql .= " AND d.date_debut >= '".$db->idate($search_date_start)."'";
304}
305if ($search_date_startend) {
306	$sql .= " AND d.date_debut <= '".$db->idate($search_date_startend)."'";
307}
308// Date End
309if ($search_date_end) {
310	$sql .= " AND d.date_fin >= '".$db->idate($search_date_end)."'";
311}
312if ($search_date_endend) {
313	$sql .= " AND d.date_fin <= '".$db->idate($search_date_endend)."'";
314}
315
316if ($search_amount_ht != '') {
317	$sql .= natural_search('d.total_ht', $search_amount_ht, 1);
318}
319if ($search_amount_ttc != '') {
320	$sql .= natural_search('d.total_ttc', $search_amount_ttc, 1);
321}
322// User
323if ($search_user != '' && $search_user >= 0) {
324	$sql .= " AND u.rowid = '".$db->escape($search_user)."'";
325}
326// Status
327if ($search_status != '' && $search_status >= 0) {
328	$sql .= " AND d.fk_statut IN (".$db->sanitize($search_status).")";
329}
330// RESTRICT RIGHTS
331if (empty($user->rights->expensereport->readall) && empty($user->rights->expensereport->lire_tous)
332	&& (empty($conf->global->MAIN_USE_ADVANCED_PERMS) || empty($user->rights->expensereport->writeall_advance))) {
333	$sql .= " AND d.fk_user_author IN (".$db->sanitize(join(',', $childids)).")\n";
334}
335// Add where from extra fields
336include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_sql.tpl.php';
337// Add where from hooks
338$parameters = array();
339$reshook = $hookmanager->executeHooks('printFieldListWhere', $parameters); // Note that $action and $object may have been modified by hook
340$sql .= $hookmanager->resPrint;
341
342$sql .= $db->order($sortfield, $sortorder);
343
344// Count total nb of records
345$nbtotalofrecords = '';
346if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
347	$result = $db->query($sql);
348	$nbtotalofrecords = $db->num_rows($result);
349	if (($page * $limit) > $nbtotalofrecords) {	// if total resultset is smaller then paging size (filtering), goto and load page 0
350		$page = 0;
351		$offset = 0;
352	}
353}
354
355$sql .= $db->plimit($limit + 1, $offset);
356
357//print $sql;
358$resql = $db->query($sql);
359if ($resql) {
360	$num = $db->num_rows($resql);
361
362	$arrayofselected = is_array($toselect) ? $toselect : array();
363
364	$param = '';
365	if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
366		$param .= '&contextpage='.urlencode($contextpage);
367	}
368	if ($limit > 0 && $limit != $conf->liste_limit) {
369		$param .= '&limit='.urlencode($limit);
370	}
371	if ($sall) {
372		$param .= "&sall=".urlencode($sall);
373	}
374	if ($search_ref) {
375		$param .= "&search_ref=".urlencode($search_ref);
376	}
377	// Start date
378	if ($search_date_startday) {
379		$param .= '&search_date_startday='.urlencode($search_date_startday);
380	}
381	if ($search_date_startmonth) {
382		$param .= '&search_date_startmonth='.urlencode($search_date_startmonth);
383	}
384	if ($search_date_startyear) {
385		$param .= '&search_date_startyear='.urlencode($search_date_startyear);
386	}
387	if ($search_date_startendday) {
388		$param .= '&search_date_startendday='.urlencode($search_date_startendday);
389	}
390	if ($search_date_startendmonth) {
391		$param .= '&search_date_startendmonth='.urlencode($search_date_startendmonth);
392	}
393	if ($search_date_startendyear) {
394		$param .= '&search_date_startendyear='.urlencode($search_date_startendyear);
395	}
396	// End date
397	if ($search_date_endday) {
398		$param .= '&search_date_endday='.urlencode($search_date_endday);
399	}
400	if ($search_date_endmonth) {
401		$param .= '&search_date_endmonth='.urlencode($search_date_endmonth);
402	}
403	if ($search_date_endyear) {
404		$param .= '&search_date_endyear='.urlencode($search_date_endyear);
405	}
406	if ($search_date_endendday) {
407		$param .= '&search_date_endendday='.urlencode($search_date_endendday);
408	}
409	if ($search_date_endendmonth) {
410		$param .= '&search_date_endendmonth='.urlencode($search_date_endendmonth);
411	}
412	if ($search_date_endendyear) {
413		$param .= '&search_date_endendyear='.urlencode($search_date_endendyear);
414	}
415	if ($search_user) {
416		$param .= "&search_user=".urlencode($search_user);
417	}
418	if ($search_amount_ht) {
419		$param .= "&search_amount_ht=".urlencode($search_amount_ht);
420	}
421	if ($search_amount_ttc) {
422		$param .= "&search_amount_ttc=".urlencode($search_amount_ttc);
423	}
424	if ($search_status >= 0) {
425		$param .= "&search_status=".urlencode($search_status);
426	}
427	if ($optioncss != '') {
428		$param .= '&optioncss='.urlencode($optioncss);
429	}
430	// Add $param from extra fields
431	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_param.tpl.php';
432
433	// List of mass actions available
434	$arrayofmassactions = array(
435		'generate_doc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("ReGeneratePDF"),
436		'builddoc'=>img_picto('', 'pdf', 'class="pictofixedwidth"').$langs->trans("PDFMerge"),
437		'presend'=>img_picto('', 'email', 'class="pictofixedwidth"').$langs->trans("SendByMail"),
438	);
439	if ($user->rights->expensereport->supprimer) {
440		$arrayofmassactions['predelete'] = img_picto('', 'delete', 'class="pictofixedwidth"').$langs->trans("Delete");
441	}
442	if (in_array($massaction, array('presend', 'predelete'))) {
443		$arrayofmassactions = array();
444	}
445	$massactionbutton = $form->selectMassAction('', $arrayofmassactions);
446
447	// Lines of title fields
448	print '<form id="searchFormList" action="'.$_SERVER["PHP_SELF"].'" method="POST">'."\n";
449	if ($optioncss != '') {
450		print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
451	}
452	print '<input type="hidden" name="token" value="'.newToken().'">';
453	print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
454	print '<input type="hidden" name="action" value="'.($action == 'edit' ? 'update' : 'list').'">';
455	print '<input type="hidden" name="contextpage" value="'.$contextpage.'">';
456	print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
457	print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
458	if ($id > 0) {
459		print '<input type="hidden" name="id" value="'.$id.'">';
460	}
461
462	if ($id > 0) {		// For user tab
463		$title = $langs->trans("User");
464		$linkback = '<a href="'.DOL_URL_ROOT.'/user/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
465		$head = user_prepare_head($fuser);
466
467		print dol_get_fiche_head($head, 'expensereport', $title, -1, 'user');
468
469		dol_banner_tab($fuser, 'id', $linkback, $user->rights->user->user->lire || $user->admin);
470
471		print dol_get_fiche_end();
472
473		if ($action != 'edit') {
474			print '<div class="tabsAction">';
475
476			$childids = $user->getAllChildIds(1);
477
478			$canedit = ((in_array($user_id, $childids) && $user->rights->expensereport->creer)
479				|| ($conf->global->MAIN_USE_ADVANCED_PERMS && $user->rights->expensereport->writeall_advance));
480
481			// Buttons for actions
482			if ($canedit) {
483				print '<a href="'.DOL_URL_ROOT.'/expensereport/card.php?action=create&fk_user_author='.$fuser->id.'" class="butAction">'.$langs->trans("AddTrip").'</a>';
484			} else {
485				print '<a href="#" class="butActionRefused" title="'.$langs->trans("NotEnoughPermission").'">'.$langs->trans("AddTrip").'</a>';
486			}
487
488			print '</div>';
489		} else {
490			print '<div class="center">';
491			print '<input type="submit" class="button button-save" name="save" value="'.$langs->trans("Save").'">';
492			print '</div><br>';
493		}
494	} else {
495		$title = $langs->trans("ListTripsAndExpenses");
496
497		$url = DOL_URL_ROOT.'/expensereport/card.php?action=create';
498		if (!empty($socid)) {
499			$url .= '&socid='.$socid;
500		}
501		$newcardbutton = dolGetButtonTitle($langs->trans('NewTrip'), '', 'fa fa-plus-circle', $url, '', $user->rights->expensereport->creer);
502
503		print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $massactionbutton, $num, $nbtotalofrecords, 'trip', 0, $newcardbutton, '', $limit, 0, 0, 1);
504	}
505
506	$topicmail = "SendExpenseReport";
507	$modelmail = "expensereport";
508	$objecttmp = new ExpenseReport($db);
509	$trackid = 'exp'.$object->id;
510	include DOL_DOCUMENT_ROOT.'/core/tpl/massactions_pre.tpl.php';
511
512	if ($sall) {
513		foreach ($fieldstosearchall as $key => $val) {
514			$fieldstosearchall[$key] = $langs->trans($val);
515		}
516		print '<div class="divsearchfieldfilter">'.$langs->trans("FilterOnInto", $sall).join(', ', $fieldstosearchall).'</div>';
517	}
518
519	$moreforfilter = '';
520
521	$parameters = array();
522	$reshook = $hookmanager->executeHooks('printFieldPreListTitle', $parameters); // Note that $action and $object may have been modified by hook
523	if (empty($reshook)) {
524		$moreforfilter .= $hookmanager->resPrint;
525	} else {
526		$moreforfilter = $hookmanager->resPrint;
527	}
528
529	if (!empty($moreforfilter)) {
530		print '<div class="liste_titre liste_titre_bydiv centpercent">';
531		print $moreforfilter;
532		print '</div>';
533	}
534
535	$varpage = empty($contextpage) ? $_SERVER["PHP_SELF"] : $contextpage;
536	$selectedfields = $form->multiSelectArrayWithCheckbox('selectedfields', $arrayfields, $varpage); // This also change content of $arrayfields
537	$selectedfields .= (count($arrayofmassactions) ? $form->showCheckAddButtons('checkforselect', 1) : '');
538
539	print '<div class="div-table-responsive">';
540	print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
541
542	// Filters
543	print '<tr class="liste_titre_filter">';
544	if (!empty($arrayfields['d.ref']['checked'])) {
545		print '<td class="liste_titre" align="left">';
546		print '<input class="flat" size="15" type="text" name="search_ref" value="'.$search_ref.'">';
547		print '</td>';
548	}
549	// User
550	if (!empty($arrayfields['user']['checked'])) {
551		if ($user->rights->expensereport->readall || $user->rights->expensereport->lire_tous) {
552			print '<td class="liste_titre maxwidthonspartphone" align="left">';
553			print $form->select_dolusers($search_user, 'search_user', 1, '', 0, '', '', 0, 0, 0, '', 0, '', 'maxwidth200');
554			print '</td>';
555		} else {
556			print '<td class="liste_titre">&nbsp;</td>';
557		}
558	}
559	// Date start
560	if (!empty($arrayfields['d.date_debut']['checked'])) {
561		print '<td class="liste_titre" align="center">';
562		print '<div class="nowrap">';
563		print $form->selectDate($search_date_start ? $search_date_start : -1, 'search_date_start', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('From'));
564		print '</div>';
565		print '<div class="nowrap">';
566		print $form->selectDate($search_date_startend ? $search_date_startend : -1, 'search_date_startend', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('to'));
567		print '</div>';
568		print '</td>';
569	}
570	// Date end
571	if (!empty($arrayfields['d.date_fin']['checked'])) {
572		print '<td class="liste_titre" align="center">';
573		print '<div class="nowrap">';
574		print $form->selectDate($search_date_end ? $search_date_end : -1, 'search_date_end', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('From'));
575		print '</div>';
576		print '<div class="nowrap">';
577		print $form->selectDate($search_date_endend ? $search_date_endend : -1, 'search_date_endend', 0, 0, 1, '', 1, 0, 0, '', '', '', '', 1, '', $langs->trans('to'));
578		print '</div>';
579		print '</td>';
580	}
581	// Date valid
582	if (!empty($arrayfields['d.date_valid']['checked'])) {
583		print '<td class="liste_titre" align="center">';
584		//print '<input class="flat" type="text" size="1" maxlength="2" name="month_end" value="'.$month_end.'">';
585		//$formother->select_year($year_end,'year_end',1, $min_year, $max_year);
586		print '</td>';
587	}
588	// Date approve
589	if (!empty($arrayfields['d.date_approve']['checked'])) {
590		print '<td class="liste_titre" align="center">';
591		//print '<input class="flat" type="text" size="1" maxlength="2" name="month_end" value="'.$month_end.'">';
592		//$formother->select_year($year_end,'year_end',1, $min_year, $max_year);
593		print '</td>';
594	}
595	// Amount with no tax
596	if (!empty($arrayfields['d.total_ht']['checked'])) {
597		print '<td class="liste_titre right"><input class="flat" type="text" size="5" name="search_amount_ht" value="'.$search_amount_ht.'"></td>';
598	}
599	if (!empty($arrayfields['d.total_vat']['checked'])) {
600		print '<td class="liste_titre right"><input class="flat" type="text" size="5" name="search_amount_vat" value="'.$search_amount_vat.'"></td>';
601	}
602	// Amount with all taxes
603	if (!empty($arrayfields['d.total_ttc']['checked'])) {
604		print '<td class="liste_titre right"><input class="flat" type="text" size="5" name="search_amount_ttc" value="'.$search_amount_ttc.'"></td>';
605	}
606	// Extra fields
607	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_input.tpl.php';
608
609	// Fields from hook
610	$parameters = array('arrayfields'=>$arrayfields);
611	$reshook = $hookmanager->executeHooks('printFieldListOption', $parameters); // Note that $action and $object may have been modified by hook
612	print $hookmanager->resPrint;
613	// Date creation
614	if (!empty($arrayfields['d.date_create']['checked'])) {
615		print '<td class="liste_titre">';
616		print '</td>';
617	}
618	// Date modification
619	if (!empty($arrayfields['d.tms']['checked'])) {
620		print '<td class="liste_titre">';
621		print '</td>';
622	}
623	// Status
624	if (!empty($arrayfields['d.fk_statut']['checked'])) {
625		print '<td class="liste_titre right">';
626		$formexpensereport->selectExpensereportStatus($search_status, 'search_status', 1, 1);
627		print '</td>';
628	}
629	// Action column
630	print '<td class="liste_titre maxwidthsearch">';
631	$searchpicto = $form->showFilterButtons();
632	print $searchpicto;
633	print '</td>';
634
635	print "</tr>\n";
636
637	print '<tr class="liste_titre">';
638	if (!empty($arrayfields['d.ref']['checked'])) {
639		print_liste_field_titre($arrayfields['d.ref']['label'], $_SERVER["PHP_SELF"], "d.ref", "", $param, '', $sortfield, $sortorder);
640	}
641	if (!empty($arrayfields['user']['checked'])) {
642		print_liste_field_titre($arrayfields['user']['label'], $_SERVER["PHP_SELF"], "u.lastname", "", $param, '', $sortfield, $sortorder);
643	}
644	if (!empty($arrayfields['d.date_debut']['checked'])) {
645		print_liste_field_titre($arrayfields['d.date_debut']['label'], $_SERVER["PHP_SELF"], "d.date_debut", "", $param, 'align="center"', $sortfield, $sortorder);
646	}
647	if (!empty($arrayfields['d.date_fin']['checked'])) {
648		print_liste_field_titre($arrayfields['d.date_fin']['label'], $_SERVER["PHP_SELF"], "d.date_fin", "", $param, 'align="center"', $sortfield, $sortorder);
649	}
650	if (!empty($arrayfields['d.date_valid']['checked'])) {
651		print_liste_field_titre($arrayfields['d.date_valid']['label'], $_SERVER["PHP_SELF"], "d.date_valid", "", $param, 'align="center"', $sortfield, $sortorder);
652	}
653	if (!empty($arrayfields['d.date_approve']['checked'])) {
654		print_liste_field_titre($arrayfields['d.date_approve']['label'], $_SERVER["PHP_SELF"], "d.date_approve", "", $param, 'align="center"', $sortfield, $sortorder);
655	}
656	if (!empty($arrayfields['d.total_ht']['checked'])) {
657		print_liste_field_titre($arrayfields['d.total_ht']['label'], $_SERVER["PHP_SELF"], "d.total_ht", "", $param, 'align="right"', $sortfield, $sortorder);
658	}
659	if (!empty($arrayfields['d.total_vat']['checked'])) {
660		print_liste_field_titre($arrayfields['d.total_vat']['label'], $_SERVER["PHP_SELF"], "d.total_tva", "", $param, 'align="right"', $sortfield, $sortorder);
661	}
662	if (!empty($arrayfields['d.total_ttc']['checked'])) {
663		print_liste_field_titre($arrayfields['d.total_ttc']['label'], $_SERVER["PHP_SELF"], "d.total_ttc", "", $param, 'align="right"', $sortfield, $sortorder);
664	}
665	// Extra fields
666	include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_search_title.tpl.php';
667	// Hook fields
668	$parameters = array('arrayfields'=>$arrayfields, 'param'=>$param, 'sortfield'=>$sortfield, 'sortorder'=>$sortorder);
669	$reshook = $hookmanager->executeHooks('printFieldListTitle', $parameters); // Note that $action and $object may have been modified by hook
670	print $hookmanager->resPrint;
671	if (!empty($arrayfields['d.date_create']['checked'])) {
672		print_liste_field_titre($arrayfields['d.date_create']['label'], $_SERVER["PHP_SELF"], "d.date_create", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
673	}
674	if (!empty($arrayfields['d.tms']['checked'])) {
675		print_liste_field_titre($arrayfields['d.tms']['label'], $_SERVER["PHP_SELF"], "d.tms", "", $param, 'align="center" class="nowrap"', $sortfield, $sortorder);
676	}
677	if (!empty($arrayfields['d.fk_statut']['checked'])) {
678		print_liste_field_titre($arrayfields['d.fk_statut']['label'], $_SERVER["PHP_SELF"], "d.fk_statut", "", $param, 'align="right"', $sortfield, $sortorder);
679	}
680	print_liste_field_titre($selectedfields, $_SERVER["PHP_SELF"], "", '', '', 'align="center"', $sortfield, $sortorder, 'maxwidthsearch ');
681	print "</tr>\n";
682
683	$total_total_ht = 0;
684	$total_total_ttc = 0;
685	$total_total_tva = 0;
686
687	$expensereportstatic = new ExpenseReport($db);
688	$usertmp = new User($db);
689
690	if ($num > 0) {
691		$i = 0;
692		$totalarray = array();
693		$totalarray['nbfield'] = 0;
694		$totalarray['val'] = array();
695		$totalarray['val']['d.total_ht'] = 0;
696		$totalarray['val']['d.total_tva'] = 0;
697		$totalarray['val']['d.total_ttc'] = 0;
698		$totalarray['totalizable'] = array();
699		while ($i < min($num, $limit)) {
700			$obj = $db->fetch_object($resql);
701
702			$expensereportstatic->id = $obj->rowid;
703			$expensereportstatic->ref = $obj->ref;
704			$expensereportstatic->status = $obj->status;
705			$expensereportstatic->date_debut = $db->jdate($obj->date_debut);
706			$expensereportstatic->date_fin = $db->jdate($obj->date_fin);
707			$expensereportstatic->date_create = $db->jdate($obj->date_create);
708			$expensereportstatic->date_modif = $db->jdate($obj->date_modif);
709			$expensereportstatic->date_valid = $db->jdate($obj->date_valid);
710			$expensereportstatic->date_approve = $db->jdate($obj->date_approve);
711			$expensereportstatic->note_private = $obj->note_private;
712			$expensereportstatic->note_public = $obj->note_public;
713
714
715			print '<tr class="oddeven">';
716			// Ref
717			if (!empty($arrayfields['d.ref']['checked'])) {
718				print '<td>';
719				print '<table class="nobordernopadding"><tr class="nocellnopadd">';
720				print '<td class="nobordernopadding nowrap">';
721				print $expensereportstatic->getNomUrl(1);
722				print '</td>';
723				// Warning late icon and note
724				print '<td class="nobordernopadding nowrap">';
725				if ($expensereportstatic->status == 2 && $expensereportstatic->hasDelay('toappove')) {
726					print img_warning($langs->trans("Late"));
727				}
728				if ($expensereportstatic->status == 5 && $expensereportstatic->hasDelay('topay')) {
729					print img_warning($langs->trans("Late"));
730				}
731				if (!empty($obj->note_private) || !empty($obj->note_public)) {
732					print ' <span class="note">';
733					print '<a href="'.DOL_URL_ROOT.'/expensereport/note.php?id='.$obj->rowid.'">'.img_picto($langs->trans("ViewPrivateNote"), 'object_generic').'</a>';
734					print '</span>';
735				}
736				print '</td>';
737				print '<td width="16" class="nobordernopadding hideonsmartphone right">';
738				$filename = dol_sanitizeFileName($obj->ref);
739				$filedir = $conf->expensereport->dir_output.'/'.dol_sanitizeFileName($obj->ref);
740				$urlsource = $_SERVER['PHP_SELF'].'?id='.$obj->rowid;
741				print $formfile->getDocumentsLink($expensereportstatic->element, $filename, $filedir);
742				print '</td>';
743				print '</tr></table>';
744				print '</td>';
745				if (!$i) {
746					$totalarray['nbfield']++;
747				}
748			}
749			// User
750			if (!empty($arrayfields['user']['checked'])) {
751				print '<td class="left">';
752				$usertmp->id = $obj->id_user;
753				$usertmp->lastname = $obj->lastname;
754				$usertmp->firstname = $obj->firstname;
755				$usertmp->login = $obj->login;
756				$usertmp->statut = $obj->statut;
757				$usertmp->photo = $obj->photo;
758				$usertmp->email = $obj->email;
759				print $usertmp->getNomUrl(-1);
760				print '</td>';
761				if (!$i) {
762					$totalarray['nbfield']++;
763				}
764			}
765			// Start date
766			if (!empty($arrayfields['d.date_debut']['checked'])) {
767				print '<td class="center">'.($obj->date_debut > 0 ? dol_print_date($db->jdate($obj->date_debut), 'day') : '').'</td>';
768				if (!$i) {
769					$totalarray['nbfield']++;
770				}
771			}
772			// End date
773			if (!empty($arrayfields['d.date_fin']['checked'])) {
774				print '<td class="center">'.($obj->date_fin > 0 ? dol_print_date($db->jdate($obj->date_fin), 'day') : '').'</td>';
775				if (!$i) {
776					$totalarray['nbfield']++;
777				}
778			}
779			// Date validation
780			if (!empty($arrayfields['d.date_valid']['checked'])) {
781				print '<td class="center">'.($obj->date_valid > 0 ? dol_print_date($db->jdate($obj->date_valid), 'day') : '').'</td>';
782				if (!$i) {
783					$totalarray['nbfield']++;
784				}
785			}
786			// Date approval
787			if (!empty($arrayfields['d.date_approve']['checked'])) {
788				print '<td class="center">'.($obj->date_approve > 0 ? dol_print_date($db->jdate($obj->date_approve), 'day') : '').'</td>';
789				if (!$i) {
790					$totalarray['nbfield']++;
791				}
792			}
793			// Amount HT
794			if (!empty($arrayfields['d.total_ht']['checked'])) {
795				  print '<td class="right">'.price($obj->total_ht)."</td>\n";
796				if (!$i) {
797					$totalarray['nbfield']++;
798				}
799				if (!$i) {
800					$totalarray['pos'][$totalarray['nbfield']] = 'd.total_ht';
801				}
802				  $totalarray['val']['d.total_ht'] += $obj->total_ht;
803			}
804			// Amount VAT
805			if (!empty($arrayfields['d.total_vat']['checked'])) {
806				print '<td class="right">'.price($obj->total_tva)."</td>\n";
807				if (!$i) {
808					$totalarray['nbfield']++;
809				}
810				if (!$i) {
811					$totalarray['pos'][$totalarray['nbfield']] = 'd.total_tva';
812				}
813				$totalarray['val']['d.total_tva'] += $obj->total_tva;
814			}
815			// Amount TTC
816			if (!empty($arrayfields['d.total_ttc']['checked'])) {
817				print '<td class="right">'.price($obj->total_ttc)."</td>\n";
818				if (!$i) {
819					$totalarray['nbfield']++;
820				}
821				if (!$i) {
822					$totalarray['pos'][$totalarray['nbfield']] = 'd.total_ttc';
823				}
824				$totalarray['val']['d.total_ttc'] += $obj->total_ttc;
825			}
826
827			// Extra fields
828			include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_list_print_fields.tpl.php';
829			// Fields from hook
830			$parameters = array('arrayfields'=>$arrayfields, 'obj'=>$obj, 'i'=>$i, 'totalarray'=>&$totalarray);
831			$reshook = $hookmanager->executeHooks('printFieldListValue', $parameters); // Note that $action and $object may have been modified by hook
832			print $hookmanager->resPrint;
833
834			// Date creation
835			if (!empty($arrayfields['d.date_create']['checked'])) {
836				print '<td class="nowrap center">';
837				print dol_print_date($db->jdate($obj->date_create), 'dayhour');
838				print '</td>';
839				if (!$i) {
840					$totalarray['nbfield']++;
841				}
842			}
843			// Date modification
844			if (!empty($arrayfields['d.tms']['checked'])) {
845				print '<td class="nowrap center">';
846				print dol_print_date($db->jdate($obj->date_modif), 'dayhour');
847				print '</td>';
848				if (!$i) {
849					$totalarray['nbfield']++;
850				}
851			}
852			// Status
853			if (!empty($arrayfields['d.fk_statut']['checked'])) {
854				print '<td class="nowrap right">'.$expensereportstatic->getLibStatut(5).'</td>';
855				if (!$i) {
856					$totalarray['nbfield']++;
857				}
858			}
859			// Action column
860			print '<td class="nowrap center">';
861			if ($massactionbutton || $massaction) {   // If we are in select mode (massactionbutton defined) or if we have already selected and sent an action ($massaction) defined
862				$selected = 0;
863				if (in_array($obj->rowid, $arrayofselected)) {
864					$selected = 1;
865				}
866				print '<input id="cb'.$obj->rowid.'" class="flat checkforselect" type="checkbox" name="toselect[]" value="'.$obj->rowid.'"'.($selected ? ' checked="checked"' : '').'>';
867			}
868			print '</td>';
869			if (!$i) {
870				$totalarray['nbfield']++;
871			}
872
873			print "</tr>\n";
874
875			$total_total_ht = $total_total_ht + $obj->total_ht;
876			$total_total_tva = $total_total_tva + $obj->total_tva;
877			$total_total_ttc = $total_total_ttc + $obj->total_ttc;
878
879			$i++;
880		}
881	} else {
882		$colspan = 1;
883		foreach ($arrayfields as $key => $val) {
884			if (!empty($val['checked'])) {
885				$colspan++;
886			}
887		}
888		print '<tr><td colspan="'.$colspan.'" class="opacitymedium">'.$langs->trans("NoRecordFound").'</td></tr>';
889	}
890
891	// Show total line
892	include DOL_DOCUMENT_ROOT.'/core/tpl/list_print_total.tpl.php';
893
894	$db->free($resql);
895
896	$parameters = array('arrayfields'=>$arrayfields, 'sql'=>$sql);
897	$reshook = $hookmanager->executeHooks('printFieldListFooter', $parameters); // Note that $action and $object may have been modified by hook
898	print $hookmanager->resPrint;
899
900	print '</table>'."\n";
901	print '</div>';
902
903	print '</form>'."\n";
904
905	if (empty($id)) {
906		$hidegeneratedfilelistifempty = 1;
907		if ($massaction == 'builddoc' || $action == 'remove_file' || $show_files) {
908			$hidegeneratedfilelistifempty = 0;
909		}
910
911		// Show list of available documents
912		$urlsource = $_SERVER['PHP_SELF'].'?sortfield='.$sortfield.'&sortorder='.$sortorder;
913		$urlsource .= str_replace('&amp;', '&', $param);
914
915		$filedir = $diroutputmassaction;
916		$genallowed = $user->rights->expensereport->lire;
917		$delallowed = $user->rights->expensereport->creer;
918
919		print $formfile->showdocuments('massfilesarea_expensereport', '', $filedir, $urlsource, 0, $delallowed, '', 1, 1, 0, 48, 1, $param, $title, '', '', '', null, $hidegeneratedfilelistifempty);
920	}
921} else {
922	dol_print_error($db);
923}
924
925// End of page
926llxFooter();
927$db->close();
928