dolibarr 24.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 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
11 * Copyright (C) 2024-2026 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.".$db->sanitize($fk_facture)." as facid, d.".$db->sanitize($f_rate)." as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$db->sanitize($total_tva)." as total_vat, d.description as descr,";
167 $sql .= " d.".$db->sanitize($total_localtax1)." as total_localtax1, d.".$db->sanitize($total_localtax2)." as total_localtax2, ";
168 $sql .= " d.date_start as date_start, d.date_end as date_end,";
169 $sql .= " f.".$db->sanitize($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.$db->sanitize($invoicetable)." as f,";
178 $sql .= " ".MAIN_DB_PREFIX."societe as s,";
179 $sql .= " ".MAIN_DB_PREFIX.$db->sanitize($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.".$db->sanitize($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.".$db->sanitize($f_rate)." <> 0 OR d.".$db->sanitize($total_tva)." <> 0)";
216 }
217 $sql .= " ORDER BY d.rowid, d.".$db->sanitize($fk_facture);
218 } else {
219 // Count on payments date
220 $sql = "SELECT d.rowid, d.product_type as dtype, d.".$db->sanitize($fk_facture)." as facid, d.".$db->sanitize($f_rate)." as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$db->sanitize($total_tva)." as total_vat, d.description as descr,";
221 $sql .= " d.".$db->sanitize($total_localtax1)." as total_localtax1, d.".$db->sanitize($total_localtax2)." as total_localtax2, ";
222 $sql .= " d.date_start as date_start, d.date_end as date_end,";
223 $sql .= " f.".$db->sanitize($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.".$db->sanitize($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.$db->sanitize($invoicetable)." as f,";
232 $sql .= " ".MAIN_DB_PREFIX.$db->sanitize($paymentfacturetable)." as pf,";
233 $sql .= " ".MAIN_DB_PREFIX.$db->sanitize($paymenttable)." as pa,";
234 $sql .= " ".MAIN_DB_PREFIX."societe as s,";
235 $sql .= " ".MAIN_DB_PREFIX.$db->sanitize($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.".$db->sanitize($fk_facture);
253 $sql .= " AND s.rowid = f.fk_soc";
254 $sql .= " AND pf.".$db->sanitize($fk_facture2)." = f.rowid";
255 $sql .= " AND pa.rowid = pf.".$db->sanitize($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.".$db->sanitize($f_rate)." <> 0 OR d.".$db->sanitize($total_tva)." <> 0)";
274 }
275 $sql .= " ORDER BY d.rowid, d.".$db->sanitize($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 $list[$assoc['company_id']]['pstatus'][] = (int) $assoc['pstatus'];
343 $list[$assoc['company_id']]['pstatusbuy'][] = (int) $assoc['pstatusbuy'];
344
345 $list[$assoc['company_id']]['payment_id'][] = (int) $assoc['payment_id'];
346 $list[$assoc['company_id']]['payment_ref'][] = (string) $assoc['payment_ref'];
347 $list[$assoc['company_id']]['payment_amount'][] = (float) $assoc['payment_amount'];
348
349 $company_id = $assoc['company_id'];
350 }
351 } else {
353 return -3;
354 }
355
356
357 // CAS DES SERVICES
358
359 // Define sql request
360 $sql = '';
361 if (($direction == 'sell' && getDolGlobalString('TAX_MODE_SELL_SERVICE') == 'invoice')
362 || ($direction == 'buy' && getDolGlobalString('TAX_MODE_BUY_SERVICE') == 'invoice')) {
363 // Count on invoice date
364 $sql = "SELECT d.rowid, d.product_type as dtype, d.".$db->sanitize($fk_facture)." as facid, d.".$db->sanitize($f_rate)." as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$db->sanitize($total_tva)." as total_vat, d.description as descr,";
365 $sql .= " d.".$db->sanitize($total_localtax1)." as total_localtax1, d.".$db->sanitize($total_localtax2)." as total_localtax2, ";
366 $sql .= " d.date_start as date_start, d.date_end as date_end,";
367 $sql .= " f.".$db->sanitize($invoicefieldref)." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
368 $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,";
369 $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
370 $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
371 $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
372 $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
373 $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
374 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($invoicetable)." as f,";
375 $sql .= " ".MAIN_DB_PREFIX."societe as s,";
376 $sql .= " ".MAIN_DB_PREFIX.$db->sanitize($invoicedettable)." as d";
377 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
378 $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
379 $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
380 if ($direction == 'buy') {
381 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
382 $sql .= " AND f.type IN (0,1,2,5)";
383 } else {
384 $sql .= " AND f.type IN (0,1,2,3,5)";
385 }
386 } else {
387 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
388 $sql .= " AND f.type IN (0,1,2,5)";
389 } else {
390 $sql .= " AND f.type IN (0,1,2,3,5)";
391 }
392 }
393 $sql .= " AND f.rowid = d.".$db->sanitize($fk_facture);
394 $sql .= " AND s.rowid = f.fk_soc";
395 if ($y && $m) {
396 $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
397 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
398 } elseif ($y) {
399 $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
400 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
401 }
402 if ($q) {
403 $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
404 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
405 }
406 if ($date_start && $date_end) {
407 $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
408 }
409 $sql .= " AND (d.product_type = 1"; // Limit to services
410 $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
411 if (getDolGlobalString('MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS')) {
412 $sql .= " AND (d.".$db->sanitize($f_rate)." <> 0 OR d.".$db->sanitize($total_tva)." <> 0)";
413 }
414 $sql .= " ORDER BY d.rowid, d.".$db->sanitize($fk_facture);
415 } else {
416 // Count on payments date
417 $sql = "SELECT d.rowid, d.product_type as dtype, d.".$db->sanitize($fk_facture)." as facid, d.".$db->sanitize($f_rate)." as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$db->sanitize($total_tva)." as total_vat, d.description as descr,";
418 $sql .= " d.".$db->sanitize($total_localtax1)." as total_localtax1, d.".$db->sanitize($total_localtax2)." as total_localtax2, ";
419 $sql .= " d.date_start as date_start, d.date_end as date_end,";
420 $sql .= " f.".$db->sanitize($invoicefieldref)." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
421 $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,";
422 $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
423 $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
424 $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
425 $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
426 $sql .= " pf.".$db->sanitize($fk_payment)." as payment_id, pf.amount as payment_amount,";
427 $sql .= " pa.datep as datep, pa.ref as payment_ref";
428 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($invoicetable)." as f,";
429 $sql .= " ".MAIN_DB_PREFIX.$db->sanitize($paymentfacturetable)." as pf,";
430 $sql .= " ".MAIN_DB_PREFIX.$db->sanitize($paymenttable)." as pa,";
431 $sql .= " ".MAIN_DB_PREFIX."societe as s,";
432 $sql .= " ".MAIN_DB_PREFIX.$db->sanitize($invoicedettable)." as d";
433 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
434 $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
435 $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
436 $sql .= " AND f.type IN (0,1,2,3,5)";
437 $sql .= " AND f.rowid = d.".$db->sanitize($fk_facture);
438 $sql .= " AND s.rowid = f.fk_soc";
439 $sql .= " AND pf.".$db->sanitize($fk_facture2)." = f.rowid";
440 $sql .= " AND pa.rowid = pf.".$db->sanitize($fk_payment);
441 if ($y && $m) {
442 $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
443 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
444 } elseif ($y) {
445 $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
446 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
447 }
448 if ($q) {
449 $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
450 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
451 }
452 if ($date_start && $date_end) {
453 $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
454 }
455 $sql .= " AND (d.product_type = 1"; // Limit to services
456 $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
457 if (getDolGlobalString('MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS')) {
458 $sql .= " AND (d.".$db->sanitize($f_rate)." <> 0 OR d.".$db->sanitize($total_tva)." <> 0)";
459 }
460 $sql .= " ORDER BY d.rowid, d.".$db->sanitize($fk_facture).", pf.rowid";
461 }
462
463 dol_syslog("Tax.lib.php::tax_by_thirdparty", LOG_DEBUG);
464 $resql = $db->query($sql);
465 if ($resql) {
466 $company_id = -1;
467 $oldrowid = '';
468 while ($assoc = $db->fetch_array($resql)) {
469 if (!isset($list[$assoc['company_id']]['totalht'])) {
470 $list[$assoc['company_id']]['totalht'] = 0;
471 }
472 if (!isset($list[$assoc['company_id']]['vat'])) {
473 $list[$assoc['company_id']]['vat'] = 0;
474 }
475 if (!isset($list[$assoc['company_id']]['localtax1'])) {
476 $list[$assoc['company_id']]['localtax1'] = 0;
477 }
478 if (!isset($list[$assoc['company_id']]['localtax2'])) {
479 $list[$assoc['company_id']]['localtax2'] = 0;
480 }
481
482 if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
483 $oldrowid = $assoc['rowid'];
484 $list[$assoc['company_id']]['totalht'] += (float) $assoc['total_ht'];
485 $list[$assoc['company_id']]['vat'] += (float) $assoc['total_vat'];
486 $list[$assoc['company_id']]['localtax1'] += (float) $assoc['total_localtax1'];
487 $list[$assoc['company_id']]['localtax2'] += (float) $assoc['total_localtax2'];
488 }
489 $list[$assoc['company_id']]['dtotal_ttc'][] = (float) $assoc['total_ttc'];
490 $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
491 $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
492 $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
493
494 $list[$assoc['company_id']]['company_name'][] = (string) $assoc['company_name'];
495 $list[$assoc['company_id']]['company_id'][] = (int) $assoc['company_id'];
496 $list[$assoc['company_id']]['company_alias'][] = (string) $assoc['company_alias'];
497 $list[$assoc['company_id']]['company_email'][] = (string) $assoc['company_email'];
498 $list[$assoc['company_id']]['company_tva_intra'][] = (string) $assoc['company_tva_intra'];
499 $list[$assoc['company_id']]['company_client'][] = (int) $assoc['company_client'];
500 $list[$assoc['company_id']]['company_fournisseur'][] = (int) $assoc['company_fournisseur'];
501 $list[$assoc['company_id']]['company_customer_code'][] = (string) $assoc['company_customer_code'];
502 $list[$assoc['company_id']]['company_supplier_code'][] = (string) $assoc['company_supplier_code'];
503 $list[$assoc['company_id']]['company_customer_accounting_code'][] = (string) $assoc['company_customer_accounting_code'];
504 $list[$assoc['company_id']]['company_supplier_accounting_code'][] = (string) $assoc['company_supplier_accounting_code'];
505 $list[$assoc['company_id']]['company_status'][] = (int) $assoc['company_status'];
506
507 $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
508 $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
509 $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
510
511 $list[$assoc['company_id']]['facid'][] = (int) $assoc['facid'];
512 $list[$assoc['company_id']]['facnum'][] = (string) $assoc['facnum'];
513 $list[$assoc['company_id']]['type'][] = (int) $assoc['type'];
514 $list[$assoc['company_id']]['ftotal_ttc'][] = (float) $assoc['ftotal_ttc'];
515 $list[$assoc['company_id']]['descr'][] = (string) $assoc['descr'];
516
517 $list[$assoc['company_id']]['totalht_list'][] = (float) $assoc['total_ht'];
518 $list[$assoc['company_id']]['vat_list'][] = (float) $assoc['total_vat'];
519 $list[$assoc['company_id']]['localtax1_list'][] = (float) $assoc['total_localtax1'];
520 $list[$assoc['company_id']]['localtax2_list'][] = (float) $assoc['total_localtax2'];
521
522 $list[$assoc['company_id']]['pid'][] = (int) $assoc['pid'];
523 $list[$assoc['company_id']]['pref'][] = (string) $assoc['pref'];
524 $list[$assoc['company_id']]['ptype'][] = (int) $assoc['ptype'];
525 $list[$assoc['company_id']]['pstatus'][] = (int) $assoc['pstatus'];
526 $list[$assoc['company_id']]['pstatusbuy'][] = (int) $assoc['pstatusbuy'];
527
528 $list[$assoc['company_id']]['payment_id'][] = (int) $assoc['payment_id'];
529 $list[$assoc['company_id']]['payment_ref'][] = (string) $assoc['payment_ref'];
530 $list[$assoc['company_id']]['payment_amount'][] = (float) $assoc['payment_amount'];
531
532 $company_id = $assoc['company_id'];
533 }
534 } else {
536 return -3;
537 }
538
539
540 // CASE OF EXPENSE REPORT
541
542 if ($direction == 'buy') { // buy only for expense reports
543 // Define sql request
544 $sql = '';
545
546 // Count on payments date
547 $sql = "SELECT d.rowid, d.product_type as dtype, e.rowid as facid, d.".$db->sanitize($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,";
548 $sql .= " d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
549 $sql .= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
550 $sql .= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
551 $sql .= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
552 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as e";
553 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid ";
554 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid ";
555 $sql .= " WHERE e.entity = ".((int) $conf->entity);
556 $sql .= " AND e.fk_statut in (6)";
557 if ($y && $m) {
558 $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
559 $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
560 } elseif ($y) {
561 $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
562 $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
563 }
564 if ($q) {
565 $sql .= " AND p.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
566 $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
567 }
568 if ($date_start && $date_end) {
569 $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
570 }
571 $sql .= " AND (d.product_type = -1";
572 $sql .= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
573 if (getDolGlobalString('MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS')) {
574 $sql .= " AND (d.".$db->sanitize($f_rate)." <> 0 OR d.total_tva <> 0)";
575 }
576 $sql .= " ORDER BY e.rowid";
577
578 dol_syslog("Tax.lib.php::tax_by_thirdparty", LOG_DEBUG);
579 $resql = $db->query($sql);
580 if ($resql) {
581 $company_id = -1;
582 $oldrowid = '';
583 while ($assoc = $db->fetch_array($resql)) {
584 if (!isset($list[$assoc['company_id']]['totalht'])) {
585 $list[$assoc['company_id']]['totalht'] = 0;
586 }
587 if (!isset($list[$assoc['company_id']]['vat'])) {
588 $list[$assoc['company_id']]['vat'] = 0;
589 }
590 if (!isset($list[$assoc['company_id']]['localtax1'])) {
591 $list[$assoc['company_id']]['localtax1'] = 0;
592 }
593 if (!isset($list[$assoc['company_id']]['localtax2'])) {
594 $list[$assoc['company_id']]['localtax2'] = 0;
595 }
596
597 if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
598 $oldrowid = $assoc['rowid'];
599 $list[$assoc['company_id']]['totalht'] += (float) $assoc['total_ht'];
600 $list[$assoc['company_id']]['vat'] += (float) $assoc['total_vat'];
601 $list[$assoc['company_id']]['localtax1'] += (float) $assoc['total_localtax1'];
602 $list[$assoc['company_id']]['localtax2'] += (float) $assoc['total_localtax2'];
603 }
604
605 $list[$assoc['company_id']]['dtotal_ttc'][] = (float) $assoc['total_ttc'];
606 $list[$assoc['company_id']]['dtype'][] = 'ExpenseReportPayment';
607 $list[$assoc['company_id']]['datef'][] = (int) $assoc['datef'];
608
609 $list[$assoc['company_id']]['company_name'][] = '';
610 $list[$assoc['company_id']]['company_id'][] = 0;
611 $list[$assoc['company_id']]['company_alias'][] = '';
612 $list[$assoc['company_id']]['company_email'][] = '';
613 $list[$assoc['company_id']]['company_tva_intra'][] = '';
614 $list[$assoc['company_id']]['company_client'][] = 0;
615 $list[$assoc['company_id']]['company_fournisseur'][] = 0;
616 $list[$assoc['company_id']]['company_customer_code'][] = '';
617 $list[$assoc['company_id']]['company_supplier_code'][] = '';
618 $list[$assoc['company_id']]['company_customer_accounting_code'][] = '';
619 $list[$assoc['company_id']]['company_supplier_accounting_code'][] = '';
620 $list[$assoc['company_id']]['company_status'][] = 0;
621
622 $list[$assoc['company_id']]['user_id'][] = (int) $assoc['fk_user_author'];
623 $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
624 $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
625 $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
626
627 $list[$assoc['company_id']]['facid'][] = (int) $assoc['facid'];
628 $list[$assoc['company_id']]['facnum'][] = (string) $assoc['facnum'];
629 $list[$assoc['company_id']]['type'][] = (int) $assoc['type'];
630 $list[$assoc['company_id']]['ftotal_ttc'][] = (float) $assoc['ftotal_ttc'];
631 $list[$assoc['company_id']]['descr'][] = (string) $assoc['descr'];
632
633 $list[$assoc['company_id']]['totalht_list'][] = (float) $assoc['total_ht'];
634 $list[$assoc['company_id']]['vat_list'][] = (float) $assoc['total_vat'];
635 $list[$assoc['company_id']]['localtax1_list'][] = (float) $assoc['total_localtax1'];
636 $list[$assoc['company_id']]['localtax2_list'][] = (float) $assoc['total_localtax2'];
637
638 $list[$assoc['company_id']]['pid'][] = (int) $assoc['pid'];
639 $list[$assoc['company_id']]['pref'][] = (string) $assoc['pref'];
640 $list[$assoc['company_id']]['ptype'][] = 'ExpenseReportPayment';
641
642 $list[$assoc['company_id']]['payment_id'][] = (int) $assoc['payment_id'];
643 $list[$assoc['company_id']]['payment_ref'][] = (string) $assoc['payment_ref'];
644 $list[$assoc['company_id']]['payment_amount'][] = (float) $assoc['payment_amount'];
645
646 $company_id = $assoc['company_id'];
647 }
648 } else {
650 return -3;
651 }
652 }
653
654 return $list;
655}
656
657
674function tax_by_rate($type, $db, $y, $q, $date_start, $date_end, $modetax, $direction, $m = 0)
675{
676 global $conf;
677
678 // If we use date_start and date_end, we must not use $y, $m, $q
679 if (($date_start || $date_end) && (!empty($y) || !empty($m) || !empty($q))) {
680 dol_print_error(null, 'Bad value of input parameter for tax_by_rate');
681 }
682
683 $list = array();
684
685 if ($direction == 'sell') {
686 $invoicetable = 'facture';
687 $invoicedettable = 'facturedet';
688 $fk_facture = 'fk_facture';
689 $fk_facture2 = 'fk_facture';
690 $fk_payment = 'fk_paiement';
691 $total_tva = 'total_tva';
692 $paymenttable = 'paiement';
693 $paymentfacturetable = 'paiement_facture';
694 $invoicefieldref = 'ref';
695 } else {
696 $invoicetable = 'facture_fourn';
697 $invoicedettable = 'facture_fourn_det';
698 $fk_facture = 'fk_facture_fourn';
699 $fk_facture2 = 'fk_facturefourn';
700 $fk_payment = 'fk_paiementfourn';
701 $total_tva = 'tva';
702 $paymenttable = 'paiementfourn';
703 $paymentfacturetable = 'paiementfourn_facturefourn';
704 $invoicefieldref = 'ref';
705 }
706
707 if (strpos($type, 'localtax') === 0) {
708 $f_rate = $type.'_tx';
709 } else {
710 $f_rate = 'tva_tx';
711 }
712
713 $total_localtax1 = 'total_localtax1';
714 $total_localtax2 = 'total_localtax2';
715
716
717 // CASE OF PRODUCTS/GOODS
718
719 // Define sql request
720 $sql = '';
721 if (($direction == 'sell' && getDolGlobalString('TAX_MODE_SELL_PRODUCT') == 'invoice')
722 || ($direction == 'buy' && getDolGlobalString('TAX_MODE_BUY_PRODUCT') == 'invoice')) {
723 // Count on delivery date (use invoice date as delivery is unknown)
724 $sql = "SELECT d.rowid, d.product_type as dtype, d.".$db->sanitize($fk_facture)." as facid, d.".$db->sanitize($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.".$db->sanitize($total_tva)." as total_vat, d.description as descr,";
725 $sql .= " d.".$db->sanitize($total_localtax1)." as total_localtax1, d.".$db->sanitize($total_localtax2)." as total_localtax2, ";
726 $sql .= " d.date_start as date_start, d.date_end as date_end,";
727 $sql .= " f.".$db->sanitize($invoicefieldref)." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
728 $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,";
729 $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
730 $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
731 $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
732 $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
733 $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount,";
734 $sql .= " '' as datep";
735 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($invoicetable)." as f";
736 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
737 $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$db->sanitize($invoicedettable)." as d ON d.".$db->sanitize($fk_facture)." = f.rowid";
738 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
739 $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
740 $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
741 if ($direction == 'buy') {
742 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
743 $sql .= " AND f.type IN (0,1,2,5)";
744 } else {
745 $sql .= " AND f.type IN (0,1,2,3,5)";
746 }
747 } else {
748 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
749 $sql .= " AND f.type IN (0,1,2,5)";
750 } else {
751 $sql .= " AND f.type IN (0,1,2,3,5)";
752 }
753 }
754 if ($y && $m) {
755 $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
756 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
757 } elseif ($y) {
758 $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
759 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
760 }
761 if ($q) {
762 $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
763 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
764 }
765 if ($date_start && $date_end) {
766 $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
767 }
768 $sql .= " AND (d.product_type = 0"; // Limit to products
769 $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
770 if (getDolGlobalString('MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS')) {
771 $sql .= " AND (d.".$db->sanitize($f_rate)." <> 0 OR d.".$db->sanitize($total_tva)." <> 0)";
772 }
773 $sql .= " ORDER BY d.rowid, d.".$db->sanitize($fk_facture);
774 } else {
775 // Count on payments date
776 $sql = "SELECT d.rowid, d.product_type as dtype, d.".$db->sanitize($fk_facture)." as facid, d.".$db->sanitize($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.".$db->sanitize($total_tva)." as total_vat, d.description as descr,";
777 $sql .= " d.".$db->sanitize($total_localtax1)." as total_localtax1, d.".$db->sanitize($total_localtax2)." as total_localtax2, ";
778 $sql .= " d.date_start as date_start, d.date_end as date_end,";
779 $sql .= " f.".$db->sanitize($invoicefieldref)." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
780 $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,";
781 $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
782 $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
783 $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
784 $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
785 $sql .= " pf.".$db->sanitize($fk_payment)." as payment_id, pf.amount as payment_amount,";
786 $sql .= " pa.datep as datep, pa.ref as payment_ref";
787 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($invoicetable)." as f";
788 $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$db->sanitize($paymentfacturetable)." as pf ON pf.".$db->sanitize($fk_facture2)." = f.rowid";
789 $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$db->sanitize($paymenttable)." as pa ON pa.rowid = pf.".$db->sanitize($fk_payment);
790 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
791 $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$db->sanitize($invoicedettable)." as d ON d.".$db->sanitize($fk_facture)." = f.rowid";
792 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
793 $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
794 $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
795 $sql .= " AND f.type IN (0,1,2,3,5)";
796 if ($y && $m) {
797 $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
798 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
799 } elseif ($y) {
800 $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
801 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
802 }
803 if ($q) {
804 $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
805 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
806 }
807 if ($date_start && $date_end) {
808 $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
809 }
810 $sql .= " AND (d.product_type = 0"; // Limit to products
811 $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
812 if (getDolGlobalString('MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS')) {
813 $sql .= " AND (d.".$db->sanitize($f_rate)." <> 0 OR d.".$db->sanitize($total_tva)." <> 0)";
814 }
815 $sql .= " ORDER BY d.rowid, d.".$db->sanitize($fk_facture).", pf.rowid";
816 }
817
818 dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
819
820 $resql = $db->query($sql);
821 if ($resql) {
822 $rate = -1;
823 $oldrowid = '';
824 while ($assoc = $db->fetch_array($resql)) {
825 $rate_key = $assoc['rate'];
826 if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\‍(/', $rate_key)) {
827 $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
828 }
829
830 // Code to avoid warnings when array entry not defined
831 if (!isset($list[$rate_key]['totalht'])) {
832 $list[$rate_key]['totalht'] = 0;
833 }
834 if (!isset($list[$rate_key]['vat'])) {
835 $list[$rate_key]['vat'] = 0;
836 }
837 if (!isset($list[$rate_key]['localtax1'])) {
838 $list[$rate_key]['localtax1'] = 0;
839 }
840 if (!isset($list[$rate_key]['localtax2'])) {
841 $list[$rate_key]['localtax2'] = 0;
842 }
843
844 if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
845 $oldrowid = $assoc['rowid'];
846 $list[$rate_key]['totalht'] += (float) $assoc['total_ht'];
847 $list[$rate_key]['vat'] += (float) $assoc['total_vat'];
848 $list[$rate_key]['localtax1'] += (float) $assoc['total_localtax1'];
849 $list[$rate_key]['localtax2'] += (float) $assoc['total_localtax2'];
850 }
851 $list[$rate_key]['dtotal_ttc'][] = (float) $assoc['total_ttc'];
852 $list[$rate_key]['dtype'][] = (int) $assoc['dtype'];
853 $list[$rate_key]['datef'][] = $db->jdate($assoc['datef']);
854 $list[$rate_key]['datep'][] = $db->jdate($assoc['datep']);
855
856 $list[$rate_key]['company_name'][] = (string) $assoc['company_name'];
857 $list[$rate_key]['company_id'][] = (int) $assoc['company_id'];
858 $list[$rate_key]['company_alias'][] = (string) $assoc['company_alias'];
859 $list[$rate_key]['company_email'][] = (string) $assoc['company_email'];
860 $list[$rate_key]['company_tva_intra'][] = (string) $assoc['company_tva_intra'];
861 $list[$rate_key]['company_client'][] = (int) $assoc['company_client'];
862 $list[$rate_key]['company_fournisseur'][] = (int) $assoc['company_fournisseur'];
863 $list[$rate_key]['company_customer_code'][] = (string) $assoc['company_customer_code'];
864 $list[$rate_key]['company_supplier_code'][] = (string) $assoc['company_supplier_code'];
865 $list[$rate_key]['company_customer_accounting_code'][] = (string) $assoc['company_customer_accounting_code'];
866 $list[$rate_key]['company_supplier_accounting_code'][] = (string) $assoc['company_supplier_accounting_code'];
867 $list[$rate_key]['company_status'][] = (int) $assoc['company_status'];
868
869 $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
870 $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
871
872 $list[$rate_key]['facid'][] = (int) $assoc['facid'];
873 $list[$rate_key]['facnum'][] = (string) $assoc['facnum'];
874 $list[$rate_key]['type'][] = (int) $assoc['type'];
875 $list[$rate_key]['ftotal_ttc'][] = (float) $assoc['ftotal_ttc'];
876 $list[$rate_key]['descr'][] = (string) $assoc['descr'];
877
878 $list[$rate_key]['totalht_list'][] = (float) $assoc['total_ht'];
879 $list[$rate_key]['vat_list'][] = (float) $assoc['total_vat'];
880 $list[$rate_key]['localtax1_list'][] = (float) $assoc['total_localtax1'];
881 $list[$rate_key]['localtax2_list'][] = (float) $assoc['total_localtax2'];
882
883 $list[$rate_key]['pid'][] = (int) $assoc['pid'];
884 $list[$rate_key]['pref'][] = (string) $assoc['pref'];
885 $list[$rate_key]['ptype'][] = (int) $assoc['ptype'];
886
887 $list[$rate_key]['payment_id'][] = (int) $assoc['payment_id'];
888 $list[$rate_key]['payment_ref'][] = (string) $assoc['payment_ref'];
889 $list[$rate_key]['payment_amount'][] = (float) $assoc['payment_amount'];
890
891 $rate = $assoc['rate'];
892 }
893 } else {
895 return -3;
896 }
897
898 // CASE OF SERVICES
899
900 // Define sql request
901 $sql = '';
902 if (($direction == 'sell' && getDolGlobalString('TAX_MODE_SELL_SERVICE') == 'invoice')
903 || ($direction == 'buy' && getDolGlobalString('TAX_MODE_BUY_SERVICE') == 'invoice')) {
904 // Count on invoice date
905 $sql = "SELECT d.rowid, d.product_type as dtype, d.".$db->sanitize($fk_facture)." as facid, d.".$db->sanitize($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.".$db->sanitize($total_tva)." as total_vat, d.description as descr,";
906 $sql .= " d.".$db->sanitize($total_localtax1)." as total_localtax1, d.".$db->sanitize($total_localtax2)." as total_localtax2, ";
907 $sql .= " d.date_start as date_start, d.date_end as date_end,";
908 $sql .= " f.".$db->sanitize($invoicefieldref)." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
909 $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,";
910 $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
911 $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
912 $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
913 $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
914 $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
915 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($invoicetable)." as f";
916 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
917 $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$db->sanitize($invoicedettable)." as d ON d.".$db->sanitize($fk_facture)." = f.rowid";
918 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
919 $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
920 $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
921 if ($direction == 'buy') {
922 if (getDolGlobalString('FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS')) {
923 $sql .= " AND f.type IN (0,1,2,5)";
924 } else {
925 $sql .= " AND f.type IN (0,1,2,3,5)";
926 }
927 } else {
928 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
929 $sql .= " AND f.type IN (0,1,2,5)";
930 } else {
931 $sql .= " AND f.type IN (0,1,2,3,5)";
932 }
933 }
934 if ($y && $m) {
935 $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
936 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
937 } elseif ($y) {
938 $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
939 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
940 }
941 if ($q) {
942 $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
943 $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
944 }
945 if ($date_start && $date_end) {
946 $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
947 }
948 $sql .= " AND (d.product_type = 1"; // Limit to services
949 $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
950 if (getDolGlobalString('MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS')) {
951 $sql .= " AND (d.".$db->sanitize($f_rate)." <> 0 OR d.".$db->sanitize($total_tva)." <> 0)";
952 }
953 $sql .= " ORDER BY d.rowid, d.".$db->sanitize($fk_facture);
954 } else {
955 // Count on payments date
956 $sql = "SELECT d.rowid, d.product_type as dtype, d.".$db->sanitize($fk_facture)." as facid, d.".$db->sanitize($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.".$db->sanitize($total_tva)." as total_vat, d.description as descr,";
957 $sql .= " d.".$db->sanitize($total_localtax1)." as total_localtax1, d.".$db->sanitize($total_localtax2)." as total_localtax2, ";
958 $sql .= " d.date_start as date_start, d.date_end as date_end,";
959 $sql .= " f.".$db->sanitize($invoicefieldref)." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
960 $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,";
961 $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
962 $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
963 $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
964 $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
965 $sql .= " pf.".$db->sanitize($fk_payment)." as payment_id, pf.amount as payment_amount,";
966 $sql .= " pa.datep as datep, pa.ref as payment_ref";
967 $sql .= " FROM ".MAIN_DB_PREFIX.$db->sanitize($invoicetable)." as f";
968 $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$db->sanitize($paymentfacturetable)." as pf ON pf.".$db->sanitize($fk_facture2)." = f.rowid";
969 $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$db->sanitize($paymenttable)." as pa ON pa.rowid = pf.".$db->sanitize($fk_payment);
970 $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
971 $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$db->sanitize($invoicedettable)." as d ON d.".$db->sanitize($fk_facture)." = f.rowid";
972 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
973 $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
974 $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
975 $sql .= " AND f.type IN (0,1,2,3,5)";
976 if ($y && $m) {
977 $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
978 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
979 } elseif ($y) {
980 $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
981 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
982 }
983 if ($q) {
984 $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
985 $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
986 }
987 if ($date_start && $date_end) {
988 $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
989 }
990 $sql .= " AND (d.product_type = 1"; // Limit to services
991 $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
992 if (getDolGlobalString('MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS')) {
993 $sql .= " AND (d.".$db->sanitize($f_rate)." <> 0 OR d.".$db->sanitize($total_tva)." <> 0)";
994 }
995 $sql .= " ORDER BY d.rowid, d.".$db->sanitize($fk_facture).", pf.rowid";
996 }
997
998 dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
999 $resql = $db->query($sql);
1000 if ($resql) {
1001 $rate = -1;
1002 $oldrowid = '';
1003 while ($assoc = $db->fetch_array($resql)) {
1004 $rate_key = $assoc['rate'];
1005 if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\‍(/', $rate_key)) {
1006 $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
1007 }
1008
1009 // Code to avoid warnings when array entry not defined
1010 if (!isset($list[$rate_key]['totalht'])) {
1011 $list[$rate_key]['totalht'] = 0;
1012 }
1013 if (!isset($list[$rate_key]['vat'])) {
1014 $list[$rate_key]['vat'] = 0;
1015 }
1016 if (!isset($list[$rate_key]['localtax1'])) {
1017 $list[$rate_key]['localtax1'] = 0;
1018 }
1019 if (!isset($list[$rate_key]['localtax2'])) {
1020 $list[$rate_key]['localtax2'] = 0;
1021 }
1022
1023 if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
1024 $oldrowid = $assoc['rowid'];
1025 $list[$rate_key]['totalht'] += (float) $assoc['total_ht'];
1026 $list[$rate_key]['vat'] += (float) $assoc['total_vat'];
1027 $list[$rate_key]['localtax1'] += (float) $assoc['total_localtax1'];
1028 $list[$rate_key]['localtax2'] += (float) $assoc['total_localtax2'];
1029 }
1030 $list[$rate_key]['dtotal_ttc'][] = (float) $assoc['total_ttc'];
1031 $list[$rate_key]['dtype'][] = (int) $assoc['dtype'];
1032 $list[$rate_key]['datef'][] = $db->jdate($assoc['datef']);
1033 $list[$rate_key]['datep'][] = $db->jdate($assoc['datep']);
1034
1035 $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
1036 $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
1037
1038 $list[$rate_key]['company_name'][] = (string) $assoc['company_name'];
1039 $list[$rate_key]['company_id'][] = (int) $assoc['company_id'];
1040 $list[$rate_key]['company_alias'][] = (string) $assoc['company_alias'];
1041 $list[$rate_key]['company_email'][] = (string) $assoc['company_email'];
1042 $list[$rate_key]['company_tva_intra'][] = (string) $assoc['company_tva_intra'];
1043 $list[$rate_key]['company_client'][] = (int) $assoc['company_client'];
1044 $list[$rate_key]['company_fournisseur'][] = (int) $assoc['company_fournisseur'];
1045 $list[$rate_key]['company_customer_code'][] = (string) $assoc['company_customer_code'];
1046 $list[$rate_key]['company_supplier_code'][] = (string) $assoc['company_supplier_code'];
1047 $list[$rate_key]['company_customer_accounting_code'][] = (string) $assoc['company_customer_accounting_code'];
1048 $list[$rate_key]['company_supplier_accounting_code'][] = (string) $assoc['company_supplier_accounting_code'];
1049 $list[$rate_key]['company_status'][] = (int) $assoc['company_status'];
1050
1051 $list[$rate_key]['facid'][] = (int) $assoc['facid'];
1052 $list[$rate_key]['facnum'][] = (string) $assoc['facnum'];
1053 $list[$rate_key]['type'][] = (int) $assoc['type'];
1054 $list[$rate_key]['ftotal_ttc'][] = (float) $assoc['ftotal_ttc'];
1055 $list[$rate_key]['descr'][] = (string) $assoc['descr'];
1056
1057 $list[$rate_key]['totalht_list'][] = (float) $assoc['total_ht'];
1058 $list[$rate_key]['vat_list'][] = (float) $assoc['total_vat'];
1059 $list[$rate_key]['localtax1_list'][] = (float) $assoc['total_localtax1'];
1060 $list[$rate_key]['localtax2_list'][] = (float) $assoc['total_localtax2'];
1061
1062 $list[$rate_key]['pid'][] = (int) $assoc['pid'];
1063 $list[$rate_key]['pref'][] = (string) $assoc['pref'];
1064 $list[$rate_key]['ptype'][] = (int) $assoc['ptype'];
1065
1066 $list[$rate_key]['payment_id'][] = (int) $assoc['payment_id'];
1067 $list[$rate_key]['payment_ref'][] = (string) $assoc['payment_ref'];
1068 $list[$rate_key]['payment_amount'][] = (float) $assoc['payment_amount'];
1069
1070 $rate = $assoc['rate'];
1071 }
1072 } else {
1074 return -3;
1075 }
1076
1077 // CASE OF EXPENSE REPORT
1078
1079 if ($direction == 'buy') { // buy only for expense reports
1080 // Define sql request
1081 $sql = '';
1082
1083 // Count on payments date
1084 $sql = "SELECT d.rowid, d.product_type as dtype, e.rowid as facid, d.".$db->sanitize($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,";
1085 $sql .= " d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
1086 $sql .= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
1087 $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,";
1088 $sql .= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid";
1089 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as e";
1090 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid";
1091 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid";
1092 $sql .= " WHERE e.entity = ".((int) $conf->entity);
1093 $sql .= " AND e.fk_statut in (6)";
1094 if ($y && $m) {
1095 $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
1096 $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
1097 } elseif ($y) {
1098 $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
1099 $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
1100 }
1101 if ($q) {
1102 $sql .= " AND p.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
1103 $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
1104 }
1105 if ($date_start && $date_end) {
1106 $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
1107 }
1108 $sql .= " AND (d.product_type = -1";
1109 $sql .= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
1110 if (getDolGlobalString('MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS')) {
1111 $sql .= " AND (d.".$db->sanitize($f_rate)." <> 0 OR d.total_tva <> 0)";
1112 }
1113 $sql .= " ORDER BY e.rowid";
1114
1115 dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
1116 $resql = $db->query($sql);
1117 if ($resql) {
1118 $rate = -1;
1119 $oldrowid = '';
1120 while ($assoc = $db->fetch_array($resql)) {
1121 $rate_key = $assoc['rate'];
1122 if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\‍(/', $rate_key)) {
1123 $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
1124 }
1125
1126 // Code to avoid warnings when array entry not defined
1127 if (!isset($list[$rate_key]['totalht'])) {
1128 $list[$rate_key]['totalht'] = 0;
1129 }
1130 if (!isset($list[$rate_key]['vat'])) {
1131 $list[$rate_key]['vat'] = 0;
1132 }
1133 if (!isset($list[$rate_key]['localtax1'])) {
1134 $list[$rate_key]['localtax1'] = 0;
1135 }
1136 if (!isset($list[$rate_key]['localtax2'])) {
1137 $list[$rate_key]['localtax2'] = 0;
1138 }
1139
1140 if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
1141 $oldrowid = $assoc['rowid'];
1142 $list[$rate_key]['totalht'] += (float) $assoc['total_ht'];
1143 $list[$rate_key]['vat'] += (float) $assoc['total_vat'];
1144 $list[$rate_key]['localtax1'] += (float) $assoc['total_localtax1'];
1145 $list[$rate_key]['localtax2'] += (float) $assoc['total_localtax2'];
1146 }
1147
1148 $list[$rate_key]['dtotal_ttc'][] = (float) $assoc['total_ttc'];
1149 $list[$rate_key]['dtype'][] = 'ExpenseReportPayment';
1150 $list[$rate_key]['datef'][] = (int) $assoc['datef'];
1151 $list[$rate_key]['company_name'][] = '';
1152 $list[$rate_key]['company_id'][] = 0;
1153 $list[$rate_key]['user_id'][] = (int) $assoc['fk_user_author'];
1154 $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
1155 $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
1156
1157 $list[$rate_key]['facid'][] = (int) $assoc['facid'];
1158 $list[$rate_key]['facnum'][] = (string) $assoc['facnum'];
1159 $list[$rate_key]['type'][] = (int) $assoc['type'];
1160 $list[$rate_key]['ftotal_ttc'][] = (float) $assoc['ftotal_ttc'];
1161 $list[$rate_key]['descr'][] = (string) $assoc['descr'];
1162
1163 $list[$rate_key]['totalht_list'][] = (float) $assoc['total_ht'];
1164 $list[$rate_key]['vat_list'][] = (float) $assoc['total_vat'];
1165 $list[$rate_key]['localtax1_list'][] = (float) $assoc['total_localtax1'];
1166 $list[$rate_key]['localtax2_list'][] = (float) $assoc['total_localtax2'];
1167
1168 $list[$rate_key]['pid'][] = (int) $assoc['pid'];
1169 $list[$rate_key]['pref'][] = (string) $assoc['pref'];
1170 $list[$rate_key]['ptype'][] = 'ExpenseReportPayment';
1171
1172 $list[$rate_key]['payment_id'][] = (int) $assoc['payment_id'];
1173 $list[$rate_key]['payment_ref'][] = (string) $assoc['payment_ref'];
1174 $list[$rate_key]['payment_amount'][] = (float) $assoc['payment_amount'];
1175
1176 $rate = $assoc['rate'];
1177 }
1178 } else {
1180 return -3;
1181 }
1182 }
1183
1184 return $list;
1185}
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:605
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:624
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
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
$date_start
Variables from include:
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.
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
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:674
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