Amlioration et mise en facteur du code de gnration des graphiques de stats d'un produit

This commit is contained in:
Laurent Destailleur 2006-08-19 01:43:36 +00:00
parent d146e10284
commit 4db6af8def
5 changed files with 193 additions and 206 deletions

View File

@ -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

View File

@ -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;

View File

@ -104,7 +104,6 @@ if ($_GET["id"] || $_GET["ref"])
dolibarr_fiche_head($head, 'category', $titre);
print($mesg);
print '<table class="border" width="100%">';
print "<tr>";
// Reference
@ -134,6 +133,9 @@ if ($_GET["id"] || $_GET["ref"])
print '</div>';
if ($mesg) print($mesg);
/*
* Barre d'actions

View File

@ -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 '</div>';
print '<table class="border" width="100%">';
print '<table width="100%">';
// Ligne de graph
print '<tr class="liste_titre"><td width="50%" colspan="2" align="center">Nombre de propositions commerciales<br>sur les 12 derniers mois</td>';
print '<td align="center" width="50%" colspan="2">-</td></tr>';
// Place pour autre graphique
print '<tr><td align="center" colspan="2">';
$file=$product->id.'/'.$img_propal_name;
print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=productstats&file='.urlencode($file).'" alt="Nombre de propales sur les 12 derniers mois">';
print '</td><td align="center" colspan="2">&nbsp;</td></tr>';
print '<tr>';
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 '<td>'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($filenbpropal),"%d %b %Y %H:%M:%S")).'</td>';
if (create_exdir($dir.'/'.$product->id) < 0)
{
$mesg = $langs->trans("ErrorCanNotCreateDir",$dir);
}
}
else
{
print '<td>'.($mesg?'<font class="error">'.$mesg.'</font>':$langs->trans("ChartNotGenerated")).'</td>';
}
print '<td align="center">[<a href="fiche.php?id='.$product->id.'&amp;action=recalcul">'.$langs->trans("ReCalculate").'</a>]</td>';
if (file_exists($filenbpiece) && filemtime($filenbpiece) && ! $px->isGraphKo())
{
print '<td>'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($filenbpiece),"%d %b %Y %H:%M:%S")).'</td>';
}
else
{
print '<td>'.($mesg?'<font class="error">'.$mesg.'</font>':$langs->trans("ChartNotGenerated")).'</td>';
}
print '<td align="center">[<a href="fiche.php?id='.$product->id.'&amp;action=recalcul">'.$langs->trans("ReCalculate").'</a>]</td></tr>';
// Ligne de graph
print '<tr class="liste_titre"><td width="50%" colspan="2" align="center">Nombre de ventes<br>sur les 12 derniers mois</td>';
print '<td align="center" width="50%" colspan="2">Nombre de pièces vendues</td></tr>';
print '<tr><td align="center" colspan="2">';
$file=$product->id.'/vente12mois.png';
print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=productstats&file='.urlencode($file).'" alt="Ventes sur les 12 derniers mois">';
print '</td><td align="center" colspan="2">';
$file=$product->id.'/vendu12mois.png';
print '<img src="'.DOL_URL_ROOT.'/viewimage.php?modulepart=productstats&file='.urlencode($file).'" alt="Nombre de pièces vendues sur les 12 derniers mois">';
print '</td></tr>';
print '<tr>';
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 '<td>'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($filenbvente),"%d %b %Y %H:%M:%S")).'</td>';
}
else
{
print '<td>'.($mesg?'<font class="error">'.$mesg.'</font>':$langs->trans("ChartNotGenerated")).'</td>';
}
print '<td align="center">[<a href="fiche.php?id='.$product->id.'&amp;action=recalcul">'.$langs->trans("ReCalculate").'</a>]</td>';
if (file_exists($filenbpiece) && filemtime($filenbpiece) && ! $px->isGraphKo())
{
print '<td>'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($filenbpiece),"%d %b %Y %H:%M:%S")).'</td>';
}
else
{
print '<td>'.($mesg?'<font class="error">'.$mesg.'</font>':$langs->trans("ChartNotGenerated")).'</td>';
}
print '<td align="center">[<a href="fiche.php?id='.$product->id.'&amp;action=recalcul">'.$langs->trans("ReCalculate").'</a>]</td></tr>';
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 '<tr>';
// Show graph
print '<td width="50%" align="center">';
print '<table class="border" width="100%">';
// Label
print '<tr class="liste_titre"><td colspan="2">';
print $graphfiles[$key]['label'];
print '</td></tr>';
// Image
print '<tr><td colspan="2" align="center">';
$url=DOL_URL_ROOT.'/viewimage.php?modulepart='.$graphfiles[$key]['modulepart'].'&file='.urlencode($graphfiles[$key]['file']);
//print $url;
print '<img src="'.$url.'">';
print '</td></tr>';
// Date génération
print '<tr>';
if (file_exists($dir."/".$graphfiles[$key]['file']) && filemtime($dir."/".$graphfiles[$key]['file']) && ! $px->isGraphKo())
{
print '<td>'.$langs->trans("GeneratedOn",dolibarr_print_date(filemtime($dir."/".$graphfiles[$key]['file']),"%d %b %Y %H:%M:%S")).'</td>';
}
else
{
print '<td>'.($mesg?'<font class="error">'.$mesg.'</font>':$langs->trans("ChartNotGenerated")).'</td>';
}
print '<td align="center"><a href="fiche.php?id='.$product->id.'&amp;action=recalcul">'.img_picto($langs->trans("ReCalculate"),'refresh').'</a></td>';
print '</tr>';
print '</table>';
print '</td>';
if ($i % 2 == 1) print '</tr>';
$i++;
}
if ($i % 2 == 1) print '<td>&nbsp;</td></tr>';
print '</table>';

View File

@ -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)