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