Completed
Push — feature/link_expense_note ( 3db735...b72b51 )
by Laurent
02:14
created

flightLog.lib.php ➔ findFlightByPilotAndQuarter()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

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