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