From 67f9b218c5871dcff879f3faf4634f5b4eea834b Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sat, 4 Apr 2020 15:38:01 +0200 Subject: [PATCH 1/7] New supplier turnover report --- htdocs/compta/stats/supplier_turnover.php | 424 ++++++++++++++++++++++ htdocs/langs/en_US/compta.lang | 9 +- 2 files changed, 431 insertions(+), 2 deletions(-) create mode 100644 htdocs/compta/stats/supplier_turnover.php diff --git a/htdocs/compta/stats/supplier_turnover.php b/htdocs/compta/stats/supplier_turnover.php new file mode 100644 index 00000000000..33cec605eed --- /dev/null +++ b/htdocs/compta/stats/supplier_turnover.php @@ -0,0 +1,424 @@ + + * + * 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/stats/supplier_ca.php + * \brief Page reporting purchase turnover + */ + +require '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; + +// Load translation files required by the page +$langs->loadLangs(array('compta', 'bills')); + +$date_startday = GETPOST('date_startday', 'int'); +$date_startmonth = GETPOST('date_startmonth', 'int'); +$date_startyear = GETPOST('date_startyear', 'int'); +$date_endday = GETPOST('date_endday', 'int'); +$date_endmonth = GETPOST('date_endmonth', 'int'); +$date_endyear = GETPOST('date_endyear', 'int'); + +$nbofyear = 4; + +// 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") ?GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); + 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 $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); } +} + +$userid = GETPOST('userid', 'int'); +$socid = GETPOST('socid', 'int'); + +$tmps = dol_getdate($date_start); +$year_start = $tmps['year']; +$tmpe = dol_getdate($date_end); +$year_end = $tmpe['year']; +$nbofyear = ($year_end - $year_start) + 1; + +// 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->socid > 0) $socid = $user->socid; +if (!empty($conf->comptabilite->enabled)) $result = restrictedArea($user, 'compta', '', '', 'resultat'); +if (!empty($conf->accounting->enabled)) $result = restrictedArea($user, 'accounting', '', '', 'comptarapport'); + + + + +/* + * View + */ + +llxHeader(); + +$form = new Form($db); + +// Affiche en-tete du rapport +if ($modecompta == "CREANCES-DETTES") +{ + $name = $langs->trans("PurchaseTurnover"); + $calcmode = $langs->trans("CalcModeDebt"); + //$calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'','').')'; + $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 = ($year_start ? "".img_previous()." ".img_next()."" : ""); + $description = $langs->trans("RulesPurchaseTurnoverDue"); + $builddate = dol_now(); + //$exportlink=$langs->trans("NotYetAvailable"); +} +elseif ($modecompta == "RECETTES-DEPENSES") +{ + $name = $langs->trans("PurchaseTurnoverCollected"); + $calcmode = $langs->trans("CalcModeEngagement"); + //$calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')'; + //$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 = ($year_start ? "".img_previous()." ".img_next()."" : ""); + $description = $langs->trans("RulesPurchaseTurnoverIn"); + $builddate = dol_now(); + //$exportlink=$langs->trans("NotYetAvailable"); +} +elseif ($modecompta == "BOOKKEEPING") +{ + $name = $langs->trans("PurchaseTurnover"); + $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); + $periodlink = ($year_start ? "".img_previous()." ".img_next()."" : ""); + $description = $langs->trans("RulesPurchaseTurnoverTotalPurchaseJournal"); + $builddate = dol_now(); + //$exportlink=$langs->trans("NotYetAvailable"); +} + +$moreparam = array(); +if (!empty($modecompta)) $moreparam['modecompta'] = $modecompta; +report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $moreparam, $calcmode); + +if (!empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING') +{ + print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1); +} + + +if ($modecompta == 'CREANCES-DETTES') +{ + $sql = "SELECT date_format(f.datef,'%Y-%m') as dm, sum(f.total_ht) as amount, sum(f.total_ttc) as amount_ttc"; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; + $sql .= " WHERE f.fk_statut in (1,2)"; + $sql .= " AND f.type IN (0,2)"; + $sql .= " AND f.entity IN (".getEntity('invoice').")"; + if ($socid) $sql .= " AND f.fk_soc = ".$socid; +} +elseif ($modecompta == "RECETTES-DEPENSES") +{ + $sql = "SELECT date_format(p.datep,'%Y-%m') as dm, sum(pf.amount) as amount_ttc"; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; + $sql .= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf"; + $sql .= ", ".MAIN_DB_PREFIX."paiementfourn as p"; + $sql .= " WHERE p.rowid = pf.fk_paiementfourn"; + $sql .= " AND pf.fk_facturefourn = f.rowid"; + $sql .= " AND f.entity IN (".getEntity('invoice').")"; + if ($socid) $sql .= " AND f.fk_soc = ".$socid; +} +elseif ($modecompta == "BOOKKEEPING") +{ + $sql = "SELECT date_format(b.doc_date,'%Y-%m') as dm, sum(b.debit) as amount_ttc"; + $sql .= " FROM ".MAIN_DB_PREFIX."accounting_bookkeeping as b, ".MAIN_DB_PREFIX."accounting_journal as aj"; + $sql .= " WHERE b.entity = ".$conf->entity; // In module double party accounting, we never share entities + $sql .= " AND aj.entity = ".$conf->entity; + $sql .= " AND b.code_journal = aj.code AND aj.nature = 3"; // @todo currently count amount in sale journal, but we need to define a category group for turnover +} + +$sql .= " GROUP BY dm"; +$sql .= " ORDER BY dm"; +// TODO Add a filter on $date_start and $date_end to reduce quantity on data +//print $sql; + +$minyearmonth = $maxyearmonth = 0; + +$result = $db->query($sql); +if ($result) +{ + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) + { + $obj = $db->fetch_object($result); + $cum_ht[$obj->dm] = !empty($obj->amount) ? $obj->amount : 0; + $cum[$obj->dm] = $obj->amount_ttc; + if ($obj->amount_ttc) + { + $minyearmonth = ($minyearmonth ? min($minyearmonth, $obj->dm) : $obj->dm); + $maxyearmonth = max($maxyearmonth, $obj->dm); + } + $i++; + } + $db->free($result); +} +else { + dol_print_error($db); +} + +$moreforfilter = ''; + +print '
'; +print ''."\n"; + +print ''; + +for ($annee = $year_start; $annee <= $year_end; $annee++) +{ + if ($modecompta == 'CREANCES-DETTES') print ''; + if ($annee != $year_end) print ''; +} +print ''; + +print ''; +for ($annee = $year_start; $annee <= $year_end; $annee++) +{ + if ($modecompta == 'CREANCES-DETTES') print ''; + print ''; + print ''; + if ($annee != $year_end) print ''; +} +print ''; + +$now_show_delta = 0; +$minyear = substr($minyearmonth, 0, 4); +$maxyear = substr($maxyearmonth, 0, 4); +$nowyear = strftime("%Y", dol_now()); +$nowyearmonth = strftime("%Y-%m", dol_now()); +$maxyearmonth = max($maxyearmonth, $nowyearmonth); +$now = dol_now(); +$casenow = dol_print_date($now, "%Y-%m"); + +// Loop on each month +$nb_mois_decalage = $conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START - 1) : 0; +for ($mois = 1 + $nb_mois_decalage; $mois <= 12 + $nb_mois_decalage; $mois++) +{ + $mois_modulo = $mois; // ajout + if ($mois > 12) {$mois_modulo = $mois - 12; } // ajout + + if ($year_start == $year_end) { + if ($mois > $date_endmonth && $year_end >= $date_endyear) { + break; + } + } + + print ''; + + // Month + print ""; + + for ($annee = $year_start - 1; $annee <= $year_end; $annee++) // We start one year before to have data to be able to make delta + { + $annee_decalage = $annee; + if ($mois > 12) {$annee_decalage = $annee + 1; } + $case = dol_print_date(dol_mktime(1, 1, 1, $mois_modulo, 1, $annee_decalage), "%Y-%m"); + $caseprev = dol_print_date(dol_mktime(1, 1, 1, $mois_modulo, 1, $annee_decalage - 1), "%Y-%m"); + + if ($annee >= $year_start) // We ignore $annee < $year_start, we loop on it to be able to make delta, nothing is output. + { + if ($modecompta == 'CREANCES-DETTES') { + // Valeur CA du mois w/o VAT + print '"; + } + + // Valeur CA du mois + print '"; + + // Pourcentage du mois + if ($annee_decalage > $minyear && $case <= $casenow) + { + if ($cum[$caseprev] && $cum[$case]) + { + $percent = (round(($cum[$case] - $cum[$caseprev]) / $cum[$caseprev], 4) * 100); + //print "X $cum[$case] - $cum[$caseprev] - $cum[$caseprev] - $percent X"; + print ''; + } + if ($cum[$caseprev] && !$cum[$case]) + { + print ''; + } + if (!$cum[$caseprev] && $cum[$case]) + { + //print ''; + print ''; + } + if (isset($cum[$caseprev]) && !$cum[$caseprev] && !$cum[$case]) + { + print ''; + } + if (!isset($cum[$caseprev]) && !$cum[$case]) + { + print ''; + } + } + else + { + print ''; + } + + if ($annee_decalage < $year_end || ($annee_decalage == $year_end && $mois > 12 && $annee < $year_end)) print ''; + } + + $total_ht[$annee] += ((!empty($cum_ht[$case])) ? $cum_ht[$case] : 0); + $total[$annee] += $cum[$case]; + } + + print ''; +} + +// Affiche total +print ''; +for ($annee = $year_start; $annee <= $year_end; $annee++) +{ + if ($modecompta == 'CREANCES-DETTES') { + // Montant total HT + if ($total_ht[$annee] || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) + { + print '"; + } + else + { + print ''; + } + } + + // Montant total + if ($total[$annee] || ($annee >= $minyear && $annee <= max($nowyear, $maxyear))) + { + print '"; + } + else + { + print ''; + } + + // Pourcentage total + if ($annee > $minyear && $annee <= max($nowyear, $maxyear)) + { + if ($total[$annee - 1] && $total[$annee]) { + $percent = (round(($total[$annee] - $total[$annee - 1]) / $total[$annee - 1], 4) * 100); + print ''; + } + if ($total[$annee - 1] && !$total[$annee]) + { + print ''; + } + if (!$total[$annee - 1] && $total[$annee]) + { + print ''; + } + if (!$total[$annee - 1] && !$total[$annee]) + { + print ''; + } + } + else + { + print ''; + } + + if ($annee != $year_end) print ''; +} +print "\n"; +print "
 '; + else print ''; + if ($modecompta != 'BOOKKEEPING') print ''; + print $annee; + if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1) print '-'.($annee + 1); + if ($modecompta != 'BOOKKEEPING') print ''; + print ' 
'.$langs->trans("Month").''.$langs->trans("AmountHT").''.$langs->trans("AmountTTC").''.$langs->trans("Delta").' 
".dol_print_date(dol_mktime(12, 0, 0, $mois_modulo, 1, 2000), "%B")."'; + if ($cum_ht[$case]) + { + $now_show_delta = 1; // On a trouve le premier mois de la premiere annee generant du chiffre. + print ''.price($cum_ht[$case], 1).''; + } + else + { + if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) { print '0'; } + else { print ' '; } + } + print "'; + if ($cum[$case]) + { + $now_show_delta = 1; // On a trouve le premier mois de la premiere annee generant du chiffre. + if ($modecompta != 'BOOKKEEPING') print ''; + print price($cum[$case], 1); + if ($modecompta != 'BOOKKEEPING') print ''; + } + else + { + if ($minyearmonth < $case && $case <= max($maxyearmonth, $nowyearmonth)) { print '0'; } + else { print ' '; } + } + print "'.($percent >= 0 ? "+$percent" : "$percent").'%-100%+Inf%-+0%-'; + if ($minyearmonth <= $case && $case <= $maxyearmonth) { print '-'; } + else { print ' '; } + print ' 
'.$langs->trans("Total").''.($total_ht[$annee] ?price($total_ht[$annee]) : "0")." '.($total[$annee] ?price($total[$annee]) : "0")." '.($percent >= 0 ? "+$percent" : "$percent").'%-100%+'.$langs->trans('Inf').'%+0%'; + if ($total[$annee] || ($minyear <= $annee && $annee <= max($nowyear, $maxyear))) { print '-'; } + else { print ' '; } + print ' 
"; +print '
'; + +// End of page +llxFooter(); +$db->close(); diff --git a/htdocs/langs/en_US/compta.lang b/htdocs/langs/en_US/compta.lang index 5865826dc73..eac579f595b 100644 --- a/htdocs/langs/en_US/compta.lang +++ b/htdocs/langs/en_US/compta.lang @@ -159,7 +159,7 @@ SeeReportInBookkeepingMode=See %sBookeeping report%s for a calculation on RulesAmountWithTaxIncluded=- Amounts shown are with all taxes included RulesResultDue=- It includes outstanding invoices, expenses, VAT, donations whether they are paid or not. Is also includes paid salaries.
- It is based on the validation date of invoices and VAT and on the due date for expenses. For salaries defined with Salary module, the value date of payment is used. RulesResultInOut=- It includes the real payments made on invoices, expenses, VAT and salaries.
- It is based on the payment dates of the invoices, expenses, VAT and salaries. The donation date for donation. -RulesCADue=- It includes the customer's due invoices whether they are paid or not.
- It is based on the validation date of these invoices.
+RulesCADue=- It includes the customer's due invoices whether they are paid or not.
- It is based on the invoice date of these invoices.
RulesCAIn=- It includes all the effective payments of invoices received from customers.
- It is based on the payment date of these invoices
RulesCATotalSaleJournal=It includes all credit lines from the Sale journal. RulesAmountOnInOutBookkeepingRecord=It includes record in your Ledger with accounting accounts that has the group "EXPENSE" or "INCOME" @@ -254,4 +254,9 @@ ByVatRate=By sale tax rate TurnoverbyVatrate=Turnover invoiced by sale tax rate TurnoverCollectedbyVatrate=Turnover collected by sale tax rate PurchasebyVatrate=Purchase by sale tax rate -LabelToShow=Short label \ No newline at end of file +LabelToShow=Short label +PurchaseTurnover=Purchase turnover +PurchaseTurnoverCollected=Purchase turnover collected +RulesPurchaseTurnoverDue=- It includes the supplier's due invoices whether they are paid or not.
- It is based on the invoice date of these invoices.
+RulesPurchaseTurnoverIn=- It includes all the effective payments of invoices done to suppliers.
- It is based on the payment date of these invoices
+RulesPurchaseTurnoverTotalPurchaseJournal=It includes all debit lines from the purchase journal. From 863f8dd1d6c0b5b9c25d3bfcfffbef858c3ce6a8 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sat, 4 Apr 2020 15:38:22 +0200 Subject: [PATCH 2/7] New supplier turnover by thirdparty report --- .../stats/supplier_turnover_by_thirdparty.php | 595 ++++++++++++++++++ 1 file changed, 595 insertions(+) create mode 100644 htdocs/compta/stats/supplier_turnover_by_thirdparty.php diff --git a/htdocs/compta/stats/supplier_turnover_by_thirdparty.php b/htdocs/compta/stats/supplier_turnover_by_thirdparty.php new file mode 100644 index 00000000000..341c86fbd20 --- /dev/null +++ b/htdocs/compta/stats/supplier_turnover_by_thirdparty.php @@ -0,0 +1,595 @@ + + * + * 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/stats/supplier_ca_by_thirdparty.php + * \brief Page reporting purchase turnover by thirdparty + */ + +require '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; +require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; + +// Load translation files required by the page +$langs->loadLangs(array('companies', 'categories', 'bills', 'compta')); + +// Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES') +$modecompta = $conf->global->ACCOUNTING_MODE; +if (GETPOST("modecompta")) $modecompta = GETPOST("modecompta"); + +$sortorder = isset($_GET["sortorder"]) ? $_GET["sortorder"] : $_POST["sortorder"]; +$sortfield = isset($_GET["sortfield"]) ? $_GET["sortfield"] : $_POST["sortfield"]; +if (!$sortorder) $sortorder = "asc"; +if (!$sortfield) $sortfield = "nom"; + +$socid = GETPOST('socid', 'int'); + +// Category +$selected_cat = (int) GETPOST('search_categ', 'int'); +$subcat = false; +if (GETPOST('subcat', 'alpha') === 'yes') { + $subcat = true; +} + +// Security check +if ($user->socid > 0) $socid = $user->socid; +if (!empty($conf->comptabilite->enabled)) $result = restrictedArea($user, 'compta', '', '', 'resultat'); +if (!empty($conf->accounting->enabled)) $result = restrictedArea($user, 'accounting', '', '', 'comptarapport'); + +// Date range +$year = GETPOST("year", 'int'); +$month = GETPOST("month", 'int'); +$search_societe = GETPOST("search_societe", 'alpha'); +$search_zip = GETPOST("search_zip", 'alpha'); +$search_town = GETPOST("search_town", 'alpha'); +$search_country = GETPOST("search_country", 'alpha'); +$date_startyear = GETPOST("date_startyear", 'alpha'); +$date_startmonth = GETPOST("date_startmonth", 'alpha'); +$date_startday = GETPOST("date_startday", 'alpha'); +$date_endyear = GETPOST("date_endyear", 'alpha'); +$date_endmonth = GETPOST("date_endmonth", 'alpha'); +$date_endday = GETPOST("date_endday", 'alpha'); +if (empty($year)) +{ + $year_current = strftime("%Y", dol_now()); + $month_current = strftime("%m", dol_now()); + $year_start = $year_current; +} else { + $year_current = $year; + $month_current = strftime("%m", dol_now()); + $year_start = $year; +} +$date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear")); +$date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear")); +// Quarter +if (empty($date_start) || empty($date_end)) // We define date_start and date_end +{ + $q = GETPOST("q", "int") ?GETPOST("q", "int") : 0; + if (empty($q)) + { + // We define date_start and date_end + $month_start = GETPOST("month") ?GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); + $year_end = $year_start; + $month_end = $month_start; + if (!GETPOST("month")) // If month not forced + { + 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++; + } + $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); } +} +else +{ + // TODO We define q +} + +// $date_start and $date_end are defined. We force $year_start and $nbofyear +$tmps = dol_getdate($date_start); +$year_start = $tmps['year']; +$tmpe = dol_getdate($date_end); +$year_end = $tmpe['year']; +$nbofyear = ($year_end - $year_start) + 1; + +$commonparams = array(); +$commonparams['modecompta'] = $modecompta; +$commonparams['sortorder'] = $sortorder; +$commonparams['sortfield'] = $sortfield; + +$headerparams = array(); +$headerparams['date_startyear'] = $date_startyear; +$headerparams['date_startmonth'] = $date_startmonth; +$headerparams['date_startday'] = $date_startday; +$headerparams['date_endyear'] = $date_endyear; +$headerparams['date_endmonth'] = $date_endmonth; +$headerparams['date_endday'] = $date_endday; +$headerparams['q'] = $q; + +$tableparams = array(); +$tableparams['search_categ'] = $selected_cat; +$tableparams['search_societe'] = $search_societe; +$tableparams['search_zip'] = $search_zip; +$tableparams['search_town'] = $search_town; +$tableparams['search_country'] = $search_country; +$tableparams['subcat'] = ($subcat === true) ? 'yes' : ''; + +// Adding common parameters +$allparams = array_merge($commonparams, $headerparams, $tableparams); +$headerparams = array_merge($commonparams, $headerparams); +$tableparams = array_merge($commonparams, $tableparams); + +foreach ($allparams as $key => $value) { + $paramslink .= '&'.$key.'='.$value; +} + + +/* + * View + */ + +llxHeader(); + +$form = new Form($db); +$thirdparty_static = new Societe($db); +$formother = new FormOther($db); + +// TODO Report from bookkeeping not yet available, so we switch on report on business events +if ($modecompta == "BOOKKEEPING") $modecompta = "CREANCES-DETTES"; +if ($modecompta == "BOOKKEEPINGCOLLECTED") $modecompta = "RECETTES-DEPENSES"; + +// Show report header +if ($modecompta == "CREANCES-DETTES") +{ + $name = $langs->trans("PurchaseTurnover").', '.$langs->trans("ByThirdParties"); + $calcmode = $langs->trans("CalcModeDebt"); + //$calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'','').')'; + $description = $langs->trans("RulesPurchaseTurnoverDue"); + $builddate = dol_now(); + //$exportlink=$langs->trans("NotYetAvailable"); +} +elseif ($modecompta == "RECETTES-DEPENSES") +{ + $name = $langs->trans("PurchaseTurnoverCollected").', '.$langs->trans("ByThirdParties"); + $calcmode = $langs->trans("CalcModeEngagement"); + //$calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')'; + $description = $langs->trans("RulesPurchaseTurnoverIn"); + $description .= $langs->trans("DepositsAreIncluded"); + $builddate = dol_now(); + //$exportlink=$langs->trans("NotYetAvailable"); +} +elseif ($modecompta == "BOOKKEEPING") +{ +} +elseif ($modecompta == "BOOKKEEPINGCOLLECTED") +{ +} +$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0); +if ($date_end == dol_time_plus_duree($date_start, 1, 'y') - 1) $periodlink = ''.img_previous().' '.img_next().''; +else $periodlink = ''; + +report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $tableparams, $calcmode); + +if (!empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING') +{ + print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1); +} + + +$name = array(); + +// Show Array +$catotal = 0; +if ($modecompta == 'CREANCES-DETTES') { + $sql = "SELECT DISTINCT s.rowid as socid, s.nom as name, s.zip, s.town, s.fk_pays,"; + $sql .= " sum(f.total_ht) as amount, sum(f.total_ttc) as amount_ttc"; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f, ".MAIN_DB_PREFIX."societe as s"; + if ($selected_cat === -2) // Without any category + { + $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."categorie_societe as cs ON s.rowid = cs.fk_soc"; + } + elseif ($selected_cat) // Into a specific category + { + $sql .= ", ".MAIN_DB_PREFIX."categorie as c, ".MAIN_DB_PREFIX."categorie_societe as cs"; + } + $sql .= " WHERE f.fk_statut in (1,2)"; + $sql .= " AND f.type IN (0,2)"; + $sql .= " AND f.fk_soc = s.rowid"; + if ($date_start && $date_end) { + $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; + } + if ($selected_cat === -2) // Without any category + { + $sql .= " AND cs.fk_soc is null"; + } + elseif ($selected_cat) { // Into a specific category + $sql .= " AND (c.rowid = ".$db->escape($selected_cat); + if ($subcat) $sql .= " OR c.fk_parent = ".$db->escape($selected_cat); + $sql .= ")"; + $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_soc = s.rowid"; + } +} else { + $sql = "SELECT s.rowid as socid, s.nom as name, s.zip, s.town, s.fk_pays, sum(pf.amount) as amount_ttc"; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; + $sql .= ", ".MAIN_DB_PREFIX."paiementfourn_facturefourn as pf"; + $sql .= ", ".MAIN_DB_PREFIX."paiementfourn as p"; + $sql .= ", ".MAIN_DB_PREFIX."societe as s"; + if ($selected_cat === -2) // Without any category + { + $sql .= " LEFT OUTER JOIN ".MAIN_DB_PREFIX."categorie_societe as cs ON s.rowid = cs.fk_soc"; + } + elseif ($selected_cat) // Into a specific category + { + $sql .= ", ".MAIN_DB_PREFIX."categorie as c, ".MAIN_DB_PREFIX."categorie_societe as cs"; + } + $sql .= " WHERE p.rowid = pf.fk_paiementfourn"; + $sql .= " AND pf.fk_facturefourn = f.rowid"; + $sql .= " AND f.fk_soc = s.rowid"; + if ($date_start && $date_end) { + $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'"; + } + if ($selected_cat === -2) // Without any category + { + $sql .= " AND cs.fk_soc is null"; + } + elseif ($selected_cat) { // Into a specific category + $sql .= " AND (c.rowid = ".$selected_cat; + if ($subcat) $sql .= " OR c.fk_parent = ".$selected_cat; + $sql .= ")"; + $sql .= " AND cs.fk_categorie = c.rowid AND cs.fk_soc = s.rowid"; + } +} +if (!empty($search_societe)) $sql .= natural_search('s.nom', $search_societe); +if (!empty($search_zip)) $sql .= natural_search('s.zip', $search_zip); +if (!empty($search_town)) $sql .= natural_search('s.town', $search_town); +if ($search_country > 0) $sql .= ' AND s.fk_pays = '.$search_country.''; +$sql .= " AND f.entity IN (".getEntity('invoice').")"; +if ($socid) $sql .= " AND f.fk_soc = ".$socid; +$sql .= " GROUP BY s.rowid, s.nom, s.zip, s.town, s.fk_pays"; +$sql .= " ORDER BY s.rowid"; +//echo $sql; + +dol_syslog("supplier_turnover_by_thirdparty", LOG_DEBUG); +$result = $db->query($sql); +if ($result) { + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) { + $obj = $db->fetch_object($result); + $amount_ht[$obj->socid] = $obj->amount; + $amount[$obj->socid] = $obj->amount_ttc; + $name[$obj->socid] = $obj->name.' '.$obj->firstname; + $address_zip[$obj->socid] = $obj->zip; + $address_town[$obj->socid] = $obj->town; + $address_pays[$obj->socid] = getCountry($obj->fk_pays); + $catotal_ht += $obj->amount; + $catotal += $obj->amount_ttc; + $i++; + } +} else { + dol_print_error($db); +} + +// Show array +$i = 0; +print '
'; +// Extra parameters management +foreach ($headerparams as $key => $value) +{ + print ''; +} + +$moreforfilter = ''; + +print '
'; +print ''."\n"; + +// Category filter +print ''; +print ''; +print ''; +print ''; + +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; + +// Array titles +print ""; +print_liste_field_titre( + $langs->trans("Company"), + $_SERVER["PHP_SELF"], + "nom", + "", + $paramslink, + "", + $sortfield, $sortorder + ); +print_liste_field_titre( + $langs->trans("Zip"), + $_SERVER["PHP_SELF"], + "zip", + "", + $paramslink, + "", + $sortfield, $sortorder + ); +print_liste_field_titre( + $langs->trans("Town"), + $_SERVER["PHP_SELF"], + "town", + "", + $paramslink, + "", + $sortfield, $sortorder + ); +print_liste_field_titre( + $langs->trans("Country"), + $_SERVER["PHP_SELF"], + "country", + "", + $paramslink, + "", + $sortfield, $sortorder + ); +if ($modecompta == 'CREANCES-DETTES') { + print_liste_field_titre( + $langs->trans('AmountHT'), + $_SERVER["PHP_SELF"], + "amount_ht", + "", + $paramslink, + 'class="right"', + $sortfield, + $sortorder + ); +} else { + print_liste_field_titre(''); +} +print_liste_field_titre( + $langs->trans("AmountTTC"), + $_SERVER["PHP_SELF"], + "amount_ttc", + "", + $paramslink, + 'class="right"', + $sortfield, + $sortorder +); +print_liste_field_titre( + $langs->trans("Percentage"), + $_SERVER["PHP_SELF"], + "amount_ttc", + "", + $paramslink, + 'class="right"', + $sortfield, + $sortorder +); +print_liste_field_titre( + $langs->trans("OtherStatistics"), + $_SERVER["PHP_SELF"], + "", + "", + "", + 'align="center" width="20%"' +); +print "\n"; + + +if (count($amount)) { + $arrayforsort = $name; + // Defining array arrayforsort + if ($sortfield == 'nom' && $sortorder == 'asc') { + asort($name); + $arrayforsort = $name; + } + if ($sortfield == 'nom' && $sortorder == 'desc') { + arsort($name); + $arrayforsort = $name; + } + if ($sortfield == 'amount_ht' && $sortorder == 'asc') { + asort($amount_ht); + $arrayforsort = $amount_ht; + } + if ($sortfield == 'amount_ht' && $sortorder == 'desc') { + arsort($amount_ht); + $arrayforsort = $amount_ht; + } + if ($sortfield == 'amount_ttc' && $sortorder == 'asc') { + asort($amount); + $arrayforsort = $amount; + } + if ($sortfield == 'amount_ttc' && $sortorder == 'desc') { + arsort($amount); + $arrayforsort = $amount; + } + if ($sortfield == 'zip' && $sortorder == 'asc') { + asort($address_zip); + $arrayforsort = $address_zip; + } + if ($sortfield == 'zip' && $sortorder == 'desc') { + arsort($address_zip); + $arrayforsort = $address_zip; + } + if ($sortfield == 'town' && $sortorder == 'asc') { + asort($address_town); + $arrayforsort = $address_town; + } + if ($sortfield == 'town' && $sortorder == 'desc') { + arsort($address_town); + $arrayforsort = $address_town; + } + if ($sortfield == 'country' && $sortorder == 'asc') { + asort($address_pays); + $arrayforsort = $address_town; + } + if ($sortfield == 'country' && $sortorder == 'desc') { + arsort($address_pays); + $arrayforsort = $address_town; + } + + foreach ($arrayforsort as $key=>$value) { + print ''; + + // Third party + $fullname = $name[$key]; + if ($key > 0) { + $thirdparty_static->id = $key; + $thirdparty_static->name = $fullname; + $thirdparty_static->client = 1; + $linkname = $thirdparty_static->getNomUrl(1, 'supplier'); + } else { + $linkname = $langs->trans("PaymentsNotLinkedToInvoice"); + } + print "\n"; + + print ''; + + print ''; + + print ''; + + // Amount w/o VAT + print ''; + + // Amount with VAT + print ''; + + // Percent; + print ''; + + // Other stats + print ''; + print "\n"; + $i++; + } + + // Total + print ''; + print ''; + print ''; + print ''; + print ''; + if ($modecompta != 'CREANCES-DETTES') { + print ''; + } else { + print ''; + } + print ''; + print ''; + print ''; + print ''; + + $db->free($result); +} + +print "
'; +print $langs->trans("Category").': '.$formother->select_categories(Categorie::TYPE_SUPPLIER, $selected_cat, 'search_categ', true); +print ' '; +print $langs->trans("SubCats").'? '; +print ''; +print ''; +print '
'; +print ''; +print ''; +print ''; +print ''; +print ''; +print ''; +print $form->select_country($search_country, 'search_country'); +//print ''; +print '    
".$linkname."'; + print $address_zip[$key]; + print ''; + print $address_town[$key]; + print ''; + print $address_pays[$key]; + print ''; + if ($modecompta != 'CREANCES-DETTES') { + if ($key > 0) { + print ''; + } else { + print ''; + } + } else { + if ($key > 0) { + print ''; + } else { + print ''; + } + print price($amount_ht[$key]); + } + print ''; + if ($modecompta != 'CREANCES-DETTES') { + if ($key > 0) { + print ''; + } else { + print ''; + } + } else { + if ($key > 0) { + print ''; + } else { + print ''; + } + } + print price($amount[$key]); + print ''; + print ''.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).'%' : ' ').''; + if (!empty($conf->supplier_proposal->enabled) && $key > 0) { + print ' '.img_picto($langs->trans("ProposalStats"), "stats").' '; + } + if (!empty($conf->fournisseur->enabled) && $key > 0) { + print ' '.img_picto($langs->trans("OrderStats"), "stats").' '; + } + if (!empty($conf->fournisseur->enabled) && $key > 0) { + print ' '.img_picto($langs->trans("InvoiceStats"), "stats").' '; + } + print '
'.$langs->trans("Total").'   '.price($catotal_ht).''.price($catotal).'  
"; +print "
"; + +print '
'; + +// End of page +llxFooter(); +$db->close(); From 1adfe5c21f71517b4b97d69bb64e549827865673 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sat, 4 Apr 2020 15:45:32 +0200 Subject: [PATCH 3/7] New supplier turnover by product service report --- htdocs/compta/stats/supplier_turnover_by_thirdparty.php | 1 - 1 file changed, 1 deletion(-) diff --git a/htdocs/compta/stats/supplier_turnover_by_thirdparty.php b/htdocs/compta/stats/supplier_turnover_by_thirdparty.php index 341c86fbd20..6894ac1b8f6 100644 --- a/htdocs/compta/stats/supplier_turnover_by_thirdparty.php +++ b/htdocs/compta/stats/supplier_turnover_by_thirdparty.php @@ -182,7 +182,6 @@ elseif ($modecompta == "RECETTES-DEPENSES") $calcmode = $langs->trans("CalcModeEngagement"); //$calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')'; $description = $langs->trans("RulesPurchaseTurnoverIn"); - $description .= $langs->trans("DepositsAreIncluded"); $builddate = dol_now(); //$exportlink=$langs->trans("NotYetAvailable"); } From 79e5099c6dbd6b9efcf471e40af6ad91fe874dd3 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sat, 4 Apr 2020 18:45:33 +0200 Subject: [PATCH 4/7] Add menu entries --- htdocs/core/menus/standard/eldy.lib.php | 21 +++++++++++++++++++++ htdocs/langs/en_US/compta.lang | 2 ++ 2 files changed, 23 insertions(+) diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index 42b15114379..ab95d5765ac 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -1376,6 +1376,27 @@ function print_left_eldy_menu($db, $menu_array_before, $menu_array_after, &$tabM //$newmenu->add("/compta/stats/byratecountry.php?leftmenu=accountancy_report&modecompta=".$modecompta, $langs->trans("ByVatRate"),3,$user->rights->accounting->comptarapport->lire); } } + + $modecompta = 'CREANCES-DETTES'; + if (!empty($conf->accounting->enabled) && !empty($user->rights->accounting->comptarapport->lire) && $mainmenu == 'accountancy') $modecompta = 'BOOKKEEPING'; // Not yet implemented. Should be BOOKKEEPINGCOLLECTED + if ($modecompta && $conf->fournisseur->enabled) + { + if ($usemenuhider || empty($leftmenu) || preg_match('/accountancy_report/', $leftmenu)) { + $newmenu->add("/compta/stats/supplier_turnover.php?leftmenu=accountancy_report&modecompta=".$modecompta, $langs->trans("ReportPurchaseTurnover"), 2, $user->rights->accounting->comptarapport->lire); + $newmenu->add("/compta/stats/supplier_turnover_by_thirdparty.php?leftmenu=accountancy_report&modecompta=".$modecompta, $langs->trans("ByCompanies"), 3, $user->rights->accounting->comptarapport->lire); + $newmenu->add("/compta/stats/supplier_turnover_by_prodserv.php?leftmenu=accountancy_report&modecompta=".$modecompta, $langs->trans("ByProductsAndServices"), 3, $user->rights->accounting->comptarapport->lire); + } + } + + $modecompta = 'RECETTES-DEPENSES'; + //if (! empty($conf->accounting->enabled) && ! empty($user->rights->accounting->comptarapport->lire) && $mainmenu == 'accountancy') $modecompta=''; // Not yet implemented. Should be BOOKKEEPINGCOLLECTED + if ($modecompta && $conf->fournisseur->enabled) + { + if ($usemenuhider || empty($leftmenu) || preg_match('/accountancy_report/', $leftmenu)) { + $newmenu->add("/compta/stats/supplier_turnover.php?leftmenu=accountancy_report&modecompta=".$modecompta, $langs->trans("ReportPurchaseTurnoverCollected"), 2, $user->rights->accounting->comptarapport->lire); + $newmenu->add("/compta/stats/supplier_turnover_by_thirdparty.php?leftmenu=accountancy_report&modecompta=".$modecompta, $langs->trans("ByCompanies"), 3, $user->rights->accounting->comptarapport->lire); + } + } } // Accountancy (simple) diff --git a/htdocs/langs/en_US/compta.lang b/htdocs/langs/en_US/compta.lang index eac579f595b..7cce228ca48 100644 --- a/htdocs/langs/en_US/compta.lang +++ b/htdocs/langs/en_US/compta.lang @@ -260,3 +260,5 @@ PurchaseTurnoverCollected=Purchase turnover collected RulesPurchaseTurnoverDue=- It includes the supplier's due invoices whether they are paid or not.
- It is based on the invoice date of these invoices.
RulesPurchaseTurnoverIn=- It includes all the effective payments of invoices done to suppliers.
- It is based on the payment date of these invoices
RulesPurchaseTurnoverTotalPurchaseJournal=It includes all debit lines from the purchase journal. +ReportPurchaseTurnover=Purchase turnover invoiced +ReportPurchaseTurnoverCollected=Purchase turnover collected From fe876b96fe784b9d6e7e5a8798fb89906e79226a Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sat, 4 Apr 2020 18:49:30 +0200 Subject: [PATCH 5/7] Fix links + getEntity --- htdocs/compta/stats/supplier_turnover.php | 6 +++--- htdocs/compta/stats/supplier_turnover_by_thirdparty.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/htdocs/compta/stats/supplier_turnover.php b/htdocs/compta/stats/supplier_turnover.php index 33cec605eed..72b3aa2e77c 100644 --- a/htdocs/compta/stats/supplier_turnover.php +++ b/htdocs/compta/stats/supplier_turnover.php @@ -163,7 +163,7 @@ if ($modecompta == 'CREANCES-DETTES') $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; $sql .= " WHERE f.fk_statut in (1,2)"; $sql .= " AND f.type IN (0,2)"; - $sql .= " AND f.entity IN (".getEntity('invoice').")"; + $sql .= " AND f.entity IN (".getEntity('supplier_invoice').")"; if ($socid) $sql .= " AND f.fk_soc = ".$socid; } elseif ($modecompta == "RECETTES-DEPENSES") @@ -174,7 +174,7 @@ elseif ($modecompta == "RECETTES-DEPENSES") $sql .= ", ".MAIN_DB_PREFIX."paiementfourn as p"; $sql .= " WHERE p.rowid = pf.fk_paiementfourn"; $sql .= " AND pf.fk_facturefourn = f.rowid"; - $sql .= " AND f.entity IN (".getEntity('invoice').")"; + $sql .= " AND f.entity IN (".getEntity('supplier_invoice').")"; if ($socid) $sql .= " AND f.fk_soc = ".$socid; } elseif ($modecompta == "BOOKKEEPING") @@ -227,7 +227,7 @@ for ($annee = $year_start; $annee <= $year_end; $annee++) { if ($modecompta == 'CREANCES-DETTES') print ''; else print ''; - if ($modecompta != 'BOOKKEEPING') print ''; + if ($modecompta != 'BOOKKEEPING') print ''; print $annee; if ($conf->global->SOCIETE_FISCAL_MONTH_START > 1) print '-'.($annee + 1); if ($modecompta != 'BOOKKEEPING') print ''; diff --git a/htdocs/compta/stats/supplier_turnover_by_thirdparty.php b/htdocs/compta/stats/supplier_turnover_by_thirdparty.php index 6894ac1b8f6..e4cfc53c59a 100644 --- a/htdocs/compta/stats/supplier_turnover_by_thirdparty.php +++ b/htdocs/compta/stats/supplier_turnover_by_thirdparty.php @@ -270,7 +270,7 @@ if (!empty($search_societe)) $sql .= natural_search('s.nom', $search_societe); if (!empty($search_zip)) $sql .= natural_search('s.zip', $search_zip); if (!empty($search_town)) $sql .= natural_search('s.town', $search_town); if ($search_country > 0) $sql .= ' AND s.fk_pays = '.$search_country.''; -$sql .= " AND f.entity IN (".getEntity('invoice').")"; +$sql .= " AND f.entity IN (".getEntity('supplier_invoice').")"; if ($socid) $sql .= " AND f.fk_soc = ".$socid; $sql .= " GROUP BY s.rowid, s.nom, s.zip, s.town, s.fk_pays"; $sql .= " ORDER BY s.rowid"; From 60bd0e49ecf29f502098b8bc1795280824b5f237 Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sat, 4 Apr 2020 18:57:51 +0200 Subject: [PATCH 6/7] Fix missing token --- htdocs/compta/stats/cabyprodserv.php | 1 + htdocs/compta/stats/cabyuser.php | 1 + htdocs/compta/stats/casoc.php | 1 + htdocs/compta/stats/supplier_turnover_by_thirdparty.php | 1 + 4 files changed, 4 insertions(+) diff --git a/htdocs/compta/stats/cabyprodserv.php b/htdocs/compta/stats/cabyprodserv.php index a1bdfb6678d..762ebe0fbcf 100644 --- a/htdocs/compta/stats/cabyprodserv.php +++ b/htdocs/compta/stats/cabyprodserv.php @@ -290,6 +290,7 @@ if ($modecompta == 'CREANCES-DETTES') // Show Array $i = 0; print '
'; + print ''."\n"; // Extra parameters management foreach ($headerparams as $key => $value) { diff --git a/htdocs/compta/stats/cabyuser.php b/htdocs/compta/stats/cabyuser.php index fb9b06025aa..9a4ce5b0427 100644 --- a/htdocs/compta/stats/cabyuser.php +++ b/htdocs/compta/stats/cabyuser.php @@ -195,6 +195,7 @@ $name = array(); // Show array print ''; +print ''."\n"; // Extra parameters management foreach ($headerparams as $key => $value) { diff --git a/htdocs/compta/stats/casoc.php b/htdocs/compta/stats/casoc.php index 5e7340f20c8..c563d8c5ae6 100644 --- a/htdocs/compta/stats/casoc.php +++ b/htdocs/compta/stats/casoc.php @@ -352,6 +352,7 @@ if ($modecompta != 'CREANCES-DETTES') { // Show array $i = 0; print ''; +print ''."\n"; // Extra parameters management foreach ($headerparams as $key => $value) { diff --git a/htdocs/compta/stats/supplier_turnover_by_thirdparty.php b/htdocs/compta/stats/supplier_turnover_by_thirdparty.php index e4cfc53c59a..ea55adcc8c2 100644 --- a/htdocs/compta/stats/supplier_turnover_by_thirdparty.php +++ b/htdocs/compta/stats/supplier_turnover_by_thirdparty.php @@ -300,6 +300,7 @@ if ($result) { // Show array $i = 0; print ''; +print ''."\n"; // Extra parameters management foreach ($headerparams as $key => $value) { From fd5b2ba3910edafeac1ab8a592d2ec3e39522fcd Mon Sep 17 00:00:00 2001 From: Maxime Kohlhaas Date: Sat, 4 Apr 2020 23:29:45 +0200 Subject: [PATCH 7/7] New supplier turnover by product service report --- .../stats/supplier_turnover_by_prodserv.php | 450 ++++++++++++++++++ 1 file changed, 450 insertions(+) create mode 100644 htdocs/compta/stats/supplier_turnover_by_prodserv.php diff --git a/htdocs/compta/stats/supplier_turnover_by_prodserv.php b/htdocs/compta/stats/supplier_turnover_by_prodserv.php new file mode 100644 index 00000000000..fafb967ace1 --- /dev/null +++ b/htdocs/compta/stats/supplier_turnover_by_prodserv.php @@ -0,0 +1,450 @@ + + * + * 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/stats/supplier_turnover_by_prodserv.php + * \brief Page reporting purchase turnover by Products & Services + */ + +require '../../main.inc.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; +require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; +require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; + +// Load translation files required by the page +$langs->loadLangs(array("products", "categories", "errors", 'accountancy')); + +// Security pack (data & check) +$socid = GETPOST('socid', 'int'); + +if ($user->socid > 0) $socid = $user->socid; +if (!empty($conf->comptabilite->enabled)) $result = restrictedArea($user, 'compta', '', '', 'resultat'); +if (!empty($conf->accounting->enabled)) $result = restrictedArea($user, 'accounting', '', '', 'comptarapport'); + +// Define modecompta ('CREANCES-DETTES' or 'RECETTES-DEPENSES') +$modecompta = $conf->global->ACCOUNTING_MODE; +if (GETPOST("modecompta")) $modecompta = GETPOST("modecompta"); + +$sortorder = isset($_GET["sortorder"]) ? $_GET["sortorder"] : $_POST["sortorder"]; +$sortfield = isset($_GET["sortfield"]) ? $_GET["sortfield"] : $_POST["sortfield"]; +if (!$sortorder) $sortorder = "asc"; +if (!$sortfield) $sortfield = "ref"; + +// Category +$selected_cat = (int) GETPOST('search_categ', 'int'); +$selected_soc = (int) GETPOST('search_soc', 'int'); +$subcat = false; +if (GETPOST('subcat', 'alpha') === 'yes') { + $subcat = true; +} +// product/service +$selected_type = GETPOST('search_type', 'int'); +if ($selected_type == '') $selected_type = -1; + +// Date range +$year = GETPOST("year"); +$month = GETPOST("month"); +$date_startyear = GETPOST("date_startyear"); +$date_startmonth = GETPOST("date_startmonth"); +$date_startday = GETPOST("date_startday"); +$date_endyear = GETPOST("date_endyear"); +$date_endmonth = GETPOST("date_endmonth"); +$date_endday = GETPOST("date_endday"); +if (empty($year)) +{ + $year_current = strftime("%Y", dol_now()); + $month_current = strftime("%m", dol_now()); + $year_start = $year_current; +} else { + $year_current = $year; + $month_current = strftime("%m", dol_now()); + $year_start = $year; +} +$date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear")); +$date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear")); +// Quarter +if (empty($date_start) || empty($date_end)) // We define date_start and date_end +{ + $q = GETPOST("q", "int"); + if (empty($q)) + { + // We define date_start and date_end + $month_start = GETPOST("month") ?GETPOST("month") : ($conf->global->SOCIETE_FISCAL_MONTH_START ? ($conf->global->SOCIETE_FISCAL_MONTH_START) : 1); + $year_end = $year_start; + $month_end = $month_start; + if (!GETPOST("month")) // If month not forced + { + 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++; + } + $date_start = dol_get_first_day($year_start, $month_start, false); $date_end = dol_get_last_day($year_end, $month_end, false); + } + else + { + 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); } + } +} else { + // TODO We define q +} + +// $date_start and $date_end are defined. We force $year_start and $nbofyear +$tmps = dol_getdate($date_start); +$year_start = $tmps['year']; +$tmpe = dol_getdate($date_end); +$year_end = $tmpe['year']; +$nbofyear = ($year_end - $year_start) + 1; + +$commonparams = array(); +if (!empty($modecompta)) $commonparams['modecompta'] = $modecompta; +if (!empty($sortorder)) $commonparams['sortorder'] = $sortorder; +if (!empty($sortfield)) $commonparams['sortfield'] = $sortfield; + +$headerparams = array(); +if (!empty($date_startyear)) $headerparams['date_startyear'] = $date_startyear; +if (!empty($date_startmonth)) $headerparams['date_startmonth'] = $date_startmonth; +if (!empty($date_startday)) $headerparams['date_startday'] = $date_startday; +if (!empty($date_endyear)) $headerparams['date_endyear'] = $date_endyear; +if (!empty($date_endmonth)) $headerparams['date_endmonth'] = $date_endmonth; +if (!empty($date_endday)) $headerparams['date_endday'] = $date_endday; +if (!empty($year)) $headerparams['year'] = $year; +if (!empty($month)) $headerparams['month'] = $month; +$headerparams['q'] = $q; + +$tableparams = array(); +if (!empty($selected_cat)) $tableparams['search_categ'] = $selected_cat; +if (!empty($selected_soc)) $tableparams['search_soc'] = $selected_soc; +if (!empty($selected_type)) $tableparams['search_type'] = $selected_type; +$tableparams['subcat'] = ($subcat === true) ? 'yes' : ''; + +// Adding common parameters +$allparams = array_merge($commonparams, $headerparams, $tableparams); +$headerparams = array_merge($commonparams, $headerparams); +$tableparams = array_merge($commonparams, $tableparams); + +foreach ($allparams as $key => $value) { + $paramslink .= '&'.$key.'='.$value; +} + + +/* + * View + */ + +llxHeader(); + +$form = new Form($db); +$formother = new FormOther($db); + +// TODO Report from bookkeeping not yet available, so we switch on report on business events +if ($modecompta == "BOOKKEEPING") $modecompta = "CREANCES-DETTES"; +if ($modecompta == "BOOKKEEPINGCOLLECTED") $modecompta = "RECETTES-DEPENSES"; + +// Show report header +if ($modecompta == "CREANCES-DETTES") { + $name = $langs->trans("PurchaseTurnover").', '.$langs->trans("ByProductsAndServices"); + $calcmode = $langs->trans("CalcModeDebt"); + //$calcmode.='
('.$langs->trans("SeeReportInInputOutputMode",'','').')'; + + $description = $langs->trans("RulesPurchaseTurnoverDue"); + $builddate = dol_now(); +} +elseif ($modecompta == "RECETTES-DEPENSES") +{ + $name = $langs->trans("PurchaseTurnoverCollected").', '.$langs->trans("ByProductsAndServices"); + $calcmode = $langs->trans("CalcModeEngagement"); + //$calcmode.='
('.$langs->trans("SeeReportInDueDebtMode",'','').')'; + $description = $langs->trans("RulesPurchaseTurnoverIn"); + + $builddate = dol_now(); +} +elseif ($modecompta == "BOOKKEEPING") +{ +} +elseif ($modecompta == "BOOKKEEPINGCOLLECTED") +{ +} + +$period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0); +if ($date_end == dol_time_plus_duree($date_start, 1, 'y') - 1) $periodlink = ''.img_previous().' '.img_next().''; +else $periodlink = ''; + +report_header($name, $namelink, $period, $periodlink, $description, $builddate, $exportlink, $tableparams, $calcmode); + +if (!empty($conf->accounting->enabled) && $modecompta != 'BOOKKEEPING') +{ + print info_admin($langs->trans("WarningReportNotReliable"), 0, 0, 1); +} + + + +$name = array(); + +// SQL request +$catotal = 0; +$catotal_ht = 0; +$qtytotal = 0; + +if ($modecompta == 'CREANCES-DETTES') +{ + $sql = "SELECT DISTINCT p.rowid as rowid, p.ref as ref, p.label as label, p.fk_product_type as product_type,"; + $sql .= " SUM(l.total_ht) as amount, SUM(l.total_ttc) as amount_ttc,"; + $sql .= " SUM(CASE WHEN f.type = 2 THEN -l.qty ELSE l.qty END) as qty"; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f"; + if ($selected_soc > 0) $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."societe as soc ON (soc.rowid = f.fk_soc)"; + $sql .= ",".MAIN_DB_PREFIX."facture_fourn_det as l"; + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p ON l.fk_product = p.rowid"; + if ($selected_cat === -2) // Without any category + { + $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."categorie_product as cp ON p.rowid = cp.fk_product"; + } + elseif ($selected_cat) // Into a specific category + { + $sql .= ", ".MAIN_DB_PREFIX."categorie as c, ".MAIN_DB_PREFIX."categorie_product as cp"; + } + $sql .= " WHERE l.fk_facture_fourn = f.rowid"; + $sql .= " AND f.fk_statut in (1,2)"; + $sql .= " AND f.type IN (0,2)"; + + if ($date_start && $date_end) { + $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'"; + } + if ($selected_type >= 0) + { + $sql .= " AND l.product_type = ".$selected_type; + } + if ($selected_cat === -2) // Without any category + { + $sql .= " AND cp.fk_product is null"; + } + elseif ($selected_cat) { // Into a specific category + $sql .= " AND (c.rowid = ".$selected_cat; + if ($subcat) $sql .= " OR c.fk_parent = ".$selected_cat; + $sql .= ")"; + $sql .= " AND cp.fk_categorie = c.rowid AND cp.fk_product = p.rowid"; + } + if ($selected_soc > 0) $sql .= " AND soc.rowid=".$selected_soc; + $sql .= " AND f.entity IN (".getEntity('supplier_invoice').")"; + $sql .= " GROUP BY p.rowid, p.ref, p.label, p.fk_product_type"; + $sql .= $db->order($sortfield, $sortorder); + + dol_syslog("supplier_turnover_by_prodserv", LOG_DEBUG); + $result = $db->query($sql); + if ($result) { + $num = $db->num_rows($result); + $i = 0; + while ($i < $num) { + $obj = $db->fetch_object($result); + $amount_ht[$obj->rowid] = $obj->amount; + $amount[$obj->rowid] = $obj->amount_ttc; + $qty[$obj->rowid] = $obj->qty; + $name[$obj->rowid] = $obj->ref.' - '.$obj->label; + $type[$obj->rowid] = $obj->product_type; + $catotal_ht += $obj->amount; + $catotal += $obj->amount_ttc; + $qtytotal += $obj->qty; + $i++; + } + } else { + dol_print_error($db); + } + + // Show Array + $i = 0; + print ''; + print ''."\n"; + // Extra parameters management + foreach ($headerparams as $key => $value) + { + print ''; + } + + $moreforfilter = ''; + + print '
'; + print ''."\n"; + + // Category filter + print ''; + print ''; + + print ''; + + // Array header + print ""; + print_liste_field_titre( + $langs->trans("Product"), + $_SERVER["PHP_SELF"], + "ref", + "", + $paramslink, + "", + $sortfield, + $sortorder + ); + print_liste_field_titre( + $langs->trans('Quantity'), + $_SERVER["PHP_SELF"], + "qty", + "", + $paramslink, + 'class="right"', + $sortfield, + $sortorder + ); + print_liste_field_titre( + $langs->trans("Percentage"), + $_SERVER["PHP_SELF"], + "qty", + "", + $paramslink, + 'class="right"', + $sortfield, + $sortorder + ); + print_liste_field_titre( + $langs->trans('AmountHT'), + $_SERVER["PHP_SELF"], + "amount", + "", + $classslink, + 'class="right"', + $sortfield, + $sortorder + ); + print_liste_field_titre( + $langs->trans("AmountTTC"), + $_SERVER["PHP_SELF"], + "amount_ttc", + "", + $paramslink, + 'class="right"', + $sortfield, + $sortorder + ); + print_liste_field_titre( + $langs->trans("Percentage"), + $_SERVER["PHP_SELF"], + "amount_ttc", + "", + $paramslink, + 'class="right"', + $sortfield, + $sortorder + ); + print "\n"; + + if (count($name)) { + foreach ($name as $key=>$value) { + print ''; + + // Product + print "\n"; + + // Quantity + print ''; + + // Percent; + print ''; + + // Amount w/o VAT + print ''; + + // Amount with VAT + print ''; + + // Percent; + print ''; + + // TODO: statistics? + + print "\n"; + $i++; + } + + // Total + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + $db->free($result); + } + print "
'; + print $langs->trans("Category").': '.$formother->select_categories(Categorie::TYPE_PRODUCT, $selected_cat, 'search_categ', true); + print ' '; + print $langs->trans("SubCats").'? '; + print ''; + // type filter (produit/service) + print ' '; + print $langs->trans("Type").': '; + $form->select_type_of_lines(isset($selected_type) ? $selected_type : -1, 'search_type', 1, 1, 1); + + //select thirdparty + print '
'; + print $langs->trans("ThirdParty").': '.$form->select_thirdparty_list($selected_soc, 'search_soc', '', 1); + print '
'; + print ''; + print '
"; + $fullname = $name[$key]; + if ($key > 0) { + $linkname = ''.img_object($langs->trans("ShowProduct"), $type[$key] == 0 ? 'product' : 'service').' '.$fullname.''; + } else { + $linkname = $langs->trans("PaymentsNotLinkedToProduct"); + } + print $linkname; + print "'; + print $qty[$key]; + print ''.($qtytotal > 0 ? round(100 * $qty[$key] / $qtytotal, 2).'%' : ' ').''; + print price($amount_ht[$key]); + //print ''; + print ''; + print price($amount[$key]); + //print ''; + print ''.($catotal > 0 ? round(100 * $amount[$key] / $catotal, 2).'%' : ' ').'
'.$langs->trans("Total").''.$qtytotal.'100%'.price($catotal_ht).''.price($catotal).'100%
"; + print '
'; + + print ''; +} else { + // $modecompta != 'CREANCES-DETTES' + // "Calculation of part of each product for accountancy in this mode is not possible. When a partial payment (for example 5 euros) is done on an + // invoice with 2 product (product A for 10 euros and product B for 20 euros), what is part of paiment for product A and part of paiment for product B ? + // Because there is no way to know this, this report is not relevant. + print '
'.$langs->trans("TurnoverPerProductInCommitmentAccountingNotRelevant").'
'; +} + +// End of page +llxFooter(); +$db->close();