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