31include_once DOL_DOCUMENT_ROOT.
'/core/class/stats.class.php';
32include_once DOL_DOCUMENT_ROOT.
'/contrat/class/contrat.class.php';
33include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
44 public $table_element;
102 public function __construct($db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
108 $this->socid = ($socid > 0 ? $socid : 0);
109 $this->userid = $userid;
110 $this->cachefilesuffix = $mode;
114 $this->from = MAIN_DB_PREFIX.$object->table_element.
" as c";
115 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line.
" as cd";
116 $this->field =
'total_ht';
117 $this->field_line =
'total_ht';
119 $this->categ_link = MAIN_DB_PREFIX.
'categorie_societe';
122 $this->where .= ($this->where ?
' AND ' :
'').
'c.entity IN ('.
getEntity(
'contract').
')';
125 $this->where .=
" AND c.fk_soc = ".((int) $this->socid);
127 if ($this->userid > 0) {
128 $this->where .=
' AND c.fk_user_author = '.((int) $this->userid);
132 $this->join .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'societe as s ON s.rowid = c.fk_soc';
133 $this->where .=
' AND s.fk_typent = '.((int) $typentid);
137 $this->where .=
' AND EXISTS (SELECT rowid FROM '.$this->categ_link.
' as cats WHERE cats.fk_soc = c.fk_soc AND cats.fk_categorie = '.((int) $categid).
')';
152 $sql =
"SELECT date_format(c.date_contrat,'%m') as dm, COUNT(*) as nb";
153 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
154 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
155 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
159 $sql .=
" AND ".$this->where;
160 $sql .=
" GROUP BY dm";
161 $sql .= $this->db->order(
'dm',
'DESC');
177 $sql =
"SELECT date_format(c.date_contrat,'%Y') as dm, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).
")";
178 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
179 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
180 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
183 $sql .=
" WHERE ".$this->where;
184 $sql .=
" GROUP BY dm";
185 $sql .= $this->db->order(
'dm',
'DESC');
201 $sql =
"SELECT date_format(c.date_contrat,'%m') as dm, count(cd.".$this->db->sanitize($this->field).
")";
202 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
203 $sql .=
" INNER JOIN ".$this->db->sanitize($this->from_line, 0, 1, 1).
" ON c.rowid = cd.fk_contrat";
204 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
205 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
209 $sql .=
" AND ".$this->where;
210 $sql .=
" GROUP BY dm";
211 $sql .= $this->db->order(
'dm',
'DESC');
227 $sql =
"SELECT date_format(c.date_contrat,'%m') as dm, AVG(c.".$this->db->sanitize($this->field).
")";
228 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
229 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
230 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
234 $sql .=
" AND ".$this->where;
235 $sql .=
" GROUP BY dm";
236 $sql .= $this->db->order(
'dm',
'DESC');
250 $sql =
"SELECT date_format(c.date_contrat,'%Y') as year, COUNT(*) as nb, SUM(c.".$this->db->sanitize($this->field).
") as total, AVG(".$this->db->sanitize($this->field).
") as avg";
251 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
252 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
253 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
256 $sql .=
" WHERE ".$this->where;
257 $sql .=
" GROUP BY year";
258 $sql .= $this->db->order(
'year',
'DESC');
274 $sql =
"SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.
") as total, AVG(tl.".$this->field_line.
") as avg";
275 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
276 $sql .=
" INNER JOIN ".$this->db->sanitize($this->from_line, 0, 1, 1).
" ON c.rowid = tl.fk_commande";
277 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"product as product ON tl.fk_product = product.rowid";
278 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
279 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
282 $sql .=
" WHERE ".$this->where;
284 $sql .=
" GROUP BY product.ref";
285 $sql .= $this->db->order(
'nb',
'DESC');
if(! $sortfield) if(! $sortorder) $object
Class to manage contract statistics.
getAllByYear()
Return nb, total and average.
__construct($db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getNbByMonth($year, $format=0)
Return contracts number by month for a year.
getNbByYear()
Return contracts number per year.
getAmountByMonth($year, $format=0)
Return the contracts amount by month for a year.
getAverageByMonth($year)
Return the contracts amount average by month for a year.
Parent class of statistics class.
_getAverageByMonth($year, $sql, $format=0)
Return the amount average par month for a given year.
_getAmountByMonth($year, $sql, $format=0)
Return the amount per month for a given year.
_getNbByYear($sql)
Return nb of elements by year.
_getAllByYear($sql)
Return nb of elements, total amount and avg amount each year.
_getAllByProduct($sql, $limit=10)
Return number or total of product refs.
_getNbByMonth($year, $sql, $format=0)
Return number of documents per month for a given year.
dol_get_first_day($year, $month=1, $gm=false)
Return GMT time for first day of a month or year.
dol_get_last_day($year, $month=12, $gm=false)
Return GMT time for last day of a month or year.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.