Skip to content

Commit

Permalink
New: Start to work on box for product distributions
Browse files Browse the repository at this point in the history
Qual: Start to move code of dol_print_graph function into class
DolGraph.
  • Loading branch information
eldy committed Aug 5, 2013
1 parent c18fed7 commit 85572a4
Show file tree
Hide file tree
Showing 7 changed files with 565 additions and 106 deletions.
28 changes: 28 additions & 0 deletions htdocs/compta/facture/class/facturestats.class.php
Expand Up @@ -62,13 +62,17 @@ function __construct($db, $socid, $mode, $userid=0)
{
$object=new Facture($this->db);
$this->from = MAIN_DB_PREFIX.$object->table_element." as f";
$this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
$this->field='total';
$this->field_line='total_ht';
}
if ($mode == 'supplier')
{
$object=new FactureFournisseur($this->db);
$this->from = MAIN_DB_PREFIX.$object->table_element." as f";
$this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
$this->field='total_ht';
$this->field_line='total_ht';
}

$this->where = " f.fk_statut > 0";
Expand Down Expand Up @@ -189,6 +193,30 @@ function getAllByYear()

return $this->_getAllByYear($sql);
}

/**
* Return nb, amount of predefined product for year
*
* @param int $year Year to scan
* @return array Array of values
*/
function getAllByProduct($year)
{
global $user;

$sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
$sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
//if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
$sql.= " AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
$sql.= " GROUP BY product.ref";
$sql.= $this->db->order('nb','DESC');
$sql.= $this->db->plimit(20);

return $this->_getAllByProduct($sql);
}


}

?>
252 changes: 252 additions & 0 deletions htdocs/core/boxes/box_graph_product_distribution.php
@@ -0,0 +1,252 @@
<?php
/* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* \file htdocs/core/boxes/box_graph_product_distribution.php.php
* \ingroup factures
* \brief Box to show graph of invoices per month
*/
include_once DOL_DOCUMENT_ROOT.'/core/boxes/modules_boxes.php';


/**
* Class to manage the box to show last invoices
*/
class box_graph_product_distribution extends ModeleBoxes
{
var $boxcode="productdistribution";
var $boximg="object_product";
var $boxlabel="BoxProductDistribution";
var $depends = array("product|service");

var $db;

var $info_box_head = array();
var $info_box_contents = array();


/**
* Constructor
*
* @param DoliDB $db Database handler
* @param string $param More parameters
*/
function __construct($db,$param)
{
global $conf;

$this->db=$db;
}

/**
* Load data into info_box_contents array to show array later.
*
* @param int $max Maximum number of records to load
* @return void
*/
function loadBox($max=5)
{
global $conf, $user, $langs, $db;

$this->max=$max;

$refreshaction='refresh_'.$this->boxcode;

include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
include_once DOL_DOCUMENT_ROOT.'/comm/propal/class/propal.class.php';
include_once DOL_DOCUMENT_ROOT.'/commande/class/commande.class.php';
$facturestatic=new Facture($db);

$text = $langs->trans("BoxProductDistribution",$max);
$this->info_box_head = array(
'text' => $text,
'limit'=> dol_strlen($text),
'graph'=> 1,
'sublink'=>'',
'subtext'=>$langs->trans("Filter"),
'subpicto'=>'filter.png',
'subclass'=>'linkobject',
'target'=>'none' // Set '' to get target="_blank"
);

$param_year='DOLUSERCOOKIE_param'.$this->boxcode.'year';
$param_showinvoicenb='DOLUSERCOOKIE_param'.$this->boxcode.'showinvoicenb';
$param_showpropalnb='DOLUSERCOOKIE_param'.$this->boxcode.'showpropalnb';
$showinvoicenb=GETPOST($param_showinvoicenb,'alpha',4);
$showpropalnb=GETPOST($param_showpropalnb,'alpha',4);
if (empty($showinvoicenb) && empty($showpropalnb)) { $showpropalnb=1; $showinvoicenb=1; }
$nowarray=dol_getdate(dol_now(),true);
$year=(GETPOST($param_year,'',4)?GETPOST($param_year,'int',4):$nowarray['year']);

if ($user->rights->facture->lire)
{

include_once DOL_DOCUMENT_ROOT.'/core/class/dolgraph.class.php';
include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facturestats.class.php';
$mode='customer';
$userid=0;
$WIDTH=(($showinvoicenb && $showpropalnb) || ! empty($conf->dol_optimize_smallscreen))?'256':'320';
$HEIGHT='192';

$stats = new FactureStats($this->db, 0, $mode, ($userid>0?$userid:0));

// Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
if ($showinvoicenb)
{
$data1 = $stats->getAllByProductEntry($year,(GETPOST('action')==$refreshaction?-1:(3600*24)));

$filenamenb = $dir."/prodserforinvoice-".$year.".png";
$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=productstats&amp;file=prodserforinvoice-'.$year.'.png';

$px1 = new DolGraph();
$mesg = $px1->isGraphKo();
if (! $mesg)
{
$px1->SetData($data1);
unset($data1);
$px1->SetPrecisionY(0);
$i=0;$tot=count($data2);$legend=array();
while ($i <= $tot)
{
$legend[]=$data2[$i][0];
$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->SetPrecisionY(0);
$px1->SetCssPrefix("cssboxes");
//$px1->mode='depth';
$px1->SetType(array('pie'));
$px1->SetTitle($langs->trans("BoxProductDistributionFor",$langs->trans("ProductsServices"),$langs->transnoentitiesnoconv("Invoices")));

$px1->draw($filenamenb,$fileurlnb);
}
}
}

/* if ($user->rights->propal->lire)
{
// Build graphic number of object. $data = array(array('Lib',val1,val2,val3),...)
if ($showpropalnb)
{
$data2 = $stats->getAmountByMonthWithPrevYear($year,(GETPOST('action')==$refreshaction?-1:(3600*24)));
$filenamenb = $dir."/prodserforpropal-".$year.".png";
$fileurlnb = DOL_URL_ROOT.'/viewimage.php?modulepart=billstats&amp;file=prodserforpropal-'.$year.'.png';
$px2 = new DolGraph();
$mesg = $px2->isGraphKo();
if (! $mesg)
{
$px2->SetData($data2);
unset($data2);
$px2->SetPrecisionY(0);
$i=0;$tot=count($data2);$legend=array();
while ($i <= $tot)
{
$legend[]=$data2[$i][0];
$i++;
}
$px2->SetLegend($legend);
$px2->SetMaxValue($px2->GetCeilMaxValue());
$px2->SetWidth($WIDTH);
$px2->SetHeight($HEIGHT);
//$px2->SetYLabel($langs->trans("AmountOfBillsHT"));
$px2->SetShading(3);
$px2->SetHorizTickIncrement(1);
$px2->SetPrecisionY(0);
$px2->SetCssPrefix("cssboxes");
$px2->mode='depth';
$px2->SetTitle($langs->trans("BoxProductDistributionFor",$langs->trans("ProductsServices"),$langs->transnoentitiesnoconv("Proposals")));
$px2->draw($filenamenb,$fileurlnb);
}
}
}
*/
if (! $mesg)
{
$stringtoshow='';
$stringtoshow.='<script type="text/javascript" language="javascript">
jQuery(document).ready(function() {
jQuery("#idsubimg'.$this->boxcode.'").click(function() {
jQuery("#idfilter'.$this->boxcode.'").toggle();
});
});
</script>';
$stringtoshow.='<div class="center hideobject" id="idfilter'.$this->boxcode.'">'; // hideobject is to start hidden
$stringtoshow.='<form class="flat formboxfilter" method="POST" action="'.$_SERVER["PHP_SELF"].'">';
$stringtoshow.='<input type="hidden" name="action" value="'.$refreshaction.'">';
$stringtoshow.='<input type="hidden" name="DOL_AUTOSET_COOKIE" value="'.$param_year.','.$param_showinvoicenb.','.$param_showpropalnb.'">';
$stringtoshow.='<input type="checkbox" name="'.$param_showinvoicenb.'"'.($showinvoicenb?' checked="true"':'').'"> '.$langs->trans("ForInvoice");
$stringtoshow.=' &nbsp; ';
$stringtoshow.='<input type="checkbox" name="'.$param_showpropalnb.'"'.($showpropalnb?' checked="true"':'').'"> '.$langs->trans("ForProposals");
$stringtoshow.='&nbsp;';
$stringtoshow.='<input type="checkbox" name="'.$param_showordernb.'"'.($showordernb?' checked="true"':'').'"> '.$langs->trans("ForOrders");
$stringtoshow.='<br>';
$stringtoshow.=$langs->trans("Year").' <input class="flat" size="4" type="text" name="'.$param_year.'" value="'.$year.'">';
$stringtoshow.='<input type="image" src="'.img_picto($langs->trans("Refresh"),'refresh.png','','',1).'">';
$stringtoshow.='</form>';
$stringtoshow.='</div>';
if ($showinvoicenb && $showpropalnb)
{
$stringtoshow.='<div class="fichecenter">';
$stringtoshow.='<div class="fichehalfleft">';
}
if ($showinvoicenb) $stringtoshow.=$px1->show();
if ($showinvoicenb && $showpropalnb)
{
$stringtoshow.='</div>';
$stringtoshow.='<div class="fichehalfright">';
}
// if ($showpropalnb) $stringtoshow.=$px2->show();
if ($showinvoicenb && $showpropalnb)
{
$stringtoshow.='</div>';
$stringtoshow.='</div>';
}
$this->info_box_contents[0][0] = array('td' => 'align="center" class="nohover"','textnoformat'=>$stringtoshow);
}
else
{
$this->info_box_contents[0][0] = array( 'td' => 'align="left" class="nohover"',
'maxlength'=>500,
'text' => $mesg);
}

}

/**
* Method to show box
*
* @param array $head Array with properties of box title
* @param array $contents Array with properties of box lines
* @return void
*/
function showBox($head = null, $contents = null)
{
parent::showBox($this->info_box_head, $this->info_box_contents);
}

}

?>

0 comments on commit 85572a4

Please sign in to comment.