27include_once DOL_DOCUMENT_ROOT.
'/core/class/stats.class.php';
28include_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
29include_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
30include_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
49 public $table_element;
79 public function __construct(
DoliDB $db, $socid, $mode, $userid = 0, $typentid = 0, $categid = 0)
84 $this->socid = ($socid > 0 ? $socid : 0);
85 $this->userid = $userid;
86 $this->cachefilesuffix = $mode;
89 if ($mode ==
'customer') {
91 $this->from = MAIN_DB_PREFIX.$object->table_element.
" as f";
92 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line.
" as tl";
93 $this->field =
'total_ht';
94 $this->field_line =
'total_ht';
96 if ($mode ==
'supplier') {
98 $this->from = MAIN_DB_PREFIX.$object->table_element.
" as f";
99 $this->from_line = MAIN_DB_PREFIX.$object->table_element_line.
" as tl";
100 $this->field =
'total_ht';
101 $this->field_line =
'total_ht';
104 $this->where .= ($this->where ?
' AND ' :
'').
" f.fk_statut >= 0";
105 $this->where .=
" AND f.entity IN (".getEntity(
'invoice').
")";
106 if (!$user->hasRight(
'societe',
'client',
'voir')) {
107 $this->where .=
" AND f.fk_soc = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
109 if ($mode ==
'customer') {
110 $this->where .=
" AND (f.fk_statut <> 3 OR f.close_code <> 'replaced')";
113 $this->where .=
" AND f.fk_soc = ".((int) $this->socid);
115 if ($this->userid > 0) {
116 $this->where .=
' AND f.fk_user_author = '.((int) $this->userid);
118 if ($mode ==
'customer') {
120 $this->where .=
" AND f.type IN (0,1,2,5)";
122 $this->where .=
" AND f.type IN (0,1,2,3,5)";
125 if ($mode ==
'supplier') {
127 $this->where .=
" AND f.type IN (0,1,2,5)";
129 $this->where .=
" AND f.type IN (0,1,2,3,5)";
134 $this->join .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'societe as s ON s.rowid = f.fk_soc';
135 $this->where .=
' AND s.fk_typent = '.((int) $typentid);
139 $this->where .=
' AND EXISTS (SELECT rowid FROM '.MAIN_DB_PREFIX.
'categorie_societe as cats WHERE cats.fk_soc = f.fk_soc AND cats.fk_categorie = '.((int) $categid).
')';
155 $sql =
"SELECT date_format(f.datef,'%m') as dm, COUNT(*) as nb";
156 $sql .=
" FROM ".$this->from;
157 if (!$user->hasRight(
'societe',
'client',
'voir')) {
158 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
162 $sql .=
" AND ".$this->where;
163 $sql .=
" GROUP BY dm";
164 $sql .= $this->db->order(
'dm',
'DESC');
181 $sql =
"SELECT date_format(f.datef,'%Y') as dm, COUNT(*), SUM(c.".$this->field.
")";
182 $sql .=
" FROM ".$this->from;
183 if (!$user->hasRight(
'societe',
'client',
'voir')) {
184 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
187 $sql .=
" WHERE ".$this->where;
188 $sql .=
" GROUP BY dm";
189 $sql .= $this->db->order(
'dm',
'DESC');
206 $sql =
"SELECT date_format(datef,'%m') as dm, SUM(f.".$this->field.
")";
207 $sql .=
" FROM ".$this->from;
208 if (!$user->hasRight(
'societe',
'client',
'voir')) {
209 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
213 $sql .=
" AND ".$this->where;
214 $sql .=
" GROUP BY dm";
215 $sql .= $this->db->order(
'dm',
'DESC');
232 $sql =
"SELECT date_format(datef,'%m') as dm, AVG(f.".$this->field.
")";
233 $sql .=
" FROM ".$this->from;
234 if (!$user->hasRight(
'societe',
'client',
'voir')) {
235 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
239 $sql .=
" AND ".$this->where;
240 $sql .=
" GROUP BY dm";
241 $sql .= $this->db->order(
'dm',
'DESC');
255 $sql =
"SELECT date_format(datef,'%Y') as year, COUNT(*) as nb, SUM(f.".$this->field.
") as total, AVG(f.".$this->field.
") as avg";
256 $sql .=
" FROM ".$this->from;
257 if (!$user->hasRight(
'societe',
'client',
'voir')) {
258 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
261 $sql .=
" WHERE ".$this->where;
262 $sql .=
" GROUP BY year";
263 $sql .= $this->db->order(
'year',
'DESC');
279 $sql =
"SELECT product.ref, COUNT(product.ref) as nb, SUM(tl.".$this->field_line.
") as total, AVG(tl.".$this->field_line.
") as avg";
280 $sql .=
" FROM ".$this->from.
", ".$this->from_line.
", ".MAIN_DB_PREFIX.
"product as product";
281 if (!$user->hasRight(
'societe',
'client',
'voir')) {
282 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
285 $sql .=
" WHERE ".$this->where;
286 $sql .=
" AND f.rowid = tl.fk_facture AND tl.fk_product = product.rowid";
288 $sql .=
" GROUP BY product.ref";
289 $sql .= $this->db->order(
'nb',
'DESC');
305 $endYear = (int) date(
'Y');
306 $startYear = $endYear - $numberYears;
307 $sql =
"SELECT date_format(datef,'%Y') as dm, SUM(f.".$this->field.
")";
308 $sql .=
" FROM ".$this->from;
309 if (!$user->hasRight(
'societe',
'client',
'voir')) {
310 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
314 $sql .=
" AND ".$this->where;
315 $sql .=
" GROUP BY dm";
316 $sql .= $this->db->order(
'dm',
'ASC');
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Class to manage Dolibarr database access.
Class to manage suppliers invoices.
Class to manage invoices.
Class to manage stats for invoices (customer and supplier)
getAverageByMonth($year)
Return average amount.
getNbByMonth($year, $format=0)
Return orders number by month for a year.
getAmountByMonth($year, $format=0)
Return the invoices amount by month for a year.
__construct(DoliDB $db, $socid, $mode, $userid=0, $typentid=0, $categid=0)
Constructor.
getAllByProduct($year, $limit=10)
Return nb, amount of predefined product for year.
getNbByYear()
Return invoices number per year.
getAmountByYear($numberYears, $format=0)
Return the invoices amount by year for a number of past years.
getAllByYear()
Return nb, total and average.
Parent class of statistics class.
_getAmountByYear($sql)
Returns the summed amounts per year for a given number of past years ending now.
_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)
Renvoie le nombre de documents par mois pour une annee donnee Return number of documents per month fo...
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.