* Copyright (C) 2013-2014 Florian Henry * Copyright (C) 2013-2016 Alexandre Spangaro * Copyright (C) 2014 Juanjo Menent * Copyright (C) 2015 Jean-François Ferry * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /** * \file htdocs/accountancy/customer/index.php * \ingroup Advanced accountancy * \brief Home customer ventilation */ require '../../main.inc.php'; // Class require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php'; require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php'; // Langs $langs->load("compta"); $langs->load("bills"); $langs->load("other"); $langs->load("main"); $langs->load("accountancy"); // Security check if ($user->societe_id > 0) accessforbidden(); if (! $user->rights->accounting->ventilation->read) accessforbidden(); // Filter $year = $_GET["year"]; if ($year == 0) { $year_current = strftime("%Y", time()); $year_start = $year_current; } else { $year_current = $year; $year_start = $year; } // Validate History $action = GETPOST('action'); if ($action == 'validatehistory') { $error = 0; $db->begin(); if ($db->type == 'pgsql') { $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd"; $sql1 .= " SET fd.fk_code_ventilation = accnt.rowid"; $sql1 .= " FROM " . MAIN_DB_PREFIX . "product as p, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst"; $sql1 .= " WHERE fd.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS; $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number"; $sql1 .= " AND fd.fk_code_ventilation = 0"; } else { $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"; $sql1 .= " SET fd.fk_code_ventilation = accnt.rowid"; $sql1 .= " WHERE fd.fk_product = p.rowid AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS; $sql1 .= " AND accnt.active = 1 AND p.accountancy_code_sell=accnt.account_number"; $sql1 .= " AND fd.fk_code_ventilation = 0"; } dol_syslog("htdocs/accountancy/customer/index.php sql=" . $sql, LOG_DEBUG); $resql1 = $db->query($sql1); if (! $resql1) { $error ++; $db->rollback(); setEventMessages($db->lasterror(), null, 'errors'); } else { $db->commit(); setEventMessages($langs->trans('Dispatched'), null, 'mesgs'); } } elseif ($action == 'fixaccountancycode') { $error = 0; $db->begin(); $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd"; $sql1 .= " SET fd.fk_code_ventilation = 0"; $sql1 .= ' WHERE fd.fk_code_ventilation NOT IN '; $sql1 .= ' (SELECT accnt.rowid '; $sql1 .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt'; $sql1 .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst'; $sql1 .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . $conf->global->CHARTOFACCOUNTS . ')'; dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG); $resql1 = $db->query($sql1); if (! $resql1) { $error ++; $db->rollback(); setEventMessage($db->lasterror(), 'errors'); } else { $db->commit(); setEventMessage($langs->trans('Done'), 'mesgs'); } } elseif ($action == 'cleanaccountancycode') { $error = 0; $db->begin(); $sql1 = "UPDATE " . MAIN_DB_PREFIX . "facturedet as fd"; $sql1 .= " SET fd.fk_code_ventilation = 0"; $sql1 .= " WHERE fd.fk_facture IN ( SELECT f.rowid FROM " . MAIN_DB_PREFIX . "facture as f"; $sql1 .= " WHERE f.datef >= '" . $db->idate(dol_get_first_day($year_current, 1, false)) . "'"; $sql1 .= " AND f.datef <= '" . $db->idate(dol_get_last_day($year_current, 12, false)) . "')"; dol_syslog("htdocs/accountancy/customer/index.php fixaccountancycode", LOG_DEBUG); $resql1 = $db->query($sql1); if (! $resql1) { $error ++; $db->rollback(); setEventMessage($db->lasterror(), 'errors'); } else { $db->commit(); setEventMessage($langs->trans('Done'), 'mesgs'); } } /* * View */ llxHeader('', $langs->trans("CustomersVentilation")); $textprevyear = '' . img_previous() . ''; $textnextyear = ' ' . img_next() . ''; print load_fiche_titre($langs->trans("CustomersVentilation") . " " . $textprevyear . " " . $langs->trans("Year") . " " . $year_start . " " . $textnextyear); print '' . $langs->trans("DescVentilCustomer") . ''; print ''; $sql = "SELECT count(*) FROM " . MAIN_DB_PREFIX . "facturedet as fd"; $sql .= " , " . MAIN_DB_PREFIX . "facture as f"; $sql .= " WHERE fd.fk_code_ventilation = 0"; $sql .= " AND f.rowid = fd.fk_facture AND f.fk_statut = 1;"; dol_syslog("htdocs/accountancy/customer/index.php sql=" . $sql, LOG_DEBUG); $result = $db->query($sql); if ($result) { $row = $db->fetch_row($result); $nbfac = $row[0]; $db->free($result); } $y = $year_current; $var = true; print ''; print ''; print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; $sql = "SELECT " . $db->ifsql('aa.account_number IS NULL', "'".$langs->trans('NotMatch')."'", 'aa.account_number') . " AS codecomptable,"; $sql .= " " . $db->ifsql('aa.label IS NULL', "'".$langs->trans('NotMatch')."'", 'aa.label') . " AS intitule,"; for($i = 1; $i <= 12; $i ++) { $sql .= " SUM(" . $db->ifsql('MONTH(f.datef)=' . $i, 'fd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ","; } $sql .= " SUM(fd.total_ht) as total"; $sql .= " FROM " . MAIN_DB_PREFIX . "facturedet as fd"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture as f ON f.rowid = fd.fk_facture"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = fd.fk_code_ventilation"; $sql .= " WHERE f.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'"; $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'"; if (! empty($conf->multicompany->enabled)) { $sql .= " AND f.entity IN (" . getEntity("facture", 1) . ")"; } $sql .= " GROUP BY fd.fk_code_ventilation,aa.account_number,aa.label"; dol_syslog("htdocs/accountancy/customer/index.php sql=" . $sql, LOG_DEBUG); $resql = $db->query($sql); if ($resql) { $i = 0; $num = $db->num_rows($resql); while ( $i < $num ) { $row = $db->fetch_row($resql); $var = ! $var; print ''; print ''; for($i = 2; $i <= 12; $i ++) { print ''; } print ''; print ''; print ''; $i ++; } $db->free($resql); } else { print $db->lasterror(); // Show last sql error } print "
' . $langs->trans("Account") . '' . $langs->trans("Label") . '' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '' . $langs->trans("Total") . '
' . length_accountg($row[0]) . '' . $row[1] . '' . price($row[$i]) . '' . price($row[13]) . '' . price($row[14]) . '
\n"; print "
\n"; print ''; print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; $sql = "SELECT '" . $langs->trans("TotalVente") . "' AS total,"; for($i = 1; $i <= 12; $i ++) { $sql .= " SUM(" . $db->ifsql('MONTH(f.datef)=' . $i, 'fd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ","; } $sql .= " SUM(fd.total_ht) as total"; $sql .= " FROM " . MAIN_DB_PREFIX . "facturedet as fd"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture as f ON f.rowid = fd.fk_facture"; $sql .= " WHERE f.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'"; $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'"; if (! empty($conf->multicompany->enabled)) { $sql .= " AND f.entity IN (" . getEntity("facture", 1) . ")"; } dol_syslog('htdocs/accountancy/customer/index.php:: $sql=' . $sql); $resql = $db->query($sql); if ($resql) { $i = 0; $num = $db->num_rows($resql); while ( $i < $num ) { $row = $db->fetch_row($resql); print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; print ''; $i ++; } $db->free($resql); } else { print $db->lasterror(); // Show last sql error } print "
' . $langs->trans("TotalVente") . '' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '' . $langs->trans("Total") . '
' . $row[0] . '' . price($row[$i]) . '' . price($row[13]) . '
\n"; if (! empty($conf->margin->enabled)) { print "
\n"; print ''; print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; $sql = "SELECT '" . $langs->trans("Vide") . "' AS 'Marge',"; for($i = 1; $i <= 12; $i ++) { $sql .= " SUM(" . $db->ifsql('MONTH(f.datef)=' . $i, '(fd.total_ht-(fd.qty * fd.buy_price_ht))', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ","; } $sql .= " SUM((fd.total_ht-(fd.qty * fd.buy_price_ht))) as 'Total'"; $sql .= " FROM " . MAIN_DB_PREFIX . "facturedet as fd"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "facture as f ON f.rowid = fd.fk_facture"; $sql .= " WHERE f.datef >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'"; $sql .= " AND f.datef <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'"; if (! empty($conf->multicompany->enabled)) { $sql .= " AND f.entity IN (" . getEntity("facture", 1) . ")"; } dol_syslog('htdocs/accountancy/customer/index.php:: $sql=' . $sql); $resql = $db->query($sql); if ($resql) { $i = 0; $num = $db->num_rows($resql); while ( $i < $num ) { $row = $db->fetch_row($resql); print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; print ''; $i ++; } $db->free($resql); } else { print $db->lasterror(); // Show last sql error } print "
' . $langs->trans("TotalMarge") . '' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '' . $langs->trans("Total") . '
' . $row[0] . '' . price($row[$i]) . '' . price($row[13]) . '
\n"; } print "\n"; print ''; llxFooter(); $db->close();