annuel.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. 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', 380); // Large for one graph in a smarpthone.
  32. $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height', 160);
  33. $id = GETPOST('account') ?GETPOST('account', 'alpha') : GETPOST('id');
  34. $ref = GETPOST('ref');
  35. // Security check
  36. $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
  37. $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
  38. if ($user->socid) {
  39. $socid = $user->socid;
  40. }
  41. $result = restrictedArea($user, 'banque', $fieldvalue, 'bank_account&bank_account', '', '', $fieldtype);
  42. $year_start = GETPOST('year_start');
  43. $year_current = strftime("%Y", time());
  44. if (!$year_start) {
  45. $year_start = $year_current - 2;
  46. $year_end = $year_current;
  47. } else {
  48. $year_end = $year_start + 2;
  49. }
  50. /*
  51. * View
  52. */
  53. $form = new Form($db);
  54. // Get account informations
  55. $object = new Account($db);
  56. if ($id > 0 && !preg_match('/,/', $id)) { // if for a particular account and not a list
  57. $result = $object->fetch($id);
  58. $id = $object->id;
  59. }
  60. if (!empty($ref)) {
  61. $result = $object->fetch(0, $ref);
  62. $id = $object->id;
  63. }
  64. $annee = '';
  65. $totentrees = array();
  66. $totsorties = array();
  67. $title = $object->ref.' - '.$langs->trans("IOMonthlyReporting");
  68. $helpurl = "";
  69. llxHeader('', $title, $helpurl);
  70. // Ce rapport de tresorerie est base sur llx_bank (car doit inclure les transactions sans facture)
  71. // plutot que sur llx_paiement + llx_paiementfourn
  72. $sql = "SELECT SUM(b.amount)";
  73. $sql .= ", date_format(b.dateo,'%Y-%m') as dm";
  74. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  75. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  76. $sql .= " WHERE b.fk_account = ba.rowid";
  77. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  78. $sql .= " AND b.amount >= 0";
  79. if (!empty($id)) {
  80. $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
  81. }
  82. $sql .= " GROUP BY dm";
  83. $resql = $db->query($sql);
  84. if ($resql) {
  85. $num = $db->num_rows($resql);
  86. $i = 0;
  87. while ($i < $num) {
  88. $row = $db->fetch_row($resql);
  89. $encaiss[$row[1]] = $row[0];
  90. $i++;
  91. }
  92. } else {
  93. dol_print_error($db);
  94. }
  95. $sql = "SELECT SUM(b.amount)";
  96. $sql .= ", date_format(b.dateo,'%Y-%m') as dm";
  97. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  98. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  99. $sql .= " WHERE b.fk_account = ba.rowid";
  100. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  101. $sql .= " AND b.amount <= 0";
  102. if (!empty($id)) {
  103. $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
  104. }
  105. $sql .= " GROUP BY dm";
  106. $resql = $db->query($sql);
  107. if ($resql) {
  108. $num = $db->num_rows($resql);
  109. $i = 0;
  110. while ($i < $num) {
  111. $row = $db->fetch_row($resql);
  112. $decaiss[$row[1]] = -$row[0];
  113. $i++;
  114. }
  115. } else {
  116. dol_print_error($db);
  117. }
  118. // Onglets
  119. $head = bank_prepare_head($object);
  120. print dol_get_fiche_head($head, 'annual', $langs->trans("FinancialAccount"), 0, 'account');
  121. $title = $langs->trans("FinancialAccount")." : ".$object->label;
  122. $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>' : '');
  123. $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  124. $morehtmlref = '';
  125. if (!empty($id)) {
  126. if (!preg_match('/,/', $id)) {
  127. dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
  128. } else {
  129. $bankaccount = new Account($db);
  130. $listid = explode(',', $id);
  131. foreach ($listid as $key => $aId) {
  132. $bankaccount->fetch($aId);
  133. $bankaccount->label = $bankaccount->ref;
  134. print $bankaccount->getNomUrl(1);
  135. if ($key < (count($listid) - 1)) {
  136. print ', ';
  137. }
  138. }
  139. }
  140. } else {
  141. print $langs->trans("AllAccounts");
  142. }
  143. print dol_get_fiche_end();
  144. // Affiche tableau
  145. print load_fiche_titre('', $link, '');
  146. print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
  147. print '<table class="noborder centpercent">';
  148. print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Month").'</td>';
  149. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  150. print '<td align="center" width="20%" colspan="2" class="liste_titre borderrightlight">'.$annee.'</td>';
  151. }
  152. print '</tr>';
  153. print '<tr class="liste_titre">';
  154. print '<td class="liste_titre">&nbsp;</td>';
  155. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  156. print '<td class="liste_titre" align="center">'.$langs->trans("Debit").'</td><td class="liste_titre" align="center">'.$langs->trans("Credit").'</td>';
  157. }
  158. print '</tr>';
  159. for ($mois = 1; $mois < 13; $mois++) {
  160. print '<tr class="oddeven">';
  161. print "<td>".dol_print_date(dol_mktime(1, 1, 1, $mois, 1, 2000), "%B")."</td>";
  162. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  163. $totsorties[$annee] = 0;
  164. $totentrees[$annee] = 0;
  165. $case = sprintf("%04s-%02s", $annee, $mois);
  166. print '<td class="right" width="10%">&nbsp;';
  167. if (isset($decaiss[$case]) && $decaiss[$case] > 0) {
  168. print price($decaiss[$case]);
  169. $totsorties[$annee] += $decaiss[$case];
  170. }
  171. print "</td>";
  172. print '<td class="right borderrightlight" width="10%">&nbsp;';
  173. if (isset($encaiss[$case]) && $encaiss[$case] > 0) {
  174. print price($encaiss[$case]);
  175. $totentrees[$annee] += $encaiss[$case];
  176. }
  177. print "</td>";
  178. }
  179. print '</tr>';
  180. }
  181. // Total debit-credit
  182. print '<tr class="liste_total"><td><b>'.$langs->trans("Total")."</b></td>";
  183. for ($annee = $year_start; $annee <= $year_end; $annee++) {
  184. print '<td class="right nowraponall"><b>'. (isset($totsorties[$annee]) ? price($totsorties[$annee]) : '') .'</b></td>';
  185. print '<td class="right nowraponall"><b>'. (isset($totentrees[$annee]) ? price($totentrees[$annee]) : '') .'</b></td>';
  186. }
  187. print "</tr>\n";
  188. print "</table>";
  189. print "</div>";
  190. print '<br>';
  191. // Current balance
  192. $balance = 0;
  193. $sql = "SELECT SUM(b.amount) as total";
  194. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  195. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  196. $sql .= " WHERE b.fk_account = ba.rowid";
  197. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  198. if (!empty($id)) {
  199. $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
  200. }
  201. $resql = $db->query($sql);
  202. if ($resql) {
  203. $obj = $db->fetch_object($resql);
  204. if ($obj) {
  205. $balance = $obj->total;
  206. }
  207. } else {
  208. dol_print_error($db);
  209. }
  210. print '<table class="noborder centpercent">';
  211. $nbcol = '';
  212. print '<tr class="liste_total"><td><b>'.$langs->trans("CurrentBalance")."</b></td>";
  213. print '<td colspan="'.($nbcol).'" class="right">'.price($balance).'</td>';
  214. print "</tr>\n";
  215. print "</table>";
  216. // BUILDING GRAPHICS
  217. $year = $year_end;
  218. $result = dol_mkdir($conf->bank->dir_temp);
  219. if ($result < 0) {
  220. $langs->load("errors");
  221. $error++;
  222. setEventMessages($langs->trans("ErrorFailedToCreateDir"), null, 'errors');
  223. } else {
  224. // Calcul de $min et $max
  225. $sql = "SELECT MIN(b.datev) as min, MAX(b.datev) as max";
  226. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  227. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  228. $sql .= " WHERE b.fk_account = ba.rowid";
  229. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  230. if ($id && GETPOST("option") != 'all') {
  231. $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
  232. }
  233. $resql = $db->query($sql);
  234. if ($resql) {
  235. $num = $db->num_rows($resql);
  236. $obj = $db->fetch_object($resql);
  237. $min = $db->jdate($obj->min);
  238. $max = $db->jdate($obj->max);
  239. } else {
  240. dol_print_error($db);
  241. }
  242. $log = "graph.php: min=".$min." max=".$max;
  243. dol_syslog($log);
  244. // CRED PART
  245. // Chargement du tableau des années
  246. $tblyear[0] = array();
  247. $tblyear[1] = array();
  248. $tblyear[2] = array();
  249. for ($annee = 0; $annee < 3; $annee++) {
  250. $sql = "SELECT date_format(b.datev,'%m')";
  251. $sql .= ", SUM(b.amount)";
  252. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  253. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  254. $sql .= " WHERE b.fk_account = ba.rowid";
  255. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  256. $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
  257. $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
  258. $sql .= " AND b.amount > 0";
  259. if ($id && GETPOST("option") != 'all') {
  260. $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
  261. }
  262. $sql .= " GROUP BY date_format(b.datev,'%m');";
  263. $resql = $db->query($sql);
  264. if ($resql) {
  265. $num = $db->num_rows($resql);
  266. $i = 0;
  267. while ($i < $num) {
  268. $row = $db->fetch_row($resql);
  269. $tblyear[$annee][$row[0]] = $row[1];
  270. $i++;
  271. }
  272. $db->free($resql);
  273. } else {
  274. dol_print_error($db);
  275. }
  276. }
  277. // Chargement de labels et data_xxx pour tableau 4 Mouvements
  278. $labels = array();
  279. $data_year_0 = array();
  280. $data_year_1 = array();
  281. $data_year_2 = array();
  282. for ($i = 0; $i < 12; $i++) {
  283. $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
  284. $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
  285. $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
  286. $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
  287. $datamin[$i] = 0;
  288. }
  289. // Fabrication tableau 4b
  290. $file = $conf->bank->dir_temp."/credmovement".$id."-".$year.".png";
  291. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/credmovement".$id."-".$year.".png";
  292. $title = $langs->transnoentities("Credit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
  293. $graph_datas = array();
  294. for ($i = 0; $i < 12; $i++) {
  295. $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
  296. }
  297. $px1 = new DolGraph();
  298. $px1->SetData($graph_datas);
  299. $px1->SetLegend(array(($year), ($year - 1), ($year - 2)));
  300. $px1->SetLegendWidthMin(180);
  301. $px1->SetMaxValue($px1->GetCeilMaxValue() < 0 ? 0 : $px1->GetCeilMaxValue());
  302. $px1->SetMinValue($px1->GetFloorMinValue() > 0 ? 0 : $px1->GetFloorMinValue());
  303. $px1->SetTitle($title);
  304. $px1->SetWidth($WIDTH);
  305. $px1->SetHeight($HEIGHT);
  306. $px1->SetType(array('line', 'line', 'line'));
  307. $px1->SetShading(3);
  308. $px1->setBgColor('onglet');
  309. $px1->setBgColorGrid(array(255, 255, 255));
  310. $px1->SetHorizTickIncrement(1);
  311. $px1->draw($file, $fileurl);
  312. $show1 = $px1->show();
  313. unset($graph_datas);
  314. unset($px1);
  315. unset($tblyear[0]);
  316. unset($tblyear[1]);
  317. unset($tblyear[2]);
  318. // DEDBT PART
  319. // Chargement du tableau des années
  320. $tblyear[0] = array();
  321. $tblyear[1] = array();
  322. $tblyear[2] = array();
  323. for ($annee = 0; $annee < 3; $annee++) {
  324. $sql = "SELECT date_format(b.datev,'%m')";
  325. $sql .= ", SUM(b.amount)";
  326. $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
  327. $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
  328. $sql .= " WHERE b.fk_account = ba.rowid";
  329. $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
  330. $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
  331. $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
  332. $sql .= " AND b.amount < 0";
  333. if ($id && GETPOST("option") != 'all') {
  334. $sql .= " AND b.fk_account IN (".$db->sanitize($id).")";
  335. }
  336. $sql .= " GROUP BY date_format(b.datev,'%m');";
  337. $resql = $db->query($sql);
  338. if ($resql) {
  339. $num = $db->num_rows($resql);
  340. $i = 0;
  341. while ($i < $num) {
  342. $row = $db->fetch_row($resql);
  343. $tblyear[$annee][$row[0]] = abs($row[1]);
  344. $i++;
  345. }
  346. $db->free($resql);
  347. } else {
  348. dol_print_error($db);
  349. }
  350. }
  351. // Chargement de labels et data_xxx pour tableau 4 Mouvements
  352. $labels = array();
  353. $data_year_0 = array();
  354. $data_year_1 = array();
  355. $data_year_2 = array();
  356. for ($i = 0; $i < 12; $i++) {
  357. $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
  358. $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
  359. $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
  360. $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
  361. $datamin[$i] = 0;
  362. }
  363. $file = $conf->bank->dir_temp."/debmovement".$id."-".$year.".png";
  364. $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/debmovement".$id."-".$year.".png";
  365. $title = $langs->transnoentities("Debit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
  366. $graph_datas = array();
  367. for ($i = 0; $i < 12; $i++) {
  368. $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
  369. }
  370. $px2 = new DolGraph();
  371. $px2->SetData($graph_datas);
  372. $px2->SetLegend(array(($year), ($year - 1), ($year - 2)));
  373. $px2->SetLegendWidthMin(180);
  374. $px2->SetMaxValue($px2->GetCeilMaxValue() < 0 ? 0 : $px2->GetCeilMaxValue());
  375. $px2->SetMinValue($px2->GetFloorMinValue() > 0 ? 0 : $px2->GetFloorMinValue());
  376. $px2->SetTitle($title);
  377. $px2->SetWidth($WIDTH);
  378. $px2->SetHeight($HEIGHT);
  379. $px2->SetType(array('line', 'line', 'line'));
  380. $px2->SetShading(3);
  381. $px2->setBgColor('onglet');
  382. $px2->setBgColorGrid(array(255, 255, 255));
  383. $px2->SetHorizTickIncrement(1);
  384. $px2->draw($file, $fileurl);
  385. $show2 = $px2->show();
  386. unset($graph_datas);
  387. unset($px2);
  388. unset($tblyear[0]);
  389. unset($tblyear[1]);
  390. unset($tblyear[2]);
  391. 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.
  392. print $show1;
  393. 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.
  394. print $show2;
  395. print '</div></div></div>';
  396. print '<div style="clear:both"></div>';
  397. }
  398. print "\n</div><br>\n";
  399. // End of page
  400. llxFooter();
  401. $db->close();