dolibarr 21.0.0-beta
annuel.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2013-2023 Charlene BENKE <charlene@patas-monkey.com>
6 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
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
29// Load Dolibarr environment
30require '../../main.inc.php';
31require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
32require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
34
43// Load translation files required by the page
44$langs->loadLangs(array('banks', 'categories'));
45
46$WIDTH = DolGraph::getDefaultGraphSizeForStats('width', '380'); // Large for one graph in a smarpthone.
47$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height', '160');
48
49$id = GETPOST('account') ? GETPOST('account', 'alpha') : GETPOST('id');
50$ref = GETPOST('ref');
51
52// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
53$hookmanager->initHooks(array('bankannualreport', 'globalcard'));
54
55// Security check
56$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
57$fieldtype = (!empty($ref) ? 'ref' : 'rowid');
58if ($user->socid) {
59 $socid = $user->socid;
60}
61$result = restrictedArea($user, 'banque', $fieldvalue, 'bank_account&bank_account', '', '', $fieldtype);
62
63$year_start = GETPOST('year_start');
64//$year_current = strftime("%Y", time());
65$year_current = (int) dol_print_date(time(), "%Y");
66if (!$year_start) {
67 $year_start = $year_current - 2;
68 $year_end = $year_current;
69} else {
70 $year_end = $year_start + 2;
71}
72
73
74/*
75 * View
76 */
77$error = 0;
78
79$form = new Form($db);
80
81// Get account information
82$object = new Account($db);
83if ($id > 0 && !preg_match('/,/', $id)) { // if for a particular account and not a list
84 $result = $object->fetch($id);
85 $id = $object->id;
86}
87if (!empty($ref)) {
88 $result = $object->fetch(0, $ref);
89 $id = $object->id;
90}
91
92$annee = '';
93$totentrees = array();
94$totsorties = array();
95
96$title = $object->ref.' - '.$langs->trans("IOMonthlyReporting");
97$helpurl = "";
98llxHeader('', $title, $helpurl);
99
100// Ce rapport de tresorerie est base sur llx_bank (car doit inclure les transactions sans facture)
101// plutot que sur llx_paiement + llx_paiementfourn
102
103$sql = "SELECT SUM(b.amount)";
104$sql .= ", date_format(b.dateo,'%Y-%m') as dm";
105$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
106$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
107$sql .= " WHERE b.fk_account = ba.rowid";
108$sql .= " AND ba.entity IN (".getEntity('bank_account').")";
109$sql .= " AND b.amount >= 0";
110if (!empty($id)) {
111 $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
112}
113$sql .= " GROUP BY dm";
114
115$resql = $db->query($sql);
116$encaiss = array();
117$decaiss = array();
118if ($resql) {
119 $num = $db->num_rows($resql);
120 $i = 0;
121 while ($i < $num) {
122 $row = $db->fetch_row($resql);
123 $encaiss[$row[1]] = $row[0];
124 $i++;
125 }
126} else {
127 dol_print_error($db);
128}
129
130$sql = "SELECT SUM(b.amount)";
131$sql .= ", date_format(b.dateo,'%Y-%m') as dm";
132$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
133$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
134$sql .= " WHERE b.fk_account = ba.rowid";
135$sql .= " AND ba.entity IN (".getEntity('bank_account').")";
136$sql .= " AND b.amount <= 0";
137if (!empty($id)) {
138 $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
139}
140$sql .= " GROUP BY dm";
141
142$resql = $db->query($sql);
143if ($resql) {
144 $num = $db->num_rows($resql);
145 $i = 0;
146 while ($i < $num) {
147 $row = $db->fetch_row($resql);
148 $decaiss[$row[1]] = -$row[0];
149 $i++;
150 }
151} else {
152 dol_print_error($db);
153}
154
155
156// Onglets
157$head = bank_prepare_head($object);
158print dol_get_fiche_head($head, 'annual', $langs->trans("FinancialAccount"), 0, 'account');
159
160$title = $langs->trans("FinancialAccount")." : ".$object->label;
161$link = ($year_start ? '<a href="'.$_SERVER["PHP_SELF"].'?account='.$object->id.'&year_start='.($year_start - 1).'">'.img_previous('', 'class="valignbottom"')."</a> ".$langs->trans("Year").' <a href="'.$_SERVER["PHP_SELF"].'?account='.$object->id.'&year_start='.($year_start + 1).'">'.img_next('', 'class="valignbottom"').'</a>' : '');
162
163$linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
164
165$morehtmlref = '';
166
167if (!empty($id)) {
168 if (!preg_match('/,/', $id)) {
169 dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
170 } else {
171 $bankaccount = new Account($db);
172 $listid = explode(',', $id);
173 foreach ($listid as $key => $aId) {
174 $bankaccount->fetch($aId);
175 $bankaccount->label = $bankaccount->ref;
176 print $bankaccount->getNomUrl(1);
177 if ($key < (count($listid) - 1)) {
178 print ', ';
179 }
180 }
181 }
182} else {
183 print $langs->trans("AllAccounts");
184}
185
186print dol_get_fiche_end();
187
188
189// Affiche tableau
190print load_fiche_titre('', $link, '');
191
192print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you don't need reserved height for your table
193print '<table class="noborder centpercent">';
194
195print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Month").'</td>';
196for ($annee = $year_start; $annee <= $year_end; $annee++) {
197 print '<td align="center" width="20%" colspan="2" class="liste_titre borderrightlight">'.$annee.'</td>';
198}
199print '</tr>';
200
201print '<tr class="liste_titre">';
202print '<td class="liste_titre">&nbsp;</td>';
203for ($annee = $year_start; $annee <= $year_end; $annee++) {
204 print '<td class="liste_titre" align="center">'.$langs->trans("Debit").'</td><td class="liste_titre" align="center">'.$langs->trans("Credit").'</td>';
205}
206print '</tr>';
207
208for ($annee = $year_start; $annee <= $year_end; $annee++) {
209 $totsorties[$annee] = 0;
210 $totentrees[$annee] = 0;
211}
212
213for ($mois = 1; $mois < 13; $mois++) {
214 print '<tr class="oddeven">';
215 print "<td>".dol_print_date(dol_mktime(1, 1, 1, $mois, 1, 2000), "%B")."</td>";
216
217 for ($annee = $year_start; $annee <= $year_end; $annee++) {
218 $case = sprintf("%04d-%02d", $annee, $mois);
219
220 print '<td class="right" width="10%">&nbsp;';
221 if (isset($decaiss[$case]) && $decaiss[$case] > 0) {
222 print price($decaiss[$case]);
223 $totsorties[$annee] += $decaiss[$case];
224 }
225 print "</td>";
226
227 print '<td class="right borderrightlight" width="10%">&nbsp;';
228 if (isset($encaiss[$case]) && $encaiss[$case] > 0) {
229 print price($encaiss[$case]);
230 $totentrees[$annee] += $encaiss[$case];
231 }
232 print "</td>";
233 }
234 print '</tr>';
235}
236
237// Total debit-credit
238print '<tr class="liste_total"><td><b>'.$langs->trans("Total")."</b></td>";
239for ($annee = $year_start; $annee <= $year_end; $annee++) {
240 print '<td class="right nowraponall"><b>'. (isset($totsorties[$annee]) ? price($totsorties[$annee]) : '') .'</b></td>';
241 print '<td class="right nowraponall"><b>'. (isset($totentrees[$annee]) ? price($totentrees[$annee]) : '') .'</b></td>';
242}
243print "</tr>\n";
244
245print "</table>";
246print "</div>";
247
248print '<br>';
249
250
251// Current balance
252$balance = 0;
253
254$sql = "SELECT SUM(b.amount) as total";
255$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
256$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
257$sql .= " WHERE b.fk_account = ba.rowid";
258$sql .= " AND ba.entity IN (".getEntity('bank_account').")";
259if (!empty($id)) {
260 $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
261}
262
263$resql = $db->query($sql);
264if ($resql) {
265 $obj = $db->fetch_object($resql);
266 if ($obj) {
267 $balance = $obj->total;
268 }
269} else {
270 dol_print_error($db);
271}
272
273print '<table class="noborder centpercent">';
274
275$nbcol = '';
276print '<tr class="liste_total"><td><b>'.$langs->trans("CurrentBalance")."</b></td>";
277print '<td colspan="'.($nbcol).'" class="right">'.price($balance).'</td>';
278print "</tr>\n";
279
280print "</table>";
281
282// BUILDING GRAPHICS
283
284$year = $year_end;
285
286$result = dol_mkdir($conf->bank->dir_temp);
287if ($result < 0) {
288 $langs->load("errors");
289 $error++;
290 setEventMessages($langs->trans("ErrorFailedToCreateDir"), null, 'errors');
291} else {
292 // Calcul de $min et $max
293 $sql = "SELECT MIN(b.datev) as min, MAX(b.datev) as max";
294 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
295 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
296 $sql .= " WHERE b.fk_account = ba.rowid";
297 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
298 if ($id && GETPOST("option") != 'all') {
299 $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
300 }
301
302 $resql = $db->query($sql);
303 if ($resql) {
304 $num = $db->num_rows($resql);
305 $obj = $db->fetch_object($resql);
306 $min = $db->jdate($obj->min);
307 $max = $db->jdate($obj->max);
308 $log = "graph.php: min=".$min." max=".$max;
309 dol_syslog($log);
310 } else {
311 dol_print_error($db);
312 }
313
314 // CRED PART
315 // Chargement du tableau des années
316 $tblyear = array();
317 '@phan-var-force array<array<string,int|float>> $tblyear';
318 $tblyear[0] = array();
319 $tblyear[1] = array();
320 $tblyear[2] = array();
321
322 for ($annee = 0; $annee < 3; $annee++) {
323 $sql = "SELECT date_format(b.datev,'%m')";
324 $sql .= ", SUM(b.amount)";
325 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
326 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
327 $sql .= " WHERE b.fk_account = ba.rowid";
328 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
329 $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
330 $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
331 $sql .= " AND b.amount > 0";
332 if ($id && GETPOST("option") != 'all') {
333 $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
334 }
335 $sql .= " GROUP BY date_format(b.datev,'%m');";
336
337 $resql = $db->query($sql);
338 if ($resql) {
339 $num = $db->num_rows($resql);
340 $i = 0;
341 while ($i < $num) {
342 $row = $db->fetch_row($resql);
343 $tblyear[$annee][$row[0]] = $row[1];
344 $i++;
345 }
346 $db->free($resql);
347 } else {
348 dol_print_error($db);
349 }
350 }
351 // Chargement de labels et data_xxx pour tableau 4 Movements
352 $labels = array();
353 $data_year_0 = array();
354 $data_year_1 = array();
355 $data_year_2 = array();
356 $datamin = array();
357
358 for ($i = 0; $i < 12; $i++) {
359 $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
360 $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
361 $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
362 $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
363 $datamin[$i] = 0;
364 }
365
366 // Fabrication tableau 4b
367 $file = $conf->bank->dir_temp."/credmovement".$id."-".$year.".png";
368 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/credmovement".$id."-".$year.".png";
369 $title = $langs->transnoentities("Credit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
370 $graph_datas = array();
371 for ($i = 0; $i < 12; $i++) {
372 $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
373 }
374
375 $px1 = new DolGraph();
376 $px1->SetData($graph_datas);
377 $px1->SetLegend(array(($year), ($year - 1), ($year - 2)));
378 $px1->SetLegendWidthMin(180);
379 $px1->SetMaxValue($px1->GetCeilMaxValue() < 0 ? 0 : $px1->GetCeilMaxValue());
380 $px1->SetMinValue($px1->GetFloorMinValue() > 0 ? 0 : $px1->GetFloorMinValue());
381 $px1->SetTitle($title);
382 $px1->SetWidth($WIDTH);
383 $px1->SetHeight($HEIGHT);
384 $px1->SetType(array('line', 'line', 'line'));
385 $px1->SetShading(3);
386 $px1->setBgColor('onglet');
387 $px1->setBgColorGrid(array(255, 255, 255));
388 $px1->SetHorizTickIncrement(1);
389 $px1->draw($file, $fileurl);
390
391 $show1 = $px1->show();
392
393 unset($graph_datas);
394 unset($px1);
395 unset($tblyear[0]);
396 unset($tblyear[1]);
397 unset($tblyear[2]);
398
399 // DEDBT PART
400 // Chargement du tableau des années
401 $tblyear[0] = array();
402 $tblyear[1] = array();
403 $tblyear[2] = array();
404
405 for ($annee = 0; $annee < 3; $annee++) {
406 $sql = "SELECT date_format(b.datev,'%m')";
407 $sql .= ", SUM(b.amount)";
408 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
409 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
410 $sql .= " WHERE b.fk_account = ba.rowid";
411 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
412 $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
413 $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
414 $sql .= " AND b.amount < 0";
415 if ($id && GETPOST("option") != 'all') {
416 $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
417 }
418 $sql .= " GROUP BY date_format(b.datev,'%m');";
419
420 $resql = $db->query($sql);
421 if ($resql) {
422 $num = $db->num_rows($resql);
423 $i = 0;
424 while ($i < $num) {
425 $row = $db->fetch_row($resql);
426 $tblyear[$annee][$row[0]] = abs($row[1]);
427 $i++;
428 }
429 $db->free($resql);
430 } else {
431 dol_print_error($db);
432 }
433 }
434 // Chargement de labels et data_xxx pour tableau 4 Movements
435 $labels = array();
436 $data_year_0 = array();
437 $data_year_1 = array();
438 $data_year_2 = array();
439
440 for ($i = 0; $i < 12; $i++) {
441 $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
442 $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
443 $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
444 $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
445 $datamin[$i] = 0;
446 }
447
448 $file = $conf->bank->dir_temp."/debmovement".$id."-".$year.".png";
449 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/debmovement".$id."-".$year.".png";
450 $title = $langs->transnoentities("Debit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
451 $graph_datas = array();
452 for ($i = 0; $i < 12; $i++) {
453 $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
454 }
455
456 $px2 = new DolGraph();
457 $px2->SetData($graph_datas);
458 $px2->SetLegend(array(($year), ($year - 1), ($year - 2)));
459 $px2->SetLegendWidthMin(180);
460 $px2->SetMaxValue($px2->GetCeilMaxValue() < 0 ? 0 : $px2->GetCeilMaxValue());
461 $px2->SetMinValue($px2->GetFloorMinValue() > 0 ? 0 : $px2->GetFloorMinValue());
462 $px2->SetTitle($title);
463 $px2->SetWidth($WIDTH);
464 $px2->SetHeight($HEIGHT);
465 $px2->SetType(array('line', 'line', 'line'));
466 $px2->SetShading(3);
467 $px2->setBgColor('onglet');
468 $px2->setBgColorGrid(array(255, 255, 255));
469 $px2->SetHorizTickIncrement(1);
470 $px2->draw($file, $fileurl);
471
472 $show2 = $px2->show();
473
474 unset($graph_datas);
475 unset($px2);
476 unset($tblyear[0]);
477 unset($tblyear[1]);
478 unset($tblyear[2]);
479
480 print '<div class="fichecenter"><div class="fichehalfleft"><div align="center">'; // do not use class="center" here, it will have no effect for the js graph inside.
481 print $show1;
482 print '</div></div><div class="fichehalfright"><div align="center">'; // do not use class="center" here, it will have no effect for the js graph inside.
483 print $show2;
484 print '</div></div></div>';
485 print '<div class="clearboth"></div>';
486}
487
488
489print "\n</div><br>\n";
490
491// End of page
492llxFooter();
493$db->close();
$id
Definition account.php:48
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
bank_prepare_head(Account $object)
Prepare array with list of tabs.
Definition bank.lib.php:39
Class to manage bank accounts.
Class to build graphs.
static getDefaultGraphSizeForStats($direction, $defaultsize='')
getDefaultGraphSizeForStats
Class to manage generation of HTML components Only common components must be here.
llxFooter()
Footer empty.
Definition document.php:107
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...
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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_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.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_mkdir($dir, $dataroot='', $newmask='')
Creation of a directory (this can create recursive subdir)
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
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.