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