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-2015 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 require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
31 
32 // Langs
33 $langs->load("compta");
34 $langs->load("bills");
35 $langs->load("other");
36 $langs->load("main");
37 $langs->load("accountancy");
38 
39 // Security check
40 if (empty($conf->accounting->enabled)) {
42 }
43 if ($user->societe_id > 0)
45 if (! $user->rights->accounting->bind->write)
47 
48 // Filter
49 $year = GETPOST("year",'int');
50 if ($year == 0) {
51  $year_current = strftime("%Y", time());
52  $year_start = $year_current;
53 } else {
54  $year_current = $year;
55  $year_start = $year;
56 }
57 
58 // Validate History
59 $action = GETPOST('action', 'aZ09');
60 
61 
62 /*
63  * Actions
64  */
65 
66 if ($action == 'validatehistory') {
67 
68  $error = 0;
69  $db->begin();
70 
71  // First clean corrupted data
72  $sqlclean = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd";
73  $sqlclean .= " SET fk_code_ventilation = 0";
74  $sqlclean .= ' WHERE fd.fk_code_ventilation NOT IN ';
75  $sqlclean .= ' (SELECT accnt.rowid ';
76  $sqlclean .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt';
77  $sqlclean .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst';
78  $sqlclean .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . $conf->global->CHARTOFACCOUNTS . ')';
79  $resql = $db->query($sqlclean);
80 
81  // Now make the binding. Bind automatically only for product with a dedicated account that exists into chart of account, others need a manual bind
82  if ($db->type == 'pgsql') {
83  $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det";
84  $sql1 .= " SET fk_code_ventilation = accnt.rowid";
85  $sql1 .= " FROM " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
86  $sql1 .= " WHERE " . MAIN_DB_PREFIX . "facture_fourn_det.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS;
87  $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_buy=accnt.account_number";
88  $sql1 .= " AND " . MAIN_DB_PREFIX . "facture_fourn_det.fk_code_ventilation = 0";
89  } else {
90  $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det as fd, " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
91  $sql1 .= " SET fk_code_ventilation = accnt.rowid";
92  $sql1 .= " WHERE fd.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS;
93  $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_buy=accnt.account_number";
94  $sql1 .= " AND fd.fk_code_ventilation = 0";
95  }
96 
97  dol_syslog('htdocs/accountancy/supplier/index.php');
98 
99  $resql1 = $db->query($sql1);
100  if (! $resql1) {
101  $error ++;
102  $db->rollback();
103  setEventMessages($db->lasterror(), null, 'errors');
104  } else {
105  $db->commit();
106  setEventMessages($langs->trans('AutomaticBindingDone'), null, 'mesgs');
107  }
108 } elseif ($action == 'cleanaccountancycode') {
109  $error = 0;
110  $db->begin();
111 
112  $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det as fd";
113  $sql1.= " SET fk_code_ventilation = 0";
114  $sql1.= " WHERE fd.fk_facture_fourn IN ( SELECT f.rowid FROM " . MAIN_DB_PREFIX . "facture_fourn as f";
115  $sql1.= " WHERE f.datef >= '" . $db->idate(dol_get_first_day($year_current, 1, false)) . "'";
116  $sql1.= " AND f.datef <= '" . $db->idate(dol_get_last_day($year_current, 12, false)) . "'";
117  $sql1.= " AND f.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("SuppliersVentilation"));
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("SuppliersVentilation") . " " . $textprevyear . "&nbsp;" . $langs->trans("Year") . "&nbsp;" . $year_start . "&nbsp;" . $textnextyear, '', 'title_accountancy');
143 
144 print $langs->trans("DescVentilSupplier") . '<br>';
145 print $langs->trans("DescVentilMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")) . '<br>';
146 print '<br>';
147 
148 // Clean database
149 $db->begin();
150 $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facture_fourn_det as fd";
151 $sql1 .= " SET fk_code_ventilation = 0";
152 $sql1 .= ' WHERE fd.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 print '<div class="div-table-responsive-no-min">';
178 print '<table class="noborder" width="100%">';
179 print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
180 print '<td width="200" align="left">' . $langs->trans("Label") . '</td>';
181 for($i = 1; $i <= 12; $i ++) {
182  print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
183 }
184 print '<td width="60" align="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
185 
186 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number') ." AS codecomptable,";
187 $sql .= " " . $db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label') . " AS intitule,";
188 for($i = 1; $i <= 12; $i ++) {
189  $sql .= " SUM(" . $db->ifsql('MONTH(ff.datef)=' . $i, 'ffd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
190 }
191 $sql .= " SUM(ffd.total_ht) as total";
192 $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
193 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
194 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = ffd.fk_code_ventilation";
195 $sql .= " WHERE ff.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
196 $sql .= " AND ff.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
197 $sql .= " AND ff.fk_statut > 0 ";
198 $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy
199 $sql .= " AND aa.account_number IS NULL";
200 $sql .= " GROUP BY ffd.fk_code_ventilation,aa.account_number,aa.label";
201 
202 dol_syslog('htdocs/accountancy/supplier/index.php');
203 $resql = $db->query($sql);
204 if ($resql) {
205  $num = $db->num_rows($resql);
206 
207  while ( $row = $db->fetch_row($resql)) {
208 
209  print '<tr class="oddeven"><td>';
210  if ($row[0] == 'tobind')
211  {
212  print $langs->trans("Unknown");
213  }
214  else print length_accountg($row[0]);
215  print '</td>';
216  print '<td align="left">';
217  if ($row[0] == 'tobind')
218  {
219  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/supplier/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
220  }
221  else print $row[1];
222  print '</td>';
223  for($i = 2; $i <= 12; $i ++) {
224  print '<td align="right">' . price($row[$i]) . '</td>';
225  }
226  print '<td align="right">' . price($row[13]) . '</td>';
227  print '<td align="right"><b>' . price($row[14]) . '</b></td>';
228  print '</tr>';
229  }
230  $db->free($resql);
231 } else {
232  print $db->lasterror(); // Show last sql error
233 }
234 print "</table>\n";
235 print '</div>';
236 
237 
238 print '<br>';
239 
240 
241 print_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), $buttonreset, '');
242 
243 print '<div class="div-table-responsive-no-min">';
244 print '<table class="noborder" width="100%">';
245 print '<tr class="liste_titre"><td width="200">' . $langs->trans("Account") . '</td>';
246 print '<td width="200" align="left">' . $langs->trans("Label") . '</td>';
247 for($i = 1; $i <= 12; $i ++) {
248  print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
249 }
250 print '<td width="60" align="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
251 
252 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number') ." AS codecomptable,";
253 $sql .= " " . $db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label') . " AS intitule,";
254 for($i = 1; $i <= 12; $i ++) {
255  $sql .= " SUM(" . $db->ifsql('MONTH(ff.datef)=' . $i, 'ffd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
256 }
257 $sql .= " SUM(ffd.total_ht) as total";
258 $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
259 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
260 $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = ffd.fk_code_ventilation";
261 $sql .= " WHERE ff.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
262 $sql .= " AND ff.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
263 $sql .= " AND ff.fk_statut > 0 ";
264 $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy
265 $sql .= " AND aa.account_number IS NOT NULL";
266 $sql .= " GROUP BY ffd.fk_code_ventilation,aa.account_number,aa.label";
267 
268 dol_syslog('htdocs/accountancy/supplier/index.php');
269 $resql = $db->query($sql);
270 if ($resql) {
271  $num = $db->num_rows($resql);
272 
273  while ( $row = $db->fetch_row($resql)) {
274 
275  print '<tr class="oddeven"><td>';
276  if ($row[0] == 'tobind')
277  {
278  print $langs->trans("Unknown");
279  }
280  else print length_accountg($row[0]);
281  print '</td>';
282  print '<td align="left">';
283  if ($row[0] == 'tobind')
284  {
285  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/supplier/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
286  }
287  else print $row[1];
288  print '</td>';
289  for($i = 2; $i <= 12; $i ++) {
290  print '<td align="right">' . price($row[$i]) . '</td>';
291  }
292  print '<td align="right">' . price($row[13]) . '</td>';
293  print '<td align="right"><b>' . price($row[14]) . '</b></td>';
294  print '</tr>';
295  }
296  $db->free($resql);
297 } else {
298  print $db->lasterror(); // Show last sql error
299 }
300 print "</table>\n";
301 print '</div>';
302 
303 
304 
305 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 ?
306 {
307  print '<br>';
308  print '<br>';
309 
310  print_fiche_titre($langs->trans("OtherInfo"), '', '');
311 
312  print '<div class="div-table-responsive-no-min">';
313  print '<table class="noborder" width="100%">';
314  print '<tr class="liste_titre"><td width="400" align="left">' . $langs->trans("Total") . '</td>';
315  for($i = 1; $i <= 12; $i ++) {
316  print '<td width="60" align="right">' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '</td>';
317  }
318  print '<td width="60" align="right"><b>' . $langs->trans("Total") . '</b></td></tr>';
319 
320  $sql = "SELECT '" . $langs->trans("CAHTF") . "' AS label,";
321  for($i = 1; $i <= 12; $i ++) {
322  $sql .= " SUM(" . $db->ifsql('MONTH(ff.datef)=' . $i, 'ffd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ",";
323  }
324  $sql .= " SUM(ffd.total_ht) as total";
325  $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as ffd";
326  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture_fourn as ff ON ff.rowid = ffd.fk_facture_fourn";
327  $sql .= " WHERE ff.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'";
328  $sql .= " AND ff.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'";
329  $sql .= " AND ff.fk_statut > 0 ";
330  $sql .= " AND ff.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy
331 
332  dol_syslog('htdocs/accountancy/supplier/index.php');
333  $resql = $db->query($sql);
334  if ($resql) {
335  $num = $db->num_rows($resql);
336 
337  while ( $row = $db->fetch_row($resql)) {
338 
339 
340  print '<tr><td>' . $row[0] . '</td>';
341  for($i = 1; $i <= 12; $i ++) {
342  print '<td align="right">' . price($row[$i]) . '</td>';
343  }
344  print '<td align="right"><b>' . price($row[13]) . '</b></td>';
345  print '</tr>';
346  }
347 
348  $db->free($resql);
349  } else {
350  print $db->lasterror(); // Show last sql error
351  }
352  print "</table>\n";
353  print '</div>';
354 }
355 
356 
357 llxFooter();
358 $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) ...