1<?php
2/* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2014 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) 2021       Gauthier VERDOL         <gauthier.verdol@atm-consulting.fr>
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/tva/payments.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/tva/class/tva.class.php';
33require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/paymentvat.class.php';
34require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
35require_once DOL_DOCUMENT_ROOT.'/salaries/class/paymentsalary.class.php';
36require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
37
38// Load translation files required by the page
39$langs->loadLangs(array('compta', 'bills'));
40
41$mode = GETPOST("mode", 'alpha');
42$year = GETPOST("year", 'int');
43$filtre = GETPOST("filtre", 'alpha');
44if (!$year && $mode != 'tvaonly') {
45	$year = date("Y", time());
46}
47
48$limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit;
49$sortfield = GETPOST("sortfield", 'alpha');
50$sortorder = GETPOST("sortorder", 'alpha');
51$page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int');
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 (!$sortfield) {
59	$sortfield = "ptva.datep";
60}
61if (!$sortorder) {
62	$sortorder = "DESC";
63}
64
65$object = new Tva($db);
66
67// Security check
68if ($user->socid) {
69	$socid = $user->socid;
70}
71//$result = restrictedArea($user, 'tax|salaries', '', '', 'charges|');
72$result = restrictedArea($user, 'tax', '', 'tva', 'charges');
73
74
75/*
76 * View
77 */
78
79$tva_static = new Tva($db);
80$tva = new Tva($db);
81$accountlinestatic = new AccountLine($db);
82$payment_vat_static = new PaymentVAT($db);
83$sal_static = new PaymentSalary($db);
84
85llxHeader('', $langs->trans("VATExpensesArea"));
86
87$title = $langs->trans("VATPayments");
88
89$param = '';
90if (!empty($contextpage) && $contextpage != $_SERVER["PHP_SELF"]) {
91	$param .= '&contextpage='.$contextpage;
92}
93if ($limit > 0 && $limit != $conf->liste_limit) {
94	$param .= '&limit='.$limit;
95}
96if ($sortfield) {
97	$param .= '&sortfield='.$sortfield;
98}
99if ($sortorder) {
100	$param .= '&sortorder='.$sortorder;
101}
102
103
104print '<form method="POST" action="'.$_SERVER["PHP_SELF"].'">';
105if ($optioncss != '') {
106	print '<input type="hidden" name="optioncss" value="'.$optioncss.'">';
107}
108print '<input type="hidden" name="token" value="'.newToken().'">';
109print '<input type="hidden" name="formfilteraction" id="formfilteraction" value="list">';
110print '<input type="hidden" name="sortfield" value="'.$sortfield.'">';
111print '<input type="hidden" name="sortorder" value="'.$sortorder.'">';
112print '<input type="hidden" name="page" value="'.$page.'">';
113print '<input type="hidden" name="mode" value="'.$mode.'">';
114
115print_barre_liste($title, $page, $_SERVER["PHP_SELF"], $param, $sortfield, $sortorder, $center, $num, $totalnboflines, 'title_accountancy', 0, '', '', $limit);
116
117if ($year) {
118	$param .= '&year='.$year;
119}
120
121if (!empty($conf->tax->enabled) && $user->rights->tax->charges->lire) {
122	print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
123	print '<table class="noborder centpercent">';
124	print '<tr class="liste_titre">';
125	print_liste_field_titre("RefPayment", $_SERVER["PHP_SELF"], "ptva.rowid", "", $param, '', $sortfield, $sortorder);
126	print_liste_field_titre("VATDeclaration", $_SERVER["PHP_SELF"], "tva.rowid", "", $param, '', $sortfield, $sortorder);
127	print_liste_field_titre("Label", $_SERVER["PHP_SELF"], "tva.label", "", $param, '', $sortfield, $sortorder);
128	print_liste_field_titre("PeriodEndDate", $_SERVER["PHP_SELF"], "tva.datev", "", $param, 'width="140px"', $sortfield, $sortorder);
129	print_liste_field_titre("DatePayment", $_SERVER["PHP_SELF"], "ptva.datep", "", $param, 'align="center"', $sortfield, $sortorder);
130	print_liste_field_titre("PaymentMode", $_SERVER["PHP_SELF"], "pct.code", "", $param, '', $sortfield, $sortorder);
131	print_liste_field_titre("Numero", $_SERVER["PHP_SELF"], "ptva.num_paiement", "", $param, '', $sortfield, $sortorder, '', 'ChequeOrTransferNumber');
132	if (!empty($conf->banque->enabled)) {
133		print_liste_field_titre("BankTransactionLine", $_SERVER["PHP_SELF"], "ptva.fk_bank", "", $param, '', $sortfield, $sortorder);
134		print_liste_field_titre("BankAccount", $_SERVER["PHP_SELF"], "bank.ref", "", $param, '', $sortfield, $sortorder);
135	}
136	//print_liste_field_titre("TypeContrib", $_SERVER["PHP_SELF"], "tva.fk_type", "", $param, '', $sortfield, $sortorder);
137	print_liste_field_titre("ExpectedToPay", $_SERVER["PHP_SELF"], "tva.amount", "", $param, 'class="right"', $sortfield, $sortorder);
138	print_liste_field_titre("PayedByThisPayment", $_SERVER["PHP_SELF"], "ptva.amount", "", $param, 'class="right"', $sortfield, $sortorder);
139	print "</tr>\n";
140
141	$sql = "SELECT tva.rowid, tva.label as label, b.fk_account, ptva.fk_bank";
142	$sql .= ", tva.datev";
143	$sql .= ", tva.amount as total,";
144	$sql .= " ptva.rowid as pid, ptva.datep, ptva.amount as totalpaye, ptva.num_paiement as num_payment,";
145	$sql .= " pct.code as payment_code";
146	$sql .= " FROM ".MAIN_DB_PREFIX."tva as tva,";
147	$sql .= " ".MAIN_DB_PREFIX."payment_vat as ptva";
148	$sql .= " INNER JOIN ".MAIN_DB_PREFIX."bank as b ON (b.rowid = ptva.fk_bank)";
149	$sql .= " INNER JOIN ".MAIN_DB_PREFIX."bank_account as bank ON (bank.rowid = b.fk_account)";
150	$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pct ON ptva.fk_typepaiement = pct.id";
151	$sql .= " WHERE ptva.fk_tva = tva.rowid";
152	$sql .= " AND tva.entity = ".$conf->entity;
153	if ($year > 0) {
154		$sql .= " AND (";
155		// Si period renseignee on l'utilise comme critere de date, sinon on prend date echeance,
156		// ceci afin d'etre compatible avec les cas ou la periode n'etait pas obligatoire
157		$sql .= "   (tva.datev IS NOT NULL AND tva.datev between '".$db->idate(dol_get_first_day($year))."' AND '".$db->idate(dol_get_last_day($year))."')";
158		$sql .= " OR (tva.datev IS NULL AND tva.datev between '".$db->idate(dol_get_first_day($year))."' AND '".$db->idate(dol_get_last_day($year))."')";
159		$sql .= ")";
160	}
161	if (preg_match('/^cs\./', $sortfield)
162		|| preg_match('/^tva\./', $sortfield)
163		|| preg_match('/^ptva\./', $sortfield)
164		|| preg_match('/^pct\./', $sortfield)
165		|| preg_match('/^bank\./', $sortfield)) {
166		$sql .= $db->order($sortfield, $sortorder);
167	}
168	//$sql.= $db->plimit($limit+1,$offset);
169	//print $sql;
170
171	dol_syslog("compta/tva/payments.php: select payment", LOG_DEBUG);
172	$resql = $db->query($sql);
173	if ($resql) {
174		$num = $db->num_rows($resql);
175		$i = 0;
176		$total = 0;
177		$totalnb = 0;
178		$totalpaye = 0;
179
180		while ($i < min($num, $limit)) {
181			$obj = $db->fetch_object($resql);
182
183			$payment_vat_static->id = $obj->pid;
184			$payment_vat_static->ref = $obj->pid;
185
186			print '<tr class="oddeven">';
187
188			// Ref payment
189			print '<td>'.$payment_vat_static->getNomUrl(1)."</td>\n";
190
191			// VAT
192			print '<td>';
193			$tva->id = $obj->rowid;
194			$tva->ref = $obj->rowid;
195			$tva->label = $obj->label;
196			print $tva->getNomUrl(1, '20');
197			print '</td>';
198
199			// Label
200			print '<td>'.$obj->label.'</td>';
201
202			// Date
203			$date = $obj->datev;
204			print '<td>'.dol_print_date($date, 'day').'</td>';
205
206			// Date payment
207			print '<td class="center">'.dol_print_date($db->jdate($obj->datep), 'day').'</td>';
208
209			// Type payment
210			print '<td>';
211			if ($obj->payment_code) {
212				print $langs->trans("PaymentTypeShort".$obj->payment_code).' ';
213			}
214			print '</td>';
215
216			// Chq number
217			print '<td>'.$obj->num_payment.'</td>';
218
219			if (!empty($conf->banque->enabled)) {
220				// Bank transaction
221				print '<td>';
222				$accountlinestatic->id = $obj->fk_bank;
223				print $accountlinestatic->getNomUrl(1);
224				print '</td>';
225
226				// Account
227				print '<td>';
228				$account = new Account($db);
229				$account->fetch($obj->fk_account);
230				print $account->getNomUrl(1);
231				print '</td>';
232			}
233
234			// Type
235			//print '<td><a href="../tva/list.php?filtre=tva.fk_type:'.$obj->type.'">'.$obj->type_label.'</a></td>';
236			// Expected to pay
237			print '<td class="right"><span class="amount">'.price($obj->total).'</span></td>';
238			// Paid
239			print '<td class="right"><span class="amount">';
240			if ($obj->totalpaye) {
241				print price($obj->totalpaye);
242			}
243			print '</span></td>';
244			print '</tr>';
245
246			$total = $total + $obj->total;
247			$totalnb = $totalnb + $obj->nb;
248			$totalpaye = $totalpaye + $obj->totalpaye;
249			$i++;
250		}
251
252		// Total
253		print '<tr class="liste_total"><td colspan="3" class="liste_total">'.$langs->trans("Total").'</td>';
254		print '<td class="liste_total right"></td>'; // A total here has no sense
255		print '<td align="center" class="liste_total">&nbsp;</td>';
256		print '<td align="center" class="liste_total">&nbsp;</td>';
257		if (!empty($conf->banque->enabled)) {
258			print '<td align="center" class="liste_total">&nbsp;</td>';
259			print '<td align="center" class="liste_total">&nbsp;</td>';
260		}
261		print '<td align="center" class="liste_total">&nbsp;</td>';
262		print '<td align="center" class="liste_total">&nbsp;</td>';
263		print '<td class="liste_total right">'.price($totalpaye)."</td>";
264		print "</tr>";
265	} else {
266		dol_print_error($db);
267	}
268	print '</table>';
269	print '</div>';
270}
271
272print '</form>';
273
274// End of page
275llxFooter();
276$db->close();
277