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