New: Add statistics report for supplier invoices

This commit is contained in:
Laurent Destailleur 2008-09-08 07:32:03 +00:00
parent e434ffd4f6
commit e2ec5cb613
9 changed files with 224 additions and 92 deletions

View File

@ -24,6 +24,8 @@
* \version $Id$ * \version $Id$
*/ */
include_once DOL_DOCUMENT_ROOT . "/stats.class.php"; include_once DOL_DOCUMENT_ROOT . "/stats.class.php";
include_once DOL_DOCUMENT_ROOT . "/facture.class.php";
include_once DOL_DOCUMENT_ROOT . "/fourn/fournisseur.facture.class.php";
/** /**
* \class FactureStats * \class FactureStats
@ -32,11 +34,37 @@ include_once DOL_DOCUMENT_ROOT . "/stats.class.php";
class FactureStats extends Stats class FactureStats extends Stats
{ {
var $db ; var $db ;
function FactureStats($DB, $socid=0) var $socid;
var $where;
var $table_element;
var $field;
function FactureStats($DB, $socid=0, $mode)
{ {
$this->db = $DB; $this->db = $DB;
if ($mode == 'customer')
{
$object=new Facture($this->db);
$this->table_element=$object->table_element;
$this->field='total';
}
if ($mode == 'supplier')
{
$object=new FactureFournisseur($this->db);
$this->table_element=$object->table_element;
$this->field='total_ht';
}
$this->socid = $socid; $this->socid = $socid;
$this->where =" fk_statut > 0";
if ($mode == 'customer') $this->where.=" AND (fk_statut != 3 OR close_code != 'replaced')"; // Exclude replaced invoices
if ($this->socid)
{
$this->where.=" AND fk_soc = ".$this->socid;
}
} }
@ -46,8 +74,10 @@ class FactureStats extends Stats
*/ */
function getNbByYear() function getNbByYear()
{ {
$sql = "SELECT date_format(datef,'%Y') as dm, count(*) FROM ".MAIN_DB_PREFIX."facture GROUP BY dm DESC WHERE fk_statut > 0"; $sql = "SELECT date_format(datef,'%Y') as dm, count(*)";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element." GROUP BY dm DESC";
$sql.= " WHERE ".$this->where;
return $this->_getNbByYear($sql); return $this->_getNbByYear($sql);
} }
@ -59,13 +89,11 @@ class FactureStats extends Stats
*/ */
function getNbByMonth($year) function getNbByMonth($year)
{ {
$sql = "SELECT date_format(datef,'%m') as dm, count(*) FROM ".MAIN_DB_PREFIX."facture"; $sql = "SELECT date_format(datef,'%m') as dm, count(*)";
$sql .= " WHERE date_format(datef,'%Y') = $year AND fk_statut > 0"; $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
if ($this->socid) $sql.= " WHERE date_format(datef,'%Y') = ".$year;
{ $sql.= " AND ".$this->where;
$sql .= " AND fk_soc = ".$this->socid; $sql.= " GROUP BY dm DESC";
}
$sql .= " GROUP BY dm DESC";
$res=$this->_getNbByMonth($year, $sql); $res=$this->_getNbByMonth($year, $sql);
//var_dump($res);print '<br>'; //var_dump($res);print '<br>';
@ -80,13 +108,11 @@ class FactureStats extends Stats
*/ */
function getAmountByMonth($year) function getAmountByMonth($year)
{ {
$sql = "SELECT date_format(datef,'%m') as dm, sum(total) FROM ".MAIN_DB_PREFIX."facture"; $sql = "SELECT date_format(datef,'%m') as dm, sum(".$this->field.")";
$sql .= " WHERE date_format(datef,'%Y') = $year AND fk_statut > 0"; $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
if ($this->socid) $sql.= " WHERE date_format(datef,'%Y') = ".$year;
{ $sql.= " AND ".$this->where;
$sql .= " AND fk_soc = ".$this->socid; $sql.= " GROUP BY dm DESC";
}
$sql .= " GROUP BY dm DESC";
$res=$this->_getAmountByMonth($year, $sql); $res=$this->_getAmountByMonth($year, $sql);
//var_dump($res);print '<br>'; //var_dump($res);print '<br>';
@ -100,16 +126,28 @@ class FactureStats extends Stats
*/ */
function getAverageByMonth($year) function getAverageByMonth($year)
{ {
$sql = "SELECT date_format(datef,'%m') as dm, avg(total) FROM ".MAIN_DB_PREFIX."facture"; $sql = "SELECT date_format(datef,'%m') as dm, avg(".$this->field.")";
$sql .= " WHERE date_format(datef,'%Y') = $year AND fk_statut > 0"; $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
if ($this->socid) $sql.= " WHERE date_format(datef,'%Y') = ".$year;
{ $sql.= " AND ".$this->where;
$sql .= " AND fk_soc = ".$this->socid; $sql.= " GROUP BY dm DESC";
}
$sql .= " GROUP BY dm DESC";
return $this->_getAverageByMonth($year, $sql); return $this->_getAverageByMonth($year, $sql);
} }
/**
* \brief Return nb, total and average
* \return array Array of values
*/
function getAllByYear()
{
$sql = "SELECT date_format(datef,'%Y') as year, count(*) as nb, sum(".$this->field.") as total, avg(".$this->field.") as avg";
$sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element;
$sql.= " WHERE ".$this->where;
$sql.= " GROUP BY year DESC";
return $this->_getAllByYear($sql);
}
} }
?> ?>

View File

@ -37,6 +37,13 @@ if ($user->societe_id > 0)
$socid = $user->societe_id; $socid = $user->societe_id;
} }
$year = strftime("%Y", time());
$startyear=$year-2;
$endyear=$year;
$mode='customer';
if (isset($_GET["mode"])) $mode=$_GET["mode"];
/* /*
* View * View
@ -44,15 +51,22 @@ if ($user->societe_id > 0)
llxHeader(); llxHeader();
print_fiche_titre($langs->trans("BillsStatistics"), $mesg); if ($mode == 'customer')
{
$title=$langs->trans("BillsStatistics");
$dir=$conf->facture->dir_temp;
}
if ($mode == 'supplier')
{
$title=$langs->trans("BillsStatisticsSuppliers");
$dir=$conf->fournisseur->facture->dir_temp;
}
create_exdir($conf->facture->dir_temp); print_fiche_titre($title, $mesg);
$year = strftime("%Y", time()); create_exdir($dir);
$startyear=$year-2;
$endyear=$year;
$stats = new FactureStats($db, $socid); $stats = new FactureStats($db, $socid, $mode);
// Build graphic number of invoices // Build graphic number of invoices
@ -60,8 +74,9 @@ $data = $stats->getNbByMonthWithPrevYear($endyear,$startyear);
//var_dump($data); //var_dump($data);
// $data = array(array('Lib',val1,val2,val3),...) // $data = array(array('Lib',val1,val2,val3),...)
$filenamenbinvoices = $conf->facture->dir_temp."/invoicesnbinyear-".$year.".png"; $filenamenbinvoices = $dir."/invoicesnbinyear-".$year.".png";
$fileurlnbinvoices = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesnbinyear-'.$year.'.png'; if ($mode == 'customer') $fileurlnbinvoices = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesnbinyear-'.$year.'.png';
if ($mode == 'supplier') $fileurlnbinvoices = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&amp;file=invoicesnbinyear-'.$year.'.png';
$px = new DolGraph(); $px = new DolGraph();
$mesg = $px->isGraphKo(); $mesg = $px->isGraphKo();
@ -94,8 +109,9 @@ $data = $stats->getAmountByMonthWithPrevYear($endyear,$startyear);
//var_dump($data); //var_dump($data);
// $data = array(array('Lib',val1,val2,val3),...) // $data = array(array('Lib',val1,val2,val3),...)
$filenameamountinvoices = $conf->facture->dir_temp."/invoicesamountinyear-".$year.".png"; $filenameamountinvoices = $dir."/invoicesamountinyear-".$year.".png";
$fileurlamountinvoices = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesamountinyear-'.$year.'.png'; if ($mode == 'customer') $fileurlamountinvoices = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=invoicesamountinyear-'.$year.'.png';
if ($mode == 'supplier') $fileurlamountinvoices = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&amp;file=invoicesamountinyear-'.$year.'.png';
$px = new DolGraph(); $px = new DolGraph();
$mesg = $px->isGraphKo(); $mesg = $px->isGraphKo();
@ -125,52 +141,66 @@ if (! $mesg)
} }
print '<table class="notopnoleftnopadd" width="100%"><tr>';
print '<td align="center" valign="top">';
// Show array // Show array
$sql = "SELECT date_format(datef,'%Y') as dm, count(*) as nb, sum(total) as total FROM ".MAIN_DB_PREFIX."facture WHERE fk_statut > 0 "; $data = $stats->getAllByYear();
if ($socid)
{
$sql .= " AND fk_soc=$socid";
}
$sql.= " GROUP BY dm DESC;";
$resql=$db->query($sql);
if ($resql)
{
$num = $db->num_rows($resql);
print '<table class="border" width="100%">'; print '<table class="border" width="100%">';
print '<tr height="24"><td align="center">'.$langs->trans("Year").'</td><td width="10%" align="center">'.$langs->trans("NumberOfBills").'</td><td align="center">'.$langs->trans("AmountTotal").'</td>'; print '<tr height="24">';
print '<td align="center" valign="top" rowspan="'.($num + 2).'">'; print '<td align="center">'.$langs->trans("Year").'</td>';
if ($mesg) { print $mesg; } print '<td align="center">'.$langs->trans("NumberOfBills").'</td>';
else { print '<td align="center">'.$langs->trans("AmountTotal").'</td>';
print '<img src="'.$fileurlnbinvoices.'" title="'.$langs->trans("NumberOfBills").'" alt="'.$langs->trans("NumberOfBills").'">'; print '<td align="center">'.$langs->trans("AmountAverage").'</td>';
print "<br>\n"; print '</tr>';
print '<img src="'.$fileurlamountinvoices.'" title="'.$langs->trans("AmountOfBills").'" alt="'.$langs->trans("AmountOfBills").'">';
}
print '</td></tr>';
while ($obj = $db->fetch_object($resql)) $oldyear=0;
{ foreach ($data as $val)
$nbproduct = $obj->nb; {
$year = $obj->dm; $year = $val['year'];
$total = price($obj->total); $nbproduct = $val['nb'];
$total = price($val['total']);
$avg = price($val['avg']);
while ($oldyear > $year+1)
{ // If we have empty year
$oldyear--;
print '<tr height="24">'; print '<tr height="24">';
print '<td align="center"><a href="month.php?year='.$year.'">'.$year.'</a></td>'; print '<td align="center"><a href="month.php?year='.$oldyear.'&amp;mode='.$mode.'">'.$oldyear.'</a></td>';
print '<td align="right">'.$nbproduct.'</td>'; print '<td align="right">0</td>';
print '<td align="right">'.$total.'</td></tr>'; print '<td align="right">0</td>';
print '<td align="right">0</td>';
print '</tr>';
} }
print '<tr height="24">';
print '<tr><td colspan="3"></td></tr>'; print '<td align="center"><a href="month.php?year='.$year.'&amp;mode='.$mode.'">'.$year.'</a></td>';
print '<td align="right">'.$nbproduct.'</td>';
print '</table>'; print '<td align="right">'.$total.'</td>';
$db->free($resql); print '<td align="right">'.$avg.'</td>';
} print '</tr>';
else $oldyear=$year;
{
dolibarr_print_error($db);
} }
print '</table>';
$db->close(); $db->close();
print '</td>';
print '<td align="center" valign="top">';
// Show graphs
print '<table class="border" width="100%"><tr valign="top"><td align="center">';
if ($mesg) { print $mesg; }
else {
print '<img src="'.$fileurlnbinvoices.'" title="'.$langs->trans("NumberOfBills").'" alt="'.$langs->trans("NumberOfBills").'">';
print "<br>\n";
print '<img src="'.$fileurlamountinvoices.'" title="'.$langs->trans("AmountOfBills").'" alt="'.$langs->trans("AmountOfBills").'">';
}
print '</td></tr></table>';
print '</td></tr></table>';
llxFooter('$Date$ - $Revision$'); llxFooter('$Date$ - $Revision$');
?> ?>

View File

@ -30,13 +30,19 @@ require_once(DOL_DOCUMENT_ROOT."/core/dolgraph.class.php");
$GRAPHWIDTH=500; $GRAPHWIDTH=500;
$GRAPHHEIGHT=200; $GRAPHHEIGHT=200;
// Sécurité accés client // Check security access
if ($user->societe_id > 0) if ($user->societe_id > 0)
{ {
$action = ''; $action = '';
$socid = $user->societe_id; $socid = $user->societe_id;
} }
$year = isset($_GET["year"])?$_GET["year"]:date("Y",time());
$mode='customer';
if (isset($_GET["mode"])) $mode=$_GET["mode"];
/* /*
* View * View
@ -44,22 +50,32 @@ if ($user->societe_id > 0)
llxHeader(); llxHeader();
$year = isset($_GET["year"])?$_GET["year"]:date("Y",time()); if ($mode == 'customer')
{
$title=$langs->trans("BillsStatistics");
$dir=$conf->facture->dir_temp;
}
if ($mode == 'supplier')
{
$title=$langs->trans("BillsStatisticsSuppliers");
$dir=$conf->fournisseur->facture->dir_temp;
}
$mesg = '<a href="month.php?year='.($year - 1).'">'.img_previous().'</a> '; $mesg = '<a href="month.php?year='.($year - 1).'&amp;mode='.$mode.'">'.img_previous().'</a> ';
$mesg.= $langs->trans("Year")." $year"; $mesg.= $langs->trans("Year")." $year";
$mesg.= ' <a href="month.php?year='.($year + 1).'">'.img_next().'</a>'; $mesg.= ' <a href="month.php?year='.($year + 1).'&amp;mode='.$mode.'">'.img_next().'</a>';
print_fiche_titre($title, $mesg);
print_fiche_titre($langs->trans("BillsStatistics"), $mesg); create_exdir($dir);
$stats = new FactureStats($db, $socid); $stats = new FactureStats($db, $socid, $mode);
create_exdir($conf->facture->dir_temp);
$data = $stats->getNbByMonth($year); $data = $stats->getNbByMonth($year);
$filename = $conf->facture->dir_temp."/facture".$year.".png"; $filename = $dir."/invoices-".$year.".png";
$fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=facture'.$year.'.png'; if ($mode == 'customer') $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoices-'.$year.'.png';
if ($mode == 'supplier') $fileurl = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&file=invoices-'.$year.'.png';
$px = new DolGraph(); $px = new DolGraph();
$mesg = $px->isGraphKo(); $mesg = $px->isGraphKo();
@ -81,8 +97,9 @@ if (! $mesg)
$data = $stats->getAmountByMonth($year); $data = $stats->getAmountByMonth($year);
$filename_amount = $conf->facture->dir_temp."/factureamount".$year.".png"; $filename_amount = $dir."/invoicesamount-".$year.".png";
$fileurl_amount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=factureamount'.$year.'.png'; if ($mode == 'customer') $fileurl_amount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesamount-'.$year.'.png';
if ($mode == 'supplier') $fileurl_amount = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&file=invoicesamount-'.$year.'.png';
$px = new DolGraph(); $px = new DolGraph();
$mesg = $px->isGraphKo(); $mesg = $px->isGraphKo();
@ -112,8 +129,9 @@ for ($i = 1 ; $i < 13 ; $i++)
$data[$i-1] = array(ucfirst(substr(strftime("%b",dolibarr_mktime(12,12,12,$i,1,$year)),0,3)), $res[$i]); $data[$i-1] = array(ucfirst(substr(strftime("%b",dolibarr_mktime(12,12,12,$i,1,$year)),0,3)), $res[$i]);
} }
$filename_avg = $conf->facture->dir_temp."/factureaverage".$year.".png"; $filename_avg = $dir."/invoicesaverage-".$year.".png";
$fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=factureaverage'.$year.'.png'; if ($mode == 'customer') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&file=invoicesaverage-'.$year.'.png';
if ($mode == 'supplier') $fileurl_avg = DOL_URL_ROOT.'/viewimage.php?modulepart=billstatssupplier&file=invoicesaverage-'.$year.'.png';
$px = new DolGraph(); $px = new DolGraph();
$mesg = $px->isGraphKo(); $mesg = $px->isGraphKo();

View File

@ -395,6 +395,8 @@ class MenuLeft {
} }
if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/fourn/facture/impayees.php", $langs->trans("Unpayed"),2,$user->rights->fournisseur->facture->lire); if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/fourn/facture/impayees.php", $langs->trans("Unpayed"),2,$user->rights->fournisseur->facture->lire);
if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/fourn/facture/paiement.php", $langs->trans("Payments"),2,$user->rights->fournisseur->facture->lire); if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/fourn/facture/paiement.php", $langs->trans("Payments"),2,$user->rights->fournisseur->facture->lire);
if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/compta/facture/stats/index.php?leftmenu=suppliers_bills&mode=supplier", $langs->trans("Statistics"),2,$user->rights->fournisseur->facture->lire);
} }
} }

View File

@ -377,6 +377,8 @@ class MenuLeft {
} }
if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/fourn/facture/impayees.php", $langs->trans("Unpayed"),2,$user->rights->fournisseur->facture->lire); if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/fourn/facture/impayees.php", $langs->trans("Unpayed"),2,$user->rights->fournisseur->facture->lire);
if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/fourn/facture/paiement.php", $langs->trans("Payments"),2,$user->rights->fournisseur->facture->lire); if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/fourn/facture/paiement.php", $langs->trans("Payments"),2,$user->rights->fournisseur->facture->lire);
if ($leftmenu=="suppliers_bills") $newmenu->add_submenu(DOL_URL_ROOT."/compta/facture/stats/index.php?leftmenu=suppliers_bills&mode=supplier", $langs->trans("Statistics"),2,$user->rights->fournisseur->facture->lire);
} }
} }

View File

@ -7,7 +7,8 @@ BillsCustomersUnpayed=Unpayed customers' invoices
BillsCustomersUnpayedForCompany=Unpayed customers' invoices for %s BillsCustomersUnpayedForCompany=Unpayed customers' invoices for %s
BillsSuppliersUnpayed=Unpayed suppliers' invoices BillsSuppliersUnpayed=Unpayed suppliers' invoices
BillsUnpayed=Unpayed BillsUnpayed=Unpayed
BillsStatistics=Invoices statistics BillsStatistics=Customers' invoices statistics
BillsStatisticsSuppliers=Suppliers' invoices statistics
InvoiceStandard=Standard invoice InvoiceStandard=Standard invoice
InvoiceStandardAsk=Standard invoice InvoiceStandardAsk=Standard invoice
InvoiceStandardDesc=This kind of invoice is the common invoice. InvoiceStandardDesc=This kind of invoice is the common invoice.

View File

@ -8,7 +8,8 @@ BillsCustomersUnpayedForCompany=Factures clients impay
BillsSuppliersUnpayed=Factures fournisseurs impayées BillsSuppliersUnpayed=Factures fournisseurs impayées
BillsUnpayed=Impayées BillsUnpayed=Impayées
BillsLate=Retards de paiement BillsLate=Retards de paiement
BillsStatistics=Statistiques factures BillsStatistics=Statistiques factures clients
BillsStatisticsSuppliers=Statistiques factures fournisseurs
InvoiceStandard=Facture standard InvoiceStandard=Facture standard
InvoiceStandardAsk=Facture standard InvoiceStandardAsk=Facture standard
InvoiceStandardDesc=Ce type de facture est la facture traditionnelle. On l'appelle aussi <b>facture de doit</b> (du verbe devoir). InvoiceStandardDesc=Ce type de facture est la facture traditionnelle. On l'appelle aussi <b>facture de doit</b> (du verbe devoir).

View File

@ -103,8 +103,9 @@ class Stats
/** /**
* \brief Renvoie le nombre d'element par ann<EFBFBD>e * \brief Return nb of elements by year
* * \param sql SQL request
* \return array
*/ */
function _getNbByYear($sql) function _getNbByYear($sql)
{ {
@ -129,6 +130,36 @@ class Stats
return $result; return $result;
} }
/**
* \brief Return nb of elements, total amount and avg amount by year
* \param sql SQL request
* \return array
*/
function _getAllByYear($sql)
{
$result = array();
$resql=$this->db->query($sql);
if ($resql)
{
$num = $this->db->num_rows($resql);
$i = 0;
while ($i < $num)
{
$row = $this->db->fetch_object($resql);
$result[$i]['year'] = $row->year;
$result[$i]['nb'] = $row->nb;
$result[$i]['total'] = $row->total;
$result[$i]['avg'] = $row->avg;
$i++;
}
$this->db->free($resql);
}
else {
dolibarr_print_error($this->db);
}
return $result;
}
/** /**
* \brief Renvoie le nombre de proposition par mois pour une annee donnee * \brief Renvoie le nombre de proposition par mois pour une annee donnee
@ -203,7 +234,7 @@ class Stats
for ($i = 1 ; $i < 13 ; $i++) for ($i = 1 ; $i < 13 ; $i++)
{ {
$data[$i-1] = array(ucfirst(substr(strftime("%b",mktime(12,0,0,$i,1,$year)),0,3)), $res[$i]); $data[$i-1] = array(ucfirst(substr(strftime("%b",dolibarr_mktime(12,0,0,$i,1,$year)),0,3)), $res[$i]);
} }
return $data; return $data;

View File

@ -167,7 +167,16 @@ if ($modulepart)
} }
$original_file=$conf->facture->dir_temp.'/'.$original_file; $original_file=$conf->facture->dir_temp.'/'.$original_file;
} }
elseif ($modulepart == 'billstatssupplier')
{
$user->getrights('fourn');
if ($user->rights->fournisseur->facture->lire)
{
$accessallowed=1;
}
$original_file=$conf->fournisseur->facture->dir_temp.'/'.$original_file;
}
// Wrapping pour les images des stats expeditions // Wrapping pour les images des stats expeditions
elseif ($modulepart == 'expeditionstats') elseif ($modulepart == 'expeditionstats')
{ {