dolibarr  13.0.2
tax.lib.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
3  * Copyright (C) 2006-2007 Yannick Warnier <ywarnier@beeznest.org>
4  * Copyright (C) 2011 Regis Houssin <regis.houssin@inodbox.com>
5  * Copyright (C) 2012-2017 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
7  * Copyright (C) 2012-2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
8  * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program. If not, see <https://www.gnu.org/licenses/>.
22  */
23 
38 {
39  global $db, $langs, $conf, $user;
40 
41  $h = 0;
42  $head = array();
43 
44  $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$object->id;
45  $head[$h][1] = $langs->trans('SocialContribution');
46  $head[$h][2] = 'card';
47  $h++;
48 
49  // Show more tabs from modules
50  // Entries must be declared in modules descriptor with line
51  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
52  // $this->tabs = array('entity:-tabname); to remove a tab
53  complete_head_from_modules($conf, $langs, $object, $head, $h, 'tax');
54 
55  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
56  require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
57  $upload_dir = $conf->tax->dir_output."/".dol_sanitizeFileName($object->ref);
58  $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
59  $nbLinks = Link::count($db, $object->element, $object->id);
60  $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/document.php?id='.$object->id;
61  $head[$h][1] = $langs->trans("Documents");
62  if (($nbFiles + $nbLinks) > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
63  $head[$h][2] = 'documents';
64  $h++;
65 
66  $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/info.php?id='.$object->id;
67  $head[$h][1] = $langs->trans("Info");
68  $head[$h][2] = 'info';
69  $h++;
70 
71  complete_head_from_modules($conf, $langs, $object, $head, $h, 'tax', 'remove');
72 
73  return $head;
74 }
75 
76 
91 function tax_by_thirdparty($type, $db, $y, $date_start, $date_end, $modetax, $direction, $m = 0, $q = 0)
92 {
93  global $conf;
94 
95  // If we use date_start and date_end, we must not use $y, $m, $q
96  if (($date_start || $date_end) && (!empty($y) || !empty($m) || !empty($q)))
97  {
98  dol_print_error('', 'Bad value of input parameter for tax_by_rate');
99  }
100 
101  $list = array();
102  if ($direction == 'sell')
103  {
104  $invoicetable = 'facture';
105  $invoicedettable = 'facturedet';
106  $fk_facture = 'fk_facture';
107  $fk_facture2 = 'fk_facture';
108  $fk_payment = 'fk_paiement';
109  $total_tva = 'total_tva';
110  $paymenttable = 'paiement';
111  $paymentfacturetable = 'paiement_facture';
112  $invoicefieldref = 'ref';
113  } elseif ($direction == 'buy')
114  {
115  $invoicetable = 'facture_fourn';
116  $invoicedettable = 'facture_fourn_det';
117  $fk_facture = 'fk_facture_fourn';
118  $fk_facture2 = 'fk_facturefourn';
119  $fk_payment = 'fk_paiementfourn';
120  $total_tva = 'tva';
121  $paymenttable = 'paiementfourn';
122  $paymentfacturetable = 'paiementfourn_facturefourn';
123  $invoicefieldref = 'ref';
124  }
125 
126  if (strpos($type, 'localtax') === 0) {
127  $f_rate = $type.'_tx';
128  } else {
129  $f_rate = 'tva_tx';
130  }
131 
132  $total_localtax1 = 'total_localtax1';
133  $total_localtax2 = 'total_localtax2';
134 
135 
136  // CAS DES BIENS/PRODUITS
137 
138  // Define sql request
139  $sql = '';
140  if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
141  || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice'))
142  {
143  // Count on delivery date (use invoice date as delivery is unknown)
144  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
145  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
146  $sql .= " d.date_start as date_start, d.date_end as date_end,";
147  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
148  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
149  $sql .= " 0 as payment_id, 0 as payment_amount";
150  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
151  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
152  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
153  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
154  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
155  $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
156  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";
157  else $sql .= " AND f.type IN (0,1,2,3,5)";
158  $sql .= " AND f.rowid = d.".$fk_facture;
159  $sql .= " AND s.rowid = f.fk_soc";
160  if ($y && $m)
161  {
162  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
163  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
164  } elseif ($y)
165  {
166  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
167  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
168  }
169  if ($q) $sql .= " AND (date_format(f.datef,'%m') > ".(($q - 1) * 3)." AND date_format(f.datef,'%m') <= ".($q * 3).")";
170  if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
171  $sql .= " AND (d.product_type = 0"; // Limit to products
172  $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
173  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
174  $sql .= " ORDER BY d.rowid, d.".$fk_facture;
175  } else {
176  // Count on payments date
177  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
178  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
179  $sql .= " d.date_start as date_start, d.date_end as date_end,";
180  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
181  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
182  $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
183  $sql .= " pa.datep as datep";
184  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
185  $sql .= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
186  $sql .= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
187  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
188  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
189  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
190  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
191  $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
192  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";
193  else $sql .= " AND f.type IN (0,1,2,3,5)";
194  $sql .= " AND f.rowid = d.".$fk_facture;
195  $sql .= " AND s.rowid = f.fk_soc";
196  $sql .= " AND pf.".$fk_facture2." = f.rowid";
197  $sql .= " AND pa.rowid = pf.".$fk_payment;
198  if ($y && $m)
199  {
200  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
201  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
202  } elseif ($y)
203  {
204  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
205  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
206  }
207  if ($q) $sql .= " AND (date_format(pa.datep,'%m') > ".(($q - 1) * 3)." AND date_format(pa.datep,'%m') <= ".($q * 3).")";
208  if ($date_start && $date_end) $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
209  $sql .= " AND (d.product_type = 0"; // Limit to products
210  $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
211  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
212  $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
213  }
214 
215  if (!$sql) return -1;
216  if ($sql == 'TODO') return -2;
217  if ($sql != 'TODO')
218  {
219  dol_syslog("Tax.lib.php::tax_by_thirdparty", LOG_DEBUG);
220 
221  $resql = $db->query($sql);
222  if ($resql)
223  {
224  $company_id = -1;
225  $oldrowid = '';
226  while ($assoc = $db->fetch_array($resql))
227  {
228  if (!isset($list[$assoc['company_id']]['totalht'])) $list[$assoc['company_id']]['totalht'] = 0;
229  if (!isset($list[$assoc['company_id']]['vat'])) $list[$assoc['company_id']]['vat'] = 0;
230  if (!isset($list[$assoc['company_id']]['localtax1'])) $list[$assoc['company_id']]['localtax1'] = 0;
231  if (!isset($list[$assoc['company_id']]['localtax2'])) $list[$assoc['company_id']]['localtax2'] = 0;
232 
233  if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
234  {
235  $oldrowid = $assoc['rowid'];
236  $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
237  $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
238  $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
239  $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
240  }
241  $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
242  $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
243  $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
244  $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
245  $list[$assoc['company_id']]['company_name'][] = $assoc['company_name'];
246  $list[$assoc['company_id']]['company_id'][] = $assoc['company_id'];
247  $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
248  $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
249  $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
250 
251  $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
252  $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
253  $list[$assoc['company_id']]['type'][] = $assoc['type'];
254  $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
255  $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
256 
257  $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
258  $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
259  $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
260  $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
261 
262  $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
263  $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
264  $list[$assoc['company_id']]['ptype'][] = $assoc['ptype'];
265 
266  $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
267  $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
268 
269  $company_id = $assoc['company_id'];
270  }
271  } else {
272  dol_print_error($db);
273  return -3;
274  }
275  }
276 
277 
278  // CAS DES SERVICES
279 
280  // Define sql request
281  $sql = '';
282  if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')
283  || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice'))
284  {
285  // Count on invoice date
286  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
287  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
288  $sql .= " d.date_start as date_start, d.date_end as date_end,";
289  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
290  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
291  $sql .= " 0 as payment_id, 0 as payment_amount";
292  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
293  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
294  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
295  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
296  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
297  $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
298  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";
299  else $sql .= " AND f.type IN (0,1,2,3,5)";
300  $sql .= " AND f.rowid = d.".$fk_facture;
301  $sql .= " AND s.rowid = f.fk_soc";
302  if ($y && $m)
303  {
304  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
305  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
306  } elseif ($y)
307  {
308  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
309  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
310  }
311  if ($q) $sql .= " AND (date_format(f.datef,'%m') > ".(($q - 1) * 3)." AND date_format(f.datef,'%m') <= ".($q * 3).")";
312  if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
313  $sql .= " AND (d.product_type = 1"; // Limit to services
314  $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
315  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
316  $sql .= " ORDER BY d.rowid, d.".$fk_facture;
317  } else {
318  // Count on payments date
319  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
320  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
321  $sql .= " d.date_start as date_start, d.date_end as date_end,";
322  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
323  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
324  $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
325  $sql .= " pa.datep as datep";
326  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
327  $sql .= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
328  $sql .= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
329  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
330  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
331  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
332  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
333  $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
334  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";
335  else $sql .= " AND f.type IN (0,1,2,3,5)";
336  $sql .= " AND f.rowid = d.".$fk_facture;
337  $sql .= " AND s.rowid = f.fk_soc";
338  $sql .= " AND pf.".$fk_facture2." = f.rowid";
339  $sql .= " AND pa.rowid = pf.".$fk_payment;
340  if ($y && $m)
341  {
342  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
343  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
344  } elseif ($y)
345  {
346  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
347  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
348  }
349  if ($q) $sql .= " AND (date_format(pa.datep,'%m') > ".(($q - 1) * 3)." AND date_format(pa.datep,'%m') <= ".($q * 3).")";
350  if ($date_start && $date_end) $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
351  $sql .= " AND (d.product_type = 1"; // Limit to services
352  $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
353  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
354  $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
355  }
356 
357  if (!$sql)
358  {
359  dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
360  return -1; // -1 = Not accountancy module enabled
361  }
362  if ($sql == 'TODO') return -2; // -2 = Feature not yet available
363  if ($sql != 'TODO')
364  {
365  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
366  $resql = $db->query($sql);
367  if ($resql)
368  {
369  $company_id = -1;
370  $oldrowid = '';
371  while ($assoc = $db->fetch_array($resql))
372  {
373  if (!isset($list[$assoc['company_id']]['totalht'])) $list[$assoc['company_id']]['totalht'] = 0;
374  if (!isset($list[$assoc['company_id']]['vat'])) $list[$assoc['company_id']]['vat'] = 0;
375  if (!isset($list[$assoc['company_id']]['localtax1'])) $list[$assoc['company_id']]['localtax1'] = 0;
376  if (!isset($list[$assoc['company_id']]['localtax2'])) $list[$assoc['company_id']]['localtax2'] = 0;
377 
378  if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
379  {
380  $oldrowid = $assoc['rowid'];
381  $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
382  $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
383  $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
384  $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
385  }
386  $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
387  $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
388  $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
389  $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
390  $list[$assoc['company_id']]['company_name'][] = $assoc['company_name'];
391  $list[$assoc['company_id']]['company_id'][] = $assoc['company_id'];
392  $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
393  $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
394  $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
395 
396  $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
397  $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
398  $list[$assoc['company_id']]['type'][] = $assoc['type'];
399  $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
400  $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
401 
402  $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
403  $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
404  $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
405  $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
406 
407  $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
408  $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
409  $list[$assoc['company_id']]['ptype'][] = $assoc['ptype'];
410 
411  $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
412  $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
413 
414  $company_id = $assoc['company_id'];
415  }
416  } else {
417  dol_print_error($db);
418  return -3;
419  }
420  }
421 
422 
423  // CASE OF EXPENSE REPORT
424 
425  if ($direction == 'buy') // buy only for expense reports
426  {
427  // Define sql request
428  $sql = '';
429 
430  // Count on payments date
431  $sql = "SELECT d.rowid, d.product_type as dtype, e.rowid as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.total_tva as total_vat, e.note_private as descr,";
432  $sql .= " d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
433  $sql .= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
434  $sql .= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
435  $sql .= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
436  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as e";
437  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid ";
438  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid ";
439  $sql .= " WHERE e.entity = ".$conf->entity;
440  $sql .= " AND e.fk_statut in (6)";
441  if ($y && $m)
442  {
443  $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
444  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
445  } elseif ($y)
446  {
447  $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
448  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
449  }
450  if ($q) $sql .= " AND (date_format(p.datep,'%m') > ".(($q - 1) * 3)." AND date_format(p.datep,'%m') <= ".($q * 3).")";
451  if ($date_start && $date_end) $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
452  $sql .= " AND (d.product_type = -1";
453  $sql .= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
454  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.total_tva <> 0)";
455  $sql .= " ORDER BY e.rowid";
456 
457  if (!$sql)
458  {
459  dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
460  return -1; // -1 = Not accountancy module enabled
461  }
462  if ($sql == 'TODO') return -2; // -2 = Feature not yet available
463  if ($sql != 'TODO')
464  {
465  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
466  $resql = $db->query($sql);
467  if ($resql)
468  {
469  $company_id = -1;
470  $oldrowid = '';
471  while ($assoc = $db->fetch_array($resql))
472  {
473  if (!isset($list[$assoc['company_id']]['totalht'])) $list[$assoc['company_id']]['totalht'] = 0;
474  if (!isset($list[$assoc['company_id']]['vat'])) $list[$assoc['company_id']]['vat'] = 0;
475  if (!isset($list[$assoc['company_id']]['localtax1'])) $list[$assoc['company_id']]['localtax1'] = 0;
476  if (!isset($list[$assoc['company_id']]['localtax2'])) $list[$assoc['company_id']]['localtax2'] = 0;
477 
478  if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
479  {
480  $oldrowid = $assoc['rowid'];
481  $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
482  $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
483  $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
484  $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
485  }
486 
487  $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
488  $list[$assoc['company_id']]['dtype'][] = 'ExpenseReportPayment';
489  $list[$assoc['company_id']]['datef'][] = $assoc['datef'];
490  $list[$assoc['company_id']]['company_name'][] = '';
491  $list[$assoc['company_id']]['company_id'][] = '';
492  $list[$assoc['company_id']]['user_id'][] = $assoc['fk_user_author'];
493  $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
494  $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
495  $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
496 
497  $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
498  $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
499  $list[$assoc['company_id']]['type'][] = $assoc['type'];
500  $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
501  $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
502 
503  $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
504  $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
505  $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
506  $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
507 
508  $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
509  $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
510  $list[$assoc['company_id']]['ptype'][] = 'ExpenseReportPayment';
511 
512  $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
513  $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
514 
515  $company_id = $assoc['company_id'];
516  }
517  } else {
518  dol_print_error($db);
519  return -3;
520  }
521  }
522  }
523 
524  return $list;
525 }
526 
543 function tax_by_rate($type, $db, $y, $q, $date_start, $date_end, $modetax, $direction, $m = 0)
544 {
545  global $conf;
546 
547  // If we use date_start and date_end, we must not use $y, $m, $q
548  if (($date_start || $date_end) && (!empty($y) || !empty($m) || !empty($q)))
549  {
550  dol_print_error('', 'Bad value of input parameter for tax_by_rate');
551  }
552 
553  $list = array();
554 
555  if ($direction == 'sell')
556  {
557  $invoicetable = 'facture';
558  $invoicedettable = 'facturedet';
559  $fk_facture = 'fk_facture';
560  $fk_facture2 = 'fk_facture';
561  $fk_payment = 'fk_paiement';
562  $total_tva = 'total_tva';
563  $paymenttable = 'paiement';
564  $paymentfacturetable = 'paiement_facture';
565  $invoicefieldref = 'ref';
566  } else {
567  $invoicetable = 'facture_fourn';
568  $invoicedettable = 'facture_fourn_det';
569  $fk_facture = 'fk_facture_fourn';
570  $fk_facture2 = 'fk_facturefourn';
571  $fk_payment = 'fk_paiementfourn';
572  $total_tva = 'tva';
573  $paymenttable = 'paiementfourn';
574  $paymentfacturetable = 'paiementfourn_facturefourn';
575  $invoicefieldref = 'ref';
576  }
577 
578  if (strpos($type, 'localtax') === 0) {
579  $f_rate = $type.'_tx';
580  } else {
581  $f_rate = 'tva_tx';
582  }
583 
584  $total_localtax1 = 'total_localtax1';
585  $total_localtax2 = 'total_localtax2';
586 
587 
588  // CAS DES BIENS/PRODUITS
589 
590  // Define sql request
591  $sql = '';
592  if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
593  || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice'))
594  {
595  // Count on delivery date (use invoice date as delivery is unknown)
596  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
597  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
598  $sql .= " d.date_start as date_start, d.date_end as date_end,";
599  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
600  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
601  $sql .= " 0 as payment_id, 0 as payment_amount";
602  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
603  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
604  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
605  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
606  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
607  $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
608  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";
609  else $sql .= " AND f.type IN (0,1,2,3,5)";
610  $sql .= " AND f.rowid = d.".$fk_facture;
611  $sql .= " AND s.rowid = f.fk_soc";
612  if ($y && $m)
613  {
614  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
615  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
616  } elseif ($y)
617  {
618  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
619  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
620  }
621  if ($q) $sql .= " AND (date_format(f.datef,'%m') > ".(($q - 1) * 3)." AND date_format(f.datef,'%m') <= ".($q * 3).")";
622  if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
623  $sql .= " AND (d.product_type = 0"; // Limit to products
624  $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
625  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
626  $sql .= " ORDER BY d.rowid, d.".$fk_facture;
627  } else {
628  // Count on payments date
629  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
630  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
631  $sql .= " d.date_start as date_start, d.date_end as date_end,";
632  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
633  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
634  $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
635  $sql .= " pa.datep as datep";
636  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
637  $sql .= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
638  $sql .= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
639  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
640  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
641  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
642  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
643  $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
644  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";
645  else $sql .= " AND f.type IN (0,1,2,3,5)";
646  $sql .= " AND f.rowid = d.".$fk_facture;
647  $sql .= " AND s.rowid = f.fk_soc";
648  $sql .= " AND pf.".$fk_facture2." = f.rowid";
649  $sql .= " AND pa.rowid = pf.".$fk_payment;
650  if ($y && $m)
651  {
652  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
653  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
654  } elseif ($y)
655  {
656  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
657  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
658  }
659  if ($q) $sql .= " AND (date_format(pa.datep,'%m') > ".(($q - 1) * 3)." AND date_format(pa.datep,'%m') <= ".($q * 3).")";
660  if ($date_start && $date_end) $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
661  $sql .= " AND (d.product_type = 0"; // Limit to products
662  $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
663  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
664  $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
665  }
666 
667  if (!$sql) return -1;
668  if ($sql == 'TODO') return -2;
669  if ($sql != 'TODO')
670  {
671  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
672 
673  $resql = $db->query($sql);
674  if ($resql)
675  {
676  $rate = -1;
677  $oldrowid = '';
678  while ($assoc = $db->fetch_array($resql))
679  {
680  // Code to avoid warnings when array entry not defined
681  if (!isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht'] = 0;
682  if (!isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat'] = 0;
683  if (!isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1'] = 0;
684  if (!isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2'] = 0;
685 
686  if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
687  {
688  $oldrowid = $assoc['rowid'];
689  $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
690  $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
691  $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
692  $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
693  }
694  $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
695  $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
696  $list[$assoc['rate']]['datef'][] = $db->jdate($assoc['datef']);
697  $list[$assoc['rate']]['datep'][] = $db->jdate($assoc['datep']);
698  $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
699  $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
700  $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
701  $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
702 
703  $list[$assoc['rate']]['facid'][] = $assoc['facid'];
704  $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
705  $list[$assoc['rate']]['type'][] = $assoc['type'];
706  $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
707  $list[$assoc['rate']]['descr'][] = $assoc['descr'];
708 
709  $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
710  $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
711  $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
712  $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
713 
714  $list[$assoc['rate']]['pid'][] = $assoc['pid'];
715  $list[$assoc['rate']]['pref'][] = $assoc['pref'];
716  $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
717 
718  $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
719  $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
720 
721  $rate = $assoc['rate'];
722  }
723  } else {
724  dol_print_error($db);
725  return -3;
726  }
727  }
728 
729 
730  // CAS DES SERVICES
731 
732  // Define sql request
733  $sql = '';
734  if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')
735  || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice'))
736  {
737  // Count on invoice date
738  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
739  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
740  $sql .= " d.date_start as date_start, d.date_end as date_end,";
741  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
742  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
743  $sql .= " 0 as payment_id, 0 as payment_amount";
744  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
745  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
746  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
747  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
748  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
749  $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
750  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";
751  else $sql .= " AND f.type IN (0,1,2,3,5)";
752  $sql .= " AND f.rowid = d.".$fk_facture;
753  $sql .= " AND s.rowid = f.fk_soc";
754  if ($y && $m)
755  {
756  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
757  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
758  } elseif ($y)
759  {
760  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
761  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
762  }
763  if ($q) $sql .= " AND (date_format(f.datef,'%m') > ".(($q - 1) * 3)." AND date_format(f.datef,'%m') <= ".($q * 3).")";
764  if ($date_start && $date_end) $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
765  $sql .= " AND (d.product_type = 1"; // Limit to services
766  $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
767  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
768  $sql .= " ORDER BY d.rowid, d.".$fk_facture;
769  } else {
770  // Count on payments date
771  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
772  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
773  $sql .= " d.date_start as date_start, d.date_end as date_end,";
774  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
775  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
776  $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
777  $sql .= " pa.datep as datep";
778  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
779  $sql .= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
780  $sql .= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
781  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
782  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
783  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
784  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
785  $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
786  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .= " AND f.type IN (0,1,2,5)";
787  else $sql .= " AND f.type IN (0,1,2,3,5)";
788  $sql .= " AND f.rowid = d.".$fk_facture;
789  $sql .= " AND s.rowid = f.fk_soc";
790  $sql .= " AND pf.".$fk_facture2." = f.rowid";
791  $sql .= " AND pa.rowid = pf.".$fk_payment;
792  if ($y && $m)
793  {
794  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
795  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
796  } elseif ($y)
797  {
798  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
799  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
800  }
801  if ($q) $sql .= " AND (date_format(pa.datep,'%m') > ".(($q - 1) * 3)." AND date_format(pa.datep,'%m') <= ".($q * 3).")";
802  if ($date_start && $date_end) $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
803  $sql .= " AND (d.product_type = 1"; // Limit to services
804  $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
805  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
806  $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
807  }
808 
809  if (!$sql)
810  {
811  dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
812  return -1; // -1 = Not accountancy module enabled
813  }
814  if ($sql == 'TODO') return -2; // -2 = Feature not yet available
815  if ($sql != 'TODO')
816  {
817  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
818  $resql = $db->query($sql);
819  if ($resql)
820  {
821  $rate = -1;
822  $oldrowid = '';
823  while ($assoc = $db->fetch_array($resql))
824  {
825  // Code to avoid warnings when array entry not defined
826  if (!isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht'] = 0;
827  if (!isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat'] = 0;
828  if (!isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1'] = 0;
829  if (!isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2'] = 0;
830 
831  if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
832  {
833  $oldrowid = $assoc['rowid'];
834  $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
835  $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
836  $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
837  $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
838  }
839  $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
840  $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
841  $list[$assoc['rate']]['datef'][] = $db->jdate($assoc['datef']);
842  $list[$assoc['rate']]['datep'][] = $db->jdate($assoc['datep']);
843  $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
844  $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
845  $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
846  $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
847 
848  $list[$assoc['rate']]['facid'][] = $assoc['facid'];
849  $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
850  $list[$assoc['rate']]['type'][] = $assoc['type'];
851  $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
852  $list[$assoc['rate']]['descr'][] = $assoc['descr'];
853 
854  $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
855  $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
856  $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
857  $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
858 
859  $list[$assoc['rate']]['pid'][] = $assoc['pid'];
860  $list[$assoc['rate']]['pref'][] = $assoc['pref'];
861  $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
862 
863  $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
864  $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
865 
866  $rate = $assoc['rate'];
867  }
868  } else {
869  dol_print_error($db);
870  return -3;
871  }
872  }
873 
874 
875  // CASE OF EXPENSE REPORT
876 
877  if ($direction == 'buy') // buy only for expense reports
878  {
879  // Define sql request
880  $sql = '';
881 
882  // Count on payments date
883  $sql = "SELECT d.rowid, d.product_type as dtype, e.rowid as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.total_tva as total_vat, e.note_private as descr,";
884  $sql .= " d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
885  $sql .= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
886  $sql .= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
887  $sql .= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
888  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as e ";
889  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid ";
890  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid ";
891  $sql .= " WHERE e.entity = ".$conf->entity;
892  $sql .= " AND e.fk_statut in (6)";
893  if ($y && $m)
894  {
895  $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
896  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
897  } elseif ($y)
898  {
899  $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
900  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
901  }
902  if ($q) $sql .= " AND (date_format(p.datep,'%m') > ".(($q - 1) * 3)." AND date_format(p.datep,'%m') <= ".($q * 3).")";
903  if ($date_start && $date_end) $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
904  $sql .= " AND (d.product_type = -1";
905  $sql .= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
906  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql .= " AND (d.".$f_rate." <> 0 OR d.total_tva <> 0)";
907  $sql .= " ORDER BY e.rowid";
908 
909  if (!$sql)
910  {
911  dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
912  return -1; // -1 = Not accountancy module enabled
913  }
914  if ($sql == 'TODO') return -2; // -2 = Feature not yet available
915  if ($sql != 'TODO')
916  {
917  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
918  $resql = $db->query($sql);
919  if ($resql)
920  {
921  $rate = -1;
922  $oldrowid = '';
923  while ($assoc = $db->fetch_array($resql))
924  {
925  // Code to avoid warnings when array entry not defined
926  if (!isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht'] = 0;
927  if (!isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat'] = 0;
928  if (!isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1'] = 0;
929  if (!isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2'] = 0;
930 
931  if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
932  {
933  $oldrowid = $assoc['rowid'];
934  $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
935  $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
936  $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
937  $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
938  }
939 
940  $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
941  $list[$assoc['rate']]['dtype'][] = 'ExpenseReportPayment';
942  $list[$assoc['rate']]['datef'][] = $assoc['datef'];
943  $list[$assoc['rate']]['company_name'][] = '';
944  $list[$assoc['rate']]['company_id'][] = '';
945  $list[$assoc['rate']]['user_id'][] = $assoc['fk_user_author'];
946  $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
947  $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
948 
949  $list[$assoc['rate']]['facid'][] = $assoc['facid'];
950  $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
951  $list[$assoc['rate']]['type'][] = $assoc['type'];
952  $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
953  $list[$assoc['rate']]['descr'][] = $assoc['descr'];
954 
955  $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
956  $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
957  $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
958  $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
959 
960  $list[$assoc['rate']]['pid'][] = $assoc['pid'];
961  $list[$assoc['rate']]['pref'][] = $assoc['pref'];
962  $list[$assoc['rate']]['ptype'][] = 'ExpenseReportPayment';
963 
964  $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
965  $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
966 
967  $rate = $assoc['rate'];
968  }
969  } else {
970  dol_print_error($db);
971  return -3;
972  }
973  }
974  }
975 
976  return $list;
977 }
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
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
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
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_sanitizeFileName($str, $newstr= '_', $unaccent=1)
Clean a string to use it as a file name.
dol_dir_list($path, $types="all", $recursive=0, $filter="", $excludefilter=null, $sortcriteria="name", $sortorder=SORT_ASC, $mode=0, $nohook=0, $relativename="", $donotfollowsymlinks=0)
Scan a directory and return a list of files/directories.
Definition: files.lib.php:60
tax_prepare_head(ChargeSociales $object)
Prepare array with list of tabs.
Definition: tax.lib.php:37
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
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
complete_head_from_modules($conf, $langs, $object, &$head, &$h, $type, $mode= 'add')
Complete or removed entries into a head array (used to build tabs).
Classe permettant la gestion des paiements des charges La tva collectee n&#39;est calculee que sur les fa...