graph.php 27 KB

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