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