annuel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. <?php
  2. /* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
  5. * Copyright (C) 2013 Charles-Fr BENKE <charles.fr@benke.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/bank/annuel.php
  22. * \ingroup banque
  23. * \brief Page to report input-output of a bank account
  24. */
  25. // Load Dolibarr environment
  26. require '../../main.inc.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
  30. // Load translation files required by the page
  31. $langs->loadLangs(array('banks', 'categories'));
  32. $WIDTH = DolGraph::getDefaultGraphSizeForStats('width', 380); // Large for one graph in a smarpthone.
  33. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height', 160);
  34. $id = GETPOST('account') ? GETPOST('account', 'alpha') : GETPOST('id');
  35. $ref = GETPOST('ref');
  36. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  37. $hookmanager->initHooks(array('bankannualreport', 'globalcard'));
  38. // Security check
  39. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  40. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  41. if ($user->socid) {
  42. $socid = $user->socid;
  43. }
  44. $result = restrictedArea($user, 'banque', $fieldvalue, 'bank_account&bank_account', '', '', $fieldtype);
  45. $year_start = GETPOST('year_start');
  46. $year_current = strftime("%Y", time());
  47. if (!$year_start) {
  48. $year_start = $year_current - 2;
  49. $year_end = $year_current;
  50. } else {
  51. $year_end = $year_start + 2;
  52. }
  53. /*
  54. * View
  55. */
  56. $form = new Form($db);
  57. // Get account informations
  58. $object = new Account($db);
  59. if ($id > 0 && !preg_match('/,/', $id)) { // if for a particular account and not a list
  60. $result = $object->fetch($id);
  61. $id = $object->id;
  62. }
  63. if (!empty($ref)) {
  64. $result = $object->fetch(0, $ref);
  65. $id = $object->id;
  66. }
  67. $annee = '';
  68. $totentrees = array();
  69. $totsorties = array();
  70. $title = $object->ref.' - '.$langs->trans("IOMonthlyReporting");
  71. $helpurl = "";
  72. llxHeader('', $title, $helpurl);
  73. // Ce rapport de tresorerie est base sur llx_bank (car doit inclure les transactions sans facture)
  74. // plutot que sur llx_paiement + llx_paiementfourn
  75. $sql = "SELECT SUM(b.amount)";
  76. $sql .= ", date_format(b.dateo,'%Y-%m') as dm";
  77. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  78. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  79. $sql .= " WHERE b.fk_account = ba.rowid";
  80. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  81. $sql .= " AND b.amount >= 0";
  82. if (!empty($id)) {
  83. $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
  84. }
  85. $sql .= " GROUP BY dm";
  86. $resql = $db->query($sql);
  87. if ($resql) {
  88. $num = $db->num_rows($resql);
  89. $i = 0;
  90. while ($i < $num) {
  91. $row = $db->fetch_row($resql);
  92. $encaiss[$row[1]] = $row[0];
  93. $i++;
  94. }
  95. } else {
  96. dol_print_error($db);
  97. }
  98. $sql = "SELECT SUM(b.amount)";
  99. $sql .= ", date_format(b.dateo,'%Y-%m') as dm";
  100. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  101. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  102. $sql .= " WHERE b.fk_account = ba.rowid";
  103. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  104. $sql .= " AND b.amount <= 0";
  105. if (!empty($id)) {
  106. $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
  107. }
  108. $sql .= " GROUP BY dm";
  109. $resql = $db->query($sql);
  110. if ($resql) {
  111. $num = $db->num_rows($resql);
  112. $i = 0;
  113. while ($i < $num) {
  114. $row = $db->fetch_row($resql);
  115. $decaiss[$row[1]] = -$row[0];
  116. $i++;
  117. }
  118. } else {
  119. dol_print_error($db);
  120. }
  121. // Onglets
  122. $head = bank_prepare_head($object);
  123. print dol_get_fiche_head($head, 'annual', $langs->trans("FinancialAccount"), 0, 'account');
  124. $title = $langs->trans("FinancialAccount")." : ".$object->label;
  125. $link = ($year_start ? '<a href="'.$_SERVER["PHP_SELF"].'?account='.$object->id.'&year_start='.($year_start - 1).'">'.img_previous('', 'class="valignbottom"')."</a> ".$langs->trans("Year").' <a href="'.$_SERVER["PHP_SELF"].'?account='.$object->id.'&year_start='.($year_start + 1).'">'.img_next('', 'class="valignbottom"').'</a>' : '');
  126. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  127. $morehtmlref = '';
  128. if (!empty($id)) {
  129. if (!preg_match('/,/', $id)) {
  130. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
  131. } else {
  132. $bankaccount = new Account($db);
  133. $listid = explode(',', $id);
  134. foreach ($listid as $key => $aId) {
  135. $bankaccount->fetch($aId);
  136. $bankaccount->label = $bankaccount->ref;
  137. print $bankaccount->getNomUrl(1);
  138. if ($key < (count($listid) - 1)) {
  139. print ', ';
  140. }
  141. }
  142. }
  143. } else {
  144. print $langs->trans("AllAccounts");
  145. }
  146. print dol_get_fiche_end();
  147. // Affiche tableau
  148. print load_fiche_titre('', $link, '');
  149. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  150. print '<table class="noborder centpercent">';
  151. print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Month").'</td>';
  152. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  153. print '<td align="center" width="20%" colspan="2" class="liste_titre borderrightlight">'.$annee.'</td>';
  154. }
  155. print '</tr>';
  156. print '<tr class="liste_titre">';
  157. print '<td class="liste_titre">&nbsp;</td>';
  158. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  159. print '<td class="liste_titre" align="center">'.$langs->trans("Debit").'</td><td class="liste_titre" align="center">'.$langs->trans("Credit").'</td>';
  160. }
  161. print '</tr>';
  162. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  163. $totsorties[$annee] = 0;
  164. $totentrees[$annee] = 0;
  165. }
  166. for ($mois = 1; $mois < 13; $mois++) {
  167. print '<tr class="oddeven">';
  168. print "<td>".dol_print_date(dol_mktime(1, 1, 1, $mois, 1, 2000), "%B")."</td>";
  169. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  170. $case = sprintf("%04s-%02s", $annee, $mois);
  171. print '<td class="right" width="10%">&nbsp;';
  172. if (isset($decaiss[$case]) && $decaiss[$case] > 0) {
  173. print price($decaiss[$case]);
  174. $totsorties[$annee] += $decaiss[$case];
  175. }
  176. print "</td>";
  177. print '<td class="right borderrightlight" width="10%">&nbsp;';
  178. if (isset($encaiss[$case]) && $encaiss[$case] > 0) {
  179. print price($encaiss[$case]);
  180. $totentrees[$annee] += $encaiss[$case];
  181. }
  182. print "</td>";
  183. }
  184. print '</tr>';
  185. }
  186. // Total debit-credit
  187. print '<tr class="liste_total"><td><b>'.$langs->trans("Total")."</b></td>";
  188. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  189. print '<td class="right nowraponall"><b>'. (isset($totsorties[$annee]) ? price($totsorties[$annee]) : '') .'</b></td>';
  190. print '<td class="right nowraponall"><b>'. (isset($totentrees[$annee]) ? price($totentrees[$annee]) : '') .'</b></td>';
  191. }
  192. print "</tr>\n";
  193. print "</table>";
  194. print "</div>";
  195. print '<br>';
  196. // Current balance
  197. $balance = 0;
  198. $sql = "SELECT SUM(b.amount) as total";
  199. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  200. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  201. $sql .= " WHERE b.fk_account = ba.rowid";
  202. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  203. if (!empty($id)) {
  204. $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
  205. }
  206. $resql = $db->query($sql);
  207. if ($resql) {
  208. $obj = $db->fetch_object($resql);
  209. if ($obj) {
  210. $balance = $obj->total;
  211. }
  212. } else {
  213. dol_print_error($db);
  214. }
  215. print '<table class="noborder centpercent">';
  216. $nbcol = '';
  217. print '<tr class="liste_total"><td><b>'.$langs->trans("CurrentBalance")."</b></td>";
  218. print '<td colspan="'.($nbcol).'" class="right">'.price($balance).'</td>';
  219. print "</tr>\n";
  220. print "</table>";
  221. // BUILDING GRAPHICS
  222. $year = $year_end;
  223. $result = dol_mkdir($conf->bank->dir_temp);
  224. if ($result < 0) {
  225. $langs->load("errors");
  226. $error++;
  227. setEventMessages($langs->trans("ErrorFailedToCreateDir"), null, 'errors');
  228. } else {
  229. // Calcul de $min et $max
  230. $sql = "SELECT MIN(b.datev) as min, MAX(b.datev) as max";
  231. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  232. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  233. $sql .= " WHERE b.fk_account = ba.rowid";
  234. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  235. if ($id && GETPOST("option") != 'all') {
  236. $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
  237. }
  238. $resql = $db->query($sql);
  239. if ($resql) {
  240. $num = $db->num_rows($resql);
  241. $obj = $db->fetch_object($resql);
  242. $min = $db->jdate($obj->min);
  243. $max = $db->jdate($obj->max);
  244. } else {
  245. dol_print_error($db);
  246. }
  247. $log = "graph.php: min=".$min." max=".$max;
  248. dol_syslog($log);
  249. // CRED PART
  250. // Chargement du tableau des années
  251. $tblyear[0] = array();
  252. $tblyear[1] = array();
  253. $tblyear[2] = array();
  254. for ($annee = 0; $annee < 3; $annee++) {
  255. $sql = "SELECT date_format(b.datev,'%m')";
  256. $sql .= ", SUM(b.amount)";
  257. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  258. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  259. $sql .= " WHERE b.fk_account = ba.rowid";
  260. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  261. $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
  262. $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
  263. $sql .= " AND b.amount > 0";
  264. if ($id && GETPOST("option") != 'all') {
  265. $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
  266. }
  267. $sql .= " GROUP BY date_format(b.datev,'%m');";
  268. $resql = $db->query($sql);
  269. if ($resql) {
  270. $num = $db->num_rows($resql);
  271. $i = 0;
  272. while ($i < $num) {
  273. $row = $db->fetch_row($resql);
  274. $tblyear[$annee][$row[0]] = $row[1];
  275. $i++;
  276. }
  277. $db->free($resql);
  278. } else {
  279. dol_print_error($db);
  280. }
  281. }
  282. // Chargement de labels et data_xxx pour tableau 4 Mouvements
  283. $labels = array();
  284. $data_year_0 = array();
  285. $data_year_1 = array();
  286. $data_year_2 = array();
  287. for ($i = 0; $i < 12; $i++) {
  288. $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
  289. $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
  290. $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
  291. $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
  292. $datamin[$i] = 0;
  293. }
  294. // Fabrication tableau 4b
  295. $file = $conf->bank->dir_temp."/credmovement".$id."-".$year.".png";
  296. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/credmovement".$id."-".$year.".png";
  297. $title = $langs->transnoentities("Credit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
  298. $graph_datas = array();
  299. for ($i = 0; $i < 12; $i++) {
  300. $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
  301. }
  302. $px1 = new DolGraph();
  303. $px1->SetData($graph_datas);
  304. $px1->SetLegend(array(($year), ($year - 1), ($year - 2)));
  305. $px1->SetLegendWidthMin(180);
  306. $px1->SetMaxValue($px1->GetCeilMaxValue() < 0 ? 0 : $px1->GetCeilMaxValue());
  307. $px1->SetMinValue($px1->GetFloorMinValue() > 0 ? 0 : $px1->GetFloorMinValue());
  308. $px1->SetTitle($title);
  309. $px1->SetWidth($WIDTH);
  310. $px1->SetHeight($HEIGHT);
  311. $px1->SetType(array('line', 'line', 'line'));
  312. $px1->SetShading(3);
  313. $px1->setBgColor('onglet');
  314. $px1->setBgColorGrid(array(255, 255, 255));
  315. $px1->SetHorizTickIncrement(1);
  316. $px1->draw($file, $fileurl);
  317. $show1 = $px1->show();
  318. unset($graph_datas);
  319. unset($px1);
  320. unset($tblyear[0]);
  321. unset($tblyear[1]);
  322. unset($tblyear[2]);
  323. // DEDBT PART
  324. // Chargement du tableau des années
  325. $tblyear[0] = array();
  326. $tblyear[1] = array();
  327. $tblyear[2] = array();
  328. for ($annee = 0; $annee < 3; $annee++) {
  329. $sql = "SELECT date_format(b.datev,'%m')";
  330. $sql .= ", SUM(b.amount)";
  331. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  332. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  333. $sql .= " WHERE b.fk_account = ba.rowid";
  334. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  335. $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
  336. $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
  337. $sql .= " AND b.amount < 0";
  338. if ($id && GETPOST("option") != 'all') {
  339. $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
  340. }
  341. $sql .= " GROUP BY date_format(b.datev,'%m');";
  342. $resql = $db->query($sql);
  343. if ($resql) {
  344. $num = $db->num_rows($resql);
  345. $i = 0;
  346. while ($i < $num) {
  347. $row = $db->fetch_row($resql);
  348. $tblyear[$annee][$row[0]] = abs($row[1]);
  349. $i++;
  350. }
  351. $db->free($resql);
  352. } else {
  353. dol_print_error($db);
  354. }
  355. }
  356. // Chargement de labels et data_xxx pour tableau 4 Mouvements
  357. $labels = array();
  358. $data_year_0 = array();
  359. $data_year_1 = array();
  360. $data_year_2 = array();
  361. for ($i = 0; $i < 12; $i++) {
  362. $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
  363. $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
  364. $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
  365. $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
  366. $datamin[$i] = 0;
  367. }
  368. $file = $conf->bank->dir_temp."/debmovement".$id."-".$year.".png";
  369. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/debmovement".$id."-".$year.".png";
  370. $title = $langs->transnoentities("Debit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
  371. $graph_datas = array();
  372. for ($i = 0; $i < 12; $i++) {
  373. $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
  374. }
  375. $px2 = new DolGraph();
  376. $px2->SetData($graph_datas);
  377. $px2->SetLegend(array(($year), ($year - 1), ($year - 2)));
  378. $px2->SetLegendWidthMin(180);
  379. $px2->SetMaxValue($px2->GetCeilMaxValue() < 0 ? 0 : $px2->GetCeilMaxValue());
  380. $px2->SetMinValue($px2->GetFloorMinValue() > 0 ? 0 : $px2->GetFloorMinValue());
  381. $px2->SetTitle($title);
  382. $px2->SetWidth($WIDTH);
  383. $px2->SetHeight($HEIGHT);
  384. $px2->SetType(array('line', 'line', 'line'));
  385. $px2->SetShading(3);
  386. $px2->setBgColor('onglet');
  387. $px2->setBgColorGrid(array(255, 255, 255));
  388. $px2->SetHorizTickIncrement(1);
  389. $px2->draw($file, $fileurl);
  390. $show2 = $px2->show();
  391. unset($graph_datas);
  392. unset($px2);
  393. unset($tblyear[0]);
  394. unset($tblyear[1]);
  395. unset($tblyear[2]);
  396. print '<div class="fichecenter"><div class="fichehalfleft"><div align="center">'; // do not use class="center" here, it will have no effect for the js graph inside.
  397. print $show1;
  398. print '</div></div><div class="fichehalfright"><div align="center">'; // do not use class="center" here, it will have no effect for the js graph inside.
  399. print $show2;
  400. print '</div></div></div>';
  401. print '<div class="clearboth"></div>';
  402. }
  403. print "\n</div><br>\n";
  404. // End of page
  405. llxFooter();
  406. $db->close();