MonthlyBillableQueryHandler   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 84
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
B __invoke() 0 54 5
1
<?php
2
require_once __DIR__ . '/../class/billing/monthly/MonthBillCollection.php';
3
require_once __DIR__ . '/../class/billing/monthly/MonthlyFlightBill.php';
4
require_once __DIR__ . '/../class/billing/monthly/MoneyReceiver.php';
5
require_once __DIR__ . '/../class/bbcvols.class.php';
6
require_once __DIR__ . '/MonthlyBillableQuery.php';
7
8
/**
9
 * @author Laurent De Coninck <[email protected]>
10
 */
11
class MonthlyBillableQueryHandler
12
{
13
14
    /**
15
     * @var DoliDB
16
     */
17
    private $db;
18
19
    /**
20
     * @var stdClass
21
     */
22
    private $config;
23
24
    /**
25
     * @param DoliDB   $db
26
     * @param stdClass $config
27
     */
28
    public function __construct($db, $config)
29
    {
30
        $this->db = $db;
31
        $this->config = $config;
32
    }
33
34
    /**
35
     * @param MonthlyBillableQuery $query
36
     *
37
     * @return MonthBillCollection
38
     */
39
    public function __invoke(MonthlyBillableQuery $query)
40
    {
41
        $sql = 'SELECT flights.*, usr.rowid as usrRowId, usr.lastname as lastname,  usr.firstname as firstname FROM llx_bbc_vols as flights INNER JOIN llx_user as usr ON flights.fk_receiver = usr.rowid';
42
        $sql .= ' WHERE ';
43
        $sql .= ' flights.fk_type = 2 ';
44
        $sql .= ' AND flights.is_facture = 0 ';
45
        $sql .= ' AND (flights.cost IS NOT NULL AND flights.cost > 0) ';
46
        $sql .= sprintf(' AND YEAR(flights.date) = %s ', $query->getYear());
47
        $sql .= sprintf(' AND MONTH(flights.date) = %s', $query->getMonth());
48
49
        $resql = $this->db->query($sql);
50
        $array = new MonthBillCollection();
51
52
        if ($resql) {
53
            $num = $this->db->num_rows($resql);
54
            $i = 0;
55
            if ($num) {
56
                while ($i < $num) {
57
                    $obj = $this->db->fetch_object($resql);
58
                    if ($obj) {
59
60
                        $flight = new Bbcvols($this->db);
61
                        $flight->id = $obj->idBBC_vols;
62
                        $flight->idBBC_vols = $obj->idBBC_vols;
63
                        $flight->date = $this->db->jdate($obj->date);
64
                        $flight->lieuD = $obj->lieuD;
65
                        $flight->lieuA = $obj->lieuA;
66
                        $flight->heureD = $obj->heureD;
67
                        $flight->heureA = $obj->heureA;
68
                        $flight->BBC_ballons_idBBC_ballons = $obj->BBC_ballons_idBBC_ballons;
69
                        $flight->nbrPax = $obj->nbrPax;
70
                        $flight->remarque = $obj->remarque;
71
                        $flight->incidents = $obj->incidents;
72
                        $flight->fk_type = $obj->fk_type;
73
                        $flight->fk_pilot = $obj->fk_pilot;
74
                        $flight->fk_organisateur = $obj->fk_organisateur;
75
                        $flight->is_facture = $obj->is_facture;
76
                        $flight->kilometers = $obj->kilometers;
77
                        $flight->cost = $obj->cost;
78
                        $flight->fk_receiver = $obj->fk_receiver;
79
                        $flight->justif_kilometers = $obj->justif_kilometers;
80
                        $flight->date_creation = $obj->date_creation;
81
                        $flight->date_update = $obj->date_update;
82
83
                        $moneyReceiver = new MoneyReceiver($obj->firstname, $obj->lastname, $obj->usrRowId);
84
                        $array->addFlight($moneyReceiver, $flight);
85
                    }
86
                    $i++;
87
                }
88
            }
89
        }
90
91
        return $array;
92
    }
93
94
}