graph.php 27 KB

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