dolibarr 23.0.3
graph.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2010 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28// Load Dolibarr environment
29require '../../main.inc.php';
30require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
31require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
32require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
33
42// Load translation files required by the page
43$langs->loadLangs(array('banks', 'categories'));
44
45$WIDTH = DolGraph::getDefaultGraphSizeForStats('width', '768');
46$HEIGHT = DolGraph::getDefaultGraphSizeForStats('height', '200');
47
48// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
49$hookmanager->initHooks(array('bankstats', 'globalcard'));
50
51// Security check
52if (GETPOST('account') || GETPOST('ref')) {
53 $id = GETPOST('account') ? GETPOST('account') : GETPOST('ref');
54}
55$fieldid = GETPOST('ref') ? 'ref' : 'rowid';
56if ($user->socid) {
57 $socid = $user->socid;
58}
59$result = restrictedArea($user, 'banque', $id, 'bank_account&bank_account', '', '', $fieldid);
60
61$account = GETPOST("account");
62$mode = 'standard';
63if (GETPOST("mode") == 'showalltime') {
64 $mode = 'showalltime';
65}
66$error = 0;
67
68
69/*
70 * View
71 */
72
73$now = dol_now();
74$nowlasthourmidday = dol_get_last_hour($now) + 1 + (12 * 3600); // +1 to get next day at 00:00:00. We add 12 hours to be at midday.
75
76$datetime = dol_now();
77$year = dol_print_date($datetime, "%Y");
78$month = dol_print_date($datetime, "%m");
79$day = dol_print_date($datetime, "%d");
80if (GETPOSTINT("year")) {
81 $year = sprintf("%04d", GETPOSTINT("year"));
82}
83if (GETPOSTINT("month")) {
84 $month = sprintf("%02d", GETPOSTINT("month"));
85}
86
87
88$object = new Account($db);
89if (GETPOST('account') && !preg_match('/,/', GETPOST('account'))) { // if for a particular account and not a list
90 $result = $object->fetch(GETPOSTINT('account'));
91}
92if (GETPOST("ref")) {
93 $result = $object->fetch(0, GETPOST("ref"));
94 $account = $object->id;
95}
96
97$title = $object->ref.' - '.$langs->trans("Graph");
98$helpurl = "";
99$show1 = '';
100$show2 = '';
101$show3 = '';
102$show4 = '';
103$show5 = '';
104$morehtml = '';
105
106llxHeader('', $title, $helpurl);
107
108$result = dol_mkdir($conf->bank->dir_temp);
109if ($result < 0) {
110 $langs->load("errors");
111 $error++;
112 setEventMessages($langs->trans("ErrorFailedToCreateDir"), null, 'errors');
113} else {
114 // Calcul $min and $max
115 $sql = "SELECT MIN(b.datev) as min, MAX(b.datev) as max";
116 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
117 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
118 $sql .= " WHERE b.fk_account = ba.rowid";
119 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
120 if ($account && GETPOST("option") != 'all') {
121 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
122 }
123
124 $resql = $db->query($sql);
125 if ($resql) {
126 $num = $db->num_rows($resql);
127 $obj = $db->fetch_object($resql);
128 $min = dol_get_first_hour($db->jdate($obj->min));
129 $max = dol_get_last_hour($db->jdate($obj->max));
130 } else {
131 dol_print_error($db);
132 }
133 if (empty($min)) {
134 $min = dol_now() - 3600 * 24;
135 }
136
137 $log = "graph.php: min=".$min." max=".$max;
138 dol_syslog($log);
139
140
141 // Graph balance of the month
142
143 if ($mode == 'standard') {
144 // Loading table $amounts
145 $amounts = array();
146
147 $monthnext = (int) $month + 1;
148 $yearnext = (int) $year;
149 if ($monthnext > 12) {
150 $monthnext = 1;
151 $yearnext++;
152 }
153 $monthnext = sprintf('%02d', $monthnext);
154 $yearnext = sprintf('%04d', $yearnext);
155
156 $sql = "SELECT date_format(b.datev,'%Y%m%d')";
157 $sql .= ", SUM(b.amount)";
158 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
159 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
160 $sql .= " WHERE b.fk_account = ba.rowid";
161 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
162 $sql .= " AND b.datev >= '".$db->escape($year)."-".$db->escape($month)."-01 00:00:00'";
163 $sql .= " AND b.datev < '".$db->escape($yearnext)."-".$db->escape($monthnext)."-01 00:00:00'";
164 if ($account && GETPOST("option") != 'all') {
165 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
166 }
167 $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
168
169 $resql = $db->query($sql);
170 if ($resql) {
171 $num = $db->num_rows($resql);
172 $i = 0;
173 while ($i < $num) {
174 $row = $db->fetch_row($resql);
175 $amounts[$row[0]] = $row[1];
176 $i++;
177 }
178 $db->free($resql);
179 } else {
180 dol_print_error($db);
181 }
182
183 // Calculation of $solde before the start of the graph
184 $solde = 0;
185
186 $sql = "SELECT SUM(b.amount)";
187 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
188 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
189 $sql .= " WHERE b.fk_account = ba.rowid";
190 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
191 $sql .= " AND b.datev < '".$db->escape($year)."-".sprintf("%02s", $month)."-01'";
192 if ($account && GETPOST("option") != 'all') {
193 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
194 }
195
196 $resql = $db->query($sql);
197 if ($resql) {
198 $row = $db->fetch_row($resql);
199 $solde = $row[0];
200 $db->free($resql);
201 } else {
202 dol_print_error($db);
203 }
204
205 // Chargement de labels et datas pour tableau 1
206 $labels = array();
207 $datas = array();
208 $datamin = array();
209
210 $subtotal = 0;
211 $day = dol_mktime(12, 0, 0, (int) $month, 1, (int) $year);
212 //$textdate = strftime("%Y%m%d", $day);
213 $textdate = dol_print_date($day, "%Y%m%d");
214 $xyear = substr($textdate, 0, 4);
215 $xday = substr($textdate, 6, 2);
216 $xmonth = substr($textdate, 4, 2);
217
218 $i = 0;
219 $dataall = array();
220 while ($xmonth == $month) {
221 $subtotal += (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
222 if ($day > $nowlasthourmidday) {
223 $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
224 } else {
225 $datas[$i] = $solde + $subtotal;
226 }
227 $datamin[$i] = $object->min_desired;
228 $dataall[$i] = $object->min_allowed;
229 //$labels[$i] = strftime("%d",$day);
230 $labels[$i] = $xday;
231
232 $day += 86400;
233
234 //$textdate = strftime("%Y%m%d", $day);
235 $textdate = dol_print_date($day, "%Y%m%d");
236 $xyear = substr($textdate, 0, 4);
237 $xday = substr($textdate, 6, 2);
238 $xmonth = substr($textdate, 4, 2);
239
240 $i++;
241 }
242 // If we are the first of month, only $datas[0] is defined to an int value, others are defined to ""
243 // and this may make graph lib report a warning.
244 //$datas[0]=100; KO
245 //$datas[0]=100; $datas[1]=90; OK
246 //var_dump($datas);
247 //exit;
248
249 // Fabrication tableau 1
250 $file = $conf->bank->dir_temp."/balance".$account."-".$year.$month.".png";
251 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account."-".$year.$month.".png";
252 $title = $langs->transnoentities("Balance").' - '.$langs->transnoentities("Month").': '.$month.' '.$langs->transnoentities("Year").': '.$year;
253 $graph_datas = array();
254 foreach ($datas as $i => $val) {
255 $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
256 if ($object->min_desired) {
257 array_push($graph_datas[$i], $datamin[$i]);
258 }
259 if ($object->min_allowed) {
260 array_push($graph_datas[$i], $dataall[$i]);
261 }
262 }
263
264 $px1 = new DolGraph();
265
266 $px1->datacolor = array(array(120, 130, 250), array(160, 160, 180), array(190, 190, 220));
267
268 $px1->SetData($graph_datas);
269 $arraylegends = array($langs->transnoentities("Balance"));
270 if ($object->min_desired) {
271 array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
272 }
273 if ($object->min_allowed) {
274 array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
275 }
276 $px1->SetLegend($arraylegends);
277 $px1->SetLegendWidthMin(180);
278 $px1->SetMaxValue($px1->GetCeilMaxValue() < 0 ? 0 : $px1->GetCeilMaxValue());
279 $px1->SetMinValue($px1->GetFloorMinValue() > 0 ? 0 : $px1->GetFloorMinValue());
280 $px1->SetTitle($title);
281 $px1->SetWidth($WIDTH);
282 $px1->SetHeight($HEIGHT);
283 $px1->SetType(array('lines', 'lines', 'lines'));
284 $px1->setBgColor('onglet');
285 $px1->setBgColorGrid(array(255, 255, 255));
286 $px1->SetHorizTickIncrement(1);
287 $px1->draw($file, $fileurl);
288
289 $show1 = $px1->show();
290
291 $px1 = null;
292 $graph_datas = null;
293 $datas = null;
294 $datamin = array();
295 $dataall = null;
296 $labels = null;
297 $amounts = null;
298 }
299
300 // Graph Balance for the year
301
302 if ($mode == 'standard') {
303 // Loading table $amounts
304 $amounts = array();
305 $sql = "SELECT date_format(b.datev,'%Y%m%d')";
306 $sql .= ", SUM(b.amount)";
307 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
308 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
309 $sql .= " WHERE b.fk_account = ba.rowid";
310 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
311 $sql .= " AND b.datev >= '".$db->escape($year)."-01-01 00:00:00'";
312 $sql .= " AND b.datev <= '".$db->escape($year)."-12-31 23:59:59'";
313 if ($account && GETPOST("option") != 'all') {
314 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
315 }
316 $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
317
318 $resql = $db->query($sql);
319 if ($resql) {
320 $num = $db->num_rows($resql);
321 $i = 0;
322 while ($i < $num) {
323 $row = $db->fetch_row($resql);
324 $amounts[$row[0]] = $row[1];
325 $i++;
326 }
327 $db->free($resql);
328 } else {
329 dol_print_error($db);
330 }
331
332 // Calculation of $solde before the start of the graph
333 $solde = 0;
334
335 $sql = "SELECT SUM(b.amount)";
336 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
337 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
338 $sql .= " WHERE b.fk_account = ba.rowid";
339 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
340 $sql .= " AND b.datev < '".$db->escape($year)."-01-01'";
341 if ($account && GETPOST("option") != 'all') {
342 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
343 }
344
345 $resql = $db->query($sql);
346 if ($resql) {
347 $row = $db->fetch_row($resql);
348 $solde = $row[0];
349 $db->free($resql);
350 } else {
351 dol_print_error($db);
352 }
353
354 // Chargement de labels et datas pour tableau 2
355 $labels = array();
356 $datas = array();
357 $datamin = array();
358 $dataall = array();
359
360 $subtotal = 0;
361
362 $day = dol_mktime(12, 0, 0, 1, 1, (int) $year);
363 //$textdate = strftime("%Y%m%d", $day);
364 $textdate = dol_print_date($day, "%Y%m%d");
365 $xyear = substr($textdate, 0, 4);
366 $xday = substr($textdate, 6, 2);
367
368 $i = 0;
369 while ($xyear == $year && $day <= $nowlasthourmidday) {
370 $subtotal += (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
371
372 $datas[$i] = $solde + $subtotal;
373
374 $datamin[$i] = $object->min_desired;
375 $dataall[$i] = $object->min_allowed;
376
377 $labels[$i] = dol_print_date($day, "%Y%m");
378
379 $day += 86400;
380
381 $textdate = dol_print_date($day, "%Y%m%d");
382 $xyear = substr($textdate, 0, 4);
383 $xday = substr($textdate, 6, 2);
384
385 $i++;
386 }
387
388 // Fabrication tableau 2
389 $file = $conf->bank->dir_temp."/balance".$account."-".$year.".png";
390 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account."-".$year.".png";
391 $title = $langs->transnoentities("Balance").' - '.$langs->transnoentities("Year").': '.$year;
392 $graph_datas = array();
393 foreach ($datas as $i => $val) {
394 $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
395 if ($object->min_desired) {
396 array_push($graph_datas[$i], $datamin[$i]);
397 }
398 if ($object->min_allowed) {
399 array_push($graph_datas[$i], $dataall[$i]);
400 }
401 }
402 $px2 = new DolGraph();
403
404 $px2->datacolor = array(array(120, 130, 250), array(160, 160, 180), array(190, 190, 220));
405
406 $px2->SetData($graph_datas);
407 $arraylegends = array($langs->transnoentities("Balance"));
408 if ($object->min_desired) {
409 array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
410 }
411 if ($object->min_allowed) {
412 array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
413 }
414 $px2->SetLegend($arraylegends);
415 $px2->SetLegendWidthMin(180);
416 $px2->SetMaxValue($px2->GetCeilMaxValue() < 0 ? 0 : $px2->GetCeilMaxValue());
417 $px2->SetMinValue($px2->GetFloorMinValue() > 0 ? 0 : $px2->GetFloorMinValue());
418 $px2->SetTitle($title);
419 $px2->SetWidth($WIDTH);
420 $px2->SetHeight($HEIGHT);
421 $px2->SetType(array('linesnopoint', 'linesnopoint', 'linesnopoint'));
422 $px2->setBgColor('onglet');
423 $px2->setBgColorGrid(array(255, 255, 255));
424 $px2->SetHideXGrid(true);
425 //$px2->SetHorizTickIncrement(30.41); // 30.41 jours/mois en moyenne
426 $px2->draw($file, $fileurl);
427
428 $show2 = $px2->show();
429
430 $px2 = null;
431 $graph_datas = null;
432 $datas = null;
433 $datamin = null;
434 $dataall = null;
435 $labels = null;
436 $amounts = null;
437 }
438
439 // Graph 3 - Balance for all time line
440
441 if ($mode == 'showalltime') {
442 // Loading table $amounts
443 $amounts = array();
444
445 $sql = "SELECT date_format(b.datev,'%Y%m%d')";
446 $sql .= ", SUM(b.amount)";
447 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
448 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
449 $sql .= " WHERE b.fk_account = ba.rowid";
450 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
451 if ($account && GETPOST("option") != 'all') {
452 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
453 }
454 $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
455
456 $resql = $db->query($sql);
457 if ($resql) {
458 $num = $db->num_rows($resql);
459 $i = 0;
460
461 while ($i < $num) {
462 $row = $db->fetch_row($resql);
463 $amounts[$row[0]] = $row[1];
464 $i++;
465 }
466 } else {
467 dol_print_error($db);
468 }
469
470 // Calcul de $solde avant le debut du graphe
471 $solde = 0;
472
473 // Chargement de labels et datas pour tableau 3
474 $labels = array();
475 $datas = array();
476 $datamin = array();
477 $dataall = array();
478
479 $subtotal = 0;
480
481 $day = $min;
482 //$textdate = strftime("%Y%m%d", $day);
483 $textdate = dol_print_date($day, "%Y%m%d");
484 //print "x".$textdate;
485 $i = 0;
486 while ($day <= ($max + 86400)) { // On va au dela du dernier jour
487 $subtotal += (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
488 //print strftime ("%e %d %m %y",$day)." ".$subtotal."\n<br>";
489 if ($day > ($max + 86400)) {
490 $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
491 } else {
492 $datas[$i] = $solde + $subtotal;
493 }
494 $datamin[$i] = $object->min_desired;
495 $dataall[$i] = $object->min_allowed;
496 /*if (substr($textdate, 6, 2) == '01' || $i == 0) // Set only few label for jflot
497 {
498 $labels[$i] = substr($textdate, 0, 6);
499 }*/
500 $labels[$i] = substr($textdate, 0, 6);
501
502 $day += 86400;
503 //$textdate = strftime("%Y%m%d", $day);
504 $textdate = dol_print_date($day, "%Y%m%d");
505 $i++;
506 }
507
508 // Fabrication tableau 3
509 $file = $conf->bank->dir_temp."/balance".$account.".png";
510 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account.".png";
511 $title = $langs->transnoentities("Balance")." - ".$langs->transnoentities("AllTime");
512 $graph_datas = array();
513 foreach ($datas as $i => $val) {
514 $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
515 if ($object->min_desired) {
516 array_push($graph_datas[$i], $datamin[$i]);
517 }
518 if ($object->min_allowed) {
519 array_push($graph_datas[$i], $dataall[$i]);
520 }
521 }
522
523 $px3 = new DolGraph();
524
525 $px3->datacolor = array(array(120, 130, 250), array(160, 160, 180), array(190, 190, 220));
526
527 $px3->SetData($graph_datas);
528 $arraylegends = array($langs->transnoentities("Balance"));
529 if ($object->min_desired) {
530 array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
531 }
532 if ($object->min_allowed) {
533 array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
534 }
535 $px3->SetLegend($arraylegends);
536 $px3->SetLegendWidthMin(180);
537 $px3->SetMaxValue($px3->GetCeilMaxValue() < 0 ? 0 : $px3->GetCeilMaxValue());
538 $px3->SetMinValue($px3->GetFloorMinValue() > 0 ? 0 : $px3->GetFloorMinValue());
539 $px3->SetTitle($title);
540 $px3->SetWidth($WIDTH);
541 $px3->SetHeight($HEIGHT);
542 $px3->SetType(array('linesnopoint', 'linesnopoint', 'linesnopoint'));
543 $px3->setBgColor('onglet');
544 $px3->setBgColorGrid(array(255, 255, 255));
545 $px3->draw($file, $fileurl);
546
547 $show3 = $px3->show();
548
549 $px3 = null;
550 $graph_datas = null;
551 $datas = null;
552 $datamin = null;
553 $dataall = null;
554 $labels = null;
555 $amounts = null;
556 }
557
558 // Graph input/output - Credit/Debit for the month
559
560 if ($mode == 'standard') {
561 // Chargement du tableau $credits, $debits
562 $credits = array();
563 $debits = array();
564
565 $monthnext = (int) $month + 1;
566 $yearnext = (int) $year;
567 if ($monthnext > 12) {
568 $monthnext = 1;
569 $yearnext++;
570 }
571 $monthnext = sprintf('%02d', $monthnext);
572 $yearnext = sprintf('%04d', $yearnext);
573
574 $sql = "SELECT date_format(b.datev,'%d')";
575 $sql .= ", SUM(b.amount)";
576 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
577 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
578 $sql .= " WHERE b.fk_account = ba.rowid";
579 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
580 $sql .= " AND b.datev >= '".$db->escape($year)."-".$db->escape($month)."-01 00:00:00'";
581 $sql .= " AND b.datev < '".$db->escape($yearnext)."-".$db->escape($monthnext)."-01 00:00:00'";
582 $sql .= " AND b.amount > 0";
583 if ($account && GETPOST("option") != 'all') {
584 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
585 }
586 $sql .= " GROUP BY date_format(b.datev,'%d')";
587
588 $resql = $db->query($sql);
589 if ($resql) {
590 $num = $db->num_rows($resql);
591 $i = 0;
592 while ($i < $num) {
593 $row = $db->fetch_row($resql);
594 $credits[$row[0]] = $row[1];
595 $i++;
596 }
597 $db->free($resql);
598 } else {
599 dol_print_error($db);
600 }
601
602 $monthnext = (int) $month + 1;
603 $yearnext = (int) $year;
604 if ($monthnext > 12) {
605 $monthnext = 1;
606 $yearnext++;
607 }
608 $monthnext = sprintf('%02d', $monthnext);
609 $yearnext = sprintf('%04d', $yearnext);
610
611 $sql = "SELECT date_format(b.datev,'%d')";
612 $sql .= ", SUM(b.amount)";
613 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
614 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
615 $sql .= " WHERE b.fk_account = ba.rowid";
616 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
617 $sql .= " AND b.datev >= '".$db->escape($year)."-".$db->escape($month)."-01 00:00:00'";
618 $sql .= " AND b.datev < '".$db->escape($yearnext)."-".$db->escape($monthnext)."-01 00:00:00'";
619 $sql .= " AND b.amount < 0";
620 if ($account && GETPOST("option") != 'all') {
621 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
622 }
623 $sql .= " GROUP BY date_format(b.datev,'%d')";
624
625 $resql = $db->query($sql);
626 if ($resql) {
627 while ($row = $db->fetch_row($resql)) {
628 $debits[$row[0]] = abs($row[1]);
629 }
630 $db->free($resql);
631 } else {
632 dol_print_error($db);
633 }
634
635
636 // Chargement de labels et data_xxx pour tableau 4 Movements
637 $labels = array();
638 $data_credit = array();
639 $data_debit = array();
640 for ($i = 0; $i < 31; $i++) {
641 $data_credit[$i] = isset($credits[substr("0".($i + 1), -2)]) ? $credits[substr("0".($i + 1), -2)] : 0;
642 $data_debit[$i] = isset($debits[substr("0".($i + 1), -2)]) ? $debits[substr("0".($i + 1), -2)] : 0;
643 $labels[$i] = sprintf("%02d", $i + 1);
644 $datamin[$i] = $object->min_desired;
645 }
646
647 // Fabrication tableau 4a
648 $file = $conf->bank->dir_temp."/movement".$account."-".$year.$month.".png";
649 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/movement".$account."-".$year.$month.".png";
650 $title = $langs->transnoentities("BankMovements").' - '.$langs->transnoentities("Month").': '.$month.' '.$langs->transnoentities("Year").': '.$year;
651 $graph_datas = array();
652 foreach ($data_credit as $i => $val) {
653 $graph_datas[$i] = array($labels[$i], $data_credit[$i], $data_debit[$i]);
654 }
655 $px4 = new DolGraph();
656 $px4->SetData($graph_datas);
657 $px4->SetLegend(array($langs->transnoentities("Credit").' ('.$langs->transnoentities("Input").')', $langs->transnoentities("Debit").' ('.$langs->transnoentities("Output").')'));
658 $px4->SetLegendWidthMin(180);
659 $px4->SetMaxValue($px4->GetCeilMaxValue() < 0 ? 0 : $px4->GetCeilMaxValue());
660 $px4->SetMinValue($px4->GetFloorMinValue() > 0 ? 0 : $px4->GetFloorMinValue());
661 $px4->SetTitle($title);
662 $px4->SetWidth($WIDTH);
663 $px4->SetHeight($HEIGHT);
664 $px4->SetType(array('bars', 'bars'));
665 $px4->SetShading(3);
666 $px4->setBgColor('onglet');
667 $px4->setBgColorGrid(array(255, 255, 255));
668 $px4->SetHorizTickIncrement(1);
669 $px4->draw($file, $fileurl);
670
671 $show4 = $px4->show();
672
673 $px4 = null;
674 $graph_datas = null;
675 $debits = null;
676 $credits = null;
677 }
678
679 // Tableau 4b - Credit/Debit
680
681 if ($mode == 'standard') {
682 // Chargement du tableau $credits, $debits
683 $credits = array();
684 $debits = array();
685 $sql = "SELECT date_format(b.datev,'%m')";
686 $sql .= ", SUM(b.amount)";
687 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
688 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
689 $sql .= " WHERE b.fk_account = ba.rowid";
690 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
691 $sql .= " AND b.datev >= '".$db->escape($year)."-01-01 00:00:00'";
692 $sql .= " AND b.datev <= '".$db->escape($year)."-12-31 23:59:59'";
693 $sql .= " AND b.amount > 0";
694 if ($account && GETPOST("option") != 'all') {
695 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
696 }
697 $sql .= " GROUP BY date_format(b.datev,'%m');";
698
699 $resql = $db->query($sql);
700 if ($resql) {
701 $num = $db->num_rows($resql);
702 $i = 0;
703 while ($i < $num) {
704 $row = $db->fetch_row($resql);
705 $credits[$row[0]] = $row[1];
706 $i++;
707 }
708 $db->free($resql);
709 } else {
710 dol_print_error($db);
711 }
712 $sql = "SELECT date_format(b.datev,'%m')";
713 $sql .= ", SUM(b.amount)";
714 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
715 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
716 $sql .= " WHERE b.fk_account = ba.rowid";
717 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
718 $sql .= " AND b.datev >= '".$db->escape($year)."-01-01 00:00:00'";
719 $sql .= " AND b.datev <= '".$db->escape($year)."-12-31 23:59:59'";
720 $sql .= " AND b.amount < 0";
721 if ($account && GETPOST("option") != 'all') {
722 $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
723 }
724 $sql .= " GROUP BY date_format(b.datev,'%m')";
725
726 $resql = $db->query($sql);
727 if ($resql) {
728 while ($row = $db->fetch_row($resql)) {
729 $debits[$row[0]] = abs($row[1]);
730 }
731 $db->free($resql);
732 } else {
733 dol_print_error($db);
734 }
735
736
737 // Chargement de labels et data_xxx pour tableau 4 Movements
738 $labels = array();
739 $data_credit = array();
740 $data_debit = array();
741 for ($i = 0; $i < 12; $i++) {
742 $data_credit[$i] = isset($credits[substr("0".($i + 1), -2)]) ? $credits[substr("0".($i + 1), -2)] : 0;
743 $data_debit[$i] = isset($debits[substr("0".($i + 1), -2)]) ? $debits[substr("0".($i + 1), -2)] : 0;
744 $labels[$i] = dol_print_date(dol_mktime(12, 0, 0, $i + 1, 1, 2000), "%b");
745 $datamin[$i] = $object->min_desired;
746 }
747
748 // Fabrication tableau 4b
749 $file = $conf->bank->dir_temp."/movement".$account."-".$year.".png";
750 $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/movement".$account."-".$year.".png";
751 $title = $langs->transnoentities("BankMovements").' - '.$langs->transnoentities("Year").': '.$year;
752 $graph_datas = array();
753 foreach ($data_credit as $i => $val) {
754 $graph_datas[$i] = array($labels[$i], $data_credit[$i], $data_debit[$i]);
755 }
756 $px5 = new DolGraph();
757 $px5->SetData($graph_datas);
758 $px5->SetLegend(array($langs->transnoentities("Credit").' ('.$langs->transnoentities("Input").')', $langs->transnoentities("Debit").' ('.$langs->transnoentities("Output").')'));
759 $px5->SetLegendWidthMin(180);
760 $px5->SetMaxValue($px5->GetCeilMaxValue() < 0 ? 0 : $px5->GetCeilMaxValue());
761 $px5->SetMinValue($px5->GetFloorMinValue() > 0 ? 0 : $px5->GetFloorMinValue());
762 $px5->SetTitle($title);
763 $px5->SetWidth($WIDTH);
764 $px5->SetHeight($HEIGHT);
765 $px5->SetType(array('bars', 'bars'));
766 $px5->SetShading(3);
767 $px5->setBgColor('onglet');
768 $px5->setBgColorGrid(array(255, 255, 255));
769 $px5->SetHorizTickIncrement(1);
770 $px5->draw($file, $fileurl);
771
772 $show5 = $px5->show();
773
774 $px5 = null;
775 $graph_datas = null;
776 $debits = null;
777 $credits = null;
778 }
779}
780
781
782// Onglets
783$head = bank_prepare_head($object);
784print dol_get_fiche_head($head, 'annual', $langs->trans("FinancialAccount"), 0, 'account');
785
786
787$linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
788
789if ($account) {
790 if (!preg_match('/,/', $account)) {
791 $moreparam = '&month='.$month.'&year='.$year.($mode == 'showalltime' ? '&mode=showalltime' : '');
792
793 if (GETPOST("option") != 'all') {
794 $morehtml = '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.'&option=all'.$moreparam.'">'.$langs->trans("ShowAllAccounts").'</a>';
795 dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', '', $moreparam, 0, '', '', 1);
796 } else {
797 $morehtml = '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.$moreparam.'">'.$langs->trans("BackToAccount").'</a>';
798 print $langs->trans("AllAccounts");
799 //print $morehtml;
800 }
801 } else {
802 $bankaccount = new Account($db);
803 $listid = explode(',', $account);
804 foreach ($listid as $key => $id) {
805 $bankaccount->fetch((int) $id);
806 $bankaccount->label = $bankaccount->ref;
807 print $bankaccount->getNomUrl(1);
808 if ($key < (count($listid) - 1)) {
809 print ', ';
810 }
811 }
812 }
813} else {
814 print $langs->trans("AllAccounts");
815}
816
817print dol_get_fiche_end();
818
819$head = bank_report_prepare_head($object);
820print dol_get_fiche_head($head, 'graph', $langs->trans("FinancialAccount"), 0);
821
822print '<table class="notopnoleftnoright centerpercent">';
823
824// Navigation links
825print '<tr><td class="right">'.$morehtml.' &nbsp; &nbsp; ';
826if ($mode == 'showalltime') {
827 print '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.(GETPOST("option") != 'all' ? '' : '&option=all').'">';
828 print $langs->trans("GoBack");
829 print '</a>';
830} else {
831 print '<a href="'.$_SERVER["PHP_SELF"].'?mode=showalltime&account='.$account.(GETPOST("option") != 'all' ? '' : '&option=all').'">';
832 print $langs->trans("ShowAllTimeBalance");
833 print '</a>';
834}
835print '<br><br></td></tr>';
836
837print '</table>';
838
839
840// Graphs
841if ($mode == 'standard') {
842 $prevyear = (int) $year;
843 $nextyear = (int) $year;
844 $prevmonth = (int) $month - 1;
845 $nextmonth = (int) $month + 1;
846 if ($prevmonth < 1) {
847 $prevmonth = 12;
848 $prevyear--;
849 }
850 if ($nextmonth > 12) {
851 $nextmonth = 1;
852 $nextyear++;
853 }
854 $nextmonth = sprintf('%02d', $nextmonth);
855 $prevmonth = sprintf('%02d', $prevmonth);
856 $nextyear = sprintf('%04d', $nextyear);
857 $prevyear = sprintf('%04d', $prevyear);
858
859 // For month
860 $link = "<a href='".$_SERVER["PHP_SELF"]."?account=".$account.(GETPOST("option") != 'all' ? '' : '&option=all')."&year=".$prevyear."&month=".$prevmonth."'>".img_previous('', 'class="valignbottom"')."</a> ".$langs->trans("Month")." <a href='".$_SERVER["PHP_SELF"]."?account=".$account.(GETPOST("option") != 'all' ? '' : '&option=all')."&year=".$nextyear."&month=".$nextmonth."'>".img_next('', 'class="valignbottom"')."</a>";
861 print '<div class="right clearboth">'.$link.'</div>';
862
863 print '<div class="center clearboth margintoponly">';
864 $file = "movement".$account."-".$year.$month.".png";
865 print $show4;
866 print '</div>';
867
868 print '<div class="center clearboth margintoponly">';
869 print $show1;
870 print '</div>';
871
872 print '<br><br>';
873
874 // For year
875 $prevyear = (int) $year - 1;
876 $nextyear = (int) $year + 1;
877 $nextyear = sprintf('%04d', $nextyear);
878 $prevyear = sprintf('%04d', $prevyear);
879 $link = "<a href='".$_SERVER["PHP_SELF"]."?account=".$account.(GETPOST("option") != 'all' ? '' : '&option=all')."&year=".($prevyear)."'>".img_previous('', 'class="valignbottom"')."</a> ".$langs->trans("Year")." <a href='".$_SERVER["PHP_SELF"]."?account=".$account.(GETPOST("option") != 'all' ? '' : '&option=all')."&year=".($nextyear)."'>".img_next('', 'class="valignbottom"')."</a>";
880
881 print '<div class="right clearboth margintoponly">'.$link.'</div>';
882
883 print '<div class="center clearboth margintoponly">';
884 print $show5;
885 print '</div>';
886
887 print '<div class="center clearboth margintoponly">';
888 print $show2;
889 print '</div>';
890}
891
892if ($mode == 'showalltime') {
893 print '<div class="center clearboth margintoponly">';
894 print $show3;
895 print '</div>';
896}
897
898print dol_get_fiche_end();
899
900// End of page
901llxFooter();
902$db->close();
$id
Support class for third parties, contacts, members, users or resources.
Definition account.php:47
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
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:73
bank_report_prepare_head(Account $object)
Prepare array with list of tabs for bank report.
Definition bank.lib.php:175
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
dol_get_first_hour($date, $gm='tzserver')
Return GMT time for first hour of a given GMT date (it removes hours, min and second part)
Definition date.lib.php:663
dol_get_last_hour($date, $gm='tzserver')
Return GMT time for last hour of a given GMT date (it replaces hours, min and second part to 23:59:59...
Definition date.lib.php:649
dol_now($mode='gmt')
Return date for now.
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...
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
Output date in a string format according to outputlangs (or langs if not defined).
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)
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.