dolibarr  16.0.5
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-2022 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 require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php';
35 
36 // Load translation files required by the page
37 $langs->loadLangs(array("compta", "bills", "other", "accountancy"));
38 
39 $validatemonth = GETPOST('validatemonth', 'int');
40 $validateyear = GETPOST('validateyear', 'int');
41 
42 // Security check
43 if (!isModEnabled('accounting')) {
45 }
46 if ($user->socid > 0) {
48 }
49 if (empty($user->rights->accounting->bind->write)) {
51 }
52 
53 $accountingAccount = new AccountingAccount($db);
54 
55 $month_start = ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1);
56 if (GETPOST("year", 'int')) {
57  $year_start = GETPOST("year", 'int');
58 } else {
59  $year_start = dol_print_date(dol_now(), '%Y');
60  if (dol_print_date(dol_now(), '%m') < $month_start) {
61  $year_start--; // If current month is lower that starting fiscal month, we start last year
62  }
63 }
64 $year_end = $year_start + 1;
65 $month_end = $month_start - 1;
66 if ($month_end < 1) {
67  $month_end = 12;
68  $year_end--;
69 }
70 $search_date_start = dol_mktime(0, 0, 0, $month_start, 1, $year_start);
71 $search_date_end = dol_get_last_day($year_end, $month_end);
72 $year_current = $year_start;
73 
74 // Validate History
75 $action = GETPOST('action', 'aZ09');
76 
77 $chartaccountcode = dol_getIdFromCode($db, $conf->global->CHARTOFACCOUNTS, 'accounting_system', 'rowid', 'pcg_version');
78 
79 // Security check
80 if (!isModEnabled('accounting')) {
82 }
83 if ($user->socid > 0) {
85 }
86 if (empty($user->rights->accounting->mouvements->lire)) {
88 }
89 
90 
91 /*
92  * Actions
93  */
94 
95 if (($action == 'clean' || $action == 'validatehistory') && $user->rights->accounting->bind->write) {
96  // Clean database by removing binding done on non existing or no more existing accounts
97  $db->begin();
98  $sql1 = "UPDATE ".MAIN_DB_PREFIX."facturedet as fd";
99  $sql1 .= " SET fk_code_ventilation = 0";
100  $sql1 .= ' WHERE fd.fk_code_ventilation NOT IN';
101  $sql1 .= ' (SELECT accnt.rowid ';
102  $sql1 .= ' FROM '.MAIN_DB_PREFIX.'accounting_account as accnt';
103  $sql1 .= ' INNER JOIN '.MAIN_DB_PREFIX.'accounting_system as syst';
104  $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid='.((int) $conf->global->CHARTOFACCOUNTS).' AND accnt.entity = '.((int) $conf->entity).')';
105  $sql1 .= ' AND fd.fk_facture IN (SELECT rowid FROM '.MAIN_DB_PREFIX.'facture WHERE entity = '.((int) $conf->entity).')';
106  $sql1 .= ' AND fk_code_ventilation <> 0';
107 
108  dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG);
109  $resql1 = $db->query($sql1);
110  if (!$resql1) {
111  $error++;
112  $db->rollback();
113  setEventMessages($db->lasterror(), null, 'errors');
114  } else {
115  $db->commit();
116  }
117  // End clean database
118 }
119 
120 if ($action == 'validatehistory') {
121  $error = 0;
122  $nbbinddone = 0;
123  $notpossible = 0;
124 
125  $db->begin();
126 
127  // Now make the binding. Bind automatically only for product with a dedicated account that exists into chart of account, others need a manual bind
128  /*if ($db->type == 'pgsql') {
129  $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet";
130  $sql1 .= " SET fk_code_ventilation = accnt.rowid";
131  $sql1 .= " FROM " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst";
132  $sql1 .= " WHERE " . MAIN_DB_PREFIX . "facturedet.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . ((int) $conf->global->CHARTOFACCOUNTS).' AND accnt.entity = '.((int) $conf->entity);
133  $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number";
134  $sql1 .= " AND " . MAIN_DB_PREFIX . "facturedet.fk_code_ventilation = 0";
135  } else {
136  $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";
137  $sql1 .= " SET fk_code_ventilation = accnt.rowid";
138  $sql1 .= " WHERE fd.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . ((int) $conf->global->CHARTOFACCOUNTS).' AND accnt.entity = '.((int) $conf->entity);
139  $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number";
140  $sql1 .= " AND fd.fk_code_ventilation = 0";
141  }*/
142 
143  // Customer Invoice lines (must be same request than into page list.php for manual binding)
144  $sql = "SELECT f.rowid as facid, f.ref as ref, f.datef, f.type as ftype,";
145  $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,";
146  $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,";
147  if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
148  $sql .= " ppe.accountancy_code_sell as code_sell, ppe.accountancy_code_sell_intra as code_sell_intra, ppe.accountancy_code_sell_export as code_sell_export,";
149  } else {
150  $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,";
151  }
152  $sql .= " aa.rowid as aarowid, aa2.rowid as aarowid_intra, aa3.rowid as aarowid_export, aa4.rowid as aarowid_thirdparty,";
153  $sql .= " co.code as country_code, co.label as country_label,";
154  $sql .= " s.tva_intra,";
155  if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
156  $sql .= " spe.accountancy_code_sell as company_code_sell"; // accounting code for product but stored on thirdparty
157  } else {
158  $sql .= " s.accountancy_code_sell as company_code_sell"; // accounting code for product but stored on thirdparty
159  }
160  $sql .= " FROM ".MAIN_DB_PREFIX."facture as f";
161  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
162  if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
163  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
164  }
165  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."c_country as co ON co.rowid = s.fk_pays ";
166  $sql .= " INNER JOIN ".MAIN_DB_PREFIX."facturedet as l ON f.rowid = l.fk_facture"; // the main table
167  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON p.rowid = l.fk_product";
168  if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
169  $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
170  }
171  $alias_societe_perentity = empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED) ? "s" : "spe";
172  $alias_product_perentity = empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED) ? "p" : "ppe";
173  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON " . $alias_product_perentity . ".accountancy_code_sell = aa.account_number AND aa.active = 1 AND aa.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa.entity = ".$conf->entity;
174  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa2 ON " . $alias_product_perentity . ".accountancy_code_sell_intra = aa2.account_number AND aa2.active = 1 AND aa2.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa2.entity = ".$conf->entity;
175  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa3 ON " . $alias_product_perentity . ".accountancy_code_sell_export = aa3.account_number AND aa3.active = 1 AND aa3.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa3.entity = ".$conf->entity;
176  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa4 ON " . $alias_societe_perentity . ".accountancy_code_sell = aa4.account_number AND aa4.active = 1 AND aa4.fk_pcg_version = '".$db->escape($chartaccountcode)."' AND aa4.entity = ".$conf->entity;
177  $sql .= " WHERE f.fk_statut > 0 AND l.fk_code_ventilation <= 0";
178  $sql .= " AND l.product_type <= 2";
179  $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
180  if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
181  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
182  }
183  if ($validatemonth && $validateyear) {
184  $sql .= dolSqlDateFilter('f.datef', 0, $validatemonth, $validateyear);
185  }
186 
187  dol_syslog('htdocs/accountancy/customer/index.php');
188 
189  $result = $db->query($sql);
190  if (!$result) {
191  $error++;
192  setEventMessages($db->lasterror(), null, 'errors');
193  } else {
194  $num_lines = $db->num_rows($result);
195 
196  $facture_static = new Facture($db);
197 
198  $isSellerInEEC = isInEEC($mysoc);
199 
200  $thirdpartystatic = new Societe($db);
201  $facture_static = new Facture($db);
202  $facture_static_det = new FactureLigne($db);
203  $product_static = new Product($db);
204 
205  $i = 0;
206  while ($i < min($num_lines, 10000)) { // No more than 10000 at once
207  $objp = $db->fetch_object($result);
208 
209  $thirdpartystatic->id = $objp->socid;
210  $thirdpartystatic->name = $objp->name;
211  $thirdpartystatic->client = $objp->client;
212  $thirdpartystatic->fournisseur = $objp->fournisseur;
213  $thirdpartystatic->code_client = $objp->code_client;
214  $thirdpartystatic->code_compta_client = $objp->code_compta_client;
215  $thirdpartystatic->code_fournisseur = $objp->code_fournisseur;
216  $thirdpartystatic->code_compta_fournisseur = $objp->code_compta_fournisseur;
217  $thirdpartystatic->email = $objp->email;
218  $thirdpartystatic->country_code = $objp->country_code;
219  $thirdpartystatic->tva_intra = $objp->tva_intra;
220  $thirdpartystatic->code_compta_product = $objp->company_code_sell; // The accounting account for product stored on thirdparty object (for level3 suggestion)
221 
222  $product_static->ref = $objp->product_ref;
223  $product_static->id = $objp->product_id;
224  $product_static->type = $objp->type;
225  $product_static->label = $objp->product_label;
226  $product_static->status = $objp->status;
227  $product_static->status_buy = $objp->status_buy;
228  $product_static->accountancy_code_sell = $objp->code_sell;
229  $product_static->accountancy_code_sell_intra = $objp->code_sell_intra;
230  $product_static->accountancy_code_sell_export = $objp->code_sell_export;
231  $product_static->accountancy_code_buy = $objp->code_buy;
232  $product_static->accountancy_code_buy_intra = $objp->code_buy_intra;
233  $product_static->accountancy_code_buy_export = $objp->code_buy_export;
234  $product_static->tva_tx = $objp->tva_tx_prod;
235 
236  $facture_static->ref = $objp->ref;
237  $facture_static->id = $objp->facid;
238  $facture_static->type = $objp->ftype;
239  $facture_static->date = $objp->datef;
240 
241  $facture_static_det->id = $objp->rowid;
242  $facture_static_det->total_ht = $objp->total_ht;
243  $facture_static_det->tva_tx = $objp->tva_tx_line;
244  $facture_static_det->vat_src_code = $objp->vat_src_code;
245  $facture_static_det->product_type = $objp->type_l;
246  $facture_static_det->desc = $objp->description;
247 
248  $accountingAccountArray = array(
249  'dom'=>$objp->aarowid,
250  'intra'=>$objp->aarowid_intra,
251  'export'=>$objp->aarowid_export,
252  'thirdparty' =>$objp->aarowid_thirdparty);
253 
254  $code_sell_p_notset = '';
255  $code_sell_t_notset = '';
256 
257  $suggestedid = 0;
258 
259  $return=$accountingAccount->getAccountingCodeToBind($thirdpartystatic, $mysoc, $product_static, $facture_static, $facture_static_det, $accountingAccountArray, 'customer');
260  if (!is_array($return) && $return < 0) {
261  setEventMessage($accountingAccount->error, 'errors');
262  } else {
263  $suggestedid = $return['suggestedid'];
264  $suggestedaccountingaccountfor = $return['suggestedaccountingaccountfor'];
265 
266  if (!empty($suggestedid) && $suggestedaccountingaccountfor != '' && $suggestedaccountingaccountfor != 'eecwithoutvatnumber') {
267  $suggestedid = $return['suggestedid'];
268  } else {
269  $suggestedid = 0;
270  }
271  }
272 
273  if ($suggestedid > 0) {
274  $sqlupdate = "UPDATE ".MAIN_DB_PREFIX."facturedet";
275  $sqlupdate .= " SET fk_code_ventilation = ".((int) $suggestedid);
276  $sqlupdate .= " WHERE fk_code_ventilation <= 0 AND product_type <= 2 AND rowid = ".((int) $facture_static_det->id);
277 
278  $resqlupdate = $db->query($sqlupdate);
279  if (!$resqlupdate) {
280  $error++;
281  setEventMessages($db->lasterror(), null, 'errors');
282  break;
283  } else {
284  $nbbinddone++;
285  }
286  } else {
287  $notpossible++;
288  }
289 
290  $i++;
291  }
292  if ($num_lines > 10000) {
293  $notpossible += ($num_lines - 10000);
294  }
295  }
296 
297  if ($error) {
298  $db->rollback();
299  } else {
300  $db->commit();
301  setEventMessages($langs->trans('AutomaticBindingDone', $nbbinddone, $notpossible), null, 'mesgs');
302  }
303 }
304 
305 
306 /*
307  * View
308  */
309 
310 llxHeader('', $langs->trans("CustomersVentilation"));
311 
312 $textprevyear = '<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current - 1).'">'.img_previous().'</a>';
313 $textnextyear = '&nbsp;<a href="'.$_SERVER["PHP_SELF"].'?year='.($year_current + 1).'">'.img_next().'</a>';
314 
315 
316 print load_fiche_titre($langs->trans("CustomersVentilation")." ".$textprevyear." ".$langs->trans("Year")." ".$year_start." ".$textnextyear, '', 'title_accountancy');
317 
318 print '<span class="opacitymedium">'.$langs->trans("DescVentilCustomer").'</span><br>';
319 print '<span class="opacitymedium hideonsmartphone">'.$langs->trans("DescVentilMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")).'<br>';
320 print '</span><br>';
321 
322 
323 $y = $year_current;
324 
325 $buttonbind = '<a class="butAction" href="'.$_SERVER['PHP_SELF'].'?action=validatehistory&token='.newToken().'">'.$langs->trans("ValidateHistory").'</a>';
326 
327 print_barre_liste(img_picto('', 'unlink', 'class="paddingright fa-color-unset"').$langs->trans("OverviewOfAmountOfLinesNotBound"), '', '', '', '', '', '', -1, '', '', 0, $buttonbind, '', 0, 1, 1);
328 //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, '');
329 
330 print '<div class="div-table-responsive-no-min">';
331 print '<table class="noborder centpercent">';
332 print '<tr class="liste_titre"><td class="minwidth100">'.$langs->trans("Account").'</td>';
333 print '<td>'.$langs->trans("Label").'</td>';
334 for ($i = 1; $i <= 12; $i++) {
335  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
336  if ($j > 12) {
337  $j -= 12;
338  }
339  $cursormonth = $j;
340  if ($cursormonth > 12) {
341  $cursormonth -= 12;
342  }
343  $cursoryear = ($cursormonth < ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1)) ? $y + 1 : $y;
344  $tmp = dol_getdate(dol_get_last_day($cursoryear, $cursormonth, 'gmt'), false, 'gmt');
345 
346  print '<td width="60" class="right">';
347  if (!empty($tmp['mday'])) {
348  $param = 'search_date_startday=1&search_date_startmonth='.$cursormonth.'&search_date_startyear='.$cursoryear;
349  $param .= '&search_date_endday='.$tmp['mday'].'&search_date_endmonth='.$tmp['mon'].'&search_date_endyear='.$tmp['year'];
350  print '<a href="'.DOL_URL_ROOT.'/accountancy/customer/list.php?'.$param.'">';
351  }
352  print $langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT));
353  if (!empty($tmp['mday'])) {
354  print '</a>';
355  }
356  print '</td>';
357 }
358 print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
359 
360 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
361 $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
362 for ($i = 1; $i <= 12; $i++) {
363  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
364  if ($j > 12) {
365  $j -= 12;
366  }
367  $sql .= " SUM(".$db->ifsql("MONTH(f.datef)=".$j, "fd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
368 }
369 $sql .= " SUM(fd.total_ht) as total";
370 $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
371 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
372 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
373 $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
374 $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
375 // Define begin binding date
376 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
377  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
378 }
379 $sql .= " AND f.fk_statut > 0";
380 $sql .= " AND fd.product_type <= 2";
381 $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
382 $sql .= " AND aa.account_number IS NULL";
383 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
384  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_SITUATION.")";
385 } else {
386  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.",".Facture::TYPE_REPLACEMENT.",".Facture::TYPE_CREDIT_NOTE.",".Facture::TYPE_DEPOSIT.",".Facture::TYPE_SITUATION.")";
387 }
388 $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label";
389 
390 dol_syslog('htdocs/accountancy/customer/index.php', LOG_DEBUG);
391 $resql = $db->query($sql);
392 if ($resql) {
393  $num = $db->num_rows($resql);
394 
395  while ($row = $db->fetch_row($resql)) {
396  print '<tr class="oddeven">';
397  print '<td>';
398  if ($row[0] == 'tobind') {
399  print '<span class="opacitymedium">'.$langs->trans("Unknown").'</span>';
400  } else {
401  print length_accountg($row[0]);
402  }
403  print '</td>';
404  print '<td>';
405  if ($row[0] == 'tobind') {
406  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/customer/list.php?search_year='.((int) $y), $langs->transnoentitiesnoconv("ToBind"));
407  } else {
408  print $row[1];
409  }
410  print '</td>';
411  for ($i = 2; $i <= 13; $i++) {
412  $cursormonth = (($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) + $i - 2);
413  if ($cursormonth > 12) {
414  $cursormonth -= 12;
415  }
416  $cursoryear = ($cursormonth < ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1)) ? $y + 1 : $y;
417  $tmp = dol_getdate(dol_get_last_day($cursoryear, $cursormonth, 'gmt'), false, 'gmt');
418 
419  print '<td class="right nowraponall amount">';
420  print price($row[$i]);
421  print '</td>';
422  }
423  print '<td class="right nowraponall amount"><b>'.price($row[14]).'</b></td>';
424  print '</tr>';
425  }
426  $db->free($resql);
427 
428  if ($num == 0) {
429  print '<tr class="oddeven"><td colspan="16">';
430  print '<span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span>';
431  print '</td></tr>';
432  }
433 } else {
434  print $db->lasterror(); // Show last sql error
435 }
436 print "</table>\n";
437 print '</div>';
438 
439 
440 print '<br>';
441 
442 
443 print_barre_liste(img_picto('', 'link', 'class="paddingright fa-color-unset"').$langs->trans("OverviewOfAmountOfLinesBound"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
444 //print load_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), '', '');
445 
446 print '<div class="div-table-responsive-no-min">';
447 print '<table class="noborder centpercent">';
448 print '<tr class="liste_titre"><td class="minwidth100">'.$langs->trans("Account").'</td>';
449 print '<td>'.$langs->trans("Label").'</td>';
450 for ($i = 1; $i <= 12; $i++) {
451  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
452  if ($j > 12) {
453  $j -= 12;
454  }
455  $cursormonth = $j;
456  if ($cursormonth > 12) {
457  $cursormonth -= 12;
458  }
459  $cursoryear = ($cursormonth < ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1)) ? $y + 1 : $y;
460  $tmp = dol_getdate(dol_get_last_day($cursoryear, $cursormonth, 'gmt'), false, 'gmt');
461 
462  print '<td width="60" class="right">';
463  if (!empty($tmp['mday'])) {
464  $param = 'search_date_startday=1&search_date_startmonth='.$cursormonth.'&search_date_startyear='.$cursoryear;
465  $param .= '&search_date_endday='.$tmp['mday'].'&search_date_endmonth='.$tmp['mon'].'&search_date_endyear='.$tmp['year'];
466  print '<a href="'.DOL_URL_ROOT.'/accountancy/customer/lines.php?'.$param.'">';
467  }
468  print $langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT));
469  if (!empty($tmp['mday'])) {
470  print '</a>';
471  }
472  print '</td>';
473 }
474 print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
475 
476 $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number')." AS codecomptable,";
477 $sql .= " ".$db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label')." AS intitule,";
478 for ($i = 1; $i <= 12; $i++) {
479  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
480  if ($j > 12) {
481  $j -= 12;
482  }
483  $sql .= " SUM(".$db->ifsql("MONTH(f.datef)=".$j, "fd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
484 }
485 $sql .= " SUM(fd.total_ht) as total";
486 $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
487 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
488 $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
489 $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
490 $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
491 // Define begin binding date
492 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
493  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
494 }
495 $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
496 $sql .= " AND f.fk_statut > 0";
497 $sql .= " AND fd.product_type <= 2";
498 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
499  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_SITUATION.")";
500 } else {
501  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_DEPOSIT.", ".Facture::TYPE_SITUATION.")";
502 }
503 $sql .= " AND aa.account_number IS NOT NULL";
504 $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label";
505 $sql .= ' ORDER BY aa.account_number';
506 
507 dol_syslog('htdocs/accountancy/customer/index.php');
508 $resql = $db->query($sql);
509 if ($resql) {
510  $num = $db->num_rows($resql);
511 
512  while ($row = $db->fetch_row($resql)) {
513  print '<tr class="oddeven">';
514  print '<td>';
515  if ($row[0] == 'tobind') {
516  print $langs->trans("Unknown");
517  } else {
518  print length_accountg($row[0]);
519  }
520  print '</td>';
521 
522  print '<td>';
523  if ($row[0] == 'tobind') {
524  print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/customer/list.php?search_year='.((int) $y), $langs->transnoentitiesnoconv("ToBind"));
525  } else {
526  print $row[1];
527  }
528  print '</td>';
529 
530  for ($i = 2; $i <= 13; $i++) {
531  $cursormonth = (($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) + $i - 2);
532  if ($cursormonth > 12) {
533  $cursormonth -= 12;
534  }
535  $cursoryear = ($cursormonth < ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1)) ? $y + 1 : $y;
536  $tmp = dol_getdate(dol_get_last_day($cursoryear, $cursormonth, 'gmt'), false, 'gmt');
537 
538  print '<td class="right nowraponall amount">';
539  print price($row[$i]);
540  print '</td>';
541  }
542  print '<td class="right nowraponall amount"><b>'.price($row[14]).'</b></td>';
543  print '</tr>';
544  }
545  $db->free($resql);
546 
547  if ($num == 0) {
548  print '<tr class="oddeven"><td colspan="16">';
549  print '<span class="opacitymedium">'.$langs->trans("NoRecordFound").'</span>';
550  print '</td></tr>';
551  }
552 } else {
553  print $db->lasterror(); // Show last sql error
554 }
555 print "</table>\n";
556 print '</div>';
557 
558 
559 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 ?
560  print '<br>';
561  print '<br>';
562 
563  print_barre_liste($langs->trans("OtherInfo"), '', '', '', '', '', '', -1, '', '', 0, '', '', 0, 1, 1);
564  //print load_fiche_titre($langs->trans("OtherInfo"), '', '');
565 
566  print '<div class="div-table-responsive-no-min">';
567  print '<table class="noborder centpercent">';
568  print '<tr class="liste_titre"><td lass="left">'.$langs->trans("TotalVente").'</td>';
569  for ($i = 1; $i <= 12; $i++) {
570  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
571  if ($j > 12) {
572  $j -= 12;
573  }
574  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
575  }
576  print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
577 
578  $sql = "SELECT '".$db->escape($langs->trans("TotalVente"))."' AS total,";
579  for ($i = 1; $i <= 12; $i++) {
580  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
581  if ($j > 12) {
582  $j -= 12;
583  }
584  $sql .= " SUM(".$db->ifsql("MONTH(f.datef)=".$j, "fd.total_ht", "0").") AS month".str_pad($j, 2, "0", STR_PAD_LEFT).",";
585  }
586  $sql .= " SUM(fd.total_ht) as total";
587  $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
588  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
589  $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
590  $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
591  // Define begin binding date
592  if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
593  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
594  }
595  $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
596  $sql .= " AND f.fk_statut > 0";
597  $sql .= " AND fd.product_type <= 2";
598  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
599  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_SITUATION.")";
600  } else {
601  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_DEPOSIT.", ".Facture::TYPE_SITUATION.")";
602  }
603 
604  dol_syslog('htdocs/accountancy/customer/index.php');
605  $resql = $db->query($sql);
606  if ($resql) {
607  $num = $db->num_rows($resql);
608 
609  while ($row = $db->fetch_row($resql)) {
610  print '<tr><td>'.$row[0].'</td>';
611  for ($i = 1; $i <= 12; $i++) {
612  print '<td class="right nowraponall amount">'.price($row[$i]).'</td>';
613  }
614  print '<td class="right nowraponall amount"><b>'.price($row[13]).'</b></td>';
615  print '</tr>';
616  }
617  $db->free($resql);
618  } else {
619  print $db->lasterror(); // Show last sql error
620  }
621  print "</table>\n";
622  print '</div>';
623 
624 
625  if (isModEnabled('margin')) {
626  print "<br>\n";
627  print '<div class="div-table-responsive-no-min">';
628  print '<table class="noborder centpercent">';
629  print '<tr class="liste_titre"><td>'.$langs->trans("TotalMarge").'</td>';
630  for ($i = 1; $i <= 12; $i++) {
631  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
632  if ($j > 12) {
633  $j -= 12;
634  }
635  print '<td width="60" class="right">'.$langs->trans('MonthShort'.str_pad($j, 2, '0', STR_PAD_LEFT)).'</td>';
636  }
637  print '<td width="60" class="right"><b>'.$langs->trans("Total").'</b></td></tr>';
638  $sql = "SELECT '".$db->escape($langs->trans("Vide"))."' AS marge,";
639  for ($i = 1; $i <= 12; $i++) {
640  $j = $i + ($conf->global->SOCIETE_FISCAL_MONTH_START ? $conf->global->SOCIETE_FISCAL_MONTH_START : 1) - 1;
641  if ($j > 12) {
642  $j -= 12;
643  }
644  $sql .= " SUM(".$db->ifsql("MONTH(f.datef)=".$j,
645  " (".$db->ifsql("fd.total_ht < 0",
646  " (-1 * (abs(fd.total_ht) - (fd.buy_price_ht * fd.qty * (fd.situation_percent / 100))))",
647  " (fd.total_ht - (fd.buy_price_ht * fd.qty * (fd.situation_percent / 100)))").")",
648  0).") AS month".str_pad($j, 2, '0', STR_PAD_LEFT).",";
649  }
650  $sql .= " SUM(".$db->ifsql("fd.total_ht < 0",
651  " (-1 * (abs(fd.total_ht) - (fd.buy_price_ht * fd.qty * (fd.situation_percent / 100))))",
652  " (fd.total_ht - (fd.buy_price_ht * fd.qty * (fd.situation_percent / 100)))").") as total";
653 
654  $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as fd";
655  $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."facture as f ON f.rowid = fd.fk_facture";
656  $sql .= " WHERE f.datef >= '".$db->idate($search_date_start)."'";
657  $sql .= " AND f.datef <= '".$db->idate($search_date_end)."'";
658  // Define begin binding date
659  if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
660  $sql .= " AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING)."'";
661  }
662  $sql .= " AND f.entity IN (".getEntity('invoice', 0).")"; // We don't share object for accountancy
663  $sql .= " AND f.fk_statut > 0";
664  $sql .= " AND fd.product_type <= 2";
665  if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
666  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_SITUATION.")";
667  } else {
668  $sql .= " AND f.type IN (".Facture::TYPE_STANDARD.", ".Facture::TYPE_REPLACEMENT.", ".Facture::TYPE_CREDIT_NOTE.", ".Facture::TYPE_DEPOSIT.", ".Facture::TYPE_SITUATION.")";
669  }
670  dol_syslog('htdocs/accountancy/customer/index.php');
671  $resql = $db->query($sql);
672  if ($resql) {
673  $num = $db->num_rows($resql);
674 
675  while ($row = $db->fetch_row($resql)) {
676  print '<tr><td>'.$row[0].'</td>';
677  for ($i = 1; $i <= 12; $i++) {
678  print '<td class="right nowraponall amount">'.price(price2num($row[$i])).'</td>';
679  }
680  print '<td class="right nowraponall amount"><b>'.price(price2num($row[13])).'</b></td>';
681  print '</tr>';
682  }
683  $db->free($resql);
684  } else {
685  print $db->lasterror(); // Show last sql error
686  }
687  print "</table>\n";
688  print '</div>';
689  }
690 }
691 
692 // End of page
693 llxFooter();
694 $db->close();
dol_getdate
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
Definition: functions.lib.php:2713
Societe
Class to manage third parties objects (customers, suppliers, prospects...)
Definition: societe.class.php:48
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
Facture
Class to manage invoices.
Definition: facture.class.php:60
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Facture\TYPE_DEPOSIT
const TYPE_DEPOSIT
Deposit invoice.
Definition: facture.class.php:397
dol_print_date
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs='', $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
Definition: functions.lib.php:2514
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
Facture\TYPE_REPLACEMENT
const TYPE_REPLACEMENT
Replacement invoice.
Definition: facture.class.php:387
llxFooter
llxFooter()
Footer empty.
Definition: index.php:71
FactureLigne
Class to manage invoice lines.
Definition: facture.class.php:5742
img_next
img_next($titlealt='default', $moreatt='')
Show next logo.
Definition: functions.lib.php:4557
dol_getIdFromCode
dol_getIdFromCode($db, $key, $tablename, $fieldkey='code', $fieldid='id', $entityfilter=0, $filters='')
Return an id or code from a code or id.
Definition: functions.lib.php:8535
length_accountg
length_accountg($account)
Return General accounting account with defined length (used for product and miscellaneous)
Definition: accounting.lib.php:94
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
setEventMessage
setEventMessage($mesgs, $style='mesgs')
Set event message in dol_events session object.
Definition: functions.lib.php:8108
print_barre_liste
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.
Definition: functions.lib.php:5257
llxHeader
if(!defined('NOTOKENRENEWAL')) if(!defined('NOLOGIN')) if(!defined('NOCSRFCHECK')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) if(!defined('NOIPCHECK')) if(!defined('NOBROWSERNOTIF')) llxHeader()
Header empty.
Definition: index.php:63
dol_get_last_day
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:570
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
isModEnabled
isModEnabled($module)
Is Dolibarr module enabled.
Definition: functions.lib.php:105
Facture\TYPE_CREDIT_NOTE
const TYPE_CREDIT_NOTE
Credit note invoice.
Definition: facture.class.php:392
AccountingAccount
Class to manage accounting accounts.
Definition: accountingaccount.class.php:36
Product
Class to manage products or services.
Definition: product.class.php:46
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
dolSqlDateFilter
dolSqlDateFilter($datefield, $day_date, $month_date, $year_date, $excludefirstand=0, $gm=false)
Generate a SQL string to make a filter into a range (for second of date until last second of date).
Definition: date.lib.php:334
price
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.
Definition: functions.lib.php:5541
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
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 ...
Definition: security.lib.php:933
dol_mktime
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...
Definition: functions.lib.php:2757
img_previous
img_previous($titlealt='default', $moreatt='')
Show previous logo.
Definition: functions.lib.php:4576
Facture\TYPE_SITUATION
const TYPE_SITUATION
Situation invoice.
Definition: facture.class.php:407
isInEEC
isInEEC($object)
Return if a country of an object is inside the EEC (European Economic Community)
Definition: company.lib.php:753