Completed
Push — feature/link_expense_note ( 1fd4be )
by Laurent
01:47
created

flightLog.lib.php ➔ bbcKilometersByQuartil()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
nc 3
nop 1
dl 0
loc 37
rs 9.0168
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
 * @deprecated
156
 *
157
 * @param int $pilotId
158
 * @param int $year
159
 * @param int $quarter
160
 *
161
 * @return array
162
 */
163
function findFlightByPilotAndQuarter($pilotId, $year, $quarter)
164
{
165
    global $db;
166
167
    $sql = generateQuarterQuery($year, $pilotId, $quarter, false);
168
    $flights = [];
169
    $resql = $db->query($sql);
170
    if ($resql) {
171
        $num = $db->num_rows($resql);
172
        $i = 0;
173
        if ($num) {
174
            while ($i < $num) {
175
                $flight = $db->fetch_object($resql);
176
                if ($flight) {
177
                    $flights[] = $flight;
178
                }
179
                $i++;
180
            }
181
        }
182
    }
183
184
185
    return $flights;
186
}
187
188
/**
189
 * @param int $year
190
 *
191
 * @return array
192
 */
193
function bbcKilometersByQuartil($year)
194
{
195
    global $db;
196
197
    $sql = generateQuarterQuery($year);
198
    $resql = $db->query($sql);
199
200
    $kmByQuartil = array();
201
    if ($resql) {
202
        $num = $db->num_rows($resql);
203
        $i = 0;
204
        if ($num) {
205
            while ($i < $num) {
206
                $obj = $db->fetch_object($resql); //vol
207
                if ($obj) {
208
209
                    $rowId = $obj->rowid;
210
                    $name = $obj->lastname;
211
                    $firstname = $obj->firstname;
212
                    $sum = $obj->SUM;
213
                    $quartil = $obj->quartil;
214
215
                    $kmByQuartil[$rowId]["name"] = $name;
216
                    $kmByQuartil[$rowId]["firstname"] = $firstname;
217
218
                    $kmByQuartil[$rowId]["quartil"][$quartil]["km"] = $sum;
219
                    $kmByQuartil[$rowId]["quartil"][$quartil]["flight"] = $obj->nbrFlight;
220
221
222
                }
223
                $i++;
224
            }
225
        }
226
    }
227
228
    return $kmByQuartil;
229
}
230
231
/**
232
 * @param QuarterPilotMissionCollection $kmByQuartil
233
 * @param int                           $tauxRemb
234
 * @param int                           $unitPriceMission
235
 */
236
function printBbcKilometersByQuartil($kmByQuartil, $tauxRemb, $unitPriceMission)
237
{
238
    print '<table class="border" width="100%">';
239
240
    print '<tr>';
241
    print '<td></td>';
242
    print '<td></td>';
243
244
    print '<td class="liste_titre" colspan="5">Trimestre 1 (Jan - Mars)</td>';
245
    print '<td class="liste_titre" colspan="5">Trimestre 2 (Avr - Juin)</td>';
246
    print '<td class="liste_titre" colspan="5">Trimestre 3 (Juil - Sept)</td>';
247
    print '<td class="liste_titre" colspan="5">Trimestre 4 (Oct - Dec)</td>';
248
    print '<td class="liste_titre" >Total</td>';
249
250
    print '</tr>';
251
252
    print '<tr class="liste_titre">';
253
    print '<td class="liste_titre" > Nom </td>';
254
    print '<td class="liste_titre" > Prenom </td>';
255
256
257
    print '<td class="liste_titre" > # T1 & T2</td>';
258
    print '<td class="liste_titre" > Forfaits pil </td>';
259
    print '<td class="liste_titre" > Total des KM </td>';
260
    print '<td class="liste_titre" > Remb km €</td>';
261
    print '<td class="liste_titre" > Total € </td>';
262
263
    print '<td class="liste_titre" > # T1 & T2</td>';
264
    print '<td class="liste_titre" > Forfaits pil </td>';
265
    print '<td class="liste_titre" > Total des KM </td>';
266
    print '<td class="liste_titre" > Remb km €</td>';
267
    print '<td class="liste_titre" > Total € </td>';
268
269
    print '<td class="liste_titre" > # T1 & T2</td>';
270
    print '<td class="liste_titre" > Forfaits pil </td>';
271
    print '<td class="liste_titre" > Total des KM </td>';
272
    print '<td class="liste_titre" > Remb km €</td>';
273
    print '<td class="liste_titre" > Total € </td>';
274
275
    print '<td class="liste_titre" > # T1 & T2</td>';
276
    print '<td class="liste_titre" > Forfaits pil </td>';
277
    print '<td class="liste_titre" > Total des KM </td>';
278
    print '<td class="liste_titre" > Remb km €</td>';
279
    print '<td class="liste_titre" > Total € </td>';
280
281
    print '<td class="liste_titre" > Total € </td>';
282
    print '</tr>';
283
284
    $totalQ1 = 0;
285
    $totalQ2 = 0;
286
    $totalQ3 = 0;
287
    $totalQ4 = 0;
288
289
    $curMonth = date("m", time());
290
    $curQuarter = ceil($curMonth / 3);
291
    $disableColor = 'style="background-color: lightyellow;" title="N/A" data-toggle="tooltip"';
292
293
    /** @var PilotMissions $pilotMission */
294
    foreach ($kmByQuartil as $pilotMission) {
295
        $sumQ1 = $pilotMission->getTotalOfKilometersForQuarter(1);
296
        $sumQ2 = $pilotMission->getTotalOfKilometersForQuarter(2);
297
        $sumQ3 = $pilotMission->getTotalOfKilometersForQuarter(3);
298
        $sumQ4 = $pilotMission->getTotalOfKilometersForQuarter(4);
299
300
        $flightsQ1 = $pilotMission->getNumberOfFlightsForQuarter(1);
301
        $flightsQ2 = $pilotMission->getNumberOfFlightsForQuarter(2);
302
        $flightsQ3 = $pilotMission->getNumberOfFlightsForQuarter(3);
303
        $flightsQ4 = $pilotMission->getNumberOfFlightsForQuarter(4);
304
305
        $amoutQ1 = ($sumQ1 * $tauxRemb) + ($flightsQ1 * $unitPriceMission);
306
        $amoutQ2 = ($sumQ2 * $tauxRemb) + ($flightsQ2 * $unitPriceMission);
307
        $amoutQ3 = ($sumQ3 * $tauxRemb) + ($flightsQ3 * $unitPriceMission);
308
        $amoutQ4 = ($sumQ4 * $tauxRemb) + ($flightsQ4 * $unitPriceMission);
309
310
        $totalQ1 += $amoutQ1;
311
        $totalQ2 += $amoutQ2;
312
        $totalQ3 += $amoutQ3;
313
        $totalQ4 += $amoutQ4;
314
315
        $sumKm = ($sumQ1 + $sumQ2 + $sumQ3 + $sumQ4);
316
        $sumFlights = ($flightsQ1 + $flightsQ2 + $flightsQ3 + $flightsQ4);
317
318
        print '<tr>';
319
320
        print '<td>' . $pilotMission->getPilotLastname() . '</td>';
321
        print '<td>' . $pilotMission->getPilotFirstname() . '</td>';
322
323
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '>' . ($flightsQ1) . '</td>';
324
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '>' . ($flightsQ1 * $unitPriceMission) . '€</td>';
325
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '>' . $sumQ1 . '</td>';
326
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '>' . ($sumQ1 * $tauxRemb) . '</td>';
327
        print '<td' . ($curQuarter < 1 ? $disableColor : '') . '><b>' . $amoutQ1 . '€</b></td>';
328
329
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '>' . ($flightsQ2) . '</td>';
330
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '>' . ($flightsQ2 * $unitPriceMission) . '€</td>';
331
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '>' . $sumQ2 . '</td>';
332
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '>' . ($sumQ2 * $tauxRemb) . '</td>';
333
        print '<td ' . ($curQuarter < 2 ? $disableColor : '') . '><b>' . $amoutQ2 . '€</b></td>';
334
335
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '>' . ($flightsQ3) . '</td>';
336
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '>' . ($flightsQ3 * $unitPriceMission) . '€</td>';
337
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '>' . $sumQ3 . '</td>';
338
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '>' . ($sumQ3 * $tauxRemb) . '</td>';
339
        print '<td ' . ($curQuarter < 3 ? $disableColor : '') . '><b>' . $amoutQ3 . '€</b></td>';
340
341
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '>' . ($flightsQ4) . '</td>';
342
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '>' . ($flightsQ4 * $unitPriceMission) . '€</td>';
343
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '>' . $sumQ4 . '</td>';
344
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '>' . ($sumQ4 * $tauxRemb) . '</td>';
345
        print '<td ' . ($curQuarter < 4 ? $disableColor : '') . '><b>' . $amoutQ4 . '€</b></td>';
346
347
        print '<td>' . (($sumFlights * $unitPriceMission) + ($sumKm * $tauxRemb)) . '€</td>';
348
349
        print '</tr>';
350
    }
351
352
    print "<td colspan='6'></td>";
353
    print "<td>" . price($totalQ1) . "€</td>";
354
    print "<td colspan='4'></td>";
355
    print "<td>" . price($totalQ2) . "€</td>";
356
    print "<td colspan='4'></td>";
357
    print "<td>" . price($totalQ3) . "€</td>";
358
    print "<td colspan='4'></td>";
359
    print "<td>" . price($totalQ4) . "€</td>";
360
    print "<td></td>";
361
362
    print '</table>';
363
}
364
365
/**
366
 * @return int[]
367
 */
368
function getFlightYears()
369
{
370
    global $db;
371
372
    $results = [];
373
374
    $sqlYear = "SELECT DISTINCT(YEAR(llx_bbc_vols.date)) as annee FROM llx_bbc_vols ";
375
    $resql_years = $db->query($sqlYear);
376
377
    $num = $db->num_rows($resql_years);
378
    $i = 0;
379
    if ($num) {
380
        while ($i < $num) {
381
            $obj = $db->fetch_object($resql_years);
382
383
            if ($obj->annee) {
384
                $results[] = $obj->annee;
385
            }
386
387
            $i++;
388
        }
389
    }
390
391
    return $results;
392
}
393
394
/**
395
 * @param GraphicalData $graphData
396
 *
397
 * @return GraphicalData
398
 */
399
function fetchGraphByTypeAndYearData(GraphicalData $graphData)
400
{
401
    global $db;
402
403
    $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";
404
    $resql = $db->query($sql);
405
406
    $num = $db->num_rows($resql);
407
    $i = 0;
408
    if ($num) {
409
        while ($i < $num) {
410
            $obj = $db->fetch_object($resql);
411
412
            if ($obj->year) {
413
                $graphData->addValue($obj->year, new GraphicalValue($obj->val, $obj->year, $obj->type));
414
            }
415
416
            $i++;
417
        }
418
    }
419
420
    return $graphData;
421
}
422
423
/**
424
 * @return GraphicalData
425
 */
426
function getGraphByTypeAndYearData()
427
{
428
429
    $flightTypes = fetchBbcFlightTypes();
430
431
    $graphData = new GraphicalData();
432
433
    foreach (getFlightYears() as $flightYear) {
434
        $pieceData = new YearGraphicalData($flightYear);
435
436
        foreach ($flightTypes as $flightType) {
437
            $pieceData->addType(new GraphicalType($flightType->id, $flightType->nom));
438
        }
439
440
        $graphData->addData($pieceData);
441
    }
442
443
    return fetchGraphByTypeAndYearData($graphData);
444
}