Passed
Push — GENERAL_BUG_REVIEW_240911 ( 3362b2...8cbbee )
by Rafael
49:13
created

DeplacementStats::getAmountByMonth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2003      Rodolphe Quiedeville <[email protected]>
4
 * Copyright (c) 2005-2008 Laurent Destailleur  <[email protected]>
5
 * Copyright (C) 2005-2009 Regis Houssin        <[email protected]>
6
 * Copyright (C) 2024       Rafael San José             <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Dolibarr\Code\Compta\Classes;
23
24
/**
25
 *       \file       htdocs/compta/deplacement/class/deplacementstats.class.php
26
 *       \ingroup    invoices
27
 *       \brief      File for class managaging the statistics of travel and expense notes
28
 */
29
30
include_once DOL_DOCUMENT_ROOT . '/compta/deplacement/class/deplacement.class.php';
31
32
/**
33
 *  Class to manage the statistics of travel and expense notes
34
 */
35
class DeplacementStats extends Stats
36
{
37
    /**
38
     * @var string Name of table without prefix where object is stored
39
     */
40
    public $table_element;
41
42
    public $socid;
43
    public $userid;
44
45
    public $from;
46
    public $field;
47
    public $where;
48
49
    /**
50
     * Constructor
51
     *
52
     * @param   DoliDB      $db        Database handler
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Compta\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
53
     * @param   int         $socid     Id third party
54
     * @param   mixed       $userid    Id user for filter or array of user ids
55
     * @return  void
56
     */
57
    public function __construct($db, $socid = 0, $userid = 0)
58
    {
59
        global $conf;
60
61
        $this->db = $db;
62
        $this->socid = $socid;
63
        $this->userid = $userid;
64
65
        $object = new Deplacement($this->db);
66
        $this->from = MAIN_DB_PREFIX . $object->table_element;
67
        $this->field = 'km';
68
69
        $this->where = " fk_statut > 0";
70
        $this->where .= " AND entity = " . $conf->entity;
71
        if ($this->socid > 0) {
72
            $this->where .= " AND fk_soc = " . ((int) $this->socid);
73
        }
74
        if (is_array($this->userid) && count($this->userid) > 0) {
75
            $this->where .= ' AND fk_user IN (' . $this->db->sanitize(implode(',', $this->userid)) . ')';
76
        } elseif ($this->userid > 0) {
77
            $this->where .= ' AND fk_user = ' . ((int) $this->userid);
78
        }
79
    }
80
81
82
    /**
83
     *  Renvoie le nombre de facture par annee
84
     *
85
     *  @return     array   Array of values
86
     */
87
    public function getNbByYear()
88
    {
89
        $sql = "SELECT YEAR(dated) as dm, count(*)";
90
        $sql .= " FROM " . $this->from;
91
        $sql .= " GROUP BY dm DESC";
92
        $sql .= " WHERE " . $this->where;
93
94
        return $this->_getNbByYear($sql);
95
    }
96
97
98
    /**
99
     *  Renvoie le nombre de facture par mois pour une annee donnee
100
     *
101
     *  @param  int     $year       Year to scan
102
     *  @param  int     $format     0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
103
     *  @return array               Array of values
104
     */
105
    public function getNbByMonth($year, $format = 0)
106
    {
107
        $sql = "SELECT MONTH(dated) as dm, count(*)";
108
        $sql .= " FROM " . $this->from;
109
        $sql .= " WHERE YEAR(dated) = " . ((int) $year);
110
        $sql .= " AND " . $this->where;
111
        $sql .= " GROUP BY dm";
112
        $sql .= $this->db->order('dm', 'DESC');
113
114
        $res = $this->_getNbByMonth($year, $sql, $format);
115
        //var_dump($res);print '<br>';
116
        return $res;
117
    }
118
119
120
    /**
121
     *  Renvoie le montant de facture par mois pour une annee donnee
122
     *
123
     *  @param  int     $year       Year to scan
124
     *  @param  int     $format     0=Label of abscissa is a translated text, 1=Label of abscissa is month number, 2=Label of abscissa is first letter of month
125
     *  @return array               Array of values
126
     */
127
    public function getAmountByMonth($year, $format = 0)
128
    {
129
        $sql = "SELECT date_format(dated,'%m') as dm, sum(" . $this->field . ")";
130
        $sql .= " FROM " . $this->from;
131
        $sql .= " WHERE date_format(dated,'%Y') = '" . $this->db->escape($year) . "'";
132
        $sql .= " AND " . $this->where;
133
        $sql .= " GROUP BY dm";
134
        $sql .= $this->db->order('dm', 'DESC');
135
136
        $res = $this->_getAmountByMonth($year, $sql, $format);
137
        //var_dump($res);print '<br>';
138
        return $res;
139
    }
140
141
    /**
142
     *  Return average amount
143
     *
144
     *  @param  int     $year       Year to scan
145
     *  @return array               Array of values
146
     */
147
    public function getAverageByMonth($year)
148
    {
149
        $sql = "SELECT date_format(dated,'%m') as dm, avg(" . $this->field . ")";
150
        $sql .= " FROM " . $this->from;
151
        $sql .= " WHERE date_format(dated,'%Y') = '" . $this->db->escape($year) . "'";
152
        $sql .= " AND " . $this->where;
153
        $sql .= " GROUP BY dm";
154
        $sql .= $this->db->order('dm', 'DESC');
155
156
        return $this->_getAverageByMonth($year, $sql);
157
    }
158
159
    /**
160
     *  Return nb, total and average
161
     *
162
     *  @return array               Array of values
163
     */
164
    public function getAllByYear()
165
    {
166
        $sql = "SELECT date_format(dated,'%Y') as year, count(*) as nb, sum(" . $this->field . ") as total, avg(" . $this->field . ") as avg";
167
        $sql .= " FROM " . $this->from;
168
        $sql .= " WHERE " . $this->where;
169
        $sql .= " GROUP BY year";
170
        $sql .= $this->db->order('year', 'DESC');
171
172
        return $this->_getAllByYear($sql);
173
    }
174
}
175