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