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       Laurent Destailleur     <eldy@users.sourceforge.net>
5 * Copyright (C) 2018       Frédéric France         <frederic.france@netlogic.fr>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21/**
22 *      \file       htdocs/compta/localtax/index.php
23 *      \ingroup    tax
24 *      \brief      Index page of IRPF reports
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$localTaxType = GETPOST('localTaxType', 'int');
37
38// Date range
39$year = GETPOST("year", "int");
40if (empty($year))
41{
42	$year_current = strftime("%Y", dol_now());
43	$year_start = $year_current;
44} else {
45	$year_current = $year;
46	$year_start = $year;
47}
48$date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"));
49$date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"));
50if (empty($date_start) || empty($date_end)) // We define date_start and date_end
51{
52	$q = GETPOST("q", "int");
53	if (empty($q))
54	{
55		if (GETPOST("month", "int")) { $date_start = dol_get_first_day($year_start, GETPOST("month", "int"), false); $date_end = dol_get_last_day($year_start, GETPOST("month", "int"), false); } else {
56			$date_start = dol_get_first_day($year_start, $conf->global->SOCIETE_FISCAL_MONTH_START, false);
57			$date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
58		}
59	} else {
60		if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); }
61		if ($q == 2) { $date_start = dol_get_first_day($year_start, 4, false); $date_end = dol_get_last_day($year_start, 6, false); }
62		if ($q == 3) { $date_start = dol_get_first_day($year_start, 7, false); $date_end = dol_get_last_day($year_start, 9, false); }
63		if ($q == 4) { $date_start = dol_get_first_day($year_start, 10, false); $date_end = dol_get_last_day($year_start, 12, false); }
64	}
65}
66
67// Define modetax (0 or 1)
68// 0=normal, 1=option vat for services is on debit, 2=option on payments for products
69$modetax = $conf->global->TAX_MODE;
70if (GETPOSTISSET("modetax")) $modetax = GETPOST("modetax", 'int');
71if (empty($modetax)) $modetax = 0;
72
73// Security check
74$socid = GETPOST('socid', 'int');
75if ($user->socid) $socid = $user->socid;
76$result = restrictedArea($user, 'tax', '', '', 'charges');
77
78
79/**
80 * print function
81 *
82 * @param		DoliDB	$db		Database handler
83 * @param		string	$sql	SQL Request
84 * @param		string	$date	Date
85 * @return		void
86 */
87function pt($db, $sql, $date)
88{
89	global $conf, $bc, $langs;
90
91	$result = $db->query($sql);
92	if ($result) {
93		$num = $db->num_rows($result);
94		$i = 0;
95		$total = 0;
96		print '<table class="noborder centpercent">';
97
98		print '<tr class="liste_titre">';
99		print '<td class="nowrap">'.$date.'</td>';
100		print '<td class="right">'.$langs->trans("ClaimedForThisPeriod").'</td>';
101		print '<td class="right">'.$langs->trans("PaidDuringThisPeriod").'</td>';
102		print "</tr>\n";
103
104		$totalclaimed = 0;
105		$totalpaid = 0;
106		$amountclaimed = 0;
107		$amountpaid = 0;
108		$previousmonth = '';
109		$previousmode = '';
110		$mode = '';
111
112		while ($i < $num) {
113			$obj = $db->fetch_object($result);
114			$mode = $obj->mode;
115
116			//print $obj->dm.' '.$obj->mode.' '.$previousmonth.' '.$previousmode;
117			if ($obj->mode == 'claimed' && !empty($previousmode))
118			{
119				print '<tr class="oddeven">';
120				print '<td class="nowrap">'.$previousmonth."</td>\n";
121				print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
122				print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
123				print "</tr>\n";
124
125				$amountclaimed = 0;
126				$amountpaid = 0;
127			}
128
129			if ($obj->mode == 'claimed')
130			{
131				$amountclaimed = $obj->mm;
132				$totalclaimed = $totalclaimed + $amountclaimed;
133			}
134			if ($obj->mode == 'paid')
135			{
136				$amountpaid = $obj->mm;
137				$totalpaid = $totalpaid + $amountpaid;
138			}
139
140			if ($obj->mode == 'paid')
141			{
142				print '<tr class="oddeven">';
143				print '<td class="nowrap">'.$obj->dm."</td>\n";
144				print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
145				print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
146				print "</tr>\n";
147				$amountclaimed = 0;
148				$amountpaid = 0;
149				$previousmode = '';
150				$previousmonth = '';
151			} else {
152				$previousmode = $obj->mode;
153				$previousmonth = $obj->dm;
154			}
155
156			$i++;
157		}
158
159		if ($mode == 'claimed' && !empty($previousmode))
160		{
161			print '<tr class="oddeven">';
162			print '<td class="nowrap">'.$previousmonth."</td>\n";
163			print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
164			print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
165			print "</tr>\n";
166
167			$amountclaimed = 0;
168			$amountpaid = 0;
169		}
170
171		print '<tr class="liste_total">';
172		print '<td class="right">'.$langs->trans("Total").'</td>';
173		print '<td class="nowrap right">'.price($totalclaimed).'</td>';
174		print '<td class="nowrap right">'.price($totalpaid).'</td>';
175		print "</tr>";
176
177		print "</table>";
178		$db->free($result);
179	} else {
180		dol_print_error($db);
181	}
182}
183
184if (empty($localTaxType))
185{
186	accessforbidden('Parameter localTaxType is missing');
187	exit;
188}
189
190
191/*
192 * Actions
193 */
194
195// None
196
197
198/*
199 * View
200 */
201
202$form = new Form($db);
203$company_static = new Societe($db);
204$tva = new Tva($db);
205
206if ($localTaxType == 1) {
207	$LT = 'LT1';
208	$LTSummary = 'LT1Summary';
209	$LTPaid = 'LT1Paid';
210	$LTCustomer = 'LT1Customer';
211	$LTSupplier = 'LT1Supplier';
212	$CalcLT = $conf->global->MAIN_INFO_LOCALTAX_CALC1;
213} else {
214	$LT = 'LT2';
215	$LTSummary = 'LT2Summary';
216	$LTPaid = 'LT2Paid';
217	$LTCustomer = 'LT2Customer';
218	$LTSupplier = 'LT2Supplier';
219	$CalcLT = $conf->global->MAIN_INFO_LOCALTAX_CALC2;
220}
221
222$fsearch = '<!-- hidden fields for form -->';
223$fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
224$fsearch .= '<input type="hidden" name="localTaxType" value="'.$localTaxType.'">';
225$fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
226
227$description = $fsearch;
228
229// Show report header
230$name = $langs->transcountry($localTaxType == 1 ? "LT1ReportByMonth" : "LT2ReportByMonth", $mysoc->country_code);
231$description .= $langs->trans($LT);
232$calcmode = $langs->trans("LTReportBuildWithOptionDefinedInModule").' ';
233$calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
234
235//if (! empty($conf->global->MAIN_MODULE_ACCOUNTING)) $description.='<br>'.$langs->trans("ThisIsAnEstimatedValue");
236
237$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
238
239$builddate = dol_now();
240
241
242llxHeader('', $name);
243
244//$textprevyear="<a href=\"index.php?localTaxType=".$localTaxType."&year=" . ($year_current-1) . "\">".img_previous()."</a>";
245//$textnextyear=" <a href=\"index.php?localTaxType=".$localTaxType."&year=" . ($year_current+1) . "\">".img_next()."</a>";
246//print load_fiche_titre($langs->transcountry($LT,$mysoc->country_code),"$textprevyear ".$langs->trans("Year")." $year_start $textnextyear", 'bill');
247
248report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
249//report_header($name,'',$textprevyear.$langs->trans("Year")." ".$year_start.$textnextyear,'',$description,$builddate,$exportlink,array(),$calcmode);
250
251
252print '<br>';
253
254print '<div class="fichecenter"><div class="fichethirdleft">';
255
256print load_fiche_titre($langs->transcountry($LTSummary, $mysoc->country_code), '', '');
257
258print '<table class="noborder centpercent">';
259print '<tr class="liste_titre">';
260print '<td width="30%">'.$langs->trans("Year")." ".$y."</td>";
261if ($CalcLT == 0) {
262	print '<td class="right">'.$langs->transcountry($LTCustomer, $mysoc->country_code).'</td>';
263	print '<td class="right">'.$langs->transcountry($LTSupplier, $mysoc->country_code).'</td>';
264}
265if ($CalcLT == 1) {
266	print '<td class="right">'.$langs->transcountry($LTSupplier, $mysoc->country_code).'</td><td></td>';
267}
268if ($CalcLT == 2) {
269	print '<td class="right">'.$langs->transcountry($LTCustomer, $mysoc->country_code).'</td><td></td>';
270}
271print '<td class="right">'.$langs->trans("TotalToPay").'</td>';
272print "<td>&nbsp;</td>\n";
273print "</tr>\n";
274
275$tmp = dol_getdate($date_start);
276$y = $tmp['year'];
277$m = $tmp['mon'];
278$tmp = dol_getdate($date_end);
279$yend = $tmp['year'];
280$mend = $tmp['mon'];
281
282$total = 0; $subtotalcoll = 0; $subtotalpaye = 0; $subtotal = 0;
283$i = 0; $mcursor = 0;
284while ((($y < $yend) || ($y == $yend && $m <= $mend)) && $mcursor < 1000)	// $mcursor is to avoid too large loop
285{
286	//$m = $conf->global->SOCIETE_FISCAL_MONTH_START + ($mcursor % 12);
287	if ($m == 13) $y++;
288	if ($m > 12) $m -= 12;
289	$mcursor++;
290
291	// Get array with details of each line
292	$x_coll = tax_by_rate(($localTaxType == 1 ? 'localtax1' : 'localtax2'), $db, $y, 0, 0, 0, $modetax, 'sell', $m);
293	$x_paye = tax_by_rate(($localTaxType == 1 ? 'localtax1' : 'localtax2'), $db, $y, 0, 0, 0, $modetax, 'buy', $m);
294
295	$x_both = array();
296	//now, from these two arrays, get another array with one rate per line
297	foreach (array_keys($x_coll) as $my_coll_rate)
298	{
299		$x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht'];
300		$x_both[$my_coll_rate]['coll']['vat'] = $x_coll[$my_coll_rate]['vat'];
301		$x_both[$my_coll_rate]['coll']['localtax1']	 = $x_coll[$my_coll_rate]['localtax1'];
302		$x_both[$my_coll_rate]['coll']['localtax2']	 = $x_coll[$my_coll_rate]['localtax2'];
303		$x_both[$my_coll_rate]['paye']['totalht'] = 0;
304		$x_both[$my_coll_rate]['paye']['vat'] = 0;
305		$x_both[$my_coll_rate]['paye']['localtax1'] = 0;
306		$x_both[$my_coll_rate]['paye']['localtax2'] = 0;
307		$x_both[$my_coll_rate]['coll']['links'] = '';
308		$x_both[$my_coll_rate]['coll']['detail'] = array();
309		foreach ($x_coll[$my_coll_rate]['facid'] as $id=>$dummy) {
310			//$invoice_customer->id=$x_coll[$my_coll_rate]['facid'][$id];
311			//$invoice_customer->ref=$x_coll[$my_coll_rate]['facnum'][$id];
312			//$invoice_customer->type=$x_coll[$my_coll_rate]['type'][$id];
313			//$company_static->fetch($x_coll[$my_coll_rate]['company_id'][$id]);
314			$x_both[$my_coll_rate]['coll']['detail'][] = array(
315			'id'        =>$x_coll[$my_coll_rate]['facid'][$id],
316			'descr'     =>$x_coll[$my_coll_rate]['descr'][$id],
317			'pid'       =>$x_coll[$my_coll_rate]['pid'][$id],
318			'pref'      =>$x_coll[$my_coll_rate]['pref'][$id],
319			'ptype'     =>$x_coll[$my_coll_rate]['ptype'][$id],
320			'payment_id'=>$x_coll[$my_coll_rate]['payment_id'][$id],
321			'payment_amount'=>$x_coll[$my_coll_rate]['payment_amount'][$id],
322			'ftotal_ttc'=>$x_coll[$my_coll_rate]['ftotal_ttc'][$id],
323			'dtotal_ttc'=>$x_coll[$my_coll_rate]['dtotal_ttc'][$id],
324			'dtype'     =>$x_coll[$my_coll_rate]['dtype'][$id],
325			'datef'     =>$x_coll[$my_coll_rate]['datef'][$id],
326			'datep'     =>$x_coll[$my_coll_rate]['datep'][$id],
327			//'company_link'=>$company_static->getNomUrl(1,'',20),
328			'ddate_start'=>$x_coll[$my_coll_rate]['ddate_start'][$id],
329			'ddate_end'  =>$x_coll[$my_coll_rate]['ddate_end'][$id],
330
331			'totalht'   =>$x_coll[$my_coll_rate]['totalht_list'][$id],
332			'vat'       =>$x_coll[$my_coll_rate]['vat_list'][$id],
333			'localtax1' =>$x_coll[$my_coll_rate]['localtax1_list'][$id],
334			'localtax2' =>$x_coll[$my_coll_rate]['localtax2_list'][$id],
335			//'link'      =>$invoice_customer->getNomUrl(1,'',12)
336			);
337		}
338	}
339
340	// tva paid
341	foreach (array_keys($x_paye) as $my_paye_rate) {
342		$x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht'];
343		$x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat'];
344		$x_both[$my_paye_rate]['paye']['localtax1'] = $x_paye[$my_paye_rate]['localtax1'];
345		$x_both[$my_paye_rate]['paye']['localtax2'] = $x_paye[$my_paye_rate]['localtax2'];
346		if (!isset($x_both[$my_paye_rate]['coll']['totalht'])) {
347			$x_both[$my_paye_rate]['coll']['totalht'] = 0;
348			$x_both[$my_paye_rate]['coll']['vat'] = 0;
349			$x_both[$my_paye_rate]['coll']['localtax1'] = 0;
350			$x_both[$my_paye_rate]['coll']['localtax2'] = 0;
351		}
352		$x_both[$my_paye_rate]['paye']['links'] = '';
353		$x_both[$my_paye_rate]['paye']['detail'] = array();
354
355		foreach ($x_paye[$my_paye_rate]['facid'] as $id=>$dummy)
356		{
357			// ExpenseReport
358			if ($x_paye[$my_paye_rate]['ptype'][$id] == 'ExpenseReportPayment')
359			{
360				//$expensereport->id=$x_paye[$my_paye_rate]['facid'][$id];
361				//$expensereport->ref=$x_paye[$my_paye_rate]['facnum'][$id];
362				//$expensereport->type=$x_paye[$my_paye_rate]['type'][$id];
363
364				$x_both[$my_paye_rate]['paye']['detail'][] = array(
365				'id'				=>$x_paye[$my_paye_rate]['facid'][$id],
366				'descr'				=>$x_paye[$my_paye_rate]['descr'][$id],
367				'pid'				=>$x_paye[$my_paye_rate]['pid'][$id],
368				'pref'				=>$x_paye[$my_paye_rate]['pref'][$id],
369				'ptype'				=>$x_paye[$my_paye_rate]['ptype'][$id],
370				'payment_id'		=>$x_paye[$my_paye_rate]['payment_id'][$id],
371				'payment_amount'	=>$x_paye[$my_paye_rate]['payment_amount'][$id],
372				'ftotal_ttc'		=>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
373				'dtotal_ttc'		=>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
374				'dtype'				=>$x_paye[$my_paye_rate]['dtype'][$id],
375				'ddate_start'		=>$x_paye[$my_paye_rate]['ddate_start'][$id],
376				'ddate_end'			=>$x_paye[$my_paye_rate]['ddate_end'][$id],
377
378				'totalht'			=>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
379				'vat'				=>$x_paye[$my_paye_rate]['vat_list'][$id],
380				'localtax1'			=>$x_paye[$my_paye_rate]['localtax1_list'][$id],
381				'localtax2'			=>$x_paye[$my_paye_rate]['localtax2_list'][$id],
382				//'link'				=>$expensereport->getNomUrl(1)
383				);
384			} else {
385				//$invoice_supplier->id=$x_paye[$my_paye_rate]['facid'][$id];
386				//$invoice_supplier->ref=$x_paye[$my_paye_rate]['facnum'][$id];
387				//$invoice_supplier->type=$x_paye[$my_paye_rate]['type'][$id];
388				//$company_static->fetch($x_paye[$my_paye_rate]['company_id'][$id]);
389				$x_both[$my_paye_rate]['paye']['detail'][] = array(
390				'id'        =>$x_paye[$my_paye_rate]['facid'][$id],
391				'descr'     =>$x_paye[$my_paye_rate]['descr'][$id],
392				'pid'       =>$x_paye[$my_paye_rate]['pid'][$id],
393				'pref'      =>$x_paye[$my_paye_rate]['pref'][$id],
394				'ptype'     =>$x_paye[$my_paye_rate]['ptype'][$id],
395				'payment_id'=>$x_paye[$my_paye_rate]['payment_id'][$id],
396				'payment_amount'=>$x_paye[$my_paye_rate]['payment_amount'][$id],
397				'ftotal_ttc'=>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
398				'dtotal_ttc'=>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
399				'dtype'     =>$x_paye[$my_paye_rate]['dtype'][$id],
400				'datef'     =>$x_paye[$my_paye_rate]['datef'][$id],
401				'datep'     =>$x_paye[$my_paye_rate]['datep'][$id],
402				//'company_link'=>$company_static->getNomUrl(1,'',20),
403				'ddate_start'=>$x_paye[$my_paye_rate]['ddate_start'][$id],
404				'ddate_end'  =>$x_paye[$my_paye_rate]['ddate_end'][$id],
405
406				'totalht'   =>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
407				'vat'       =>$x_paye[$my_paye_rate]['vat_list'][$id],
408				'localtax1' =>$x_paye[$my_paye_rate]['localtax1_list'][$id],
409				'localtax2' =>$x_paye[$my_paye_rate]['localtax2_list'][$id],
410				//'link'      =>$invoice_supplier->getNomUrl(1,'',12)
411				);
412			}
413		}
414	}
415	//now we have an array (x_both) indexed by rates for coll and paye
416
417	$action = "tva";
418	$object = array(&$x_coll, &$x_paye, &$x_both);
419	$parameters["mode"] = $modetax;
420	$parameters["year"] = $y;
421	$parameters["month"] = $m;
422	$parameters["type"] = 'localtax'.$localTaxType;
423
424	// Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
425	$hookmanager->initHooks(array('externalbalance'));
426	$reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
427
428	if (!is_array($x_coll) && $coll_listbuy == -1)
429	{
430		$langs->load("errors");
431		print '<tr><td colspan="5">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
432		break;
433	}
434	if (!is_array($x_paye) && $coll_listbuy == -2)
435	{
436		print '<tr><td colspan="5">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
437		break;
438	}
439
440
441	print '<tr class="oddeven">';
442	print '<td class="nowrap"><a href="'.DOL_URL_ROOT.'/compta/localtax/quadri_detail.php?leftmenu=tax_vat&month='.$m.'&year='.$y.'">'.dol_print_date(dol_mktime(0, 0, 0, $m, 1, $y), "%b %Y").'</a></td>';
443
444	$x_coll_sum = 0;
445	foreach (array_keys($x_coll) as $rate)
446	{
447		$subtot_coll_total_ht = 0;
448		$subtot_coll_vat = 0;
449
450		foreach ($x_both[$rate]['coll']['detail'] as $index => $fields)
451		{
452			// Payment
453			$ratiopaymentinvoice = 1;
454			if ($modetax != 1)
455			{
456				// Define type
457				// We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
458				$type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
459				// Try to enhance type detection using date_start and date_end for free lines where type
460				// was not saved.
461				if (!empty($fields['ddate_start'])) {
462					$type = 1;
463				}
464				if (!empty($fields['ddate_end'])) {
465					$type = 1;
466				}
467
468				if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
469					|| ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice'))
470				{
471					//print $langs->trans("NA");
472				} else {
473					if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
474						$ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
475					}
476				}
477			}
478			//var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
479			$temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
480			$temp_vat = $fields['localtax'.$localTaxType] * $ratiopaymentinvoice;
481			$subtot_coll_total_ht += $temp_ht;
482			$subtot_coll_vat      += $temp_vat;
483			$x_coll_sum           += $temp_vat;
484		}
485	}
486	print '<td class="nowrap right">'.price(price2num($x_coll_sum, 'MT')).'</td>';
487
488	$x_paye_sum = 0;
489	foreach (array_keys($x_paye) as $rate)
490	{
491		$subtot_paye_total_ht = 0;
492		$subtot_paye_vat = 0;
493
494		foreach ($x_both[$rate]['paye']['detail'] as $index => $fields)
495		{
496			// Payment
497			$ratiopaymentinvoice = 1;
498			if ($modetax != 1)
499			{
500				// Define type
501				// We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
502				$type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
503				// Try to enhance type detection using date_start and date_end for free lines where type
504				// was not saved.
505				if (!empty($fields['ddate_start'])) {
506					$type = 1;
507				}
508				if (!empty($fields['ddate_end'])) {
509					$type = 1;
510				}
511
512				if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
513					|| ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice'))
514				{
515					//print $langs->trans("NA");
516				} else {
517					if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
518						$ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
519					}
520				}
521			}
522			//var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
523			$temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
524			$temp_vat = $fields['localtax'.$localTaxType] * $ratiopaymentinvoice;
525			$subtot_paye_total_ht += $temp_ht;
526			$subtot_paye_vat      += $temp_vat;
527			$x_paye_sum           += $temp_vat;
528		}
529	}
530	print '<td class="nowrap right">'.price(price2num($x_paye_sum, 'MT')).'</td>';
531
532	$subtotalcoll = $subtotalcoll + $x_coll_sum;
533	$subtotalpaye = $subtotalpaye + $x_paye_sum;
534
535	$diff = $x_coll_sum - $x_paye_sum;
536	$total = $total + $diff;
537	$subtotal = price2num($subtotal + $diff, 'MT');
538
539	print '<td class="nowrap right">'.price(price2num($diff, 'MT')).'</td>'."\n";
540	print "<td>&nbsp;</td>\n";
541	print "</tr>\n";
542
543	$i++; $m++;
544	if ($i > 2)
545	{
546		print '<tr class="liste_total">';
547		print '<td class="right"><a href="quadri_detail.php?leftmenu=tax_vat&q='.round($m / 3).'&year='.$y.'">'.$langs->trans("SubTotal").'</a>:</td>';
548		print '<td class="nowrap right">'.price(price2num($subtotalcoll, 'MT')).'</td>';
549		print '<td class="nowrap right">'.price(price2num($subtotalpaye, 'MT')).'</td>';
550		print '<td class="nowrap right">'.price(price2num($subtotal, 'MT')).'</td>';
551		print '<td>&nbsp;</td></tr>';
552		$i = 0;
553		$subtotalcoll = 0; $subtotalpaye = 0; $subtotal = 0;
554	}
555}
556print '<tr class="liste_total"><td class="right" colspan="3">'.$langs->trans("TotalToPay").':</td><td class="nowrap right">'.price(price2num($total, 'MT')).'</td>';
557print "<td>&nbsp;</td>\n";
558print '</tr>';
559
560print '</table>';
561
562
563print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
564
565
566
567/*
568 * Paid
569 */
570
571print load_fiche_titre($langs->transcountry($LTPaid, $mysoc->country_code), '', '');
572
573$sql = '';
574
575$sql .= "SELECT SUM(amount) as mm, date_format(f.datev,'%Y-%m') as dm, 'claimed' as mode";
576$sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
577$sql .= " WHERE f.entity = ".$conf->entity;
578$sql .= " AND (f.datev >= '".$db->idate($date_start)."' AND f.datev <= '".$db->idate($date_end)."')";
579$sql .= " AND localtaxtype=".$localTaxType;
580$sql .= " GROUP BY dm";
581
582$sql .= " UNION ";
583
584$sql .= "SELECT SUM(amount) as mm, date_format(f.datep,'%Y-%m') as dm, 'paid' as mode";
585$sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
586$sql .= " WHERE f.entity = ".$conf->entity;
587$sql .= " AND (f.datep >= '".$db->idate($date_start)."' AND f.datep <= '".$db->idate($date_end)."')";
588$sql .= " AND localtaxtype=".$localTaxType;
589$sql .= " GROUP BY dm";
590
591$sql .= " ORDER BY dm ASC, mode ASC";
592//print $sql;
593
594pt($db, $sql, $langs->trans("Month"));
595
596
597print '</div></div>';
598
599// End of page
600llxFooter();
601$db->close();
602