dolibarr  13.0.2
annuel.php
Go to the documentation of this file.
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 
27 require '../../main.inc.php';
28 require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
31 
32 // Load translation files required by the page
33 $langs->loadLangs(array('banks', 'categories'));
34 
35 $WIDTH = DolGraph::getDefaultGraphSizeForStats('width', 380); // Large for one graph in a smarpthone.
36 $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height', 160);
37 
38 $id = GETPOST('account') ?GETPOST('account', 'alpha') : GETPOST('id');
39 $ref = GETPOST('ref');
40 
41 // Security check
42 $fieldvalue = (!empty($id) ? $id : (!empty($ref) ? $ref : ''));
43 $fieldtype = (!empty($ref) ? 'ref' : 'rowid');
44 if ($user->socid) $socid = $user->socid;
45 $result = restrictedArea($user, 'banque', $fieldvalue, 'bank_account&bank_account', '', '', $fieldtype);
46 
47 $year_start = GETPOST('year_start');
48 $year_current = strftime("%Y", time());
49 if (!$year_start)
50 {
51  $year_start = $year_current - 2;
52  $year_end = $year_current;
53 } else {
54  $year_end = $year_start + 2;
55 }
56 
57 
58 
59 /*
60  * View
61  */
62 
63 $title = $langs->trans("FinancialAccount").' - '.$langs->trans("IOMonthlyReporting");
64 $helpurl = "";
65 llxHeader('', $title, $helpurl);
66 
67 $form = new Form($db);
68 
69 // Get account informations
70 $object = new Account($db);
71 if ($id > 0 && !preg_match('/,/', $id)) // if for a particular account and not a list
72 {
73  $result = $object->fetch($id);
74  $id = $object->id;
75 }
76 if (!empty($ref))
77 {
78  $result = $object->fetch(0, $ref);
79  $id = $object->id;
80 }
81 
82 
83 // Ce rapport de tresorerie est base sur llx_bank (car doit inclure les transactions sans facture)
84 // plutot que sur llx_paiement + llx_paiementfourn
85 
86 $sql = "SELECT SUM(b.amount)";
87 $sql .= ", date_format(b.dateo,'%Y-%m') as dm";
88 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
89 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
90 $sql .= " WHERE b.fk_account = ba.rowid";
91 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
92 $sql .= " AND b.amount >= 0";
93 if (!empty($id))
94  $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
95 $sql .= " GROUP BY dm";
96 
97 $resql = $db->query($sql);
98 if ($resql)
99 {
100  $num = $db->num_rows($resql);
101  $i = 0;
102  while ($i < $num)
103  {
104  $row = $db->fetch_row($resql);
105  $encaiss[$row[1]] = $row[0];
106  $i++;
107  }
108 } else {
109  dol_print_error($db);
110 }
111 
112 $sql = "SELECT SUM(b.amount)";
113 $sql .= ", date_format(b.dateo,'%Y-%m') as dm";
114 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
115 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
116 $sql .= " WHERE b.fk_account = ba.rowid";
117 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
118 $sql .= " AND b.amount <= 0";
119 if (!empty($id))
120  $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
121 $sql .= " GROUP BY dm";
122 
123 $resql = $db->query($sql);
124 if ($resql)
125 {
126  $num = $db->num_rows($resql);
127  $i = 0;
128  while ($i < $num)
129  {
130  $row = $db->fetch_row($resql);
131  $decaiss[$row[1]] = -$row[0];
132  $i++;
133  }
134 } else {
135  dol_print_error($db);
136 }
137 
138 
139 // Onglets
140 $head = bank_prepare_head($object);
141 print dol_get_fiche_head($head, 'annual', $langs->trans("FinancialAccount"), 0, 'account');
142 
143 $title = $langs->trans("FinancialAccount")." : ".$object->label;
144 $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>" : "");
145 
146 $linkback = '<a href="'.DOL_URL_ROOT.'/compta/bank/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
147 
148 
149 if (!empty($id))
150 {
151  if (!preg_match('/,/', $id))
152  {
153  dol_banner_tab($object, 'ref', $linkback, 1, 'ref', 'ref', $morehtmlref, '', 0, '', '', 1);
154  } else {
155  $bankaccount = new Account($db);
156  $listid = explode(',', $id);
157  foreach ($listid as $key => $aId)
158  {
159  $bankaccount->fetch($aId);
160  $bankaccount->label = $bankaccount->ref;
161  print $bankaccount->getNomUrl(1);
162  if ($key < (count($listid) - 1)) print ', ';
163  }
164  }
165 } else {
166  print $langs->trans("AllAccounts");
167 }
168 
170 
171 
172 // Affiche tableau
173 print load_fiche_titre('', $link, '');
174 
175 print '<div class="div-table-responsive">'; // You can use div-table-responsive-no-min if you dont need reserved height for your table
176 print '<table class="noborder centpercent">';
177 
178 print '<tr class="liste_titre"><td class="liste_titre">'.$langs->trans("Month").'</td>';
179 for ($annee = $year_start; $annee <= $year_end; $annee++)
180 {
181  print '<td align="center" width="20%" colspan="2" class="liste_titre borderrightlight">'.$annee.'</td>';
182 }
183 print '</tr>';
184 
185 print '<tr class="liste_titre">';
186 print '<td class="liste_titre">&nbsp;</td>';
187 for ($annee = $year_start; $annee <= $year_end; $annee++)
188 {
189  print '<td class="liste_titre" align="center">'.$langs->trans("Debit").'</td><td class="liste_titre" align="center">'.$langs->trans("Credit").'</td>';
190 }
191 print '</tr>';
192 
193 for ($mois = 1; $mois < 13; $mois++)
194 {
195  print '<tr class="oddeven">';
196  print "<td>".dol_print_date(dol_mktime(1, 1, 1, $mois, 1, 2000), "%B")."</td>";
197  for ($annee = $year_start; $annee <= $year_end; $annee++)
198  {
199  $case = sprintf("%04s-%02s", $annee, $mois);
200 
201  print '<td class="right" width="10%">&nbsp;';
202  if ($decaiss[$case] > 0)
203  {
204  print price($decaiss[$case]);
205  $totsorties[$annee] += $decaiss[$case];
206  }
207  print "</td>";
208 
209  print '<td class="right borderrightlight" width="10%">&nbsp;';
210  if ($encaiss[$case] > 0)
211  {
212  print price($encaiss[$case]);
213  $totentrees[$annee] += $encaiss[$case];
214  }
215  print "</td>";
216  }
217  print '</tr>';
218 }
219 
220 // Total debit-credit
221 print '<tr class="liste_total"><td><b>'.$langs->trans("Total")."</b></td>";
222 for ($annee = $year_start; $annee <= $year_end; $annee++)
223 {
224  print '<td class="right nowraponall"><b>'.price($totsorties[$annee]).'</b></td><td class="right nowraponall"><b>'.price($totentrees[$annee]).'</b></td>';
225 }
226 print "</tr>\n";
227 
228 print "</table>";
229 print "</div>";
230 
231 print '<br>';
232 
233 
234 // Current balance
235 $balance = 0;
236 
237 $sql = "SELECT SUM(b.amount) as total";
238 $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
239 $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
240 $sql .= " WHERE b.fk_account = ba.rowid";
241 $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
242 if (!empty($id))
243  $sql .= " AND b.fk_account IN (".$db->sanitize($db->escape($id)).")";
244 
245 $resql = $db->query($sql);
246 if ($resql)
247 {
248  $obj = $db->fetch_object($resql);
249  if ($obj) $balance = $obj->total;
250 } else {
251  dol_print_error($db);
252 }
253 
254 print '<table class="noborder centpercent">';
255 
256 print '<tr class="liste_total"><td><b>'.$langs->trans("CurrentBalance")."</b></td>";
257 print '<td colspan="'.($nbcol).'" class="right">'.price($balance).'</td>';
258 print "</tr>\n";
259 
260 print "</table>";
261 
262 // BUILDING GRAPHICS
263 
264 $year = $year_end;
265 
266 $result = dol_mkdir($conf->bank->dir_temp);
267 if ($result < 0)
268 {
269  $langs->load("errors");
270  $error++;
271  setEventMessages($langs->trans("ErrorFailedToCreateDir"), null, 'errors');
272 } else {
273  // Calcul de $min et $max
274  $sql = "SELECT MIN(b.datev) as min, MAX(b.datev) as max";
275  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
276  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
277  $sql .= " WHERE b.fk_account = ba.rowid";
278  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
279  if ($id && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$id.")";
280 
281  $resql = $db->query($sql);
282  if ($resql)
283  {
284  $num = $db->num_rows($resql);
285  $obj = $db->fetch_object($resql);
286  $min = $db->jdate($obj->min);
287  $max = $db->jdate($obj->max);
288  } else {
289  dol_print_error($db);
290  }
291  $log = "graph.php: min=".$min." max=".$max;
292  dol_syslog($log);
293 
294  // CRED PART
295  // Chargement du tableau des années
296  $tblyear[0] = array();
297  $tblyear[1] = array();
298  $tblyear[2] = array();
299 
300  for ($annee = 0; $annee < 3; $annee++)
301  {
302  $sql = "SELECT date_format(b.datev,'%m')";
303  $sql .= ", SUM(b.amount)";
304  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
305  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
306  $sql .= " WHERE b.fk_account = ba.rowid";
307  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
308  $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
309  $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
310  $sql .= " AND b.amount > 0";
311  if ($id && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$id.")";
312  $sql .= " GROUP BY date_format(b.datev,'%m');";
313 
314  $resql = $db->query($sql);
315  if ($resql)
316  {
317  $num = $db->num_rows($resql);
318  $i = 0;
319  while ($i < $num)
320  {
321  $row = $db->fetch_row($resql);
322  $tblyear[$annee][$row[0]] = $row[1];
323  $i++;
324  }
325  $db->free($resql);
326  } else {
327  dol_print_error($db);
328  }
329  }
330  // Chargement de labels et data_xxx pour tableau 4 Mouvements
331  $labels = array();
332  $data_year_0 = array();
333  $data_year_1 = array();
334  $data_year_2 = array();
335 
336  for ($i = 0; $i < 12; $i++)
337  {
338  $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
339  $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
340  $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
341  $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
342  $datamin[$i] = 0;
343  }
344 
345  // Fabrication tableau 4b
346  $file = $conf->bank->dir_temp."/credmovement".$id."-".$year.".png";
347  $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/credmovement".$id."-".$year.".png";
348  $title = $langs->transnoentities("Credit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
349  $graph_datas = array();
350  for ($i = 0; $i < 12; $i++)
351  {
352  $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
353  }
354 
355  $px1 = new DolGraph();
356  $px1->SetData($graph_datas);
357  $px1->SetLegend(array(($year), ($year - 1), ($year - 2)));
358  $px1->SetLegendWidthMin(180);
359  $px1->SetMaxValue($px1->GetCeilMaxValue() < 0 ? 0 : $px1->GetCeilMaxValue());
360  $px1->SetMinValue($px1->GetFloorMinValue() > 0 ? 0 : $px1->GetFloorMinValue());
361  $px1->SetTitle($title);
362  $px1->SetWidth($WIDTH);
363  $px1->SetHeight($HEIGHT);
364  $px1->SetType(array('line', 'line', 'line'));
365  $px1->SetShading(3);
366  $px1->setBgColor('onglet');
367  $px1->setBgColorGrid(array(255, 255, 255));
368  $px1->SetHorizTickIncrement(1);
369  $px1->draw($file, $fileurl);
370 
371  $show1 = $px1->show();
372 
373  unset($graph_datas);
374  unset($px1);
375  unset($tblyear[0]);
376  unset($tblyear[1]);
377  unset($tblyear[2]);
378 
379  // DEDBT PART
380  // Chargement du tableau des années
381  $tblyear[0] = array();
382  $tblyear[1] = array();
383  $tblyear[2] = array();
384 
385  for ($annee = 0; $annee < 3; $annee++)
386  {
387  $sql = "SELECT date_format(b.datev,'%m')";
388  $sql .= ", SUM(b.amount)";
389  $sql .= " FROM ".MAIN_DB_PREFIX."bank as b";
390  $sql .= ", ".MAIN_DB_PREFIX."bank_account as ba";
391  $sql .= " WHERE b.fk_account = ba.rowid";
392  $sql .= " AND ba.entity IN (".getEntity('bank_account').")";
393  $sql .= " AND b.datev >= '".($year - $annee)."-01-01 00:00:00'";
394  $sql .= " AND b.datev <= '".($year - $annee)."-12-31 23:59:59'";
395  $sql .= " AND b.amount < 0";
396  if ($id && $_GET["option"] != 'all') $sql .= " AND b.fk_account IN (".$id.")";
397  $sql .= " GROUP BY date_format(b.datev,'%m');";
398 
399  $resql = $db->query($sql);
400  if ($resql)
401  {
402  $num = $db->num_rows($resql);
403  $i = 0;
404  while ($i < $num)
405  {
406  $row = $db->fetch_row($resql);
407  $tblyear[$annee][$row[0]] = abs($row[1]);
408  $i++;
409  }
410  $db->free($resql);
411  } else {
412  dol_print_error($db);
413  }
414  }
415  // Chargement de labels et data_xxx pour tableau 4 Mouvements
416  $labels = array();
417  $data_year_0 = array();
418  $data_year_1 = array();
419  $data_year_2 = array();
420 
421  for ($i = 0; $i < 12; $i++)
422  {
423  $data_year_0[$i] = isset($tblyear[0][substr("0".($i + 1), -2)]) ? $tblyear[0][substr("0".($i + 1), -2)] : 0;
424  $data_year_1[$i] = isset($tblyear[1][substr("0".($i + 1), -2)]) ? $tblyear[1][substr("0".($i + 1), -2)] : 0;
425  $data_year_2[$i] = isset($tblyear[2][substr("0".($i + 1), -2)]) ? $tblyear[2][substr("0".($i + 1), -2)] : 0;
426  $labels[$i] = $langs->transnoentitiesnoconv("MonthVeryShort".sprintf("%02d", $i + 1));
427  $datamin[$i] = 0;
428  }
429 
430  $file = $conf->bank->dir_temp."/debmovement".$id."-".$year.".png";
431  $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=banque_temp&file='."/debmovement".$id."-".$year.".png";
432  $title = $langs->transnoentities("Debit").' - '.$langs->transnoentities("Year").': '.($year - 2).' - '.($year - 1)." - ".$year;
433  $graph_datas = array();
434  for ($i = 0; $i < 12; $i++)
435  {
436  $graph_datas[$i] = array($labels[$i], $data_year_0[$i], $data_year_1[$i], $data_year_2[$i]);
437  }
438 
439  $px2 = new DolGraph();
440  $px2->SetData($graph_datas);
441  $px2->SetLegend(array(($year), ($year - 1), ($year - 2)));
442  $px2->SetLegendWidthMin(180);
443  $px2->SetMaxValue($px2->GetCeilMaxValue() < 0 ? 0 : $px2->GetCeilMaxValue());
444  $px2->SetMinValue($px2->GetFloorMinValue() > 0 ? 0 : $px2->GetFloorMinValue());
445  $px2->SetTitle($title);
446  $px2->SetWidth($WIDTH);
447  $px2->SetHeight($HEIGHT);
448  $px2->SetType(array('line', 'line', 'line'));
449  $px2->SetShading(3);
450  $px2->setBgColor('onglet');
451  $px2->setBgColorGrid(array(255, 255, 255));
452  $px2->SetHorizTickIncrement(1);
453  $px2->draw($file, $fileurl);
454 
455  $show2 = $px2->show();
456 
457  unset($graph_datas);
458  unset($px2);
459  unset($tblyear[0]);
460  unset($tblyear[1]);
461  unset($tblyear[2]);
462 
463  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.
464  print $show1;
465  print '</div></div><div class="fichehalfright"><div class="ficheaddleft"><div align="center">'; // do not use class="center" here, it will have no effect for the js graph inside.
466  print $show2;
467  print '</div></div></div></div>';
468  print '<div style="clear:both"></div>';
469 }
470 
471 
472 print "\n</div><br>\n";
473 
474 // End of page
475 llxFooter();
476 $db->close();
GETPOST($paramname, $check= 'alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm= 'auto', $check=1)
Return a timestamp date built from detailed informations (by default a local PHP server timestamp) Re...
bank_prepare_head(Account $object)
Prepare array with list of tabs.
Definition: bank.lib.php:35
Class to manage bank accounts.
price($amount, $form=0, $outlangs= '', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code= '')
Function to format a value into an amount for visual output Function used into PDF and HTML pages...
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
Class to manage generation of HTML components Only common components must be here.
load_fiche_titre($titre, $morehtmlright= '', $picto= 'generic', $pictoisfullpath=0, $id= '', $morecssontable= '', $morehtmlcenter= '')
Load a title with picto.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
restrictedArea($user, $features, $objectid=0, $tableandshare= '', $feature2= '', $dbt_keyfield= 'fk_soc', $dbt_select= 'rowid', $isdraft=0)
Check permissions of a user to show a page and an object.
print $_SERVER["PHP_SELF"]
Edit parameters.
img_next($titlealt= 'default', $moreatt= '')
Show next logo.
dol_get_fiche_head($links=array(), $active= '', $title= '', $notab=0, $picto= '', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limittoshow=0, $moretabssuffix= '')
Show tabs of a record.
print
Draft customers invoices.
Definition: index.php:89
img_previous($titlealt= 'default', $moreatt= '')
Show previous logo.
Class to build graphs.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_get_fiche_end($notab=0)
Return tab footer of a card.
static getDefaultGraphSizeForStats($direction, $defaultsize= '')
getDefaultGraphSizeForStats
dol_banner_tab($object, $paramid, $morehtml= '', $shownav=1, $fieldid= 'rowid', $fieldref= 'ref', $morehtmlref= '', $moreparam= '', $nodbprefix=0, $morehtmlleft= '', $morehtmlstatus= '', $onlybanner=0, $morehtmlright= '')
Show tab footer of a card.
llxFooter()
Empty footer.
Definition: wrapper.php:59
dol_mkdir($dir, $dataroot= '', $newmask=null)
Creation of a directory (this can create recursive subdir)