31 require
'../../main.inc.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
36 $langs->loadLangs(array(
'compta',
'bills',
'donation',
'salaries'));
38 $date_startmonth =
GETPOST(
'date_startmonth',
'int');
39 $date_startday =
GETPOST(
'date_startday',
'int');
40 $date_startyear =
GETPOST(
'date_startyear',
'int');
41 $date_endmonth =
GETPOST(
'date_endmonth',
'int');
42 $date_endday =
GETPOST(
'date_endday',
'int');
43 $date_endyear =
GETPOST(
'date_endyear',
'int');
51 $year_current = strftime(
"%Y",
dol_now());
52 $month_current = strftime(
"%m",
dol_now());
53 $year_start = $year_current - ($nbofyear - 1);
55 $year_current = $year;
56 $month_current = strftime(
"%m",
dol_now());
57 $year_start = $year - ($nbofyear - 1);
59 $date_start =
dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
60 $date_end =
dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
63 if (empty($date_start) || empty($date_end))
69 $year_end = $year_start + ($nbofyear - 1);
70 $month_start =
GETPOST(
"month") ?
GETPOST(
"month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
73 if (!
GETPOST(
"year") && $month_start > $month_current)
78 $month_end = $month_start - 1;
79 if ($month_end < 1) $month_end = 12;
81 }
else $month_end = $month_start;
92 $year_start = $tmps[
'year'];
94 $year_end = $tmpe[
'year'];
95 $nbofyear = ($year_end - $year_start) + 1;
100 $socid =
GETPOST(
'socid',
'int');
101 if ($user->socid > 0) $socid = $user->socid;
102 if (!empty($conf->comptabilite->enabled)) $result =
restrictedArea($user,
'compta',
'',
'',
'resultat');
103 if (!empty($conf->accounting->enabled)) $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
106 $modecompta = $conf->global->ACCOUNTING_MODE;
107 if (!empty($conf->accounting->enabled)) $modecompta =
'BOOKKEEPING';
108 if (
GETPOST(
"modecompta",
'alpha')) $modecompta =
GETPOST(
"modecompta",
'alpha');
117 $form =
new Form($db);
122 if ($modecompta ==
'CREANCES-DETTES')
124 $name = $langs->trans(
"ReportInOut").
', '.$langs->trans(
"ByYear");
125 $calcmode = $langs->trans(
"CalcModeDebt");
126 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInInputOutputMode",
'{s1}',
'{s2}').
')';
127 $calcmode = str_replace(array(
'{s1}',
'{s2}'), array(
'<a href="'.
$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=RECETTES-DEPENSES">',
'</a>'), $calcmode);
128 if (!empty($conf->accounting->enabled)) {
129 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInBookkeepingMode",
'{s1}',
'{s2}').
')';
130 $calcmode = str_replace(array(
'{s1}',
'{s2}'), array(
'<a href="'.
$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=BOOKKEEPING">',
'</a>'), $calcmode);
132 $period = $form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0).
' - '.$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0);
133 $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>" :
"");
134 $description = $langs->trans(
"RulesAmountWithTaxIncluded");
135 $description .=
'<br>'.$langs->trans(
"RulesResultDue");
136 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description .=
"<br>".$langs->trans(
"DepositsAreNotIncluded");
137 else $description .=
"<br>".$langs->trans(
"DepositsAreIncluded");
140 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
141 $name = $langs->trans(
"ReportInOut").
', '.$langs->trans(
"ByYear");
142 $calcmode = $langs->trans(
"CalcModeEngagement");
143 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInDueDebtMode",
'{s1}',
'{s2}').
')';
144 $calcmode = str_replace(array(
'{s1}',
'{s2}'), array(
'<a href="'.
$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=CREANCES-DETTES">',
'</a>'), $calcmode);
145 if (!empty($conf->accounting->enabled)) {
146 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInBookkeepingMode",
'{s1}',
'{s2}').
')';
147 $calcmode = str_replace(array(
'{s1}',
'{s2}'), array(
'<a href="'.
$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=BOOKKEEPING">',
'</a>'), $calcmode);
149 $period = $form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0).
' - '.$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0);
150 $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>" :
"");
151 $description = $langs->trans(
"RulesAmountWithTaxIncluded");
152 $description .=
'<br>'.$langs->trans(
"RulesResultInOut");
155 } elseif ($modecompta ==
"BOOKKEEPING")
157 $name = $langs->trans(
"ReportInOut").
', '.$langs->trans(
"ByYear");
158 $calcmode = $langs->trans(
"CalcModeBookkeeping");
159 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInInputOutputMode",
'{s1}',
'{s2}').
')';
160 $calcmode = str_replace(array(
'{s1}',
'{s2}'), array(
'<a href="'.
$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=RECETTES-DEPENSES">',
'</a>'), $calcmode);
161 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInDueDebtMode",
'{s1}',
'{s2}').
')';
162 $calcmode = str_replace(array(
'{s1}',
'{s2}'), array(
'<a href="'.
$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=CREANCES-DETTES">',
'</a>'), $calcmode);
163 $period = $form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0).
' - '.$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0);
164 $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>" :
"");
165 $description = $langs->trans(
"RulesAmountOnInOutBookkeepingRecord");
166 $description .=
' ('.$langs->trans(
"SeePageForSetup", DOL_URL_ROOT.
'/accountancy/admin/account.php?mainmenu=accountancy&leftmenu=accountancy_admin', $langs->transnoentitiesnoconv(
"Accountancy").
' / '.$langs->transnoentitiesnoconv(
"Setup").
' / '.$langs->trans(
"Chartofaccounts")).
')';
171 $hselected =
'report';
173 report_header($name,
'', $period, $periodlink, $description, $builddate, $exportlink, array(
'modecompta'=>$modecompta), $calcmode);
175 if (!empty($conf->accounting->enabled) && $modecompta !=
'BOOKKEEPING')
188 if (!empty($conf->facture->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
190 if ($modecompta ==
'CREANCES-DETTES')
192 $sql =
"SELECT sum(f.total) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
193 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s";
194 $sql .=
", ".MAIN_DB_PREFIX.
"facture as f";
195 $sql .=
" WHERE f.fk_soc = s.rowid";
196 $sql .=
" AND f.fk_statut IN (1,2)";
197 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .=
" AND f.type IN (0,1,2,5)";
198 else $sql .=
" AND f.type IN (0,1,2,3,5)";
199 if (!empty($date_start) && !empty($date_end))
200 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
201 } elseif ($modecompta ==
"RECETTES-DEPENSES")
207 $sql =
"SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
208 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
209 $sql .=
", ".MAIN_DB_PREFIX.
"paiement_facture as pf";
210 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
211 $sql .=
" WHERE p.rowid = pf.fk_paiement";
212 $sql .=
" AND pf.fk_facture = f.rowid";
213 if (!empty($date_start) && !empty($date_end))
214 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
217 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
218 if ($socid) $sql .=
" AND f.fk_soc = $socid";
219 $sql .=
" GROUP BY dm";
220 $sql .=
" ORDER BY dm";
223 dol_syslog(
"get customers invoices", LOG_DEBUG);
224 $result = $db->query($sql);
227 $num = $db->num_rows($result);
231 $row = $db->fetch_object($result);
232 $encaiss[$row->dm] = (isset($row->amount_ht) ? $row->amount_ht : 0);
233 $encaiss_ttc[$row->dm] = $row->amount_ttc;
240 } elseif ($modecompta ==
"BOOKKEEPING")
245 if (!empty($conf->facture->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
248 if ($modecompta !=
'CREANCES-DETTES')
250 $sql =
"SELECT sum(p.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
251 $sql .=
" FROM ".MAIN_DB_PREFIX.
"bank as b";
252 $sql .=
", ".MAIN_DB_PREFIX.
"bank_account as ba";
253 $sql .=
", ".MAIN_DB_PREFIX.
"paiement as p";
254 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON p.rowid = pf.fk_paiement";
255 $sql .=
" WHERE pf.rowid IS NULL";
256 $sql .=
" AND p.fk_bank = b.rowid";
257 $sql .=
" AND b.fk_account = ba.rowid";
258 $sql .=
" AND ba.entity IN (".getEntity(
'bank_account').
")";
259 if (!empty($date_start) && !empty($date_end))
260 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
261 $sql .=
" GROUP BY dm";
262 $sql .=
" ORDER BY dm";
264 dol_syslog(
"get old customers payments not linked to invoices", LOG_DEBUG);
265 $result = $db->query($sql);
267 $num = $db->num_rows($result);
271 $row = $db->fetch_object($result);
273 if (!isset($encaiss[$row->dm])) $encaiss[$row->dm] = 0;
274 $encaiss[$row->dm] += (isset($row->amount_ht) ? $row->amount_ht : 0);
276 if (!isset($encaiss_ttc[$row->dm])) $encaiss_ttc[$row->dm] = 0;
277 $encaiss_ttc[$row->dm] += $row->amount_ttc;
284 } elseif ($modecompta ==
"RECETTES-DEPENSES")
288 } elseif ($modecompta ==
"BOOKKEEPING")
300 if (!empty($conf->facture->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
302 if ($modecompta ==
'CREANCES-DETTES')
304 $sql =
"SELECT sum(f.total_ht) as amount_ht, sum(f.total_ttc) as amount_ttc, date_format(f.datef,'%Y-%m') as dm";
305 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
306 $sql .=
" WHERE f.fk_statut IN (1,2)";
307 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .=
" AND f.type IN (0,1,2)";
308 else $sql .=
" AND f.type IN (0,1,2,3)";
309 if (!empty($date_start) && !empty($date_end))
310 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
311 } elseif ($modecompta ==
"RECETTES-DEPENSES")
313 $sql =
"SELECT sum(pf.amount) as amount_ttc, date_format(p.datep,'%Y-%m') as dm";
314 $sql .=
" FROM ".MAIN_DB_PREFIX.
"paiementfourn as p";
315 $sql .=
", ".MAIN_DB_PREFIX.
"facture_fourn as f";
316 $sql .=
", ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf";
317 $sql .=
" WHERE f.rowid = pf.fk_facturefourn";
318 $sql .=
" AND p.rowid = pf.fk_paiementfourn";
319 if (!empty($date_start) && !empty($date_end))
320 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
323 $sql .=
" AND f.entity = ".$conf->entity;
324 if ($socid) $sql .=
" AND f.fk_soc = ".$socid;
325 $sql .=
" GROUP BY dm";
327 dol_syslog(
"get suppliers invoices", LOG_DEBUG);
328 $result = $db->query($sql);
331 $num = $db->num_rows($result);
335 $row = $db->fetch_object($result);
337 if (!isset($decaiss[$row->dm])) $decaiss[$row->dm] = 0;
338 $decaiss[$row->dm] = (isset($row->amount_ht) ? $row->amount_ht : 0);
340 if (!isset($decaiss_ttc[$row->dm])) $decaiss_ttc[$row->dm] = 0;
341 $decaiss_ttc[$row->dm] = $row->amount_ttc;
349 } elseif ($modecompta ==
"BOOKKEEPING")
362 if (!empty($conf->tax->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
364 if ($modecompta ==
'CREANCES-DETTES')
367 $sql =
"SELECT sum(f.tva) as amount, date_format(f.datef,'%Y-%m') as dm";
368 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
369 $sql .=
" WHERE f.fk_statut IN (1,2)";
370 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .=
" AND f.type IN (0,1,2,5)";
371 else $sql .=
" AND f.type IN (0,1,2,3,5)";
372 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
373 if (!empty($date_start) && !empty($date_end))
374 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
375 $sql .=
" GROUP BY dm";
378 $result = $db->query($sql);
380 $num = $db->num_rows($result);
384 $obj = $db->fetch_object($result);
386 if (!isset($decaiss[$obj->dm])) $decaiss[$obj->dm] = 0;
387 $decaiss[$obj->dm] += $obj->amount;
389 if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;
390 $decaiss_ttc[$obj->dm] += $obj->amount;
399 $sql =
"SELECT sum(f.total_tva) as amount, date_format(f.datef,'%Y-%m') as dm";
400 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
401 $sql .=
" WHERE f.fk_statut IN (1,2)";
402 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql .=
" AND f.type IN (0,1,2)";
403 else $sql .=
" AND f.type IN (0,1,2,3)";
404 $sql .=
" AND f.entity = ".$conf->entity;
405 if (!empty($date_start) && !empty($date_end))
406 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
407 $sql .=
" GROUP BY dm";
409 dol_syslog(
"get vat to receive back", LOG_DEBUG);
410 $result = $db->query($sql);
412 $num = $db->num_rows($result);
416 $obj = $db->fetch_object($result);
418 if (!isset($encaiss[$obj->dm])) $encaiss[$obj->dm] = 0;
419 $encaiss[$obj->dm] += $obj->amount;
421 if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;
422 $encaiss_ttc[$obj->dm] += $obj->amount;
430 } elseif ($modecompta ==
"RECETTES-DEPENSES")
433 $sql =
"SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
434 $sql .=
" FROM ".MAIN_DB_PREFIX.
"tva as t";
435 $sql .=
" WHERE amount > 0";
436 $sql .=
" AND t.entity = ".$conf->entity;
437 if (!empty($date_start) && !empty($date_end))
438 $sql .=
" AND t.datev >= '".$db->idate($date_start).
"' AND t.datev <= '".$db->idate($date_end).
"'";
439 $sql .=
" GROUP BY dm";
442 $result = $db->query($sql);
444 $num = $db->num_rows($result);
448 $obj = $db->fetch_object($result);
450 if (!isset($decaiss[$obj->dm])) $decaiss[$obj->dm] = 0;
451 $decaiss[$obj->dm] += $obj->amount;
453 if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;
454 $decaiss_ttc[$obj->dm] += $obj->amount;
463 $sql =
"SELECT sum(t.amount) as amount, date_format(t.datev,'%Y-%m') as dm";
464 $sql .=
" FROM ".MAIN_DB_PREFIX.
"tva as t";
465 $sql .=
" WHERE amount < 0";
466 $sql .=
" AND t.entity = ".$conf->entity;
467 if (!empty($date_start) && !empty($date_end))
468 $sql .=
" AND t.datev >= '".$db->idate($date_start).
"' AND t.datev <= '".$db->idate($date_end).
"'";
469 $sql .=
" GROUP BY dm";
471 dol_syslog(
"get vat really received back", LOG_DEBUG);
472 $result = $db->query($sql);
474 $num = $db->num_rows($result);
478 $obj = $db->fetch_object($result);
480 if (!isset($encaiss[$obj->dm])) $encaiss[$obj->dm] = 0;
481 $encaiss[$obj->dm] += -$obj->amount;
483 if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;
484 $encaiss_ttc[$obj->dm] += -$obj->amount;
493 } elseif ($modecompta ==
"BOOKKEEPING")
504 if (!empty($conf->tax->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
506 if ($modecompta ==
'CREANCES-DETTES')
508 $sql =
"SELECT c.libelle as nom, date_format(cs.date_ech,'%Y-%m') as dm, sum(cs.amount) as amount";
509 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_chargesociales as c";
510 $sql .=
", ".MAIN_DB_PREFIX.
"chargesociales as cs";
511 $sql .=
" WHERE cs.fk_type = c.id";
512 if (!empty($date_start) && !empty($date_end))
513 $sql .=
" AND cs.date_ech >= '".$db->idate($date_start).
"' AND cs.date_ech <= '".$db->idate($date_end).
"'";
514 } elseif ($modecompta ==
"RECETTES-DEPENSES")
516 $sql =
"SELECT c.libelle as nom, date_format(p.datep,'%Y-%m') as dm, sum(p.amount) as amount";
517 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_chargesociales as c";
518 $sql .=
", ".MAIN_DB_PREFIX.
"chargesociales as cs";
519 $sql .=
", ".MAIN_DB_PREFIX.
"paiementcharge as p";
520 $sql .=
" WHERE p.fk_charge = cs.rowid";
521 $sql .=
" AND cs.fk_type = c.id";
522 if (!empty($date_start) && !empty($date_end))
523 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
526 $sql .=
" AND cs.entity = ".$conf->entity;
527 $sql .=
" GROUP BY c.libelle, dm";
529 dol_syslog(
"get social contributions", LOG_DEBUG);
530 $result = $db->query($sql);
532 $num = $db->num_rows($result);
536 $obj = $db->fetch_object($result);
538 if (!isset($decaiss[$obj->dm])) $decaiss[$obj->dm] = 0;
539 $decaiss[$obj->dm] += $obj->amount;
541 if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;
542 $decaiss_ttc[$obj->dm] += $obj->amount;
550 } elseif ($modecompta ==
"BOOKKEEPING")
560 if (!empty($conf->salaries->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
562 if ($modecompta ==
'CREANCES-DETTES') $column =
'p.datev';
563 if ($modecompta ==
"RECETTES-DEPENSES") $column =
'p.datep';
567 $sql =
"SELECT p.label as nom, date_format(".$column.
",'%Y-%m') as dm, sum(p.amount) as amount";
568 $sql .=
" FROM ".MAIN_DB_PREFIX.
"payment_salary as p";
569 $sql .=
" WHERE p.entity IN (".getEntity(
'payment_salary').
")";
570 if (!empty($date_start) && !empty($date_end))
571 $sql .=
" AND ".$column.
" >= '".$db->idate($date_start).
"' AND ".$column.
" <= '".$db->idate($date_end).
"'";
572 $sql .=
" GROUP BY p.label, dm";
575 $result = $db->query($sql);
577 $num = $db->num_rows($result);
581 $obj = $db->fetch_object($result);
583 if (!isset($decaiss[$obj->dm]))
584 $decaiss[$obj->dm] = 0;
585 $decaiss[$obj->dm] += $obj->amount;
587 if (!isset($decaiss_ttc[$obj->dm]))
588 $decaiss_ttc[$obj->dm] = 0;
589 $decaiss_ttc[$obj->dm] += $obj->amount;
597 } elseif ($modecompta ==
"BOOKKEEPING")
607 if (!empty($conf->expensereport->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
609 $langs->load(
'trips');
611 if ($modecompta ==
'CREANCES-DETTES') {
612 $sql =
"SELECT date_format(date_valid,'%Y-%m') as dm, sum(p.total_ht) as amount_ht,sum(p.total_ttc) as amount_ttc";
613 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport as p";
614 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user as u ON u.rowid=p.fk_user_author";
615 $sql .=
" WHERE p.entity IN (".getEntity(
'expensereport').
")";
616 $sql .=
" AND p.fk_statut>=5";
618 $column =
'p.date_valid';
619 if (!empty($date_start) && !empty($date_end))
620 $sql .=
" AND ".$column.
" >= '".$db->idate($date_start).
"' AND ".$column.
" <= '".$db->idate($date_end).
"'";
621 } elseif ($modecompta ==
'RECETTES-DEPENSES') {
622 $sql =
"SELECT date_format(pe.datep,'%Y-%m') as dm, sum(p.total_ht) as amount_ht,sum(p.total_ttc) as amount_ttc";
623 $sql .=
" FROM ".MAIN_DB_PREFIX.
"expensereport as p";
624 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"user as u ON u.rowid=p.fk_user_author";
625 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"payment_expensereport as pe ON pe.fk_expensereport = p.rowid";
626 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as c ON pe.fk_typepayment = c.id";
627 $sql .=
" WHERE p.entity IN (".getEntity(
'expensereport').
")";
628 $sql .=
" AND p.fk_statut>=5";
630 $column =
'pe.datep';
631 if (!empty($date_start) && !empty($date_end))
632 $sql .=
" AND ".$column.
" >= '".$db->idate($date_start).
"' AND ".$column.
" <= '".$db->idate($date_end).
"'";
635 $sql .=
" GROUP BY dm";
638 $result = $db->query($sql);
643 $num = $db->num_rows($result);
646 while ($obj = $db->fetch_object($result))
648 if (!isset($decaiss[$obj->dm])) $decaiss[$obj->dm] = 0;
649 $decaiss[$obj->dm] += $obj->amount_ht;
651 if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;
652 $decaiss_ttc[$obj->dm] += $obj->amount_ttc;
658 } elseif ($modecompta ==
'BOOKKEEPING') {
667 if (!empty($conf->don->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
672 if ($modecompta ==
'CREANCES-DETTES') {
673 $sql =
"SELECT p.societe as nom, p.firstname, p.lastname, date_format(p.datedon,'%Y-%m') as dm, sum(p.amount) as amount";
674 $sql .=
" FROM ".MAIN_DB_PREFIX.
"don as p";
675 $sql .=
" WHERE p.entity IN (".getEntity(
'donation').
")";
676 $sql .=
" AND fk_statut in (1,2)";
677 if (!empty($date_start) && !empty($date_end))
678 $sql .=
" AND p.datedon >= '".$db->idate($date_start).
"' AND p.datedon <= '".$db->idate($date_end).
"'";
679 } elseif ($modecompta ==
'RECETTES-DEPENSES') {
680 $sql =
"SELECT p.societe as nom, p.firstname, p.lastname, date_format(pe.datep,'%Y-%m') as dm, sum(p.amount) as amount";
681 $sql .=
" FROM ".MAIN_DB_PREFIX.
"don as p";
682 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"payment_donation as pe ON pe.fk_donation = p.rowid";
683 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_paiement as c ON pe.fk_typepayment = c.id";
684 $sql .=
" WHERE p.entity IN (".getEntity(
'donation').
")";
685 $sql .=
" AND fk_statut >= 2";
686 if (!empty($date_start) && !empty($date_end)) {
687 $sql .=
" AND pe.datep >= '".$db->idate($date_start).
"' AND pe.datep <= '".$db->idate($date_end).
"'";
691 $sql .=
" GROUP BY p.societe, p.firstname, p.lastname, dm";
694 $result = $db->query($sql);
697 $num = $db->num_rows($result);
703 $obj = $db->fetch_object($result);
705 if (!isset($encaiss[$obj->dm])) $encaiss[$obj->dm] = 0;
706 $encaiss[$obj->dm] += $obj->amount;
708 if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;
709 $encaiss_ttc[$obj->dm] += $obj->amount;
717 } elseif ($modecompta ==
'BOOKKEEPING') {
725 if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_VARPAY) && !empty($conf->banque->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
729 $sql =
"SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX.
"payment_various as p";
730 $sql .=
' WHERE p.sens = 0';
731 if (!empty($date_start) && !empty($date_end))
732 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
733 $sql .=
' GROUP BY dm';
736 $result = $db->query($sql);
739 $num = $db->num_rows($result);
745 $obj = $db->fetch_object($result);
746 if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;
747 if (isset($obj->amount)) $decaiss_ttc[$obj->dm] += $obj->amount;
757 $sql =
"SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount) AS amount FROM ".MAIN_DB_PREFIX.
"payment_various AS p";
758 $sql .=
' WHERE p.sens = 1';
759 if (!empty($date_start) && !empty($date_end))
760 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
761 $sql .=
' GROUP BY dm';
764 $result = $db->query($sql);
767 $num = $db->num_rows($result);
773 $obj = $db->fetch_object($result);
774 if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;
775 if (isset($obj->amount)) $encaiss_ttc[$obj->dm] += $obj->amount;
791 if (!empty($conf->global->ACCOUNTING_REPORTS_INCLUDE_LOAN) && !empty($conf->loan->enabled) && ($modecompta ==
'CREANCES-DETTES' || $modecompta ==
"RECETTES-DEPENSES"))
793 $sql =
"SELECT date_format(p.datep, '%Y-%m') AS dm, SUM(p.amount_capital + p.amount_insurance + p.amount_interest) AS amount FROM ".MAIN_DB_PREFIX.
"payment_loan AS p";
794 $sql .=
' WHERE 1 = 1';
795 if (!empty($date_start) && !empty($date_end))
796 $sql .=
" AND p.datep >= '".$db->idate($date_start).
"' AND p.datep <= '".$db->idate($date_end).
"'";
797 $sql .=
' GROUP BY dm';
800 $result = $db->query($sql);
803 $num = $db->num_rows($result);
809 $obj = $db->fetch_object($result);
810 if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;
811 if (isset($obj->amount)) $decaiss_ttc[$obj->dm] += $obj->amount;
828 if (!empty($conf->accounting->enabled) && ($modecompta ==
'BOOKKEEPING'))
830 $predefinedgroupwhere =
"(";
831 $predefinedgroupwhere .=
" (aa.pcg_type = 'EXPENSE')";
832 $predefinedgroupwhere .=
" OR ";
833 $predefinedgroupwhere .=
" (aa.pcg_type = 'INCOME')";
834 $predefinedgroupwhere .=
")";
836 $charofaccountstring = $conf->global->CHARTOFACCOUNTS;
837 $charofaccountstring =
dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS,
'accounting_system',
'rowid',
'pcg_version');
839 $sql =
"SELECT b.doc_ref, b.numero_compte, b.subledger_account, b.subledger_label, aa.pcg_type, date_format(b.doc_date,'%Y-%m') as dm, sum(b.debit) as debit, sum(b.credit) as credit, sum(b.montant) as amount";
840 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as b, ".MAIN_DB_PREFIX.
"accounting_account as aa";
841 $sql .=
" WHERE b.entity = ".$conf->entity;
842 $sql .=
" AND aa.entity = ".$conf->entity;
843 $sql .=
" AND b.numero_compte = aa.account_number";
844 $sql .=
" AND ".$predefinedgroupwhere;
845 $sql .=
" AND fk_pcg_version = '".$db->escape($charofaccountstring).
"'";
846 if (!empty($date_start) && !empty($date_end))
847 $sql .=
" AND b.doc_date >= '".$db->idate($date_start).
"' AND b.doc_date <= '".$db->idate($date_end).
"'";
848 $sql .=
" GROUP BY b.doc_ref, b.numero_compte, b.subledger_account, b.subledger_label, pcg_type, dm";
855 $result = $db->query($sql);
858 $num = $db->num_rows($result);
864 $obj = $db->fetch_object($result);
866 if ($obj->pcg_type ==
'INCOME') {
867 if (!isset($encaiss[$obj->dm])) $encaiss[$obj->dm] = 0;
868 $encaiss[$obj->dm] += $obj->credit;
869 $encaiss[$obj->dm] -= $obj->debit;
871 if ($obj->pcg_type ==
'EXPENSE') {
872 if (!isset($decaiss[$obj->dm])) $decaiss[$obj->dm] = 0;
873 $decaiss[$obj->dm] += $obj->debit;
874 $decaiss[$obj->dm] -= $obj->credit;
878 if (!isset($encaiss_ttc[$obj->dm])) $encaiss_ttc[$obj->dm] = 0;
879 if (!isset($decaiss_ttc[$obj->dm])) $decaiss_ttc[$obj->dm] = 0;
880 $encaiss_ttc[$obj->dm] += 0;
881 $decaiss_ttc[$obj->dm] += 0;
894 $object = array(&$encaiss, &$encaiss_ttc, &$decaiss, &$decaiss_ttc);
895 $parameters[
"mode"] = $modecompta;
897 $hookmanager->initHooks(array(
'externalbalance'));
898 $reshook = $hookmanager->executeHooks(
'addReportInfo', $parameters, $object, $action);
906 $totentrees = array();
907 $totsorties = array();
909 print '<div class="div-table-responsive">';
910 print '<table class="tagtable liste">'.
"\n";
912 print '<tr class="liste_titre"><td class="liste_titre"> </td>';
914 for ($annee = $year_start; $annee <= $year_end; $annee++)
916 print '<td align="center" colspan="2" class="liste_titre borderrightlight">';
917 print '<a href="clientfourn.php?year='.$annee.
'">';
919 if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1)
print '-'.($annee + 1);
923 print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans(
"Month").
'</td>';
925 for ($annee = $year_start; $annee <= $year_end; $annee++)
927 print '<td class="liste_titre" align="center">';
930 print $form->textwithpicto($langs->trans(
"Outcome"), $htmlhelp);
932 print '<td class="liste_titre" align="center" class="borderrightlight">';
935 print $form->textwithpicto($langs->trans(
"Income"), $htmlhelp);
942 $nb_mois_decalage = $conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START - 1) : 0;
943 for ($mois = 1 + $nb_mois_decalage; $mois <= 12 + $nb_mois_decalage; $mois++)
945 $mois_modulo = $mois;
946 if ($mois > 12) {$mois_modulo = $mois - 12; }
948 print '<tr class="oddeven">';
949 print "<td>".dol_print_date(
dol_mktime(12, 0, 0, $mois_modulo, 1, $annee),
"%B").
"</td>";
950 for ($annee = $year_start; $annee <= $year_end; $annee++)
952 $annee_decalage = $annee;
953 if ($mois > 12) {$annee_decalage = $annee + 1; }
954 $case = strftime(
"%Y-%m",
dol_mktime(12, 0, 0, $mois_modulo, 1, $annee_decalage));
956 print '<td class="right"> ';
957 if ($modecompta ==
'BOOKKEEPING')
959 if (isset($decaiss[$case]) && $decaiss[$case] != 0)
961 print '<a href="clientfourn.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">'.
price(
price2num($decaiss[$case],
'MT')).
'</a>';
962 if (!isset($totsorties[$annee])) $totsorties[$annee] = 0;
963 $totsorties[$annee] += $decaiss[$case];
966 if (isset($decaiss_ttc[$case]) && $decaiss_ttc[$case] != 0)
968 print '<a href="clientfourn.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">'.
price(
price2num($decaiss_ttc[$case],
'MT')).
'</a>';
969 if (!isset($totsorties[$annee])) $totsorties[$annee] = 0;
970 $totsorties[$annee] += $decaiss_ttc[$case];
975 print '<td class="borderrightlight nowrap right"> ';
976 if ($modecompta ==
'BOOKKEEPING')
978 if (isset($encaiss[$case]))
980 print '<a href="clientfourn.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">'.
price(
price2num($encaiss[$case],
'MT')).
'</a>';
981 if (!isset($totentrees[$annee])) $totentrees[$annee] = 0;
982 $totentrees[$annee] += $encaiss[$case];
985 if (isset($encaiss_ttc[$case]))
987 print '<a href="clientfourn.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">'.
price(
price2num($encaiss_ttc[$case],
'MT')).
'</a>';
988 if (!isset($totentrees[$annee])) $totentrees[$annee] = 0;
989 $totentrees[$annee] += $encaiss_ttc[$case];
1001 print '<tr class="liste_total impair"><td>';
1002 if ($modecompta ==
'BOOKKEEPING')
print $langs->trans(
"Total");
1003 else print $langs->trans(
"TotalTTC");
1005 for ($annee = $year_start; $annee <= $year_end; $annee++)
1008 print '<td class="nowrap right">'.(isset($totsorties[$annee]) ?
price(
price2num($totsorties[$annee],
'MT')) :
' ').
'</td>';
1009 print '<td class="nowrap right" style="border-right: 1px solid #DDD">'.(isset($totentrees[$annee]) ?
price(
price2num($totentrees[$annee],
'MT')) :
' ').
'</td>';
1014 print '<tr class="impair"><td> </td>';
1015 print '<td colspan="'.$nbcols.
'"> </td>';
1020 print '<tr class="liste_total"><td>'.$langs->trans(
"AccountingResult").
'</td>';
1021 for ($annee = $year_start; $annee <= $year_end; $annee++)
1023 print '<td colspan="2" class="borderrightlight right"> ';
1024 if (isset($totentrees[$annee]) || isset($totsorties[$annee]))
1026 $in = (isset($totentrees[$annee]) ?
price2num($totentrees[$annee],
'MT') : 0);
1027 $out = (isset($totsorties[$annee]) ?
price2num($totsorties[$annee],
'MT') : 0);
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
dol_now($mode= 'auto')
Return date for now.
report_header($reportname, $notused, $period, $periodlink, $description, $builddate, $exportlink= '', $moreparam=array(), $calcmode= '', $varlink= '')
Show header of a report.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
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...
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is '...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone= '')
Return an array with locale date info.
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
print $_SERVER["PHP_SELF"]
Edit parameters.
img_next($titlealt= 'default', $moreatt= '')
Show next logo.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
print
Draft customers invoices.
img_previous($titlealt= 'default', $moreatt= '')
Show previous logo.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
if(!defined('CSRFCHECK_WITH_TOKEN')) define('CSRFCHECK_WITH_TOKEN'
Draft customers invoices.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin= '1', $morecss= '', $textfordropdown= '')
Show information for admin users or standard users.