dolibarr  13.0.2
index.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2013 Olivier Geffroy <jeff@jeffinfo.com>
3  * Copyright (C) 2013-2014 Florian Henry <florian.henry@open-concept.pro>
4  * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
5  * Copyright (C) 2014 Juanjo Menent <jmenent@2byte.es>
6  * Copyright (C) 2015 Jean-François Ferry <jfefe@aternatik.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program. If not, see <https://www.gnu.org/licenses/>.
20  *
21  */
22 
29 require '../../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array("compta", "bills", "other", "main", "accountancy"));
37 
38 // Security check
39 if (empty($conf->accounting->enabled)) {
41 }
42 if ($user->socid > 0)
44 if (!$user->rights->accounting->bind->write)
46 
47 
48 $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
49 if (GETPOST("year", 'int')) $year_start = GETPOST("year", 'int');
50 else {
51  $year_start = dol_print_date(dol_now(), '%Y');
52  if (dol_print_date(dol_now(), '%m') < $month_start) $year_start--; // If current month is lower that starting fiscal month, we start last year
53 }
54 $year_end = $year_start + 1;
55 $month_end = $month_start - 1;
56 if ($month_end < 1)
57 {
58  $month_end = 12;
59  $year_end--;
60 }
61 $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
62 $search_date_end = dol_get_last_day($year_end, $month_end);
63 $year_current = $year_start;
64 
65 // Validate History
66 $action = GETPOST('action', 'aZ09');
67 
68 $chartaccountcode = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
69 
70 
71 /*
72  * Actions
73  */
74 
75 if ($action == 'clean' || $action == 'validatehistory')
76 {
77  // Clean database
78  $db->begin();
79  $sql1 = "UPDATE ".MAIN_DB_PREFIX."facturedet as fd";
80  $sql1 .= " SET fk_code_ventilation = 0";
81  $sql1 .= ' WHERE fd.fk_code_ventilation NOT IN';
82  $sql1 .= ' (SELECT accnt.rowid ';
83  $sql1 .= ' FROM '.MAIN_DB_PREFIX.'accounting_account as accnt';
84  $sql1 .= ' INNER JOIN '.MAIN_DB_PREFIX.'accounting_system as syst';
85  $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid='.$conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity.')';
86  $sql1 .= ' AND fd.fk_facture IN (SELECT rowid FROM '.MAIN_DB_PREFIX.'facture WHERE entity = '.$conf->entity.')';
87  $sql1 .= ' AND fk_code_ventilation <> 0';
88 
89  dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
90  $resql1 = $db->query($sql1);
91  if (!$resql1) {
92  $error++;
93  $db->rollback();
94  setEventMessages($db->lasterror(), null, 'errors');
95  } else {
96  $db->commit();
97  }
98  // End clean database
99 }
100 
101 if ($action == 'validatehistory') {
102  $error = 0;
103  $db->begin();
104 
105  // Now make the binding. Bind automatically only for product with a dedicated account that exists into chart of account, others need a manual bind
106  /*if ($db->type == 'pgsql') {
107  $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet";
108  $sql1 .= " SET fk_code_ventilation = accnt.rowid";
109  $sql1 .= " FROM " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
110  $sql1 .= " WHERE " . MAIN_DB_PREFIX . "facturedet.fk_product = p.rowid 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 p.accountancy_code_sell=accnt.account_number";
112  $sql1 .= " AND " . MAIN_DB_PREFIX . "facturedet.fk_code_ventilation = 0";
113  } else {
114  $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd, " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
115  $sql1 .= " SET fk_code_ventilation = accnt.rowid";
116  $sql1 .= " WHERE fd.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS.' AND accnt.entity = '.$conf->entity;
117  $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number";
118  $sql1 .= " AND fd.fk_code_ventilation = 0";
119  }*/
120 
121  // Customer Invoice lines (must be same request than into page list.php for manual binding)
122  $sql = "SELECT f.rowid as facid, f.ref as ref, f.datef, f.type as ftype,";
123  $sql .= " l.rowid, l.fk_product, l.description, l.total_ht, l.fk_code_ventilation, l.product_type as type_l, l.tva_tx as tva_tx_line, l.vat_src_code,";
124  $sql .= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.fk_product_type as type, p.tva_tx as tva_tx_prod,";
125  $sql .= " p.accountancy_code_sell as code_sell, p.accountancy_code_sell_intra as code_sell_intra, p.accountancy_code_sell_export as code_sell_export,";
126  $sql .= " aa.rowid as aarowid, aa2.rowid as aarowid_intra, aa3.rowid as aarowid_export,";
127  $sql .= " co.code as country_code, co.label as country_label,";
128  $sql .= " s.tva_intra";
129  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
130  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
131  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = s.fk_pays ";
132  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as l ON f.rowid = l.fk_facture";
133  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product";
134  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON p.accountancy_code_sell = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa.entity = ".$conf->entity;
135  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa2 ON p.accountancy_code_sell_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa2.entity = ".$conf->entity;
136  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa3 ON p.accountancy_code_sell_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa3.entity = ".$conf->entity;
137  $sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0";
138  $sql .= " AND l.product_type <= 2";
139 
140  dol_syslog('htdocs/accountancy/customer/index.php');
141  $result = $db->query($sql);
142  if (!$result) {
143  $error++;
144  setEventMessages($db->lasterror(), null, 'errors');
145  } else {
146  $num_lines = $db->num_rows($result);
147 
148  $isSellerInEEC = isInEEC($mysoc);
149 
150  $i = 0;
151  while ($i < min($num_lines, 10000)) { // No more than 10000 at once
152  $objp = $db->fetch_object($result);
153 
154  $isBuyerInEEC = isInEEC($objp);
155 
156  // Search suggested account for product/service (similar code exists in page list.php to make manual binding)
157  $suggestedaccountingaccountfor = '';
158  if (($objp->country_code == $mysoc->country_code) || empty($objp->country_code)) { // If buyer in same country than seller (if not defined, we assume it is same country)
159  $objp->code_sell_p = $objp->code_sell;
160  $objp->aarowid_suggest = $objp->aarowid;
161  $suggestedaccountingaccountfor = '';
162  } else {
163  if ($isSellerInEEC && $isBuyerInEEC && $objp->tva_tx_line != 0) { // European intravat sale, but with VAT
164  $objp->code_sell_p = $objp->code_sell;
165  $objp->aarowid_suggest = $objp->aarowid;
166  $suggestedaccountingaccountfor = 'eecwithvat';
167  } elseif ($isSellerInEEC && $isBuyerInEEC && empty($objp->tva_intra)) { // European intravat sale, without VAT intra community number
168  $objp->code_sell_p = $objp->code_sell;
169  $objp->aarowid_suggest = 0; // There is a doubt, no automatic binding
170  $suggestedaccountingaccountfor = 'eecwithoutvatnumber';
171  } elseif ($isSellerInEEC && $isBuyerInEEC) { // European intravat sale
172  $objp->code_sell_p = $objp->code_sell_intra;
173  $objp->aarowid_suggest = $objp->aarowid_intra;
174  $suggestedaccountingaccountfor = 'eec';
175  } else { // Foreign sale
176  $objp->code_sell_p = $objp->code_sell_export;
177  $objp->aarowid_suggest = $objp->aarowid_export;
178  $suggestedaccountingaccountfor = 'export';
179  }
180  }
181 
182  if ($objp->aarowid_suggest > 0)
183  {
184  $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."facturedet";
185  $sqlupdate .= " SET fk_code_ventilation = ".$objp->aarowid_suggest;
186  $sqlupdate .= " WHERE fk_code_ventilation <= 0 AND product_type <= 2 AND rowid = ".$objp->rowid;
187 
188  $resqlupdate = $db->query($sqlupdate);
189  if (!$resqlupdate)
190  {
191  $error++;
192  setEventMessages($db->lasterror(), null, 'errors');
193  break;
194  }
195  }
196 
197  $i++;
198  }
199  }
200 
201  if ($error)
202  {
203  $db->rollback();
204  } else {
205  $db->commit();
206  setEventMessages($langs->trans('AutomaticBindingDone'), null, 'mesgs');
207  }
208 }
209 
210 
211 /*
212  * View
213  */
214 
215 llxHeader('', $langs->trans("CustomersVentilation"));
216 
217 $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current - 1).'">'.img_previous().'</a>';
218 $textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current + 1).'">'.img_next().'</a>';
219 
220 
221 print load_fiche_titre($langs->trans("CustomersVentilation")." ".$textprevyear." ".$langs->trans("Year")." ".$year_start." ".$textnextyear, '', 'title_accountancy');
222 
223 print '<span class="opacitymedium">'.$langs->trans("DescVentilCustomer").'</span><br>';
224 print '<span class="opacitymedium hideonsmartphone">'.$langs->trans("DescVentilMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")).'<br>';
225 print '</span><br>';
226 
227 
228 $y = $year_current;
229 
230 $buttonbind = '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?year='.$year_current.'&action=validatehistory">'.$langs->trans("ValidateHistory").'</a>';
231 
232 print_barre_liste($langs->trans("OverviewOfAmountOfLinesNotBound"), '', '', '', '', '', '', -1, '', '', 0, $buttonbind, '', 0, 1, 1);
233 //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
234 
235 print '<div class="div-table-responsive-no-min">';
236 print '<table class="noborder centpercent">';
237 print '<tr class="liste_titre"><td width="200">'.$langs->trans("Account").'</td>';
238 print '<td width="200" class="left">'.$langs->trans("Label").'</td>';
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  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
243 }
244 print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
245 
246 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
247 $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
248 for ($i = 1; $i <= 12; $i++) {
249  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
250  if ($j > 12) $j -= 12;
251  $sql .= " SUM(".$db->ifsql('MONTH(f.datef)='.$j, 'fd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
252 }
253 $sql .= " SUM(fd.total_ht) as total";
254 $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
255 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
256 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
257 $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
258 $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
259 // Define begin binding date
260 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
261  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
262 }
263 $sql .= " AND f.fk_statut > 0";
264 $sql .= " AND fd.product_type <= 2";
265 $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
266 $sql .= " AND aa.account_number IS NULL";
267 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
268  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
269 } else {
270  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
271 }
272 $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label";
273 
274 dol_syslog('htdocs/accountancy/customer/index.php sql='.$sql, LOG_DEBUG);
275 $resql = $db->query($sql);
276 if ($resql) {
277  $num = $db->num_rows($resql);
278 
279  while ($row = $db->fetch_row($resql)) {
280  print '<tr class="oddeven"><td>';
281  if ($row[0] == 'tobind')
282  {
283  print $langs->trans("Unknown");
284  } else print length_accountg($row[0]);
285  print '</td>';
286  print '<td class="left">';
287  if ($row[0] == 'tobind')
288  {
289  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/customer/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
290  } else print $row[1];
291  print '</td>';
292  for ($i = 2; $i <= 12; $i++) {
293  print '<td class="nowrap right">'.price($row[$i]).'</td>';
294  }
295  print '<td class="nowrap right">'.price($row[13]).'</td>';
296  print '<td class="nowrap right"><b>'.price($row[14]).'</b></td>';
297  print '</tr>';
298  }
299  $db->free($resql);
300 } else {
301  print $db->lasterror(); // Show last sql error
302 }
303 print "</table>\n";
304 print '</div>';
305 
306 
307 print '<br>';
308 
309 
310 print_barre_liste($langs->trans("OverviewOfAmountOfLinesBound"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
311 //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), '', '');
312 
313 print '<div class="div-table-responsive-no-min">';
314 print '<table class="noborder centpercent">';
315 print '<tr class="liste_titre"><td width="200">'.$langs->trans("Account").'</td>';
316 print '<td width="200" class="left">'.$langs->trans("Label").'</td>';
317 for ($i = 1; $i <= 12; $i++) {
318  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
319  if ($j > 12) $j -= 12;
320  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
321 }
322 print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
323 
324 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
325 $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
326 for ($i = 1; $i <= 12; $i++) {
327  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
328  if ($j > 12) $j -= 12;
329  $sql .= " SUM(".$db->ifsql('MONTH(f.datef)='.$j, 'fd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
330 }
331 $sql .= " SUM(fd.total_ht) as total";
332 $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
333 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
334 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
335 $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
336 $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
337 // Define begin binding date
338 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
339  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
340 }
341 $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
342 $sql .= " AND f.fk_statut > 0";
343 $sql .= " AND fd.product_type <= 2";
344 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
345  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_SITUATION.")";
346 } else {
347  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_DEPOSIT.", ".Facture::TYPE_SITUATION.")";
348 }
349 $sql .= " AND aa.account_number IS NOT NULL";
350 $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label";
351 
352 dol_syslog('htdocs/accountancy/customer/index.php');
353 $resql = $db->query($sql);
354 if ($resql) {
355  $num = $db->num_rows($resql);
356 
357  while ($row = $db->fetch_row($resql)) {
358  print '<tr class="oddeven"><td>';
359  if ($row[0] == 'tobind')
360  {
361  print $langs->trans("Unknown");
362  } else print length_accountg($row[0]);
363  print '</td>';
364 
365  print '<td class="left">';
366  if ($row[0] == 'tobind')
367  {
368  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/customer/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind"));
369  } else print $row[1];
370  print '</td>';
371 
372  for ($i = 2; $i <= 12; $i++) {
373  print '<td class="nowrap right">'.price($row[$i]).'</td>';
374  }
375  print '<td class="nowrap right">'.price($row[13]).'</td>';
376  print '<td class="nowrap right"><b>'.price($row[14]).'</b></td>';
377  print '</tr>';
378  }
379  $db->free($resql);
380 } else {
381  print $db->lasterror(); // Show last sql error
382 }
383 print "</table>\n";
384 print '</div>';
385 
386 
387 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 ?
388 {
389  print '<br>';
390  print '<br>';
391 
392  print_barre_liste($langs->trans("OtherInfo"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
393  //print load_fiche_titre($langs->trans("OtherInfo"), '', '');
394 
395  print '<div class="div-table-responsive-no-min">';
396  print '<table class="noborder centpercent">';
397  print '<tr class="liste_titre"><td width="400" class="left">'.$langs->trans("TotalVente").'</td>';
398  for ($i = 1; $i <= 12; $i++) {
399  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
400  if ($j > 12) $j -= 12;
401  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
402  }
403  print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
404 
405  $sql = "SELECT '".$db->escape($langs->trans("TotalVente"))."' AS total,";
406  for ($i = 1; $i <= 12; $i++) {
407  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
408  if ($j > 12) $j -= 12;
409  $sql .= " SUM(".$db->ifsql('MONTH(f.datef)='.$j, 'fd.total_ht', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
410  }
411  $sql .= " SUM(fd.total_ht) as total";
412  $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
413  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
414  $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
415  $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
416  // Define begin binding date
417  if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
418  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
419  }
420  $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
421  $sql .= " AND f.fk_statut > 0";
422  $sql .= " AND fd.product_type <= 2";
423  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
424  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_SITUATION.")";
425  } else {
426  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_DEPOSIT.", ".Facture::TYPE_SITUATION.")";
427  }
428 
429  dol_syslog('htdocs/accountancy/customer/index.php');
430  $resql = $db->query($sql);
431  if ($resql) {
432  $num = $db->num_rows($resql);
433 
434  while ($row = $db->fetch_row($resql)) {
435  print '<tr><td>'.$row[0].'</td>';
436  for ($i = 1; $i <= 12; $i++) {
437  print '<td class="nowrap right">'.price($row[$i]).'</td>';
438  }
439  print '<td class="nowrap right"><b>'.price($row[13]).'</b></td>';
440  print '</tr>';
441  }
442  $db->free($resql);
443  } else {
444  print $db->lasterror(); // Show last sql error
445  }
446  print "</table>\n";
447  print '</div>';
448 
449 
450  if (!empty($conf->margin->enabled)) {
451  print "<br>\n";
452  print '<div class="div-table-responsive-no-min">';
453  print '<table class="noborder centpercent">';
454  print '<tr class="liste_titre"><td width="400">'.$langs->trans("TotalMarge").'</td>';
455  for ($i = 1; $i <= 12; $i++) {
456  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
457  if ($j > 12) $j -= 12;
458  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
459  }
460  print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
461 
462  $sql = "SELECT '".$db->escape($langs->trans("Vide"))."' AS marge,";
463  for ($i = 1; $i <= 12; $i++) {
464  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
465  if ($j > 12) $j -= 12;
466  $sql .= " SUM(".$db->ifsql('MONTH(f.datef)='.$j, '(fd.total_ht-(fd.qty * fd.buy_price_ht))', '0').") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
467  }
468  $sql .= " SUM((fd.total_ht-(fd.qty * fd.buy_price_ht))) as total";
469  $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
470  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
471  $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
472  $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
473  // Define begin binding date
474  if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
475  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
476  }
477  $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
478  $sql .= " AND f.fk_statut > 0";
479  $sql .= " AND fd.product_type <= 2";
480  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
481  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_SITUATION.")";
482  } else {
483  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_DEPOSIT.", ".Facture::TYPE_SITUATION.")";
484  }
485 
486  dol_syslog('htdocs/accountancy/customer/index.php');
487  $resql = $db->query($sql);
488  if ($resql) {
489  $num = $db->num_rows($resql);
490 
491  while ($row = $db->fetch_row($resql)) {
492  print '<tr><td>'.$row[0].'</td>';
493  for ($i = 1; $i <= 12; $i++) {
494  print '<td class="nowrap right">'.price(price2num($row[$i])).'</td>';
495  }
496  print '<td class="nowrap right"><b>'.price(price2num($row[13])).'</b></td>';
497  print '</tr>';
498  }
499  $db->free($resql);
500  } else {
501  print $db->lasterror(); // Show last sql error
502  }
503  print "</table>\n";
504  print '</div>';
505  }
506 }
507 
508 // End of page
509 llxFooter();
510 $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 TYPE_REPLACEMENT
Replacement invoice.
const TYPE_SITUATION
Situation invoice.
const TYPE_CREDIT_NOTE
Credit note invoice.
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.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
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.
const TYPE_DEPOSIT
Deposit invoice.
dol_getIdFromCode($db, $key, $tablename, $fieldkey= 'code', $fieldid= 'id', $entityfilter=0)
Return an id or code from a code or id.
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
isInEEC($object)
Return if a country of an object is inside the EEC (European Economic Community)