dolibarr  13.0.2
clients.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) 2006 Yannick Warnier <ywarnier@beeznest.org>
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 
29 require '../../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
34 require_once DOL_DOCUMENT_ROOT.'/compta/localtax/class/localtax.class.php';
35 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
36 require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
37 require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
38 require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
39 require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
40 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
41 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
42 
43 // Load translation files required by the page
44 $langs->loadLangs(array("other", "compta", "banks", "bills", "companies", "product", "trips", "admin"));
45 
46 
47 $now = dol_now();
48 $current_date = dol_getdate($now);
49 if (empty($conf->global->SOCIETE_FISCAL_MONTH_START)) $conf->global->SOCIETE_FISCAL_MONTH_START = 1;
50 
51 // Date range
52 $year = GETPOST("year", "int");
53 if (empty($year))
54 {
55  $year_current = $current_date['year'];
56  $year_start = $year_current;
57 } else {
58  $year_current = $year;
59  $year_start = $year;
60 }
61 $date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"));
62 $date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"));
63 // Set default period if not defined
64 if (empty($date_start) || empty($date_end)) // We define date_start and date_end
65 {
66  $q = GETPOST("q", "int");
67  if (empty($q))
68  {
69  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 {
70  if (empty($conf->global->MAIN_INFO_VAT_RETURN) || $conf->global->MAIN_INFO_VAT_RETURN == 2) { // quaterly vat, we take last past complete quarter
71  $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');
72  $date_end = dol_time_plus_duree($date_start, 3, 'm') - 1;
73  } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 3) { // yearly vat
74  if ($current_date['mon'] < $conf->global->SOCIETE_FISCAL_MONTH_START) {
75  if (($conf->global->SOCIETE_FISCAL_MONTH_START - $current_date['mon']) > 6) { // If period started from less than 6 years, we show past year
76  $year_start--;
77  }
78  } else {
79  if (($current_date['mon'] - $conf->global->SOCIETE_FISCAL_MONTH_START) < 6) { // If perdio started from less than 6 years, we show past year
80  $year_start--;
81  }
82  }
83  $date_start = dol_get_first_day($year_start, $conf->global->SOCIETE_FISCAL_MONTH_START, false);
84  $date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
85  } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 1) { // monthly vat, we take last past complete month
86  $date_start = dol_time_plus_duree(dol_get_first_day($year_start, $current_date['mon'], false), -1, 'm');
87  $date_end = dol_time_plus_duree($date_start, 1, 'm') - 1;
88  }
89  }
90  } else {
91  if ($q == 1) { $date_start = dol_get_first_day($year_start, 1, false); $date_end = dol_get_last_day($year_start, 3, false); }
92  if ($q == 2) { $date_start = dol_get_first_day($year_start, 4, false); $date_end = dol_get_last_day($year_start, 6, false); }
93  if ($q == 3) { $date_start = dol_get_first_day($year_start, 7, false); $date_end = dol_get_last_day($year_start, 9, false); }
94  if ($q == 4) { $date_start = dol_get_first_day($year_start, 10, false); $date_end = dol_get_last_day($year_start, 12, false); }
95  }
96 }
97 
98 $min = price2num(GETPOST("min", "alpha"));
99 if (empty($min)) $min = 0;
100 
101 // Define modetax (0 or 1)
102 // 0=normal, 1=option vat for services is on debit, 2=option on payments for products
103 $modetax = $conf->global->TAX_MODE;
104 if (GETPOSTISSET("modetax")) $modetax = GETPOST("modetax", 'int');
105 if (empty($modetax)) $modetax = 0;
106 
107 // Security check
108 $socid = GETPOST('socid', 'int');
109 if ($user->socid) $socid = $user->socid;
110 $result = restrictedArea($user, 'tax', '', '', 'charges');
111 
112 
113 
114 /*
115  * View
116  */
117 
118 $form = new Form($db);
119 $company_static = new Societe($db);
120 $invoice_customer = new Facture($db);
121 $invoice_supplier = new FactureFournisseur($db);
122 $expensereport = new ExpenseReport($db);
123 $product_static = new Product($db);
124 $payment_static = new Paiement($db);
125 $paymentfourn_static = new PaiementFourn($db);
126 $paymentexpensereport_static = new PaymentExpenseReport($db);
127 $user_static = new User($db);
128 
129 $morequerystring = '';
130 $listofparams = array('date_startmonth', 'date_startyear', 'date_startday', 'date_endmonth', 'date_endyear', 'date_endday');
131 foreach ($listofparams as $param)
132 {
133  if (GETPOST($param) != '') $morequerystring .= ($morequerystring ? '&' : '').$param.'='.GETPOST($param);
134 }
135 
136 $special_report = false;
137 if (isset($_REQUEST['extra_report']) && $_REQUEST['extra_report'] == 1) {
138  $special_report = true;
139 }
140 
141 llxHeader('', $langs->trans("VATReport"), '', '', 0, 0, '', '', $morequerystring);
142 
143 $fsearch = '<!-- hidden fields for form -->';
144 $fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
145 $fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
146 $fsearch .= $langs->trans("SalesTurnoverMinimum").': ';
147 $fsearch .= '<input type="text" name="min" id="min" value="'.$min.'" size="6">';
148 
149 // Show report header
150 $name = $langs->trans("VATReportByThirdParties");
151 $calcmode = '';
152 if ($modetax == 0) $calcmode = $langs->trans('OptionVATDefault');
153 if ($modetax == 1) $calcmode = $langs->trans('OptionVATDebitOption');
154 if ($modetax == 2) $calcmode = $langs->trans('OptionPaymentForProductAndServices');
155 $calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRules", DOL_URL_ROOT.'/admin/taxes.php').')</span>';
156 // Set period
157 $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
158 $prevyear = $year_start;
159 $prevquarter = $q;
160 if ($prevquarter > 1) {
161  $prevquarter--;
162 } else {
163  $prevquarter = 4;
164  $prevyear--;
165 }
166 $nextyear = $year_start;
167 $nextquarter = $q;
168 if ($nextquarter < 4) {
169  $nextquarter++;
170 } else {
171  $nextquarter = 1;
172  $nextyear++;
173 }
174 $builddate = dol_now();
175 
176 if ($conf->global->TAX_MODE_SELL_PRODUCT == 'invoice') $description .= $langs->trans("RulesVATDueProducts");
177 if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment') $description .= $langs->trans("RulesVATInProducts");
178 if ($conf->global->TAX_MODE_SELL_SERVICE == 'invoice') $description .= '<br>'.$langs->trans("RulesVATDueServices");
179 if ($conf->global->TAX_MODE_SELL_SERVICE == 'payment') $description .= '<br>'.$langs->trans("RulesVATInServices");
180 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
181  $description .= '<br>'.$langs->trans("DepositsAreNotIncluded");
182 }
183 if (!empty($conf->global->MAIN_MODULE_ACCOUNTING)) $description .= '<br>'.$langs->trans("ThisIsAnEstimatedValue");
184 
185 //$periodlink=($year_start?"<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start-1)."&modetax=".$modetax."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start+1)."&modetax=".$modetax."'>".img_next()."</a>":"");
186 $description .= ($description ? '<br>' : '').$fsearch;
187 if (!empty($conf->global->TAX_REPORT_EXTRA_REPORT))
188 {
189  $description .= '<br>';
190  $description .= '<input type="radio" name="extra_report" value="0" '.($special_report ? '' : 'checked="checked"').'> ';
191  $description .= $langs->trans('SimpleReport');
192  $description .= '</input>';
193  $description .= '<br>';
194  $description .= '<input type="radio" name="extra_report" value="1" '.($special_report ? 'checked="checked"' : '').'> ';
195  $description .= $langs->trans('AddExtraReport');
196  $description .= '</input>';
197  $description .= '<br>';
198 }
199 
200 $elementcust = $langs->trans("CustomersInvoices");
201 $productcust = $langs->trans("Description");
202 $namerate = $langs->trans("VATRate");
203 $amountcust = $langs->trans("AmountHT");
204 if ($mysoc->tva_assuj) {
205  $vatcust .= ' ('.$langs->trans("StatusToPay").')';
206 }
207 $elementsup = $langs->trans("SuppliersInvoices");
208 $productsup = $langs->trans("Description");
209 $amountsup = $langs->trans("AmountHT");
210 if ($mysoc->tva_assuj) {
211  $vatsup .= ' ('.$langs->trans("ToGetBack").')';
212 }
213 report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
214 
215 $vatcust = $langs->trans("VATReceived");
216 $vatsup = $langs->trans("VATPaid");
217 
218 
219 // VAT Received
220 print '<div class="div-table-responsive">';
221 print "<table class=\"noborder\" width=\"100%\">";
222 
223 $y = $year_current;
224 $total = 0;
225 $i = 0;
226 $columns = 5;
227 
228 // Load arrays of datas
229 $x_coll = tax_by_thirdparty('vat', $db, 0, $date_start, $date_end, $modetax, 'sell');
230 $x_paye = tax_by_thirdparty('vat', $db, 0, $date_start, $date_end, $modetax, 'buy');
231 
232 if (!is_array($x_coll) || !is_array($x_paye))
233 {
234  $langs->load("errors");
235  if ($x_coll == -1) {
236  print '<tr><td colspan="'.$columns.'">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
237  } elseif ($x_coll == -2) {
238  print '<tr><td colspan="'.$columns.'">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
239  } else {
240  print '<tr><td colspan="'.$columns.'">'.$langs->trans("Error").'</td></tr>';
241  }
242 } else {
243  $x_both = array();
244  //now, from these two arrays, get another array with one rate per line
245  foreach (array_keys($x_coll) as $my_coll_thirdpartyid)
246  {
247  $x_both[$my_coll_thirdpartyid]['coll']['totalht'] = $x_coll[$my_coll_thirdpartyid]['totalht'];
248  $x_both[$my_coll_thirdpartyid]['coll']['vat'] = $x_coll[$my_coll_thirdpartyid]['vat'];
249  $x_both[$my_coll_thirdpartyid]['paye']['totalht'] = 0;
250  $x_both[$my_coll_thirdpartyid]['paye']['vat'] = 0;
251  $x_both[$my_coll_thirdpartyid]['coll']['links'] = '';
252  $x_both[$my_coll_thirdpartyid]['coll']['detail'] = array();
253  foreach ($x_coll[$my_coll_thirdpartyid]['facid'] as $id=>$dummy) {
254  $invoice_customer->id = $x_coll[$my_coll_thirdpartyid]['facid'][$id];
255  $invoice_customer->ref = $x_coll[$my_coll_thirdpartyid]['facnum'][$id];
256  $invoice_customer->type = $x_coll[$my_coll_thirdpartyid]['type'][$id];
257  $company_static->fetch($x_coll[$my_coll_thirdpartyid]['company_id'][$id]);
258  $x_both[$my_coll_thirdpartyid]['coll']['detail'][] = array(
259  'id' =>$x_coll[$my_coll_thirdpartyid]['facid'][$id],
260  'descr' =>$x_coll[$my_coll_thirdpartyid]['descr'][$id],
261  'pid' =>$x_coll[$my_coll_thirdpartyid]['pid'][$id],
262  'pref' =>$x_coll[$my_coll_thirdpartyid]['pref'][$id],
263  'ptype' =>$x_coll[$my_coll_thirdpartyid]['ptype'][$id],
264  'payment_id'=>$x_coll[$my_coll_thirdpartyid]['payment_id'][$id],
265  'payment_amount'=>$x_coll[$my_coll_thirdpartyid]['payment_amount'][$id],
266  'ftotal_ttc'=>$x_coll[$my_coll_thirdpartyid]['ftotal_ttc'][$id],
267  'dtotal_ttc'=>$x_coll[$my_coll_thirdpartyid]['dtotal_ttc'][$id],
268  'dtype' =>$x_coll[$my_coll_thirdpartyid]['dtype'][$id],
269  'drate' =>$x_coll[$my_coll_thirdpartyid]['drate'][$id],
270  'datef' =>$x_coll[$my_coll_thirdpartyid]['datef'][$id],
271  'datep' =>$x_coll[$my_coll_thirdpartyid]['datep'][$id],
272  'company_link'=>$company_static->getNomUrl(1, '', 20),
273  'ddate_start'=>$x_coll[$my_coll_thirdpartyid]['ddate_start'][$id],
274  'ddate_end' =>$x_coll[$my_coll_thirdpartyid]['ddate_end'][$id],
275  'totalht' =>$x_coll[$my_coll_thirdpartyid]['totalht_list'][$id],
276  'vat' =>$x_coll[$my_coll_thirdpartyid]['vat_list'][$id],
277  'link' =>$invoice_customer->getNomUrl(1, '', 12)
278  );
279  }
280  }
281  // tva paid
282  foreach (array_keys($x_paye) as $my_paye_thirdpartyid) {
283  $x_both[$my_paye_thirdpartyid]['paye']['totalht'] = $x_paye[$my_paye_thirdpartyid]['totalht'];
284  $x_both[$my_paye_thirdpartyid]['paye']['vat'] = $x_paye[$my_paye_thirdpartyid]['vat'];
285  if (!isset($x_both[$my_paye_thirdpartyid]['coll']['totalht'])) {
286  $x_both[$my_paye_thirdpartyid]['coll']['totalht'] = 0;
287  $x_both[$my_paye_thirdpartyid]['coll']['vat'] = 0;
288  }
289  $x_both[$my_paye_thirdpartyid]['paye']['links'] = '';
290  $x_both[$my_paye_thirdpartyid]['paye']['detail'] = array();
291 
292  foreach ($x_paye[$my_paye_thirdpartyid]['facid'] as $id=>$dummy)
293  {
294  // ExpenseReport
295  if ($x_paye[$my_paye_thirdpartyid]['ptype'][$id] == 'ExpenseReportPayment')
296  {
297  $expensereport->id = $x_paye[$my_paye_thirdpartyid]['facid'][$id];
298  $expensereport->ref = $x_paye[$my_paye_thirdpartyid]['facnum'][$id];
299  $expensereport->type = $x_paye[$my_paye_thirdpartyid]['type'][$id];
300 
301  $x_both[$my_paye_thirdpartyid]['paye']['detail'][] = array(
302  'id' =>$x_paye[$my_paye_thirdpartyid]['facid'][$id],
303  'descr' =>$x_paye[$my_paye_thirdpartyid]['descr'][$id],
304  'pid' =>$x_paye[$my_paye_thirdpartyid]['pid'][$id],
305  'pref' =>$x_paye[$my_paye_thirdpartyid]['pref'][$id],
306  'ptype' =>$x_paye[$my_paye_thirdpartyid]['ptype'][$id],
307  'payment_id' =>$x_paye[$my_paye_thirdpartyid]['payment_id'][$id],
308  'payment_amount' =>$x_paye[$my_paye_thirdpartyid]['payment_amount'][$id],
309  'ftotal_ttc' =>price2num($x_paye[$my_paye_thirdpartyid]['ftotal_ttc'][$id]),
310  'dtotal_ttc' =>price2num($x_paye[$my_paye_thirdpartyid]['dtotal_ttc'][$id]),
311  'dtype' =>$x_paye[$my_paye_thirdpartyid]['dtype'][$id],
312  'drate' =>$x_paye[$my_coll_thirdpartyid]['drate'][$id],
313  'ddate_start' =>$x_paye[$my_paye_thirdpartyid]['ddate_start'][$id],
314  'ddate_end' =>$x_paye[$my_paye_thirdpartyid]['ddate_end'][$id],
315  'totalht' =>price2num($x_paye[$my_paye_thirdpartyid]['totalht_list'][$id]),
316  'vat' =>$x_paye[$my_paye_thirdpartyid]['vat_list'][$id],
317  'link' =>$expensereport->getNomUrl(1)
318  );
319  } else {
320  $invoice_supplier->id = $x_paye[$my_paye_thirdpartyid]['facid'][$id];
321  $invoice_supplier->ref = $x_paye[$my_paye_thirdpartyid]['facnum'][$id];
322  $invoice_supplier->type = $x_paye[$my_paye_thirdpartyid]['type'][$id];
323  $company_static->fetch($x_paye[$my_paye_thirdpartyid]['company_id'][$id]);
324  $x_both[$my_paye_thirdpartyid]['paye']['detail'][] = array(
325  'id' =>$x_paye[$my_paye_thirdpartyid]['facid'][$id],
326  'descr' =>$x_paye[$my_paye_thirdpartyid]['descr'][$id],
327  'pid' =>$x_paye[$my_paye_thirdpartyid]['pid'][$id],
328  'pref' =>$x_paye[$my_paye_thirdpartyid]['pref'][$id],
329  'ptype' =>$x_paye[$my_paye_thirdpartyid]['ptype'][$id],
330  'payment_id'=>$x_paye[$my_paye_thirdpartyid]['payment_id'][$id],
331  'payment_amount'=>$x_paye[$my_paye_thirdpartyid]['payment_amount'][$id],
332  'ftotal_ttc'=>price2num($x_paye[$my_paye_thirdpartyid]['ftotal_ttc'][$id]),
333  'dtotal_ttc'=>price2num($x_paye[$my_paye_thirdpartyid]['dtotal_ttc'][$id]),
334  'dtype' =>$x_paye[$my_paye_thirdpartyid]['dtype'][$id],
335  'drate' =>$x_paye[$my_coll_thirdpartyid]['drate'][$id],
336  'datef' =>$x_paye[$my_paye_thirdpartyid]['datef'][$id],
337  'datep' =>$x_paye[$my_paye_thirdpartyid]['datep'][$id],
338  'company_link'=>$company_static->getNomUrl(1, '', 20),
339  'ddate_start'=>$x_paye[$my_paye_thirdpartyid]['ddate_start'][$id],
340  'ddate_end' =>$x_paye[$my_paye_thirdpartyid]['ddate_end'][$id],
341  'totalht' =>price2num($x_paye[$my_paye_thirdpartyid]['totalht_list'][$id]),
342  'vat' =>$x_paye[$my_paye_thirdpartyid]['vat_list'][$id],
343  'link' =>$invoice_supplier->getNomUrl(1, '', 12)
344  );
345  }
346  }
347  }
348  //now we have an array (x_both) indexed by rates for coll and paye
349 
350 
351  //print table headers for this quadri - incomes first
352 
353  $x_coll_sum = 0;
354  $x_coll_ht = 0;
355  $x_paye_sum = 0;
356  $x_paye_ht = 0;
357 
358  $span = $columns;
359  if ($modetax != 1) $span += 2;
360 
361  //print '<tr><td colspan="'.($span+1).'">'..')</td></tr>';
362 
363  // Customers invoices
364  print '<tr class="liste_titre">';
365  print '<td class="left">'.$elementcust.'</td>';
366  print '<td class="left">'.$langs->trans("DateInvoice").'</td>';
367  if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment' || $conf->global->TAX_MODE_SELL_SERVICE == 'payment') print '<td class="left">'.$langs->trans("DatePayment").'</td>';
368  else print '<td></td>';
369  print '<td class="right">'.$namerate.'</td>';
370  print '<td class="left">'.$productcust.'</td>';
371  if ($modetax != 1)
372  {
373  print '<td class="right">'.$amountcust.'</td>';
374  print '<td class="right">'.$langs->trans("Payment").' ('.$langs->trans("PercentOfInvoice").')</td>';
375  }
376  print '<td class="right">'.$langs->trans("AmountHTVATRealReceived").'</td>';
377  print '<td class="right">'.$vatcust.'</td>';
378  print '</tr>';
379 
380  $action = "tvadetail";
381  $parameters["mode"] = $modetax;
382  $parameters["start"] = $date_start;
383  $parameters["end"] = $date_end;
384  $parameters["type"] = 'vat';
385 
386  $object = array(&$x_coll, &$x_paye, &$x_both);
387  // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
388  $hookmanager->initHooks(array('externalbalance'));
389  $reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
390 
391  foreach (array_keys($x_coll) as $thirdparty_id) {
392  $subtot_coll_total_ht = 0;
393  $subtot_coll_vat = 0;
394 
395  if ($min == 0 || ($min > 0 && $x_both[$thirdparty_id]['coll']['totalht'] > $min))
396  {
397  if (is_array($x_both[$thirdparty_id]['coll']['detail']))
398  {
399  // VAT Rate
400  print "<tr>";
401  print '<td class="tax_rate">';
402  if (is_numeric($thirdparty_id)) {
403  $company_static->fetch($thirdparty_id);
404  print $langs->trans("ThirdParty").': '.$company_static->getNomUrl(1);
405  } else {
406  $tmpid = preg_replace('/userid_/', '', $thirdparty_id);
407  $user_static->fetch($tmpid);
408  print $langs->trans("User").': '.$user_static->getNomUrl(1);
409  }
410  print '</td><td colspan="'.($span + 1).'"></td>';
411  print '</tr>'."\n";
412 
413  foreach ($x_both[$thirdparty_id]['coll']['detail'] as $index => $fields) {
414  // Define type
415  // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
416  $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
417  // Try to enhance type detection using date_start and date_end for free lines where type
418  // was not saved.
419  if (!empty($fields['ddate_start'])) {
420  $type = 1;
421  }
422  if (!empty($fields['ddate_end'])) {
423  $type = 1;
424  }
425 
426  print '<tr class="oddeven">';
427 
428  // Ref
429  print '<td class="nowrap left">'.$fields['link'].'</td>';
430 
431  // Invoice date
432  print '<td class="left">'.dol_print_date($fields['datef'], 'day').'</td>';
433 
434  // Payment date
435  if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment' || $conf->global->TAX_MODE_SELL_SERVICE == 'payment') {
436  print '<td class="left">'.dol_print_date($fields['datep'], 'day').'</td>';
437  } else {
438  print '<td></td>';
439  }
440 
441  // Rate
442  print '<td class="right">'.$fields['drate'].'</td>';
443 
444  // Description
445  print '<td class="left">';
446  if ($fields['pid']) {
447  $product_static->id = $fields['pid'];
448  $product_static->ref = $fields['pref'];
449  $product_static->type = $fields['dtype']; // We force with the type of line to have type how line is registered
450  print $product_static->getNomUrl(1);
451  if (dol_string_nohtmltag($fields['descr'])) {
452  print ' - '.dol_trunc(dol_string_nohtmltag($fields['descr']), 24);
453  }
454  } else {
455  if ($type) {
456  $text = img_object($langs->trans('Service'), 'service');
457  } else {
458  $text = img_object($langs->trans('Product'), 'product');
459  }
460  if (preg_match('/^\((.*)\)$/', $fields['descr'], $reg)) {
461  if ($reg[1] == 'DEPOSIT') {
462  $fields['descr'] = $langs->transnoentitiesnoconv('Deposit');
463  } elseif ($reg[1] == 'CREDIT_NOTE') {
464  $fields['descr'] = $langs->transnoentitiesnoconv('CreditNote');
465  } else {
466  $fields['descr'] = $langs->transnoentitiesnoconv($reg[1]);
467  }
468  }
469  print $text.' '.dol_trunc(dol_string_nohtmltag($fields['descr']), 24);
470 
471  // Show range
472  print_date_range($fields['ddate_start'], $fields['ddate_end']);
473  }
474  print '</td>';
475 
476  // Total HT
477  if ($modetax != 1) {
478  print '<td class="nowrap right">';
479  print price($fields['totalht']);
480  if (price2num($fields['ftotal_ttc'])) {
481  //print $fields['dtotal_ttc']."/".$fields['ftotal_ttc']." - ";
482  $ratiolineinvoice = ($fields['dtotal_ttc'] / $fields['ftotal_ttc']);
483  //print ' ('.round($ratiolineinvoice*100,2).'%)';
484  }
485  print '</td>';
486  }
487 
488  // Payment
489  $ratiopaymentinvoice = 1;
490  if ($modetax != 1) {
491  print '<td class="nowrap right">';
492  //print $fields['totalht']."-".$fields['payment_amount']."-".$fields['ftotal_ttc'];
493  if ($fields['payment_amount'] && $fields['ftotal_ttc']) {
494  $payment_static->id = $fields['payment_id'];
495  print $payment_static->getNomUrl(2);
496  }
497  if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
498  || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
499  print $langs->trans("NA");
500  } else {
501  if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
502  $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
503  }
504  print price(price2num($fields['payment_amount'], 'MT'));
505  if (isset($fields['payment_amount'])) {
506  print ' ('.round($ratiopaymentinvoice * 100, 2).'%)';
507  }
508  }
509  print '</td>';
510  }
511 
512  // Total collected
513  print '<td class="nowrap right">';
514  $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
515  print price(price2num($temp_ht, 'MT'), 1);
516  print '</td>';
517 
518  // VAT
519  print '<td class="nowrap right">';
520  $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
521  print price(price2num($temp_vat, 'MT'), 1);
522  //print price($fields['vat']);
523  print '</td>';
524  print '</tr>';
525 
526  $subtot_coll_total_ht += $temp_ht;
527  $subtot_coll_vat += $temp_vat;
528  $x_coll_sum += $temp_vat;
529  }
530  }
531  // Total customers for this vat rate
532  print '<tr class="liste_total">';
533  print '<td colspan="4"></td>';
534  print '<td class="right">'.$langs->trans("Total").':</td>';
535  if ($modetax != 1) {
536  print '<td class="nowrap right">&nbsp;</td>';
537  print '<td class="right">&nbsp;</td>';
538  }
539  print '<td class="right">'.price(price2num($subtot_coll_total_ht, 'MT')).'</td>';
540  print '<td class="nowrap right">'.price(price2num($subtot_coll_vat, 'MT')).'</td>';
541  print '</tr>';
542  }
543  }
544 
545  if (count($x_coll) == 0) // Show a total ine if nothing shown
546  {
547  print '<tr class="liste_total">';
548  print '<td colspan="4"></td>';
549  print '<td class="right">'.$langs->trans("Total").':</td>';
550  if ($modetax != 1) {
551  print '<td class="nowrap right">&nbsp;</td>';
552  print '<td class="right">&nbsp;</td>';
553  }
554  print '<td class="right">'.price(price2num(0, 'MT')).'</td>';
555  print '<td class="nowrap right">'.price(price2num(0, 'MT')).'</td>';
556  print '</tr>';
557  }
558 
559  // Blank line
560  print '<tr><td colspan="'.($span + 1).'">&nbsp;</td></tr>';
561 
562  // Print table headers for this quadri - expenses now
563  print '<tr class="liste_titre liste_titre_topborder">';
564  print '<td class="left">'.$elementsup.'</td>';
565  print '<td class="left">'.$langs->trans("DateInvoice").'</td>';
566  if ($conf->global->TAX_MODE_BUY_PRODUCT == 'payment' || $conf->global->TAX_MODE_BUY_SERVICE == 'payment') print '<td class="left">'.$langs->trans("DatePayment").'</td>';
567  else print '<td></td>';
568  print '<td class="left">'.$namesup.'</td>';
569  print '<td class="left">'.$productsup.'</td>';
570  if ($modetax != 1) {
571  print '<td class="right">'.$amountsup.'</td>';
572  print '<td class="right">'.$langs->trans("Payment").' ('.$langs->trans("PercentOfInvoice").')</td>';
573  }
574  print '<td class="right">'.$langs->trans("AmountHTVATRealPaid").'</td>';
575  print '<td class="right">'.$vatsup.'</td>';
576  print '</tr>'."\n";
577 
578  foreach (array_keys($x_paye) as $thirdparty_id)
579  {
580  $subtot_paye_total_ht = 0;
581  $subtot_paye_vat = 0;
582 
583  if ($min == 0 || ($min > 0 && $x_both[$thirdparty_id]['paye']['totalht'] > $min)) {
584  if (is_array($x_both[$thirdparty_id]['paye']['detail'])) {
585  print "<tr>";
586  print '<td class="tax_rate">';
587  if (is_numeric($thirdparty_id)) {
588  $company_static->fetch($thirdparty_id);
589  print $langs->trans("ThirdParty").': '.$company_static->getNomUrl(1);
590  } else {
591  $tmpid = preg_replace('/userid_/', '', $thirdparty_id);
592  $user_static->fetch($tmpid);
593  print $langs->trans("User").': '.$user_static->getNomUrl(1);
594  }
595  print '<td colspan="'.($span + 1).'"></td>';
596  print '</tr>'."\n";
597 
598  foreach ($x_both[$thirdparty_id]['paye']['detail'] as $index => $fields) {
599  // Define type
600  // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
601  $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
602  // Try to enhance type detection using date_start and date_end for free lines where type
603  // was not saved.
604  if (!empty($fields['ddate_start'])) {
605  $type = 1;
606  }
607  if (!empty($fields['ddate_end'])) {
608  $type = 1;
609  }
610 
611 
612  print '<tr class="oddeven">';
613 
614  // Ref
615  print '<td class="nowrap left">'.$fields['link'].'</td>';
616 
617  // Invoice date
618  print '<td class="left">'.dol_print_date($fields['datef'], 'day').'</td>';
619 
620  // Payment date
621  if ($conf->global->TAX_MODE_BUY_PRODUCT == 'payment' || $conf->global->TAX_MODE_BUY_SERVICE == 'payment') {
622  print '<td class="left">'.dol_print_date($fields['datep'], 'day').'</td>';
623  } else {
624  print '<td></td>';
625  }
626 
627  // Company name
628  print '<td class="left">'.$fields['company_link'].'</td>';
629 
630  // Description
631  print '<td class="left">';
632  if ($fields['pid']) {
633  $product_static->id = $fields['pid'];
634  $product_static->ref = $fields['pref'];
635  $product_static->type = $fields['dtype']; // We force with the type of line to have type how line is registered
636  print $product_static->getNomUrl(1);
637  if (dol_string_nohtmltag($fields['descr'])) {
638  print ' - '.dol_trunc(dol_string_nohtmltag($fields['descr']), 24);
639  }
640  } else {
641  if ($type) {
642  $text = img_object($langs->trans('Service'), 'service');
643  } else {
644  $text = img_object($langs->trans('Product'), 'product');
645  }
646  if (preg_match('/^\((.*)\)$/', $fields['descr'], $reg)) {
647  if ($reg[1] == 'DEPOSIT') {
648  $fields['descr'] = $langs->transnoentitiesnoconv('Deposit');
649  } elseif ($reg[1] == 'CREDIT_NOTE') {
650  $fields['descr'] = $langs->transnoentitiesnoconv('CreditNote');
651  } else {
652  $fields['descr'] = $langs->transnoentitiesnoconv($reg[1]);
653  }
654  }
655  print $text.' '.dol_trunc(dol_string_nohtmltag($fields['descr']), 24);
656 
657  // Show range
658  print_date_range($fields['ddate_start'], $fields['ddate_end']);
659  }
660  print '</td>';
661 
662  // Total HT
663  if ($modetax != 1) {
664  print '<td class="nowrap right">';
665  print price($fields['totalht']);
666  if (price2num($fields['ftotal_ttc'])) {
667  //print $fields['dtotal_ttc']."/".$fields['ftotal_ttc']." - ";
668  $ratiolineinvoice = ($fields['dtotal_ttc'] / $fields['ftotal_ttc']);
669  //print ' ('.round($ratiolineinvoice*100,2).'%)';
670  }
671  print '</td>';
672  }
673 
674  // Payment
675  $ratiopaymentinvoice = 1;
676  if ($modetax != 1)
677  {
678  print '<td class="nowrap right">';
679  if ($fields['payment_amount'] && $fields['ftotal_ttc'])
680  {
681  $paymentfourn_static->id = $fields['payment_id'];
682  print $paymentfourn_static->getNomUrl(2);
683  }
684 
685  if (($type == 0 && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice')
686  || ($type == 1 && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice'))
687  {
688  print $langs->trans("NA");
689  } else {
690  if (isset($fields['payment_amount']) && $fields['ftotal_ttc']) {
691  $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
692  }
693  print price(price2num($fields['payment_amount'], 'MT'));
694  if (isset($fields['payment_amount'])) {
695  print ' ('.round($ratiopaymentinvoice * 100, 2).'%)';
696  }
697  }
698  print '</td>';
699  }
700 
701  // VAT paid
702  print '<td class="nowrap right">';
703  $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
704  print price(price2num($temp_ht, 'MT'), 1);
705  print '</td>';
706 
707  // VAT
708  print '<td class="nowrap right">';
709  $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
710  print price(price2num($temp_vat, 'MT'), 1);
711  //print price($fields['vat']);
712  print '</td>';
713  print '</tr>';
714 
715  $subtot_paye_total_ht += $temp_ht;
716  $subtot_paye_vat += $temp_vat;
717  $x_paye_sum += $temp_vat;
718  }
719  }
720  // Total suppliers for this vat rate
721  print '<tr class="liste_total">';
722  print '<td colspan="4"></td>';
723  print '<td class="right">'.$langs->trans("Total").':</td>';
724  if ($modetax != 1) {
725  print '<td class="nowrap right">&nbsp;</td>';
726  print '<td class="right">&nbsp;</td>';
727  }
728  print '<td class="right">'.price(price2num($subtot_paye_total_ht, 'MT')).'</td>';
729  print '<td class="nowrap right">'.price(price2num($subtot_paye_vat, 'MT')).'</td>';
730  print '</tr>';
731  }
732  }
733 
734  if (count($x_paye) == 0) { // Show a total line if nothing shown
735  print '<tr class="liste_total">';
736  print '<td colspan="4"></td>';
737  print '<td class="right">'.$langs->trans("Total").':</td>';
738  if ($modetax != 1) {
739  print '<td class="nowrap right">&nbsp;</td>';
740  print '<td class="right">&nbsp;</td>';
741  }
742  print '<td class="right">'.price(price2num(0, 'MT')).'</td>';
743  print '<td class="nowrap right">'.price(price2num(0, 'MT')).'</td>';
744  print '</tr>';
745  }
746 
747  // Total to pay
748  print '<tr><td colspan="'.($span + 2).'"></td></tr>';
749 
750  $diff = $x_coll_sum - $x_paye_sum;
751  print '<tr class="liste_total">';
752  print '<td class="liste_total" colspan="'.($span + 1).'">'.$langs->trans("TotalToPay").($q ? ', '.$langs->trans("Quadri").' '.$q : '').'</td>';
753  print '<td class="liste_total nowrap right"><b>'.price(price2num($diff, 'MT'))."</b></td>\n";
754  print "</tr>\n";
755 
756  $i++;
757 }
758 
759 print '</table>';
760 print '</div>';
761 
762 llxFooter();
763 
764 $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_string_nohtmltag($stringtoclean, $removelinefeed=1, $pagecodeto= 'UTF-8', $strip_tags=0, $removedoublespaces=1)
Clean a string from all HTML tags and entities.
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...
tax_by_thirdparty($type, $db, $y, $date_start, $date_end, $modetax, $direction, $m=0, $q=0)
Look for collectable VAT clients in the chosen year (and month)
Definition: tax.lib.php:91
Class to manage products or services.
dol_now($mode= 'auto')
Return date for now.
Class to manage Dolibarr users.
Definition: user.class.php:44
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
Class to manage suppliers invoices.
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.
GETPOSTISSET($paramname)
Return true if we are in a context of submitting the parameter $paramname.
Class to manage third parties objects (customers, suppliers, prospects...)
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
Class to manage payments of customer invoices.
dol_getdate($timestamp, $fast=false, $forcetimezone= '')
Return an array with locale date info.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
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.
Class to manage Trips and Expenses.
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
print
Draft customers invoices.
Definition: index.php:89
Class to manage payments of expense report.
dol_trunc($string, $size=40, $trunc= 'right', $stringencoding= 'UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding &#39;...&#39; if string larger than length.
print_date_range($date_start, $date_end, $format= '', $outputlangs= '')
Format output for start and end date.
Class to manage invoices.
Class to manage payments for supplier invoices.
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