1<?php
2/* Copyright (C) 2003		Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2012	Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2004		Eric Seigne          <eric.seigne@ryxeo.com>
5 * Copyright (C) 2005-2011	Regis Houssin        <regis.houssin@inodbox.com>
6 * Copyright (C) 2012		Juanjo Menent        <jmenent@2byte.es>
7 * Copyright (C) 2018           charlene Benke	     <charlie@patas-monkey.com>
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 <https://www.gnu.org/licenses/>.
21 */
22
23/**
24 *  \file       htdocs/compta/deplacement/list.php
25 *  \brief      Page to list trips and expenses
26 */
27
28require '../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/deplacement/class/deplacement.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
33
34// Load translation files required by the page
35$langs->loadLangs(array('companies', 'users', 'trips'));
36
37// Security check
38$socid = GETPOST('socid', 'int');
39if ($user->socid) {
40	$socid = $user->socid;
41}
42$result = restrictedArea($user, 'deplacement', '', '');
43
44$search_ref = GETPOST('search_ref', 'int');
45$search_name = GETPOST('search_name', 'alpha');
46$search_company = GETPOST('search_company', 'alpha');
47// $search_amount=GETPOST('search_amount','alpha');
48$sortfield = GETPOST("sortfield", 'alpha');
49$sortorder = GETPOST("sortorder", 'alpha');
50$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
51$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
52if (empty($page) || $page == -1) {
53	$page = 0;
54}     // If $page is not defined, or '' or -1
55$offset = $limit * $page;
56$pageprev = $page - 1;
57$pagenext = $page + 1;
58if (!$sortorder) {
59	$sortorder = "DESC";
60}
61if (!$sortfield) {
62	$sortfield = "d.dated";
63}
64
65$year = GETPOST("year");
66$month = GETPOST("month");
67$day = GETPOST("day");
68
69if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { // Both test are required to be compatible with all browsers
70	$search_ref = "";
71	$search_name = "";
72	$search_company = "";
73	// $search_amount="";
74	$year = "";
75	$month = "";
76	$day = "";
77}
78
79/*
80 * View
81 */
82
83$formother = new FormOther($db);
84$tripandexpense_static = new Deplacement($db);
85$userstatic = new User($db);
86
87$childids = $user->getAllChildIds();
88$childids[] = $user->id;
89
90llxHeader();
91
92$sql = "SELECT s.nom, d.fk_user, s.rowid as socid,"; // Ou
93$sql .= " d.rowid, d.type, d.dated as dd, d.km,"; // Comment
94$sql .= " d.fk_statut,";
95$sql .= " u.lastname, u.firstname"; // Qui
96$sql .= " FROM ".MAIN_DB_PREFIX."user as u";
97$sql .= ", ".MAIN_DB_PREFIX."deplacement as d";
98$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as s ON d.fk_soc = s.rowid";
99if (!$user->rights->societe->client->voir && !$socid) {
100	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe_commerciaux as sc ON s.rowid = sc.fk_soc";
101}
102$sql .= " WHERE d.fk_user = u.rowid";
103$sql .= " AND d.entity = ".$conf->entity;
104if (empty($user->rights->deplacement->readall) && empty($user->rights->deplacement->lire_tous)) {
105	$sql .= ' AND d.fk_user IN ('.$db->sanitize(join(',', $childids)).')';
106}
107if (!$user->rights->societe->client->voir && !$socid) {
108	$sql .= " AND (sc.fk_user = ".((int) $user->id)." OR d.fk_soc IS NULL) ";
109}
110if ($socid) {
111	$sql .= " AND s.rowid = ".((int) $socid);
112}
113
114if ($search_ref) {
115	$sql .= " AND d.rowid = ".((int) $search_ref);
116}
117if ($search_name) {
118	$sql .= natural_search('u.lastname', $search_name);
119}
120if ($search_company) {
121	$sql .= natural_search('s.nom', $search_company);
122}
123$sql .= dolSqlDateFilter("d.dated", $day, $month, $year);
124
125$sql .= $db->order($sortfield, $sortorder);
126$sql .= $db->plimit($limit + 1, $offset);
127
128//print $sql;
129$resql = $db->query($sql);
130if ($resql) {
131	$num = $db->num_rows($resql);
132
133	print_barre_liste($langs->trans("ListOfFees"), $page, $_SERVER["PHP_SELF"], "&socid=$socid", $sortfield, $sortorder, '', $num);
134
135	$i = 0;
136	print '<form method="get" action="'.$_SERVER["PHP_SELF"].'">'."\n";
137	print '<table class="noborder centpercent">';
138	print "<tr class=\"liste_titre\">";
139	print_liste_field_titre("Ref", $_SERVER["PHP_SELF"], "d.rowid", "", "&socid=$socid", '', $sortfield, $sortorder);
140	print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "d.type", "", "&socid=$socid", '', $sortfield, $sortorder);
141	print_liste_field_titre("Date", $_SERVER["PHP_SELF"], "d.dated", "", "&socid=$socid", 'align="center"', $sortfield, $sortorder);
142	print_liste_field_titre("Person", $_SERVER["PHP_SELF"], "u.lastname", "", "&socid=$socid", '', $sortfield, $sortorder);
143	print_liste_field_titre("Company", $_SERVER["PHP_SELF"], "s.nom", "", "&socid=$socid", '', $sortfield, $sortorder);
144	print_liste_field_titre("FeesKilometersOrAmout", $_SERVER["PHP_SELF"], "d.km", "", "&socid=$socid", 'class="right"', $sortfield, $sortorder);
145	print_liste_field_titre('');
146	print "</tr>\n";
147
148	// Filters lines
149	print '<tr class="liste_titre">';
150	print '<td class="liste_titre">';
151	print '<input class="flat" size="4" type="text" name="search_ref" value="'.$search_ref.'">';
152	print '</td>';
153	print '<td class="liste_titre">';
154	print '&nbsp;';
155	print '</td>';
156	print '<td class="liste_titre" align="center">';
157	if (!empty($conf->global->MAIN_LIST_FILTER_ON_DAY)) {
158		print '<input class="flat" type="text" size="1" maxlength="2" name="day" value="'.$day.'">';
159	}
160	print '<input class="flat" type="text" size="1" maxlength="2" name="month" value="'.$month.'">';
161	$formother->select_year($year ? $year : -1, 'year', 1, 20, 5);
162	print '</td>';
163	print '<td class="liste_titre">';
164	print '<input class="flat" size="10" type="text" name="search_name" value="'.$search_name.'">';
165	print '</td>';
166	print '<td class="liste_titre">';
167	print '<input class="flat" size="10" type="text" name="search_company" value="'.$search_company.'">';
168	print '</td>';
169	print '<td class="liste_titre right">';
170	// print '<input class="flat" size="10" type="text" name="search_amount" value="'.$search_amount.'">';
171	print '</td>';
172	print '<td class="liste_titre maxwidthsearch">';
173	$searchpicto = $form->showFilterAndCheckAddButtons(0);
174	print $searchpicto;
175	print '</td>';
176	print "</tr>\n";
177
178	while ($i < min($num, $limit)) {
179		$obj = $db->fetch_object($resql);
180
181		$soc = new Societe($db);
182		if ($obj->socid) {
183			$soc->fetch($obj->socid);
184		}
185
186		print '<tr class="oddeven">';
187		// Id
188		print '<td><a href="card.php?id='.$obj->rowid.'">'.img_object($langs->trans("ShowTrip"), "trip").' '.$obj->rowid.'</a></td>';
189		// Type
190		print '<td>'.$langs->trans($obj->type).'</td>';
191		// Date
192		print '<td class="center">'.dol_print_date($db->jdate($obj->dd), 'day').'</td>';
193		// User
194		print '<td>';
195		$userstatic->id = $obj->fk_user;
196		$userstatic->lastname = $obj->lastname;
197		$userstatic->firstname = $obj->firstname;
198		print $userstatic->getNomUrl(1);
199		print '</td>';
200
201		if ($obj->socid) {
202			print '<td>'.$soc->getNomUrl(1).'</td>';
203		} else {
204			print '<td>&nbsp;</td>';
205		}
206
207		print '<td class="right">'.$obj->km.'</td>';
208
209		$tripandexpense_static->statut = $obj->fk_statut;
210		print '<td class="right">'.$tripandexpense_static->getLibStatut(5).'</td>';
211		print "</tr>\n";
212
213		$i++;
214	}
215
216	print "</table>";
217	print "</form>\n";
218	$db->free($resql);
219} else {
220	dol_print_error($db);
221}
222
223// End of page
224llxFooter();
225$db->close();
226