27 require
'../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
32 $langs->loadLangs(array(
'compta',
'bills',
'donation',
'salaries'));
34 $date_startday =
GETPOST(
'date_startday',
'int');
35 $date_startmonth =
GETPOST(
'date_startmonth',
'int');
36 $date_startyear =
GETPOST(
'date_startyear',
'int');
37 $date_endday =
GETPOST(
'date_endday',
'int');
38 $date_endmonth =
GETPOST(
'date_endmonth',
'int');
39 $date_endyear =
GETPOST(
'date_endyear',
'int');
48 $year_start = $year_current - ($nbofyear - 1);
50 $year_current = $year;
52 $year_start = $year - ($nbofyear - 1);
54 $date_start =
dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear,
'tzserver');
55 $date_end =
dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear,
'tzserver');
58 if (empty($date_start) || empty($date_end)) {
62 $year_end = $year_start + ($nbofyear - 1);
63 $month_start =
GETPOSTISSET(
"month") ?
GETPOST(
"month",
'int') : ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1);
65 if (!
GETPOST(
"year") && $month_start > $month_current) {
69 $month_end = $month_start - 1;
76 $month_end = $month_start;
99 $userid =
GETPOST(
'userid',
'int');
100 $socid =
GETPOST(
'socid',
'int');
103 $mothn_start = $tmps[
'mon'];
104 $year_start = $tmps[
'year'];
106 $month_end = $tmpe[
'mon'];
107 $year_end = $tmpe[
'year'];
108 $nbofyear = ($year_end - $year_start) + 1;
111 $modecompta = $conf->global->ACCOUNTING_MODE;
113 $modecompta =
'BOOKKEEPING';
116 $modecompta =
GETPOST(
"modecompta",
'alpha');
120 if ($user->socid > 0) {
121 $socid = $user->socid;
127 $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
138 if ($date_startday && $date_startmonth && $date_startyear) {
139 $param .=
'&date_startday='.$date_startday.
'&date_startmonth='.$date_startmonth.
'&date_startyear='.$date_startyear;
141 if ($date_endday && $date_endmonth && $date_endyear) {
142 $param .=
'&date_endday='.$date_endday.
'&date_endmonth='.$date_endmonth.
'&date_endyear='.$date_endyear;
152 if ($modecompta ==
"CREANCES-DETTES") {
153 $name = $langs->trans(
"Turnover");
154 $calcmode = $langs->trans(
"CalcModeDebt");
157 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInBookkeepingMode",
'{link1}',
'{link2}').
')';
158 $calcmode = str_replace(
'{link1}',
'<a class="bold" href="'.$_SERVER[
"PHP_SELF"].
'?'.($param ? $param :
'year_start='.$year_start).
'&modecompta=BOOKKEEPING">', $calcmode);
159 $calcmode = str_replace(
'{link2}',
'</a>', $calcmode);
161 $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>" :
"");
162 $description = $langs->trans(
"RulesCADue");
163 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
164 $description .= $langs->trans(
"DepositsAreNotIncluded");
166 $description .= $langs->trans(
"DepositsAreIncluded");
170 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
171 $name = $langs->trans(
"TurnoverCollected");
172 $calcmode = $langs->trans(
"CalcModeEngagement");
177 $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>" :
"");
178 $description = $langs->trans(
"RulesCAIn");
179 $description .= $langs->trans(
"DepositsAreIncluded");
182 } elseif ($modecompta ==
"BOOKKEEPING") {
183 $name = $langs->trans(
"Turnover");
184 $calcmode = $langs->trans(
"CalcModeBookkeeping");
185 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInDueDebtMode",
'{link1}',
'{link2}').
')';
186 $calcmode = str_replace(
'{link1}',
'<a class="bold" href="'.$_SERVER[
"PHP_SELF"].
'?'.($param ? $param :
'year_start='.$year_start).
'&modecompta=CREANCES-DETTES">', $calcmode);
187 $calcmode = str_replace(
'{link2}',
'</a>', $calcmode);
189 $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>" :
"");
190 $description = $langs->trans(
"RulesSalesTurnoverOfIncomeAccounts");
194 $period =
$form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'',
'',
'tzserver');
196 $period .=
$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'',
'',
'tzserver');
198 $moreparam = array();
199 if (!empty($modecompta)) {
200 $moreparam[
'modecompta'] = $modecompta;
202 report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam, $calcmode);
204 if (
isModEnabled(
'accounting') && $modecompta !=
'BOOKKEEPING') {
205 print
info_admin($langs->trans(
"WarningReportNotReliable"), 0, 0, 1);
209 if ($modecompta ==
'CREANCES-DETTES') {
210 $sql =
"SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_ht) as amount, sum(f.total_ttc) as amount_ttc";
211 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
212 $sql .=
" WHERE f.fk_statut in (1,2)";
213 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
214 $sql .=
" AND f.type IN (0,1,2,5)";
216 $sql .=
" AND f.type IN (0,1,2,3,5)";
218 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
220 $sql .=
" AND f.fk_soc = ".((int) $socid);
222 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
227 $sql =
"SELECT date_format(p.datep, '%Y-%m') as dm, sum(pf.amount) as amount_ttc";
228 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
229 $sql .=
", ".MAIN_DB_PREFIX.
"paiement_facture as pf";
230 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
231 $sql .=
" WHERE p.rowid = pf.fk_paiement";
232 $sql .=
" AND pf.fk_facture = f.rowid";
233 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
235 $sql .=
" AND f.fk_soc = ".((int) $socid);
237 } elseif ($modecompta ==
"BOOKKEEPING") {
238 $pcgverid = $conf->global->CHARTOFACCOUNTS;
239 $pcgvercode =
dol_getIdFromCode($db, $pcgverid,
'accounting_system',
'rowid',
'pcg_version');
240 if (empty($pcgvercode)) {
241 $pcgvercode = $pcgverid;
244 $sql =
"SELECT date_format(b.doc_date, '%Y-%m') as dm, sum(b.credit - b.debit) as amount_ttc";
245 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as b,";
246 $sql .=
" ".MAIN_DB_PREFIX.
"accounting_account as aa";
247 $sql .=
" WHERE b.entity = ".$conf->entity;
248 $sql .=
" AND b.numero_compte = aa.account_number";
249 $sql .=
" AND b.doc_type = 'customer_invoice'";
250 $sql .=
" AND aa.entity = ".$conf->entity;
251 $sql .=
" AND aa.fk_pcg_version = '".$db->escape($pcgvercode).
"'";
252 $sql .=
" AND aa.pcg_type = 'INCOME'";
254 $sql .=
" GROUP BY dm";
255 $sql .=
" ORDER BY dm";
259 $minyearmonth = $maxyearmonth = 0;
266 $result = $db->query($sql);
268 $num = $db->num_rows($result);
271 $obj = $db->fetch_object($result);
272 $cum_ht[$obj->dm] = empty($obj->amount) ? 0 : $obj->amount;
273 $cum[$obj->dm] = empty($obj->amount_ttc) ? 0 : $obj->amount_ttc;
274 if ($obj->amount_ttc) {
275 $minyearmonth = ($minyearmonth ? min($minyearmonth, $obj->dm) : $obj->dm);
276 $maxyearmonth = max($maxyearmonth, $obj->dm);
286 if ($modecompta ==
'RECETTES-DEPENSES') {
287 $sql =
"SELECT date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ttc";
288 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
289 $sql .=
", ".MAIN_DB_PREFIX.
"bank_account as ba";
290 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
291 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON p.rowid = pf.fk_paiement";
292 $sql .=
" WHERE pf.rowid IS NULL";
293 $sql .=
" AND p.fk_bank = b.rowid";
294 $sql .=
" AND b.fk_account = ba.rowid";
295 $sql .=
" AND ba.entity IN (".getEntity(
'bank_account').
")";
296 $sql .=
" GROUP BY dm";
297 $sql .=
" ORDER BY dm";
299 $result = $db->query($sql);
301 $num = $db->num_rows($result);
304 $obj = $db->fetch_object($result);
305 if (empty($cum[$obj->dm])) {
306 $cum[$obj->dm] = $obj->amount_ttc;
308 $cum[$obj->dm] += $obj->amount_ttc;
310 if ($obj->amount_ttc) {
311 $minyearmonth = ($minyearmonth ?min($minyearmonth, $obj->dm) : $obj->dm);
312 $maxyearmonth = max($maxyearmonth, $obj->dm);
323 print
'<div class="div-table-responsive">';
324 print
'<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
326 print
'<tr class="liste_titre"><td> </td>';
328 for ($annee = $year_start; $annee <= $year_end; $annee++) {
329 if ($modecompta ==
'CREANCES-DETTES') {
330 print
'<td align="center" width="10%" colspan="3">';
332 print
'<td align="center" width="10%" colspan="2" class="borderrightlight">';
334 if ($modecompta !=
'BOOKKEEPING') {
335 print
'<a href="casoc.php?year='.$annee.
'">';
338 if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1) {
339 print
'-'.($annee + 1);
341 if ($modecompta !=
'BOOKKEEPING') {
345 if ($annee != $year_end) {
346 print
'<td width="15"> </td>';
351 print
'<tr class="liste_titre"><td class="liste_titre">'.$langs->trans(
"Month").
'</td>';
352 for ($annee = $year_start; $annee <= $year_end; $annee++) {
353 if ($modecompta ==
'CREANCES-DETTES') {
354 print
'<td class="liste_titre right">'.$langs->trans(
"AmountHT").
'</td>';
356 print
'<td class="liste_titre right">';
357 if ($modecompta ==
"BOOKKEEPING") {
358 print $langs->trans(
"Amount");
360 print $langs->trans(
"AmountTTC");
363 print
'<td class="liste_titre right borderrightlight">'.$langs->trans(
"Delta").
'</td>';
364 if ($annee != $year_end) {
365 print
'<td class="liste_titre" width="15"> </td>';
371 $minyear = substr($minyearmonth, 0, 4);
372 $maxyear = substr($maxyearmonth, 0, 4);
373 $nowyear = strftime(
"%Y",
dol_now());
374 $nowyearmonth = strftime(
"%Y-%m",
dol_now());
375 $maxyearmonth = max($maxyearmonth, $nowyearmonth);
380 $nb_mois_decalage =
GETPOSTISSET(
'date_startmonth') ? (
GETPOST(
'date_startmonth',
'int') - 1) : (empty($conf->global->SOCIETE_FISCAL_MONTH_START) ? 0 : ($conf->global->SOCIETE_FISCAL_MONTH_START - 1));
381 for ($mois = 1 + $nb_mois_decalage; $mois <= 12 + $nb_mois_decalage; $mois++) {
382 $mois_modulo = $mois;
384 $mois_modulo = $mois - 12;
387 if ($year_start == $year_end) {
389 if ($mois < $date_startmonth && $year_start <= $date_startyear) {
393 if ($mois > $date_endmonth && $year_end >= $date_endyear) {
398 print
'<tr class="oddeven">';
401 print
"<td>".dol_print_date(
dol_mktime(12, 0, 0, $mois_modulo, 1, 2000),
"%B").
"</td>";
403 for ($annee = $year_start - 1; $annee <= $year_end; $annee++) {
404 $annee_decalage = $annee;
406 $annee_decalage = $annee + 1;
411 if ($annee >= $year_start) {
412 if ($modecompta ==
'CREANCES-DETTES') {
414 print
'<td class="right">';
415 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
416 if ($cum_ht[$case]) {
418 print
'<a href="casoc.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">'.
price($cum_ht[$case], 1).
'</a>';
420 if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
431 print
'<td class="right">';
432 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
435 if ($modecompta !=
'BOOKKEEPING') {
436 print
'<a href="casoc.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">';
438 print
price($cum[$case], 1);
439 if ($modecompta !=
'BOOKKEEPING') {
443 if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
453 print
'<td class="borderrightlight right"><span class="opacitymedium">';
455 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
456 if ($annee_decalage > $minyear && $case <= $casenow) {
457 if ($cum[$caseprev] && $cum[$case]) {
458 $percent = (round(($cum[$case] - $cum[$caseprev]) / $cum[$caseprev], 4) * 100);
460 print ($percent >= 0 ?
"+$percent" :
"$percent").
'%';
462 if ($cum[$caseprev] && !$cum[$case]) {
465 if (!$cum[$caseprev] && $cum[$case]) {
469 if (isset($cum[$caseprev]) && !$cum[$caseprev] && !$cum[$case]) {
472 if (!isset($cum[$caseprev]) && !$cum[$case]) {
476 if ($minyearmonth <= $case && $case <= $maxyearmonth) {
483 print
'</span></td>';
485 if ($annee_decalage < $year_end || ($annee_decalage == $year_end && $mois > 12 && $annee < $year_end)) {
486 print
'<td width="15"> </td>';
490 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
491 $total_ht[$annee] += ((!empty($cum_ht[$case])) ? $cum_ht[$case] : 0);
492 $total[$annee] += $cum[$case];
565 print
'<tr class="liste_total"><td>'.$langs->trans(
"Total").
'</td>';
566 for ($annee = $year_start; $annee <= $year_end; $annee++) {
567 if ($modecompta ==
'CREANCES-DETTES') {
569 if ($total_ht[$annee] || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
570 print
'<td class="nowrap right">';
571 print ($total_ht[$annee] ?
price($total_ht[$annee]) :
"0");
574 print
'<td> </td>';
579 if ($total[$annee] || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
580 print
'<td class="nowrap right">';
581 print ($total[$annee] ?
price($total[$annee]) :
"0");
584 print
'<td> </td>';
588 if ($annee > $minyear && $annee <= max($nowyear, $maxyear)) {
589 if ($total[$annee - 1] && $total[$annee]) {
590 $percent = (round(($total[$annee] - $total[$annee - 1]) / $total[$annee - 1], 4) * 100);
591 print
'<td class="nowrap borderrightlight right">';
592 print ($percent >= 0 ?
"+$percent" :
"$percent").
'%';
595 if ($total[$annee - 1] && !$total[$annee]) {
596 print
'<td class="borderrightlight right">-100%</td>';
598 if (!$total[$annee - 1] && $total[$annee]) {
599 print
'<td class="borderrightlight right">+'.$langs->trans(
'Inf').
'%</td>';
601 if (!$total[$annee - 1] && !$total[$annee]) {
602 print
'<td class="borderrightlight right">+0%</td>';
605 print
'<td class="borderrightlight right">';
606 if ($total[$annee] || ($minyear <= $annee && $annee <= max($nowyear, $maxyear))) {
614 if ($annee != $year_end) {
615 print
'<td width="15"> </td>';