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