dolibarr  13.0.2
index.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013-2014 Olivier Geffroy <jeff@jeffinfo.com>
3  * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
4  * Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro@open-dsi.fr>
5  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
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/date.lib.php';
29 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
30 require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
31 
32 // Load translation files required by the page
33 $langs->loadLangs(array("compta", "bills", "other", "main", "accountancy"));
34 
35 // Security check
36 if (empty($conf->accounting->enabled)) {
38 }
39 if ($user->socid > 0)
41 if (!$user->rights->accounting->bind->write)
43 
44 $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
45 if (GETPOST("year", 'int')) $year_start = GETPOST("year", 'int');
46 else {
47  $year_start = dol_print_date(dol_now(), '%Y');
48  if (dol_print_date(dol_now(), '%m') < $month_start) $year_start--; // If current month is lower that starting fiscal month, we start last year
49 }
50 $year_end = $year_start + 1;
51 $month_end = $month_start - 1;
52 if ($month_end < 1)
53 {
54  $month_end = 12;
55  $year_end--;
56 }
57 $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
58 $search_date_end = dol_get_last_day($year_end, $month_end);
59 $year_current = $year_start;
60 
61 // Validate History
62 $action = GETPOST('action', 'aZ09');
63 
64 
65 
66 /*
67  * Actions
68  */
69 
70 if ($action == 'clean' || $action == 'validatehistory')
71 {
72  // Clean database
73  $db->begin();
74  $sql1 = "UPDATE ".MAIN_DB_PREFIX."expensereport_det as erd";
75  $sql1 .= " SET fk_code_ventilation = 0";
76  $sql1 .= ' WHERE erd.fk_code_ventilation NOT IN';
77  $sql1 .= ' (SELECT accnt.rowid ';
78  $sql1 .= ' FROM '.MAIN_DB_PREFIX.'accounting_account as accnt';
79  $sql1 .= ' INNER JOIN '.MAIN_DB_PREFIX.'accounting_system as syst';
80  $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid='.$conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity.')';
81  $sql1 .= ' AND erd.fk_expensereport IN (SELECT rowid FROM '.MAIN_DB_PREFIX.'expensereport WHERE entity = '.$conf->entity.')';
82  $sql1 .= ' AND fk_code_ventilation <> 0';
83  dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
84  $resql1 = $db->query($sql1);
85  if (!$resql1) {
86  $error++;
87  $db->rollback();
88  setEventMessages($db->lasterror(), null, 'errors');
89  } else {
90  $db->commit();
91  }
92  // End clean database
93 }
94 
95 if ($action == 'validatehistory') {
96  $error = 0;
97  $db->begin();
98 
99  // Now make the binding
100  if ($db->type == 'pgsql') {
101  $sql1 = "UPDATE ".MAIN_DB_PREFIX."expensereport_det";
102  $sql1 .= " SET fk_code_ventilation = accnt.rowid";
103  $sql1 .= " FROM ".MAIN_DB_PREFIX."c_type_fees as t, ".MAIN_DB_PREFIX."accounting_account as accnt , ".MAIN_DB_PREFIX."accounting_system as syst";
104  $sql1 .= " WHERE ".MAIN_DB_PREFIX."expensereport_det.fk_c_type_fees = t.id AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=".$conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity;
105  $sql1 .= " AND accnt.active = 1 AND t.accountancy_code = accnt.account_number";
106  $sql1 .= " AND ".MAIN_DB_PREFIX."expensereport_det.fk_code_ventilation = 0";
107  } else {
108  $sql1 = "UPDATE ".MAIN_DB_PREFIX."expensereport_det as erd, ".MAIN_DB_PREFIX."c_type_fees as t, ".MAIN_DB_PREFIX."accounting_account as accnt , ".MAIN_DB_PREFIX."accounting_system as syst";
109  $sql1 .= " SET erd.fk_code_ventilation = accnt.rowid";
110  $sql1 .= " WHERE erd.fk_c_type_fees = t.id AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=".$conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity;
111  $sql1 .= " AND accnt.active = 1 AND t.accountancy_code=accnt.account_number";
112  $sql1 .= " AND erd.fk_code_ventilation = 0";
113  }
114 
115  dol_syslog('htdocs/accountancy/expensereport/index.php');
116 
117  $resql1 = $db->query($sql1);
118  if (!$resql1) {
119  $error++;
120  $db->rollback();
121  setEventMessages($db->lasterror(), null, 'errors');
122  } else {
123  $db->commit();
124  setEventMessages($langs->trans('AutomaticBindingDone'), null, 'mesgs');
125  }
126 }
127 
128 
129 /*
130  * View
131  */
132 
133 llxHeader('', $langs->trans("ExpenseReportsVentilation"));
134 
135 $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current - 1).'">'.img_previous().'</a>';
136 $textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current + 1).'">'.img_next().'</a>';
137 
138 print load_fiche_titre($langs->trans("ExpenseReportsVentilation")."&nbsp;".$textprevyear."&nbsp;".$langs->trans("Year")."&nbsp;".$year_start."&nbsp;".$textnextyear, '', 'title_accountancy');
139 
140 print '<span class="opacitymedium">'.$langs->trans("DescVentilExpenseReport").'</span><br>';
141 print '<span class="opacitymedium hideonsmartphone">'.$langs->trans("DescVentilExpenseReportMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")).'<br>';
142 print '</span><br>';
143 
144 
145 $y = $year_current;
146 
147 $buttonbind = '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?year='.$year_current.'&action=validatehistory">'.$langs->trans("ValidateHistory").'</a>';
148 
149 
150 print_barre_liste($langs->trans("OverviewOfAmountOfLinesNotBound"), '', '', '', '', '', '', -1, '', '', 0, $buttonbind, '', 0, 1, 1);
151 //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
152 
153 print '<div class="div-table-responsive-no-min">';
154 print '<table class="noborder centpercent">';
155 print '<tr class="liste_titre"><td width="200">'.$langs->trans("Account").'</td>';
156 print '<td width="200" class="left">'.$langs->trans("Label").'</td>';
157 for ($i = 1; $i <= 12; $i++) {
158  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
159  if ($j > 12) $j -= 12;
160  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
161 }
162 print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
163 
164 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
165 $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
166 for ($i = 1; $i <= 12; $i++) {
167  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
168  if ($j > 12) $j -= 12;
169  $sql .= " SUM(".$db->ifsql('MONTH(er.date_debut)='.$j, 'erd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
170 }
171 $sql .= " SUM(erd.total_ht) as total";
172 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as erd";
173 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport as er ON er.rowid = erd.fk_expensereport";
174 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
175 $sql .= " WHERE er.date_debut >= '".$db->idate($search_date_start)."'";
176 $sql .= " AND er.date_debut <= '".$db->idate($search_date_end)."'";
177 // Define begin binding date
178 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
179  $sql .= " AND er.date_debut >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
180 }
181 $sql .= " AND er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.")";
182 $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
183 $sql .= " AND aa.account_number IS NULL";
184 $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label";
185 
186 dol_syslog('/accountancy/expensereport/index.php:: sql='.$sql);
187 $resql = $db->query($sql);
188 if ($resql) {
189  $num = $db->num_rows($resql);
190 
191  while ($row = $db->fetch_row($resql)) {
192  print '<tr class="oddeven"><td>';
193  if ($row[0] == 'tobind')
194  {
195  print $langs->trans("Unknown");
196  } else print length_accountg($row[0]);
197  print '</td>';
198  print '<td class="left">';
199  if ($row[0] == 'tobind')
200  {
201  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/expensereport/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
202  } else print $row[1];
203  print '</td>';
204  for ($i = 2; $i <= 12; $i++) {
205  print '<td class="nowrap right">'.price($row[$i]).'</td>';
206  }
207  print '<td class="nowrap right">'.price($row[13]).'</td>';
208  print '<td class="nowrap right"><b>'.price($row[14]).'</b></td>';
209  print '</tr>';
210  }
211  $db->free($resql);
212 } else {
213  print $db->lasterror(); // Show last sql error
214 }
215 print "</table>\n";
216 print '</div>';
217 
218 
219 print '<br>';
220 
221 
222 print_barre_liste($langs->trans("OverviewOfAmountOfLinesBound"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
223 //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), '', '');
224 
225 
226 print '<div class="div-table-responsive-no-min">';
227 print '<table class="noborder centpercent">';
228 print '<tr class="liste_titre"><td width="200">'.$langs->trans("Account").'</td>';
229 print '<td width="200" class="left">'.$langs->trans("Label").'</td>';
230 for ($i = 1; $i <= 12; $i++) {
231  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
232  if ($j > 12) $j -= 12;
233  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
234 }
235 print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
236 
237 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
238 $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
239 for ($i = 1; $i <= 12; $i++) {
240  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
241  if ($j > 12) $j -= 12;
242  $sql .= " SUM(".$db->ifsql('MONTH(er.date_debut)='.$j, 'erd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
243 }
244 $sql .= " ROUND(SUM(erd.total_ht),2) as total";
245 $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as erd";
246 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport as er ON er.rowid = erd.fk_expensereport";
247 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
248 $sql .= " WHERE er.date_debut >= '".$db->idate($search_date_start)."'";
249 $sql .= " AND er.date_debut <= '".$db->idate($search_date_end)."'";
250 // Define begin binding date
251 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
252  $sql .= " AND er.date_debut >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
253 }
254 $sql .= " AND er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.")";
255 $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
256 $sql .= " AND aa.account_number IS NOT NULL";
257 $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label";
258 
259 dol_syslog('htdocs/accountancy/expensereport/index.php');
260 $resql = $db->query($sql);
261 if ($resql) {
262  $num = $db->num_rows($resql);
263 
264  while ($row = $db->fetch_row($resql)) {
265  print '<tr class="oddeven"><td>';
266  if ($row[0] == 'tobind')
267  {
268  print $langs->trans("Unknown");
269  } else print length_accountg($row[0]);
270  print '</td>';
271 
272  print '<td class="left">';
273  if ($row[0] == 'tobind')
274  {
275  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/expensereport/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
276  } else print $row[1];
277  print '</td>';
278  for ($i = 2; $i <= 12; $i++) {
279  print '<td class="nowrap right">'.price($row[$i]).'</td>';
280  }
281  print '<td class="nowrap right">'.price($row[13]).'</td>';
282  print '<td class="nowrap right"><b>'.price($row[14]).'</b></td>';
283  print '</tr>';
284  }
285  $db->free($resql);
286 } else {
287  print $db->lasterror(); // Show last sql error
288 }
289 print "</table>\n";
290 print '</div>';
291 
292 
293 
294 if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange. Why showing a report where results depends on next step (so not yet available) ?
295 {
296  print '<br>';
297  print '<br>';
298 
299  print_barre_liste($langs->trans("OtherInfo"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
300  //print load_fiche_titre($langs->trans("OtherInfo"), '', '');
301 
302  print '<div class="div-table-responsive-no-min">';
303  print '<table class="noborder centpercent">';
304  print '<tr class="liste_titre"><td width="400" class="left">'.$langs->trans("Total").'</td>';
305  for ($i = 1; $i <= 12; $i++) {
306  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
307  if ($j > 12) $j -= 12;
308  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
309  }
310  print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
311 
312  $sql = "SELECT '".$db->escape($langs->trans("TotalExpenseReport"))."' AS label,";
313  for ($i = 1; $i <= 12; $i++) {
314  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
315  if ($j > 12) $j -= 12;
316  $sql .= " SUM(".$db->ifsql('MONTH(er.date_create)='.$j, 'erd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
317  }
318  $sql .= " SUM(erd.total_ht) as total";
319  $sql .= " FROM ".MAIN_DB_PREFIX."expensereport_det as erd";
320  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport as er ON er.rowid = erd.fk_expensereport";
321  $sql .= " WHERE er.date_debut >= '".$db->idate($search_date_start)."'";
322  $sql .= " AND er.date_debut <= '".$db->idate($search_date_end)."'";
323  // Define begin binding date
324  if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
325  $sql .= " AND er.date_debut >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
326  }
327  $sql .= " AND er.fk_statut IN (".ExpenseReport::STATUS_APPROVED.", ".ExpenseReport::STATUS_CLOSED.")";
328  $sql .= " AND er.entity IN (".getEntity('expensereport', 0).")"; // We don't share object for accountancy
329 
330  dol_syslog('htdocs/accountancy/expensereport/index.php');
331  $resql = $db->query($sql);
332  if ($resql) {
333  $num = $db->num_rows($resql);
334 
335  while ($row = $db->fetch_row($resql)) {
336  print '<tr><td>'.$row[0].'</td>';
337  for ($i = 1; $i <= 12; $i++) {
338  print '<td class="nowrap right">'.price($row[$i]).'</td>';
339  }
340  print '<td class="nowrap right"><b>'.price($row[13]).'</b></td>';
341  print '</tr>';
342  }
343 
344  $db->free($resql);
345  } else {
346  print $db->lasterror(); // Show last sql error
347  }
348  print "</table>\n";
349  print '</div>';
350 }
351 
352 // End of page
353 llxFooter();
354 $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...
dol_now($mode= 'auto')
Return date for now.
const STATUS_CLOSED
Classified paid.
llxHeader()
Empty header.
Definition: wrapper.php:45
setEventMessages($mesg, $mesgs, $style= 'mesgs', $messagekey= '')
Set event messages in dol_events session object.
print_barre_liste($titre, $page, $file, $options= '', $sortfield= '', $sortorder= '', $morehtmlcenter= '', $num=-1, $totalnboflines= '', $picto= 'generic', $pictoisfullpath=0, $morehtmlright= '', $morecss= '', $limit=-1, $hideselectlimit=0, $hidenavigation=0, $pagenavastextinput=0, $morehtmlrightbeforearrow= '')
Print a title with navigation controls for pagination.
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.
accessforbidden($message= '', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
img_next($titlealt= 'default', $moreatt= '')
Show next logo.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:498
print
Draft customers invoices.
Definition: index.php:89
dol_print_date($time, $format= '', $tzoutput= 'auto', $outputlangs= '', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
img_previous($titlealt= 'default', $moreatt= '')
Show previous logo.
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
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...
llxFooter()
Empty footer.
Definition: wrapper.php:59