dolibarr  17.0.4
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
27 require '../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
30 require_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
42 if (GETPOST('account') || GETPOST('ref')) {
43  $id = GETPOST('account') ? GETPOST('account') : GETPOST('ref');
44 }
45 $fieldid = GETPOST('ref') ? 'ref' : 'rowid';
46 if ($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';
53 if (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");
68 if (GETPOST("year", 'int')) {
69  $year = sprintf("%04d", GETPOST("year", 'int'));
70 }
71 if (GETPOST("month", 'int')) {
72  $month = sprintf("%02d", GETPOST("month", 'int'));
73 }
74 
75 
76 $object = new Account($db);
77 if (GETPOST('account') && !preg_match('/,/', GETPOST('account'))) { // if for a particular account and not a list
78  $result = $object->fetch(GETPOST('account', 'int'));
79 }
80 if (GETPOST("ref")) {
81  $result = $object->fetch(0, GETPOST("ref"));
82  $account = $object->id;
83 }
84 
85 $title = $object->ref.' - '.$langs->trans("Graph");
86 $helpurl = "";
87 llxHeader('', $title, $helpurl);
88 
89 $result = dol_mkdir($conf->bank->dir_temp);
90 if ($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  $xyear = substr($textdate, 0, 4);
193  $xday = substr($textdate, 6, 2);
194  $xmonth = substr($textdate, 4, 2);
195 
196  $i = 0;
197  while ($xmonth == $month) {
198  $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
199  if ($day > time()) {
200  $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
201  } else {
202  $datas[$i] = $solde + $subtotal;
203  }
204  $datamin[$i] = $object->min_desired;
205  $dataall[$i] = $object->min_allowed;
206  //$labels[$i] = strftime("%d",$day);
207  $labels[$i] = $xday;
208 
209  $day += 86400;
210  $textdate = strftime("%Y%m%d", $day);
211  $xyear = substr($textdate, 0, 4);
212  $xday = substr($textdate, 6, 2);
213  $xmonth = substr($textdate, 4, 2);
214 
215  $i++;
216  }
217  // If we are the first of month, only $datas[0] is defined to an int value, others are defined to ""
218  // and this may make graph lib report a warning.
219  //$datas[0]=100; KO
220  //$datas[0]=100; $datas[1]=90; OK
221  //var_dump($datas);
222  //exit;
223 
224  // Fabrication tableau 1
225  $file = $conf->bank->dir_temp."/balance".$account."-".$year.$month.".png";
226  $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account."-".$year.$month.".png";
227  $title = $langs->transnoentities("Balance").' - '.$langs->transnoentities("Month").': '.$month.' '.$langs->transnoentities("Year").': '.$year;
228  $graph_datas = array();
229  foreach ($datas as $i => $val) {
230  $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
231  if ($object->min_desired) {
232  array_push($graph_datas[$i], $datamin[$i]);
233  }
234  if ($object->min_allowed) {
235  array_push($graph_datas[$i], $dataall[$i]);
236  }
237  }
238 
239  $px1 = new DolGraph();
240  $px1->SetData($graph_datas);
241  $arraylegends = array($langs->transnoentities("Balance"));
242  if ($object->min_desired) {
243  array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
244  }
245  if ($object->min_allowed) {
246  array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
247  }
248  $px1->SetLegend($arraylegends);
249  $px1->SetLegendWidthMin(180);
250  $px1->SetMaxValue($px1->GetCeilMaxValue() < 0 ? 0 : $px1->GetCeilMaxValue());
251  $px1->SetMinValue($px1->GetFloorMinValue() > 0 ? 0 : $px1->GetFloorMinValue());
252  $px1->SetTitle($title);
253  $px1->SetWidth($WIDTH);
254  $px1->SetHeight($HEIGHT);
255  $px1->SetType(array('lines', 'lines', 'lines'));
256  $px1->setBgColor('onglet');
257  $px1->setBgColorGrid(array(255, 255, 255));
258  $px1->SetHorizTickIncrement(1);
259  $px1->draw($file, $fileurl);
260 
261  $show1 = $px1->show();
262 
263  $px1 = null;
264  $graph_datas = null;
265  $datas = null;
266  $datamin = null;
267  $dataall = null;
268  $labels = null;
269  $amounts = null;
270  }
271 
272  // Graph Balance for the year
273 
274  if ($mode == 'standard') {
275  // Loading table $amounts
276  $amounts = array();
277  $sql = "SELECT date_format(b.datev,'%Y%m%d')";
278  $sql .= ", SUM(b.amount)";
279  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
280  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
281  $sql .= " WHERE b.fk_account = ba.rowid";
282  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
283  $sql .= " AND b.datev >= '".$db->escape($year)."-01-01 00:00:00'";
284  $sql .= " AND b.datev <= '".$db->escape($year)."-12-31 23:59:59'";
285  if ($account && GETPOST("option") != 'all') {
286  $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
287  }
288  $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
289 
290  $resql = $db->query($sql);
291  if ($resql) {
292  $num = $db->num_rows($resql);
293  $i = 0;
294  while ($i < $num) {
295  $row = $db->fetch_row($resql);
296  $amounts[$row[0]] = $row[1];
297  $i++;
298  }
299  $db->free($resql);
300  } else {
301  dol_print_error($db);
302  }
303 
304  // Calculation of $solde before the start of the graph
305  $solde = 0;
306 
307  $sql = "SELECT SUM(b.amount)";
308  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
309  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
310  $sql .= " WHERE b.fk_account = ba.rowid";
311  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
312  $sql .= " AND b.datev < '".$db->escape($year)."-01-01'";
313  if ($account && GETPOST("option") != 'all') {
314  $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
315  }
316 
317  $resql = $db->query($sql);
318  if ($resql) {
319  $row = $db->fetch_row($resql);
320  $solde = $row[0];
321  $db->free($resql);
322  } else {
323  dol_print_error($db);
324  }
325 
326  // Chargement de labels et datas pour tableau 2
327  $labels = array();
328  $datas = array();
329  $datamin = array();
330  $dataall = array();
331 
332  $subtotal = 0;
333  $now = time();
334  $day = dol_mktime(12, 0, 0, 1, 1, $year);
335  $textdate = strftime("%Y%m%d", $day);
336  $xyear = substr($textdate, 0, 4);
337  $xday = substr($textdate, 6, 2);
338 
339  $i = 0;
340  while ($xyear == $year && $day <= $datetime) {
341  $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
342  if ($day > $now) {
343  $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
344  } else {
345  $datas[$i] = $solde + $subtotal;
346  }
347  $datamin[$i] = $object->min_desired;
348  $dataall[$i] = $object->min_allowed;
349  /*if ($xday == '15') // Set only some label for jflot
350  {
351  $labels[$i] = dol_print_date($day, "%b");
352  }*/
353  $labels[$i] = dol_print_date($day, "%Y%m");
354  $day += 86400;
355  $textdate = strftime("%Y%m%d", $day);
356  $xyear = substr($textdate, 0, 4);
357  $xday = substr($textdate, 6, 2);
358  $i++;
359  }
360 
361  // Fabrication tableau 2
362  $file = $conf->bank->dir_temp."/balance".$account."-".$year.".png";
363  $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account."-".$year.".png";
364  $title = $langs->transnoentities("Balance").' - '.$langs->transnoentities("Year").': '.$year;
365  $graph_datas = array();
366  foreach ($datas as $i => $val) {
367  $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
368  if ($object->min_desired) {
369  array_push($graph_datas[$i], $datamin[$i]);
370  }
371  if ($object->min_allowed) {
372  array_push($graph_datas[$i], $dataall[$i]);
373  }
374  }
375  $px2 = new DolGraph();
376  $px2->SetData($graph_datas);
377  $arraylegends = array($langs->transnoentities("Balance"));
378  if ($object->min_desired) {
379  array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
380  }
381  if ($object->min_allowed) {
382  array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
383  }
384  $px2->SetLegend($arraylegends);
385  $px2->SetLegendWidthMin(180);
386  $px2->SetMaxValue($px2->GetCeilMaxValue() < 0 ? 0 : $px2->GetCeilMaxValue());
387  $px2->SetMinValue($px2->GetFloorMinValue() > 0 ? 0 : $px2->GetFloorMinValue());
388  $px2->SetTitle($title);
389  $px2->SetWidth($WIDTH);
390  $px2->SetHeight($HEIGHT);
391  $px2->SetType(array('linesnopoint', 'linesnopoint', 'linesnopoint'));
392  $px2->setBgColor('onglet');
393  $px2->setBgColorGrid(array(255, 255, 255));
394  $px2->SetHideXGrid(true);
395  //$px2->SetHorizTickIncrement(30.41); // 30.41 jours/mois en moyenne
396  $px2->draw($file, $fileurl);
397 
398  $show2 = $px2->show();
399 
400  $px2 = null;
401  $graph_datas = null;
402  $datas = null;
403  $datamin = null;
404  $dataall = null;
405  $labels = null;
406  $amounts = null;
407  }
408 
409  // Graph 3 - Balance for all time line
410 
411  if ($mode == 'showalltime') {
412  // Loading table $amounts
413  $amounts = array();
414 
415  $sql = "SELECT date_format(b.datev,'%Y%m%d')";
416  $sql .= ", SUM(b.amount)";
417  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
418  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
419  $sql .= " WHERE b.fk_account = ba.rowid";
420  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
421  if ($account && GETPOST("option") != 'all') {
422  $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
423  }
424  $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
425 
426  $resql = $db->query($sql);
427  if ($resql) {
428  $num = $db->num_rows($resql);
429  $i = 0;
430 
431  while ($i < $num) {
432  $row = $db->fetch_row($resql);
433  $amounts[$row[0]] = $row[1];
434  $i++;
435  }
436  } else {
437  dol_print_error($db);
438  }
439 
440  // Calcul de $solde avant le debut du graphe
441  $solde = 0;
442 
443  // Chargement de labels et datas pour tableau 3
444  $labels = array();
445  $datas = array();
446  $datamin = array();
447  $dataall = array();
448 
449  $subtotal = 0;
450 
451  $day = $min;
452  $textdate = strftime("%Y%m%d", $day);
453  //print "x".$textdate;
454  $i = 0;
455  while ($day <= ($max + 86400)) { // On va au dela du dernier jour
456  $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
457  //print strftime ("%e %d %m %y",$day)." ".$subtotal."\n<br>";
458  if ($day > ($max + 86400)) {
459  $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
460  } else {
461  $datas[$i] = 0 + $solde + $subtotal;
462  }
463  $datamin[$i] = $object->min_desired;
464  $dataall[$i] = $object->min_allowed;
465  /*if (substr($textdate, 6, 2) == '01' || $i == 0) // Set only few label for jflot
466  {
467  $labels[$i] = substr($textdate, 0, 6);
468  }*/
469  $labels[$i] = substr($textdate, 0, 6);
470 
471  $day += 86400;
472  $textdate = strftime("%Y%m%d", $day);
473  $i++;
474  }
475 
476  // Fabrication tableau 3
477  $file = $conf->bank->dir_temp."/balance".$account.".png";
478  $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account.".png";
479  $title = $langs->transnoentities("Balance")." - ".$langs->transnoentities("AllTime");
480  $graph_datas = array();
481  foreach ($datas as $i => $val) {
482  $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
483  if ($object->min_desired) {
484  array_push($graph_datas[$i], $datamin[$i]);
485  }
486  if ($object->min_allowed) {
487  array_push($graph_datas[$i], $dataall[$i]);
488  }
489  }
490 
491  $px3 = new DolGraph();
492  $px3->SetData($graph_datas);
493  $arraylegends = array($langs->transnoentities("Balance"));
494  if ($object->min_desired) {
495  array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
496  }
497  if ($object->min_allowed) {
498  array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
499  }
500  $px3->SetLegend($arraylegends);
501  $px3->SetLegendWidthMin(180);
502  $px3->SetMaxValue($px3->GetCeilMaxValue() < 0 ? 0 : $px3->GetCeilMaxValue());
503  $px3->SetMinValue($px3->GetFloorMinValue() > 0 ? 0 : $px3->GetFloorMinValue());
504  $px3->SetTitle($title);
505  $px3->SetWidth($WIDTH);
506  $px3->SetHeight($HEIGHT);
507  $px3->SetType(array('linesnopoint', 'linesnopoint', 'linesnopoint'));
508  $px3->setBgColor('onglet');
509  $px3->setBgColorGrid(array(255, 255, 255));
510  $px3->draw($file, $fileurl);
511 
512  $show3 = $px3->show();
513 
514  $px3 = null;
515  $graph_datas = null;
516  $datas = null;
517  $datamin = null;
518  $dataall = null;
519  $labels = null;
520  $amounts = null;
521  }
522 
523  // Tableau 4a - Credit/Debit
524 
525  if ($mode == 'standard') {
526  // Chargement du tableau $credits, $debits
527  $credits = array();
528  $debits = array();
529 
530  $monthnext = $month + 1;
531  $yearnext = $year;
532  if ($monthnext > 12) {
533  $monthnext = 1;
534  $yearnext++;
535  }
536 
537  $sql = "SELECT date_format(b.datev,'%d')";
538  $sql .= ", SUM(b.amount)";
539  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
540  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
541  $sql .= " WHERE b.fk_account = ba.rowid";
542  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
543  $sql .= " AND b.datev >= '".$db->escape($year)."-".$db->escape($month)."-01 00:00:00'";
544  $sql .= " AND b.datev < '".$db->escape($yearnext)."-".$db->escape($monthnext)."-01 00:00:00'";
545  $sql .= " AND b.amount > 0";
546  if ($account && GETPOST("option") != 'all') {
547  $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
548  }
549  $sql .= " GROUP BY date_format(b.datev,'%d')";
550 
551  $resql = $db->query($sql);
552  if ($resql) {
553  $num = $db->num_rows($resql);
554  $i = 0;
555  while ($i < $num) {
556  $row = $db->fetch_row($resql);
557  $credits[$row[0]] = $row[1];
558  $i++;
559  }
560  $db->free($resql);
561  } else {
562  dol_print_error($db);
563  }
564 
565  $monthnext = $month + 1;
566  $yearnext = $year;
567  if ($monthnext > 12) {
568  $monthnext = 1;
569  $yearnext++;
570  }
571 
572  $sql = "SELECT date_format(b.datev,'%d')";
573  $sql .= ", SUM(b.amount)";
574  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
575  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
576  $sql .= " WHERE b.fk_account = ba.rowid";
577  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
578  $sql .= " AND b.datev >= '".$db->escape($year)."-".$db->escape($month)."-01 00:00:00'";
579  $sql .= " AND b.datev < '".$db->escape($yearnext)."-".$db->escape($monthnext)."-01 00:00:00'";
580  $sql .= " AND b.amount < 0";
581  if ($account && GETPOST("option") != 'all') {
582  $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
583  }
584  $sql .= " GROUP BY date_format(b.datev,'%d')";
585 
586  $resql = $db->query($sql);
587  if ($resql) {
588  while ($row = $db->fetch_row($resql)) {
589  $debits[$row[0]] = abs($row[1]);
590  }
591  $db->free($resql);
592  } else {
593  dol_print_error($db);
594  }
595 
596 
597  // Chargement de labels et data_xxx pour tableau 4 Mouvements
598  $labels = array();
599  $data_credit = array();
600  $data_debit = array();
601  for ($i = 0; $i < 31; $i++) {
602  $data_credit[$i] = isset($credits[substr("0".($i + 1), -2)]) ? $credits[substr("0".($i + 1), -2)] : 0;
603  $data_debit[$i] = isset($debits[substr("0".($i + 1), -2)]) ? $debits[substr("0".($i + 1), -2)] : 0;
604  $labels[$i] = sprintf("%02d", $i + 1);
605  $datamin[$i] = $object->min_desired;
606  }
607 
608  // Fabrication tableau 4a
609  $file = $conf->bank->dir_temp."/movement".$account."-".$year.$month.".png";
610  $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/movement".$account."-".$year.$month.".png";
611  $title = $langs->transnoentities("BankMovements").' - '.$langs->transnoentities("Month").': '.$month.' '.$langs->transnoentities("Year").': '.$year;
612  $graph_datas = array();
613  foreach ($data_credit as $i => $val) {
614  $graph_datas[$i] = array($labels[$i], $data_credit[$i], $data_debit[$i]);
615  }
616  $px4 = new DolGraph();
617  $px4->SetData($graph_datas);
618  $px4->SetLegend(array($langs->transnoentities("Credit"), $langs->transnoentities("Debit")));
619  $px4->SetLegendWidthMin(180);
620  $px4->SetMaxValue($px4->GetCeilMaxValue() < 0 ? 0 : $px4->GetCeilMaxValue());
621  $px4->SetMinValue($px4->GetFloorMinValue() > 0 ? 0 : $px4->GetFloorMinValue());
622  $px4->SetTitle($title);
623  $px4->SetWidth($WIDTH);
624  $px4->SetHeight($HEIGHT);
625  $px4->SetType(array('bars', 'bars'));
626  $px4->SetShading(3);
627  $px4->setBgColor('onglet');
628  $px4->setBgColorGrid(array(255, 255, 255));
629  $px4->SetHorizTickIncrement(1);
630  $px4->draw($file, $fileurl);
631 
632  $show4 = $px4->show();
633 
634  $px4 = null;
635  $graph_datas = null;
636  $debits = null;
637  $credits = null;
638  }
639 
640  // Tableau 4b - Credit/Debit
641 
642  if ($mode == 'standard') {
643  // Chargement du tableau $credits, $debits
644  $credits = array();
645  $debits = array();
646  $sql = "SELECT date_format(b.datev,'%m')";
647  $sql .= ", SUM(b.amount)";
648  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
649  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
650  $sql .= " WHERE b.fk_account = ba.rowid";
651  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
652  $sql .= " AND b.datev >= '".$db->escape($year)."-01-01 00:00:00'";
653  $sql .= " AND b.datev <= '".$db->escape($year)."-12-31 23:59:59'";
654  $sql .= " AND b.amount > 0";
655  if ($account && GETPOST("option") != 'all') {
656  $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
657  }
658  $sql .= " GROUP BY date_format(b.datev,'%m');";
659 
660  $resql = $db->query($sql);
661  if ($resql) {
662  $num = $db->num_rows($resql);
663  $i = 0;
664  while ($i < $num) {
665  $row = $db->fetch_row($resql);
666  $credits[$row[0]] = $row[1];
667  $i++;
668  }
669  $db->free($resql);
670  } else {
671  dol_print_error($db);
672  }
673  $sql = "SELECT date_format(b.datev,'%m')";
674  $sql .= ", SUM(b.amount)";
675  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
676  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
677  $sql .= " WHERE b.fk_account = ba.rowid";
678  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
679  $sql .= " AND b.datev >= '".$db->escape($year)."-01-01 00:00:00'";
680  $sql .= " AND b.datev <= '".$db->escape($year)."-12-31 23:59:59'";
681  $sql .= " AND b.amount < 0";
682  if ($account && GETPOST("option") != 'all') {
683  $sql .= " AND b.fk_account IN (".$db->sanitize($account).")";
684  }
685  $sql .= " GROUP BY date_format(b.datev,'%m')";
686 
687  $resql = $db->query($sql);
688  if ($resql) {
689  while ($row = $db->fetch_row($resql)) {
690  $debits[$row[0]] = abs($row[1]);
691  }
692  $db->free($resql);
693  } else {
694  dol_print_error($db);
695  }
696 
697 
698  // Chargement de labels et data_xxx pour tableau 4 Mouvements
699  $labels = array();
700  $data_credit = array();
701  $data_debit = array();
702  for ($i = 0; $i < 12; $i++) {
703  $data_credit[$i] = isset($credits[substr("0".($i + 1), -2)]) ? $credits[substr("0".($i + 1), -2)] : 0;
704  $data_debit[$i] = isset($debits[substr("0".($i + 1), -2)]) ? $debits[substr("0".($i + 1), -2)] : 0;
705  $labels[$i] = dol_print_date(dol_mktime(12, 0, 0, $i + 1, 1, 2000), "%b");
706  $datamin[$i] = $object->min_desired;
707  }
708 
709  // Fabrication tableau 4b
710  $file = $conf->bank->dir_temp."/movement".$account."-".$year.".png";
711  $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/movement".$account."-".$year.".png";
712  $title = $langs->transnoentities("BankMovements").' - '.$langs->transnoentities("Year").': '.$year;
713  $graph_datas = array();
714  foreach ($data_credit as $i => $val) {
715  $graph_datas[$i] = array($labels[$i], $data_credit[$i], $data_debit[$i]);
716  }
717  $px5 = new DolGraph();
718  $px5->SetData($graph_datas);
719  $px5->SetLegend(array($langs->transnoentities("Credit"), $langs->transnoentities("Debit")));
720  $px5->SetLegendWidthMin(180);
721  $px5->SetMaxValue($px5->GetCeilMaxValue() < 0 ? 0 : $px5->GetCeilMaxValue());
722  $px5->SetMinValue($px5->GetFloorMinValue() > 0 ? 0 : $px5->GetFloorMinValue());
723  $px5->SetTitle($title);
724  $px5->SetWidth($WIDTH);
725  $px5->SetHeight($HEIGHT);
726  $px5->SetType(array('bars', 'bars'));
727  $px5->SetShading(3);
728  $px5->setBgColor('onglet');
729  $px5->setBgColorGrid(array(255, 255, 255));
730  $px5->SetHorizTickIncrement(1);
731  $px5->draw($file, $fileurl);
732 
733  $show5 = $px5->show();
734 
735  $px5 = null;
736  $graph_datas = null;
737  $debits = null;
738  $credits = null;
739  }
740 }
741 
742 
743 // Onglets
744 $head = bank_prepare_head($object);
745 print dol_get_fiche_head($head, 'graph', $langs->trans("FinancialAccount"), 0, 'account');
746 
747 
748 $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
749 
750 if ($account) {
751  if (!preg_match('/,/', $account)) {
752  $moreparam = '&month='.$month.'&year='.$year.($mode == 'showalltime' ? '&mode=showalltime' : '');
753 
754  if (GETPOST("option") != 'all') {
755  $morehtml = '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.'&option=all'.$moreparam.'">'.$langs->trans("ShowAllAccounts").'</a>';
756  dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', '', $moreparam, 0, '', '', 1);
757  } else {
758  $morehtml = '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.$moreparam.'">'.$langs->trans("BackToAccount").'</a>';
759  print $langs->trans("AllAccounts");
760  //print $morehtml;
761  }
762  } else {
763  $bankaccount = new Account($db);
764  $listid = explode(',', $account);
765  foreach ($listid as $key => $id) {
766  $bankaccount->fetch($id);
767  $bankaccount->label = $bankaccount->ref;
768  print $bankaccount->getNomUrl(1);
769  if ($key < (count($listid) - 1)) {
770  print ', ';
771  }
772  }
773  }
774 } else {
775  print $langs->trans("AllAccounts");
776 }
777 
778 print dol_get_fiche_end();
779 
780 
781 print '<table class="notopnoleftnoright" width="100%">';
782 
783 // Navigation links
784 print '<tr><td class="right">'.$morehtml.' &nbsp; &nbsp; ';
785 if ($mode == 'showalltime') {
786  print '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.(GETPOST("option") != 'all' ? '' : '&option=all').'">';
787  print $langs->trans("GoBack");
788  print '</a>';
789 } else {
790  print '<a href="'.$_SERVER["PHP_SELF"].'?mode=showalltime&account='.$account.(GETPOST("option") != 'all' ? '' : '&option=all').'">';
791  print $langs->trans("ShowAllTimeBalance");
792  print '</a>';
793 }
794 print '<br><br></td></tr>';
795 
796 print '</table>';
797 
798 
799 // Graphs
800 if ($mode == 'standard') {
801  $prevyear = $year;
802  $nextyear = $year;
803  $prevmonth = $month - 1;
804  $nextmonth = $month + 1;
805  if ($prevmonth < 1) {
806  $prevmonth = 12;
807  $prevyear--;
808  }
809  if ($nextmonth > 12) {
810  $nextmonth = 1;
811  $nextyear++;
812  }
813 
814  // For month
815  $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>";
816  print '<div class="right clearboth">'.$link.'</div>';
817 
818  print '<div class="center clearboth margintoponly">';
819  $file = "movement".$account."-".$year.$month.".png";
820  print $show4;
821  print '</div>';
822 
823  print '<div class="center clearboth margintoponly">';
824  print $show1;
825  print '</div>';
826 
827  // For year
828  $prevyear = $year - 1;
829  $nextyear = $year + 1;
830  $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>";
831 
832  print '<div class="right clearboth margintoponly">'.$link.'</div>';
833 
834  print '<div class="center clearboth margintoponly">';
835  print $show5;
836  print '</div>';
837 
838  print '<div class="center clearboth margintoponly">';
839  print $show2;
840  print '</div>';
841 }
842 
843 if ($mode == 'showalltime') {
844  print '<div class="center clearboth margintoponly">';
845  print $show3;
846  print '</div>';
847 }
848 
849 
850 // End of page
851 llxFooter();
852 $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:56
llxFooter()
Empty footer.
Definition: wrapper.php:70
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.
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("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->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:745
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:143
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='')
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.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
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.
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.