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