29include_once DOL_DOCUMENT_ROOT.
'/core/class/stats.class.php';
30include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
42 public $table_element;
99 public function __construct($db, $socid, $mode, $userid = 0, $categid = 0)
104 $this->socid = ($socid > 0 ? $socid : 0);
105 $this->userid = $userid;
106 $this->cachefilesuffix = $mode;
110 $this->from = MAIN_DB_PREFIX.$object->table_element.
" as c";
111 if (
$object->ismultientitymanaged && (
string)
$object->ismultientitymanaged !=
'1') {
113 $tmparray = explode(
'@', (
string)
$object->ismultientitymanaged);
114 $field = $tmparray[0];
115 $table = $tmparray[1];
116 $this->from =
" INNER JOIN ".MAIN_DB_PREFIX.$table.
" as e ON c.".$this->db->sanitize($field).
" = e.rowid AND e.entity IN (".
getEntity($table.
'@mymodule').
")";
118 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line.
" as tl";
119 $this->field =
'total_ht';
120 $this->field_line =
'total_ht';
122 $this->categ_link = MAIN_DB_PREFIX.
'categorie_societe';
123 if ((
string)
$object->ismultientitymanaged ==
'1') {
124 $this->where .= ($this->where ?
' AND ' :
'').
"c.entity IN (".
getEntity(
'myobject@mymodule').
")";
127 $this->where .= ($this->where ?
' AND ' :
'').
"c.fk_soc = ".((
int) $this->socid);
129 if ($this->userid > 0) {
130 $this->where .= ($this->where ?
' AND ' :
'').
"c.fk_user_creat = ".((
int) $this->userid);
134 $this->where .= ($this->where ?
' AND ' :
'').
'EXISTS (SELECT rowid FROM '.$this->db->sanitize($this->categ_link).
' as cats WHERE cats.fk_soc = c.fk_soc AND cats.fk_categorie = '.((int) $categid).
')';
137 $parameters = array(
'socid' => $socid);
138 $hookmanager->executeHooks(
'printFieldListWhere', $parameters,
$object);
139 $this->where .= $hookmanager->resPrint;
153 $sql =
"SELECT date_format(c.date_creation, '%m') as dm, COUNT(*) as nb";
154 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
155 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
156 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
160 $sql .= ($this->where ?
" AND ".$this->where :
"");
161 $sql .=
" GROUP BY dm";
162 $sql .= $this->db->order(
'dm',
'DESC');
177 $sql =
"SELECT date_format(c.date_creation, '%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 .= ($this->where ?
" WHERE ".$this->where :
"");
184 $sql .=
" GROUP BY dm";
185 $sql .= $this->db->order(
'dm',
'DESC');
201 $sql =
"SELECT date_format(c.date_creation, '%m') as dm, SUM(c.".$this->db->sanitize($this->field).
")";
202 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
203 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
204 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
208 $sql .= ($this->where ?
" AND ".$this->where :
"");
209 $sql .=
" GROUP BY dm";
210 $sql .= $this->db->order(
'dm',
'DESC');
226 $sql =
"SELECT date_format(c.date_creation, '%m') as dm, AVG(c.".$this->db->sanitize($this->field).
")";
227 $sql .=
" FROM ".$this->db->sanitize($this->from, 0, 1, 1);
228 if (empty($user->socid) && !$user->hasRight(
'societe',
'client',
'voir')) {
229 $sql .=
" INNER JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON c.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
233 $sql .= ($this->where ?
" AND ".$this->where :
"");
234 $sql .=
" GROUP BY dm";
235 $sql .= $this->db->order(
'dm',
'DESC');
249 $sql =
"SELECT date_format(c.date_creation, '%Y') as year, COUNT(*) as nb";
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 .= ($this->where ?
" 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->db->sanitize($this->field_line).
") as total, AVG(tl.".$this->db->sanitize($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;
283 $sql .=
" AND c.date_creation BETWEEN '".$this->db->idate(
dol_get_first_day($year, 1,
false)).
"' AND '".$this->db->idate(
dol_get_last_day($year, 12,
false)).
"'";
284 $sql .=
" GROUP BY product.ref";
285 $sql .= $this->db->order(
'nb',
'DESC');
if(! $sortfield) if(! $sortorder) $object
Class to manage order statistics (customer and supplier)
getAllByYear()
Return nb, total and average.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getAmountByMonth($year, $format=0)
Return the orders amount by month for a year.
__construct($db, $socid, $mode, $userid=0, $categid=0)
Constructor.
getAverageByMonth($year)
Return the orders amount average by month for a year.
getNbByYear()
Return orders number per year.
getNbByMonth($year, $format=0)
Return orders number 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.
if(!function_exists( 'dol_getprefix')) dol_include_once($relpath, $classname='')
Make an include_once using default root and alternate root if it fails.
getEntity($element, $shared=1, $currentobject=null)
Get list of entity id to use.