* * 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. */ /** * \file htdocs/custom/buchaltungswidget/invoice_category_stats.php * \ingroup buchaltungswidget * \brief Invoice statistics filtered by invoice category (Schlagwort/Kategorie Rechnung) */ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php'; // Security check if (!$user->hasRight('facture', 'lire')) { accessforbidden(); } $langs->loadLangs(array("buchaltungswidget@buchaltungswidget", "bills", "companies", "categories")); $WIDTH = DolGraph::getDefaultGraphSizeForStats('width'); $HEIGHT = DolGraph::getDefaultGraphSizeForStats('height'); // Get parameters $socid = GETPOSTINT('socid'); $userid = GETPOSTINT('userid'); $categ_invoice_id = GETPOST('categ_invoice_id', 'array'); $object_status = GETPOST('object_status', 'intcomma'); if ($user->socid > 0) { $socid = $user->socid; } $nowyear = dol_print_date(dol_now('gmt'), "%Y", 'gmt'); $year = GETPOST('year') > 0 ? GETPOSTINT('year') : $nowyear; $startyear = $year - (!getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS') ? 2 : max(1, min(10, getDolGlobalString('MAIN_STATS_GRAPHS_SHOW_N_YEARS')))); $endyear = $year; /* * View */ $form = new Form($db); $formcompany = new FormCompany($db); $formother = new FormOther($db); $title = $langs->trans("InvoiceCategoryStatistics"); $dir = $conf->facture->dir_temp; llxHeader('', $title); print load_fiche_titre($title, '', 'bill'); dol_mkdir($dir); // Build WHERE clause for invoice category filter $sql_category_join = ''; $sql_category_where = ''; if (is_array($categ_invoice_id) && !empty($categ_invoice_id) && !in_array('0', $categ_invoice_id)) { // Sanitize category IDs as integers $categ_ids_sanitized = array_map('intval', $categ_invoice_id); $categ_ids_sanitized = array_filter($categ_ids_sanitized, function($v) { return $v > 0; }); if (!empty($categ_ids_sanitized)) { // Table is llx_categorie_invoice (not categorie_facture!) $sql_category_join = ' INNER JOIN '.MAIN_DB_PREFIX.'categorie_invoice as cf ON cf.fk_invoice = f.rowid'; $sql_category_where = ' AND cf.fk_categorie IN ('.implode(',', $categ_ids_sanitized).')'; } } // Get statistics data $data_nb = getInvoiceStatsByMonth($db, $conf, $startyear, $endyear, $socid, $userid, $object_status, $sql_category_join, $sql_category_where, 'nb'); $data_amount = getInvoiceStatsByMonth($db, $conf, $startyear, $endyear, $socid, $userid, $object_status, $sql_category_join, $sql_category_where, 'amount'); $data_avg = getInvoiceStatsByMonth($db, $conf, $startyear, $endyear, $socid, $userid, $object_status, $sql_category_join, $sql_category_where, 'avg'); $data_yearly = getInvoiceStatsByYear($db, $conf, $socid, $userid, $object_status, $sql_category_join, $sql_category_where); // Build graphs $filenamenb = $dir."/invoicescatnbinyear-".$year.".png"; $fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicescatnbinyear-'.$year.'.png'; $px1 = new DolGraph(); $mesg = $px1->isGraphKo(); if (!$mesg && !empty($data_nb)) { $px1->SetData($data_nb); $legend = array(); for ($i = $startyear; $i <= $endyear; $i++) { $legend[] = $i; } $px1->SetLegend($legend); $px1->SetMaxValue($px1->GetCeilMaxValue()); $px1->SetWidth($WIDTH); $px1->SetHeight($HEIGHT); $px1->SetYLabel($langs->trans("NumberOfBills")); $px1->SetShading(3); $px1->SetHorizTickIncrement(1); $px1->mode = 'depth'; $px1->SetTitle($langs->trans("NumberOfBillsByMonth")); $px1->draw($filenamenb, $fileurlnb); } $filenameamount = $dir."/invoicescatamountinyear-".$year.".png"; $fileurlamount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicescatamountinyear-'.$year.'.png'; $px2 = new DolGraph(); if (!$mesg && !empty($data_amount)) { $px2->SetData($data_amount); $legend = array(); for ($i = $startyear; $i <= $endyear; $i++) { $legend[] = $i; } $px2->SetLegend($legend); $px2->SetMaxValue($px2->GetCeilMaxValue()); $px2->SetMinValue(min(0, $px2->GetFloorMinValue())); $px2->SetWidth($WIDTH); $px2->SetHeight($HEIGHT); $px2->SetYLabel($langs->trans("AmountOfBills")); $px2->SetShading(3); $px2->SetHorizTickIncrement(1); $px2->mode = 'depth'; $px2->SetTitle($langs->trans("AmountOfBillsByMonthHT")); $px2->draw($filenameamount, $fileurlamount); } $filename_avg = $dir."/invoicescatavginyear-".$year.".png"; $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicescatavginyear-'.$year.'.png'; $px3 = new DolGraph(); if (!$mesg && !empty($data_avg)) { $px3->SetData($data_avg); $legend = array(); for ($i = $startyear; $i <= $endyear; $i++) { $legend[] = $i; } $px3->SetLegend($legend); $px3->SetYLabel($langs->trans("AmountAverage")); $px3->SetMaxValue($px3->GetCeilMaxValue()); $px3->SetMinValue((int) $px3->GetFloorMinValue()); $px3->SetWidth($WIDTH); $px3->SetHeight($HEIGHT); $px3->SetShading(3); $px3->SetHorizTickIncrement(1); $px3->mode = 'depth'; $px3->SetTitle($langs->trans("AmountAverage")); $px3->draw($filename_avg, $fileurl_avg); } // Get ALL available years from invoices (without category filter) $arrayyears = array(); $sql_years = "SELECT DISTINCT YEAR(f.datef) as year FROM ".MAIN_DB_PREFIX."facture as f"; $sql_years .= " WHERE f.entity = ".((int) $conf->entity); $sql_years .= " AND f.fk_statut > 0"; $sql_years .= " ORDER BY year DESC"; $resql_years = $db->query($sql_years); if ($resql_years) { while ($obj_year = $db->fetch_object($resql_years)) { if ($obj_year->year) { $arrayyears[$obj_year->year] = $obj_year->year; } } $db->free($resql_years); } if (!count($arrayyears)) { $arrayyears[$nowyear] = $nowyear; } print '
| '.$langs->trans("Year").' | '; print ''.$langs->trans("NumberOfBills").' | '; print '% | '; print ''.$langs->trans("AmountTotal").' | '; print '% | '; print ''.$langs->trans("AmountAverage").' | '; print '% | '; print '
| '.$oldyear.' | '; print '0 | '; print ''; print ' | 0 | '; print ''; print ' | 0 | '; print ''; print ' |
| 0 ? '&socid='.$socid : '').($userid > 0 ? '&userid='.$userid : '').'">'.$year_row.' | '; print ''.$val['nb'].' | '; print ''.(!empty($val['nb_diff']) && $val['nb_diff'] < 0 ? '' : '+').round(!empty($val['nb_diff']) ? $val['nb_diff'] : 0).'% | '; print ''.price(price2num($val['total'], 'MT'), 1).' | '; print ''.(!empty($val['total_diff']) && $val['total_diff'] < 0 ? '' : '+').round(!empty($val['total_diff']) ? $val['total_diff'] : 0).'% | '; print ''.price(price2num($val['avg'], 'MT'), 1).' | '; print ''.(!empty($val['avg_diff']) && $val['avg_diff'] < 0 ? '' : '+').round(!empty($val['avg_diff']) ? $val['avg_diff'] : 0).'% | '; print '
| ';
if ($mesg) {
print $mesg;
} else {
print $px1->show();
print " \n"; print $px2->show(); print " \n"; print $px3->show(); } print ' |