Completed
Push — feature/refactor_ui ( 5b72cd...525b74 )
by Laurent
01:37
created

addFlight.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
// Load Dolibarr environment
4
if (false === (@include '../main.inc.php')) {  // From htdocs directory
5
    require '../../documents/custom/main.inc.php'; // From "custom" directory
6
}
7
8
global $db, $langs, $user, $conf;
9
10
dol_include_once('/commande/class/commande.class.php');
11
dol_include_once('/flightlog/class/bbcvols.class.php');
12
dol_include_once('/flightlog/class/bbctypes.class.php');
13
dol_include_once("/flightlog/lib/flightLog.lib.php");
14
dol_include_once("/flightlog/flightlog.inc.php");
15
16
17
// Load translation files required by the page
18
$langs->load("mymodule@flightlog");
19
20
$validator = new FlightValidator($langs, $db, $conf->global->BBC_FLIGHT_TYPE_CUSTOMER);
21
$createFlightHandler = new CreateFlightCommandHandler($db, $conf, $user, $langs, $validator);
22
23
if (!$user->rights->flightlog->vol->add) {
24
    accessforbidden();
25
}
26
27
/* * *****************************************************************
28
 * ACTIONS
29
 *
30
 * Put here all code to do according to value of "action" parameter
31
 * ****************************************************************** */
32
$msg = '';
33
if (GETPOST("action") == 'add') {
34
    if (!$_POST["cancel"]) {
35
        $isGroupedFlight = (int) GETPOST('grouped_flight', 'int', 2) === 1;
36
        $orderIds = GETPOST('order_id', 'array', 2);
37
        $orderPassengersCount = GETPOST('order_passengers_count', 'array', 2);
38
39
        try {
40
            $volCommand = new CreateFlightCommand();
41
42
            $volCommand
43
                ->setDate(new DateTimeImmutable($_POST['flight_date']))
44
                ->setLieuD($_POST['lieuD'])
45
                ->setLieuA($_POST['lieuA'])
46
                ->setHeureD(new DateTimeImmutable($_POST['flight_date'].' '.$_POST['heureD']))
47
                ->setHeureA(new DateTimeImmutable($_POST['flight_date'].' '.$_POST['heureA']))
48
                ->setBBCBallonsIdBBCBallons($_POST['ballon'])
49
                ->setNbrPax($_POST['nbrPax'])
50
                ->setRemarque($_POST['comm'])
51
                ->setIncidents($_POST['inci'])
52
                ->setFkType($_POST['type'])
53
                ->setFkPilot($_POST['pilot'])
54
                ->setFkOrganisateur($_POST['orga'])
55
                ->setKilometers($_POST['kilometers'])
56
                ->setCost($_POST['cost'])
57
                ->setFkReceiver($_POST['fk_receiver'])
58
                ->setJustifKilometers($_POST['justif_kilometers'])
59
                ->setPassengerNames($_POST['passenger_names'])
60
                ->setGroupedFlight($isGroupedFlight)
61
                ->setOrderIds($orderPassengersCount);
62
63
            $vol = $createFlightHandler->handle($volCommand);
64
65
            include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
66
            $interface = new Interfaces($db);
67
            $triggerResult = $interface->run_triggers('BBC_FLIGHT_LOG_ADD_FLIGHT', $vol, $user, $langs, $conf);
68
69
            $msg = '<div class="ok">L\'ajout du vol du : ' . $_POST["reday"] . '/' . $_POST["remonth"] . '/' . $_POST["reyear"] . ' s\'est correctement effectue ! </div>';
70
            Header("Location: card.php?id=" . $vol->id);
71
        }catch (\Exception $e){
72
            $msg = '<div class="error">Erreur lors de l\'ajout du vol : ' . ($vol->error?:$e->getMessage()) . '! </div>';
73
        }
74
75
    }
76
}
77
78
79
/* * *************************************************
80
 * PAGE
81
 *
82
 * Put here all code to build page
83
 * ************************************************** */
84
85
llxHeader('', 'Carnet de vol', '');
86
87
$html = new Form($db);
88
$commande = new Commande($db);
89
$orders = $commande->liste_array(2);
90
$datec = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
91
if ($msg) {
92
    print $msg;
93
}
94
95
?>
96
97
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300&display=swap" rel="stylesheet">
98
99
<section class="bbc-style">
100
101
102
    <div class="errors error-messages">
103
        <?php
104
        foreach ($validator->getErrors() as $errorMessage) {
105
            print sprintf('<div class="error"><span>%s</span></div>', $errorMessage);
106
        }
107
        ?>
108
    </div>
109
    <form class="flight-form js-form" name='add' action="addFlight.php" method="post">
110
        <input type="hidden" name="action" value="add"/>
111
        <input type="hidden" name="token" value="<?php echo newToken();?>"/>
112
113
        <!-- Date et heures -->
114
        <section class="form-section">
115
            <h1 class="form-section-title"><?php echo $langs->trans('Date & heures'); ?></h1>
116
117
            <div>
118
                <div class="form-group">
119
                    <label class="fieldrequired"> Type du vol</label>
120
121
                    <div class="inline-radio">
122
                        <?php foreach (fetchBbcFlightTypes() as $flightType) : ?>
123
                            <label class="">
124
                                <input type="radio" class="js-flight-type" name="type" value="<?php echo $flightType->id ?>" <?php echo $flightType->numero == $_POST['type'] ? 'checked' : '' ?>>
125
                                <span class="text-bold"><?php echo "T" . $flightType->numero ?></span>
126
                                <span class="font-italic hide-sm"><?php echo $flightType->nom; ?></span>
127
                            </label>
128
                        <?php endforeach; ?>
129
                    </div>
130
131
                </div>
132
133
                <div class="form-group">
134
                    <label class="fieldrequired"> Date du vol</label>
135
                    <input type="date" name="flight_date" value="<?php print (new DateTimeImmutable())->format('Y-m-d')?>"/>
136
                </div>
137
138
                <div class="form-group">
139
                    <label class="fieldrequired">Heure de d&#233;part</label>
140
141
                    <input type="time"
142
                           name="heureD"
143
                           class="flat <?php echo($validator->hasError('heureD') ? 'error' : '') ?>"
144
                           value="<?php echo $_POST['heureD'] ?>"/>
145
146
                </div>
147
148
                <div class="form-group ">
149
                    <label class="fieldrequired">Heure d'arriv&#233;e</label>
150
                    <input type="time"
151
                           name="heureA"
152
                           class="flat <?php echo($validator->hasError('heureA') ? 'error' : '') ?>"
153
                           value="<?php echo $_POST['heureA'] ?>"/>
154
                </div>
155
156
157
            </div>
158
        </section>
159
160
        <!-- Pilote et Ballon -->
161
        <section class="form-section">
162
            <h1 class="form-section-title"><?php echo $langs->trans('Vol') ?></h1>
163
            <div >
164
                <div class="form-group">
165
                    <label class="fieldrequired"> Pilote</label>
166
                    <?php print $html->select_dolusers($_POST["pilot"] ? $_POST["pilot"] : $user->id, 'pilot', 0, null, 0, '', '', 0,0,0,'',0,'','', true); ?>
167
                </div>
168
169
                <div class="form-group ">
170
                    <label class="fieldrequired">
171
                        <span class="js-organisator-field">Organisateur</span>
172
                        <span class="js-instructor-field">Instructeur</span>
173
                    </label>
174
                    <?php
175
                        //organisateur
176
                        print $html->select_dolusers($_POST["orga"] ? $_POST["orga"] : $user->id, 'orga', 0, null, 0, '', '', 0,0,0,'',0,'','', true);
177
                    ?>
178
                </div>
179
180
                <div class="form-group">
181
                    <label class="fieldrequired">Lieu de d&#233;part </label>
182
                    <input type="text" name="lieuD" class="flat" value="<?php print  $_POST['lieuD'] ?>"/>
183
                </div>
184
185
                <div class="form-group ">
186
                    <label class="fieldrequired">Lieu d'arriv&#233;e </label>
187
                    <input type="text" name="lieuA" class="flat" value="<?php print  $_POST['lieuA'] ?>"/>
188
                </div>
189
190
                <div class="form-group">
191
                    <label class="fieldrequired">Ballon</label>
192
                    <?php select_balloons($_POST['ballon'], 'ballon', 0, false, true); ?>
0 ignored issues
show
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
193
                </div>
194
195
                <div class="form-group">
196
                    <label>Il y'avait-il plusieurs ballons ?</label>
197
                    <input type="checkbox" value="1" name="grouped_flight"/> - Oui
198
                </div>
199
            </div>
200
        </section>
201
202
        <!-- Movements -->
203
        <section class="form-section js-expensable-field">
204
            <h1 class="form-section-title"><?php echo $langs->trans('Déplacements') ?></h1>
205
            <div >
206
                <!-- number of kilometers done for the flight -->
207
                <div class="form-group">
208
                    <label class="fieldrequired">Nombre de kilometres effectués pour le vol</label>
209
                    <input type="number" name="kilometers" class="flat <?php echo($validator->hasError('kilometers') ? 'error' : '') ?>" value="<?php echo $_POST['kilometers'] ?>"/>
210
                </div>
211
212
                <!-- Justif Kilometers -->
213
                <div class="form-group">
214
215
                    <label class="fieldrequired">Justificatif des KM </label>
216
                    <textarea name="justif_kilometers" rows="2" cols="60" class="flat <?php echo($validator->hasError('justif_kilometers') ? 'error' : '') ?>"><?php echo $_POST['justif_kilometers'] ?></textarea>
217
                </div>
218
            </div>
219
        </section>
220
221
        <!-- Passagers -->
222
        <section class="form-section">
223
            <h1 class="form-section-title"><?php echo $langs->trans('Passagers') ?></h1>
224
            <div >
225
                <div class="form-group">
226
                    <label class="fieldrequired"><?php echo $langs->trans('Nombre de passagers'); ?></label>
227
                    <input type="number"
228
                           name="nbrPax"
229
                           class="flat <?php echo $validator->hasError('nbrPax') ? 'error' : '' ?>"
230
                           value="<?php echo $_POST['nbrPax']?: 0 ?>"/>
231
                </div>
232
233
                <!-- passenger names -->
234
                <div class="form-group">
235
                    <label class="fieldrequired"><?php echo $langs->trans('Noms des passagers'); ?><br/>(Séparé par des ; )</label>
236
                    <textarea name="passenger_names" cols="60" rows="2" class="flat <?php echo $validator->hasError('passenger_names') ? 'error' : '' ?>"><?php echo $_POST['passenger_names'] ?></textarea>
237
                </div>
238
            </div>
239
        </section>
240
241
        <!-- billing information -->
242
        <section class="form-section js-billable-field">
243
            <h1 class="form-section-title"><?php echo $langs->trans('Facturation') ?></h1>
244
245
            <div>
246
                <p class="text-muted">
247
                    Le bloc sur la facturation permet de savoir où retrouver l'argent du vol. Sur des commandes, au près d'un membre, ... <br/>
248
                    Il est donc normal de devoir réencoder le nombre de passagers.
249
                </p>
250
251
                <!-- Order -->
252
                <div id="list_order" class="js-base-form js-billable-field form-group">
253
                    <!-- BASE form -->
254
                    <table class="bill style-default">
255
                        <!-- Cash -->
256
                        <tr>
257
                            <th colspan="2">Cash</th>
258
                        </tr>
259
260
                        <tr>
261
                            <td class="js-receiver" data-user-id="<?php echo $user->id;?>">
262
                                <label class=""><?php echo $langs->trans('Qui a perçu l\'argent')?>?</label>
263
                                <?php print $html->select_dolusers(
264
                                        $_POST["fk_receiver"] ? $_POST["fk_receiver"] : -1,
265
                                    'fk_receiver', true, null, 0, '', '', 0,0,0,'',0,'','', true); ?>
266
267
                            </td>
268
                            <td>
269
                                <label>&nbsp;</label>
270
                                <div class="input-group">
271
                                    <input type="number" name="cost"  step="1" min="0" class="flat js-cost" value="<?php echo $_POST['cost']?:0 ?>"/>
272
                                    <span class="input-symbol">&euro;</span>
273
                                </div>
274
                            </td>
275
                        </tr>
276
277
                        <!-- Order -->
278
                        <tr>
279
                            <th>Commande(s)</th>
280
                            <th>Nombre de passagers.</th>
281
                        </tr>
282
                        <?php if(is_array($_POST['order_passengers_count']) && !empty($_POST['order_passengers_count'])): ?>
283
                            <?php foreach($_POST['order_passengers_count'] as $order => $orderQuantity): ?>
284
                                <tr class="order-row">
285
                                    <td>
286
                                        <span class="fa fa-trash remove js-remove" data-order-id="<?php echo $order; ?>"></span>
287
                                        <span class="js-order-ref"><?php echo $orders[$order]?></span>
288
                                    </td>
289
                                    <td><input type="number" value="<?php echo $orderQuantity; ?>" min="1" max="5" name="order_passengers_count[<?php echo $order; ?>]" class="js-nbr-pax" /></td>
290
                                </tr>
291
                            <?php endforeach; ?>
292
                        <?php endif; ?>
293
294
                        <tr class="js-order">
295
                            <td>
296
                                <?php
297
                                echo $html::selectarray(
298
                                    'order_id',
299
                                    $orders,
300
                                    $_POST['order_id'],
301
                                    1,
302
                                    0,
303
                                    $validator->hasError('order_id') ? 'error' : '',
304
                                    0,
305
                                    '100%',
306
                                    0,
307
                                    0,
308
                                    '',
309
                                    'js-order-select',
310
                                    true
311
                                );
312
                                ?>
313
                                <span class="text-muted">Sélection de la commande réalisée en totalité (ou en partie)</span>
314
                            </td>
315
316
                            <td >&nbsp;</td>
317
                        </tr>
318
319
                    </table>
320
                </div>
321
            </div>
322
        </section>
323
324
        <!-- Comments -->
325
        <section class="form-section">
326
            <h1 class="form-section-title"><?php echo $langs->trans('Commentaires') ?></h1>
327
            <div>
328
                <!-- commentaires -->
329
                <div class=" form-group">
330
                    <label class="fieldrequired"> Note sur le vol </label>
331
                    <textarea rows="2" cols="60" class="flat" name="comm" placeholder="RAS"><?php print $_POST['comm']; ?></textarea>
332
                </div>
333
334
                <!-- incidents -->
335
                <div class=" form-group">
336
                    <label class="fieldrequired"> Incidents, Brulure, ...</label>
337
                    <textarea rows="2" cols="60" class="flat" name="inci" placeholder="RAS"><?php print $_POST['inci']; ?></textarea>
338
                    <p class="text-muted">Incidents ou dégâts constatés au ballon.</p>
339
                </div>
340
            </div>
341
        </section>
342
343
        <div class="d-grid">
344
            <div class="grid-col grid-col-6">
345
                <button class="button _info" type="button" name="cancel" ><?php print $langs->trans("Cancel") ?></button>
346
            </div>
347
348
            <div class="grid-col grid-col-6">
349
                <button class="button _success" type="submit" ><span class="fa fa-check"></span> <?php print $langs->trans("Save") ?></button>
350
            </div>
351
        </div>
352
    </form>
353
<?php
354
355
$db->close();
356
?>
357
358
<script type="text/html" id="orderRow">
359
360
    <tr class="order-row">
361
        <td><span class="fa fa-trash remove js-remove"></span> <span class="js-order-ref"></span></td>
362
        <td><input type="number" value="1" min="1" max="5" name="" class="js-nbr-pax" /></td>
363
    </tr>
364
</script>
365
366
<script type="application/javascript">
367
368
    /**
369
     * get the flight type object from an id.
370
     */
371
    function getFlightType(flightTypeId){
372
        var types = {
373
            1:{
374
                'billable' : 1,
375
                'expensable' : 1,
376
                'id' : 1
377
            },
378
            2:{
379
                'billable' : 1,
380
                'expensable' : 1,
381
                'id' : 2
382
            },
383
            3:{
384
                'billable' : 0,
385
                'expensable' : 0,
386
                'id' : 3
387
            },
388
            4:{
389
                'billable' : 0,
390
                'expensable' : 0,
391
                'id' : 4
392
            },
393
            5:{
394
                'billable' : 0,
395
                'expensable' : 0,
396
                'id' : 5
397
            },
398
            6:{
399
                'billable' : 0,
400
                'expensable' : 0,
401
                'id' : 6
402
            },
403
            7:{
404
                'billable' : 0,
405
                'expensable' : 0,
406
                'id' : 7
407
            }
408
        };
409
410
        var flightTypeNull = {
411
            'billable' : 0,
412
            'expensable' : 0,
413
            'id' : 0
414
        };
415
416
        return typeof types[flightTypeId] === 'undefined' ? flightTypeNull : types[flightTypeId];
417
    }
418
419
    function flightTypeChanged($this){
420
        var typeId = $this.val();
421
        var flightType = getFlightType(typeId);
422
423
        if(flightType.billable === 1){
424
            $('.js-form .js-billable-field').removeClass('hidden');
425
        }else{
426
            $('.js-form .js-billable-field').addClass('hidden');
427
        }
428
429
        if(flightType.expensable === 1){
430
            $('.js-form .js-expensable-field').removeClass('hidden');
431
        }else{
432
            $('.js-form .js-expensable-field').addClass('hidden');
433
        }
434
435
        if(flightType.id === 6){
436
            //instruction flight
437
            $('.js-form .js-instructor-field').removeClass('hidden');
438
            $('.js-form .js-organisator-field').addClass('hidden');
439
        } else {
440
            $('.js-form .js-instructor-field').addClass('hidden');
441
            $('.js-form .js-organisator-field').removeClass('hidden');
442
        }
443
444
    }
445
446
    function removeOrderLine(){
447
        var $this = $(this);
448
        $('.js-base-form .js-order select option[value="'+$this.data('orderId')+'"]').attr('disabled', false);
449
        $this.parents('tr').remove();
450
    }
451
452
    function addOrder(){
453
        var orderId = parseInt($('.js-base-form .js-order select').val(), 10);
454
        var $option = $('.js-base-form .js-order select option[value="'+orderId+'"]');
455
        var orderRef = $option.html();
456
        var $addingElement = $($('#orderRow').html());
457
        var $removeButton = $addingElement.find('.js-remove');
458
        var nbrPax = 1;
459
460
        if(orderId <= 0){
461
            return;
462
        }
463
464
        // Manage remove button
465
        $removeButton.data('orderId', orderId);
466
        $removeButton.on('click', removeOrderLine);
467
468
        // Add the reference
469
        $addingElement.find('.js-order-ref').html(orderRef);
470
471
        // Add the number of pax
472
        $addingElement.find('.js-nbr-pax').html(nbrPax);
473
        $addingElement.find('input.js-nbr-pax').val(nbrPax);
474
        $addingElement.find('input.js-nbr-pax').attr('name', 'order_passengers_count['+orderId+']');
475
476
        // disable the option
477
        $option.attr('disabled', true);
478
479
        $('.js-base-form .js-order').before($addingElement);
480
481
    }
482
483
    function changeReceiver(){
484
        var $select = $(this);
485
        var userId = parseInt($select.val(), 10);
486
        var currentUserId = parseInt($select.parents('.js-receiver').data('userId'), 10);
487
        var $cost = $('input.js-cost');
488
489
        $cost.val(0);
490
        $cost.prop('disabled', true);
491
        if(userId === currentUserId){
492
            $cost.val(0);
493
            $cost.prop('disabled', false);
494
        }
495
    }
496
497
    $(function(){
498
        $('.js-base-form .js-order select').on('change', addOrder);
499
        $('.js-base-form .js-receiver select').on('change', changeReceiver);
500
501
        $('.js-flight-type').on('change', function(){
502
            var $this = $(this);
503
            flightTypeChanged($this);
504
        });
505
        $('.js-remove').on('click', removeOrderLine);
506
        flightTypeChanged($('.js-flight-type:checked'));
507
508
    });
509
</script>
510
511
</section>
512