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