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