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