flightLog.lib.php ➔ printBbcKilometersByQuartil()   F
last analyzed

Complexity

Conditions 22
Paths > 20000

Size

Total Lines 128

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
nc 1048577
nop 3
dl 0
loc 128
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/**
4
 * @param int $active
5
 *
6
 * @return BbctypesLine[]
7
 */
8
function fetchBbcFlightTypes($active = 1)
9
{
10
    global $db;
11
12
    $bbcTypes = new Bbctypes($db);
13
14
    $bbcTypes->fetchAll('', '', 0, 0, [
15
        "active" => $active
16
    ]);
17
18
    return $bbcTypes->lines;
19
}
20
21
/**
22
 * @deprecated should use the form instead.
23
 *
24
 * Return list of flight type
25
 *
26
 * @param   mixed $selected  Preselected type
27
 * @param   mixed $htmlname  Name of field in form
28
 * @param   mixed $showempty Add an empty field
29
 */
30
function select_flight_type($selected = '1', $htmlname = 'type', $showempty = false)
31
{
32
33
    global $langs;
34
    $langs->load("trips");
35
36
    $types = fetchBbcFlightTypes();
37
38
    print '<select class="flat js-flight-type" name="' . $htmlname . '">';
39
40
    if ($showempty) {
41
        print sprintf('<option selected="%s" value=""></option>',
42
            (($selected == "" || $selected == 0 || $selected == -1) ? "selected" : ""));
43
    }
44
45
    foreach ($types as $flightType) {
46
        print '<option value="' . $flightType->id . '"';
47
        if ($flightType->numero == $selected) {
48
            print ' selected="selected"';
49
        }
50
        print '>';
51
        echo "T" . $flightType->numero . '-' . $flightType->nom;
52
        print "</option>";
53
    }
54
55
    print '</select>';
56
}
57
58
/**
59
 * @param string $selected
60
 * @param string $htmlname
61
 * @param int    $showimmat
62
 * @param int    $showDeclasse
63
 */
64
function select_balloons($selected = '', $htmlname = 'ballon', $showimmat = 0, $showDeclasse = 1)
65
{
66
67
    global $db, $langs;
68
69
    $langs->load("trips");
70
    print '<!-- select_balloons in form class -->';
71
    print '<select class="flat" name="' . $htmlname . '">';
72
73
    print '<option value=""';
74
    if ($selected == -1 || $selected == '' || $selected == 0) {
75
        print ' selected="selected"';
76
    }
77
    print '>&nbsp;</option>';
78
79
    if (!$showDeclasse) {
80
        $resql = $db->query("SELECT B.immat,B.rowid FROM llx_bbc_ballons as B WHERE is_disable = false  ORDER BY B.immat");
81
    } else {
82
        $resql = $db->query("SELECT B.immat,B.rowid FROM llx_bbc_ballons as B ORDER BY B.immat");
83
    }
84
85
    if ($resql) {
86
        $num = $db->num_rows($resql);
87
        $i = 0;
88
        if ($num) {
89
            while ($i < $num) {
90
                $obj = $db->fetch_object($resql);
91
                if ($obj) {
92
                    if ($showimmat) {
93
                        print '<option value="' . $obj->immat . '"';
94
                    } else {
95
                        print '<option value="' . $obj->rowid . '"';
96
                    }
97
                    if ($obj->rowid == $selected) {
98
                        print ' selected="selected"';
99
                    }
100
                    print '>';
101
                    echo strtoupper($obj->immat);
102
                    print "</option>";
103
                }
104
                $i++;
105
            }
106
        }
107
    }
108
109
    print '</select>';
110
}
111
112
/**
113
 * @param null $year
114
 * @param null $pilotId
115
 * @param null $quarter
116
 * @param bool $groupBy
117
 *
118
 * @return string
119
 */
120
function generateQuarterQuery($year = null, $pilotId = null, $quarter = null, $groupBy = true)
121
{
122
123
    global $db;
124
125
    $sql = "SELECT USR.rowid, USR.lastname, USR.firstname, QUARTER(VOL.date) as quartil ";
126
127
    if ($groupBy) {
128
        $sql .= " , SUM(VOL.kilometers) as SUM";
129
        $sql .= " , COUNT(VOL.idBBC_vols) as nbrFlight";
130
    } else {
131
        $sql .= " , VOL.*";
132
    }
133
134
    $sql .= " FROM llx_bbc_vols as VOL";
135
    $sql .= " LEFT OUTER JOIN llx_user AS USR ON VOL.fk_pilot = USR.rowid";
136
    $sql .= " WHERE ";
137
    $sql .= " YEAR(VOL.date) = " . ($year ?: 'YEAR(NOW())');
138
    $sql .= " AND ( VOL.fk_type = 1 OR VOL.fk_type = 2 ) ";
139
140
    if ($pilotId !== null) {
141
        $sql .= " AND USR.rowid = " . $pilotId;
142
    }
143
144
    if ($quarter !== null) {
145
        $sql .= " AND QUARTER(VOL.date) = " . $quarter;
146
    }
147
148
    if ($groupBy) {
149
        $sql .= " GROUP BY QUARTER(VOL.date), VOL.fk_pilot";
150
    }
151
    $sql .= " ORDER BY QUARTER(VOL.date), VOL.fk_pilot";
152
153
    return $db->escape($sql);
154
}
155
156
/**
157
 * @param int $year
158
 *
159
 * @return array
160
 */
161
function bbcKilometersByQuartil($year)
162
{
163
    global $db;
164
165
    $sql = generateQuarterQuery($year);
166
    $resql = $db->query($sql);
167
168
    $kmByQuartil = array();
169
    if ($resql) {
170
        $num = $db->num_rows($resql);
171
        $i = 0;
172
        if ($num) {
173
            while ($i < $num) {
174
                $obj = $db->fetch_object($resql); //vol
175
                if ($obj) {
176
177
                    $rowId = $obj->rowid;
178
                    $name = $obj->lastname;
179
                    $firstname = $obj->firstname;
180
                    $sum = $obj->SUM;
181
                    $quartil = $obj->quartil;
182
183
                    $kmByQuartil[$rowId]["name"] = $name;
184
                    $kmByQuartil[$rowId]["firstname"] = $firstname;
185
186
                    $kmByQuartil[$rowId]["quartil"][$quartil]["km"] = $sum;
187
                    $kmByQuartil[$rowId]["quartil"][$quartil]["flight"] = $obj->nbrFlight;
188
189
190
                }
191
                $i++;
192
            }
193
        }
194
    }
195
196
    return $kmByQuartil;
197
}
198
199
/**
200
 * @param QuarterPilotMissionCollection $kmByQuartil
201
 * @param int                           $tauxRemb
202
 * @param int                           $unitPriceMission
203
 */
204
function printBbcKilometersByQuartil($kmByQuartil, $tauxRemb, $unitPriceMission)
205
{
206
    print '<table class="border" width="100%">';
207
208
    print '<tr>';
209
    print '<td></td>';
210
    print '<td></td>';
211
212
    print '<td class="liste_titre" colspan="5">Trimestre 1 (Jan - Mars)</td>';
213
    print '<td class="liste_titre" colspan="5">Trimestre 2 (Avr - Juin)</td>';
214
    print '<td class="liste_titre" colspan="5">Trimestre 3 (Juil - Sept)</td>';
215
    print '<td class="liste_titre" colspan="5">Trimestre 4 (Oct - Dec)</td>';
216
    print '<td class="liste_titre" >Total</td>';
217
218
    print '</tr>';
219
220
    print '<tr class="liste_titre">';
221
    print '<td class="liste_titre" > Nom </td>';
222
    print '<td class="liste_titre" > Prenom </td>';
223
224
225
    print '<td class="liste_titre" > # T1 & T2</td>';
226
    print '<td class="liste_titre" > Forfaits pil </td>';
227
    print '<td class="liste_titre" > Total des KM </td>';
228
    print '<td class="liste_titre" > Remb km €</td>';
229
    print '<td class="liste_titre" > Total € </td>';
230
231
    print '<td class="liste_titre" > # T1 & T2</td>';
232
    print '<td class="liste_titre" > Forfaits pil </td>';
233
    print '<td class="liste_titre" > Total des KM </td>';
234
    print '<td class="liste_titre" > Remb km €</td>';
235
    print '<td class="liste_titre" > Total € </td>';
236
237
    print '<td class="liste_titre" > # T1 & T2</td>';
238
    print '<td class="liste_titre" > Forfaits pil </td>';
239
    print '<td class="liste_titre" > Total des KM </td>';
240
    print '<td class="liste_titre" > Remb km €</td>';
241
    print '<td class="liste_titre" > Total € </td>';
242
243
    print '<td class="liste_titre" > # T1 & T2</td>';
244
    print '<td class="liste_titre" > Forfaits pil </td>';
245
    print '<td class="liste_titre" > Total des KM </td>';
246
    print '<td class="liste_titre" > Remb km €</td>';
247
    print '<td class="liste_titre" > Total € </td>';
248
249
    print '<td class="liste_titre" > Total € </td>';
250
    print '</tr>';
251
252
    $totalQ1 = 0;
253
    $totalQ2 = 0;
254
    $totalQ3 = 0;
255
    $totalQ4 = 0;
256
257
    $curMonth = date("m", time());
258
    $curQuarter = ceil($curMonth / 3);
259
    $disableColor = 'style="background-color: lightyellow;" title="N/A" data-toggle="tooltip"';
260
261
    /** @var PilotMissions $pilotMission */
262
    foreach ($kmByQuartil as $pilotMission) {
263
        $sumQ1 = $pilotMission->getTotalOfKilometersForQuarter(1);
264
        $sumQ2 = $pilotMission->getTotalOfKilometersForQuarter(2);
265
        $sumQ3 = $pilotMission->getTotalOfKilometersForQuarter(3);
266
        $sumQ4 = $pilotMission->getTotalOfKilometersForQuarter(4);
267
268
        $flightsQ1 = $pilotMission->getNumberOfFlightsForQuarter(1);
269
        $flightsQ2 = $pilotMission->getNumberOfFlightsForQuarter(2);
270
        $flightsQ3 = $pilotMission->getNumberOfFlightsForQuarter(3);
271
        $flightsQ4 = $pilotMission->getNumberOfFlightsForQuarter(4);
272
273
        $amoutQ1 = ($sumQ1 * $tauxRemb) + ($flightsQ1 * $unitPriceMission);
274
        $amoutQ2 = ($sumQ2 * $tauxRemb) + ($flightsQ2 * $unitPriceMission);
275
        $amoutQ3 = ($sumQ3 * $tauxRemb) + ($flightsQ3 * $unitPriceMission);
276
        $amoutQ4 = ($sumQ4 * $tauxRemb) + ($flightsQ4 * $unitPriceMission);
277
278
        $totalQ1 += $amoutQ1;
279
        $totalQ2 += $amoutQ2;
280
        $totalQ3 += $amoutQ3;
281
        $totalQ4 += $amoutQ4;
282
283
        $sumKm = ($sumQ1 + $sumQ2 + $sumQ3 + $sumQ4);
284
        $sumFlights = ($flightsQ1 + $flightsQ2 + $flightsQ3 + $flightsQ4);
285
286
        print '<tr>';
287
288
        print '<td>' . $pilotMission->getPilotLastname() . '</td>';
289
        print '<td>' . $pilotMission->getPilotFirstname() . '</td>';
290
291
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '>' . ($flightsQ1) . '</td>';
292
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '>' . ($flightsQ1 * $unitPriceMission) . '€</td>';
293
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '>' . $sumQ1 . '</td>';
294
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '>' . ($sumQ1 * $tauxRemb) . '</td>';
295
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '><b>' . $amoutQ1 . '€</b></td>';
296
297
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '>' . ($flightsQ2) . '</td>';
298
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '>' . ($flightsQ2 * $unitPriceMission) . '€</td>';
299
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '>' . $sumQ2 . '</td>';
300
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '>' . ($sumQ2 * $tauxRemb) . '</td>';
301
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '><b>' . $amoutQ2 . '€</b></td>';
302
303
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '>' . ($flightsQ3) . '</td>';
304
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '>' . ($flightsQ3 * $unitPriceMission) . '€</td>';
305
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '>' . $sumQ3 . '</td>';
306
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '>' . ($sumQ3 * $tauxRemb) . '</td>';
307
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '><b>' . $amoutQ3 . '€</b></td>';
308
309
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '>' . ($flightsQ4) . '</td>';
310
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '>' . ($flightsQ4 * $unitPriceMission) . '€</td>';
311
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '>' . $sumQ4 . '</td>';
312
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '>' . ($sumQ4 * $tauxRemb) . '</td>';
313
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '><b>' . $amoutQ4 . '€</b></td>';
314
315
        print '<td>' . (($sumFlights * $unitPriceMission) + ($sumKm * $tauxRemb)) . '€</td>';
316
317
        print '</tr>';
318
    }
319
320
    print "<td colspan='6'></td>";
321
    print "<td>" . price($totalQ1) . "€</td>";
322
    print "<td colspan='4'></td>";
323
    print "<td>" . price($totalQ2) . "€</td>";
324
    print "<td colspan='4'></td>";
325
    print "<td>" . price($totalQ3) . "€</td>";
326
    print "<td colspan='4'></td>";
327
    print "<td>" . price($totalQ4) . "€</td>";
328
    print "<td></td>";
329
330
    print '</table>';
331
}
332
333
/**
334
 * @return int[]
335
 */
336
function getFlightYears()
337
{
338
    global $db;
339
340
    $results = [];
341
342
    $sqlYear = "SELECT DISTINCT(YEAR(llx_bbc_vols.date)) as annee FROM llx_bbc_vols ";
343
    $resql_years = $db->query($sqlYear);
344
345
    $num = $db->num_rows($resql_years);
346
    $i = 0;
347
    if ($num) {
348
        while ($i < $num) {
349
            $obj = $db->fetch_object($resql_years);
350
351
            if ($obj->annee) {
352
                $results[] = $obj->annee;
353
            }
354
355
            $i++;
356
        }
357
    }
358
359
    return $results;
360
}
361
362
/**
363
 * @param GraphicalData $graphData
364
 *
365
 * @return GraphicalData
366
 */
367
function fetchGraphByTypeAndYearData(GraphicalData $graphData)
368
{
369
    global $db;
370
371
    $sql = "SELECT YEAR(date) as year, fk_type as type,COUNT(idBBC_vols) as val FROM llx_bbc_vols GROUP BY YEAR(date), fk_type ORDER BY year,fk_type";
372
    $resql = $db->query($sql);
373
374
    $num = $db->num_rows($resql);
375
    $i = 0;
376
    if ($num) {
377
        while ($i < $num) {
378
            $obj = $db->fetch_object($resql);
379
380
            if ($obj->year) {
381
                $graphData->addValue($obj->year, new GraphicalValue($obj->val, $obj->year, $obj->type));
382
            }
383
384
            $i++;
385
        }
386
    }
387
388
    return $graphData;
389
}
390
391
/**
392
 * @return GraphicalData
393
 */
394
function getGraphByTypeAndYearData()
395
{
396
397
    $flightTypes = fetchBbcFlightTypes();
398
399
    $graphData = new GraphicalData();
400
401
    foreach (getFlightYears() as $flightYear) {
402
        $pieceData = new YearGraphicalData($flightYear);
403
404
        foreach ($flightTypes as $flightType) {
405
            $pieceData->addType(new GraphicalType($flightType->id, $flightType->nom));
406
        }
407
408
        $graphData->addData($pieceData);
409
    }
410
411
    return fetchGraphByTypeAndYearData($graphData);
412
}