dolibarr  16.0.1
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  * Copyright (C) 2021-2022 Open-Dsi <support@open-dsi.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program. If not, see <https://www.gnu.org/licenses/>.
23  */
24 
39 {
40  global $db, $langs, $conf, $user;
41 
42  $h = 0;
43  $head = array();
44 
45  $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$object->id;
46  $head[$h][1] = $langs->trans('SocialContribution');
47  $head[$h][2] = 'card';
48  $h++;
49 
50  // Show more tabs from modules
51  // Entries must be declared in modules descriptor with line
52  // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
53  // $this->tabs = array('entity:-tabname); to remove a tab
54  complete_head_from_modules($conf, $langs, $object, $head, $h, 'tax');
55 
56  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
57  require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
58  $upload_dir = $conf->tax->dir_output."/".dol_sanitizeFileName($object->ref);
59  $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
60  $nbLinks = Link::count($db, $object->element, $object->id);
61  $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/document.php?id='.$object->id;
62  $head[$h][1] = $langs->trans("Documents");
63  if (($nbFiles + $nbLinks) > 0) {
64  $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
65  }
66  $head[$h][2] = 'documents';
67  $h++;
68 
69 
70  $nbNote = 0;
71  if (!empty($object->note_private)) {
72  $nbNote++;
73  }
74  if (!empty($object->note_public)) {
75  $nbNote++;
76  }
77  $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/note.php?id='.$object->id;
78  $head[$h][1] = $langs->trans('Notes');
79  if ($nbNote > 0) {
80  $head[$h][1] .= (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '<span class="badge marginleftonlyshort">'.$nbNote.'</span>' : '');
81  }
82  $head[$h][2] = 'note';
83  $h++;
84 
85 
86  $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/info.php?id='.$object->id;
87  $head[$h][1] = $langs->trans("Info");
88  $head[$h][2] = 'info';
89  $h++;
90 
91 
92  complete_head_from_modules($conf, $langs, $object, $head, $h, 'tax', 'remove');
93 
94  return $head;
95 }
96 
97 
112 function tax_by_thirdparty($type, $db, $y, $date_start, $date_end, $modetax, $direction, $m = 0, $q = 0)
113 {
114  global $conf;
115 
116  // If we use date_start and date_end, we must not use $y, $m, $q
117  if (($date_start || $date_end) && (!empty($y) || !empty($m) || !empty($q))) {
118  dol_print_error('', 'Bad value of input parameter for tax_by_rate');
119  }
120 
121  $list = array();
122  if ($direction == 'sell') {
123  $invoicetable = 'facture';
124  $invoicedettable = 'facturedet';
125  $fk_facture = 'fk_facture';
126  $fk_facture2 = 'fk_facture';
127  $fk_payment = 'fk_paiement';
128  $total_tva = 'total_tva';
129  $paymenttable = 'paiement';
130  $paymentfacturetable = 'paiement_facture';
131  $invoicefieldref = 'ref';
132  } elseif ($direction == 'buy') {
133  $invoicetable = 'facture_fourn';
134  $invoicedettable = 'facture_fourn_det';
135  $fk_facture = 'fk_facture_fourn';
136  $fk_facture2 = 'fk_facturefourn';
137  $fk_payment = 'fk_paiementfourn';
138  $total_tva = 'tva';
139  $paymenttable = 'paiementfourn';
140  $paymentfacturetable = 'paiementfourn_facturefourn';
141  $invoicefieldref = 'ref';
142  }
143 
144  if (strpos($type, 'localtax') === 0) {
145  $f_rate = $type.'_tx';
146  } else {
147  $f_rate = 'tva_tx';
148  }
149 
150  $total_localtax1 = 'total_localtax1';
151  $total_localtax2 = 'total_localtax2';
152 
153 
154  // CAS DES BIENS/PRODUITS
155 
156  // Define sql request
157  $sql = '';
158  if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
159  || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice')) {
160  // Count on delivery date (use invoice date as delivery is unknown)
161  $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,";
162  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
163  $sql .= " d.date_start as date_start, d.date_end as date_end,";
164  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
165  $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
166  $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
167  $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
168  $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
169  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
170  $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
171  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
172  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
173  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
174  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
175  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
176  $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
177  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
178  $sql .= " AND f.type IN (0,1,2,5)";
179  } else {
180  $sql .= " AND f.type IN (0,1,2,3,5)";
181  }
182  $sql .= " AND f.rowid = d.".$fk_facture;
183  $sql .= " AND s.rowid = f.fk_soc";
184  if ($y && $m) {
185  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
186  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
187  } elseif ($y) {
188  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
189  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
190  }
191  if ($q) {
192  $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
193  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
194  }
195  if ($date_start && $date_end) {
196  $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
197  }
198  $sql .= " AND (d.product_type = 0"; // Limit to products
199  $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
200  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
201  $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
202  }
203  $sql .= " ORDER BY d.rowid, d.".$fk_facture;
204  } else {
205  // Count on payments date
206  $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,";
207  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
208  $sql .= " d.date_start as date_start, d.date_end as date_end,";
209  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
210  $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
211  $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
212  $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
213  $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
214  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
215  $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
216  $sql .= " pa.datep as datep, pa.ref as payment_ref";
217  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
218  $sql .= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
219  $sql .= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
220  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
221  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
222  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
223  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
224  $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
225  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
226  $sql .= " AND f.type IN (0,1,2,5)";
227  } else {
228  $sql .= " AND f.type IN (0,1,2,3,5)";
229  }
230  $sql .= " AND f.rowid = d.".$fk_facture;
231  $sql .= " AND s.rowid = f.fk_soc";
232  $sql .= " AND pf.".$fk_facture2." = f.rowid";
233  $sql .= " AND pa.rowid = pf.".$fk_payment;
234  if ($y && $m) {
235  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
236  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
237  } elseif ($y) {
238  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
239  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
240  }
241  if ($q) {
242  $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
243  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
244  }
245  if ($date_start && $date_end) {
246  $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
247  }
248  $sql .= " AND (d.product_type = 0"; // Limit to products
249  $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
250  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
251  $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
252  }
253  $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
254  }
255 
256  if (!$sql) {
257  return -1;
258  }
259  if ($sql == 'TODO') {
260  return -2;
261  }
262  if ($sql != 'TODO') {
263  dol_syslog("Tax.lib.php::tax_by_thirdparty", LOG_DEBUG);
264 
265  $resql = $db->query($sql);
266  if ($resql) {
267  $company_id = -1;
268  $oldrowid = '';
269  while ($assoc = $db->fetch_array($resql)) {
270  if (!isset($list[$assoc['company_id']]['totalht'])) {
271  $list[$assoc['company_id']]['totalht'] = 0;
272  }
273  if (!isset($list[$assoc['company_id']]['vat'])) {
274  $list[$assoc['company_id']]['vat'] = 0;
275  }
276  if (!isset($list[$assoc['company_id']]['localtax1'])) {
277  $list[$assoc['company_id']]['localtax1'] = 0;
278  }
279  if (!isset($list[$assoc['company_id']]['localtax2'])) {
280  $list[$assoc['company_id']]['localtax2'] = 0;
281  }
282 
283  if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
284  $oldrowid = $assoc['rowid'];
285  $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
286  $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
287  $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
288  $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
289  }
290  $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
291  $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
292  $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
293  $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
294 
295  $list[$assoc['company_id']]['company_name'][] = $assoc['company_name'];
296  $list[$assoc['company_id']]['company_id'][] = $assoc['company_id'];
297  $list[$assoc['company_id']]['company_alias'][] = $assoc['company_alias'];
298  $list[$assoc['company_id']]['company_email'][] = $assoc['company_email'];
299  $list[$assoc['company_id']]['company_tva_intra'][] = $assoc['company_tva_intra'];
300  $list[$assoc['company_id']]['company_client'][] = $assoc['company_client'];
301  $list[$assoc['company_id']]['company_fournisseur'][] = $assoc['company_fournisseur'];
302  $list[$assoc['company_id']]['company_customer_code'][] = $assoc['company_customer_code'];
303  $list[$assoc['company_id']]['company_supplier_code'][] = $assoc['company_supplier_code'];
304  $list[$assoc['company_id']]['company_customer_accounting_code'][] = $assoc['company_customer_accounting_code'];
305  $list[$assoc['company_id']]['company_supplier_accounting_code'][] = $assoc['company_supplier_accounting_code'];
306  $list[$assoc['company_id']]['company_status'][] = $assoc['company_status'];
307 
308  $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
309  $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
310  $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
311 
312  $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
313  $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
314  $list[$assoc['company_id']]['type'][] = $assoc['type'];
315  $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
316  $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
317 
318  $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
319  $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
320  $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
321  $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
322 
323  $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
324  $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
325  $list[$assoc['company_id']]['ptype'][] = $assoc['ptype'];
326 
327  $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
328  $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
329 
330  $company_id = $assoc['company_id'];
331  }
332  } else {
333  dol_print_error($db);
334  return -3;
335  }
336  }
337 
338 
339  // CAS DES SERVICES
340 
341  // Define sql request
342  $sql = '';
343  if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')
344  || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice')) {
345  // Count on invoice date
346  $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,";
347  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
348  $sql .= " d.date_start as date_start, d.date_end as date_end,";
349  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
350  $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
351  $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
352  $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
353  $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
354  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
355  $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
356  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
357  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
358  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
359  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
360  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
361  $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
362  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
363  $sql .= " AND f.type IN (0,1,2,5)";
364  } else {
365  $sql .= " AND f.type IN (0,1,2,3,5)";
366  }
367  $sql .= " AND f.rowid = d.".$fk_facture;
368  $sql .= " AND s.rowid = f.fk_soc";
369  if ($y && $m) {
370  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
371  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
372  } elseif ($y) {
373  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
374  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
375  }
376  if ($q) {
377  $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
378  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
379  }
380  if ($date_start && $date_end) {
381  $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
382  }
383  $sql .= " AND (d.product_type = 1"; // Limit to services
384  $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
385  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
386  $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
387  }
388  $sql .= " ORDER BY d.rowid, d.".$fk_facture;
389  } else {
390  // Count on payments date
391  $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,";
392  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
393  $sql .= " d.date_start as date_start, d.date_end as date_end,";
394  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
395  $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
396  $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
397  $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
398  $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
399  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
400  $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
401  $sql .= " pa.datep as datep, pa.ref as payment_ref";
402  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
403  $sql .= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
404  $sql .= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
405  $sql .= " ".MAIN_DB_PREFIX."societe as s,";
406  $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
407  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
408  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
409  $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
410  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
411  $sql .= " AND f.type IN (0,1,2,5)";
412  } else {
413  $sql .= " AND f.type IN (0,1,2,3,5)";
414  }
415  $sql .= " AND f.rowid = d.".$fk_facture;
416  $sql .= " AND s.rowid = f.fk_soc";
417  $sql .= " AND pf.".$fk_facture2." = f.rowid";
418  $sql .= " AND pa.rowid = pf.".$fk_payment;
419  if ($y && $m) {
420  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
421  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
422  } elseif ($y) {
423  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
424  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
425  }
426  if ($q) {
427  $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
428  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
429  }
430  if ($date_start && $date_end) {
431  $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
432  }
433  $sql .= " AND (d.product_type = 1"; // Limit to services
434  $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
435  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
436  $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
437  }
438  $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
439  }
440 
441  if (!$sql) {
442  dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
443  return -1; // -1 = Not accountancy module enabled
444  }
445  if ($sql == 'TODO') {
446  return -2; // -2 = Feature not yet available
447  }
448  if ($sql != 'TODO') {
449  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
450  $resql = $db->query($sql);
451  if ($resql) {
452  $company_id = -1;
453  $oldrowid = '';
454  while ($assoc = $db->fetch_array($resql)) {
455  if (!isset($list[$assoc['company_id']]['totalht'])) {
456  $list[$assoc['company_id']]['totalht'] = 0;
457  }
458  if (!isset($list[$assoc['company_id']]['vat'])) {
459  $list[$assoc['company_id']]['vat'] = 0;
460  }
461  if (!isset($list[$assoc['company_id']]['localtax1'])) {
462  $list[$assoc['company_id']]['localtax1'] = 0;
463  }
464  if (!isset($list[$assoc['company_id']]['localtax2'])) {
465  $list[$assoc['company_id']]['localtax2'] = 0;
466  }
467 
468  if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
469  $oldrowid = $assoc['rowid'];
470  $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
471  $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
472  $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
473  $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
474  }
475  $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
476  $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
477  $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
478  $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
479 
480  $list[$assoc['company_id']]['company_name'][] = $assoc['company_name'];
481  $list[$assoc['company_id']]['company_id'][] = $assoc['company_id'];
482  $list[$assoc['company_id']]['company_alias'][] = $assoc['company_alias'];
483  $list[$assoc['company_id']]['company_email'][] = $assoc['company_email'];
484  $list[$assoc['company_id']]['company_tva_intra'][] = $assoc['company_tva_intra'];
485  $list[$assoc['company_id']]['company_client'][] = $assoc['company_client'];
486  $list[$assoc['company_id']]['company_fournisseur'][] = $assoc['company_fournisseur'];
487  $list[$assoc['company_id']]['company_customer_code'][] = $assoc['company_customer_code'];
488  $list[$assoc['company_id']]['company_supplier_code'][] = $assoc['company_supplier_code'];
489  $list[$assoc['company_id']]['company_customer_accounting_code'][] = $assoc['company_customer_accounting_code'];
490  $list[$assoc['company_id']]['company_supplier_accounting_code'][] = $assoc['company_supplier_accounting_code'];
491  $list[$assoc['company_id']]['company_status'][] = $assoc['company_status'];
492 
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'][] = $assoc['ptype'];
511 
512  $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
513  $list[$assoc['company_id']]['payment_ref'][] = $assoc['payment_ref'];
514  $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
515 
516  $company_id = $assoc['company_id'];
517  }
518  } else {
519  dol_print_error($db);
520  return -3;
521  }
522  }
523 
524 
525  // CASE OF EXPENSE REPORT
526 
527  if ($direction == 'buy') { // buy only for expense reports
528  // Define sql request
529  $sql = '';
530 
531  // Count on payments date
532  $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,";
533  $sql .= " d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
534  $sql .= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
535  $sql .= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
536  $sql .= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
537  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as e";
538  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid ";
539  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid ";
540  $sql .= " WHERE e.entity = ".$conf->entity;
541  $sql .= " AND e.fk_statut in (6)";
542  if ($y && $m) {
543  $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
544  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
545  } elseif ($y) {
546  $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
547  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
548  }
549  if ($q) {
550  $sql .= " AND p.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
551  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
552  }
553  if ($date_start && $date_end) {
554  $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
555  }
556  $sql .= " AND (d.product_type = -1";
557  $sql .= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
558  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
559  $sql .= " AND (d.".$f_rate." <> 0 OR d.total_tva <> 0)";
560  }
561  $sql .= " ORDER BY e.rowid";
562 
563  if (!$sql) {
564  dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
565  return -1; // -1 = Not accountancy module enabled
566  }
567  if ($sql == 'TODO') {
568  return -2; // -2 = Feature not yet available
569  }
570  if ($sql != 'TODO') {
571  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
572  $resql = $db->query($sql);
573  if ($resql) {
574  $company_id = -1;
575  $oldrowid = '';
576  while ($assoc = $db->fetch_array($resql)) {
577  if (!isset($list[$assoc['company_id']]['totalht'])) {
578  $list[$assoc['company_id']]['totalht'] = 0;
579  }
580  if (!isset($list[$assoc['company_id']]['vat'])) {
581  $list[$assoc['company_id']]['vat'] = 0;
582  }
583  if (!isset($list[$assoc['company_id']]['localtax1'])) {
584  $list[$assoc['company_id']]['localtax1'] = 0;
585  }
586  if (!isset($list[$assoc['company_id']]['localtax2'])) {
587  $list[$assoc['company_id']]['localtax2'] = 0;
588  }
589 
590  if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
591  $oldrowid = $assoc['rowid'];
592  $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
593  $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
594  $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
595  $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
596  }
597 
598  $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
599  $list[$assoc['company_id']]['dtype'][] = 'ExpenseReportPayment';
600  $list[$assoc['company_id']]['datef'][] = $assoc['datef'];
601 
602  $list[$assoc['company_id']]['company_name'][] = '';
603  $list[$assoc['company_id']]['company_id'][] = '';
604  $list[$assoc['company_id']]['company_alias'][] = '';
605  $list[$assoc['company_id']]['company_email'][] = '';
606  $list[$assoc['company_id']]['company_tva_intra'][] = '';
607  $list[$assoc['company_id']]['company_client'][] = '';
608  $list[$assoc['company_id']]['company_fournisseur'][] = '';
609  $list[$assoc['company_id']]['company_customer_code'][] = '';
610  $list[$assoc['company_id']]['company_supplier_code'][] = '';
611  $list[$assoc['company_id']]['company_customer_accounting_code'][] = '';
612  $list[$assoc['company_id']]['company_supplier_accounting_code'][] = '';
613  $list[$assoc['company_id']]['company_status'][] = '';
614 
615  $list[$assoc['company_id']]['user_id'][] = $assoc['fk_user_author'];
616  $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
617  $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
618  $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
619 
620  $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
621  $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
622  $list[$assoc['company_id']]['type'][] = $assoc['type'];
623  $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
624  $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
625 
626  $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
627  $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
628  $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
629  $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
630 
631  $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
632  $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
633  $list[$assoc['company_id']]['ptype'][] = 'ExpenseReportPayment';
634 
635  $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
636  $list[$assoc['company_id']]['payment_ref'][] = $assoc['payment_ref'];
637  $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
638 
639  $company_id = $assoc['company_id'];
640  }
641  } else {
642  dol_print_error($db);
643  return -3;
644  }
645  }
646  }
647 
648  return $list;
649 }
650 
667 function tax_by_rate($type, $db, $y, $q, $date_start, $date_end, $modetax, $direction, $m = 0)
668 {
669  global $conf;
670 
671  // If we use date_start and date_end, we must not use $y, $m, $q
672  if (($date_start || $date_end) && (!empty($y) || !empty($m) || !empty($q))) {
673  dol_print_error('', 'Bad value of input parameter for tax_by_rate');
674  }
675 
676  $list = array();
677 
678  if ($direction == 'sell') {
679  $invoicetable = 'facture';
680  $invoicedettable = 'facturedet';
681  $fk_facture = 'fk_facture';
682  $fk_facture2 = 'fk_facture';
683  $fk_payment = 'fk_paiement';
684  $total_tva = 'total_tva';
685  $paymenttable = 'paiement';
686  $paymentfacturetable = 'paiement_facture';
687  $invoicefieldref = 'ref';
688  } else {
689  $invoicetable = 'facture_fourn';
690  $invoicedettable = 'facture_fourn_det';
691  $fk_facture = 'fk_facture_fourn';
692  $fk_facture2 = 'fk_facturefourn';
693  $fk_payment = 'fk_paiementfourn';
694  $total_tva = 'tva';
695  $paymenttable = 'paiementfourn';
696  $paymentfacturetable = 'paiementfourn_facturefourn';
697  $invoicefieldref = 'ref';
698  }
699 
700  if (strpos($type, 'localtax') === 0) {
701  $f_rate = $type.'_tx';
702  } else {
703  $f_rate = 'tva_tx';
704  }
705 
706  $total_localtax1 = 'total_localtax1';
707  $total_localtax2 = 'total_localtax2';
708 
709 
710  // CASE OF PRODUCTS/GOODS
711 
712  // Define sql request
713  $sql = '';
714  if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
715  || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice')) {
716  // Count on delivery date (use invoice date as delivery is unknown)
717  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
718  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
719  $sql .= " d.date_start as date_start, d.date_end as date_end,";
720  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
721  $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
722  $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
723  $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
724  $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
725  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
726  $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
727  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f";
728  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
729  $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$invoicedettable." as d ON d.".$fk_facture."=f.rowid";
730  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
731  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
732  $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
733  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
734  $sql .= " AND f.type IN (0,1,2,5)";
735  } else {
736  $sql .= " AND f.type IN (0,1,2,3,5)";
737  }
738  if ($y && $m) {
739  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
740  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
741  } elseif ($y) {
742  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
743  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
744  }
745  if ($q) {
746  $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
747  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
748  }
749  if ($date_start && $date_end) {
750  $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
751  }
752  $sql .= " AND (d.product_type = 0"; // Limit to products
753  $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
754  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
755  $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
756  }
757  $sql .= " ORDER BY d.rowid, d.".$fk_facture;
758  } else {
759  // Count on payments date
760  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
761  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
762  $sql .= " d.date_start as date_start, d.date_end as date_end,";
763  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
764  $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
765  $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
766  $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
767  $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
768  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
769  $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
770  $sql .= " pa.datep as datep, pa.ref as payment_ref";
771  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f";
772  $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$paymentfacturetable." as pf ON pf.".$fk_facture2." = f.rowid";
773  $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$paymenttable." as pa ON pa.rowid = pf.".$fk_payment;
774  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
775  $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$invoicedettable." as d ON d.".$fk_facture." = f.rowid";
776  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
777  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
778  $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
779  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
780  $sql .= " AND f.type IN (0,1,2,5)";
781  } else {
782  $sql .= " AND f.type IN (0,1,2,3,5)";
783  }
784  if ($y && $m) {
785  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
786  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
787  } elseif ($y) {
788  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
789  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
790  }
791  if ($q) {
792  $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
793  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
794  }
795  if ($date_start && $date_end) {
796  $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
797  }
798  $sql .= " AND (d.product_type = 0"; // Limit to products
799  $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
800  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
801  $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
802  }
803  $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
804  }
805 
806  if (!$sql) {
807  return -1;
808  }
809  if ($sql == 'TODO') {
810  return -2;
811  }
812  if ($sql != 'TODO') {
813  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
814 
815  $resql = $db->query($sql);
816  if ($resql) {
817  $rate = -1;
818  $oldrowid = '';
819  while ($assoc = $db->fetch_array($resql)) {
820  $rate_key = $assoc['rate'];
821  if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\(/', $rate_key)) {
822  $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
823  }
824 
825  // Code to avoid warnings when array entry not defined
826  if (!isset($list[$rate_key]['totalht'])) {
827  $list[$rate_key]['totalht'] = 0;
828  }
829  if (!isset($list[$rate_key]['vat'])) {
830  $list[$rate_key]['vat'] = 0;
831  }
832  if (!isset($list[$rate_key]['localtax1'])) {
833  $list[$rate_key]['localtax1'] = 0;
834  }
835  if (!isset($list[$rate_key]['localtax2'])) {
836  $list[$rate_key]['localtax2'] = 0;
837  }
838 
839  if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
840  $oldrowid = $assoc['rowid'];
841  $list[$rate_key]['totalht'] += $assoc['total_ht'];
842  $list[$rate_key]['vat'] += $assoc['total_vat'];
843  $list[$rate_key]['localtax1'] += $assoc['total_localtax1'];
844  $list[$rate_key]['localtax2'] += $assoc['total_localtax2'];
845  }
846  $list[$rate_key]['dtotal_ttc'][] = $assoc['total_ttc'];
847  $list[$rate_key]['dtype'][] = $assoc['dtype'];
848  $list[$rate_key]['datef'][] = $db->jdate($assoc['datef']);
849  $list[$rate_key]['datep'][] = $db->jdate($assoc['datep']);
850 
851  $list[$rate_key]['company_name'][] = $assoc['company_name'];
852  $list[$rate_key]['company_id'][] = $assoc['company_id'];
853  $list[$rate_key]['company_alias'][] = $assoc['company_alias'];
854  $list[$rate_key]['company_email'][] = $assoc['company_email'];
855  $list[$rate_key]['company_tva_intra'][] = $assoc['company_tva_intra'];
856  $list[$rate_key]['company_client'][] = $assoc['company_client'];
857  $list[$rate_key]['company_fournisseur'][] = $assoc['company_fournisseur'];
858  $list[$rate_key]['company_customer_code'][] = $assoc['company_customer_code'];
859  $list[$rate_key]['company_supplier_code'][] = $assoc['company_supplier_code'];
860  $list[$rate_key]['company_customer_accounting_code'][] = $assoc['company_customer_accounting_code'];
861  $list[$rate_key]['company_supplier_accounting_code'][] = $assoc['company_supplier_accounting_code'];
862  $list[$rate_key]['company_status'][] = $assoc['company_status'];
863 
864  $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
865  $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
866 
867  $list[$rate_key]['facid'][] = $assoc['facid'];
868  $list[$rate_key]['facnum'][] = $assoc['facnum'];
869  $list[$rate_key]['type'][] = $assoc['type'];
870  $list[$rate_key]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
871  $list[$rate_key]['descr'][] = $assoc['descr'];
872 
873  $list[$rate_key]['totalht_list'][] = $assoc['total_ht'];
874  $list[$rate_key]['vat_list'][] = $assoc['total_vat'];
875  $list[$rate_key]['localtax1_list'][] = $assoc['total_localtax1'];
876  $list[$rate_key]['localtax2_list'][] = $assoc['total_localtax2'];
877 
878  $list[$rate_key]['pid'][] = $assoc['pid'];
879  $list[$rate_key]['pref'][] = $assoc['pref'];
880  $list[$rate_key]['ptype'][] = $assoc['ptype'];
881 
882  $list[$rate_key]['payment_id'][] = $assoc['payment_id'];
883  $list[$rate_key]['payment_ref'][] = $assoc['payment_ref'];
884  $list[$rate_key]['payment_amount'][] = $assoc['payment_amount'];
885 
886  $rate = $assoc['rate'];
887  }
888  } else {
889  dol_print_error($db);
890  return -3;
891  }
892  }
893 
894 
895  // CASE OF SERVICES
896 
897  // Define sql request
898  $sql = '';
899  if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')
900  || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice')) {
901  // Count on invoice date
902  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
903  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
904  $sql .= " d.date_start as date_start, d.date_end as date_end,";
905  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
906  $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
907  $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
908  $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
909  $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
910  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
911  $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
912  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f";
913  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
914  $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$invoicedettable." as d ON d.".$fk_facture." = f.rowid";
915  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
916  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
917  $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
918  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
919  $sql .= " AND f.type IN (0,1,2,5)";
920  } else {
921  $sql .= " AND f.type IN (0,1,2,3,5)";
922  }
923  if ($y && $m) {
924  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
925  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
926  } elseif ($y) {
927  $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
928  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
929  }
930  if ($q) {
931  $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
932  $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
933  }
934  if ($date_start && $date_end) {
935  $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
936  }
937  $sql .= " AND (d.product_type = 1"; // Limit to services
938  $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
939  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
940  $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
941  }
942  $sql .= " ORDER BY d.rowid, d.".$fk_facture;
943  } else {
944  // Count on payments date
945  $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
946  $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
947  $sql .= " d.date_start as date_start, d.date_end as date_end,";
948  $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
949  $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
950  $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
951  $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
952  $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
953  $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
954  $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
955  $sql .= " pa.datep as datep, pa.ref as payment_ref";
956  $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f";
957  $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$paymentfacturetable." as pf ON pf.".$fk_facture2." = f.rowid";
958  $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$paymenttable." as pa ON pa.rowid = pf.".$fk_payment;
959  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
960  $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$invoicedettable." as d ON d.".$fk_facture." = f.rowid";
961  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
962  $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
963  $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
964  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
965  $sql .= " AND f.type IN (0,1,2,5)";
966  } else {
967  $sql .= " AND f.type IN (0,1,2,3,5)";
968  }
969  if ($y && $m) {
970  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
971  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
972  } elseif ($y) {
973  $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
974  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
975  }
976  if ($q) {
977  $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
978  $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
979  }
980  if ($date_start && $date_end) {
981  $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
982  }
983  $sql .= " AND (d.product_type = 1"; // Limit to services
984  $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
985  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
986  $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
987  }
988  $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
989  }
990 
991  if (!$sql) {
992  dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
993  return -1; // -1 = Not accountancy module enabled
994  }
995  if ($sql == 'TODO') {
996  return -2; // -2 = Feature not yet available
997  }
998  if ($sql != 'TODO') {
999  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
1000  $resql = $db->query($sql);
1001  if ($resql) {
1002  $rate = -1;
1003  $oldrowid = '';
1004  while ($assoc = $db->fetch_array($resql)) {
1005  $rate_key = $assoc['rate'];
1006  if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\(/', $rate_key)) {
1007  $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
1008  }
1009 
1010  // Code to avoid warnings when array entry not defined
1011  if (!isset($list[$rate_key]['totalht'])) {
1012  $list[$rate_key]['totalht'] = 0;
1013  }
1014  if (!isset($list[$rate_key]['vat'])) {
1015  $list[$rate_key]['vat'] = 0;
1016  }
1017  if (!isset($list[$rate_key]['localtax1'])) {
1018  $list[$rate_key]['localtax1'] = 0;
1019  }
1020  if (!isset($list[$rate_key]['localtax2'])) {
1021  $list[$rate_key]['localtax2'] = 0;
1022  }
1023 
1024  if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
1025  $oldrowid = $assoc['rowid'];
1026  $list[$rate_key]['totalht'] += $assoc['total_ht'];
1027  $list[$rate_key]['vat'] += $assoc['total_vat'];
1028  $list[$rate_key]['localtax1'] += $assoc['total_localtax1'];
1029  $list[$rate_key]['localtax2'] += $assoc['total_localtax2'];
1030  }
1031  $list[$rate_key]['dtotal_ttc'][] = $assoc['total_ttc'];
1032  $list[$rate_key]['dtype'][] = $assoc['dtype'];
1033  $list[$rate_key]['datef'][] = $db->jdate($assoc['datef']);
1034  $list[$rate_key]['datep'][] = $db->jdate($assoc['datep']);
1035 
1036  $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
1037  $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
1038 
1039  $list[$rate_key]['company_name'][] = $assoc['company_name'];
1040  $list[$rate_key]['company_id'][] = $assoc['company_id'];
1041  $list[$rate_key]['company_alias'][] = $assoc['company_alias'];
1042  $list[$rate_key]['company_email'][] = $assoc['company_email'];
1043  $list[$rate_key]['company_tva_intra'][] = $assoc['company_tva_intra'];
1044  $list[$rate_key]['company_client'][] = $assoc['company_client'];
1045  $list[$rate_key]['company_fournisseur'][] = $assoc['company_fournisseur'];
1046  $list[$rate_key]['company_customer_code'][] = $assoc['company_customer_code'];
1047  $list[$rate_key]['company_supplier_code'][] = $assoc['company_supplier_code'];
1048  $list[$rate_key]['company_customer_accounting_code'][] = $assoc['company_customer_accounting_code'];
1049  $list[$rate_key]['company_supplier_accounting_code'][] = $assoc['company_supplier_accounting_code'];
1050  $list[$rate_key]['company_status'][] = $assoc['company_status'];
1051 
1052  $list[$rate_key]['facid'][] = $assoc['facid'];
1053  $list[$rate_key]['facnum'][] = $assoc['facnum'];
1054  $list[$rate_key]['type'][] = $assoc['type'];
1055  $list[$rate_key]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
1056  $list[$rate_key]['descr'][] = $assoc['descr'];
1057 
1058  $list[$rate_key]['totalht_list'][] = $assoc['total_ht'];
1059  $list[$rate_key]['vat_list'][] = $assoc['total_vat'];
1060  $list[$rate_key]['localtax1_list'][] = $assoc['total_localtax1'];
1061  $list[$rate_key]['localtax2_list'][] = $assoc['total_localtax2'];
1062 
1063  $list[$rate_key]['pid'][] = $assoc['pid'];
1064  $list[$rate_key]['pref'][] = $assoc['pref'];
1065  $list[$rate_key]['ptype'][] = $assoc['ptype'];
1066 
1067  $list[$rate_key]['payment_id'][] = $assoc['payment_id'];
1068  $list[$rate_key]['payment_ref'][] = $assoc['payment_ref'];
1069  $list[$rate_key]['payment_amount'][] = $assoc['payment_amount'];
1070 
1071  $rate = $assoc['rate'];
1072  }
1073  } else {
1074  dol_print_error($db);
1075  return -3;
1076  }
1077  }
1078 
1079 
1080  // CASE OF EXPENSE REPORT
1081 
1082  if ($direction == 'buy') { // buy only for expense reports
1083  // Define sql request
1084  $sql = '';
1085 
1086  // Count on payments date
1087  $sql = "SELECT d.rowid, d.product_type as dtype, e.rowid as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.total_tva as total_vat, e.note_private as descr,";
1088  $sql .= " d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
1089  $sql .= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
1090  $sql .= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
1091  $sql .= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
1092  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as e";
1093  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid";
1094  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid";
1095  $sql .= " WHERE e.entity = ".$conf->entity;
1096  $sql .= " AND e.fk_statut in (6)";
1097  if ($y && $m) {
1098  $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
1099  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
1100  } elseif ($y) {
1101  $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
1102  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
1103  }
1104  if ($q) {
1105  $sql .= " AND p.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
1106  $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
1107  }
1108  if ($date_start && $date_end) {
1109  $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
1110  }
1111  $sql .= " AND (d.product_type = -1";
1112  $sql .= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
1113  if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
1114  $sql .= " AND (d.".$f_rate." <> 0 OR d.total_tva <> 0)";
1115  }
1116  $sql .= " ORDER BY e.rowid";
1117 
1118  if (!$sql) {
1119  dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
1120  return -1; // -1 = Not accountancy module enabled
1121  }
1122  if ($sql == 'TODO') {
1123  return -2; // -2 = Feature not yet available
1124  }
1125  if ($sql != 'TODO') {
1126  dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
1127  $resql = $db->query($sql);
1128  if ($resql) {
1129  $rate = -1;
1130  $oldrowid = '';
1131  while ($assoc = $db->fetch_array($resql)) {
1132  $rate_key = $assoc['rate'];
1133  if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\(/', $rate_key)) {
1134  $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
1135  }
1136 
1137  // Code to avoid warnings when array entry not defined
1138  if (!isset($list[$rate_key]['totalht'])) {
1139  $list[$rate_key]['totalht'] = 0;
1140  }
1141  if (!isset($list[$rate_key]['vat'])) {
1142  $list[$rate_key]['vat'] = 0;
1143  }
1144  if (!isset($list[$rate_key]['localtax1'])) {
1145  $list[$rate_key]['localtax1'] = 0;
1146  }
1147  if (!isset($list[$rate_key]['localtax2'])) {
1148  $list[$rate_key]['localtax2'] = 0;
1149  }
1150 
1151  if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
1152  $oldrowid = $assoc['rowid'];
1153  $list[$rate_key]['totalht'] += $assoc['total_ht'];
1154  $list[$rate_key]['vat'] += $assoc['total_vat'];
1155  $list[$rate_key]['localtax1'] += $assoc['total_localtax1'];
1156  $list[$rate_key]['localtax2'] += $assoc['total_localtax2'];
1157  }
1158 
1159  $list[$rate_key]['dtotal_ttc'][] = $assoc['total_ttc'];
1160  $list[$rate_key]['dtype'][] = 'ExpenseReportPayment';
1161  $list[$rate_key]['datef'][] = $assoc['datef'];
1162  $list[$rate_key]['company_name'][] = '';
1163  $list[$rate_key]['company_id'][] = '';
1164  $list[$rate_key]['user_id'][] = $assoc['fk_user_author'];
1165  $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
1166  $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
1167 
1168  $list[$rate_key]['facid'][] = $assoc['facid'];
1169  $list[$rate_key]['facnum'][] = $assoc['facnum'];
1170  $list[$rate_key]['type'][] = $assoc['type'];
1171  $list[$rate_key]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
1172  $list[$rate_key]['descr'][] = $assoc['descr'];
1173 
1174  $list[$rate_key]['totalht_list'][] = $assoc['total_ht'];
1175  $list[$rate_key]['vat_list'][] = $assoc['total_vat'];
1176  $list[$rate_key]['localtax1_list'][] = $assoc['total_localtax1'];
1177  $list[$rate_key]['localtax2_list'][] = $assoc['total_localtax2'];
1178 
1179  $list[$rate_key]['pid'][] = $assoc['pid'];
1180  $list[$rate_key]['pref'][] = $assoc['pref'];
1181  $list[$rate_key]['ptype'][] = 'ExpenseReportPayment';
1182 
1183  $list[$rate_key]['payment_id'][] = $assoc['payment_id'];
1184  $list[$rate_key]['payment_ref'][] = $assoc['payment_ref'];
1185  $list[$rate_key]['payment_amount'][] = $assoc['payment_amount'];
1186 
1187  $rate = $assoc['rate'];
1188  }
1189  } else {
1190  dol_print_error($db);
1191  return -3;
1192  }
1193  }
1194  }
1195 
1196  return $list;
1197 }
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:112
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:551
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:667
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
if(isModEnabled('facture')&&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur')&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)&&$user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice')&&$user->rights->supplier_invoice->lire)) if(isModEnabled('don')&&!empty($user->rights->don->lire)) if(isModEnabled('tax')&&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture')&&isModEnabled('commande')&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
tax_prepare_head(ChargeSociales $object)
Prepare array with list of tabs.
Definition: tax.lib.php:38
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
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...