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