1<?php
2/* Copyright (C) 2011-2014	Juanjo Menent           <jmenent@2byte.es>
3 * Copyright (C) 2014	    Ferran Marcet           <fmarcet@2byte.es>
4 * Copyright (C) 2018       Frédéric France         <frederic.france@netlogic.fr>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20/**
21 *	    \file       htdocs/compta/localtax/clients.php
22 *      \ingroup    tax
23 *		\brief      Third parties localtax report
24 */
25
26require '../../main.inc.php';
27require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
28require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/localtax/class/localtax.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array("other", "compta", "banks", "bills", "companies", "product", "trips", "admin"));
35
36$local = GETPOST('localTaxType', 'int');
37
38// Date range
39$year = GETPOST("year", "int");
40if (empty($year)) {
41	$year_current = strftime("%Y", dol_now());
42	$year_start = $year_current;
43} else {
44	$year_current = $year;
45	$year_start = $year;
46}
47$date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"));
48$date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"));
49// Quarter
50if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
51	$q = GETPOST("q");
52	if (empty($q)) {
53		if (GETPOST("month")) {
54			$date_start = dol_get_first_day($year_start, GETPOST("month"), false);
55			$date_end = dol_get_last_day($year_start, GETPOST("month"), false);
56		} else {
57			$date_start = dol_get_first_day($year_start, empty($conf->global->SOCIETE_FISCAL_MONTH_START) ? 1 : $conf->global->SOCIETE_FISCAL_MONTH_START, false);
58			if (empty($conf->global->MAIN_INFO_VAT_RETURN) || $conf->global->MAIN_INFO_VAT_RETURN == 2) {
59				$date_end = dol_time_plus_duree($date_start, 3, 'm') - 1;
60			} elseif ($conf->global->MAIN_INFO_VAT_RETURN == 3) {
61				$date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
62			} elseif ($conf->global->MAIN_INFO_VAT_RETURN == 1) {
63				$date_end = dol_time_plus_duree($date_start, 1, 'm') - 1;
64			}
65		}
66	} else {
67		if ($q == 1) {
68			$date_start = dol_get_first_day($year_start, 1, false);
69			$date_end = dol_get_last_day($year_start, 3, false);
70		}
71		if ($q == 2) {
72			$date_start = dol_get_first_day($year_start, 4, false);
73			$date_end = dol_get_last_day($year_start, 6, false);
74		}
75		if ($q == 3) {
76			$date_start = dol_get_first_day($year_start, 7, false);
77			$date_end = dol_get_last_day($year_start, 9, false);
78		}
79		if ($q == 4) {
80			$date_start = dol_get_first_day($year_start, 10, false);
81			$date_end = dol_get_last_day($year_start, 12, false);
82		}
83	}
84}
85
86$min = price2num(GETPOST("min", "alpha"));
87if (empty($min)) {
88	$min = 0;
89}
90
91// Define modetax (0 or 1)
92// 0=normal, 1=option vat for services is on debit, 2=option on payments for products
93$modetax = $conf->global->TAX_MODE;
94if (GETPOSTISSET("modetax")) {
95	$modetax = GETPOST("modetax", 'int');
96}
97if (empty($modetax)) {
98	$modetax = 0;
99}
100
101// Security check
102$socid = GETPOST('socid', 'int');
103if ($user->socid) {
104	$socid = $user->socid;
105}
106$result = restrictedArea($user, 'tax', '', '', 'charges');
107
108if (empty($local)) {
109	accessforbidden('Parameter localTaxType is missing');
110	exit;
111}
112
113
114$calc = 0;
115/*
116 * View
117 */
118
119$form = new Form($db);
120$company_static = new Societe($db);
121
122$morequerystring = '';
123$listofparams = array('date_startmonth', 'date_startyear', 'date_startday', 'date_endmonth', 'date_endyear', 'date_endday');
124foreach ($listofparams as $param) {
125	if (GETPOST($param) != '') {
126		$morequerystring .= ($morequerystring ? '&' : '').$param.'='.GETPOST($param);
127	}
128}
129
130llxHeader('', '', '', '', 0, 0, '', '', $morequerystring);
131
132
133$name = $langs->transcountry($local == 1 ? "LT1ReportByCustomers" : "LT2ReportByCustomers", $mysoc->country_code);
134
135$fsearch = '<!-- hidden fields for form -->';
136$fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
137$fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
138$fsearch .= '<input type="hidden" name="localTaxType" value="'.$local.'">';
139$fsearch .= $langs->trans("SalesTurnoverMinimum").': ';
140$fsearch .= '<input type="text" name="min" id="min" value="'.$min.'" size="6">';
141
142$calc = $conf->global->MAIN_INFO_LOCALTAX_CALC.$local;
143// Affiche en-tete du rapport
144if ($calc == 0 || $calc == 1) {	// Calculate on invoice for goods and services
145	$calcmode = $calc == 0 ? $langs->trans("CalcModeLT".$local) : $langs->trans("CalcModeLT".$local."Rec");
146	$calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
147	$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
148	if (!empty($conf->global->MAIN_MODULE_COMPTABILITE)) {
149		$description .= '<br>'.$langs->trans("WarningDepositsNotIncluded");
150	}
151	$description .= $fsearch;
152	$description .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
153	$builddate = dol_now();
154
155	$elementcust = $langs->trans("CustomersInvoices");
156	$productcust = $langs->trans("Description");
157	$amountcust = $langs->trans("AmountHT");
158	$elementsup = $langs->trans("SuppliersInvoices");
159	$productsup = $langs->trans("Description");
160	$amountsup = $langs->trans("AmountHT");
161}
162if ($calc == 2) { 	// Invoice for goods, payment for services
163	$calcmode = $langs->trans("CalcModeLT2Debt");
164	$calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
165	$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
166	if (!empty($conf->global->MAIN_MODULE_COMPTABILITE)) {
167		$description .= '<br>'.$langs->trans("WarningDepositsNotIncluded");
168	}
169	$description .= $fsearch;
170	$description .= '<span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
171	$builddate = dol_now();
172
173	$elementcust = $langs->trans("CustomersInvoices");
174	$productcust = $langs->trans("Description");
175	$amountcust = $langs->trans("AmountHT");
176	$elementsup = $langs->trans("SuppliersInvoices");
177	$productsup = $langs->trans("Description");
178	$amountsup = $langs->trans("AmountHT");
179}
180report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
181
182
183$vatcust = $langs->transcountry($local == 1 ? "LT1" : "LT2", $mysoc->country_code);
184$vatsup = $langs->transcountry($local == 1 ? "LT1" : "LT2", $mysoc->country_code);
185
186print '<div class="div-table-responsive">';
187print '<table class="noborder centpercent">';
188
189// IRPF that the customer has retained me
190if ($calc == 0 || $calc == 2) {
191	print '<tr class="liste_titre">';
192	print '<td class="left">'.$langs->trans("Num").'</td>';
193	print '<td class="left">'.$langs->trans("Customer").'</td>';
194	print '<td>'.$langs->transcountry("ProfId1", $mysoc->country_code).'</td>';
195	print '<td class="right">'.$langs->trans("TotalHT").'</td>';
196	print '<td class="right">'.$vatcust.'</td>';
197	print "</tr>\n";
198
199	$coll_list = tax_by_thirdparty('localtax'.$local, $db, 0, $date_start, $date_end, $modetax, 'sell');
200
201	$action = "tvaclient";
202	$object = &$coll_list;
203	$parameters["mode"] = $modetax;
204	$parameters["start"] = $date_start;
205	$parameters["end"] = $date_end;
206	$parameters["direction"] = 'sell';
207	$parameters["type"] = 'localtax'.$local;
208
209	// Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
210	$hookmanager->initHooks(array('externalbalance'));
211	$reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
212
213	if (is_array($coll_list)) {
214		$total = 0;
215		$totalamount = 0;
216		$i = 1;
217		foreach ($coll_list as $coll) {
218			if (($min == 0 || ($min > 0 && $coll->amount > $min)) && ($local == 1 ? $coll->localtax1 : $coll->localtax2) != 0) {
219				$intra = str_replace($find, $replace, $coll->tva_intra);
220				if (empty($intra)) {
221					if ($coll->assuj == '1') {
222						$intra = $langs->trans('Unknown');
223					} else {
224						$intra = '';
225					}
226				}
227				print '<tr class="oddeven">';
228				print '<td class="nowrap">'.$i."</td>";
229				$company_static->id = $coll->socid;
230				$company_static->name = $coll->name;
231				print '<td class="nowrap">'.$company_static->getNomUrl(1).'</td>';
232				$find = array(' ', '.');
233				$replace = array('', '');
234				print '<td class="nowrap">'.$intra.'</td>';
235				print '<td class="nowrap right">'.price($coll->amount).'</td>';
236				print '<td class="nowrap right">'.price($local == 1 ? $coll->localtax1 : $coll->localtax2).'</td>';
237				$totalamount = $totalamount + $coll->amount;
238				$total = $total + ($local == 1 ? $coll->localtax1 : $coll->localtax2);
239				print "</tr>\n";
240				$i++;
241			}
242		}
243		$x_coll_sum = $total;
244
245		print '<tr class="liste_total"><td class="right" colspan="3">'.$langs->trans("Total").':</td>';
246		print '<td class="nowrap right">'.price($totalamount).'</td>';
247		print '<td class="nowrap right">'.price($total).'</td>';
248		print '</tr>';
249	} else {
250		$langs->load("errors");
251		if ($coll_list == -1) {
252			print '<tr><td colspan="5">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
253		} elseif ($coll_list == -2) {
254			print '<tr><td colspan="5">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
255		} else {
256			print '<tr><td colspan="5">'.$langs->trans("Error").'</td></tr>';
257		}
258	}
259}
260
261// IRPF I retained my supplier
262if ($calc == 0 || $calc == 1) {
263	print '<tr class="liste_titre">';
264	print '<td class="left">'.$langs->trans("Num")."</td>";
265	print '<td class="left">'.$langs->trans("Supplier")."</td>";
266	print '<td>'.$langs->transcountry("ProfId1", $mysoc->country_code).'</td>';
267	print '<td class="right">'.$langs->trans("TotalHT").'</td>';
268	print '<td class="right">'.$vatsup.'</td>';
269	print "</tr>\n";
270
271	$company_static = new Societe($db);
272
273	$coll_list = tax_by_thirdparty('localtax'.$local, $db, 0, $date_start, $date_end, $modetax, 'buy');
274	$parameters["direction"] = 'buy';
275	$parameters["type"] = 'localtax'.$local;
276
277	$reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
278	if (is_array($coll_list)) {
279		$total = 0;
280		$totalamount = 0;
281		$i = 1;
282		foreach ($coll_list as $coll) {
283			if (($min == 0 || ($min > 0 && $coll->amount > $min)) && ($local == 1 ? $coll->localtax1 : $coll->localtax2) != 0) {
284				$intra = str_replace($find, $replace, $coll->tva_intra);
285				if (empty($intra)) {
286					if ($coll->assuj == '1') {
287						$intra = $langs->trans('Unknown');
288					} else {
289						$intra = '';
290					}
291				}
292				print '<tr class="oddeven">';
293				print '<td class="nowrap">'.$i."</td>";
294				$company_static->id = $coll->socid;
295				$company_static->name = $coll->name;
296				print '<td class="nowrap">'.$company_static->getNomUrl(1).'</td>';
297				$find = array(' ', '.');
298				$replace = array('', '');
299				print '<td class="nowrap">'.$intra."</td>";
300				print '<td class="nowrap right">'.price($coll->amount).'</td>';
301				print '<td class="nowrap right">'.price($local == 1 ? $coll->localtax1 : $coll->localtax2).'</td>';
302				$totalamount = $totalamount + $coll->amount;
303				$total = $total + ($local == 1 ? $coll->localtax1 : $coll->localtax2);
304				print "</tr>\n";
305				$i++;
306			}
307		}
308		$x_paye_sum = $total;
309
310		print '<tr class="liste_total"><td class="right" colspan="3">'.$langs->trans("Total").':</td>';
311		print '<td class="nowrap right">'.price($totalamount).'</td>';
312		print '<td class="nowrap right">'.price($total).'</td>';
313		print '</tr>';
314	} else {
315		$langs->load("errors");
316		if ($coll_list == -1) {
317			print '<tr><td colspan="5">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
318		} elseif ($coll_list == -2) {
319			print '<tr><td colspan="5">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
320		} else {
321			print '<tr><td colspan="5">'.$langs->trans("Error").'</td></tr>';
322		}
323	}
324}
325
326if ($calc == 0) {
327	// Total to pay
328	print '<tr><td colspan="5"></td></tr>';
329
330	$diff = $x_coll_sum - $x_paye_sum;
331	print '<tr class="liste_total">';
332	print '<td class="liste_total" colspan="4">'.$langs->trans("TotalToPay").($q ? ', '.$langs->trans("Quadri").' '.$q : '').'</td>';
333	print '<td class="liste_total nowrap right"><b>'.price(price2num($diff, 'MT'))."</b></td>\n";
334	print "</tr>\n";
335}
336
337print '</table>';
338print '</div>';
339
340
341// End of page
342llxFooter();
343$db->close();
344