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