PageRenderTime 48ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/compta/bank/graph.php

https://github.com/asterix14/dolibarr
PHP | 859 lines | 690 code | 100 blank | 69 comment | 107 complexity | dda134e7e3b5d14fa14e0b7a330086d1 MD5 | raw file
Possible License(s): LGPL-2.0
  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@dolibarr.fr>
  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 2 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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/bank/graph.php
  21. * \ingroup banque
  22. * \brief Page graph des transactions bancaires
  23. */
  24. require("./pre.inc.php");
  25. require_once(DOL_DOCUMENT_ROOT."/core/lib/bank.lib.php");
  26. require_once(DOL_DOCUMENT_ROOT."/compta/bank/class/account.class.php");
  27. require_once(DOL_DOCUMENT_ROOT."/core/class/dolgraph.class.php");
  28. $langs->load("banks");
  29. // Security check
  30. if (isset($_GET["account"]) || isset($_GET["ref"]))
  31. {
  32. $id = isset($_GET["account"])?$_GET["account"]:(isset($_GET["ref"])?$_GET["ref"]:'');
  33. }
  34. $fieldid = isset($_GET["ref"])?'ref':'rowid';
  35. if ($user->societe_id) $socid=$user->societe_id;
  36. $result=restrictedArea($user,'banque',$id,'bank_account','','',$fieldid);
  37. $account=$_GET["account"];
  38. $mode='standard';
  39. if (isset($_GET["mode"]) && $_GET["mode"] == 'showalltime') $mode='showalltime';
  40. $mesg = '';
  41. $error=0;
  42. /*
  43. * View
  44. */
  45. llxHeader();
  46. $form = new Form($db);
  47. // If lib forced
  48. if (! empty($_GET["lib"])) $conf->global->MAIN_GRAPH_LIBRARY=$_GET["lib"];
  49. $datetime = time();
  50. $year = dol_print_date($datetime, "%Y");
  51. $month = dol_print_date($datetime, "%m");
  52. $day = dol_print_date($datetime, "%d");
  53. if (! empty($_GET["year"])) $year=sprintf("%04d",$_GET["year"]);
  54. if (! empty($_GET["month"])) $month=sprintf("%02d",$_GET["month"]);
  55. $acct = new Account($db);
  56. if ($_GET["account"] && ! preg_match('/,/',$_GET["account"])) // if for a particular account and not a list
  57. {
  58. $result=$acct->fetch($_GET["account"]);
  59. }
  60. if ($_GET["ref"])
  61. {
  62. $result=$acct->fetch(0,$_GET["ref"]);
  63. $account=$acct->id;
  64. }
  65. $result=create_exdir($conf->banque->dir_temp);
  66. if ($result < 0)
  67. {
  68. $langs->load("errors");
  69. $error++;
  70. $mesg='<div class="error">'.$langs->trans("ErrorFailedToCreateDir").'</div>';
  71. }
  72. else
  73. {
  74. // Definition de $width et $height
  75. $width = 768;
  76. $height = 200;
  77. // Calcul de $min et $max
  78. $sql = "SELECT MIN(b.datev) as min, MAX(b.datev) as max";
  79. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  80. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  81. $sql.= " WHERE b.fk_account = ba.rowid";
  82. $sql.= " AND ba.entity = ".$conf->entity;
  83. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  84. $resql = $db->query($sql);
  85. if ($resql)
  86. {
  87. $num = $db->num_rows($resql);
  88. $obj = $db->fetch_object($resql);
  89. $min = $db->jdate($obj->min);
  90. $max = $db->jdate($obj->max);
  91. }
  92. else
  93. {
  94. dol_print_error($db);
  95. }
  96. $log="graph.php: min=".$min." max=".$max;
  97. dol_syslog($log);
  98. // Tableau 1
  99. if ($mode == 'standard')
  100. {
  101. // Chargement du tableau $amounts
  102. $amounts = array();
  103. $monthnext = $month+1;
  104. $yearnext = $year;
  105. if ($monthnext > 12)
  106. {
  107. $monthnext=1;
  108. $yearnext++;
  109. }
  110. $sql = "SELECT date_format(b.datev,'%Y%m%d')";
  111. $sql.= ", SUM(b.amount)";
  112. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  113. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  114. $sql.= " WHERE b.fk_account = ba.rowid";
  115. $sql.= " AND ba.entity = ".$conf->entity;
  116. $sql.= " AND b.datev >= '".$year."-".$month."-01 00:00:00'";
  117. $sql.= " AND b.datev < '".$yearnext."-".$monthnext."-01 00:00:00'";
  118. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  119. $sql.= " GROUP BY date_format(b.datev,'%Y%m%d')";
  120. $resql = $db->query($sql);
  121. if ($resql)
  122. {
  123. $num = $db->num_rows($resql);
  124. $i = 0;
  125. while ($i < $num)
  126. {
  127. $row = $db->fetch_row($resql);
  128. $amounts[$row[0]] = $row[1];
  129. $i++;
  130. }
  131. $db->free($resql);
  132. }
  133. else
  134. {
  135. dol_print_error($db);
  136. }
  137. // Calcul de $solde avant le debut du graphe
  138. $solde = 0;
  139. $sql = "SELECT SUM(b.amount)";
  140. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  141. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  142. $sql.= " WHERE b.fk_account = ba.rowid";
  143. $sql.= " AND ba.entity = ".$conf->entity;
  144. $sql.= " AND b.datev < '".$year."-".sprintf("%02s",$month)."-01'";
  145. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  146. $resql = $db->query($sql);
  147. if ($resql)
  148. {
  149. $row = $db->fetch_row($resql);
  150. $solde = $row[0];
  151. $db->free($resql);
  152. }
  153. else
  154. {
  155. dol_print_error($db);
  156. }
  157. // Chargement de labels et datas pour tableau 1
  158. $labels = array();
  159. $datas = array();
  160. $datamin = array();
  161. $subtotal = 0;
  162. $day = dol_mktime(12,0,0,$month,1,$year);
  163. $textdate = strftime("%Y%m%d",$day);
  164. $xyear = substr($textdate,0,4);
  165. $xday = substr($textdate,6,2);
  166. $xmonth = substr($textdate,4,2);
  167. $i = 0;
  168. while ($xmonth == $month)
  169. {
  170. $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
  171. if ($day > time())
  172. {
  173. $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
  174. }
  175. else
  176. {
  177. $datas[$i] = $solde + $subtotal;
  178. }
  179. $datamin[$i] = $acct->min_desired;
  180. //$labels[$i] = strftime("%d",$day);
  181. $labels[$i] = $xday;
  182. $day += 86400;
  183. $textdate = strftime("%Y%m%d",$day);
  184. $xyear = substr($textdate,0,4);
  185. $xday = substr($textdate,6,2);
  186. $xmonth = substr($textdate,4,2);
  187. $i++;
  188. }
  189. // If we are the first of month, only $datas[0] is defined to an int value, others are defined to ""
  190. // and this make artichow report a warning.
  191. //$datas[0]=100; KO
  192. //$datas[0]=100; $datas[1]=90; OK
  193. //var_dump($datas);
  194. //exit;
  195. // Fabrication tableau 1
  196. $file= $conf->banque->dir_temp."/balance".$account."-".$year.$month.".png";
  197. $title=$langs->transnoentities("Balance").' - '.$langs->transnoentities("Month").': '.$month.' '.$langs->transnoentities("Year").': '.$year;
  198. $graph_datas=array();
  199. foreach($datas as $i => $val)
  200. {
  201. if ($acct->min_desired) $graph_datas[$i]=array(isset($labels[$i])?$labels[$i]:'',$datas[$i],$datamin[$i]);
  202. else $graph_datas[$i]=array(isset($labels[$i])?$labels[$i]:'',$datas[$i]);
  203. }
  204. $px = new DolGraph();
  205. $px->SetData($graph_datas);
  206. if ($acct->min_desired) $px->SetLegend(array($langs->transnoentities("Balance"),$langs->transnoentities("BalanceMinimalDesired")));
  207. else $px->SetLegend(array($langs->transnoentities("Balance")));
  208. $px->SetLegendWidthMin(180);
  209. $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue());
  210. $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue());
  211. $px->SetTitle($title);
  212. $px->SetWidth($width);
  213. $px->SetHeight($height);
  214. $px->SetType('lines');
  215. $px->setBgColor('onglet');
  216. $px->setBgColorGrid(array(255,255,255));
  217. $px->SetHorizTickIncrement(1);
  218. $px->SetPrecisionY(0);
  219. $px->draw($file);
  220. unset($graph_datas);
  221. unset($px);
  222. unset($datas);
  223. unset($datamin);
  224. unset($labels);
  225. unset($amounts);
  226. }
  227. // Tableau 2
  228. if ($mode == 'standard')
  229. {
  230. // Chargement du tableau $amounts
  231. $amounts = array();
  232. $sql = "SELECT date_format(b.datev,'%Y%m%d')";
  233. $sql.= ", SUM(b.amount)";
  234. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  235. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  236. $sql.= " WHERE b.fk_account = ba.rowid";
  237. $sql.= " AND ba.entity = ".$conf->entity;
  238. $sql.= " AND b.datev >= '".$year."-01-01 00:00:00'";
  239. $sql.= " AND b.datev <= '".$year."-12-31 23:59:59'";
  240. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  241. $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
  242. $resql = $db->query($sql);
  243. if ($resql)
  244. {
  245. $num = $db->num_rows($resql);
  246. $i = 0;
  247. while ($i < $num)
  248. {
  249. $row = $db->fetch_row($resql);
  250. $amounts[$row[0]] = $row[1];
  251. $i++;
  252. }
  253. $db->free($resql);
  254. }
  255. else
  256. {
  257. dol_print_error($db);
  258. }
  259. // Calcul de $solde avant le debut du graphe
  260. $solde = 0;
  261. $sql = "SELECT SUM(b.amount)";
  262. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  263. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  264. $sql.= " WHERE b.fk_account = ba.rowid";
  265. $sql.= " AND ba.entity = ".$conf->entity;
  266. $sql.= " AND b.datev < '".$year."-01-01'";
  267. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  268. $resql = $db->query($sql);
  269. if ($resql)
  270. {
  271. $row = $db->fetch_row($resql);
  272. $solde = $row[0];
  273. $db->free($resql);
  274. }
  275. else
  276. {
  277. dol_print_error($db);
  278. }
  279. // Chargement de labels et datas pour tableau 2
  280. $labels = array();
  281. $datas = array();
  282. $datamin = array();
  283. $subtotal = 0;
  284. $now = time();
  285. $day = dol_mktime(12,0,0,1,1,$year);
  286. $textdate = strftime("%Y%m%d",$day);
  287. $xyear = substr($textdate,0,4);
  288. $xday = substr($textdate,6,2);
  289. $i = 0;
  290. while ($xyear == $year && $day <= $datetime)
  291. {
  292. $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
  293. if ($day > $now)
  294. {
  295. $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
  296. }
  297. else
  298. {
  299. $datas[$i] = $solde + $subtotal;
  300. }
  301. $datamin[$i] = $acct->min_desired;
  302. if ($xday == '15')
  303. {
  304. $labels[$i] = dol_print_date($day,"%b");
  305. }
  306. $day += 86400;
  307. $textdate = strftime("%Y%m%d",$day);
  308. $xyear = substr($textdate,0,4);
  309. $xday = substr($textdate,6,2);
  310. $i++;
  311. }
  312. // Fabrication tableau 2
  313. $file= $conf->banque->dir_temp."/balance".$account."-".$year.".png";
  314. $title=$langs->transnoentities("Balance").' - '.$langs->transnoentities("Year").': '.$year;
  315. $graph_datas=array();
  316. foreach($datas as $i => $val)
  317. {
  318. if ($acct->min_desired) $graph_datas[$i]=array(isset($labels[$i])?$labels[$i]:'',$datas[$i],$datamin[$i]);
  319. else $graph_datas[$i]=array(isset($labels[$i])?$labels[$i]:'',$datas[$i]);
  320. }
  321. $px = new DolGraph();
  322. $px->SetData($graph_datas);
  323. if ($acct->min_desired) $px->SetLegend(array($langs->transnoentities("Balance"),$langs->transnoentities("BalanceMinimalDesired")));
  324. else $px->SetLegend(array($langs->transnoentities("Balance")));
  325. $px->SetLegendWidthMin(180);
  326. $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue());
  327. $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue());
  328. $px->SetTitle($title);
  329. $px->SetWidth($width);
  330. $px->SetHeight($height);
  331. $px->SetType('lines');
  332. $px->setBgColor('onglet');
  333. $px->setBgColorGrid(array(255,255,255));
  334. $px->SetHideXGrid(true);
  335. //$px->SetHorizTickIncrement(30.41); // 30.41 jours/mois en moyenne
  336. $px->SetPrecisionY(0);
  337. $px->draw($file);
  338. unset($px);
  339. unset($graph_datas);
  340. unset($datas);
  341. unset($datamin);
  342. unset($labels);
  343. unset($amounts);
  344. }
  345. // Tableau 3 - All time line
  346. if ($mode == 'showalltime')
  347. {
  348. // Chargement du tableau $amounts
  349. $amounts = array();
  350. $sql = "SELECT date_format(b.datev,'%Y%m%d')";
  351. $sql.= ", SUM(b.amount)";
  352. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  353. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  354. $sql.= " WHERE b.fk_account = ba.rowid";
  355. $sql.= " AND ba.entity = ".$conf->entity;
  356. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  357. $sql.= " GROUP BY date_format(b.datev,'%Y%m%d')";
  358. $resql = $db->query($sql);
  359. if ($resql)
  360. {
  361. $num = $db->num_rows($resql);
  362. $i = 0;
  363. while ($i < $num)
  364. {
  365. $row = $db->fetch_row($resql);
  366. $amounts[$row[0]] = $row[1];
  367. $i++;
  368. }
  369. }
  370. else
  371. {
  372. dol_print_error($db);
  373. }
  374. // Calcul de $solde avant le debut du graphe
  375. $solde = 0;
  376. // Chargement de labels et datas pour tableau 3
  377. $labels = array();
  378. $datas = array();
  379. $datamin = array();
  380. $subtotal = 0;
  381. $day = $min;
  382. $textdate=strftime("%Y%m%d",$day);
  383. //print "x".$textdate;
  384. $i = 0;
  385. while ($day <= ($max+86400)) // On va au dela du dernier jour
  386. {
  387. $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
  388. //print strftime ("%e %d %m %y",$day)." ".$subtotal."\n<br>";
  389. if ($day > ($max+86400))
  390. {
  391. $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
  392. }
  393. else
  394. {
  395. $datas[$i] = '' + $solde + $subtotal;
  396. }
  397. $datamin[$i] = $acct->min_desired;
  398. if (substr($textdate,6,2) == '01' || $i == 0)
  399. {
  400. $labels[$i] = substr($textdate,4,2);
  401. }
  402. $day += 86400;
  403. $textdate=strftime("%Y%m%d",$day);
  404. $i++;
  405. }
  406. // Fabrication tableau 3
  407. $file= $conf->banque->dir_temp."/balance".$account.".png";
  408. $title=$langs->transnoentities("Balance")." - ".$langs->transnoentities("AllTime");
  409. $graph_datas=array();
  410. foreach($datas as $i => $val)
  411. {
  412. if ($acct->min_desired) $graph_datas[$i]=array(isset($labels[$i])?$labels[$i]:'',$datas[$i],$datamin[$i]);
  413. else $graph_datas[$i]=array(isset($labels[$i])?$labels[$i]:'',$datas[$i]);
  414. }
  415. $px = new DolGraph();
  416. $px->SetData($graph_datas);
  417. if ($acct->min_desired) $px->SetLegend(array($langs->transnoentities("Balance"),$langs->transnoentities("BalanceMinimalDesired")));
  418. else $px->SetLegend(array($langs->transnoentities("Balance")));
  419. $px->SetLegendWidthMin(180);
  420. $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue());
  421. $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue());
  422. $px->SetTitle($title);
  423. $px->SetWidth($width);
  424. $px->SetHeight($height);
  425. $px->SetType('lines');
  426. $px->setBgColor('onglet');
  427. $px->setBgColorGrid(array(255,255,255));
  428. $px->SetPrecisionY(0);
  429. $px->draw($file);
  430. unset($graph_datas);
  431. unset($datas);
  432. unset($datamin);
  433. unset($labels);
  434. unset($amounts);
  435. }
  436. // Tableau 4a - Credit/Debit
  437. if ($mode == 'standard')
  438. {
  439. // Chargement du tableau $credits, $debits
  440. $credits = array();
  441. $debits = array();
  442. $monthnext = $month+1;
  443. $yearnext = $year;
  444. if ($monthnext > 12)
  445. {
  446. $monthnext=1;
  447. $yearnext++;
  448. }
  449. $sql = "SELECT date_format(b.datev,'%d')";
  450. $sql.= ", SUM(b.amount)";
  451. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  452. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  453. $sql.= " WHERE b.fk_account = ba.rowid";
  454. $sql.= " AND ba.entity = ".$conf->entity;
  455. $sql.= " AND b.datev >= '".$year."-".$month."-01 00:00:00'";
  456. $sql.= " AND b.datev < '".$yearnext."-".$monthnext."-01 00:00:00'";
  457. $sql.= " AND b.amount > 0";
  458. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  459. $sql.= " GROUP BY date_format(b.datev,'%d')";
  460. $resql = $db->query($sql);
  461. if ($resql)
  462. {
  463. $num = $db->num_rows($resql);
  464. $i = 0;
  465. while ($i < $num)
  466. {
  467. $row = $db->fetch_row($resql);
  468. $credits[$row[0]] = $row[1];
  469. $i++;
  470. }
  471. $db->free($resql);
  472. }
  473. else
  474. {
  475. dol_print_error($db);
  476. }
  477. $monthnext = $month+1;
  478. $yearnext = $year;
  479. if ($monthnext > 12)
  480. {
  481. $monthnext=1;
  482. $yearnext++;
  483. }
  484. $sql = "SELECT date_format(b.datev,'%d')";
  485. $sql.= ", SUM(b.amount)";
  486. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  487. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  488. $sql.= " WHERE b.fk_account = ba.rowid";
  489. $sql.= " AND ba.entity = ".$conf->entity;
  490. $sql.= " AND b.datev >= '".$year."-".$month."-01 00:00:00'";
  491. $sql.= " AND b.datev < '".$yearnext."-".$monthnext."-01 00:00:00'";
  492. $sql.= " AND b.amount < 0";
  493. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  494. $sql .= " GROUP BY date_format(b.datev,'%d')";
  495. $resql = $db->query($sql);
  496. if ($resql)
  497. {
  498. while ($row = $db->fetch_row($resql))
  499. {
  500. $debits[$row[0]] = abs($row[1]);
  501. }
  502. $db->free($resql);
  503. }
  504. else
  505. {
  506. dol_print_error($db);
  507. }
  508. // Chargement de labels et data_xxx pour tableau 4 Mouvements
  509. $labels = array();
  510. $data_credit = array();
  511. $data_debit = array();
  512. for ($i = 0 ; $i < 31 ; $i++)
  513. {
  514. $data_credit[$i] = isset($credits[substr("0".($i+1),-2)]) ? $credits[substr("0".($i+1),-2)] : 0;
  515. $data_debit[$i] = isset($debits[substr("0".($i+1),-2)]) ? $debits[substr("0".($i+1),-2)] : 0;
  516. $labels[$i] = sprintf("%02d",$i+1);
  517. $datamin[$i] = $acct->min_desired;
  518. }
  519. // Fabrication tableau 4a
  520. $file= $conf->banque->dir_temp."/movement".$account."-".$year.$month.".png";
  521. $title=$langs->transnoentities("BankMovements").' - '.$langs->transnoentities("Month").': '.$month.' '.$langs->transnoentities("Year").': '.$year;
  522. $graph_datas=array();
  523. foreach($data_credit as $i => $val)
  524. {
  525. $graph_datas[$i]=array($labels[$i],$data_credit[$i],$data_debit[$i]);
  526. }
  527. $px = new DolGraph();
  528. $px->SetData($graph_datas);
  529. $px->SetLegend(array($langs->transnoentities("Credit"),$langs->transnoentities("Debit")));
  530. $px->SetLegendWidthMin(180);
  531. $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue());
  532. $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue());
  533. $px->SetTitle($title);
  534. $px->SetWidth($width);
  535. $px->SetHeight($height);
  536. $px->SetType('bars');
  537. $px->SetShading(3);
  538. $px->setBgColor('onglet');
  539. $px->setBgColorGrid(array(255,255,255));
  540. $px->SetHorizTickIncrement(1);
  541. $px->SetPrecisionY(0);
  542. $px->draw($file);
  543. unset($graph_datas);
  544. unset($px);
  545. unset($debits);
  546. unset($credits);
  547. }
  548. // Tableau 4b - Credit/Debit
  549. if ($mode == 'standard')
  550. {
  551. // Chargement du tableau $credits, $debits
  552. $credits = array();
  553. $debits = array();
  554. $sql = "SELECT date_format(b.datev,'%m')";
  555. $sql.= ", SUM(b.amount)";
  556. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  557. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  558. $sql.= " WHERE b.fk_account = ba.rowid";
  559. $sql.= " AND ba.entity = ".$conf->entity;
  560. $sql.= " AND b.datev >= '".$year."-01-01 00:00:00'";
  561. $sql.= " AND b.datev <= '".$year."-12-31 23:59:59'";
  562. $sql.= " AND b.amount > 0";
  563. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  564. $sql .= " GROUP BY date_format(b.datev,'%m');";
  565. $resql = $db->query($sql);
  566. if ($resql)
  567. {
  568. $num = $db->num_rows($resql);
  569. $i = 0;
  570. while ($i < $num)
  571. {
  572. $row = $db->fetch_row($resql);
  573. $credits[$row[0]] = $row[1];
  574. $i++;
  575. }
  576. $db->free($resql);
  577. }
  578. else
  579. {
  580. dol_print_error($db);
  581. }
  582. $sql = "SELECT date_format(b.datev,'%m')";
  583. $sql.= ", SUM(b.amount)";
  584. $sql.= " FROM ".MAIN_DB_PREFIX."bank as b";
  585. $sql.= ", ".MAIN_DB_PREFIX."bank_account as ba";
  586. $sql.= " WHERE b.fk_account = ba.rowid";
  587. $sql.= " AND ba.entity = ".$conf->entity;
  588. $sql.= " AND b.datev >= '".$year."-01-01 00:00:00'";
  589. $sql.= " AND b.datev <= '".$year."-12-31 23:59:59'";
  590. $sql.= " AND b.amount < 0";
  591. if ($account && $_GET["option"]!='all') $sql.= " AND b.fk_account IN (".$account.")";
  592. $sql .= " GROUP BY date_format(b.datev,'%m')";
  593. $resql = $db->query($sql);
  594. if ($resql)
  595. {
  596. while ($row = $db->fetch_row($resql))
  597. {
  598. $debits[$row[0]] = abs($row[1]);
  599. }
  600. $db->free($resql);
  601. }
  602. else
  603. {
  604. dol_print_error($db);
  605. }
  606. // Chargement de labels et data_xxx pour tableau 4 Mouvements
  607. $labels = array();
  608. $data_credit = array();
  609. $data_debit = array();
  610. for ($i = 0 ; $i < 12 ; $i++)
  611. {
  612. $data_credit[$i] = isset($credits[substr("0".($i+1),-2)]) ? $credits[substr("0".($i+1),-2)] : 0;
  613. $data_debit[$i] = isset($debits[substr("0".($i+1),-2)]) ? $debits[substr("0".($i+1),-2)] : 0;
  614. $labels[$i] = dol_print_date(dol_mktime(12,0,0,$i+1,1,2000),"%b");
  615. $datamin[$i] = $acct->min_desired;
  616. }
  617. // Fabrication tableau 4b
  618. $file= $conf->banque->dir_temp."/movement".$account."-".$year.".png";
  619. $title=$langs->transnoentities("BankMovements").' - '.$langs->transnoentities("Year").': '.$year;
  620. $graph_datas=array();
  621. foreach($data_credit as $i => $val)
  622. {
  623. $graph_datas[$i]=array($labels[$i],$data_credit[$i],$data_debit[$i]);
  624. }
  625. $px = new DolGraph();
  626. $px->SetData($graph_datas);
  627. $px->SetLegend(array($langs->transnoentities("Credit"),$langs->transnoentities("Debit")));
  628. $px->SetLegendWidthMin(180);
  629. $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue());
  630. $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue());
  631. $px->SetTitle($title);
  632. $px->SetWidth($width);
  633. $px->SetHeight($height);
  634. $px->SetType('bars');
  635. $px->SetShading(3);
  636. $px->setBgColor('onglet');
  637. $px->setBgColorGrid(array(255,255,255));
  638. $px->SetHorizTickIncrement(1);
  639. $px->SetPrecisionY(0);
  640. $px->draw($file);
  641. unset($graph_datas);
  642. unset($px);
  643. unset($debits);
  644. unset($credits);
  645. }
  646. }
  647. // Onglets
  648. $head=bank_prepare_head($acct);
  649. dol_fiche_head($head,'graph',$langs->trans("FinancialAccount"),0,'account');
  650. if ($mesg) print $mesg.'<br>';
  651. print '<table class="border" width="100%">';
  652. // Ref
  653. print '<tr><td valign="top" width="25%">'.$langs->trans("Ref").'</td>';
  654. print '<td colspan="3">';
  655. if ($account)
  656. {
  657. if (! preg_match('/,/',$account))
  658. {
  659. $moreparam='&month='.$month.'&year='.$year.($mode=='showalltime'?'&mode=showalltime':'');
  660. if ($_GET["option"]!='all')
  661. {
  662. $morehtml='<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.'&option=all'.$moreparam.'">'.$langs->trans("ShowAllAccounts").'</a>';
  663. print $form->showrefnav($acct,'ref','',1,'ref','ref','',$moreparam);
  664. }
  665. else
  666. {
  667. $morehtml='<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.$moreparam.'">'.$langs->trans("BackToAccount").'</a>';
  668. print $langs->trans("All");
  669. //print $morehtml;
  670. }
  671. }
  672. else
  673. {
  674. $bankaccount=new Account($db);
  675. $listid=explode(',',$account);
  676. foreach($listid as $key => $id)
  677. {
  678. $bankaccount->fetch($id);
  679. $bankaccount->label=$bankaccount->ref;
  680. print $bankaccount->getNomUrl(1);
  681. if ($key < (count($listid)-1)) print ', ';
  682. }
  683. }
  684. }
  685. else
  686. {
  687. print $langs->trans("All");
  688. }
  689. print '</td></tr>';
  690. // Label
  691. print '<tr><td valign="top">'.$langs->trans("Label").'</td>';
  692. print '<td colspan="3">';
  693. if ($account && $_GET["option"]!='all')
  694. {
  695. print $acct->label;
  696. }
  697. else
  698. {
  699. print $langs->trans("AllAccounts");
  700. }
  701. print '</td></tr>';
  702. print '</table>';
  703. print '<table class="notopnoleftnoright" width="100%">';
  704. // Navigation links
  705. print '<tr><td align="right">'.$morehtml.' &nbsp; &nbsp; ';
  706. if ($mode == 'showalltime')
  707. {
  708. print '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.'">';
  709. print $langs->trans("GoBack");
  710. print '</a>';
  711. }
  712. else
  713. {
  714. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=showalltime&account='.$account.'">';
  715. print $langs->trans("ShowAllTimeBalance");
  716. print '</a>';
  717. }
  718. print '<br><br></td></tr>';
  719. if ($mode == 'standard')
  720. {
  721. $prevyear=$year;$nextyear=$year;
  722. $prevmonth=$month-1;$nextmonth=$month+1;
  723. if ($prevmonth < 1) { $prevmonth=12; $prevyear--; }
  724. if ($nextmonth > 12) { $nextmonth=1; $nextyear++; }
  725. // For month
  726. $lien="<a href='".$_SERVER["PHP_SELF"]."?account=".$account.($_GET["option"]!='all'?'':'&option=all')."&year=".$prevyear."&month=".$prevmonth."'>".img_previous()."</a> ".$langs->trans("Month")." <a href='".$_SERVER["PHP_SELF"]."?account=".$account."&year=".$nextyear."&month=".$nextmonth."'>".img_next()."</a>";
  727. print '<tr><td align="right">'.$lien.'</td></tr>';
  728. print '<tr><td align="center">';
  729. $file = "movement".$account."-".$year.$month.".png";
  730. print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='.$file.'" alt="" title="">';
  731. print '<tr><td align="center">';
  732. $file = "balance".$account."-".$year.$month.".png";
  733. print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='.$file.'" alt="" title="">';
  734. print '</td></tr>';
  735. // For year
  736. $prevyear=$year-1;$nextyear=$year+1;
  737. $lien="<a href='".$_SERVER["PHP_SELF"]."?account=".$account.($_GET["option"]!='all'?'':'&option=all')."&year=".($prevyear)."'>".img_previous()."</a> ".$langs->trans("Year")." <a href='".$_SERVER["PHP_SELF"]."?account=".$account."&year=".($nextyear)."'>".img_next()."</a>";
  738. print '<tr><td align="right">'.$lien.'</td></tr>';
  739. print '<tr><td align="center">';
  740. $file = "movement".$account."-".$year.".png";
  741. print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='.$file.'" alt="" title="">';
  742. print '</td></tr>';
  743. print '<tr><td align="center">';
  744. $file = "balance".$account."-".$year.".png";
  745. print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='.$file.'" alt="" title="">';
  746. print '</td></tr>';
  747. }
  748. if ($mode == 'showalltime')
  749. {
  750. print '<tr><td align="center">';
  751. $file = "balance".$account.".png";
  752. print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='.$file.'" alt="" title="">';
  753. print '</td></tr>';
  754. }
  755. print '</table>';
  756. print "\n</div>\n";
  757. $db->close();
  758. llxFooter();
  759. ?>