* Copyright (C) 2013-2014 Florian Henry * Copyright (C) 2013-2016 Alexandre Spangaro * Copyright (C) 2014 Juanjo Menent * * 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/expensereport/index.php * \ingroup Advanced accountancy * \brief Home expense report ventilation */ require '../../main.inc.php'; 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 (empty($conf->accounting->enabled)) { accessforbidden(); } if ($user->societe_id > 0) accessforbidden(); if (! $user->rights->accounting->bind->write) accessforbidden(); // Filter $year = GETPOST('year', 'int'); if ($year == 0) { $year_current = strftime("%Y", time()); $year_start = $year_current; } else { $year_current = $year; $year_start = $year; } // Validate History $action = GETPOST('action','aZ09'); /* * Actions */ if ($action == 'validatehistory') { $error = 0; $db->begin(); // First clean corrupted data $sqlclean = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd"; $sqlclean .= " SET fk_code_ventilation = 0"; $sqlclean .= ' WHERE erd.fk_code_ventilation NOT IN '; $sqlclean .= ' (SELECT accnt.rowid '; $sqlclean .= ' FROM ' . MAIN_DB_PREFIX . 'accounting_account as accnt'; $sqlclean .= ' INNER JOIN ' . MAIN_DB_PREFIX . 'accounting_system as syst'; $sqlclean .= ' ON accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=' . $conf->global->CHARTOFACCOUNTS . ')'; $resql = $db->query($sqlclean); // Now make the binding if ($db->type == 'pgsql') { $sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det"; $sql1 .= " SET fk_code_ventilation = accnt.rowid"; $sql1 .= " FROM " . MAIN_DB_PREFIX . "c_type_fees as t, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst"; $sql1 .= " WHERE " . MAIN_DB_PREFIX . "expensereport_det.fk_c_type_fees = t.id AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS; $sql1 .= " AND accnt.active = 1 AND t.accountancy_code = accnt.account_number"; $sql1 .= " AND " . MAIN_DB_PREFIX . "expensereport_det.fk_code_ventilation = 0"; } else { $sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd, " . MAIN_DB_PREFIX . "c_type_fees as t, " . MAIN_DB_PREFIX . "accounting_account as accnt , " . MAIN_DB_PREFIX . "accounting_system as syst"; $sql1 .= " SET erd.fk_code_ventilation = accnt.rowid"; $sql1 .= " WHERE erd.fk_c_type_fees = t.id AND accnt.fk_pcg_version = syst.pcg_version AND syst.rowid=" . $conf->global->CHARTOFACCOUNTS; $sql1 .= " AND accnt.active = 1 AND t.accountancy_code=accnt.account_number"; $sql1 .= " AND erd.fk_code_ventilation = 0"; } dol_syslog('htdocs/accountancy/expensereport/index.php'); $resql1 = $db->query($sql1); if (! $resql1) { $error ++; $db->rollback(); setEventMessages($db->lasterror(), null, 'errors'); } else { $db->commit(); setEventMessages($langs->trans('AutomaticBindingDone'), null, 'mesgs'); } } elseif ($action == 'fixaccountancycode') { $error = 0; $db->begin(); $sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd"; $sql1 .= " SET fk_code_ventilation = 0"; $sql1 .= ' WHERE erd.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(); // Now clean $sql1 = "UPDATE " . MAIN_DB_PREFIX . "expensereport_det as erd"; $sql1.= " SET fk_code_ventilation = 0"; $sql1.= " WHERE erd.fk_expensereport IN ( SELECT er.rowid FROM " . MAIN_DB_PREFIX . "expensereport as er"; $sql1.= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($year_current, 1, false)) . "'"; $sql1.= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($year_current, 12, false)) . "'"; $sql1.= " AND er.entity IN (" . getEntity('accountancy') . ")"; $sql1.=")"; 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("ExpenseReportsVentilation")); $textprevyear = '' . img_previous() . ''; $textnextyear = ' ' . img_next() . ''; print load_fiche_titre($langs->trans("ExpenseReportsVentilation") . " " . $textprevyear . " " . $langs->trans("Year") . " " . $year_start . " " . $textnextyear, '', 'title_accountancy'); print $langs->trans("DescVentilExpenseReport") . '
'; print $langs->trans("DescVentilExpenseReportMore", $langs->transnoentitiesnoconv("ValidateHistory"), $langs->transnoentitiesnoconv("ToBind")) . '
'; print '
'; //print '
'; // TODO Remove this. Should be done always or into the repair.php script. if ($conf->global->MAIN_FEATURES_LEVEL > 1) print '' . $langs->trans("CleanFixHistory", $year_current) . ''; //print '
'; $y = $year_current; $buttonbind = '' . $langs->trans("ValidateHistory") . ''; $buttonreset = '' . $langs->trans("CleanHistory", $year_current) . ''; print_fiche_titre($langs->trans("OverviewOfAmountOfLinesNotBound"), $buttonbind, ''); print ''; print ''; print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number') ." AS codecomptable,"; $sql .= " " . $db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label') . " AS intitule,"; for($i = 1; $i <= 12; $i ++) { $sql .= " SUM(" . $db->ifsql('MONTH(er.date_debut)=' . $i, 'erd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ","; } $sql .= " SUM(erd.total_ht) as total"; $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = erd.fk_code_ventilation"; $sql .= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'"; $sql .= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'"; $sql .= " AND er.fk_statut > 0 "; $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy $sql .= " AND aa.account_number IS NULL"; $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label"; dol_syslog('/accountancy/expensereport/index.php:: sql=' . $sql); $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); while ( $row = $db->fetch_row($resql)) { print ''; print ''; for($i = 2; $i <= 12; $i ++) { print ''; } print ''; print ''; print ''; } $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") . '
'; if ($row[0] == 'tobind') { print $langs->trans("Unknown"); } else print length_accountg($row[0]); print ''; if ($row[0] == 'tobind') { print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/expensereport/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind")); } else print $row[1]; print '' . price($row[$i]) . '' . price($row[13]) . '' . price($row[14]) . '
\n"; print '
'; print_fiche_titre($langs->trans("OverviewOfAmountOfLinesBound"), $buttonreset, ''); print ''; print ''; print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; $sql = "SELECT ".$db->ifsql('aa.account_number IS NULL', "'tobind'", 'aa.account_number') ." AS codecomptable,"; $sql .= " " . $db->ifsql('aa.label IS NULL', "'tobind'", 'aa.label') . " AS intitule,"; for($i = 1; $i <= 12; $i ++) { $sql .= " SUM(" . $db->ifsql('MONTH(er.date_debut)=' . $i, 'erd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ","; } $sql .= " ROUND(SUM(erd.total_ht),2) as total"; $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = erd.fk_code_ventilation"; $sql .= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'"; $sql .= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'"; $sql .= " AND er.fk_statut > 0 "; $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy $sql .= " AND aa.account_number IS NOT NULL"; $sql .= " GROUP BY erd.fk_code_ventilation,aa.account_number,aa.label"; dol_syslog('htdocs/accountancy/expensereport/index.php'); $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); while ( $row = $db->fetch_row($resql)) { print ''; print ''; for($i = 2; $i <= 12; $i ++) { print ''; } print ''; print ''; print ''; } $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") . '
'; if ($row[0] == 'tobind') { print $langs->trans("Unknown"); } else print length_accountg($row[0]); print ''; if ($row[0] == 'tobind') { print $langs->trans("UseMenuToSetBindindManualy", DOL_URL_ROOT.'/accountancy/expensereport/list.php?search_year='.$y, $langs->transnoentitiesnoconv("ToBind")); } else print $row[1]; print '' . price($row[$i]) . '' . price($row[13]) . '' . price($row[14]) . '
\n"; 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 ? { print '
'; print '
'; print_fiche_titre($langs->trans("OtherInfo"), '', ''); print "
\n"; print ''; print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; $sql = "SELECT '" . $langs->trans("TotalExpenseReport") . "' AS label,"; for($i = 1; $i <= 12; $i ++) { $sql .= " SUM(" . $db->ifsql('MONTH(er.date_create)=' . $i, 'erd.total_ht', '0') . ") AS month" . str_pad($i, 2, '0', STR_PAD_LEFT) . ","; } $sql .= " SUM(erd.total_ht) as total"; $sql .= " FROM " . MAIN_DB_PREFIX . "expensereport_det as erd"; $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "expensereport as er ON er.rowid = erd.fk_expensereport"; $sql .= " WHERE er.date_debut >= '" . $db->idate(dol_get_first_day($y, 1, false)) . "'"; $sql .= " AND er.date_debut <= '" . $db->idate(dol_get_last_day($y, 12, false)) . "'"; $sql .= " AND er.fk_statut > 0 "; $sql .= " AND er.entity IN (" . getEntity('expensereport', 0) . ")"; // We don't share object for accountancy dol_syslog('htdocs/accountancy/expensereport/index.php'); $resql = $db->query($sql); if ($resql) { $num = $db->num_rows($resql); while ( $row = $db->fetch_row($resql)) { print ''; for($i = 1; $i <= 12; $i ++) { print ''; } print ''; print ''; } $db->free($resql); } else { print $db->lasterror(); // Show last sql error } print "
' . $langs->trans("Total") . '' . $langs->trans('MonthShort' . str_pad($i, 2, '0', STR_PAD_LEFT)) . '' . $langs->trans("Total") . '
' . $row[0] . '' . price($row[$i]) . '' . price($row[13]) . '
\n"; } llxFooter(); $db->close();