dolibarr/htdocs/commande/class/commandestats.class.php

189 lines
5.5 KiB
PHP
Raw Permalink Normal View History

<?php
/* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (c) 2005 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2005-2009 Regis Houssin <regis@dolibarr.fr>
2003-11-09 18:32:09 +01:00
*
* 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 2 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, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/**
2010-06-05 17:32:18 +02:00
* \file htdocs/commande/class/commandestats.class.php
2008-09-08 15:32:37 +02:00
* \ingroup commandes
* \brief Fichier de la classe de gestion des stats des commandes
* \version $Id$
*/
2010-04-28 12:11:41 +02:00
include_once DOL_DOCUMENT_ROOT . "/core/class/stats.class.php";
2010-04-27 10:13:42 +02:00
include_once DOL_DOCUMENT_ROOT . "/commande/class/commande.class.php";
2010-04-28 14:53:28 +02:00
include_once DOL_DOCUMENT_ROOT . "/fourn/class/fournisseur.commande.class.php";
2005-02-06 16:00:01 +01:00
/**
2008-09-08 15:32:37 +02:00
* \class CommandeStats
* \brief Classe permettant la gestion des stats des commandes
*/
class CommandeStats extends Stats
2003-11-09 18:32:09 +01:00
{
2008-09-08 15:32:37 +02:00
var $db ;
var $socid;
var $where;
var $table_element;
var $field;
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
/**
* Constructor
*
* @param $DB Database handler
* @param $socid Id third party
2008-09-08 21:04:19 +02:00
* @param $mode Option
2010-06-05 17:20:09 +02:00
* @return CommandeStats
2008-09-08 15:32:37 +02:00
*/
2008-09-08 21:04:19 +02:00
function CommandeStats($DB, $socid=0, $mode)
2008-09-08 15:32:37 +02:00
{
global $user, $conf;
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
$this->db = $DB;
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
$this->socid = $socid;
2010-06-05 17:20:09 +02:00
2008-09-08 21:04:19 +02:00
if ($mode == 'customer')
{
$object=new Commande($this->db);
$this->from = MAIN_DB_PREFIX.$object->table_element." as c";
$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
2008-09-08 21:04:19 +02:00
$this->field='total_ht';
$this->where.= " c.fk_statut > 0";
}
if ($mode == 'supplier')
{
$object=new CommandeFournisseur($this->db);
$this->from = MAIN_DB_PREFIX.$object->table_element." as c";
$this->from.= ", ".MAIN_DB_PREFIX."societe as s";
2008-09-08 21:04:19 +02:00
$this->field='total_ht';
$this->where.= " c.fk_statut >= 3 AND c.date_commande IS NOT NULL";
}
$this->where.= " AND c.fk_soc = s.rowid AND s.entity = ".$conf->entity;
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND c.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
if($this->socid)
{
$this->where .= " AND c.fk_soc = ".$this->socid;
}
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
}
/**
2010-04-08 15:19:42 +02:00
* \brief Renvoie le nombre de commande par mois pour une ann<EFBFBD>e donn<EFBFBD>e
2008-09-08 15:32:37 +02:00
*
*/
function getNbByMonth($year)
{
global $conf;
global $user;
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
$sql = "SELECT date_format(c.date_commande,'%m') as dm, count(*) nb";
$sql.= " FROM ".$this->from;
2008-09-08 15:32:37 +02:00
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year;
$sql.= " AND ".$this->where;
2011-03-09 11:47:17 +01:00
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
2008-09-08 15:32:37 +02:00
return $this->_getNbByMonth($year, $sql);
}
/**
2011-03-09 11:47:17 +01:00
* Renvoie le nombre de commande par annee
2008-09-08 15:32:37 +02:00
*
*/
function getNbByYear()
{
global $conf;
global $user;
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
$sql = "SELECT date_format(c.date_commande,'%Y') as dm, count(*), sum(c.".$this->field.")";
$sql.= " FROM ".$this->from;
2008-09-08 15:32:37 +02:00
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
2011-03-09 11:47:17 +01:00
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
2008-09-08 15:32:37 +02:00
return $this->_getNbByYear($sql);
}
/**
2011-03-09 11:47:17 +01:00
* Renvoie le nombre de commande par mois pour une annee donnee
2008-09-08 15:32:37 +02:00
*
*/
function getAmountByMonth($year)
{
global $conf;
global $user;
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
$sql = "SELECT date_format(c.date_commande,'%m') as dm, sum(c.".$this->field.")";
$sql.= " FROM ".$this->from;
2008-09-08 15:32:37 +02:00
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year;
$sql.= " AND ".$this->where;
2011-03-09 11:47:17 +01:00
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
2008-09-08 15:32:37 +02:00
return $this->_getAmountByMonth($year, $sql);
}
/**
2011-03-09 11:47:17 +01:00
* Renvoie le nombre de commande par mois pour une annee donnee
2008-09-08 15:32:37 +02:00
*
*/
function getAverageByMonth($year)
{
global $conf;
global $user;
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
$sql = "SELECT date_format(c.date_commande,'%m') as dm, avg(c.".$this->field.")";
$sql.= " FROM ".$this->from;
2008-09-08 15:32:37 +02:00
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE date_format(c.date_commande,'%Y') = ".$year;
$sql.= " AND ".$this->where;
2011-03-09 11:47:17 +01:00
$sql.= " GROUP BY dm";
$sql.= $this->db->order('dm','DESC');
2008-09-08 15:32:37 +02:00
return $this->_getAverageByMonth($year, $sql);
}
2010-06-05 17:20:09 +02:00
2008-09-08 15:32:37 +02:00
/**
* \brief Return nb, total and average
* \return array Array of values
*/
function getAllByYear()
{
global $user;
2010-06-05 17:20:09 +02:00
2008-09-08 21:04:19 +02:00
$sql = "SELECT date_format(c.date_commande,'%Y') as year, count(*) as nb, sum(c.".$this->field.") as total, avg(".$this->field.") as avg";
$sql.= " FROM ".$this->from;
2008-09-08 15:32:37 +02:00
if (!$user->rights->societe->client->voir && !$this->socid) $sql .= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
$sql.= " WHERE ".$this->where;
2011-03-09 11:47:17 +01:00
$sql.= " GROUP BY year";
$sql.= $this->db->order('year','DESC');
2008-09-08 15:32:37 +02:00
return $this->_getAllByYear($sql);
2010-06-05 17:20:09 +02:00
}
2003-11-09 18:32:09 +01:00
}
?>