From 47dfe633f9823a0d7954a4a7b29a25e547af83e9 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 28 Oct 2020 13:19:35 +0100 Subject: [PATCH 1/3] Fix filter on date on statistics of products --- htdocs/product/class/product.class.php | 5 ++--- htdocs/product/stats/card.php | 22 +++++++++++----------- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index 99590e27a36..ba63afdab44 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -3188,7 +3188,7 @@ class Product extends CommonObject return -1; } - if (empty($year)) { + if (empty($year) || $year == -1) { $year = strftime('%Y', time()); $month = strftime('%m', time()); } @@ -3339,8 +3339,7 @@ class Product extends CommonObject public function get_nb_propal($socid, $mode, $filteronproducttype = -1, $year = 0, $morefilter = '') { // phpcs:enable - global $conf; - global $user; + global $conf, $user; $sql = "SELECT sum(d.qty), date_format(p.datep, '%Y%m')"; if ($mode == 'bynumber') { diff --git a/htdocs/product/stats/card.php b/htdocs/product/stats/card.php index 4600baa1597..6c6d2b80cf8 100644 --- a/htdocs/product/stats/card.php +++ b/htdocs/product/stats/card.php @@ -208,14 +208,14 @@ if ($result || empty($id)) // Year print ''.$langs->trans("Year").''; $arrayyears = array(); - for ($year = $currentyear - 10; $year < $currentyear + 10; $year++) + for ($year = $currentyear - 25; $year < $currentyear; $year++) { $arrayyears[$year] = $year; } if (!in_array($year, $arrayyears)) $arrayyears[$year] = $year; - if (!in_array($nowyear, $arrayyears)) $arrayyears[$nowyear] = $nowyear; + if (!in_array($currentyear, $arrayyears)) $arrayyears[$currentyear] = $currentyear; arsort($arrayyears); - print $form->selectarray('search_year', $arrayyears, $search_year, 0); + print $form->selectarray('search_year', $arrayyears, $search_year, 1); print ''; print ''; print '
'; @@ -262,49 +262,49 @@ if ($result || empty($id)) if ($conf->propal->enabled) { $graphfiles['propal'] = array('modulepart'=>'productstats_proposals', - 'file' => $object->id.'/propal12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year ? '_year'.$search_year : '').'.png', + 'file' => $object->id.'/propal12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year > 0 ? '_year'.$search_year : '').'.png', 'label' => ($mode == 'byunit' ? $langs->transnoentitiesnoconv("NumberOfUnitsProposals") : $langs->transnoentitiesnoconv("NumberOfProposals"))); } if ($conf->supplier_proposal->enabled) { $graphfiles['proposalssuppliers'] = array('modulepart'=>'productstats_proposalssuppliers', - 'file' => $object->id.'/proposalssuppliers12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year ? '_year'.$search_year : '').'.png', + 'file' => $object->id.'/proposalssuppliers12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year > 0 ? '_year'.$search_year : '').'.png', 'label' => ($mode == 'byunit' ? $langs->transnoentitiesnoconv("NumberOfUnitsSupplierProposals") : $langs->transnoentitiesnoconv("NumberOfSupplierProposals"))); } if ($conf->order->enabled) { $graphfiles['orders'] = array('modulepart'=>'productstats_orders', - 'file' => $object->id.'/orders12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year ? '_year'.$search_year : '').'.png', + 'file' => $object->id.'/orders12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year > 0 ? '_year'.$search_year : '').'.png', 'label' => ($mode == 'byunit' ? $langs->transnoentitiesnoconv("NumberOfUnitsCustomerOrders") : $langs->transnoentitiesnoconv("NumberOfCustomerOrders"))); } if ($conf->supplier_order->enabled) { $graphfiles['orderssuppliers'] = array('modulepart'=>'productstats_orderssuppliers', - 'file' => $object->id.'/orderssuppliers12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year ? '_year'.$search_year : '').'.png', + 'file' => $object->id.'/orderssuppliers12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year > 0 ? '_year'.$search_year : '').'.png', 'label' => ($mode == 'byunit' ? $langs->transnoentitiesnoconv("NumberOfUnitsSupplierOrders") : $langs->transnoentitiesnoconv("NumberOfSupplierOrders"))); } if ($conf->facture->enabled) { $graphfiles['invoices'] = array('modulepart'=>'productstats_invoices', - 'file' => $object->id.'/invoices12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year ? '_year'.$search_year : '').'.png', + 'file' => $object->id.'/invoices12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year > 0 ? '_year'.$search_year : '').'.png', 'label' => ($mode == 'byunit' ? $langs->transnoentitiesnoconv("NumberOfUnitsCustomerInvoices") : $langs->transnoentitiesnoconv("NumberOfCustomerInvoices"))); } if ($conf->supplier_invoice->enabled) { $graphfiles['invoicessuppliers'] = array('modulepart'=>'productstats_invoicessuppliers', - 'file' => $object->id.'/invoicessuppliers12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year ? '_year'.$search_year : '').'.png', + 'file' => $object->id.'/invoicessuppliers12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year > 0 ? '_year'.$search_year : '').'.png', 'label' => ($mode == 'byunit' ? $langs->transnoentitiesnoconv("NumberOfUnitsSupplierInvoices") : $langs->transnoentitiesnoconv("NumberOfSupplierInvoices"))); } if ($conf->contrat->enabled) { $graphfiles['contracts'] = array('modulepart'=>'productstats_contracts', - 'file' => $object->id.'/contracts12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year ? '_year'.$search_year : '').'.png', + 'file' => $object->id.'/contracts12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year > 0 ? '_year'.$search_year : '').'.png', 'label' => ($mode == 'byunit' ? $langs->transnoentitiesnoconv("NumberOfUnitsContracts") : $langs->transnoentitiesnoconv("NumberOfContracts"))); } if ($conf->mrp->enabled) { $graphfiles['mrp'] = array('modulepart'=>'productstats_mrp', - 'file' => $object->id.'/mos12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year ? '_year'.$search_year : '').'.png', + 'file' => $object->id.'/mos12m'.((string) $type != '' ? '_type'.$type : '').'_'.$mode.($search_year > 0 ? '_year'.$search_year : '').'.png', 'label' => ($mode == 'byunit' ? $langs->transnoentitiesnoconv("NumberOfUnitsMos") : $langs->transnoentitiesnoconv("NumberOfMos"))); } From 6363a6cd606453284642e4df946f88553724ec0f Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 28 Oct 2020 14:11:46 +0100 Subject: [PATCH 2/3] Fix sanitize --- htdocs/product/stats/commande.php | 2 +- htdocs/product/stats/commande_fournisseur.php | 2 +- htdocs/product/stats/facture.php | 2 +- htdocs/product/stats/facture_fournisseur.php | 2 +- htdocs/product/stats/propal.php | 2 +- htdocs/product/stats/supplier_proposal.php | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/htdocs/product/stats/commande.php b/htdocs/product/stats/commande.php index 9898c13f21b..b55b43d218d 100644 --- a/htdocs/product/stats/commande.php +++ b/htdocs/product/stats/commande.php @@ -59,7 +59,7 @@ $pageprev = $page - 1; $pagenext = $page + 1; if (!$sortorder) $sortorder = "DESC"; if (!$sortfield) $sortfield = "c.date_commande"; -$search_month = GETPOST('search_month', 'alpha'); +$search_month = GETPOST('search_month', 'int'); $search_year = GETPOST('search_year', 'int'); if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { diff --git a/htdocs/product/stats/commande_fournisseur.php b/htdocs/product/stats/commande_fournisseur.php index c111e075972..b4a837679a9 100644 --- a/htdocs/product/stats/commande_fournisseur.php +++ b/htdocs/product/stats/commande_fournisseur.php @@ -61,7 +61,7 @@ if (!$sortorder) $sortorder = "DESC"; if (!$sortfield) $sortfield = "c.date_commande"; -$search_month = GETPOST('search_month', 'alpha'); +$search_month = GETPOST('search_month', 'int'); $search_year = GETPOST('search_year', 'int'); if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { diff --git a/htdocs/product/stats/facture.php b/htdocs/product/stats/facture.php index d4bbe68386c..f858dbc91e4 100644 --- a/htdocs/product/stats/facture.php +++ b/htdocs/product/stats/facture.php @@ -61,7 +61,7 @@ $pagenext = $page + 1; if (!$sortorder) $sortorder = "DESC"; if (!$sortfield) $sortfield = "f.datef"; -$search_month = GETPOST('search_month', 'alpha'); +$search_month = GETPOST('search_month', 'int'); $search_year = GETPOST('search_year', 'int'); if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { diff --git a/htdocs/product/stats/facture_fournisseur.php b/htdocs/product/stats/facture_fournisseur.php index f39d4ab73b3..58d52452654 100644 --- a/htdocs/product/stats/facture_fournisseur.php +++ b/htdocs/product/stats/facture_fournisseur.php @@ -60,7 +60,7 @@ $pageprev = $page - 1; $pagenext = $page + 1; if (!$sortorder) $sortorder = "DESC"; if (!$sortfield) $sortfield = "f.datef"; -$search_month = GETPOST('search_month', 'alpha'); +$search_month = GETPOST('search_month', 'int'); $search_year = GETPOST('search_year', 'int'); if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { diff --git a/htdocs/product/stats/propal.php b/htdocs/product/stats/propal.php index f5b097d0655..2dffd4d78d6 100644 --- a/htdocs/product/stats/propal.php +++ b/htdocs/product/stats/propal.php @@ -60,7 +60,7 @@ $pagenext = $page + 1; if (!$sortorder) $sortorder = "DESC"; if (!$sortfield) $sortfield = "p.datep"; -$search_month = GETPOST('search_month', 'alpha'); +$search_month = GETPOST('search_month', 'int'); $search_year = GETPOST('search_year', 'int'); if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { diff --git a/htdocs/product/stats/supplier_proposal.php b/htdocs/product/stats/supplier_proposal.php index 5a59cbb9c83..8d5b8ad14c4 100644 --- a/htdocs/product/stats/supplier_proposal.php +++ b/htdocs/product/stats/supplier_proposal.php @@ -60,7 +60,7 @@ $pagenext = $page + 1; if (!$sortorder) $sortorder = "DESC"; if (!$sortfield) $sortfield = "p.date_valid"; -$search_month = GETPOST('search_month', 'alpha'); +$search_month = GETPOST('search_month', 'int'); $search_year = GETPOST('search_year', 'int'); if (GETPOST('button_removefilter_x', 'alpha') || GETPOST('button_removefilter', 'alpha')) { From f02a35d398208ad45bdac8b5e9c7e761879e6eb7 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 28 Oct 2020 16:47:00 +0100 Subject: [PATCH 3/3] Fix bad array when year is not defined into graph stat of products --- htdocs/product/class/product.class.php | 36 ++++++++++++++++---------- htdocs/product/stats/card.php | 1 + 2 files changed, 23 insertions(+), 14 deletions(-) diff --git a/htdocs/product/class/product.class.php b/htdocs/product/class/product.class.php index ba63afdab44..98c4084587c 100644 --- a/htdocs/product/class/product.class.php +++ b/htdocs/product/class/product.class.php @@ -3158,14 +3158,16 @@ class Product extends CommonObject /** * Return an array formated for showing graphs * - * @param string $sql Request to execute - * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities - * @param int $year Year (0=current year) - * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11 + * @param string $sql Request to execute + * @param string $mode 'byunit'=number of unit, 'bynumber'=nb of entities + * @param int $year Year (0=current year, -1=all years) + * @return array <0 if KO, result[month]=array(valuex,valuey) where month is 0 to 11 */ private function _get_stats($sql, $mode, $year = 0) { // phpcs:enable + $tab = array(); + $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); @@ -3173,11 +3175,14 @@ class Product extends CommonObject while ($i < $num) { $arr = $this->db->fetch_array($resql); - if ($mode == 'byunit') { - $tab[$arr[1]] = $arr[0]; // 1st field + $keyfortab = (string) $arr[1]; + if ($year == -1) { + $keyfortab = substr($keyfortab, -2); } - if ($mode == 'bynumber') { - $tab[$arr[1]] = $arr[2]; // 3rd field + if ($mode == 'byunit') { + $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[0]; // 1st field + } elseif ($mode == 'bynumber') { + $tab[$keyfortab] = (empty($tab[$keyfortab]) ? 0 : $tab[$keyfortab]) + $arr[2]; // 3rd field } $i++; } @@ -3188,21 +3193,24 @@ class Product extends CommonObject return -1; } - if (empty($year) || $year == -1) { + if (empty($year)) { $year = strftime('%Y', time()); $month = strftime('%m', time()); - } - else - { + } elseif ($year == -1) { + $year = ''; + $month = 12; // We imagine we are at end of year, so we get last 12 month before, so all correct year. + } else { $month = 12; // We imagine we are at end of year, so we get last 12 month before, so all correct year. } + $result = array(); for ($j = 0; $j < 12; $j++) { - //$idx = ucfirst(dol_trunc(dol_print_date(dol_mktime(12, 0, 0, $month, 1, $year), "%b"), 3, 'right', 'UTF-8', 1)); - $idx = ucfirst(dol_trunc(dol_print_date(dol_mktime(12, 0, 0, $month, 1, $year), "%b"), 1, 'right', 'UTF-8', 1)); + // $ids is 'D', 'N', 'O', 'S', ... (First letter of month in user language) + $idx = ucfirst(dol_trunc(dol_print_date(dol_mktime(12, 0, 0, $month, 1, 1970), "%b"), 1, 'right', 'UTF-8', 1)); + //print $idx.'-'.$year.'-'.$month.'
'; $result[$j] = array($idx, isset($tab[$year.$month]) ? $tab[$year.$month] : 0); // $result[$j] = array($monthnum,isset($tab[$year.$month])?$tab[$year.$month]:0); diff --git a/htdocs/product/stats/card.php b/htdocs/product/stats/card.php index 6c6d2b80cf8..fb161ea40cf 100644 --- a/htdocs/product/stats/card.php +++ b/htdocs/product/stats/card.php @@ -180,6 +180,7 @@ if (empty($id) & empty($ref)) if ($result || empty($id)) { print '
'; + print ''; print ''; print '';