Completed
Push — feature/api ( de88d6...6cf568 )
by Laurent
02:01
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/validators/FlightValidator.php");
15
dol_include_once("/flightlog/command/CommandInterface.php");
16
dol_include_once("/flightlog/command/CommandHandlerInterface.php");
17
dol_include_once("/flightlog/command/CreateFlightCommand.php");
18
dol_include_once("/flightlog/command/CreateFlightCommandHandler.php");
19
20
// Load translation files required by the page
21
$langs->load("mymodule@flightlog");
22
23
$validator = new FlightValidator($langs, $db, $conf->global->BBC_FLIGHT_TYPE_CUSTOMER);
24
$createFlightHandler = new CreateFlightCommandHandler($db, $conf, $user, $langs, $validator);
25
26
if (!$user->rights->flightlog->vol->add) {
27
    accessforbidden();
28
}
29
30
/* * *****************************************************************
31
 * ACTIONS
32
 *
33
 * Put here all code to do according to value of "action" parameter
34
 * ****************************************************************** */
35
$msg = '';
36
if (GETPOST("action") == 'add') {
37
    if (!$_POST["cancel"]) {
38
        $dated = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
39
        $isGroupedFlight = (int) GETPOST('grouped_flight', 'int', 2) === 1;
40
        $orderIds = GETPOST('order_id', 'array', 2);
41
        $orderPassengersCount = GETPOST('order_passengers_count', 'array', 2);
42
43
        $volCommand = new CreateFlightCommand();
44
        $volCommand->setDate($dated)
45
            ->setLieuD($_POST['lieuD'])
46
            ->setLieuA($_POST['lieuA'])
47
            ->setHeureD($_POST['heureD'])
48
            ->setHeureA($_POST['heureA'])
49
            ->setBBCBallonsIdBBCBallons($_POST['ballon'])
50
            ->setNbrPax($_POST['nbrPax'])
51
            ->setRemarque($_POST['comm'])
52
            ->setIncidents($_POST['inci'])
53
            ->setFkType($_POST['type'])
54
            ->setFkPilot($_POST['pilot'])
55
            ->setFkOrganisateur($_POST['orga'])
56
            ->setKilometers($_POST['kilometers'])
57
            ->setCost($_POST['cost'])
58
            ->setFkReceiver($_POST['fk_receiver'])
59
            ->setJustifKilometers($_POST['justif_kilometers'])
60
            ->setPassengerNames($_POST['passenger_names'])
61
            ->setGroupedFlight($isGroupedFlight)
62
            ->setOrderIds($orderPassengersCount);
63
64
        try{
65
            $vol = $createFlightHandler->handle($volCommand);
66
67
            include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
68
            $interface = new Interfaces($db);
69
            $triggerResult = $interface->run_triggers('BBC_FLIGHT_LOG_ADD_FLIGHT', $vol, $user, $langs, $conf);
70
71
            $msg = '<div class="ok">L\'ajout du vol du : ' . $_POST["reday"] . '/' . $_POST["remonth"] . '/' . $_POST["reyear"] . ' s\'est correctement effectue ! </div>';
72
            Header("Location: card.php?id=" . $vol->id);
73
        }catch (\Exception $e){
74
            $msg = '<div class="error">Erreur lors de l\'ajout du vol : ' . ($vol->error?:$e->getMessage()) . '! </div>';
75
        }
76
77
    }
78
}
79
80
81
/* * *************************************************
82
 * PAGE
83
 *
84
 * Put here all code to build page
85
 * ************************************************** */
86
87
llxHeader('', 'Carnet de vol', '');
88
89
$html = new Form($db);
90
$commande = new Commande($db);
91
$datec = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
92
if ($msg) {
93
    print $msg;
94
}
95
96
?>
97
98
    <div class="errors error-messages">
99
        <?php
100
        foreach ($validator->getErrors() as $errorMessage) {
101
            print sprintf('<div class="error"><span>%s</span></div>', $errorMessage);
102
        }
103
        ?>
104
    </div>
105
    <form class="flight-form js-form" name='add' action="addFlight.php" method="post">
106
    <input type="hidden" name="action" value="add"/>
107
108
    <!-- Date et heures -->
109
    <section class="form-section">
110
        <h1 class="form-section-title"><?php echo $langs->trans('Date & heures'); ?></h1>
111
        <table class="border" width="100%">
112
            <tr>
113
                <td class="fieldrequired"> Type du vol</td>
114
                <td colspan="3">
115
                    <?php
116
                        //type du vol
117
                        select_flight_type($_POST['type']);
0 ignored issues
show
Deprecated Code introduced by
The function select_flight_type() has been deprecated with message: should use the form instead. Return list of flight type

This function has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed from the class and what other function to use instead.

Loading history...
118
                    ?>
119
                </td>
120
            </tr>
121
122
            <tr>
123
                <td class="fieldrequired"> Date du vol</td>
124
                <td>
125
                    <?php
126
                        print $html->select_date($datec ? $datec : -1, '', '', '', '', 'add', 1, 1);
127
                    ?>
128
                </td>
129
            </tr>
130
131
            <tr>
132
                <td class="fieldrequired">Heure de d&#233;part (format autorise XXXX)</td>
133
                <td width="25%" >
134
                    <input type="text"
135
                           name="heureD"
136
                           class="flat <?php echo($validator->hasError('heureD') ? 'error' : '') ?>"
137
                           value="<?php echo $_POST['heureD'] ?>"/>
138
                </td>
139
140
            <td class="fieldrequired">Heure d\'arriv&#233;e (format autorise XXXX)</td>
141
            <td>
142
                <input type="text"
143
                       name="heureA"
144
                       class="flat <?php echo($validator->hasError('heureA') ? 'error' : '') ?>"
145
                       value="<?php echo $_POST['heureA'] ?>"/>
146
            </td>
147
            </tr>
148
149
        </table>
150
    </section>
151
152
    <section class="form-section">
153
        <h1 class="form-section-title"><?php echo $langs->trans('Pilote & ballon') ?></h1>
154
        <table class="border" width="50%">
155
            <?php
156
            //Pilote
157
            print "<tr>";
158
            print '<td class="fieldrequired"> Pilote </td><td >';
159
            print $html->select_dolusers($_POST["pilot"] ? $_POST["pilot"] : $user->id, 'pilot', 0, null, 0, '', '', 0,0,0,'',0,'','', true);
160
            print '</td></tr>';
161
162
            //Ballon
163
            print "<tr>";
164
            print '<td width="25%" class="fieldrequired">Ballon</td><td>';
165
            select_balloons($_POST['ballon'], 'ballon', 0, 0);
166
            print '</td></tr>';
167
            ?>
168
169
            <tr>
170
                <td>Il y'avait-il plusieurs ballons ?</td>
171
                <td colspan="3"><input type="checkbox" value="1" name="grouped_flight"/> - Oui</td>
172
            </tr>
173
        </table>
174
    </section>
175
176
    <section class="form-section">
177
        <h1 class="form-section-title"><?php echo $langs->trans('Lieux') ?></h1>
178
        <table class="border" width="100%">
179
            <?php
180
181
            //place start
182
            print "<tr>";
183
            print '<td class="fieldrequired">Lieu de d&#233;part </td><td width="25%" >';
184
            print '<input type="text" name="lieuD" class="flat" value="' . $_POST['lieuD'] . '"/>';
185
            print '</td>';
186
187
            //place end
188
            print '<td class="fieldrequired">Lieu d\'arriv&#233;e </td><td>';
189
            print '<input type="text" name="lieuA" class="flat" value="' . $_POST['lieuA'] . '"/>';
190
            print '</td></tr>';
191
192
            ?>
193
194
        </table>
195
    </section>
196
197
    <section class="form-section">
198
        <h1 class="form-section-title"><span class="js-organisator-field">Organisateur</span><span class="js-instructor-field">Instructeur</span></h1>
199
        <table class="border" width="50%">
200
            <tr>
201
                <td class="fieldrequired"><span class="js-organisator-field">Organisateur</span><span class="js-instructor-field">Instructeur</span></td>
202
                <td>
203
                <?php
204
                    //organisateur
205
                    print $html->select_dolusers($_POST["orga"] ? $_POST["orga"] : $user->id, 'orga', 0, null, 0, '', '', 0,0,0,'',0,'','', true);
206
                ?>
207
                </td>
208
            </tr>
209
        </table>
210
    </section>
211
212
213
    <section class="form-section js-expensable-field">
214
        <h1 class="form-section-title"><?php echo $langs->trans('Déplacements') ?></h1>
215
        <table class="border" width="50%">
216
            <!-- number of kilometers done for the flight -->
217
            <tr>
218
                <td class="fieldrequired">Nombre de kilometres effectués pour le vol</td>
219
                <td>
220
                    <input type="number" name="kilometers" class="flat <?php echo($validator->hasError('kilometers') ? 'error' : '') ?>" value="<?php echo $_POST['kilometers'] ?>"/>
221
                </td>
222
            </tr>
223
224
            <!-- Justif Kilometers -->
225
            <tr>
226
227
                <td width="25%" class="fieldrequired">Justificatif des KM </td>
228
                <td>
229
                    <textarea name="justif_kilometers" rows="2" cols="60" class="flat <?php echo($validator->hasError('justif_kilometers') ? 'error' : '') ?>"><?php echo $_POST['justif_kilometers'] ?></textarea>
230
                </td>
231
            </tr>
232
        </table>
233
    </section>
234
235
    <!-- Passagers -->
236
    <section class="form-section">
237
        <h1 class="form-section-title"><?php echo $langs->trans('Passager') ?></h1>
238
        <table class="border" width="50%">
239
            <tr>
240
                <td class="fieldrequired"><?php echo $langs->trans('Nombre de passagers'); ?></td>
241
                <td>
242
                    <input type="number"
243
                           name="nbrPax"
244
                           class="flat <?php echo $validator->hasError('nbrPax') ? 'error' : '' ?>"
245
                           value="<?php echo $_POST['nbrPax']?: 0 ?>"/>
246
                </td>
247
            </tr>
248
249
            <!-- passenger names -->
250
            <tr>
251
                <td width="25%" class="fieldrequired"><?php echo $langs->trans('Noms des passagers'); ?><br/>(Séparé par des ; )</td>
252
                <td>
253
                    <textarea name="passenger_names" cols="60" rows="2" class="flat <?php echo $validator->hasError('passenger_names') ? 'error' : '' ?>"><?php echo $_POST['passenger_names'] ?></textarea>
254
                </td>
255
            </tr>
256
        </table>
257
    </section>
258
259
    <!-- billing information -->
260
    <section class="form-section">
261
        <h1 class="form-section-title js-billable-field"><?php echo $langs->trans('Facturation') ?></h1>
262
        <table class="border" width="50%">
263
264
            <!-- Order -->
265
            <tr id="list_order" class=" js-billable-field">
266
                <td class="fieldrequired"><?php echo $langs->trans('Commande du vol')?></td>
267
                <td class="js-order">
268
                    <p class="text-muted">Pour retirer une commande merci de la retirer de la liste ci-dessous.</p>
269
                    <?php
270
                        echo $html::multiselectarray('order_id', $commande->liste_array(2),$_POST['order_id'],0,0, $validator->hasError('order_id') ? 'error' : '',0,'100%');
271
                    ?>
272
                </td>
273
            </tr>
274
275
            <!-- Money receiver -->
276
            <tr class="js-hide-order js-billable-field">
277
                <td class="fieldrequired"><?php echo $langs->trans('Qui a perçu l\'argent')?></td><td>
278
                    <?php print $html->select_dolusers($_POST["fk_receiver"] ? $_POST["fk_receiver"] : $user->id,
279
                        'fk_receiver', true, null, 0, '', '', 0,0,0,'',0,'','', true); ?>
280
                </td>
281
            </tr>
282
283
            <!-- Flight cost -->
284
            <tr class="js-hide-order js-billable-field">
285
                <td class="fieldrequired">Montant perçu</td>
286
                <td>
287
                    <input type="text" name="cost" class="flat  <?php echo $validator->hasError('cost') ? 'error' : '' ?>" value="<?php echo $_POST['cost']?:0 ?> "/>
288
                    &euro;
289
                </td>
290
            </tr>
291
        </table>
292
    </section>
293
294
    <!-- comments -->
295
    <section class="form-section">
296
        <h1 class="form-section-title"><?php echo $langs->trans('Commentaires') ?></h1>
297
        <table class="border" width="50%">
298
            <!-- commentaires -->
299
            <tr class="">
300
                <td class="fieldrequired"> Note sur le vol </td><td>
301
                    <textarea rows="2" cols="60" class="flat" name="comm" placeholder="RAS"><?php print $_POST['comm']; ?></textarea>
302
                </td>
303
            </tr>
304
305
            <!-- incidents -->
306
            <tr class="">
307
                <td class="fieldrequired"> Incidents, Brulure, ...</td><td>
308
                    <textarea rows="2" cols="60" class="flat" name="inci" placeholder="RAS"><?php print $_POST['inci']; ?></textarea>
309
                </td>
310
            </tr>
311
        </table>
312
    </section>
313
<?php
314
315
print '<br><input class="button" type="submit" value="' . $langs->trans("Save") . '"> &nbsp; &nbsp; ';
316
print '<input class="button" type="submit" name="cancel" value="' . $langs->trans("Cancel") . '">';
317
318
print '</form>';
319
320
$db->close();
321
?>
322
323
<script type="text/html" id="orderRow">
324
    <tr class="js-detail-order">
325
        <td class="js-order-ref"></td>
326
        <td class="js-order-passenger">
327
            <input type="text" name="order_passengers_count[]" class="flat" value="1"/> Passager(s)
328
        </td>
329
    </tr>
330
</script>
331
332
<script type="application/javascript">
333
334
    <?php if(!empty(GETPOST('order_passengers_count', 'array', 2))): ?>
335
        var orders = {};
336
        <?php foreach( GETPOST('order_passengers_count', 'array', 2) as $currentOrderId=>$nbrPaxForOrder): ?>
337
        orders[<?php echo $currentOrderId; ?>] = <?php echo $nbrPaxForOrder; ?>;
338
        <?php endforeach; ?>
339
    <?php endif; ?>
340
341
    function hideOrderInformation (){
342
        var $this = $(this);
343
344
        // Hide - unhide
345
        if($this.val().length > 0){
346
            $('.js-hide-order').hide();
347
        }else{
348
            $('.js-hide-order').show();
349
        }
350
351
        $('tr.js-detail-order').remove();
352
353
        //Multi orders
354
        $this.find('option:selected').each(function(){
355
            var $option = $(this);
356
            var $addingElement = $($('#orderRow').html());
357
            var $input = $addingElement.find('.js-order-passenger input');
358
359
            $addingElement.find('.js-order-ref').html($option.html());
360
361
            $input.attr('name' , 'order_passengers_count['+$option.val()+']');
362
            if(typeof orders !== "undefined" && typeof orders[$option.val()] !== 'undefined'){
363
                $input.val(orders[$option.val()]);
364
            }
365
366
            $addingElement.insertAfter($('#list_order'));
367
        });
368
    }
369
370
    /**
371
     * get the flight type object from an id.
372
     */
373
    function getFlightType(flightTypeId){
374
        var types = {
375
            1:{
376
                'billable' : 1,
377
                'expensable' : 1,
378
                'id' : 1
379
            },
380
            2:{
381
                'billable' : 1,
382
                'expensable' : 1,
383
                'id' : 2
384
            },
385
            3:{
386
                'billable' : 0,
387
                'expensable' : 0,
388
                'id' : 3
389
            },
390
            4:{
391
                'billable' : 0,
392
                'expensable' : 0,
393
                'id' : 4
394
            },
395
            5:{
396
                'billable' : 0,
397
                'expensable' : 0,
398
                'id' : 5
399
            },
400
            6:{
401
                'billable' : 0,
402
                'expensable' : 0,
403
                'id' : 6
404
            },
405
            7:{
406
                'billable' : 0,
407
                'expensable' : 0,
408
                'id' : 7
409
            }
410
        };
411
412
        var flightTypeNull = {
413
            'billable' : 0,
414
            'expensable' : 0,
415
            'id' : 0
416
        };
417
418
        return typeof types[flightTypeId] === 'undefined' ? flightTypeNull : types[flightTypeId];
419
    }
420
421
    function flightTypeChanged(){
422
        var $this = $(this);
423
        var typeId = $this.val();
424
        var flightType = getFlightType(typeId);
425
426
        if(flightType.billable === 1){
427
            $('.js-form .js-billable-field').removeClass('hidden');
428
        }else{
429
            $('.js-form .js-billable-field').addClass('hidden');
430
        }
431
432
        if(flightType.expensable === 1){
433
            $('.js-form .js-expensable-field').removeClass('hidden');
434
        }else{
435
            $('.js-form .js-expensable-field').addClass('hidden');
436
        }
437
438
        if(flightType.id === 6){
439
            //instruction flight
440
            $('.js-form .js-instructor-field').removeClass('hidden');
441
            $('.js-form .js-organisator-field').addClass('hidden');
442
        }else{
443
            $('.js-form .js-instructor-field').addClass('hidden');
444
            $('.js-form .js-organisator-field').removeClass('hidden');
445
        }
446
447
    }
448
449
    $(function(){
450
        $('.js-order select').on('change', hideOrderInformation);
451
        $('.js-order select').each(hideOrderInformation);
452
453
        $('.js-flight-type').on('change', flightTypeChanged);
454
        $('.js-flight-type').each(flightTypeChanged);
455
    });
456
</script>
457