1<?php
2/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2016 Laurent Destailleur  <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2010 Regis Houssin        <regis.houssin@inodbox.com>
5 * Copyright (C) 2011-2016 Alexandre Spangaro   <aspangaro@open-dsi.fr>
6 * Copyright (C) 2011-2014 Juanjo Menent	<jmenent@2byte.es>
7 * Copyright (C) 2015      Jean-François Ferry	<jfefe@aternatik.fr>
8 * Copyright (C) 2019      Nicolas ZABOURI      <info@inovea-conseil.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
24/**
25 *      \file       htdocs/compta/charges/index.php
26 *      \ingroup    compta
27 *		\brief      Page to list payments of special expenses
28 */
29
30require '../../main.inc.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
32require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
33require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/paymentsocialcontribution.class.php';
34require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
36require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
37require_once DOL_DOCUMENT_ROOT.'/core/class/html.formsocialcontrib.class.php';
38
39
40$hookmanager = new HookManager($db);
41
42// Initialize technical object to manage hooks. Note that conf->hooks_modules contains array
43$hookmanager->initHooks(array('specialexpensesindex'));
44
45// Load translation files required by the page
46$langs->loadLangs(array('compta', 'bills'));
47
48// Security check
49if ($user->socid) $socid = $user->socid;
50$result = restrictedArea($user, 'tax|salaries', '', '', 'charges|');
51
52$year = GETPOST("year", 'int');
53$search_sc_type = GETPOST('search_sc_type', 'int');
54
55$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
56$sortfield = GETPOST("sortfield", 'alpha');
57$sortorder = GETPOST("sortorder", 'alpha');
58$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
59if (empty($page) || $page == -1) { $page = 0; }     // If $page is not defined, or '' or -1
60$offset = $limit * $page;
61$pageprev = $page - 1;
62$pagenext = $page + 1;
63if (!$sortfield) $sortfield = "cs.date_ech";
64if (!$sortorder) $sortorder = "DESC";
65
66if (empty($conf->tax->enabled) || empty($user->rights->tax->charges->lire))
67{
68	accessforbidden();
69}
70
71
72/*
73 * Actions
74 */
75
76// Purge search criteria
77if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter.x', 'alpha') || GETPOST('button_removefilter', 'alpha')) // All tests are required to be compatible with all browsers
78{
79	$search_sc_type = '';
80	//$toselect = '';
81	//$search_array_options = array();
82}
83
84
85/*
86 * View
87 */
88
89$tva_static = new Tva($db);
90$socialcontrib = new ChargeSociales($db);
91$payment_sc_static = new PaymentSocialContribution($db);
92$sal_static = new PaymentSalary($db);
93$accountstatic = new Account($db);
94$formsocialcontrib = new FormSocialContrib($db);
95
96$title = $langs->trans("SocialContributionsPayments");
97
98llxHeader('', $title);
99
100
101$param = '';
102if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) $param .= '&contextpage='.urlencode($contextpage);
103if ($limit > 0 && $limit != $conf->liste_limit) $param .= '&limit='.urlencode($limit);
104if ($sortfield) $param .= '&sortfield='.urlencode($sortfield);
105if ($sortorder) $param .= '&sortorder='.urlencode($sortorder);
106if ($year) $param .= '&year='.urlencode($year);
107if ($search_sc_type) $param .= '&search_sc_type='.urlencode($search_sc_type);
108$num = 0;
109
110print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
111if ($optioncss != '') print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
112print '<input type="hidden" name="token" value="'.newToken().'">';
113print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
114print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
115print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
116print '<input type="hidden" name="page" value="'.$page.'">';
117
118$sql = "SELECT c.id, c.libelle as label,";
119$sql .= " cs.rowid, cs.libelle, cs.fk_type as type, cs.periode, cs.date_ech, cs.amount as total,";
120$sql .= " pc.rowid as pid, pc.datep, pc.amount as totalpaye, pc.num_paiement as num_payment, pc.fk_bank,";
121$sql .= " pct.code as payment_code,";
122$sql .= " ba.rowid as bid, ba.ref as bref, ba.number as bnumber, ba.account_number, ba.fk_accountancy_journal, ba.label as blabel";
123$sql .= " FROM ".MAIN_DB_PREFIX."c_chargesociales as c,";
124$sql .= " ".MAIN_DB_PREFIX."chargesociales as cs";
125$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiementcharge as pc ON pc.fk_charge = cs.rowid";
126$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pct ON pc.fk_typepaiement = pct.id";
127$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON pc.fk_bank = b.rowid";
128$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."bank_account as ba ON b.fk_account = ba.rowid";
129$sql .= " WHERE cs.fk_type = c.id";
130$sql .= " AND cs.entity IN (".getEntity("tax").")";
131if ($search_sc_type > 0) {
132	$sql .= " AND cs.fk_type = ".((int) $search_sc_type);
133}
134if ($year > 0) {
135	$sql .= " AND (";
136	// Si period renseignee on l'utilise comme critere de date, sinon on prend date echeance,
137	// ceci afin d'etre compatible avec les cas ou la periode n'etait pas obligatoire
138	$sql .= "   (cs.periode IS NOT NULL AND cs.periode between '".$db->idate(dol_get_first_day($year))."' AND '".$db->idate(dol_get_last_day($year))."')";
139	$sql .= " OR (cs.periode IS NULL AND cs.date_ech between '".$db->idate(dol_get_first_day($year))."' AND '".$db->idate(dol_get_last_day($year))."')";
140	$sql .= ")";
141}
142if (preg_match('/^cs\./', $sortfield) || preg_match('/^c\./', $sortfield) || preg_match('/^pc\./', $sortfield) || preg_match('/^pct\./', $sortfield)) {
143		$sql .= $db->order($sortfield, $sortorder);
144}
145
146// Count total nb of records
147$nbtotalofrecords = '';
148if (empty($conf->global->MAIN_DISABLE_FULL_SCANLIST)) {
149	$resql = $db->query($sql);
150	$nbtotalofrecords = $db->num_rows($resql);
151	if (($page * $limit) > $nbtotalofrecords) {	// if total of record found is smaller than page * limit, goto and load page 0
152		$page = 0;
153		$offset = 0;
154	}
155}
156// if total of record found is smaller than limit, no need to do paging and to restart another select with limits set.
157if (is_numeric($nbtotalofrecords) && ($limit > $nbtotalofrecords || empty($limit))) {
158	$num = $nbtotalofrecords;
159} else {
160	if ($limit) $sql .= $db->plimit($limit + 1, $offset);
161
162	$resql = $db->query($sql);
163	if (!$resql) {
164		dol_print_error($db);
165		exit;
166	}
167
168	$num = $db->num_rows($resql);
169}
170//$sql.= $db->plimit($limit+1,$offset);
171//print $sql;
172
173$nav = '';
174print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, '', $num, $nbtotalofrecords, 'object_payment', 0, $nav, '', $limit, 0);
175
176print '<table class="noborder centpercent">';
177
178print '<tr class="liste_titre">';
179print '<td class="liste_titre"></td>';
180print '<td class="liste_titre"></td>';
181print '<td class="liste_titre">';
182$formsocialcontrib->select_type_socialcontrib(GETPOSTISSET("search_sc_type") ? $search_sc_type : '', 'search_sc_type', 1, 0, 0, 'minwidth200 maxwidth300');
183print '</td>';
184print '<td class="liste_titre"></td>';
185print '<td class="liste_titre"></td>';
186print '<td class="liste_titre"></td>';
187print '<td class="liste_titre"></td>';
188if (!empty($conf->banque->enabled)) print '<td class="liste_titre"></td>';
189print '<td class="liste_titre"></td>';
190print '<td class="liste_titre center">';
191$searchpicto = $form->showFilterButtons();
192print $searchpicto;
193print '</td>';
194print "</tr>\n";
195
196print '<tr class="liste_titre">';
197print_liste_field_titre("PeriodEndDate", $_SERVER["PHP_SELF"], "cs.date_ech", "", $param, 'width="140px"', $sortfield, $sortorder);
198print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "c.libelle", "", $param, '', $sortfield, $sortorder);
199print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "cs.fk_type", "", $param, '', $sortfield, $sortorder);
200print_liste_field_titre("ExpectedToPay", $_SERVER["PHP_SELF"], "cs.amount", "", $param, 'class="right"', $sortfield, $sortorder);
201print_liste_field_titre("RefPayment", $_SERVER["PHP_SELF"], "pc.rowid", "", $param, '', $sortfield, $sortorder);
202print_liste_field_titre("DatePayment", $_SERVER["PHP_SELF"], "pc.datep", "", $param, 'align="center"', $sortfield, $sortorder);
203print_liste_field_titre("Type", $_SERVER["PHP_SELF"], "pct.code", "", $param, '', $sortfield, $sortorder);
204if (!empty($conf->banque->enabled)) print_liste_field_titre("Account", $_SERVER["PHP_SELF"], "ba.label", "", $param, "", $sortfield, $sortorder);
205print_liste_field_titre("PayedByThisPayment", $_SERVER["PHP_SELF"], "pc.amount", "", $param, 'class="right"', $sortfield, $sortorder);
206print_liste_field_titre('');
207print "</tr>\n";
208
209if (!$resql)
210{
211	dol_print_error($db);
212	exit;
213}
214
215$i = 0;
216$total = 0;
217$totalnb = 0;
218$totalpaye = 0;
219
220while ($i < min($num, $limit)) {
221	$obj = $db->fetch_object($resql);
222	print '<tr class="oddeven">';
223	// Date
224	$date = $obj->periode;
225	if (empty($date)) $date = $obj->date_ech;
226	print '<td>'.dol_print_date($date, 'day').'</td>';
227	// Label
228	print '<td>';
229	$socialcontrib->id = $obj->rowid;
230	$socialcontrib->ref = $obj->label;
231	$socialcontrib->label = $obj->label;
232	print $socialcontrib->getNomUrl(1, '20');
233	print '</td>';
234	// Type
235	print '<td title="'.dol_escape_htmltag($obj->label).'" class="tdmaxoverflow300">'.$obj->label.'</td>';
236	// Expected to pay
237	print '<td class="right">'.price($obj->total).'</td>';
238	// Ref payment
239	$payment_sc_static->id = $obj->pid;
240	$payment_sc_static->ref = $obj->pid;
241	print '<td>'.$payment_sc_static->getNomUrl(1)."</td>\n";
242	// Date payment
243	print '<td class="center">'.dol_print_date($db->jdate($obj->datep), 'day').'</td>';
244	// Type payment
245	print '<td>';
246	if ($obj->payment_code) print $langs->trans("PaymentTypeShort".$obj->payment_code).' ';
247	print $obj->num_payment.'</td>';
248	// Account
249	if (!empty($conf->banque->enabled))
250	{
251		print '<td>';
252		if ($obj->fk_bank > 0)
253		{
254			//$accountstatic->fetch($obj->fk_bank);
255			$accountstatic->id = $obj->bid;
256			$accountstatic->ref = $obj->bref;
257			$accountstatic->number = $obj->bnumber;
258			$accountstatic->accountancy_number = $obj->account_number;
259			$accountstatic->accountancy_journal = $obj->accountancy_journal;
260			$accountstatic->label = $obj->blabel;
261			print $accountstatic->getNomUrl(1);
262		} else print '&nbsp;';
263		print '</td>';
264	}
265	// Paid
266	print '<td class="right">';
267	if ($obj->totalpaye) print price($obj->totalpaye);
268	print '</td>';
269
270	print '<td></td>';
271
272	print '</tr>';
273
274	$total = $total + $obj->total;
275	$totalnb = $totalnb + $obj->nb;
276	$totalpaye = $totalpaye + $obj->totalpaye;
277	$i++;
278}
279
280// Total
281print '<tr class="liste_total"><td colspan="3" class="liste_total">'.$langs->trans("Total").'</td>';
282print '<td class="liste_total right"></td>'; // A total here has no sense
283print '<td align="center" class="liste_total">&nbsp;</td>';
284print '<td align="center" class="liste_total">&nbsp;</td>';
285print '<td align="center" class="liste_total">&nbsp;</td>';
286if (!empty($conf->banque->enabled)) print '<td></td>';
287print '<td class="liste_total right">'.price($totalpaye)."</td>";
288print '<td></td>';
289print "</tr>";
290
291print '</table>';
292
293
294print '</form>';
295
296$parameters = array('user' => $user);
297$reshook = $hookmanager->executeHooks('dashboardSpecialBills', $parameters, $object); // Note that $action and $object may have been modified by hook
298
299// End of page
300llxFooter();
301$db->close();
302