graph.php 25 KB

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