dolibarr  13.0.2
index.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
4  * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
5  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
6  * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
7  * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
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 
28 require '../../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/compta/localtax/class/localtax.class.php';
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array("other", "compta", "banks", "bills", "companies", "product", "trips", "admin"));
37 
38 $now = dol_now();
39 $current_date = dol_getdate($now);
40 if (empty($conf->global->SOCIETE_FISCAL_MONTH_START)) $conf->global->SOCIETE_FISCAL_MONTH_START = 1;
41 
42 // Date range
43 $year = GETPOST("year", "int");
44 if (empty($year))
45 {
46  $year_current = $current_date['year'];
47  $year_start = $year_current;
48 } else {
49  $year_current = $year;
50  $year_start = $year;
51 }
52 $date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"));
53 $date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"));
54 // Set default period if not defined
55 if (empty($date_start) || empty($date_end)) // We define date_start and date_end
56 {
57  $q = GETPOST("q", "int");
58  if (empty($q))
59  {
60  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 {
61  if (empty($conf->global->MAIN_INFO_VAT_RETURN) || $conf->global->MAIN_INFO_VAT_RETURN == 2) { // quaterly vat, we take last past complete quarter
62  $date_start = dol_time_plus_duree(dol_get_first_day($year_start, $current_date['mon'], false), -3 - (($current_date['mon'] - $conf->global->SOCIETE_FISCAL_MONTH_START) % 3), 'm');
63  $date_end = dol_time_plus_duree($date_start, 3, 'm') - 1;
64  } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 3) { // yearly vat
65  if ($current_date['mon'] < $conf->global->SOCIETE_FISCAL_MONTH_START) {
66  if (($conf->global->SOCIETE_FISCAL_MONTH_START - $current_date['mon']) > 6) { // If period started from less than 6 years, we show past year
67  $year_start--;
68  }
69  } else {
70  if (($current_date['mon'] - $conf->global->SOCIETE_FISCAL_MONTH_START) < 6) { // If perdio started from less than 6 years, we show past year
71  $year_start--;
72  }
73  }
74  $date_start = dol_get_first_day($year_start, $conf->global->SOCIETE_FISCAL_MONTH_START, false);
75  $date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
76  } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 1) { // monthly vat, we take last past complete month
77  $date_start = dol_time_plus_duree(dol_get_first_day($year_start, $current_date['mon'], false), -1, 'm');
78  $date_end = dol_time_plus_duree($date_start, 1, 'm') - 1;
79  }
80  }
81  } else {
82  if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); }
83  if ($q == 2) { $date_start = dol_get_first_day($year_start, 4, false); $date_end = dol_get_last_day($year_start, 6, false); }
84  if ($q == 3) { $date_start = dol_get_first_day($year_start, 7, false); $date_end = dol_get_last_day($year_start, 9, false); }
85  if ($q == 4) { $date_start = dol_get_first_day($year_start, 10, false); $date_end = dol_get_last_day($year_start, 12, false); }
86  }
87 }
88 
89 // Define modetax (0 or 1)
90 // 0=normal, 1=option vat for services is on debit, 2=option on payments for products
91 $modetax = $conf->global->TAX_MODE;
92 if (GETPOSTISSET("modetax")) $modetax = GETPOST("modetax", 'int');
93 if (empty($modetax)) $modetax = 0;
94 
95 // Security check
96 $socid = GETPOST('socid', 'int');
97 if ($user->socid) $socid = $user->socid;
98 $result = restrictedArea($user, 'tax', '', '', 'charges');
99 
100 
109 function pt($db, $sql, $date)
110 {
111  global $conf, $bc, $langs;
112 
113  $result = $db->query($sql);
114  if ($result) {
115  $num = $db->num_rows($result);
116  $i = 0;
117  $total = 0;
118  print '<table class="noborder centpercent">';
119 
120  print '<tr class="liste_titre">';
121  print '<td class="nowrap">'.$date.'</td>';
122  print '<td class="right">'.$langs->trans("ClaimedForThisPeriod").'</td>';
123  print '<td class="right">'.$langs->trans("PaidDuringThisPeriod").'</td>';
124  print "</tr>\n";
125 
126  $totalclaimed = 0;
127  $totalpaid = 0;
128  $amountclaimed = 0;
129  $amountpaid = 0;
130  $previousmonth = '';
131  $previousmode = '';
132  $mode = '';
133 
134  while ($i < $num) {
135  $obj = $db->fetch_object($result);
136  $mode = $obj->mode;
137 
138  //print $obj->dm.' '.$obj->mode.' '.$previousmonth.' '.$previousmode;
139  if ($obj->mode == 'claimed' && !empty($previousmode))
140  {
141  print '<tr class="oddeven">';
142  print '<td class="nowrap">'.$previousmonth."</td>\n";
143  print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
144  print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
145  print "</tr>\n";
146 
147  $amountclaimed = 0;
148  $amountpaid = 0;
149  }
150 
151  if ($obj->mode == 'claimed')
152  {
153  $amountclaimed = $obj->mm;
154  $totalclaimed = $totalclaimed + $amountclaimed;
155  }
156  if ($obj->mode == 'paid')
157  {
158  $amountpaid = $obj->mm;
159  $totalpaid = $totalpaid + $amountpaid;
160  }
161 
162  if ($obj->mode == 'paid')
163  {
164  print '<tr class="oddeven">';
165  print '<td class="nowrap">'.$obj->dm."</td>\n";
166  print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
167  print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
168  print "</tr>\n";
169  $amountclaimed = 0;
170  $amountpaid = 0;
171  $previousmode = '';
172  $previousmonth = '';
173  } else {
174  $previousmode = $obj->mode;
175  $previousmonth = $obj->dm;
176  }
177 
178  $i++;
179  }
180 
181  if ($mode == 'claimed' && !empty($previousmode))
182  {
183  print '<tr class="oddeven">';
184  print '<td class="nowrap">'.$previousmonth."</td>\n";
185  print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
186  print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
187  print "</tr>\n";
188 
189  $amountclaimed = 0;
190  $amountpaid = 0;
191  }
192 
193  print '<tr class="liste_total">';
194  print '<td class="right">'.$langs->trans("Total").'</td>';
195  print '<td class="nowrap right">'.price($totalclaimed).'</td>';
196  print '<td class="nowrap right">'.price($totalpaid).'</td>';
197  print "</tr>";
198 
199  print "</table>";
200 
201  $db->free($result);
202  } else {
203  dol_print_error($db);
204  }
205 }
206 
207 
208 /*
209  * View
210  */
211 
212 $form = new Form($db);
213 $company_static = new Societe($db);
214 $tva = new Tva($db);
215 
216 $fsearch = '<!-- hidden fields for form -->';
217 $fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
218 $fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
219 
220 $description = $fsearch;
221 
222 // Show report header
223 $name = $langs->trans("VATReportByMonth");
224 $calcmode = '';
225 if ($modetax == 0) $calcmode = $langs->trans('OptionVATDefault');
226 if ($modetax == 1) $calcmode = $langs->trans('OptionVATDebitOption');
227 if ($modetax == 2) $calcmode = $langs->trans('OptionPaymentForProductAndServices');
228 $calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRules", DOL_URL_ROOT.'/admin/taxes.php').')</span>';
229 
230 $description .= $langs->trans("VATSummary").'<br>';
231 if ($conf->global->TAX_MODE_SELL_PRODUCT == 'invoice') $description .= $langs->trans("RulesVATDueProducts");
232 if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment') $description .= $langs->trans("RulesVATInProducts");
233 if ($conf->global->TAX_MODE_SELL_SERVICE == 'invoice') $description .= '<br>'.$langs->trans("RulesVATDueServices");
234 if ($conf->global->TAX_MODE_SELL_SERVICE == 'payment') $description .= '<br>'.$langs->trans("RulesVATInServices");
235 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
236  $description .= '<br>'.$langs->trans("DepositsAreNotIncluded");
237 }
238 if (!empty($conf->global->MAIN_MODULE_ACCOUNTING)) $description .= '<br>'.$langs->trans("ThisIsAnEstimatedValue");
239 
240 $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
241 
242 $builddate = dol_now();
243 
244 
245 llxHeader('', $name);
246 
247 //$textprevyear="<a href=\"index.php?year=" . ($year_current-1) . "\">".img_previous($langs->trans("Previous"), 'class="valignbottom"')."</a>";
248 //$textnextyear=" <a href=\"index.php?year=" . ($year_current+1) . "\">".img_next($langs->trans("Next"), 'class="valignbottom"')."</a>";
249 //print load_fiche_titre($langs->transcountry("VAT", $mysoc->country_code), $textprevyear." ".$langs->trans("Year")." ".$year_start." ".$textnextyear, 'bill');
250 
251 report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
252 //report_header($name,'',$textprevyear.$langs->trans("Year")." ".$year_start.$textnextyear,'',$description,$builddate,$exportlink,array(),$calcmode);
253 
254 
255 print '<br>';
256 
257 print '<div class="fichecenter"><div class="fichethirdleft">';
258 
259 print load_fiche_titre($langs->trans("VATSummary"), '', '');
260 
261 print '<table class="noborder centpercent">';
262 print '<tr class="liste_titre">';
263 print '<td width="30%">'.$langs->trans("Year")." ".$y.'</td>';
264 print '<td class="right">'.$langs->trans("VATToPay").'</td>';
265 print '<td class="right">'.$langs->trans("VATToCollect").'</td>';
266 print '<td class="right">'.$langs->trans("Balance").'</td>';
267 print '<td>&nbsp;</td>'."\n";
268 print '</tr>'."\n";
269 
270 $tmp = dol_getdate($date_start);
271 $y = $tmp['year'];
272 $m = $tmp['mon'];
273 $tmp = dol_getdate($date_end);
274 $yend = $tmp['year'];
275 $mend = $tmp['mon'];
276 //var_dump($m);
277 $total = 0; $subtotalcoll = 0; $subtotalpaye = 0; $subtotal = 0;
278 $i = 0; $mcursor = 0;
279 
280 while ((($y < $yend) || ($y == $yend && $m <= $mend)) && $mcursor < 1000) // $mcursor is to avoid too large loop
281 {
282  //$m = $conf->global->SOCIETE_FISCAL_MONTH_START + ($mcursor % 12);
283  if ($m == 13) $y++;
284  if ($m > 12) $m -= 12;
285  $mcursor++;
286 
287  $x_coll = tax_by_rate('vat', $db, $y, 0, 0, 0, $modetax, 'sell', $m);
288  $x_paye = tax_by_rate('vat', $db, $y, 0, 0, 0, $modetax, 'buy', $m);
289 
290  $x_both = array();
291  //now, from these two arrays, get another array with one rate per line
292  foreach (array_keys($x_coll) as $my_coll_rate)
293  {
294  $x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht'];
295  $x_both[$my_coll_rate]['coll']['vat'] = $x_coll[$my_coll_rate]['vat'];
296  $x_both[$my_coll_rate]['paye']['totalht'] = 0;
297  $x_both[$my_coll_rate]['paye']['vat'] = 0;
298  $x_both[$my_coll_rate]['coll']['links'] = '';
299  $x_both[$my_coll_rate]['coll']['detail'] = array();
300  foreach ($x_coll[$my_coll_rate]['facid'] as $id=>$dummy) {
301  //$invoice_customer->id=$x_coll[$my_coll_rate]['facid'][$id];
302  //$invoice_customer->ref=$x_coll[$my_coll_rate]['facnum'][$id];
303  //$invoice_customer->type=$x_coll[$my_coll_rate]['type'][$id];
304  //$company_static->fetch($x_coll[$my_coll_rate]['company_id'][$id]);
305  $x_both[$my_coll_rate]['coll']['detail'][] = array(
306  'id' =>$x_coll[$my_coll_rate]['facid'][$id],
307  'descr' =>$x_coll[$my_coll_rate]['descr'][$id],
308  'pid' =>$x_coll[$my_coll_rate]['pid'][$id],
309  'pref' =>$x_coll[$my_coll_rate]['pref'][$id],
310  'ptype' =>$x_coll[$my_coll_rate]['ptype'][$id],
311  'payment_id'=>$x_coll[$my_coll_rate]['payment_id'][$id],
312  'payment_amount'=>$x_coll[$my_coll_rate]['payment_amount'][$id],
313  'ftotal_ttc'=>$x_coll[$my_coll_rate]['ftotal_ttc'][$id],
314  'dtotal_ttc'=>$x_coll[$my_coll_rate]['dtotal_ttc'][$id],
315  'dtype' =>$x_coll[$my_coll_rate]['dtype'][$id],
316  'datef' =>$x_coll[$my_coll_rate]['datef'][$id],
317  'datep' =>$x_coll[$my_coll_rate]['datep'][$id],
318  //'company_link'=>$company_static->getNomUrl(1,'',20),
319  'ddate_start'=>$x_coll[$my_coll_rate]['ddate_start'][$id],
320  'ddate_end' =>$x_coll[$my_coll_rate]['ddate_end'][$id],
321  'totalht' =>$x_coll[$my_coll_rate]['totalht_list'][$id],
322  'vat' =>$x_coll[$my_coll_rate]['vat_list'][$id],
323  //'link' =>$invoice_customer->getNomUrl(1,'',12)
324  );
325  }
326  }
327 
328  // tva paid
329  foreach (array_keys($x_paye) as $my_paye_rate) {
330  $x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht'];
331  $x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat'];
332  if (!isset($x_both[$my_paye_rate]['coll']['totalht'])) {
333  $x_both[$my_paye_rate]['coll']['totalht'] = 0;
334  $x_both[$my_paye_rate]['coll']['vat'] = 0;
335  }
336  $x_both[$my_paye_rate]['paye']['links'] = '';
337  $x_both[$my_paye_rate]['paye']['detail'] = array();
338 
339  foreach ($x_paye[$my_paye_rate]['facid'] as $id=>$dummy)
340  {
341  // ExpenseReport
342  if ($x_paye[$my_paye_rate]['ptype'][$id] == 'ExpenseReportPayment')
343  {
344  //$expensereport->id=$x_paye[$my_paye_rate]['facid'][$id];
345  //$expensereport->ref=$x_paye[$my_paye_rate]['facnum'][$id];
346  //$expensereport->type=$x_paye[$my_paye_rate]['type'][$id];
347 
348  $x_both[$my_paye_rate]['paye']['detail'][] = array(
349  'id' =>$x_paye[$my_paye_rate]['facid'][$id],
350  'descr' =>$x_paye[$my_paye_rate]['descr'][$id],
351  'pid' =>$x_paye[$my_paye_rate]['pid'][$id],
352  'pref' =>$x_paye[$my_paye_rate]['pref'][$id],
353  'ptype' =>$x_paye[$my_paye_rate]['ptype'][$id],
354  'payment_id' =>$x_paye[$my_paye_rate]['payment_id'][$id],
355  'payment_amount' =>$x_paye[$my_paye_rate]['payment_amount'][$id],
356  'ftotal_ttc' =>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
357  'dtotal_ttc' =>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
358  'dtype' =>$x_paye[$my_paye_rate]['dtype'][$id],
359  'ddate_start' =>$x_paye[$my_paye_rate]['ddate_start'][$id],
360  'ddate_end' =>$x_paye[$my_paye_rate]['ddate_end'][$id],
361  'totalht' =>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
362  'vat' =>$x_paye[$my_paye_rate]['vat_list'][$id],
363  //'link' =>$expensereport->getNomUrl(1)
364  );
365  } else {
366  //$invoice_supplier->id=$x_paye[$my_paye_rate]['facid'][$id];
367  //$invoice_supplier->ref=$x_paye[$my_paye_rate]['facnum'][$id];
368  //$invoice_supplier->type=$x_paye[$my_paye_rate]['type'][$id];
369  //$company_static->fetch($x_paye[$my_paye_rate]['company_id'][$id]);
370  $x_both[$my_paye_rate]['paye']['detail'][] = array(
371  'id' =>$x_paye[$my_paye_rate]['facid'][$id],
372  'descr' =>$x_paye[$my_paye_rate]['descr'][$id],
373  'pid' =>$x_paye[$my_paye_rate]['pid'][$id],
374  'pref' =>$x_paye[$my_paye_rate]['pref'][$id],
375  'ptype' =>$x_paye[$my_paye_rate]['ptype'][$id],
376  'payment_id'=>$x_paye[$my_paye_rate]['payment_id'][$id],
377  'payment_amount'=>$x_paye[$my_paye_rate]['payment_amount'][$id],
378  'ftotal_ttc'=>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
379  'dtotal_ttc'=>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
380  'dtype' =>$x_paye[$my_paye_rate]['dtype'][$id],
381  'datef' =>$x_paye[$my_paye_rate]['datef'][$id],
382  'datep' =>$x_paye[$my_paye_rate]['datep'][$id],
383  //'company_link'=>$company_static->getNomUrl(1,'',20),
384  'ddate_start'=>$x_paye[$my_paye_rate]['ddate_start'][$id],
385  'ddate_end' =>$x_paye[$my_paye_rate]['ddate_end'][$id],
386  'totalht' =>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
387  'vat' =>$x_paye[$my_paye_rate]['vat_list'][$id],
388  //'link' =>$invoice_supplier->getNomUrl(1,'',12)
389  );
390  }
391  }
392  }
393  //now we have an array (x_both) indexed by rates for coll and paye
394 
395  $action = "tva";
396  $object = array(&$x_coll, &$x_paye, &$x_both);
397  $parameters["mode"] = $modetax;
398  $parameters["year"] = $y;
399  $parameters["month"] = $m;
400  $parameters["type"] = 'vat';
401 
402  // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
403  $hookmanager->initHooks(array('externalbalance'));
404  $reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
405 
406  if (!is_array($x_coll) && $coll_listbuy == -1)
407  {
408  $langs->load("errors");
409  print '<tr><td colspan="5">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
410  break;
411  }
412  if (!is_array($x_paye) && $coll_listbuy == -2)
413  {
414  print '<tr><td colspan="5">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
415  break;
416  }
417 
418 
419  print '<tr class="oddeven">';
420  print '<td class="nowrap"><a href="'.DOL_URL_ROOT.'/compta/tva/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>';
421 
422  $x_coll_sum = 0;
423  foreach (array_keys($x_coll) as $rate)
424  {
425  $subtot_coll_total_ht = 0;
426  $subtot_coll_vat = 0;
427 
428  foreach ($x_both[$rate]['coll']['detail'] as $index => $fields)
429  {
430  // Payment
431  $ratiopaymentinvoice = 1;
432  if ($modetax != 1)
433  {
434  // Define type
435  // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
436  $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
437  // Try to enhance type detection using date_start and date_end for free lines where type
438  // was not saved.
439  if (!empty($fields['ddate_start'])) {
440  $type = 1;
441  }
442  if (!empty($fields['ddate_end'])) {
443  $type = 1;
444  }
445 
446  if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
447  || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice'))
448  {
449  //print $langs->trans("NA");
450  } else {
451  if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
452  $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
453  }
454  }
455  }
456  //var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
457  $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
458  $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
459  $subtot_coll_total_ht += $temp_ht;
460  $subtot_coll_vat += $temp_vat;
461  $x_coll_sum += $temp_vat;
462  }
463  }
464  print '<td class="nowrap right">'.price(price2num($x_coll_sum, 'MT')).'</td>';
465 
466  $x_paye_sum = 0;
467  foreach (array_keys($x_paye) as $rate)
468  {
469  $subtot_paye_total_ht = 0;
470  $subtot_paye_vat = 0;
471 
472  foreach ($x_both[$rate]['paye']['detail'] as $index => $fields)
473  {
474  // Payment
475  $ratiopaymentinvoice = 1;
476  if ($modetax != 1)
477  {
478  // Define type
479  // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
480  $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
481  // Try to enhance type detection using date_start and date_end for free lines where type
482  // was not saved.
483  if (!empty($fields['ddate_start'])) {
484  $type = 1;
485  }
486  if (!empty($fields['ddate_end'])) {
487  $type = 1;
488  }
489 
490  if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
491  || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice'))
492  {
493  //print $langs->trans("NA");
494  } else {
495  if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
496  $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
497  }
498  }
499  }
500  //var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
501  $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
502  $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
503  $subtot_paye_total_ht += $temp_ht;
504  $subtot_paye_vat += $temp_vat;
505  $x_paye_sum += $temp_vat;
506  }
507  }
508  print '<td class="nowrap right">'.price(price2num($x_paye_sum, 'MT')).'</td>';
509 
510  $subtotalcoll = $subtotalcoll + $x_coll_sum;
511  $subtotalpaye = $subtotalpaye + $x_paye_sum;
512 
513  $diff = $x_coll_sum - $x_paye_sum;
514  $total = $total + $diff;
515  $subtotal = price2num($subtotal + $diff, 'MT');
516 
517  print '<td class="nowrap right">'.price(price2num($diff, 'MT')).'</td>'."\n";
518  print "<td>&nbsp;</td>\n";
519  print "</tr>\n";
520 
521  $i++; $m++;
522  if ($i > 2)
523  {
524  print '<tr class="liste_total">';
525  print '<td class="right"><a href="quadri_detail.php?leftmenu=tax_vat&q='.round($m / 3).'&year='.$y.'">'.$langs->trans("SubTotal").'</a>:</td>';
526  print '<td class="nowrap right">'.price(price2num($subtotalcoll, 'MT')).'</td>';
527  print '<td class="nowrap right">'.price(price2num($subtotalpaye, 'MT')).'</td>';
528  print '<td class="nowrap right">'.price(price2num($subtotal, 'MT')).'</td>';
529  print '<td>&nbsp;</td></tr>';
530  $i = 0;
531  $subtotalcoll = 0; $subtotalpaye = 0; $subtotal = 0;
532  }
533 }
534 print '<tr class="liste_total"><td class="right" colspan="3">'.$langs->trans("TotalToPay").':</td><td class="nowrap right">'.price(price2num($total, 'MT')).'</td>';
535 print "<td>&nbsp;</td>\n";
536 print '</tr>';
537 
538 print '</table>';
539 
540 
541 print '</div><div class="fichetwothirdright"><div class="ficheaddleft">';
542 
543 
544 
545 /*
546  * Paid
547  */
548 
549 print load_fiche_titre($langs->trans("VATPaid"), '', '');
550 
551 $sql = '';
552 
553 $sql .= "SELECT SUM(amount) as mm, date_format(f.datev,'%Y-%m') as dm, 'claimed' as mode";
554 $sql .= " FROM ".MAIN_DB_PREFIX."tva as f";
555 $sql .= " WHERE f.entity = ".$conf->entity;
556 $sql .= " AND (f.datev >= '".$db->idate($date_start)."' AND f.datev <= '".$db->idate($date_end)."')";
557 $sql .= " GROUP BY dm";
558 
559 $sql .= " UNION ";
560 
561 $sql .= "SELECT SUM(amount) as mm, date_format(f.datep,'%Y-%m') as dm, 'paid' as mode";
562 $sql .= " FROM ".MAIN_DB_PREFIX."tva as f";
563 $sql .= " WHERE f.entity = ".$conf->entity;
564 $sql .= " AND (f.datep >= '".$db->idate($date_start)."' AND f.datep <= '".$db->idate($date_end)."')";
565 $sql .= " GROUP BY dm";
566 
567 $sql .= " ORDER BY dm ASC, mode ASC";
568 //print $sql;
569 
570 pt($db, $sql, $langs->trans("Month"));
571 
572 
573 if (!empty($conf->global->MAIN_FEATURES_LEVEL))
574 {
575  print '<br>';
576 
577  /*
578  * Recap
579  */
580 
581  print load_fiche_titre($langs->trans("VATBalance"), '', ''); // need to add translation
582 
583  $sql1 = "SELECT SUM(amount) as mm";
584  $sql1 .= " FROM ".MAIN_DB_PREFIX."tva as f";
585  $sql1 .= " WHERE f.entity = ".$conf->entity;
586  $sql1 .= " AND f.datev >= '".$db->idate($date_start)."'";
587  $sql1 .= " AND f.datev <= '".$db->idate($date_end)."'";
588 
589  $result = $db->query($sql1);
590  if ($result) {
591  $obj = $db->fetch_object($result);
592  print '<table class="noborder centpercent">';
593 
594  print "<tr>";
595  print '<td class="right">'.$langs->trans("VATDue").'</td>';
596  print '<td class="nowrap right">'.price(price2num($total, 'MT')).'</td>';
597  print "</tr>\n";
598 
599  print "<tr>";
600  print '<td class="right">'.$langs->trans("VATPaid").'</td>';
601  print '<td class="nowrap right">'.price(price2num($obj->mm, 'MT'))."</td>\n";
602  print "</tr>\n";
603 
604  $restopay = $total - $obj->mm;
605  print "<tr>";
606  print '<td class="right">'.$langs->trans("RemainToPay").'</td>';
607  print '<td class="nowrap right">'.price(price2num($restopay, 'MT')).'</td>';
608  print "</tr>\n";
609 
610  print '</table>';
611  }
612 }
613 
614 print '</div></div>';
615 
616 llxFooter();
617 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_now($mode= 'auto')
Return date for now.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink= '', $moreparam=array(), $calcmode= '', $varlink= '')
Show header of a report.
Definition: report.lib.php:41
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:481
Put here description of your class.
Definition: tva.class.php:34
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
llxHeader()
Empty header.
Definition: wrapper.php:45
Class to manage generation of HTML components Only common components must be here.
tax_by_rate($type, $db, $y, $q, $date_start, $date_end, $modetax, $direction, $m=0)
Gets Tax to collect for the given year (and given quarter or month) The function gets the Tax in spli...
Definition: tax.lib.php:543
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
Class to manage third parties objects (customers, suppliers, prospects...)
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_getdate($timestamp, $fast=false, $forcetimezone= '')
Return an array with locale date info.
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:498
pt($db, $sql, $date)
print function
Definition: index.php:87
print
Draft customers invoices.
Definition: index.php:89
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
llxFooter()
Empty footer.
Definition: wrapper.php:59
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
Definition: date.lib.php:114