From 4db6af8def8506dfe5472a2a21dca26de16ef3a6 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sat, 19 Aug 2006 01:43:36 +0000 Subject: [PATCH] =?UTF-8?q?Am=E9lioration=20et=20mise=20en=20facteur=20du?= =?UTF-8?q?=20code=20de=20g=E9n=E9ration=20des=20graphiques=20de=20stats?= =?UTF-8?q?=20d'un=20produit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- htdocs/langs/fr_FR/suppliers.lang | 2 +- htdocs/product.class.php | 160 +++++++++++---------- htdocs/product/categorie.php | 4 +- htdocs/product/stats/fiche.php | 231 ++++++++++++++---------------- htdocs/viewimage.php | 2 +- 5 files changed, 193 insertions(+), 206 deletions(-) diff --git a/htdocs/langs/fr_FR/suppliers.lang b/htdocs/langs/fr_FR/suppliers.lang index d70b5720fe1..811ef08723e 100644 --- a/htdocs/langs/fr_FR/suppliers.lang +++ b/htdocs/langs/fr_FR/suppliers.lang @@ -4,7 +4,7 @@ Supplier=Fournisseur AddSupplier=Ajouter un fournisseur SupplierRemoved=Fournisseur supprimé SuppliersInvoice=Facture fournisseur -SuppliersInvoices=Factures fournisseur +SuppliersInvoices=Factures fournisseurs NewSupplier=Nouveau fournisseur History=Historique ListOfSuppliers=Liste des fournisseurs diff --git a/htdocs/product.class.php b/htdocs/product.class.php index 87e50f153ab..beb7dde6bb0 100644 --- a/htdocs/product.class.php +++ b/htdocs/product.class.php @@ -1169,94 +1169,74 @@ class Product } } - /** - * \brief Renvoie tableau des stats pour une requete donnée - * \param sql Requete a exécuter - * \return array Tableau de stats - */ - function _get_stats($sql) - { - $result = $this->db->query($sql) ; - if ($result) - { - $num = $this->db->num_rows($result); - $i = 0; - while ($i < $num) - { - $arr = $this->db->fetch_array($result); - $tab[$arr[1]] = $arr[0]; - $i++; - } - } - - $year = strftime('%Y',time()); - $month = strftime('%m',time()); - $result = array(); - - for ($j = 0 ; $j < 12 ; $j++) - { - $idx=ucfirst(substr( strftime("%b",mktime(12,0,0,$month,1,$year)) ,0,3) ); - $monthnum=sprintf("%02s",$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); - - $month = "0".($month - 1); - if (strlen($month) == 3) - { - $month = substr($month,1); - } - if ($month == 0) - { - $month = 12; - $year = $year - 1; - } - } - return array_reverse($result); - - } - - - /** - * \brief Renvoie le nombre de ventes du produit/service par mois - * \param socid id societe - * \return array nombre de vente par mois - */ - - function get_nb_vente($socid=0) - { - global $conf; - global $user; - - $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m') "; - $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as d, ".MAIN_DB_PREFIX."facture as f"; - if (!$user->rights->commercial->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql .= " WHERE f.rowid = d.fk_facture and d.fk_product =".$this->id; - if (!$user->rights->commercial->client->voir && !$socid) $sql .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; - if ($socid > 0) + /** + * \brief Renvoie tableau des stats pour une requete donnée + * \param sql Requete a exécuter + * \return array Tableau de stats, -1 si ko + */ + function _get_stats($sql) { - $sql .= " AND f.fk_soc = $socid"; + $result = $this->db->query($sql) ; + if ($result) + { + $num = $this->db->num_rows($result); + $i = 0; + while ($i < $num) + { + $arr = $this->db->fetch_array($result); + $tab[$arr[1]] = $arr[0]; + $i++; + } + } + else + { + $this->error=$this->db->error().' sql='.$sql; + return -1; + } + + $year = strftime('%Y',time()); + $month = strftime('%m',time()); + $result = array(); + + for ($j = 0 ; $j < 12 ; $j++) + { + $idx=ucfirst(substr( strftime("%b",mktime(12,0,0,$month,1,$year)) ,0,3) ); + $monthnum=sprintf("%02s",$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); + + $month = "0".($month - 1); + if (strlen($month) == 3) + { + $month = substr($month,1); + } + if ($month == 0) + { + $month = 12; + $year = $year - 1; + } + } + + return array_reverse($result); + } - $sql .= " GROUP BY date_format(f.datef,'%Y%m') DESC ;"; - - return $this->_get_stats($sql); - } /** - * \brief Renvoie le nombre de factures dans lesquelles figure le produit par mois + * \brief Renvoie le nombre de factures clients du produit/service par mois * \param socid id societe - * \return array nombre de factures par mois + * \return array nombre de vente par mois */ - function get_num_vente($socid=0) + function get_nb_vente($socid=0) { global $conf; global $user; - $sql = "SELECT count(*), date_format(f.datef, '%Y%m') "; + $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m') "; $sql .= " FROM ".MAIN_DB_PREFIX."facturedet as d, ".MAIN_DB_PREFIX."facture as f"; if (!$user->rights->commercial->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; - $sql .= " WHERE f.rowid = d.fk_facture AND d.fk_product =".$this->id; + $sql .= " WHERE f.rowid = d.fk_facture and d.fk_product =".$this->id; if (!$user->rights->commercial->client->voir && !$socid) $sql .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; if ($socid > 0) { @@ -1266,6 +1246,32 @@ class Product return $this->_get_stats($sql); } + + + /** + * \brief Renvoie le nombre de factures fournisseurs du produit/service par mois + * \param socid id societe + * \return array nombre d'achat par mois + */ + function get_nb_achat($socid=0) + { + global $conf; + global $user; + + $sql = "SELECT sum(d.qty), date_format(f.datef, '%Y%m') "; + $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn_det as d, ".MAIN_DB_PREFIX."facture_fourn as f"; + if (!$user->rights->commercial->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; + $sql .= " WHERE f.rowid = d.fk_facture_fourn and d.fk_product =".$this->id; + if (!$user->rights->commercial->client->voir && !$socid) $sql .= " AND f.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id; + if ($socid > 0) + { + $sql .= " AND f.fk_soc = $socid"; + } + $sql .= " GROUP BY date_format(f.datef,'%Y%m') DESC ;"; + + $resarray=$this->_get_stats($sql); + return $resarray; + } /** @@ -1273,12 +1279,12 @@ class Product * \param socid id societe * \return array nombre de propales par mois */ - function get_num_propal($socid=0) + function get_nb_propal($socid=0) { global $conf; global $user; - $sql = "SELECT count(*), date_format(p.datep, '%Y%m') "; + $sql = "SELECT sum(d.qty), date_format(p.datep, '%Y%m') "; $sql .= " FROM ".MAIN_DB_PREFIX."propaldet as d, ".MAIN_DB_PREFIX."propal as p"; if (!$user->rights->commercial->client->voir && !$socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; $sql .= " WHERE p.rowid = d.fk_propal and d.fk_product =".$this->id; diff --git a/htdocs/product/categorie.php b/htdocs/product/categorie.php index 1b2c2fbc97d..f035ec31710 100644 --- a/htdocs/product/categorie.php +++ b/htdocs/product/categorie.php @@ -104,7 +104,6 @@ if ($_GET["id"] || $_GET["ref"]) dolibarr_fiche_head($head, 'category', $titre); - print($mesg); print ''; print ""; // Reference @@ -134,6 +133,9 @@ if ($_GET["id"] || $_GET["ref"]) print ''; + + if ($mesg) print($mesg); + /* * Barre d'actions diff --git a/htdocs/product/stats/fiche.php b/htdocs/product/stats/fiche.php index dc95c520269..690d3e88721 100644 --- a/htdocs/product/stats/fiche.php +++ b/htdocs/product/stats/fiche.php @@ -71,78 +71,7 @@ if ($_GET["id"] || $_GET["ref"]) { // Efface rep obsolete if(is_dir(DOL_DOCUMENT_ROOT."/document/produits")) - rmdir(DOL_DOCUMENT_ROOT."/document/produits"); - - // Création répertoire pour images générées - // $conf->produit->dir_images définit dans master.inc.php - - $dir = $conf->produit->dir_images."/".$product->id; - - if (! file_exists($dir)) - { - if (create_exdir($dir) < 0) - { - $mesg = $langs->trans("ErrorCanNotCreateDir",$dir); - } - } - - $img_propal_name = "propal12mois.png"; - $filenbpropal = $dir . "/" . $img_propal_name; - $filenbvente = $dir . "/vente12mois.png"; - $filenbpiece = $dir . "/vendu12mois.png"; - - $WIDTH=380; - $HEIGHT=200; - - $px = new DolGraph(); - $mesg = $px->isGraphKo(); - if (! $mesg) - { - $graph_data = $product->get_num_vente($socid); - $px->SetData($graph_data); - $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue()); - $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue()); - $px->SetWidth($WIDTH); - $px->SetHeight($HEIGHT); - $px->SetHorizTickIncrement(1); - $px->SetPrecisionY(0); - $px->SetShading(5); - $px->draw($filenbvente); - } - - $px = new DolGraph(); - $mesg = $px->isGraphKo(); - if (! $mesg) - { - $graph_data = $product->get_nb_vente($socid); - $px->SetData($graph_data); - $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue()); - $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue()); - $px->SetWidth($WIDTH); - $px->SetHeight($HEIGHT); - $px->SetHorizTickIncrement(1); - $px->SetPrecisionY(0); - $px->SetShading(5); - $px->draw($filenbpiece); - } - - $px = new DolGraph(); - $mesg = $px->isGraphKo(); - if (! $mesg) - { - $graph_data = $product->get_num_propal($socid); - $px->SetData($graph_data); - $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue()); - $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue()); - $px->SetWidth($WIDTH); - $px->SetHeight($HEIGHT); - $px->SetHorizTickIncrement(1); - $px->SetPrecisionY(0); - $px->SetShading(5); - $px->draw($filenbpropal); - } - - $mesg = $langs->trans("ChartGenerated"); + rmdir(DOL_DOCUMENT_ROOT."/document/produits"); $head=product_prepare_head($product); @@ -180,65 +109,115 @@ if ($_GET["id"] || $_GET["ref"]) print ''; - print '
'; + print '
'; - // Ligne de graph - print ''; - print ''; - // Place pour autre graphique - print ''; - print ''; - if (file_exists($filenbpropal) && filemtime($filenbpropal) && ! $px->isGraphKo()) + // Generation des graphs + $WIDTH=380; + $HEIGHT=160; + $dir = $conf->produit->dir_images; + if (! file_exists($dir.'/'.$product->id)) { - print ''; + if (create_exdir($dir.'/'.$product->id) < 0) + { + $mesg = $langs->trans("ErrorCanNotCreateDir",$dir); + } } - else - { - print ''; - } - print ''; - if (file_exists($filenbpiece) && filemtime($filenbpiece) && ! $px->isGraphKo()) - { - print ''; - } - else - { - print ''; - } - print ''; - // Ligne de graph - print ''; - print ''; - print ''; - print ''; - if (file_exists($filenbvente) && filemtime($filenbvente) && ! $px->isGraphKo()) + $graphfiles=array( + 'propal' =>array('modulepart'=>'productstats_proposals', 'file' => $product->id.'/propal12m.png', 'label' => $langs->trans("Nombre propal sur les 12 derniers mois")), +// 'orders' =>array('modulepart'=>'productstats_orders', 'file' => $product->id.'/orders12m.png', 'label' => $langs->trans("Nombre commande clients sur les 12 derniers mois")), +// 'orderssuppliers' =>array('modulepart'=>'productstats_orderssuppliers', 'file' => $product->id.'/orderssuppliers12m.png', 'label' => $langs->trans("Nombre commande fournisseurs sur les 12 derniers mois")), +// 'contracts' =>array('modulepart'=>'productstats_contracts', 'file' => $product->id.'/contracts12m.png', 'label' => $langs->trans("Nombre contrats sur les 12 derniers mois")), + 'invoices' =>array('modulepart'=>'productstats_invoices', 'file' => $product->id.'/invoices12m.png', 'label' => $langs->trans("Nombre facture clients sur les 12 derniers mois")), + 'invoicessuppliers'=>array('modulepart'=>'productstats_invoicessuppliers', 'file' => $product->id.'/invoicessuppliers12m.png', 'label' => $langs->trans("Nombre facture fournisseurs sur les 12 derniers mois")), + ); + + $px = new DolGraph(); + $mesg = $px->isGraphKo(); + if (! $mesg) { - print ''; - } - else - { - print ''; - } - print ''; - if (file_exists($filenbpiece) && filemtime($filenbpiece) && ! $px->isGraphKo()) - { - print ''; - } - else - { - print ''; - } - print ''; + foreach($graphfiles as $key => $val) + { + if (! $graphfiles[$key]['file']) continue; + + $graph_data = array(); + + // \todo Test si deja existant et recent, on ne genere pas + if ($key == 'propal') $graph_data = $product->get_nb_propal($socid); + if ($key == 'invoices') $graph_data = $product->get_nb_vente($socid); + if ($key == 'invoicessuppliers') $graph_data = $product->get_nb_achat($socid); + if (is_array($graph_data)) + { + $px->SetData($graph_data); + $px->SetMaxValue($px->GetCeilMaxValue()<0?0:$px->GetCeilMaxValue()); + $px->SetMinValue($px->GetFloorMinValue()>0?0:$px->GetFloorMinValue()); + $px->SetWidth($WIDTH); + $px->SetHeight($HEIGHT); + $px->SetHorizTickIncrement(1); + $px->SetPrecisionY(0); + $px->SetShading(5); + //print 'x '.$key.' '.$graphfiles[$key]['file']; + $px->draw($dir."/".$graphfiles[$key]['file']); + } + else + { + dolibarr_print_error($db,'Error for calculating graph on key='.$key.' - '.$product->error); + } + } + } + + $mesg = $langs->trans("ChartGenerated"); + + // Affichage graphs + $i=0; + foreach($graphfiles as $key => $val) + { + if (! $graphfiles[$key]['file']) continue; + + if ($graphfiles == 'propal' && ! $user->right->propale->lire) continue; + if ($graphfiles == 'invoices' && ! $user->right->facture->lire) continue; + if ($graphfiles == 'invoices_suppliers' && ! $user->right->fournisseur->facture->lire) continue; + + + if ($i % 2 == 0) print ''; + + // Show graph + print ''; + + if ($i % 2 == 1) print ''; + + + $i++; + } + + if ($i % 2 == 1) print ''; print '
Nombre de propositions commerciales
sur les 12 derniers mois
-
'; - $file=$product->id.'/'.$img_propal_name; - print 'Nombre de propales sur les 12 derniers mois'; - print ' 
'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($filenbpropal),"%d %b %Y %H:%M:%S")).''.($mesg?''.$mesg.'':$langs->trans("ChartNotGenerated")).'['.$langs->trans("ReCalculate").']'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($filenbpiece),"%d %b %Y %H:%M:%S")).''.($mesg?''.$mesg.'':$langs->trans("ChartNotGenerated")).'['.$langs->trans("ReCalculate").']
Nombre de ventes
sur les 12 derniers mois
Nombre de pièces vendues
'; - $file=$product->id.'/vente12mois.png'; - print 'Ventes sur les 12 derniers mois'; - print ''; - $file=$product->id.'/vendu12mois.png'; - print 'Nombre de pièces vendues sur les 12 derniers mois'; - print '
'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($filenbvente),"%d %b %Y %H:%M:%S")).''.($mesg?''.$mesg.'':$langs->trans("ChartNotGenerated")).'['.$langs->trans("ReCalculate").']'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($filenbpiece),"%d %b %Y %H:%M:%S")).''.($mesg?''.$mesg.'':$langs->trans("ChartNotGenerated")).'['.$langs->trans("ReCalculate").']
'; + + print ''; + // Label + print ''; + // Image + print ''; + // Date génération + print ''; + if (file_exists($dir."/".$graphfiles[$key]['file']) && filemtime($dir."/".$graphfiles[$key]['file']) && ! $px->isGraphKo()) + { + print ''; + } + else + { + print ''; + } + print ''; + print ''; + print '
'; + print $graphfiles[$key]['label']; + print '
'; + $url=DOL_URL_ROOT.'/viewimage.php?modulepart='.$graphfiles[$key]['modulepart'].'&file='.urlencode($graphfiles[$key]['file']); + //print $url; + print ''; + print '
'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($dir."/".$graphfiles[$key]['file']),"%d %b %Y %H:%M:%S")).''.($mesg?''.$mesg.'':$langs->trans("ChartNotGenerated")).''.img_picto($langs->trans("ReCalculate"),'refresh').'
'; + + print '
 
'; diff --git a/htdocs/viewimage.php b/htdocs/viewimage.php index 29cf183eace..cb4c0a2c49e 100644 --- a/htdocs/viewimage.php +++ b/htdocs/viewimage.php @@ -145,7 +145,7 @@ if ($modulepart) } // Wrapping pour les images des stats produits - if ($modulepart == 'productstats') + if (eregi('^productstats_',$modulepart)) { $user->getrights('produit'); if ($user->rights->produit->lire)