23 require
'../../main.inc.php';
24 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
25 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
28 $langs->loadLangs(array(
'compta',
'bills'));
30 $date_startday =
GETPOST(
'date_startday',
'int');
31 $date_startmonth =
GETPOST(
'date_startmonth',
'int');
32 $date_startyear =
GETPOST(
'date_startyear',
'int');
33 $date_endday =
GETPOST(
'date_endday',
'int');
34 $date_endmonth =
GETPOST(
'date_endmonth',
'int');
35 $date_endyear =
GETPOST(
'date_endyear',
'int');
44 $year_start = $year_current - ($nbofyear - 1);
46 $year_current = $year;
48 $year_start = $year - ($nbofyear - 1);
50 $date_start =
dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear,
'tzserver');
51 $date_end =
dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear,
'tzserver');
54 if (empty($date_start) || empty($date_end)) {
58 $year_end = $year_start + ($nbofyear - 1);
59 $month_start =
GETPOSTISSET(
"month") ?
GETPOST(
"month",
'int') : ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1);
61 if (!
GETPOST(
"year") && $month_start > $month_current) {
65 $month_end = $month_start - 1;
72 $month_end = $month_start;
95 $userid =
GETPOST(
'userid',
'int');
96 $socid =
GETPOST(
'socid',
'int');
99 $year_start = $tmps[
'year'];
101 $year_end = $tmpe[
'year'];
102 $nbofyear = ($year_end - $year_start) + 1;
105 $modecompta = $conf->global->ACCOUNTING_MODE;
107 $modecompta =
'BOOKKEEPING';
110 $modecompta =
GETPOST(
"modecompta",
'alpha');
114 if ($user->socid > 0) {
115 $socid = $user->socid;
121 $result =
restrictedArea($user,
'accounting',
'',
'',
'comptarapport');
134 if ($modecompta ==
"BOOKKEEPING") {
135 $modecompta =
"CREANCES-DETTES";
137 if ($modecompta ==
"BOOKKEEPINGCOLLECTED") {
138 $modecompta =
"RECETTES-DEPENSES";
142 if ($modecompta ==
"CREANCES-DETTES") {
143 $name = $langs->trans(
"PurchaseTurnover");
144 $calcmode = $langs->trans(
"CalcModeDebt");
146 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInBookkeepingMode",
'{link1}',
'{link2}').
')';
147 $calcmode = str_replace(
'{link1}',
'<a href="'.$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=BOOKKEEPING">', $calcmode);
148 $calcmode = str_replace(
'{link2}',
'</a>', $calcmode);
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(
"RulesPurchaseTurnoverDue");
153 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
154 $name = $langs->trans(
"PurchaseTurnoverCollected");
155 $calcmode = $langs->trans(
"CalcModeEngagement");
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(
"RulesPurchaseTurnoverIn");
159 } elseif ($modecompta ==
"BOOKKEEPING") {
160 $name = $langs->trans(
"PurchaseTurnover");
161 $calcmode = $langs->trans(
"CalcModeBookkeeping");
162 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInDueDebtMode",
'{link1}',
'{link2}').
')';
163 $calcmode = str_replace(
'{link1}',
'<a href="'.$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=CREANCES-DETTES">', $calcmode);
164 $calcmode = str_replace(
'{link2}',
'</a>', $calcmode);
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(
"RulesPurchaseTurnoverOfExpenseAccounts");
168 } elseif ($modecompta ==
"BOOKKEEPINGCOLLECTED") {
169 $name = $langs->trans(
"PurchaseTurnoverCollected");
170 $calcmode = $langs->trans(
"CalcModeBookkeeping");
171 $calcmode .=
'<br>('.$langs->trans(
"SeeReportInDueDebtMode",
'{link1}',
'{link2}').
')';
172 $calcmode = str_replace(
'{link1}',
'<a href="'.$_SERVER[
"PHP_SELF"].
'?year_start='.$year_start.
'&modecompta=RECETTES-DEPENSES">', $calcmode);
173 $calcmode = str_replace(
'{link2}',
'</a>', $calcmode);
174 $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>" :
"");
175 $description = $langs->trans(
"RulesPurchaseTurnoverCollectedOfExpenseAccounts");
180 $period =
$form->selectDate($date_start,
'date_start', 0, 0, 0,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'',
'',
'tzserver');
182 $period .=
$form->selectDate($date_end,
'date_end', 0, 0, 0,
'', 1, 0, 0,
'',
'',
'',
'', 1,
'',
'',
'tzserver');
184 $moreparam = array();
185 if (!empty($modecompta)) {
186 $moreparam[
'modecompta'] = $modecompta;
188 report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam, $calcmode);
190 if (
isModEnabled(
'accounting') && $modecompta !=
'BOOKKEEPING') {
191 print
info_admin($langs->trans(
"WarningReportNotReliable"), 0, 0, 1);
195 if ($modecompta ==
'CREANCES-DETTES') {
196 $sql =
"SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_ht) as amount, sum(f.total_ttc) as amount_ttc";
197 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
198 $sql .=
" WHERE f.fk_statut in (1,2)";
199 $sql .=
" AND f.type IN (0,2)";
200 $sql .=
" AND f.entity IN (".getEntity(
'supplier_invoice').
")";
202 $sql .=
" AND f.fk_soc = ".((int) $socid);
204 } elseif ($modecompta ==
"RECETTES-DEPENSES") {
205 $sql =
"SELECT date_format(p.datep,'%Y-%m') as dm, sum(pf.amount) as amount_ttc";
206 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn as f";
207 $sql .=
", ".MAIN_DB_PREFIX.
"paiementfourn_facturefourn as pf";
208 $sql .=
", ".MAIN_DB_PREFIX.
"paiementfourn as p";
209 $sql .=
" WHERE p.rowid = pf.fk_paiementfourn";
210 $sql .=
" AND pf.fk_facturefourn = f.rowid";
211 $sql .=
" AND f.entity IN (".getEntity(
'supplier_invoice').
")";
213 $sql .=
" AND f.fk_soc = ".((int) $socid);
215 } elseif ($modecompta ==
"BOOKKEEPING") {
216 $pcgverid = $conf->global->CHARTOFACCOUNTS;
217 $pcgvercode =
dol_getIdFromCode($db, $pcgverid,
'accounting_system',
'rowid',
'pcg_version');
218 if (empty($pcgvercode)) {
219 $pcgvercode = $pcgverid;
222 $sql =
"SELECT date_format(b.doc_date, '%Y-%m') as dm, sum(b.debit - b.credit) as amount_ttc";
223 $sql .=
" FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as b,";
224 $sql .=
" ".MAIN_DB_PREFIX.
"accounting_account as aa";
225 $sql .=
" WHERE b.entity = ".$conf->entity;
226 $sql .=
" AND b.doc_type = 'supplier_invoice'";
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 .=
" AND aa.pcg_type = 'EXPENSE'";
234 $sql .=
" GROUP BY dm";
235 $sql .=
" ORDER BY dm";
239 $minyearmonth = $maxyearmonth = 0;
241 $result = $db->query($sql);
243 $num = $db->num_rows($result);
246 $obj = $db->fetch_object($result);
247 $cum_ht[$obj->dm] = !empty($obj->amount) ? $obj->amount : 0;
248 $cum[$obj->dm] = $obj->amount_ttc;
249 if ($obj->amount_ttc) {
250 $minyearmonth = ($minyearmonth ? min($minyearmonth, $obj->dm) : $obj->dm);
251 $maxyearmonth = max($maxyearmonth, $obj->dm);
262 print
'<div class="div-table-responsive">';
263 print
'<table class="tagtable liste'.($moreforfilter ?
" listwithfilterbefore" :
"").
'">'.
"\n";
265 print
'<tr class="liste_titre"><td> </td>';
267 for ($annee = $year_start; $annee <= $year_end; $annee++) {
268 if ($modecompta ==
'CREANCES-DETTES') {
269 print
'<td align="center" width="10%" colspan="3">';
271 print
'<td align="center" width="10%" colspan="2" class="borderrightlight">';
273 if ($modecompta !=
'BOOKKEEPING') {
274 print
'<a href="supplier_turnover_by_thirdparty.php?year='.$annee.($modecompta ?
'&modecompta='.$modecompta :
'').
'">';
277 if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1) {
278 print
'-'.($annee + 1);
280 if ($modecompta !=
'BOOKKEEPING') {
284 if ($annee != $year_end) {
285 print
'<td width="15"> </td>';
290 print
'<tr class="liste_titre"><td class="liste_titre">'.$langs->trans(
"Month").
'</td>';
291 for ($annee = $year_start; $annee <= $year_end; $annee++) {
292 if ($modecompta ==
'CREANCES-DETTES') {
293 print
'<td class="liste_titre right">'.$langs->trans(
"AmountHT").
'</td>';
295 print
'<td class="liste_titre right">';
296 if ($modecompta ==
"BOOKKEEPING") {
297 print $langs->trans(
"Amount");
299 print $langs->trans(
"AmountTTC");
302 print
'<td class="liste_titre right borderrightlight">'.$langs->trans(
"Delta").
'</td>';
303 if ($annee != $year_end) {
304 print
'<td class="liste_titre" width="15"> </td>';
310 $minyear = substr($minyearmonth, 0, 4);
311 $maxyear = substr($maxyearmonth, 0, 4);
312 $nowyear = strftime(
"%Y",
dol_now());
313 $nowyearmonth = strftime(
"%Y-%m",
dol_now());
314 $maxyearmonth = max($maxyearmonth, $nowyearmonth);
319 $nb_mois_decalage = $conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START - 1) : 0;
320 for ($mois = 1 + $nb_mois_decalage; $mois <= 12 + $nb_mois_decalage; $mois++) {
321 $mois_modulo = $mois;
323 $mois_modulo = $mois - 12;
326 if ($year_start == $year_end) {
327 if ($mois > $date_endmonth && $year_end >= $date_endyear) {
332 print
'<tr class="oddeven">';
335 print
"<td>".dol_print_date(
dol_mktime(12, 0, 0, $mois_modulo, 1, 2000),
"%B").
"</td>";
337 for ($annee = $year_start - 1; $annee <= $year_end; $annee++) {
338 $annee_decalage = $annee;
340 $annee_decalage = $annee + 1;
345 if ($annee >= $year_start) {
346 if ($modecompta ==
'CREANCES-DETTES') {
348 print
'<td class="right">';
349 if ($cum_ht[$case]) {
351 print
'<a href="supplier_turnover_by_thirdparty.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">'.
price($cum_ht[$case], 1).
'</a>';
353 if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
363 print
'<td class="right">';
366 if ($modecompta !=
'BOOKKEEPING') {
367 print
'<a href="supplier_turnover_by_thirdparty.php?year='.$annee_decalage.
'&month='.$mois_modulo.($modecompta ?
'&modecompta='.$modecompta :
'').
'">';
369 print
price($cum[$case], 1);
370 if ($modecompta !=
'BOOKKEEPING') {
374 if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) {
383 if ($annee_decalage > $minyear && $case <= $casenow) {
384 if ($cum[$caseprev] && $cum[$case]) {
385 $percent = (round(($cum[$case] - $cum[$caseprev]) / $cum[$caseprev], 4) * 100);
387 print
'<td class="borderrightlight right">'.($percent >= 0 ?
"+$percent" :
"$percent").
'%</td>';
389 if ($cum[$caseprev] && !$cum[$case]) {
390 print
'<td class="borderrightlight right">-100%</td>';
392 if (!$cum[$caseprev] && $cum[$case]) {
394 print
'<td class="borderrightlight right">-</td>';
396 if (isset($cum[$caseprev]) && !$cum[$caseprev] && !$cum[$case]) {
397 print
'<td class="borderrightlight right">+0%</td>';
399 if (!isset($cum[$caseprev]) && !$cum[$case]) {
400 print
'<td class="borderrightlight right">-</td>';
403 print
'<td class="borderrightlight right">';
404 if ($minyearmonth <= $case && $case <= $maxyearmonth) {
412 if ($annee_decalage < $year_end || ($annee_decalage == $year_end && $mois > 12 && $annee < $year_end)) {
413 print
'<td width="15"> </td>';
417 $total_ht[$annee] += ((!empty($cum_ht[$case])) ? $cum_ht[$case] : 0);
418 $total[$annee] += $cum[$case];
425 print
'<tr class="liste_total"><td>'.$langs->trans(
"Total").
'</td>';
426 for ($annee = $year_start; $annee <= $year_end; $annee++) {
427 if ($modecompta ==
'CREANCES-DETTES') {
429 if ($total_ht[$annee] || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
430 print
'<td class="nowrap right">'.($total_ht[$annee] ?
price($total_ht[$annee]) :
"0").
"</td>";
432 print
'<td> </td>';
437 if ($total[$annee] || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) {
438 print
'<td class="nowrap right">'.($total[$annee] ?
price($total[$annee]) :
"0").
"</td>";
440 print
'<td> </td>';
444 if ($annee > $minyear && $annee <= max($nowyear, $maxyear)) {
445 if ($total[$annee - 1] && $total[$annee]) {
446 $percent = (round(($total[$annee] - $total[$annee - 1]) / $total[$annee - 1], 4) * 100);
447 print
'<td class="nowrap borderrightlight right">'.($percent >= 0 ?
"+$percent" :
"$percent").
'%</td>';
449 if ($total[$annee - 1] && !$total[$annee]) {
450 print
'<td class="borderrightlight right">-100%</td>';
452 if (!$total[$annee - 1] && $total[$annee]) {
453 print
'<td class="borderrightlight right">+'.$langs->trans(
'Inf').
'%</td>';
455 if (!$total[$annee - 1] && !$total[$annee]) {
456 print
'<td class="borderrightlight right">+0%</td>';
459 print
'<td class="borderrightlight right">';
460 if ($total[$annee] || ($minyear <= $annee && $annee <= max($nowyear, $maxyear))) {
468 if ($annee != $year_end) {
469 print
'<td width="15"> </td>';