dolibarr 19.0.3
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
27// Load Dolibarr environment
28require '../../main.inc.php';
29require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
30require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
31require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
32
33// Load translation files required by the page
34$langs->loadLangs(array('banks', 'categories'));
35
36$WIDTH = DolGraph::getDefaultGraphSizeForStats('width', 380); // Large for one graph in a smarpthone.
37$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height', 160);
38
39$id = GETPOST('account') ? GETPOST('account', 'alpha') : GETPOST('id');
40$ref = GETPOST('ref');
41
42// Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
43$hookmanager->initHooks(array('bankannualreport', 'globalcard'));
44
45// Security check
46$fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
47$fieldtype = (!empty($ref) ? 'ref' : 'rowid');
48if ($user->socid) {
49 $socid = $user->socid;
50}
51$result = restrictedArea($user, 'banque', $fieldvalue, 'bank_account&bank_account', '', '', $fieldtype);
52
53$year_start = GETPOST('year_start');
54//$year_current = strftime("%Y", time());
55$year_current = dol_print_date(time(), "%Y");
56if (!$year_start) {
57 $year_start = $year_current - 2;
58 $year_end = $year_current;
59} else {
60 $year_end = $year_start + 2;
61}
62
63
64
65/*
66 * View
67 */
68
69$form = new Form($db);
70
71// Get account informations
72$object = new Account($db);
73if ($id > 0 && !preg_match('/,/', $id)) { // if for a particular account and not a list
74 $result = $object->fetch($id);
75 $id = $object->id;
76}
77if (!empty($ref)) {
78 $result = $object->fetch(0, $ref);
79 $id = $object->id;
80}
81
82$annee = '';
83$totentrees = array();
84$totsorties = array();
85
86$title = $object->ref.' - '.$langs->trans("IOMonthlyReporting");
87$helpurl = "";
88llxHeader('', $title, $helpurl);
89
90// Ce rapport de tresorerie est base sur llx_bank (car doit inclure les transactions sans facture)
91// plutot que sur llx_paiement + llx_paiementfourn
92
93$sql = "SELECT SUM(b.amount)";
94$sql .= ", date_format(b.dateo,'%Y-%m') as dm";
95$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
96$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
97$sql .= " WHERE b.fk_account = ba.rowid";
98$sql .= " AND ba.entity IN (".getEntity('bank_account').")";
99$sql .= " AND b.amount >= 0";
100if (!empty($id)) {
101 $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
102}
103$sql .= " GROUP BY dm";
104
105$resql = $db->query($sql);
106if ($resql) {
107 $num = $db->num_rows($resql);
108 $i = 0;
109 while ($i < $num) {
110 $row = $db->fetch_row($resql);
111 $encaiss[$row[1]] = $row[0];
112 $i++;
113 }
114} else {
115 dol_print_error($db);
116}
117
118$sql = "SELECT SUM(b.amount)";
119$sql .= ", date_format(b.dateo,'%Y-%m') as dm";
120$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
121$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
122$sql .= " WHERE b.fk_account = ba.rowid";
123$sql .= " AND ba.entity IN (".getEntity('bank_account').")";
124$sql .= " AND b.amount <= 0";
125if (!empty($id)) {
126 $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
127}
128$sql .= " GROUP BY dm";
129
130$resql = $db->query($sql);
131if ($resql) {
132 $num = $db->num_rows($resql);
133 $i = 0;
134 while ($i < $num) {
135 $row = $db->fetch_row($resql);
136 $decaiss[$row[1]] = -$row[0];
137 $i++;
138 }
139} else {
140 dol_print_error($db);
141}
142
143
144// Onglets
145$head = bank_prepare_head($object);
146print dol_get_fiche_head($head, 'annual', $langs->trans("FinancialAccount"), 0, 'account');
147
148$title = $langs->trans("FinancialAccount")." : ".$object->label;
149$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>' : '');
150
151$linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
152
153$morehtmlref = '';
154
155if (!empty($id)) {
156 if (!preg_match('/,/', $id)) {
157 dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
158 } else {
159 $bankaccount = new Account($db);
160 $listid = explode(',', $id);
161 foreach ($listid as $key => $aId) {
162 $bankaccount->fetch($aId);
163 $bankaccount->label = $bankaccount->ref;
164 print $bankaccount->getNomUrl(1);
165 if ($key < (count($listid) - 1)) {
166 print ', ';
167 }
168 }
169 }
170} else {
171 print $langs->trans("AllAccounts");
172}
173
174print dol_get_fiche_end();
175
176
177// Affiche tableau
178print load_fiche_titre('', $link, '');
179
180print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
181print '<table class="noborder centpercent">';
182
183print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Month").'</td>';
184for ($annee = $year_start; $annee <= $year_end; $annee++) {
185 print '<td align="center" width="20%" colspan="2" class="liste_titre borderrightlight">'.$annee.'</td>';
186}
187print '</tr>';
188
189print '<tr class="liste_titre">';
190print '<td class="liste_titre">&nbsp;</td>';
191for ($annee = $year_start; $annee <= $year_end; $annee++) {
192 print '<td class="liste_titre" align="center">'.$langs->trans("Debit").'</td><td class="liste_titre" align="center">'.$langs->trans("Credit").'</td>';
193}
194print '</tr>';
195
196for ($annee = $year_start; $annee <= $year_end; $annee++) {
197 $totsorties[$annee] = 0;
198 $totentrees[$annee] = 0;
199}
200
201for ($mois = 1; $mois < 13; $mois++) {
202 print '<tr class="oddeven">';
203 print "<td>".dol_print_date(dol_mktime(1, 1, 1, $mois, 1, 2000), "%B")."</td>";
204
205 for ($annee = $year_start; $annee <= $year_end; $annee++) {
206 $case = sprintf("%04s-%02s", $annee, $mois);
207
208 print '<td class="right" width="10%">&nbsp;';
209 if (isset($decaiss[$case]) && $decaiss[$case] > 0) {
210 print price($decaiss[$case]);
211 $totsorties[$annee] += $decaiss[$case];
212 }
213 print "</td>";
214
215 print '<td class="right borderrightlight" width="10%">&nbsp;';
216 if (isset($encaiss[$case]) && $encaiss[$case] > 0) {
217 print price($encaiss[$case]);
218 $totentrees[$annee] += $encaiss[$case];
219 }
220 print "</td>";
221 }
222 print '</tr>';
223}
224
225// Total debit-credit
226print '<tr class="liste_total"><td><b>'.$langs->trans("Total")."</b></td>";
227for ($annee = $year_start; $annee <= $year_end; $annee++) {
228 print '<td class="right nowraponall"><b>'. (isset($totsorties[$annee]) ? price($totsorties[$annee]) : '') .'</b></td>';
229 print '<td class="right nowraponall"><b>'. (isset($totentrees[$annee]) ? price($totentrees[$annee]) : '') .'</b></td>';
230}
231print "</tr>\n";
232
233print "</table>";
234print "</div>";
235
236print '<br>';
237
238
239// Current balance
240$balance = 0;
241
242$sql = "SELECT SUM(b.amount) as total";
243$sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
244$sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
245$sql .= " WHERE b.fk_account = ba.rowid";
246$sql .= " AND ba.entity IN (".getEntity('bank_account').")";
247if (!empty($id)) {
248 $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
249}
250
251$resql = $db->query($sql);
252if ($resql) {
253 $obj = $db->fetch_object($resql);
254 if ($obj) {
255 $balance = $obj->total;
256 }
257} else {
258 dol_print_error($db);
259}
260
261print '<table class="noborder centpercent">';
262
263$nbcol = '';
264print '<tr class="liste_total"><td><b>'.$langs->trans("CurrentBalance")."</b></td>";
265print '<td colspan="'.($nbcol).'" class="right">'.price($balance).'</td>';
266print "</tr>\n";
267
268print "</table>";
269
270// BUILDING GRAPHICS
271
272$year = $year_end;
273
274$result = dol_mkdir($conf->bank->dir_temp);
275if ($result < 0) {
276 $langs->load("errors");
277 $error++;
278 setEventMessages($langs->trans("ErrorFailedToCreateDir"), null, 'errors');
279} else {
280 // Calcul de $min et $max
281 $sql = "SELECT MIN(b.datev) as min, MAX(b.datev) as max";
282 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
283 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
284 $sql .= " WHERE b.fk_account = ba.rowid";
285 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
286 if ($id && GETPOST("option") != 'all') {
287 $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
288 }
289
290 $resql = $db->query($sql);
291 if ($resql) {
292 $num = $db->num_rows($resql);
293 $obj = $db->fetch_object($resql);
294 $min = $db->jdate($obj->min);
295 $max = $db->jdate($obj->max);
296 } else {
297 dol_print_error($db);
298 }
299 $log = "graph.php: min=".$min." max=".$max;
300 dol_syslog($log);
301
302 // CRED PART
303 // Chargement du tableau des années
304 $tblyear[0] = array();
305 $tblyear[1] = array();
306 $tblyear[2] = array();
307
308 for ($annee = 0; $annee < 3; $annee++) {
309 $sql = "SELECT date_format(b.datev,'%m')";
310 $sql .= ", SUM(b.amount)";
311 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
312 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
313 $sql .= " WHERE b.fk_account = ba.rowid";
314 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
315 $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
316 $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
317 $sql .= " AND b.amount > 0";
318 if ($id && GETPOST("option") != 'all') {
319 $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
320 }
321 $sql .= " GROUP BY date_format(b.datev,'%m');";
322
323 $resql = $db->query($sql);
324 if ($resql) {
325 $num = $db->num_rows($resql);
326 $i = 0;
327 while ($i < $num) {
328 $row = $db->fetch_row($resql);
329 $tblyear[$annee][$row[0]] = $row[1];
330 $i++;
331 }
332 $db->free($resql);
333 } else {
334 dol_print_error($db);
335 }
336 }
337 // Chargement de labels et data_xxx pour tableau 4 Mouvements
338 $labels = array();
339 $data_year_0 = array();
340 $data_year_1 = array();
341 $data_year_2 = array();
342
343 for ($i = 0; $i < 12; $i++) {
344 $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
345 $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
346 $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
347 $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
348 $datamin[$i] = 0;
349 }
350
351 // Fabrication tableau 4b
352 $file = $conf->bank->dir_temp."/credmovement".$id."-".$year.".png";
353 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/credmovement".$id."-".$year.".png";
354 $title = $langs->transnoentities("Credit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
355 $graph_datas = array();
356 for ($i = 0; $i < 12; $i++) {
357 $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
358 }
359
360 $px1 = new DolGraph();
361 $px1->SetData($graph_datas);
362 $px1->SetLegend(array(($year), ($year - 1), ($year - 2)));
363 $px1->SetLegendWidthMin(180);
364 $px1->SetMaxValue($px1->GetCeilMaxValue() < 0 ? 0 : $px1->GetCeilMaxValue());
365 $px1->SetMinValue($px1->GetFloorMinValue() > 0 ? 0 : $px1->GetFloorMinValue());
366 $px1->SetTitle($title);
367 $px1->SetWidth($WIDTH);
368 $px1->SetHeight($HEIGHT);
369 $px1->SetType(array('line', 'line', 'line'));
370 $px1->SetShading(3);
371 $px1->setBgColor('onglet');
372 $px1->setBgColorGrid(array(255, 255, 255));
373 $px1->SetHorizTickIncrement(1);
374 $px1->draw($file, $fileurl);
375
376 $show1 = $px1->show();
377
378 unset($graph_datas);
379 unset($px1);
380 unset($tblyear[0]);
381 unset($tblyear[1]);
382 unset($tblyear[2]);
383
384 // DEDBT PART
385 // Chargement du tableau des années
386 $tblyear[0] = array();
387 $tblyear[1] = array();
388 $tblyear[2] = array();
389
390 for ($annee = 0; $annee < 3; $annee++) {
391 $sql = "SELECT date_format(b.datev,'%m')";
392 $sql .= ", SUM(b.amount)";
393 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
394 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
395 $sql .= " WHERE b.fk_account = ba.rowid";
396 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
397 $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
398 $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
399 $sql .= " AND b.amount < 0";
400 if ($id && GETPOST("option") != 'all') {
401 $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
402 }
403 $sql .= " GROUP BY date_format(b.datev,'%m');";
404
405 $resql = $db->query($sql);
406 if ($resql) {
407 $num = $db->num_rows($resql);
408 $i = 0;
409 while ($i < $num) {
410 $row = $db->fetch_row($resql);
411 $tblyear[$annee][$row[0]] = abs($row[1]);
412 $i++;
413 }
414 $db->free($resql);
415 } else {
416 dol_print_error($db);
417 }
418 }
419 // Chargement de labels et data_xxx pour tableau 4 Mouvements
420 $labels = array();
421 $data_year_0 = array();
422 $data_year_1 = array();
423 $data_year_2 = array();
424
425 for ($i = 0; $i < 12; $i++) {
426 $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
427 $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
428 $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
429 $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
430 $datamin[$i] = 0;
431 }
432
433 $file = $conf->bank->dir_temp."/debmovement".$id."-".$year.".png";
434 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/debmovement".$id."-".$year.".png";
435 $title = $langs->transnoentities("Debit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
436 $graph_datas = array();
437 for ($i = 0; $i < 12; $i++) {
438 $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
439 }
440
441 $px2 = new DolGraph();
442 $px2->SetData($graph_datas);
443 $px2->SetLegend(array(($year), ($year - 1), ($year - 2)));
444 $px2->SetLegendWidthMin(180);
445 $px2->SetMaxValue($px2->GetCeilMaxValue() < 0 ? 0 : $px2->GetCeilMaxValue());
446 $px2->SetMinValue($px2->GetFloorMinValue() > 0 ? 0 : $px2->GetFloorMinValue());
447 $px2->SetTitle($title);
448 $px2->SetWidth($WIDTH);
449 $px2->SetHeight($HEIGHT);
450 $px2->SetType(array('line', 'line', 'line'));
451 $px2->SetShading(3);
452 $px2->setBgColor('onglet');
453 $px2->setBgColorGrid(array(255, 255, 255));
454 $px2->SetHorizTickIncrement(1);
455 $px2->draw($file, $fileurl);
456
457 $show2 = $px2->show();
458
459 unset($graph_datas);
460 unset($px2);
461 unset($tblyear[0]);
462 unset($tblyear[1]);
463 unset($tblyear[2]);
464
465 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.
466 print $show1;
467 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.
468 print $show2;
469 print '</div></div></div>';
470 print '<div class="clearboth"></div>';
471}
472
473
474print "\n</div><br>\n";
475
476// End of page
477llxFooter();
478$db->close();
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader()
Empty header.
Definition wrapper.php:55
llxFooter()
Empty footer.
Definition wrapper.php:69
bank_prepare_head(Account $object)
Prepare array with list of tabs.
Definition bank.lib.php:37
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.
dol_banner_tab($object, $paramid, $morehtml='', $shownav=1, $fieldid='rowid', $fieldref='ref', $morehtmlref='', $moreparam='', $nodbprefix=0, $morehtmlleft='', $morehtmlstatus='', $onlybanner=0, $morehtmlright='')
Show tab footer of a card.
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...
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
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_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
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='', $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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0)
Set event messages in dol_events session object.
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)
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.