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