dolibarr 20.0.4
index.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2001-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2017 Olivier Geffroy <jeff@jeffinfo.com>
6 * Copyright (C) 2018-2020 Frédéric France <frederic.france@netlogic.fr>
7 * Copyright (C) 2024 Benjamin B. <b.crozon@trebisol.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
28// Load Dolibarr environment
29require '../../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('compta', 'bills', 'donation', 'salaries'));
35
36$date_startday = GETPOSTINT('date_startday');
37$date_startmonth = GETPOSTINT('date_startmonth');
38$date_startyear = GETPOSTINT('date_startyear');
39$date_endday = GETPOSTINT('date_endday');
40$date_endmonth = GETPOSTINT('date_endmonth');
41$date_endyear = GETPOSTINT('date_endyear');
42
43$nbofyear = 4;
44
45// Date range
46$year = GETPOSTINT('year');
47if (empty($year)) {
48 $year_current = dol_print_date(dol_now(), "%Y");
49 $month_current = dol_print_date(dol_now(), "%m");
50 $year_start = $year_current - ($nbofyear - 1);
51} else {
52 $year_current = $year;
53 $month_current = dol_print_date(dol_now(), "%m");
54 $year_start = $year - $nbofyear + (getDolGlobalInt('SOCIETE_FISCAL_MONTH_START') > 1 ? 0 : 1);
55}
56$date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear, 'tzserver'); // We use timezone of server so report is same from everywhere
57$date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear, 'tzserver'); // We use timezone of server so report is same from everywhere
58
59// We define date_start and date_end
60if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
61 $q = GETPOSTINT("q");
62 if (empty($q)) {
63 // We define date_start and date_end
64 $year_end = $year_start + $nbofyear - (getDolGlobalInt('SOCIETE_FISCAL_MONTH_START') > 1 ? 0 : 1);
65 $month_start = GETPOSTISSET("month") ? GETPOSTINT("month") : getDolGlobalInt('SOCIETE_FISCAL_MONTH_START', 1);
66 if (!GETPOST('month')) { // If month not forced
67 if (!$year && $month_start > $month_current) {
68 $year_start--;
69 $year_end--;
70 }
71 $month_end = $month_start - 1;
72 if ($month_end < 1) {
73 $month_end = 12;
74 }
75 } else {
76 $month_end = $month_start;
77 }
78 $date_start = dol_get_first_day($year_start, $month_start, false);
79 $date_end = dol_get_last_day($year_end, $month_end, false);
80 }
81 if ($q == 1) {
82 $date_start = dol_get_first_day($year_start, 1, false);
83 $date_end = dol_get_last_day($year_start, 3, false);
84 }
85 if ($q == 2) {
86 $date_start = dol_get_first_day($year_start, 4, false);
87 $date_end = dol_get_last_day($year_start, 6, false);
88 }
89 if ($q == 3) {
90 $date_start = dol_get_first_day($year_start, 7, false);
91 $date_end = dol_get_last_day($year_start, 9, false);
92 }
93 if ($q == 4) {
94 $date_start = dol_get_first_day($year_start, 10, false);
95 $date_end = dol_get_last_day($year_start, 12, false);
96 }
97}
98
99$userid = GETPOSTINT('userid');
100$socid = GETPOSTINT('socid');
101
102$tmps = dol_getdate($date_start);
103$month_start = $tmps['mon'];
104$year_start = $tmps['year'];
105$tmpe = dol_getdate($date_end);
106$month_end = $tmpe['mon'];
107$year_end = $tmpe['year'];
108$nbofyear = ($year_end - $year_start) + 1;
109
110// Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES' or 'BOOKKEEPING')
111$modecompta = getDolGlobalString('ACCOUNTING_MODE');
112if (isModEnabled('accounting')) {
113 $modecompta = 'BOOKKEEPING';
114}
115if (GETPOST("modecompta", 'alpha')) {
116 $modecompta = GETPOST("modecompta", 'alpha');
117}
118
119// Security check
120if ($user->socid > 0) {
121 $socid = $user->socid;
122}
123if (isModEnabled('comptabilite')) {
124 $result = restrictedArea($user, 'compta', '', '', 'resultat');
125}
126if (isModEnabled('accounting')) {
127 $result = restrictedArea($user, 'accounting', '', '', 'comptarapport');
128}
129
130
131
132
133/*
134 * View
135 */
136
137$param = '';
138if ($date_startday && $date_startmonth && $date_startyear) {
139 $param .= '&date_startday='.$date_startday.'&date_startmonth='.$date_startmonth.'&date_startyear='.$date_startyear;
140}
141if ($date_endday && $date_endmonth && $date_endyear) {
142 $param .= '&date_endday='.$date_endday.'&date_endmonth='.$date_endmonth.'&date_endyear='.$date_endyear;
143}
144
145llxHeader();
146
147$form = new Form($db);
148
149$exportlink = '';
150$namelink = '';
151$builddate = dol_now();
152
153// Affiche en-tete du rapport
154if ($modecompta == "CREANCES-DETTES") {
155 $name = $langs->trans("Turnover");
156 $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
157 $description = $langs->trans("RulesCADue");
158 if (getDolGlobalString('FACTURE_DEPOSITS_ARE_JUST_PAYMENTS')) {
159 $description .= $langs->trans("DepositsAreNotIncluded");
160 } else {
161 $description .= $langs->trans("DepositsAreIncluded");
162 }
163 //$exportlink=$langs->trans("NotYetAvailable");
164} elseif ($modecompta == "RECETTES-DEPENSES") {
165 $name = $langs->trans("TurnoverCollected");
166 $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
167 $description = $langs->trans("RulesCAIn");
168 $description .= $langs->trans("DepositsAreIncluded");
169 //$exportlink=$langs->trans("NotYetAvailable");
170} elseif ($modecompta == "BOOKKEEPING") {
171 $name = $langs->trans("Turnover");
172 $periodlink = ($year_start ? "<a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear - 2)."&modecompta=".$modecompta."'>".img_previous()."</a> <a href='".$_SERVER["PHP_SELF"]."?year=".($year_start + $nbofyear)."&modecompta=".$modecompta."'>".img_next()."</a>" : "");
173 $description = $langs->trans("RulesSalesTurnoverOfIncomeAccounts");
174 //$exportlink=$langs->trans("NotYetAvailable");
175}
176$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0, 0, '', '', '', '', 1, '', '', 'tzserver');
177$period .= ' - ';
178$period .= $form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0, 0, '', '', '', '', 1, '', '', 'tzserver');
179
180$moreparam = array();
181if (!empty($modecompta)) {
182 $moreparam['modecompta'] = $modecompta;
183}
184
185// Define $calcmode line
186$calcmode = '';
187if ($modecompta == "RECETTES-DEPENSES" || $modecompta == "BOOKKEEPINGCOLLECTED") {
188 /*if (isModEnabled('accounting')) {
189 $calcmode .= '<input type="radio" name="modecompta" id="modecompta3" value="BOOKKEEPINGCOLLECTED"'.($modecompta == 'BOOKKEEPINGCOLLECTED' ? ' checked="checked"' : '').'><label for="modecompta3"> '.$langs->trans("CalcModeBookkeeping").'</label>';
190 $calcmode .= '<br>';
191 }*/
192 $calcmode .= '<input type="radio" name="modecompta" id="modecompta2" value="RECETTES-DEPENSES"'.($modecompta == 'RECETTES-DEPENSES' ? ' checked="checked"' : '').'><label for="modecompta2"> '.$langs->trans("CalcModePayment");
193 if (isModEnabled('accounting')) {
194 $calcmode .= ' <span class="opacitymedium hideonsmartphone">('.$langs->trans("CalcModeNoBookKeeping").')</span>';
195 }
196 $calcmode .= '</label>';
197} else {
198 if (isModEnabled('accounting')) {
199 $calcmode .= '<input type="radio" name="modecompta" id="modecompta3" value="BOOKKEEPING"'.($modecompta == 'BOOKKEEPING' ? ' checked="checked"' : '').'><label for="modecompta3"> '.$langs->trans("CalcModeBookkeeping").'</label>';
200 $calcmode .= ' <span class="opacitymedium hideonsmartphone">('.$langs->trans("DataMustHaveBeenTransferredInAccounting").')</span>';
201 $calcmode .= '<br>';
202 }
203 $calcmode .= '<input type="radio" name="modecompta" id="modecompta2" value="CREANCES-DETTES"'.($modecompta == 'CREANCES-DETTES' ? ' checked="checked"' : '').'><label for="modecompta2"> '.$langs->trans("CalcModeDebt");
204 if (isModEnabled('accounting')) {
205 $calcmode .= ' <span class="opacitymedium hideonsmartphone">('.$langs->trans("CalcModeNoBookKeeping").')</span>';
206 }
207 $calcmode .= '</label>';
208}
209
210report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam, $calcmode);
211
212if (isModEnabled('accounting')) {
213 if ($modecompta != 'BOOKKEEPING') {
214 print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1);
215 } else {
216 // Test if there is at least one line in bookkeeping
217 $pcgverid = getDolGlobalInt('CHARTOFACCOUNTS');
218 $pcgvercode = dol_getIdFromCode($db, $pcgverid, 'accounting_system', 'rowid', 'pcg_version');
219 if (empty($pcgvercode)) {
220 $pcgvercode = $pcgverid;
221 }
222
223 $sql = "SELECT b.rowid ";
224 $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b,";
225 $sql .= " ".MAIN_DB_PREFIX."accounting_account as aa";
226 $sql .= " WHERE b.entity = ".$conf->entity; // In module double party accounting, we never share entities
227 $sql .= " AND b.numero_compte = aa.account_number";
228 $sql .= " AND aa.entity = ".$conf->entity;
229 $sql .= " AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'";
230 $sql .= $db->plimit(1);
231
232 $resql = $db->query($sql);
233 $nb = $db->num_rows($resql);
234 if ($nb == 0) {
235 $langs->load("errors");
236 print info_admin($langs->trans("WarningNoDataTransferedInAccountancyYet"), 0, 0, 1);
237 }
238 }
239}
240
241
242if ($modecompta == 'CREANCES-DETTES') {
243 $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_ht) as amount, sum(f.total_ttc) as amount_ttc";
244 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
245 $sql .= " WHERE f.fk_statut in (1,2)";
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 $sql .= " AND f.entity IN (".getEntity('invoice').")";
252 if ($socid) {
253 $sql .= " AND f.fk_soc = ".((int) $socid);
254 }
255} elseif ($modecompta == "RECETTES-DEPENSES") {
256 /*
257 * Liste des paiements (les anciens paiements ne sont pas vus par cette requete car, sur les
258 * vieilles versions, ils n'etaient pas lies via paiement_facture. On les ajoute plus loin)
259 */
260 $sql = "SELECT date_format(p.datep, '%Y-%m') as dm, sum(pf.amount) as amount_ttc";
261 $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
262 $sql .= ", ".MAIN_DB_PREFIX."paiement_facture as pf";
263 $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
264 $sql .= " WHERE p.rowid = pf.fk_paiement";
265 $sql .= " AND pf.fk_facture = f.rowid";
266 $sql .= " AND f.entity IN (".getEntity('invoice').")";
267 if ($socid) {
268 $sql .= " AND f.fk_soc = ".((int) $socid);
269 }
270} elseif ($modecompta == "BOOKKEEPING") {
271 $pcgverid = getDolGlobalInt('CHARTOFACCOUNTS');
272 $pcgvercode = dol_getIdFromCode($db, $pcgverid, 'accounting_system', 'rowid', 'pcg_version');
273 if (empty($pcgvercode)) {
274 $pcgvercode = $pcgverid;
275 }
276
277 $sql = "SELECT date_format(b.doc_date, '%Y-%m') as dm, sum(b.credit - b.debit) as amount_ttc";
278 $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b,";
279 $sql .= " ".MAIN_DB_PREFIX."accounting_account as aa";
280 $sql .= " WHERE b.entity = ".$conf->entity; // In module double party accounting, we never share entities
281 $sql .= " AND b.numero_compte = aa.account_number";
282 $sql .= " AND b.doc_type = 'customer_invoice'";
283 $sql .= " AND aa.entity = ".$conf->entity;
284 $sql .= " AND aa.fk_pcg_version = '".$db->escape($pcgvercode)."'";
285 $sql .= " AND aa.pcg_type = 'INCOME'"; // TODO Be able to use a custom group
286}
287$sql .= " GROUP BY dm";
288$sql .= " ORDER BY dm";
289// TODO Add a filter on $date_start and $date_end to reduce quantity on data
290//print $sql;
291
292$minyearmonth = $maxyearmonth = 0;
293
294$cumulative = array();
295$cumulative_ht = array();
296$total_ht = array();
297$total = array();
298
299$result = $db->query($sql);
300if ($result) {
301 $num = $db->num_rows($result);
302 $i = 0;
303 while ($i < $num) {
304 $obj = $db->fetch_object($result);
305 $cumulative_ht[$obj->dm] = empty($obj->amount) ? 0 : $obj->amount;
306 $cumulative[$obj->dm] = empty($obj->amount_ttc) ? 0 : $obj->amount_ttc;
307 if ($obj->amount_ttc) {
308 $minyearmonth = ($minyearmonth ? min($minyearmonth, $obj->dm) : $obj->dm);
309 $maxyearmonth = max($maxyearmonth, $obj->dm);
310 }
311 $i++;
312 }
313 $db->free($result);
314} else {
315 dol_print_error($db);
316}
317
318// On ajoute les paiements anciennes version, non lies par paiement_facture (very old versions)
319if ($modecompta == 'RECETTES-DEPENSES') {
320 $sql = "SELECT date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ttc";
321 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
322 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
323 $sql .= ", ".MAIN_DB_PREFIX."paiement as p";
324 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."paiement_facture as pf ON p.rowid = pf.fk_paiement";
325 $sql .= " WHERE pf.rowid IS NULL";
326 $sql .= " AND p.fk_bank = b.rowid";
327 $sql .= " AND b.fk_account = ba.rowid";
328 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
329 $sql .= " GROUP BY dm";
330 $sql .= " ORDER BY dm";
331
332 $result = $db->query($sql);
333 if ($result) {
334 $num = $db->num_rows($result);
335 $i = 0;
336 while ($i < $num) {
337 $obj = $db->fetch_object($result);
338 if (empty($cumulative[$obj->dm])) {
339 $cumulative[$obj->dm] = $obj->amount_ttc;
340 } else {
341 $cumulative[$obj->dm] += $obj->amount_ttc;
342 }
343 if ($obj->amount_ttc) {
344 $minyearmonth = ($minyearmonth ? min($minyearmonth, $obj->dm) : $obj->dm);
345 $maxyearmonth = max($maxyearmonth, $obj->dm);
346 }
347 $i++;
348 }
349 } else {
350 dol_print_error($db);
351 }
352}
353
354$moreforfilter = '';
355
356print '<div class="div-table-responsive">';
357print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";
358
359print '<tr class="liste_titre"><td>&nbsp;</td>';
360
361for ($annee = $year_start; $annee <= $year_end; $annee++) {
362 if ($modecompta == 'CREANCES-DETTES') {
363 print '<td align="center" width="10%" colspan="3">';
364 } else {
365 print '<td align="center" width="10%" colspan="2" class="borderrightlight">';
366 }
367 if ($modecompta != 'BOOKKEEPING') {
368 print '<a href="casoc.php?year='.$annee.($modecompta ? '&modecompta='.$modecompta : '').'">';
369 }
370 print $annee;
371 if (getDolGlobalInt('SOCIETE_FISCAL_MONTH_START') > 1) {
372 print '-'.($annee + 1);
373 }
374 if ($modecompta != 'BOOKKEEPING') {
375 print '</a>';
376 }
377 print '</td>';
378 if ($annee != $year_end) {
379 print '<td width="15">&nbsp;</td>';
380 }
381}
382print '</tr>';
383
384print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Month").'</td>';
385for ($annee = $year_start; $annee <= $year_end; $annee++) {
386 if ($modecompta == 'CREANCES-DETTES') {
387 print '<td class="liste_titre right">'.$langs->trans("AmountHT").'</td>';
388 }
389 print '<td class="liste_titre right">';
390 if ($modecompta == "BOOKKEEPING") {
391 print $langs->trans("Amount");
392 } else {
393 print $langs->trans("AmountTTC");
394 }
395 print '</td>';
396 print '<td class="liste_titre right borderrightlight">'.$langs->trans("Delta").'</td>';
397 if ($annee != $year_end) {
398 print '<td class="liste_titre" width="15">&nbsp;</td>';
399 }
400}
401print '</tr>';
402
403$now_show_delta = 0;
404$minyear = substr($minyearmonth, 0, 4);
405$maxyear = substr($maxyearmonth, 0, 4);
406$nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
407$nowyearmonth = dol_print_date(dol_now(), "%Y%m");
408$maxyearmonth = max($maxyearmonth, $nowyearmonth);
409$now = dol_now();
410$casenow = dol_print_date($now, "%Y-%m");
411
412// Loop on each month
413$nb_mois_decalage = GETPOSTISSET('date_startmonth') ? (GETPOSTINT('date_startmonth') - 1) : (!getDolGlobalInt('SOCIETE_FISCAL_MONTH_START') ? 0 : (getDolGlobalInt('SOCIETE_FISCAL_MONTH_START') - 1));
414for ($mois = 1 + $nb_mois_decalage; $mois <= 12 + $nb_mois_decalage; $mois++) {
415 $mois_modulo = $mois; // ajout
416 if ($mois > 12) {
417 $mois_modulo = $mois - 12;
418 } // ajout
419
420 if ($year_start == $year_end) {
421 // If we show only one year or one month, we do not show month before the selected month
422 if ($mois < $date_startmonth && $year_start <= $date_startyear) {
423 continue;
424 }
425 // If we show only one year or one month, we do not show month after the selected month
426 if ($mois > $date_endmonth && $year_end >= $date_endyear) {
427 break;
428 }
429 }
430
431 print '<tr class="oddeven">';
432
433 // Month
434 print "<td>".dol_print_date(dol_mktime(12, 0, 0, $mois_modulo, 1, 2000), "%B")."</td>";
435
436 for ($annee = $year_start - 1; $annee <= $year_end; $annee++) { // We start one year before to have data to be able to make delta
437 $annee_decalage = $annee;
438 if ($mois > 12) {
439 $annee_decalage = $annee + 1;
440 }
441 $case = dol_print_date(dol_mktime(1, 1, 1, $mois_modulo, 1, $annee_decalage), "%Y-%m");
442 $caseprev = dol_print_date(dol_mktime(1, 1, 1, $mois_modulo, 1, $annee_decalage - 1), "%Y-%m");
443
444 if ($annee >= $year_start) { // We ignore $annee < $year_start, we loop on it to be able to make delta, nothing is output.
445 if ($modecompta == 'CREANCES-DETTES') {
446 // Value turnover of month w/o VAT
447 print '<td class="right">';
448 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
449 if (!empty($cumulative_ht[$case])) {
450 $now_show_delta = 1; // On a trouve le premier mois de la premiere annee generant du chiffre.
451 print '<a href="casoc.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">'.price($cumulative_ht[$case], 1).'</a>';
452 } else {
453 if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
454 print '0';
455 } else {
456 print '&nbsp;';
457 }
458 }
459 }
460 print "</td>";
461 }
462
463 // Value turnover of month
464 print '<td class="right">';
465 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
466 if (!empty($cumulative[$case])) {
467 $now_show_delta = 1; // On a trouve le premier mois de la premiere annee generant du chiffre.
468 if ($modecompta != 'BOOKKEEPING') {
469 print '<a href="casoc.php?year='.$annee_decalage.'&month='.$mois_modulo.($modecompta ? '&modecompta='.$modecompta : '').'">';
470 }
471 print price($cumulative[$case], 1);
472 if ($modecompta != 'BOOKKEEPING') {
473 print '</a>';
474 }
475 } else {
476 if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
477 print '0';
478 } else {
479 print '&nbsp;';
480 }
481 }
482 }
483 print "</td>";
484
485 // Percentage of month
486 print '<td class="borderrightlight right"><span class="opacitymedium">';
487 //var_dump($annee.' '.$year_end.' '.$mois.' '.$month_end);
488 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
489 if ($annee_decalage > $minyear && $case <= $casenow) {
490 if ($modecompta=='CREANCES-DETTES') {
491 $cumulative_previous_year = (!empty($cumulative_ht[$caseprev])?$cumulative_ht[$caseprev]:0);
492 $cumulative_year = (!empty($cumulative_ht[$case])?$cumulative_ht[$case]:0);
493 } else {
494 $cumulative_previous_year = (!empty($cumulative[$caseprev])?$cumulative[$caseprev]:0);
495 $cumulative_year = (!empty($cumulative[$case])?$cumulative[$case]:0);
496 }
497 if (!empty($cumulative_previous_year) && !empty($cumulative_year)) {
498 $percent = (round(($cumulative_year - $cumulative_previous_year) / $cumulative_previous_year, 4) * 100);
499 //print "X $cumulative_year - $cumulative_previous_year - $cumulative_previous_year - $percent X";
500 print($percent >= 0 ? "+$percent" : "$percent").'%';
501 }
502 if (!empty($cumulative_previous_year) && empty($cumulative_year)) {
503 print '-100%';
504 }
505 if (empty($cumulative_previous_year) && !empty($cumulative_year)) {
506 //print '<td class="right">+Inf%</td>';
507 print '-';
508 }
509 if (isset($cumulative_previous_year) && empty($cumulative_previous_year) && empty($cumulative_year)) {
510 print '+0%';
511 }
512 if (!isset($cumulative_previous_year) && empty($cumulative_year)) {
513 print '-';
514 }
515 } else {
516 if ($minyearmonth <= $case && $case <= $maxyearmonth) {
517 print '-';
518 } else {
519 print '&nbsp;';
520 }
521 }
522 }
523 print '</span></td>';
524
525 if ($annee_decalage < $year_end || ($annee_decalage == $year_end && $mois > 12 && $annee < $year_end)) {
526 print '<td width="15">&nbsp;</td>';
527 }
528 }
529
530 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
531 if (empty($total_ht[$annee])) {
532 $total_ht[$annee] = (empty($cumulative_ht[$case]) ? 0 : $cumulative_ht[$case]);
533 } else {
534 $total_ht[$annee] += (empty($cumulative_ht[$case]) ? 0 : $cumulative_ht[$case]);
535 }
536 if (empty($total[$annee])) {
537 $total[$annee] = empty($cumulative[$case]) ? 0 : $cumulative[$case];
538 } else {
539 $total[$annee] += empty($cumulative[$case]) ? 0 : $cumulative[$case];
540 }
541 }
542 }
543
544 print '</tr>';
545}
546
547/*
548 for ($mois = 1 ; $mois < 13 ; $mois++)
549 {
550
551 print '<tr class="oddeven">';
552
553 print "<td>".dol_print_date(dol_mktime(12,0,0,$mois,1,2000),"%B")."</td>";
554 for ($annee = $year_start ; $annee <= $year_end ; $annee++)
555 {
556 $casenow = dol_print_date(dol_now(),"%Y-%m");
557 $case = dol_print_date(dol_mktime(1,1,1,$mois,1,$annee),"%Y-%m");
558 $caseprev = dol_print_date(dol_mktime(1,1,1,$mois,1,$annee-1),"%Y-%m");
559
560 // Valeur CA du mois
561 print '<td class="right">';
562 if ($cumulative[$case])
563 {
564 $now_show_delta=1; // On a trouve le premier mois de la premiere annee generant du chiffre.
565 print '<a href="casoc.php?year='.$annee.'&month='.$mois.'">'.price($cumulative[$case],1).'</a>';
566 }
567 else
568 {
569 if ($minyearmonth < $case && $case <= max($maxyearmonth,$nowyearmonth)) { print '0'; }
570 else { print '&nbsp;'; }
571 }
572 print "</td>";
573
574 // Pourcentage du mois
575 if ($annee > $minyear && $case <= $casenow) {
576 if ($cumulative[$caseprev] && $cumulative[$case])
577 {
578 $percent=(round(($cumulative[$case]-$cumulative[$caseprev])/$cumulative[$caseprev],4)*100);
579 //print "X $cumulative[$case] - $cumulative[$caseprev] - $cumulative[$caseprev] - $percent X";
580 print '<td class="right">'.($percent>=0?"+$percent":"$percent").'%</td>';
581
582 }
583 if ($cumulative[$caseprev] && ! $cumulative[$case])
584 {
585 print '<td class="right">-100%</td>';
586 }
587 if (! $cumulative[$caseprev] && $cumulative[$case])
588 {
589 print '<td class="right">+Inf%</td>';
590 }
591 if (! $cumulative[$caseprev] && ! $cumulative[$case])
592 {
593 print '<td class="right">+0%</td>';
594 }
595 }
596 else
597 {
598 print '<td class="right">';
599 if ($minyearmonth <= $case && $case <= $maxyearmonth) { print '-'; }
600 else { print '&nbsp;'; }
601 print '</td>';
602 }
603
604 $total[$annee]+=$cumulative[$case];
605 if ($annee != $year_end) print '<td width="15">&nbsp;</td>';
606 }
607
608 print '</tr>';
609 }
610 */
611
612// Show total
613print '<tr class="liste_total"><td>'.$langs->trans("Total").'</td>';
614for ($annee = $year_start; $annee <= $year_end; $annee++) {
615 if ($modecompta == 'CREANCES-DETTES') {
616 // Montant total HT
617 if (isset($total_ht[$annee]) || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
618 print '<td class="nowrap right">';
619 print(empty($total_ht[$annee]) ? '0' : price($total_ht[$annee]));
620 print "</td>";
621 } else {
622 print '<td>&nbsp;</td>';
623 }
624 }
625
626 // Total amount
627 if (!empty($total[$annee]) || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
628 print '<td class="nowrap right">';
629 print(empty($total[$annee]) ? '0' : price($total[$annee]));
630 print "</td>";
631 } else {
632 print '<td>&nbsp;</td>';
633 }
634
635 // Pourcentage total
636 if ($annee > $minyear && $annee <= max($nowyear, $maxyear)) {
637 if ($modecompta == 'CREANCES-DETTES') {
638 $total_previous_year = (!empty($total_ht[$annee - 1])?$total_ht[$annee - 1]:0);
639 $total_year = (!empty($total_ht[$annee])?$total_ht[$annee]:0);
640 } else {
641 $total_previous_year = (!empty($total[$annee - 1])?$total[$annee - 1]:0);
642 $total_year = (!empty($total[$annee])?$total[$annee]:0);
643 }
644 if (!empty($total_previous_year) && !empty($total_year)) {
645 $percent = (round(($total_year - $total_previous_year) / $total_previous_year, 4) * 100);
646 print '<td class="nowrap borderrightlight right">';
647 print($percent >= 0 ? "+$percent" : "$percent").'%';
648 print '</td>';
649 }
650 if (!empty($total_previous_year) && empty($total_year)) {
651 print '<td class="borderrightlight right">-100%</td>';
652 }
653 if (empty($total_previous_year) && !empty($total_year)) {
654 print '<td class="borderrightlight right">+'.$langs->trans('Inf').'%</td>';
655 }
656 if (empty($total_previous_year) && empty($total_year)) {
657 print '<td class="borderrightlight right">+0%</td>';
658 }
659 } else {
660 print '<td class="borderrightlight right">';
661 if (!empty($total_ht[$annee]) || ($minyear <= $annee && $annee <= max($nowyear, $maxyear))) {
662 print '-';
663 } else {
664 print '&nbsp;';
665 }
666 print '</td>';
667 }
668
669 if ($annee != $year_end) {
670 print '<td width="15">&nbsp;</td>';
671 }
672}
673print "</tr>\n";
674print "</table>";
675print '</div>';
676
677
678/*
679 * En mode recettes/depenses, on complete avec les montants factures non regles
680 * et les propales signees mais pas facturees. En effet, en recettes-depenses,
681 * on comptabilise lorsque le montant est sur le compte donc il est interessant
682 * d'avoir une vision de ce qui va arriver.
683 */
684
685/*
686 Je commente toute cette partie car les chiffres affichees sont faux - Eldy.
687 En attendant correction.
688
689 if ($modecompta != 'CREANCES-DETTES')
690 {
691
692 print '<br><table width="100%" class="noborder">';
693
694 // Factures non reglees
695 // Y a bug ici. Il faut prendre le reste a payer et non le total des factures non reglees !
696
697 $sql = "SELECT f.ref, f.rowid, s.nom, s.rowid as socid, f.total_ttc, sum(pf.amount) as am";
698 $sql .= " FROM ".MAIN_DB_PREFIX."societe as s,".MAIN_DB_PREFIX."facture as f left join ".MAIN_DB_PREFIX."paiement_facture as pf on f.rowid=pf.fk_facture";
699 $sql .= " WHERE s.rowid = f.fk_soc AND f.paye = 0 AND f.fk_statut = 1";
700 if ($socid)
701 {
702 $sql .= " AND f.fk_soc = $socid";
703 }
704 $sql .= " GROUP BY f.ref,f.rowid,s.nom, s.rowid, f.total_ttc";
705
706 $resql=$db->query($sql);
707 if ($resql)
708 {
709 $num = $db->num_rows($resql);
710 $i = 0;
711
712 if ($num)
713 {
714 $total_ttc_Rac = $totalam_Rac = $total_Rac = 0;
715 while ($i < $num)
716 {
717 $obj = $db->fetch_object($resql);
718 $total_ttc_Rac += $obj->total_ttc;
719 $totalam_Rac += $obj->am;
720 $i++;
721 }
722
723 print "<tr class="oddeven"><td class=\"right\" colspan=\"5\"><i>Facture a encaisser : </i></td><td class=\"right\"><i>".price($total_ttc_Rac)."</i></td><td colspan=\"5\"><-- bug ici car n'exclut pas le deja r?gl? des factures partiellement r?gl?es</td></tr>";
724 }
725 $db->free($resql);
726 }
727 else
728 {
729 dol_print_error($db);
730 }
731 */
732
733/*
734 *
735 * Propales signees, et non facturees
736 *
737 */
738
739/*
740 Je commente toute cette partie car les chiffres affichees sont faux - Eldy.
741 En attendant correction.
742
743 $sql = "SELECT sum(f.total_ht) as tot_fht,sum(f.total_ttc) as tot_fttc, p.rowid, p.ref, s.nom, s.rowid as socid, p.total_ht, p.total_ttc
744 FROM ".MAIN_DB_PREFIX."commande AS p, ".MAIN_DB_PREFIX."societe AS s
745 LEFT JOIN ".MAIN_DB_PREFIX."co_fa AS co_fa ON co_fa.fk_commande = p.rowid
746 LEFT JOIN ".MAIN_DB_PREFIX."facture AS f ON co_fa.fk_facture = f.rowid
747 WHERE p.fk_soc = s.rowid
748 AND p.fk_statut >=1
749 AND p.facture =0";
750 if ($socid)
751 {
752 $sql .= " AND f.fk_soc = ".((int) $socid);
753 }
754 $sql .= " GROUP BY p.rowid";
755
756 $resql=$db->query($sql);
757 if ($resql)
758 {
759 $num = $db->num_rows($resql);
760 $i = 0;
761
762 if ($num)
763 {
764 $total_pr = 0;
765 while ($i < $num)
766 {
767 $obj = $db->fetch_object($resql);
768 $total_pr += $obj->total_ttc-$obj->tot_fttc;
769 $i++;
770 }
771
772 print "<tr class="oddeven"><td class=\"right\" colspan=\"5\"><i>Signe et non facture:</i></td><td class=\"right\"><i>".price($total_pr)."</i></td><td colspan=\"5\"><-- bug ici, ca devrait exclure le deja facture</td></tr>";
773 }
774 $db->free($resql);
775 }
776 else
777 {
778 dol_print_error($db);
779 }
780 print "<tr class="oddeven"><td class=\"right\" colspan=\"5\"><i>Total CA previsionnel : </i></td><td class=\"right\"><i>".price($total_CA)."</i></td><td colspan=\"3\"><-- bug ici car bug sur les 2 precedents</td></tr>";
781 }
782 print "</table>";
783
784 */
785
786// End of page
787llxFooter();
788$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
Class to manage generation of HTML components Only common components must be here.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition date.lib.php:595
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition date.lib.php:614
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_previous($titlealt='default', $moreatt='')
Show previous logo.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
img_next($titlealt='default', $moreatt='')
Show next logo.
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='')
Show information in HTML for admin users or standard users.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink='', $moreparam=array(), $calcmode='', $varlink='')
Show header of a report.
restrictedArea(User $user, $features, $object=0, $tableandshare='', $feature2='', $dbt_keyfield='fk_soc', $dbt_select='rowid', $isdraft=0, $mode=0)
Check permissions of a user to show a page and an object.