graph.php 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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. /**
  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->socid) $socid = $user->socid;
  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. } else {
  74. // Calcul $min and $max
  75. $sql = "SELECT MIN(b.datev) as min, MAX(b.datev) as max";
  76. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  77. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  78. $sql .= " WHERE b.fk_account = ba.rowid";
  79. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  80. if ($account && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$account.")";
  81. $resql = $db->query($sql);
  82. if ($resql)
  83. {
  84. $num = $db->num_rows($resql);
  85. $obj = $db->fetch_object($resql);
  86. $min = $db->jdate($obj->min);
  87. $max = $db->jdate($obj->max);
  88. } else {
  89. dol_print_error($db);
  90. }
  91. if (empty($min)) $min = dol_now() - 3600 * 24;
  92. $log = "graph.php: min=".$min." max=".$max;
  93. dol_syslog($log);
  94. // Tableau 1
  95. if ($mode == 'standard')
  96. {
  97. // Loading table $amounts
  98. $amounts = array();
  99. $monthnext = $month + 1;
  100. $yearnext = $year;
  101. if ($monthnext > 12)
  102. {
  103. $monthnext = 1;
  104. $yearnext++;
  105. }
  106. $sql = "SELECT date_format(b.datev,'%Y%m%d')";
  107. $sql .= ", SUM(b.amount)";
  108. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  109. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  110. $sql .= " WHERE b.fk_account = ba.rowid";
  111. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  112. $sql .= " AND b.datev >= '".$db->escape($year)."-".$db->escape($month)."-01 00:00:00'";
  113. $sql .= " AND b.datev < '".$db->escape($yearnext)."-".$db->escape($monthnext)."-01 00:00:00'";
  114. if ($account && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$account.")";
  115. $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
  116. $resql = $db->query($sql);
  117. if ($resql)
  118. {
  119. $num = $db->num_rows($resql);
  120. $i = 0;
  121. while ($i < $num)
  122. {
  123. $row = $db->fetch_row($resql);
  124. $amounts[$row[0]] = $row[1];
  125. $i++;
  126. }
  127. $db->free($resql);
  128. } else {
  129. dol_print_error($db);
  130. }
  131. // Calculation of $solde before the start of the graph
  132. $solde = 0;
  133. $sql = "SELECT SUM(b.amount)";
  134. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  135. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  136. $sql .= " WHERE b.fk_account = ba.rowid";
  137. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  138. $sql .= " AND b.datev < '".$db->escape($year)."-".sprintf("%02s", $month)."-01'";
  139. if ($account && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$account.")";
  140. $resql = $db->query($sql);
  141. if ($resql)
  142. {
  143. $row = $db->fetch_row($resql);
  144. $solde = $row[0];
  145. $db->free($resql);
  146. } else {
  147. dol_print_error($db);
  148. }
  149. // Chargement de labels et datas pour tableau 1
  150. $labels = array();
  151. $datas = array();
  152. $datamin = array();
  153. $subtotal = 0;
  154. $day = dol_mktime(12, 0, 0, $month, 1, $year);
  155. $textdate = strftime("%Y%m%d", $day);
  156. $xyear = substr($textdate, 0, 4);
  157. $xday = substr($textdate, 6, 2);
  158. $xmonth = substr($textdate, 4, 2);
  159. $i = 0;
  160. while ($xmonth == $month)
  161. {
  162. $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
  163. if ($day > time())
  164. {
  165. $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
  166. } else {
  167. $datas[$i] = $solde + $subtotal;
  168. }
  169. $datamin[$i] = $object->min_desired;
  170. $dataall[$i] = $object->min_allowed;
  171. //$labels[$i] = strftime("%d",$day);
  172. $labels[$i] = $xday;
  173. $day += 86400;
  174. $textdate = strftime("%Y%m%d", $day);
  175. $xyear = substr($textdate, 0, 4);
  176. $xday = substr($textdate, 6, 2);
  177. $xmonth = substr($textdate, 4, 2);
  178. $i++;
  179. }
  180. // If we are the first of month, only $datas[0] is defined to an int value, others are defined to ""
  181. // and this may make graph lib report a warning.
  182. //$datas[0]=100; KO
  183. //$datas[0]=100; $datas[1]=90; OK
  184. //var_dump($datas);
  185. //exit;
  186. // Fabrication tableau 1
  187. $file = $conf->bank->dir_temp."/balance".$account."-".$year.$month.".png";
  188. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account."-".$year.$month.".png";
  189. $title = $langs->transnoentities("Balance").' - '.$langs->transnoentities("Month").': '.$month.' '.$langs->transnoentities("Year").': '.$year;
  190. $graph_datas = array();
  191. foreach ($datas as $i => $val)
  192. {
  193. $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
  194. if ($object->min_desired) array_push($graph_datas[$i], $datamin[$i]);
  195. if ($object->min_allowed) array_push($graph_datas[$i], $dataall[$i]);
  196. }
  197. $px1 = new DolGraph();
  198. $px1->SetData($graph_datas);
  199. $arraylegends = array($langs->transnoentities("Balance"));
  200. if ($object->min_desired) array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
  201. if ($object->min_allowed) array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
  202. $px1->SetLegend($arraylegends);
  203. $px1->SetLegendWidthMin(180);
  204. $px1->SetMaxValue($px1->GetCeilMaxValue() < 0 ? 0 : $px1->GetCeilMaxValue());
  205. $px1->SetMinValue($px1->GetFloorMinValue() > 0 ? 0 : $px1->GetFloorMinValue());
  206. $px1->SetTitle($title);
  207. $px1->SetWidth($WIDTH);
  208. $px1->SetHeight($HEIGHT);
  209. $px1->SetType(array('lines', 'lines', 'lines'));
  210. $px1->setBgColor('onglet');
  211. $px1->setBgColorGrid(array(255, 255, 255));
  212. $px1->SetHorizTickIncrement(1);
  213. $px1->draw($file, $fileurl);
  214. $show1 = $px1->show();
  215. $px1 = null;
  216. $graph_datas = null;
  217. $datas = null;
  218. $datamin = null;
  219. $dataall = null;
  220. $labels = null;
  221. $amounts = null;
  222. }
  223. // Graph Balance for the year
  224. if ($mode == 'standard')
  225. {
  226. // Loading table $amounts
  227. $amounts = array();
  228. $sql = "SELECT date_format(b.datev,'%Y%m%d')";
  229. $sql .= ", SUM(b.amount)";
  230. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  231. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  232. $sql .= " WHERE b.fk_account = ba.rowid";
  233. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  234. $sql .= " AND b.datev >= '".$db->escape($year)."-01-01 00:00:00'";
  235. $sql .= " AND b.datev <= '".$db->escape($year)."-12-31 23:59:59'";
  236. if ($account && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$account.")";
  237. $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
  238. $resql = $db->query($sql);
  239. if ($resql)
  240. {
  241. $num = $db->num_rows($resql);
  242. $i = 0;
  243. while ($i < $num)
  244. {
  245. $row = $db->fetch_row($resql);
  246. $amounts[$row[0]] = $row[1];
  247. $i++;
  248. }
  249. $db->free($resql);
  250. } else {
  251. dol_print_error($db);
  252. }
  253. // Calculation of $solde before the start of the graph
  254. $solde = 0;
  255. $sql = "SELECT SUM(b.amount)";
  256. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  257. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  258. $sql .= " WHERE b.fk_account = ba.rowid";
  259. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  260. $sql .= " AND b.datev < '".$db->escape($year)."-01-01'";
  261. if ($account && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$account.")";
  262. $resql = $db->query($sql);
  263. if ($resql)
  264. {
  265. $row = $db->fetch_row($resql);
  266. $solde = $row[0];
  267. $db->free($resql);
  268. } else {
  269. dol_print_error($db);
  270. }
  271. // Chargement de labels et datas pour tableau 2
  272. $labels = array();
  273. $datas = array();
  274. $datamin = array();
  275. $dataall = array();
  276. $subtotal = 0;
  277. $now = time();
  278. $day = dol_mktime(12, 0, 0, 1, 1, $year);
  279. $textdate = strftime("%Y%m%d", $day);
  280. $xyear = substr($textdate, 0, 4);
  281. $xday = substr($textdate, 6, 2);
  282. $i = 0;
  283. while ($xyear == $year && $day <= $datetime)
  284. {
  285. $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
  286. if ($day > $now)
  287. {
  288. $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
  289. } else {
  290. $datas[$i] = $solde + $subtotal;
  291. }
  292. $datamin[$i] = $object->min_desired;
  293. $dataall[$i] = $object->min_allowed;
  294. /*if ($xday == '15') // Set only some label for jflot
  295. {
  296. $labels[$i] = dol_print_date($day, "%b");
  297. }*/
  298. $labels[$i] = dol_print_date($day, "%Y%m");
  299. $day += 86400;
  300. $textdate = strftime("%Y%m%d", $day);
  301. $xyear = substr($textdate, 0, 4);
  302. $xday = substr($textdate, 6, 2);
  303. $i++;
  304. }
  305. // Fabrication tableau 2
  306. $file = $conf->bank->dir_temp."/balance".$account."-".$year.".png";
  307. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account."-".$year.".png";
  308. $title = $langs->transnoentities("Balance").' - '.$langs->transnoentities("Year").': '.$year;
  309. $graph_datas = array();
  310. foreach ($datas as $i => $val)
  311. {
  312. $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
  313. if ($object->min_desired) array_push($graph_datas[$i], $datamin[$i]);
  314. if ($object->min_allowed) array_push($graph_datas[$i], $dataall[$i]);
  315. }
  316. $px2 = new DolGraph();
  317. $px2->SetData($graph_datas);
  318. $arraylegends = array($langs->transnoentities("Balance"));
  319. if ($object->min_desired) array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
  320. if ($object->min_allowed) array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
  321. $px2->SetLegend($arraylegends);
  322. $px2->SetLegendWidthMin(180);
  323. $px2->SetMaxValue($px2->GetCeilMaxValue() < 0 ? 0 : $px2->GetCeilMaxValue());
  324. $px2->SetMinValue($px2->GetFloorMinValue() > 0 ? 0 : $px2->GetFloorMinValue());
  325. $px2->SetTitle($title);
  326. $px2->SetWidth($WIDTH);
  327. $px2->SetHeight($HEIGHT);
  328. $px2->SetType(array('linesnopoint', 'linesnopoint', 'linesnopoint'));
  329. $px2->setBgColor('onglet');
  330. $px2->setBgColorGrid(array(255, 255, 255));
  331. $px2->SetHideXGrid(true);
  332. //$px2->SetHorizTickIncrement(30.41); // 30.41 jours/mois en moyenne
  333. $px2->draw($file, $fileurl);
  334. $show2 = $px2->show();
  335. $px2 = null;
  336. $graph_datas = null;
  337. $datas = null;
  338. $datamin = null;
  339. $dataall = null;
  340. $labels = null;
  341. $amounts = null;
  342. }
  343. // Graph 3 - Balance for all time line
  344. if ($mode == 'showalltime')
  345. {
  346. // Loading table $amounts
  347. $amounts = array();
  348. $sql = "SELECT date_format(b.datev,'%Y%m%d')";
  349. $sql .= ", SUM(b.amount)";
  350. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  351. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  352. $sql .= " WHERE b.fk_account = ba.rowid";
  353. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  354. if ($account && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$account.")";
  355. $sql .= " GROUP BY date_format(b.datev,'%Y%m%d')";
  356. $resql = $db->query($sql);
  357. if ($resql)
  358. {
  359. $num = $db->num_rows($resql);
  360. $i = 0;
  361. while ($i < $num)
  362. {
  363. $row = $db->fetch_row($resql);
  364. $amounts[$row[0]] = $row[1];
  365. $i++;
  366. }
  367. } else {
  368. dol_print_error($db);
  369. }
  370. // Calcul de $solde avant le debut du graphe
  371. $solde = 0;
  372. // Chargement de labels et datas pour tableau 3
  373. $labels = array();
  374. $datas = array();
  375. $datamin = array();
  376. $dataall = array();
  377. $subtotal = 0;
  378. $day = $min;
  379. $textdate = strftime("%Y%m%d", $day);
  380. //print "x".$textdate;
  381. $i = 0;
  382. while ($day <= ($max + 86400)) // On va au dela du dernier jour
  383. {
  384. $subtotal = $subtotal + (isset($amounts[$textdate]) ? $amounts[$textdate] : 0);
  385. //print strftime ("%e %d %m %y",$day)." ".$subtotal."\n<br>";
  386. if ($day > ($max + 86400))
  387. {
  388. $datas[$i] = ''; // Valeur speciale permettant de ne pas tracer le graph
  389. } else {
  390. $datas[$i] = 0 + $solde + $subtotal;
  391. }
  392. $datamin[$i] = $object->min_desired;
  393. $dataall[$i] = $object->min_allowed;
  394. /*if (substr($textdate, 6, 2) == '01' || $i == 0) // Set only few label for jflot
  395. {
  396. $labels[$i] = substr($textdate, 0, 6);
  397. }*/
  398. $labels[$i] = substr($textdate, 0, 6);
  399. $day += 86400;
  400. $textdate = strftime("%Y%m%d", $day);
  401. $i++;
  402. }
  403. // Fabrication tableau 3
  404. $file = $conf->bank->dir_temp."/balance".$account.".png";
  405. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/balance".$account.".png";
  406. $title = $langs->transnoentities("Balance")." - ".$langs->transnoentities("AllTime");
  407. $graph_datas = array();
  408. foreach ($datas as $i => $val)
  409. {
  410. $graph_datas[$i] = array(isset($labels[$i]) ? $labels[$i] : '', $datas[$i]);
  411. if ($object->min_desired) array_push($graph_datas[$i], $datamin[$i]);
  412. if ($object->min_allowed) array_push($graph_datas[$i], $dataall[$i]);
  413. }
  414. $px3 = new DolGraph();
  415. $px3->SetData($graph_datas);
  416. $arraylegends = array($langs->transnoentities("Balance"));
  417. if ($object->min_desired) array_push($arraylegends, $langs->transnoentities("BalanceMinimalDesired"));
  418. if ($object->min_allowed) array_push($arraylegends, $langs->transnoentities("BalanceMinimalAllowed"));
  419. $px3->SetLegend($arraylegends);
  420. $px3->SetLegendWidthMin(180);
  421. $px3->SetMaxValue($px3->GetCeilMaxValue() < 0 ? 0 : $px3->GetCeilMaxValue());
  422. $px3->SetMinValue($px3->GetFloorMinValue() > 0 ? 0 : $px3->GetFloorMinValue());
  423. $px3->SetTitle($title);
  424. $px3->SetWidth($WIDTH);
  425. $px3->SetHeight($HEIGHT);
  426. $px3->SetType(array('linesnopoint', 'linesnopoint', 'linesnopoint'));
  427. $px3->setBgColor('onglet');
  428. $px3->setBgColorGrid(array(255, 255, 255));
  429. $px3->draw($file, $fileurl);
  430. $show3 = $px3->show();
  431. $px3 = null;
  432. $graph_datas = null;
  433. $datas = null;
  434. $datamin = null;
  435. $dataall = null;
  436. $labels = null;
  437. $amounts = null;
  438. }
  439. // Tableau 4a - Credit/Debit
  440. if ($mode == 'standard')
  441. {
  442. // Chargement du tableau $credits, $debits
  443. $credits = array();
  444. $debits = array();
  445. $monthnext = $month + 1;
  446. $yearnext = $year;
  447. if ($monthnext > 12)
  448. {
  449. $monthnext = 1;
  450. $yearnext++;
  451. }
  452. $sql = "SELECT date_format(b.datev,'%d')";
  453. $sql .= ", SUM(b.amount)";
  454. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  455. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  456. $sql .= " WHERE b.fk_account = ba.rowid";
  457. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  458. $sql .= " AND b.datev >= '".$db->escape($year)."-".$db->escape($month)."-01 00:00:00'";
  459. $sql .= " AND b.datev < '".$db->escape($yearnext)."-".$db->escape($monthnext)."-01 00:00:00'";
  460. $sql .= " AND b.amount > 0";
  461. if ($account && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$account.")";
  462. $sql .= " GROUP BY date_format(b.datev,'%d')";
  463. $resql = $db->query($sql);
  464. if ($resql)
  465. {
  466. $num = $db->num_rows($resql);
  467. $i = 0;
  468. while ($i < $num)
  469. {
  470. $row = $db->fetch_row($resql);
  471. $credits[$row[0]] = $row[1];
  472. $i++;
  473. }
  474. $db->free($resql);
  475. } else {
  476. dol_print_error($db);
  477. }
  478. $monthnext = $month + 1;
  479. $yearnext = $year;
  480. if ($monthnext > 12)
  481. {
  482. $monthnext = 1;
  483. $yearnext++;
  484. }
  485. $sql = "SELECT date_format(b.datev,'%d')";
  486. $sql .= ", SUM(b.amount)";
  487. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  488. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  489. $sql .= " WHERE b.fk_account = ba.rowid";
  490. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  491. $sql .= " AND b.datev >= '".$db->escape($year)."-".$db->escape($month)."-01 00:00:00'";
  492. $sql .= " AND b.datev < '".$db->escape($yearnext)."-".$db->escape($monthnext)."-01 00:00:00'";
  493. $sql .= " AND b.amount < 0";
  494. if ($account && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$account.")";
  495. $sql .= " GROUP BY date_format(b.datev,'%d')";
  496. $resql = $db->query($sql);
  497. if ($resql)
  498. {
  499. while ($row = $db->fetch_row($resql))
  500. {
  501. $debits[$row[0]] = abs($row[1]);
  502. }
  503. $db->free($resql);
  504. } else {
  505. dol_print_error($db);
  506. }
  507. // Chargement de labels et data_xxx pour tableau 4 Mouvements
  508. $labels = array();
  509. $data_credit = array();
  510. $data_debit = array();
  511. for ($i = 0; $i < 31; $i++)
  512. {
  513. $data_credit[$i] = isset($credits[substr("0".($i + 1), -2)]) ? $credits[substr("0".($i + 1), -2)] : 0;
  514. $data_debit[$i] = isset($debits[substr("0".($i + 1), -2)]) ? $debits[substr("0".($i + 1), -2)] : 0;
  515. $labels[$i] = sprintf("%02d", $i + 1);
  516. $datamin[$i] = $object->min_desired;
  517. }
  518. // Fabrication tableau 4a
  519. $file = $conf->bank->dir_temp."/movement".$account."-".$year.$month.".png";
  520. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/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. $px4 = new DolGraph();
  528. $px4->SetData($graph_datas);
  529. $px4->SetLegend(array($langs->transnoentities("Credit"), $langs->transnoentities("Debit")));
  530. $px4->SetLegendWidthMin(180);
  531. $px4->SetMaxValue($px4->GetCeilMaxValue() < 0 ? 0 : $px4->GetCeilMaxValue());
  532. $px4->SetMinValue($px4->GetFloorMinValue() > 0 ? 0 : $px4->GetFloorMinValue());
  533. $px4->SetTitle($title);
  534. $px4->SetWidth($WIDTH);
  535. $px4->SetHeight($HEIGHT);
  536. $px4->SetType(array('bars', 'bars'));
  537. $px4->SetShading(3);
  538. $px4->setBgColor('onglet');
  539. $px4->setBgColorGrid(array(255, 255, 255));
  540. $px4->SetHorizTickIncrement(1);
  541. $px4->draw($file, $fileurl);
  542. $show4 = $px4->show();
  543. $px4 = null;
  544. $graph_datas = null;
  545. $debits = null;
  546. $credits = null;
  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 IN (".getEntity('bank_account').")";
  560. $sql .= " AND b.datev >= '".$db->escape($year)."-01-01 00:00:00'";
  561. $sql .= " AND b.datev <= '".$db->escape($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. } else {
  578. dol_print_error($db);
  579. }
  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 >= '".$db->escape($year)."-01-01 00:00:00'";
  587. $sql .= " AND b.datev <= '".$db->escape($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. while ($row = $db->fetch_row($resql))
  595. {
  596. $debits[$row[0]] = abs($row[1]);
  597. }
  598. $db->free($resql);
  599. } else {
  600. dol_print_error($db);
  601. }
  602. // Chargement de labels et data_xxx pour tableau 4 Mouvements
  603. $labels = array();
  604. $data_credit = array();
  605. $data_debit = array();
  606. for ($i = 0; $i < 12; $i++)
  607. {
  608. $data_credit[$i] = isset($credits[substr("0".($i + 1), -2)]) ? $credits[substr("0".($i + 1), -2)] : 0;
  609. $data_debit[$i] = isset($debits[substr("0".($i + 1), -2)]) ? $debits[substr("0".($i + 1), -2)] : 0;
  610. $labels[$i] = dol_print_date(dol_mktime(12, 0, 0, $i + 1, 1, 2000), "%b");
  611. $datamin[$i] = $object->min_desired;
  612. }
  613. // Fabrication tableau 4b
  614. $file = $conf->bank->dir_temp."/movement".$account."-".$year.".png";
  615. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/movement".$account."-".$year.".png";
  616. $title = $langs->transnoentities("BankMovements").' - '.$langs->transnoentities("Year").': '.$year;
  617. $graph_datas = array();
  618. foreach ($data_credit as $i => $val)
  619. {
  620. $graph_datas[$i] = array($labels[$i], $data_credit[$i], $data_debit[$i]);
  621. }
  622. $px5 = new DolGraph();
  623. $px5->SetData($graph_datas);
  624. $px5->SetLegend(array($langs->transnoentities("Credit"), $langs->transnoentities("Debit")));
  625. $px5->SetLegendWidthMin(180);
  626. $px5->SetMaxValue($px5->GetCeilMaxValue() < 0 ? 0 : $px5->GetCeilMaxValue());
  627. $px5->SetMinValue($px5->GetFloorMinValue() > 0 ? 0 : $px5->GetFloorMinValue());
  628. $px5->SetTitle($title);
  629. $px5->SetWidth($WIDTH);
  630. $px5->SetHeight($HEIGHT);
  631. $px5->SetType(array('bars', 'bars'));
  632. $px5->SetShading(3);
  633. $px5->setBgColor('onglet');
  634. $px5->setBgColorGrid(array(255, 255, 255));
  635. $px5->SetHorizTickIncrement(1);
  636. $px5->draw($file, $fileurl);
  637. $show5 = $px5->show();
  638. $px5 = null;
  639. $graph_datas = null;
  640. $debits = null;
  641. $credits = null;
  642. }
  643. }
  644. // Onglets
  645. $head = bank_prepare_head($object);
  646. print dol_get_fiche_head($head, 'graph', $langs->trans("FinancialAccount"), 0, 'account');
  647. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  648. if ($account)
  649. {
  650. if (!preg_match('/,/', $account))
  651. {
  652. $moreparam = '&month='.$month.'&year='.$year.($mode == 'showalltime' ? '&mode=showalltime' : '');
  653. if ($_GET["option"] != 'all')
  654. {
  655. $morehtml = '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.'&option=all'.$moreparam.'">'.$langs->trans("ShowAllAccounts").'</a>';
  656. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', '', $moreparam, 0, '', '', 1);
  657. } else {
  658. $morehtml = '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.$moreparam.'">'.$langs->trans("BackToAccount").'</a>';
  659. print $langs->trans("AllAccounts");
  660. //print $morehtml;
  661. }
  662. } else {
  663. $bankaccount = new Account($db);
  664. $listid = explode(',', $account);
  665. foreach ($listid as $key => $id)
  666. {
  667. $bankaccount->fetch($id);
  668. $bankaccount->label = $bankaccount->ref;
  669. print $bankaccount->getNomUrl(1);
  670. if ($key < (count($listid) - 1)) print ', ';
  671. }
  672. }
  673. } else {
  674. print $langs->trans("AllAccounts");
  675. }
  676. print dol_get_fiche_end();
  677. print '<table class="notopnoleftnoright" width="100%">';
  678. // Navigation links
  679. print '<tr><td class="right">'.$morehtml.' &nbsp; &nbsp; ';
  680. if ($mode == 'showalltime')
  681. {
  682. print '<a href="'.$_SERVER["PHP_SELF"].'?account='.$account.'">';
  683. print $langs->trans("GoBack");
  684. print '</a>';
  685. } else {
  686. print '<a href="'.$_SERVER["PHP_SELF"].'?mode=showalltime&account='.$account.'">';
  687. print $langs->trans("ShowAllTimeBalance");
  688. print '</a>';
  689. }
  690. print '<br><br></td></tr>';
  691. print '</table>';
  692. // Graphs
  693. if ($mode == 'standard')
  694. {
  695. $prevyear = $year; $nextyear = $year;
  696. $prevmonth = $month - 1; $nextmonth = $month + 1;
  697. if ($prevmonth < 1) { $prevmonth = 12; $prevyear--; }
  698. if ($nextmonth > 12) { $nextmonth = 1; $nextyear++; }
  699. // For month
  700. $link = "<a href='".$_SERVER["PHP_SELF"]."?account=".$account."&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>";
  701. print '<div class="right clearboth">'.$link.'</div>';
  702. print '<div class="center clearboth margintoponly">';
  703. $file = "movement".$account."-".$year.$month.".png";
  704. print $show4;
  705. print '</div>';
  706. print '<div class="center clearboth margintoponly">';
  707. print $show1;
  708. print '</div>';
  709. // For year
  710. $prevyear = $year - 1; $nextyear = $year + 1;
  711. $link = "<a href='".$_SERVER["PHP_SELF"]."?account=".$account."&year=".($prevyear)."'>".img_previous('', 'class="valignbottom"')."</a> ".$langs->trans("Year")." <a href='".$_SERVER["PHP_SELF"]."?account=".$account."&year=".($nextyear)."'>".img_next('', 'class="valignbottom"')."</a>";
  712. print '<div class="right clearboth margintoponly">'.$link.'</div>';
  713. print '<div class="center clearboth margintoponly">';
  714. print $show5;
  715. print '</div>';
  716. print '<div class="center clearboth margintoponly">';
  717. print $show2;
  718. print '</div>';
  719. }
  720. if ($mode == 'showalltime')
  721. {
  722. print '<div class="center clearboth margintoponly">';
  723. print $show3;
  724. print '</div>';
  725. }
  726. // End of page
  727. llxFooter();
  728. $db->close();