* Copyright (C) 2016 Alexandre Spangaro * Copyright (C) 2018 Laurent Destailleur * Copyright (C) 2018 Frédéric France * * 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/compta/resultat/result.php * \ingroup compta, accountancy * \brief Page for accounting result */ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php'; require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountancycategory.class.php'; // Load translation files required by the page $langs->loadLangs(array('compta','bills','donation','salaries','accountancy')); $error = 0; $mesg = ''; $action = GETPOST('action','aZ09'); $cat_id = GETPOST('account_category'); $selectcpt = GETPOST('cpt_bk'); $id = GETPOST('id', 'int'); $rowid = GETPOST('rowid', 'int'); $cancel = GETPOST('cancel','alpha'); $showaccountdetail = GETPOST('showaccountdetail','aZ09')?GETPOST('showaccountdetail','aZ09'):'no'; $date_startmonth=GETPOST('date_startmonth','int'); $date_startday=GETPOST('date_startday','int'); $date_startyear=GETPOST('date_startyear','int'); $date_endmonth=GETPOST('date_endmonth','int'); $date_endday=GETPOST('date_endday','int'); $date_endyear=GETPOST('date_endyear','int'); $nbofyear=1; // Date range $year=GETPOST('year','int'); if (empty($year)) { $year_current = strftime("%Y", dol_now()); $month_current = strftime("%m", dol_now()); $year_start = $year_current - ($nbofyear - 1); } else { $year_current = $year; $month_current = strftime("%m", dol_now()); $year_start = $year - ($nbofyear - 1); } $date_start=dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear); $date_end=dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear); // We define date_start and date_end if (empty($date_start) || empty($date_end)) // We define date_start and date_end { $q=GETPOST("q")?GETPOST("q"):0; if ($q==0) { // We define date_start and date_end $year_end=$year_start + ($nbofyear - 1); $month_start=GETPOST("month",'int')?GETPOST("month",'int'):($conf->global->SOCIETE_FISCAL_MONTH_START?($conf->global->SOCIETE_FISCAL_MONTH_START):1); $date_startmonth = $month_start; if (! GETPOST('month')) { if (! GETPOST("year") && $month_start > $month_current) { $year_start--; $year_end--; } $month_end=$month_start-1; if ($month_end < 1) $month_end=12; else $year_end++; } else $month_end=$month_start; $date_start=dol_get_first_day($year_start,$month_start,false); $date_end=dol_get_last_day($year_end,$month_end,false); } if ($q==1) { $date_start=dol_get_first_day($year_start,1,false); $date_end=dol_get_last_day($year_start,3,false); } if ($q==2) { $date_start=dol_get_first_day($year_start,4,false); $date_end=dol_get_last_day($year_start,6,false); } if ($q==3) { $date_start=dol_get_first_day($year_start,7,false); $date_end=dol_get_last_day($year_start,9,false); } if ($q==4) { $date_start=dol_get_first_day($year_start,10,false); $date_end=dol_get_last_day($year_start,12,false); } } if (($date_start < dol_time_plus_duree($date_end, -1, 'y')) || ($date_start > $date_end)) { $date_end = dol_time_plus_duree($date_start - 1, 1, 'y'); } // $date_start and $date_end are defined. We force $start_year and $nbofyear $tmps=dol_getdate($date_start); $start_year = $tmps['year']; $start_month = $tmps['mon']; $tmpe=dol_getdate($date_end); $year_end = $tmpe['year']; $month_end = $tmpe['mon']; $nbofyear = ($year_end - $start_year) + 1; $date_start_previous = dol_time_plus_duree($date_start, -1, 'y'); $date_end_previous = dol_time_plus_duree($date_end, -1, 'y'); //var_dump($date_start." ".$date_end." ".$date_start_previous." ".$date_end_previous." ".$nbofyear); if($cat_id == 0){ $cat_id = null; } // Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES' or 'BOOKKEEPING') $modecompta = $conf->global->ACCOUNTING_MODE; if (! empty($conf->accounting->enabled)) $modecompta='BOOKKEEPING'; if (GETPOST("modecompta")) $modecompta=GETPOST("modecompta",'alpha'); // Security check if ($user->societe_id > 0) accessforbidden(); if (! $user->rights->accounting->comptarapport->lire) accessforbidden(); $AccCat = new AccountancyCategory($db); /* * View */ $months = array( $langs->trans("JanuaryMin"), $langs->trans("FebruaryMin"), $langs->trans("MarchMin"), $langs->trans("AprilMin"), $langs->trans("MayMin"), $langs->trans("JuneMin"), $langs->trans("JulyMin"), $langs->trans("AugustMin"), $langs->trans("SeptemberMin"), $langs->trans("OctoberMin"), $langs->trans("NovemberMin"), $langs->trans("DecemberMin"), ); llxheader('', $langs->trans('ReportInOut')); $formaccounting = new FormAccounting($db); $form = new Form($db); $textprevyear = '' . img_previous() . ''; $textnextyear = ' ' . img_next() . ''; // Affiche en-tete de rapport if ($modecompta=="CREANCES-DETTES") { $name=$langs->trans("AnnualByAccountDueDebtMode"); $calcmode=$langs->trans("CalcModeDebt"); $calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'','').')'; if (! empty($conf->accounting->enabled)) $calcmode.='
('.$langs->trans("SeeReportInBookkeepingMode",'','').')'; $period=$form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0); //$periodlink=''.img_previous().' '.img_next().''; $description=$langs->trans("RulesResultDue"); if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded"); else $description.= $langs->trans("DepositsAreIncluded"); $builddate=dol_now(); //$exportlink=$langs->trans("NotYetAvailable"); } elseif ($modecompta=="RECETTES-DEPENSES") { $name=$langs->trans("AnnualByAccountInputOutputMode"); $calcmode=$langs->trans("CalcModeEngagement"); $calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')'; if (! empty($conf->accounting->enabled)) $calcmode.='
('.$langs->trans("SeeReportInBookkeepingMode",'','').')'; $period=$form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0); //$periodlink=''.img_previous().' '.img_next().''; $description=$langs->trans("RulesResultInOut"); $builddate=dol_now(); //$exportlink=$langs->trans("NotYetAvailable"); } elseif ($modecompta=="BOOKKEEPING") { $name = $langs->trans("ReportInOut").', '.$langs->trans("ByPersonalizedAccountGroups"); $calcmode=$langs->trans("CalcModeBookkeeping"); //$calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')'; //$calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'','').')'; $period=$form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0); $arraylist=array('no'=>$langs->trans("No"), 'yes'=>$langs->trans("AccountWithNonZeroValues"), 'all'=>$langs->trans("All")); $period.='     '.$langs->trans("DetailByAccount").' '. $form->selectarray('showaccountdetail', $arraylist, $showaccountdetail, 0); $periodlink = $textprevyear . $textnextyear ; $exportlink = ''; $description=$langs->trans("RulesResultBookkeepingPersonalized"). $description.=' ('.$langs->trans("SeePageForSetup", DOL_URL_ROOT.'/accountancy/admin/categories_list.php?search_country_id='.$mysoc->country_id.'&mainmenu=accountancy&leftmenu=accountancy_admin', $langs->transnoentitiesnoconv("Accountancy").' / '.$langs->transnoentitiesnoconv("Setup").' / '.$langs->transnoentitiesnoconv("AccountingCategory")).')'; //if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $description.= $langs->trans("DepositsAreNotIncluded"); //else $description.= $langs->trans("DepositsAreIncluded"); $builddate=dol_now(); } report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array('modecompta'=>$modecompta, 'action' => ''), $calcmode); if (! empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING') { print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1); } $moreforfilter=''; print '
'; print ''."\n"; print ''; print ''; print ''; print ''; print ''; foreach($months as $k => $v){ if (($k+1) >= $date_startmonth) { print ''; } } foreach($months as $k => $v){ if (($k+1) < $date_startmonth) { print ''; } } print ''; if ($modecompta == 'CREANCES-DETTES') { //if (! empty($date_start) && ! empty($date_end)) // $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; } elseif ($modecompta=="RECETTES-DEPENSES") { //if (! empty($date_start) && ! empty($date_end)) // $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'"; } elseif ($modecompta=="BOOKKEEPING") { // Get array of all report groups that are active $cats = $AccCat->getCats(); // WARNING: Computed groups must be after group they include /* $sql = 'SELECT DISTINCT t.numero_compte as nb FROM '.MAIN_DB_PREFIX.'accounting_bookkeeping as t, '.MAIN_DB_PREFIX.'accounting_account as aa'; $sql.= " WHERE t.numero_compte = aa.account_number AND aa.fk_accounting_category = 0"; if (! empty($date_start) && ! empty($date_end)) $sql.= " AND t.doc_date >= '".$db->idate($date_start)."' AND t.doc_date <= '".$db->idate($date_end)."'"; if (! empty($month)) { $sql .= " AND MONTH(t.doc_date) = " . $month; } $resql = $db->query($sql); if ($resql) { $num_rows = $db->num_rows($resql); if ($num_rows) { print '
Warning: There is '.$num_rows.' accounts in your ledger table that are not set into a reporting group
'; $i = 0; //while ($i < $num) { // $obj = $db->fetch_object($resql); // $i++; //} } } else dol_print_error($db); */ $j=1; $sommes = array(); $totPerAccount = array(); foreach ($cats as $cat) // Loop on each group { if (!empty($cat['category_type'])) // category calculed { // When we enter here, $sommes was filled by group of accounts $formula = $cat['formula']; print ''; // Year NP print ''; $vars = array(); // Previous Fiscal year (N-1) foreach($sommes as $code => $det){ $vars[$code] = $det['NP']; } $result = strtr($formula, $vars); //var_dump($result); //$r = $AccCat->calculate($result); $r = dol_eval($result, 1); //var_dump($r); print ''; // Year N $code = $cat['code']; // code of categorie ('VTE', 'MAR', ...) $sommes[$code]['NP'] += $r; // Current fiscal year (N) if (is_array($sommes) && ! empty($sommes)){ foreach($sommes as $code => $det){ $vars[$code] = $det['N']; } } $result = strtr($formula, $vars); //$r = $AccCat->calculate($result); $r = dol_eval($result, 1); print ''; $sommes[$code]['N'] += $r; // Detail by month foreach($months as $k => $v) { if (($k+1) >= $date_startmonth) { foreach($sommes as $code => $det){ $vars[$code] = $det['M'][$k]; } $result = strtr($formula, $vars); //$r = $AccCat->calculate($result); $r = dol_eval($result, 1); print ''; $sommes[$code]['M'][$k] += $r; } } foreach($months as $k => $v) { if (($k+1) < $date_startmonth) { foreach($sommes as $code => $det){ $vars[$code] = $det['M'][$k]; } $result = strtr($formula, $vars); //$r = $AccCat->calculate($result); $r = dol_eval($result, 1); print ''; $sommes[$code]['M'][$k] += $r; } } print "\n"; //var_dump($sommes); } else // normal category { $code = $cat['code']; // Category code we process $totCat = array(); $totCat['NP'] = 0; $totCat['N'] = 0; $totCat['M'] = array(); foreach($months as $k => $v) { $totCat['M'][$k] = 0; } // Set $cpts with array of accounts in the category/group $cpts = $AccCat->getCptsCat($cat['rowid']); $arrayofaccountforfilter=array(); foreach($cpts as $i => $cpt) // Loop on each account. { $arrayofaccountforfilter[]=$cpt['account_number']; } // N-1 if (! empty($arrayofaccountforfilter)) { $return = $AccCat->getSumDebitCredit($arrayofaccountforfilter, $date_start_previous, $date_end_previous, $cpt['dc']?$cpt['dc']:0); if ($return < 0) { setEventMessages(null, $AccCat->errors, 'errors'); $resultNP=0; } else { foreach($cpts as $i => $cpt) // Loop on each account. { $resultNP = empty($AccCat->sdcperaccount[$cpt['account_number']])?0:$AccCat->sdcperaccount[$cpt['account_number']]; $totCat['NP'] += $resultNP; $sommes[$code]['NP'] += $resultNP; $totPerAccount[$cpt['account_number']]['NP'] = $resultNP; } } } // Set value into column N and month M ($totCat) // This make 12 calls for each accountancy account (12 monthes M) foreach($cpts as $i => $cpt) // Loop on each account. { // We make 1 loop for each account because we may want detail per account. // @TODO Optimize to ask a 'group by' account and a filter with account in (..., ...) in request // Each month $resultN = 0; foreach($months as $k => $v) { $monthtoprocess = $k+1; // ($k+1) is month 1, 2, ..., 12 $yeartoprocess = $start_year; if (($k+1) < $start_month) $yeartoprocess++; //var_dump($monthtoprocess.'_'.$yeartoprocess); $return = $AccCat->getSumDebitCredit($cpt['account_number'], $date_start, $date_end, $cpt['dc']?$cpt['dc']:0, 'nofilter', $monthtoprocess, $yeartoprocess); if ($return < 0) { setEventMessages(null, $AccCat->errors, 'errors'); $resultM=0; } else { $resultM=$AccCat->sdc; } $totCat['M'][$k] += $resultM; $sommes[$code]['M'][$k] += $resultM; $totPerAccount[$cpt['account_number']]['M'][$k] = $resultM; $resultN += $resultM; } $totCat['N'] += $resultN; $sommes[$code]['N'] += $resultN; $totPerAccount[$cpt['account_number']]['N'] = $resultN; } // Now output columns for row $code ('VTE', 'MAR', ...) print ""; // Column group print ''; // Label of group print ''; print ''; print ''; // Each month foreach($totCat['M'] as $k => $v){ if (($k+1) >= $date_startmonth) print ''; } foreach($totCat['M'] as $k => $v){ if (($k+1) < $date_startmonth) print ''; } print "\n"; // Loop on detail of all accounts to output the detail if ($showaccountdetail != 'no') { foreach($cpts as $i => $cpt) { $resultNP=$totPerAccount[$cpt['account_number']]['NP']; $resultN=$totPerAccount[$cpt['account_number']]['N']; if ($showaccountdetail == 'all' || $resultN > 0) { print ''; print ''; print ''; print ''; print ''; // Make one call for each month foreach($months as $k => $v) { if (($k+1) >= $date_startmonth) { $resultM=$totPerAccount[$cpt['account_number']]['M'][$k]; print ''; } } foreach($months as $k => $v) { if (($k+1) < $date_startmonth) { $resultM=$totPerAccount[$cpt['account_number']]['M'][$k]; print ''; } } print "\n"; } } } } } } print "
'.$langs->trans("AccountingCategory").''.$langs->trans("PreviousPeriod").''.$langs->trans("SelectedPeriod").''.$langs->trans('MonthShort'.sprintf("%02s",($k+1))).''.$langs->trans('MonthShort'.sprintf("%02s",($k+1))).'
'; print $cat['code']; print ''; print $cat['label']; print '' . price($r) . '' . price($r) . '' . price($r) . '' . price($r) . '
'; print $cat['code']; print ''; print $cat['label']; if (count($cpts) > 0) // Show example of 5 first accounting accounts { $i=0; foreach($cpts as $cpt) { if ($i > 5) { print '...)'; break; } if ($i > 0) print ', '; else print ' ('; print $cpt['account_number']; $i++; } if ($i <= 5) print ')'; } else { print ' - '.$langs->trans("GroupIsEmptyCheckSetup").''; } print '' . price($totCat['NP']) . '' . price($totCat['N']) . '' . price($v) . '' . price($v) . '
'; print '     ' . length_accountg($cpt['account_number']); print ' - '; print $cpt['account_label']; print '' . price($resultNP) . '' . price($resultN) . '' . price($resultM) . '' . price($resultM) . '
"; print '
'; // End of page llxFooter(); $db->close();