28 require
'../../main.inc.php';
29 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
30 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
33 $langs->loadLangs(array(
'compta',
'bills',
'donation',
'salaries'));
35 $date_startday =
GETPOST(
'date_startday',
'int');
36 $date_startmonth =
GETPOST(
'date_startmonth',
'int');
37 $date_startyear =
GETPOST(
'date_startyear',
'int');
38 $date_endday =
GETPOST(
'date_endday',
'int');
39 $date_endmonth =
GETPOST(
'date_endmonth',
'int');
40 $date_endyear =
GETPOST(
'date_endyear',
'int');
49 $year_start = $year_current - ($nbofyear - 1);
51 $year_current = $year;
53 $year_start = $year - $nbofyear + (
getDolGlobalInt(
'SOCIETE_FISCAL_MONTH_START') > 1 ? 0 : 1);
55 $date_start =
dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear,
'tzserver');
56 $date_end =
dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear,
'tzserver');
59 if (empty($date_start) || empty($date_end)) {
63 $year_end = $year_start + $nbofyear - (
getDolGlobalInt(
'SOCIETE_FISCAL_MONTH_START') > 1 ? 0 : 1);
66 if (!$year && $month_start > $month_current) {
70 $month_end = $month_start - 1;
75 $month_end = $month_start;
99 $mothn_start = $tmps[
'mon'];
100 $year_start = $tmps[
'year'];
102 $month_end = $tmpe[
'mon'];
103 $year_end = $tmpe[
'year'];
104 $nbofyear = ($year_end - $year_start) + 1;
107 $modecompta = $conf->global->ACCOUNTING_MODE;
109 $modecompta =
'BOOKKEEPING';
111 if (
GETPOST(
"modecompta",
'alpha')) {
112 $modecompta =
GETPOST(
"modecompta",
'alpha');
115 $userid =
GETPOST(
'userid',
'int');
118 $socid =
GETPOST(
'socid',
'int');
119 if ($user->socid > 0) {
120 $socid = $user->socid;
126 $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
137 if ($date_startday && $date_startmonth && $date_startyear) {
138 $param .=
'&date_startday='.$date_startday.
'&date_startmonth='.$date_startmonth.
'&date_startyear='.$date_startyear;
140 if ($date_endday && $date_endmonth && $date_endyear) {
141 $param .=
'&date_endday='.$date_endday.
'&date_endmonth='.$date_endmonth.
'&date_endyear='.$date_endyear;
153 if ($modecompta ==
"CREANCES-DETTES") {
154 $name = $langs->trans(
"Turnover");
155 $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>" :
"");
156 $description = $langs->trans(
"RulesCADue");
157 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
158 $description .= $langs->trans(
"DepositsAreNotIncluded");
160 $description .= $langs->trans(
"DepositsAreIncluded");
163 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
164 $name = $langs->trans(
"TurnoverCollected");
165 $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>" :
"");
166 $description = $langs->trans(
"RulesCAIn");
167 $description .= $langs->trans(
"DepositsAreIncluded");
169 } elseif ($modecompta ==
"BOOKKEEPING") {
170 $name = $langs->trans(
"Turnover");
171 $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>" :
"");
172 $description = $langs->trans(
"RulesSalesTurnoverOfIncomeAccounts");
175 $period =
$form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'',
'',
'tzserver');
177 $period .=
$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'',
'',
'tzserver');
179 $moreparam = array();
180 if (!empty($modecompta)) {
181 $moreparam[
'modecompta'] = $modecompta;
186 if ($modecompta ==
"RECETTES-DEPENSES" || $modecompta ==
"BOOKKEEINGCOLLECTED") {
191 $calcmode .=
'<input type="radio" name="modecompta" id="modecompta2" value="RECETTES-DEPENSES"'.($modecompta ==
'RECETTES-DEPENSES' ?
' checked="checked"' :
'').
'><label for="modecompta2"> '.$langs->trans(
"CalcModeEngagement");
193 $calcmode .=
' <span class="opacitymedium hideonsmartphone">('.$langs->trans(
"CalcModeNoBookKeeping").
')</span>';
195 $calcmode .=
'</label>';
198 $calcmode .=
'<input type="radio" name="modecompta" id="modecompta3" value="BOOKKEEPING"'.($modecompta ==
'BOOKKEEPING' ?
' checked="checked"' :
'').
'><label for="modecompta3"> '.$langs->trans(
"CalcModeBookkeeping").
'</label>';
201 $calcmode .=
'<input type="radio" name="modecompta" id="modecompta2" value="CREANCES-DETTES"'.($modecompta ==
'CREANCES-DETTES' ?
' checked="checked"' :
'').
'><label for="modecompta2"> '.$langs->trans(
"CalcModeDebt");
203 $calcmode .=
' <span class="opacitymedium hideonsmartphone">('.$langs->trans(
"CalcModeNoBookKeeping").
')</span>';
205 $calcmode .=
'</label>';
208 report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam, $calcmode);
210 if (
isModEnabled(
'accounting') && $modecompta !=
'BOOKKEEPING') {
211 print
info_admin($langs->trans(
"WarningReportNotReliable"), 0, 0, 1);
215 if ($modecompta ==
'CREANCES-DETTES') {
216 $sql =
"SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_ht) as amount, sum(f.total_ttc) as amount_ttc";
217 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
218 $sql .=
" WHERE f.fk_statut in (1,2)";
219 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
220 $sql .=
" AND f.type IN (0,1,2,5)";
222 $sql .=
" AND f.type IN (0,1,2,3,5)";
224 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
226 $sql .=
" AND f.fk_soc = ".((int) $socid);
228 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
233 $sql =
"SELECT date_format(p.datep, '%Y-%m') as dm, sum(pf.amount) as amount_ttc";
234 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
235 $sql .=
", ".MAIN_DB_PREFIX.
"paiement_facture as pf";
236 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
237 $sql .=
" WHERE p.rowid = pf.fk_paiement";
238 $sql .=
" AND pf.fk_facture = f.rowid";
239 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
241 $sql .=
" AND f.fk_soc = ".((int) $socid);
243 } elseif ($modecompta ==
"BOOKKEEPING") {
245 $pcgvercode =
dol_getIdFromCode($db, $pcgverid,
'accounting_system',
'rowid',
'pcg_version');
246 if (empty($pcgvercode)) {
247 $pcgvercode = $pcgverid;
250 $sql =
"SELECT date_format(b.doc_date, '%Y-%m') as dm, sum(b.credit - b.debit) as amount_ttc";
251 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as b,";
252 $sql .=
" ".MAIN_DB_PREFIX.
"accounting_account as aa";
253 $sql .=
" WHERE b.entity = ".$conf->entity;
254 $sql .=
" AND b.numero_compte = aa.account_number";
255 $sql .=
" AND b.doc_type = 'customer_invoice'";
256 $sql .=
" AND aa.entity = ".$conf->entity;
257 $sql .=
" AND aa.fk_pcg_version = '".$db->escape($pcgvercode).
"'";
258 $sql .=
" AND aa.pcg_type = 'INCOME'";
260 $sql .=
" GROUP BY dm";
261 $sql .=
" ORDER BY dm";
265 $minyearmonth = $maxyearmonth = 0;
272 $result = $db->query(
$sql);
274 $num = $db->num_rows($result);
277 $obj = $db->fetch_object($result);
278 $cum_ht[$obj->dm] = empty($obj->amount) ? 0 : $obj->amount;
279 $cum[$obj->dm] = empty($obj->amount_ttc) ? 0 : $obj->amount_ttc;
280 if ($obj->amount_ttc) {
281 $minyearmonth = ($minyearmonth ? min($minyearmonth, $obj->dm) : $obj->dm);
282 $maxyearmonth = max($maxyearmonth, $obj->dm);
292 if ($modecompta ==
'RECETTES-DEPENSES') {
293 $sql =
"SELECT date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount_ttc";
294 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
295 $sql .=
", ".MAIN_DB_PREFIX.
"bank_account as ba";
296 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
297 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON p.rowid = pf.fk_paiement";
298 $sql .=
" WHERE pf.rowid IS NULL";
299 $sql .=
" AND p.fk_bank = b.rowid";
300 $sql .=
" AND b.fk_account = ba.rowid";
301 $sql .=
" AND ba.entity IN (".getEntity(
'bank_account').
")";
302 $sql .=
" GROUP BY dm";
303 $sql .=
" ORDER BY dm";
305 $result = $db->query(
$sql);
307 $num = $db->num_rows($result);
310 $obj = $db->fetch_object($result);
311 if (empty($cum[$obj->dm])) {
312 $cum[$obj->dm] = $obj->amount_ttc;
314 $cum[$obj->dm] += $obj->amount_ttc;
316 if ($obj->amount_ttc) {
317 $minyearmonth = ($minyearmonth ?min($minyearmonth, $obj->dm) : $obj->dm);
318 $maxyearmonth = max($maxyearmonth, $obj->dm);
329 print
'<div class="div-table-responsive">';
330 print
'<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
332 print
'<tr class="liste_titre"><td> </td>';
334 for ($annee = $year_start; $annee <= $year_end; $annee++) {
335 if ($modecompta ==
'CREANCES-DETTES') {
336 print
'<td align="center" width="10%" colspan="3">';
338 print
'<td align="center" width="10%" colspan="2" class="borderrightlight">';
340 if ($modecompta !=
'BOOKKEEPING') {
341 print
'<a href="casoc.php?year='.$annee.
'">';
344 if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1) {
345 print
'-'.($annee + 1);
347 if ($modecompta !=
'BOOKKEEPING') {
351 if ($annee != $year_end) {
352 print
'<td width="15"> </td>';
357 print
'<tr class="liste_titre"><td class="liste_titre">'.$langs->trans(
"Month").
'</td>';
358 for ($annee = $year_start; $annee <= $year_end; $annee++) {
359 if ($modecompta ==
'CREANCES-DETTES') {
360 print
'<td class="liste_titre right">'.$langs->trans(
"AmountHT").
'</td>';
362 print
'<td class="liste_titre right">';
363 if ($modecompta ==
"BOOKKEEPING") {
364 print $langs->trans(
"Amount");
366 print $langs->trans(
"AmountTTC");
369 print
'<td class="liste_titre right borderrightlight">'.$langs->trans(
"Delta").
'</td>';
370 if ($annee != $year_end) {
371 print
'<td class="liste_titre" width="15"> </td>';
377 $minyear = substr($minyearmonth, 0, 4);
378 $maxyear = substr($maxyearmonth, 0, 4);
380 $nowyearmonth = strftime(
"%Y-%m",
dol_now());
381 $maxyearmonth = max($maxyearmonth, $nowyearmonth);
386 $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));
387 for ($mois = 1 + $nb_mois_decalage; $mois <= 12 + $nb_mois_decalage; $mois++) {
388 $mois_modulo = $mois;
390 $mois_modulo = $mois - 12;
393 if ($year_start == $year_end) {
395 if ($mois < $date_startmonth && $year_start <= $date_startyear) {
399 if ($mois > $date_endmonth && $year_end >= $date_endyear) {
404 print
'<tr class="oddeven">';
407 print
"<td>".dol_print_date(
dol_mktime(12, 0, 0, $mois_modulo, 1, 2000),
"%B").
"</td>";
409 for ($annee = $year_start - 1; $annee <= $year_end; $annee++) {
410 $annee_decalage = $annee;
412 $annee_decalage = $annee + 1;
417 if ($annee >= $year_start) {
418 if ($modecompta ==
'CREANCES-DETTES') {
420 print
'<td class="right">';
421 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
422 if ($cum_ht[$case]) {
424 print
'<a href="casoc.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">'.
price($cum_ht[$case], 1).
'</a>';
426 if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
437 print
'<td class="right">';
438 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
439 if (!empty($cum[$case])) {
441 if ($modecompta !=
'BOOKKEEPING') {
442 print
'<a href="casoc.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">';
444 print
price($cum[$case], 1);
445 if ($modecompta !=
'BOOKKEEPING') {
449 if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
459 print
'<td class="borderrightlight right"><span class="opacitymedium">';
461 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
462 if ($annee_decalage > $minyear && $case <= $casenow) {
463 if (!empty($cum[$caseprev]) && !empty($cum[$case])) {
464 $percent = (round(($cum[$case] - $cum[$caseprev]) / $cum[$caseprev], 4) * 100);
466 print ($percent >= 0 ?
"+$percent" :
"$percent").
'%';
468 if (!empty($cum[$caseprev]) && empty($cum[$case])) {
471 if (empty($cum[$caseprev]) && !empty($cum[$case])) {
475 if (isset($cum[$caseprev]) && empty($cum[$caseprev]) && empty($cum[$case])) {
478 if (!isset($cum[$caseprev]) && empty($cum[$case])) {
482 if ($minyearmonth <= $case && $case <= $maxyearmonth) {
489 print
'</span></td>';
491 if ($annee_decalage < $year_end || ($annee_decalage == $year_end && $mois > 12 && $annee < $year_end)) {
492 print
'<td width="15"> </td>';
496 if ($annee < $year_end || ($annee == $year_end && $mois <= $month_end)) {
497 if (empty($total_ht[$annee])) {
498 $total_ht[$annee] = (empty($cum_ht[$case]) ? 0 : $cum_ht[$case]);
500 $total_ht[$annee] += (empty($cum_ht[$case]) ? 0 : $cum_ht[$case]);
502 if (empty($total[$annee])) {
503 $total[$annee] = empty($cum[$case]) ? 0 : $cum[$case];
505 $total[$annee] += empty($cum[$case]) ? 0 : $cum[$case];
579 print
'<tr class="liste_total"><td>'.$langs->trans(
"Total").
'</td>';
580 for ($annee = $year_start; $annee <= $year_end; $annee++) {
581 if ($modecompta ==
'CREANCES-DETTES') {
583 if ($total_ht[$annee] || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
584 print
'<td class="nowrap right">';
585 print (empty($total_ht[$annee]) ?
'0' :
price($total_ht[$annee]));
588 print
'<td> </td>';
593 if (!empty($total[$annee]) || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
594 print
'<td class="nowrap right">';
595 print (empty($total[$annee]) ?
'0' :
price($total[$annee]));
598 print
'<td> </td>';
602 if ($annee > $minyear && $annee <= max($nowyear, $maxyear)) {
603 if (!empty($total[$annee - 1]) && !empty($total[$annee])) {
604 $percent = (round(($total[$annee] - $total[$annee - 1]) / $total[$annee - 1], 4) * 100);
605 print
'<td class="nowrap borderrightlight right">';
606 print ($percent >= 0 ?
"+$percent" :
"$percent").
'%';
609 if (!empty($total[$annee - 1]) && empty($total[$annee])) {
610 print
'<td class="borderrightlight right">-100%</td>';
612 if (empty($total[$annee - 1]) && !empty($total[$annee])) {
613 print
'<td class="borderrightlight right">+'.$langs->trans(
'Inf').
'%</td>';
615 if (empty($total[$annee - 1]) && empty($total[$annee])) {
616 print
'<td class="borderrightlight right">+0%</td>';
619 print
'<td class="borderrightlight right">';
620 if (!empty($total[$annee]) || ($minyear <= $annee && $annee <= max($nowyear, $maxyear))) {
628 if ($annee != $year_end) {
629 print
'<td width="15"> </td>';