dolibarr  7.0.0-beta
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@zendsi.com>
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 <http://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 
31 // Langs
32 $langs->load("compta");
33 $langs->load("bills");
34 $langs->load("other");
35 $langs->load("main");
36 $langs->load("accountancy");
37 
38 // Security check
39 if (empty($conf->accounting->enabled)) {
41 }
42 if ($user->societe_id > 0)
44 if (! $user->rights->accounting->bind->write)
46 
47 // Filter
48 $year = GETPOST('year', 'int');
49 if ($year == 0) {
50  $year_current = strftime("%Y", time());
51  $year_start = $year_current;
52 } else {
53  $year_current = $year;
54  $year_start = $year;
55 }
56 
57 // Validate History
58 $action = GETPOST('action','aZ09');
59 
60 
61 /*
62  * Actions
63  */
64 
65 if ($action == 'validatehistory') {
66 
67  $error = 0;
68  $db->begin();
69 
70  // First clean corrupted data
71  $sqlclean = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd";
72  $sqlclean .= " SET fk_code_ventilation = 0";
73  $sqlclean .= ' WHERE erd.fk_code_ventilation NOT IN ';
74  $sqlclean .= ' (SELECT accnt.rowid ';
75  $sqlclean .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt';
76  $sqlclean .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst';
77  $sqlclean .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . $conf->global->CHARTOFACCOUNTS . ')';
78  $resql = $db->query($sqlclean);
79 
80  // Now make the binding
81  if ($db->type == 'pgsql') {
82  $sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det";
83  $sql1 .= " SET fk_code_ventilation = accnt.rowid";
84  $sql1 .= " FROM " . MAIN_DB_PREFIX . "c_type_fees as t, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
85  $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;
86  $sql1 .= " AND accnt.active = 1 AND t.accountancy_code = accnt.account_number";
87  $sql1 .= " AND " . MAIN_DB_PREFIX . "expensereport_det.fk_code_ventilation = 0";
88  } else {
89  $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";
90  $sql1 .= " SET erd.fk_code_ventilation = accnt.rowid";
91  $sql1 .= " WHERE erd.fk_c_type_fees = t.id AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS;
92  $sql1 .= " AND accnt.active = 1 AND t.accountancy_code=accnt.account_number";
93  $sql1 .= " AND erd.fk_code_ventilation = 0";
94  }
95 
96  dol_syslog('htdocs/accountancy/expensereport/index.php');
97 
98  $resql1 = $db->query($sql1);
99  if (! $resql1) {
100  $error ++;
101  $db->rollback();
102  setEventMessages($db->lasterror(), null, 'errors');
103  } else {
104  $db->commit();
105  setEventMessages($langs->trans('AutomaticBindingDone'), null, 'mesgs');
106  }
107 } elseif ($action == 'cleanaccountancycode') {
108  $error = 0;
109  $db->begin();
110 
111  // Now clean
112  $sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd";
113  $sql1.= " SET fk_code_ventilation = 0";
114  $sql1.= " WHERE erd.fk_expensereport IN ( SELECT er.rowid FROM " . MAIN_DB_PREFIX . "expensereport as er";
115  $sql1.= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($year_current, 1, false)) . "'";
116  $sql1.= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($year_current, 12, false)) . "'";
117  $sql1.= " AND er.entity IN (" . getEntity('accountancy') . ")";
118  $sql1.=")";
119 
120  dol_syslog("htdocs/accountancy/customer/index.php cleanaccountancycode", LOG_DEBUG);
121 
122  $resql1 = $db->query($sql1);
123  if (! $resql1) {
124  $error ++;
125  $db->rollback();
126  setEventMessage($db->lasterror(), 'errors');
127  } else {
128  $db->commit();
129  setEventMessage($langs->trans('Done'), 'mesgs');
130  }
131 }
132 
133 /*
134  * View
135  */
136 
137 llxHeader('', $langs->trans("ExpenseReportsVentilation"));
138 
139 $textprevyear = '<a href="' . $_SERVER["PHP_SELF"] . '?year=' . ($year_current - 1) . '">' . img_previous() . '</a>';
140 $textnextyear = '&nbsp;<a href="' . $_SERVER["PHP_SELF"] . '?year=' . ($year_current + 1) . '">' . img_next() . '</a>';
141 
142 print load_fiche_titre($langs->trans("ExpenseReportsVentilation") . "&nbsp;" . $textprevyear . "&nbsp;" . $langs->trans("Year") . "&nbsp;" . $year_start . "&nbsp;" . $textnextyear, '', 'title_accountancy');
143 
144 print $langs->trans("DescVentilExpenseReport") . '<br>';
145 print $langs->trans("DescVentilExpenseReportMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")) . '<br>';
146 print '<br>';
147 
148 // Clean database
149 $db->begin();
150 $sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd";
151 $sql1 .= " SET fk_code_ventilation = 0";
152 $sql1 .= ' WHERE erd.fk_code_ventilation NOT IN ';
153 $sql1 .= ' (SELECT accnt.rowid ';
154 $sql1 .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt';
155 $sql1 .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst';
156 $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . $conf->global->CHARTOFACCOUNTS . ')';
157 dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
158 $resql1 = $db->query($sql1);
159 if (! $resql1) {
160  $error ++;
161  $db->rollback();
162  setEventMessage($db->lasterror(), 'errors');
163 } else {
164  $db->commit();
165 }
166 // End clean database
167 
168 
169 $y = $year_current;
170 
171 $buttonbind = '<a class="butAction" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=validatehistory">' . $langs->trans("ValidateHistory") . '</a>';
172 $buttonreset = '<a class="butActionDelete" href="' . $_SERVER['PHP_SELF'] . '?year=' . $year_current . '&action=cleanaccountancycode">' . $langs->trans("CleanHistory", $year_current) . '</a>';
173 
174 
175 print_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
176 
177 
178 print '<div class="div-table-responsive-no-min">';
179 print '<table class="noborder" width="100%">';
180 print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
181 print '<td width="200" align="left">' . $langs->trans("Label") . '</td>';
182 for($i = 1; $i <= 12; $i ++) {
183  print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
184 }
185 print '<td width="60" align="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
186 
187 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number') ." AS codecomptable,";
188 $sql .= " " . $db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label') . " AS intitule,";
189 for($i = 1; $i <= 12; $i ++) {
190  $sql .= " SUM(" . $db->ifsql('MONTH(er.date_debut)=' . $i, 'erd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
191 }
192 $sql .= " SUM(erd.total_ht) as total";
193 $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd";
194 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport";
195 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
196 $sql .= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
197 $sql .= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
198 $sql .= " AND er.fk_statut > 0 ";
199 $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy
200 $sql .= " AND aa.account_number IS NULL";
201 $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label";
202 
203 dol_syslog('/accountancy/expensereport/index.php:: sql=' . $sql);
204 $resql = $db->query($sql);
205 if ($resql) {
206  $num = $db->num_rows($resql);
207 
208  while ( $row = $db->fetch_row($resql)) {
209 
210  print '<tr class="oddeven"><td>';
211  if ($row[0] == 'tobind')
212  {
213  print $langs->trans("Unknown");
214  }
215  else print length_accountg($row[0]);
216  print '</td>';
217  print '<td align="left">';
218  if ($row[0] == 'tobind')
219  {
220  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/expensereport/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
221  }
222  else print $row[1];
223  print '</td>';
224  for($i = 2; $i <= 12; $i ++) {
225  print '<td align="right">' . price($row[$i]) . '</td>';
226  }
227  print '<td align="right">' . price($row[13]) . '</td>';
228  print '<td align="right"><b>' . price($row[14]) . '</b></td>';
229  print '</tr>';
230  }
231  $db->free($resql);
232 } else {
233  print $db->lasterror(); // Show last sql error
234 }
235 print "</table>\n";
236 print '</div>';
237 
238 
239 print '<br>';
240 
241 print_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), $buttonreset, '');
242 
243 
244 print '<div class="div-table-responsive-no-min">';
245 print '<table class="noborder" width="100%">';
246 print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
247 print '<td width="200" align="left">' . $langs->trans("Label") . '</td>';
248 for($i = 1; $i <= 12; $i ++) {
249  print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
250 }
251 print '<td width="60" align="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
252 
253 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number') ." AS codecomptable,";
254 $sql .= " " . $db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label') . " AS intitule,";
255 for($i = 1; $i <= 12; $i ++) {
256  $sql .= " SUM(" . $db->ifsql('MONTH(er.date_debut)=' . $i, 'erd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
257 }
258 $sql .= " ROUND(SUM(erd.total_ht),2) as total";
259 $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd";
260 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport";
261 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = erd.fk_code_ventilation";
262 $sql .= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
263 $sql .= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
264 $sql .= " AND er.fk_statut > 0 ";
265 $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy
266 $sql .= " AND aa.account_number IS NOT NULL";
267 $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label";
268 
269 dol_syslog('htdocs/accountancy/expensereport/index.php');
270 $resql = $db->query($sql);
271 if ($resql) {
272  $num = $db->num_rows($resql);
273 
274  while ( $row = $db->fetch_row($resql)) {
275 
276  print '<tr class="oddeven"><td>';
277  if ($row[0] == 'tobind')
278  {
279  print $langs->trans("Unknown");
280  }
281  else print length_accountg($row[0]);
282  print '</td>';
283  print '<td align="left">';
284  if ($row[0] == 'tobind')
285  {
286  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/expensereport/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
287  }
288  else print $row[1];
289  print '</td>';
290  for($i = 2; $i <= 12; $i ++) {
291  print '<td align="right">' . price($row[$i]) . '</td>';
292  }
293  print '<td align="right">' . price($row[13]) . '</td>';
294  print '<td align="right"><b>' . price($row[14]) . '</b></td>';
295  print '</tr>';
296  }
297  $db->free($resql);
298 } else {
299  print $db->lasterror(); // Show last sql error
300 }
301 print "</table>\n";
302 print '</div>';
303 
304 
305 
306 if ($conf->global->MAIN_FEATURES_LEVEL > 0) // This part of code looks strange. Why showing a report that should rely on result of this step ?
307 {
308  print '<br>';
309  print '<br>';
310 
311  print_fiche_titre($langs->trans("OtherInfo"), '', '');
312 
313  print '<div class="div-table-responsive-no-min">';
314  print '<table class="noborder" width="100%">';
315  print '<tr class="liste_titre"><td width="400" align="left">' . $langs->trans("Total") . '</td>';
316  for($i = 1; $i <= 12; $i ++) {
317  print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
318  }
319  print '<td width="60" align="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
320 
321  $sql = "SELECT '" . $langs->trans("TotalExpenseReport") . "' AS label,";
322  for($i = 1; $i <= 12; $i ++) {
323  $sql .= " SUM(" . $db->ifsql('MONTH(er.date_create)=' . $i, 'erd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
324  }
325  $sql .= " SUM(erd.total_ht) as total";
326  $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd";
327  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport";
328  $sql .= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
329  $sql .= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
330  $sql .= " AND er.fk_statut > 0 ";
331  $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy
332 
333  dol_syslog('htdocs/accountancy/expensereport/index.php');
334  $resql = $db->query($sql);
335  if ($resql) {
336  $num = $db->num_rows($resql);
337 
338  while ( $row = $db->fetch_row($resql)) {
339  print '<tr><td>' . $row[0] . '</td>';
340  for($i = 1; $i <= 12; $i ++) {
341  print '<td align="right">' . price($row[$i]) . '</td>';
342  }
343  print '<td align="right"><b>' . price($row[13]) . '</b></td>';
344  print '</tr>';
345  }
346 
347  $db->free($resql);
348  } else {
349  print $db->lasterror(); // Show last sql error
350  }
351  print "</table>\n";
352  print '</div>';
353 }
354 
355 llxFooter();
356 $db->close();
llxFooter()
Footer empty.
Definition: index.php:43
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
img_previous($titlealt= 'default', $moreatt='')
Show previous logo.
img_next($titlealt= 'default', $moreatt='')
Show next logo.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:445
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
llxHeader()
Header empty.
Definition: index.php:37
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
getEntity($element, $shared=1, $forceentity=null)
Get list of entity id to use.
print_fiche_titre($title, $mesg='', $picto='title_generic.png', $pictoisfullpath=0, $id='')
Show a title with picto.
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...
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:459
print
Draft customers invoices.
Definition: index.php:91
setEventMessage($mesgs, $style='mesgs')
Set event message in dol_events session object.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if(!empty($conf->fournisseur->enabled)&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->societe->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)&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1013
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous) ...