dolibarr  7.0.0-beta
propalestats.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3  * Copyright (c) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
4  * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@capnetworks.com>
5  * Copyright (c) 2011 Juanjo Menent <jmenent@2byte.es>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
27 include_once DOL_DOCUMENT_ROOT . '/core/class/stats.class.php';
28 include_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php';
29 include_once DOL_DOCUMENT_ROOT . '/supplier_proposal/class/supplier_proposal.class.php';
30 include_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
31 
32 
36 class PropaleStats extends Stats
37 {
38  public $table_element;
39 
40  var $socid;
41  var $userid;
42 
43  var $from;
44  var $field;
45  var $where;
46 
47 
56  function __construct($db, $socid=0, $userid=0, $mode='customer')
57  {
58  global $user, $conf;
59 
60  $this->db = $db;
61  $this->socid = ($socid > 0 ? $socid : 0);
62  $this->userid = $userid;
63 
64  if ($mode == 'customer')
65  {
66  $object=new Propal($this->db);
67 
68  $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
69  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
70  $this->field_date='p.datep';
71  $this->field='total_ht';
72  $this->field_line='total_ht';
73 
74  $this->where.= " p.fk_statut > 0";
75  }
76  if ($mode == 'supplier')
77  {
78  $object=new SupplierProposal($this->db);
79 
80  $this->from = MAIN_DB_PREFIX.$object->table_element." as p";
81  $this->from_line = MAIN_DB_PREFIX.$object->table_element_line." as tl";
82  $this->field_date='p.date_valid';
83  $this->field='total_ht';
84  $this->field_line='total_ht';
85 
86  $this->where.= " p.fk_statut > 0"; // Validated, accepted, refused and closed
87  }
88  //$this->where.= " AND p.fk_soc = s.rowid AND p.entity = ".$conf->entity;
89  $this->where.= " AND p.entity IN (".getEntity('propal').")";
90  if (!$user->rights->societe->client->voir && !$this->socid) $this->where .= " AND p.fk_soc = sc.fk_soc AND sc.fk_user = " .$user->id;
91  if($this->socid)
92  {
93  $this->where.=" AND p.fk_soc = ".$this->socid;
94  }
95  if ($this->userid > 0) $this->where.=' AND fk_user_author = '.$this->userid;
96  }
97 
98 
106  function getNbByMonth($year, $format=0)
107  {
108  global $user;
109 
110  $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, COUNT(*) as nb";
111  $sql.= " FROM ".$this->from;
112  if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
113  $sql.= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
114  $sql.= " AND ".$this->where;
115  $sql.= " GROUP BY dm";
116  $sql.= $this->db->order('dm','DESC');
117 
118  $res=$this->_getNbByMonth($year, $sql, $format);
119  return $res;
120  }
121 
128  function getNbByYear()
129  {
130  global $user;
131 
132  $sql = "SELECT date_format(".$this->field_date.",'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->field.")";
133  $sql.= " FROM ".$this->from;
134  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
135  $sql.= " WHERE ".$this->where;
136  $sql.= " GROUP BY dm";
137  $sql.= $this->db->order('dm','DESC');
138 
139  return $this->_getNbByYear($sql);
140  }
141 
149  function getAmountByMonth($year, $format)
150  {
151  global $user;
152 
153  $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, SUM(p.".$this->field.")";
154  $sql.= " FROM ".$this->from;
155  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
156  $sql.= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
157  $sql.= " AND ".$this->where;
158  $sql.= " GROUP BY dm";
159  $sql.= $this->db->order('dm','DESC');
160 
161  $res=$this->_getAmountByMonth($year, $sql, $format);
162  return $res;
163  }
164 
171  function getAverageByMonth($year)
172  {
173  global $user;
174 
175  $sql = "SELECT date_format(".$this->field_date.",'%m') as dm, AVG(p.".$this->field.")";
176  $sql.= " FROM ".$this->from;
177  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
178  $sql.= " WHERE ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year))."' AND '".$this->db->idate(dol_get_last_day($year))."'";
179  $sql.= " AND ".$this->where;
180  $sql.= " GROUP BY dm";
181  $sql.= $this->db->order('dm','DESC');
182 
183  return $this->_getAverageByMonth($year, $sql);
184  }
185 
191  function getAllByYear()
192  {
193  global $user;
194 
195  $sql = "SELECT date_format(".$this->field_date.",'%Y') as year, COUNT(*) as nb, SUM(".$this->field.") as total, AVG(".$this->field.") as avg";
196  $sql.= " FROM ".$this->from;
197  if (!$user->rights->societe->client->voir && !$this->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
198  $sql.= " WHERE ".$this->where;
199  $sql.= " GROUP BY year";
200  $sql.= $this->db->order('year','DESC');
201 
202  return $this->_getAllByYear($sql);
203  }
204 
205 
206 
213  function getAllByProduct($year)
214  {
215  global $user;
216 
217  $sql = "SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.") as total, AVG(tl.".$this->field_line.") as avg";
218  $sql.= " FROM ".$this->from.", ".$this->from_line.", ".MAIN_DB_PREFIX."product as product";
219  if (!$user->rights->societe->client->voir && !$user->socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
220  $sql.= " WHERE ".$this->where;
221  $sql.= " AND p.rowid = tl.fk_propal AND tl.fk_product = product.rowid";
222  $sql.= " AND ".$this->field_date." BETWEEN '".$this->db->idate(dol_get_first_day($year,1,false))."' AND '".$this->db->idate(dol_get_last_day($year,12,false))."'";
223  $sql.= " GROUP BY product.ref";
224  $sql.= $this->db->order('nb','DESC');
225  //$sql.= $this->db->plimit(20);
226 
227  return $this->_getAllByProduct($sql);
228  }
229 }
Parent class of statistics class.
Definition: stats.class.php:30
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
Definition: date.lib.php:445
_getNbByMonth($year, $sql, $format=0)
Renvoie le nombre de proposition par mois pour une annee donnee.
getAmountByMonth($year, $format)
Return the propals amount by month for a year.
getAllByYear()
Return nb, total and average.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
__construct($db, $socid=0, $userid=0, $mode='customer')
Constructor.
getAllByProduct($year)
Return nb, amount of predefined product for year.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
Definition: date.lib.php:459
getAverageByMonth($year)
Return the propals amount average by month for a year.
Class to manage price ask supplier.
_getAmountByMonth($year, $sql, $format=0)
Renvoie le nombre d'element par mois pour une annee donnee.
getNbByMonth($year, $format=0)
Return propals number by month for a year.
_getNbByYear($sql)
Return nb of elements by year.
getNbByYear()
Return propals number per year.
_getAverageByMonth($year, $sql, $format=0)
Renvoie le montant moyen par mois pour une annee donnee.
Class to manage proposals statistics.
Class to manage proposals.