Completed
Push — feature/multi_order ( eb7707 )
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/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 . '! </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
                    <?php
263
                        echo $html::multiselectarray('order_id', $commande->liste_array(2),$_POST['order_id'],0,0,'',0,'100%');
264
                    ?>
265
                </td>
266
            </tr>
267
268
            <!-- Money receiver -->
269
            <tr class="js-hide-order js-billable-field">
270
                <td class="fieldrequired"><?php echo $langs->trans('Qui a perçu l\'argent')?></td><td>
271
                    <?php print $html->select_dolusers($_POST["fk_receiver"] ? $_POST["fk_receiver"] : $user->id,
272
                        'fk_receiver', true, null, 0, '', '', 0,0,0,'',0,'','', true); ?>
273
                </td>
274
            </tr>
275
276
            <!-- Flight cost -->
277
            <tr class="js-hide-order js-billable-field">
278
                <td class="fieldrequired">Montant perçu</td>
279
                <td>
280
                    <input type="text" name="cost" class="flat  <?php echo $validator->hasError('cost') ? 'error' : '' ?>" value="<?php echo $_POST['cost']?:0 ?> "/>
281
                    &euro;
282
                </td>
283
            </tr>
284
        </table>
285
    </section>
286
287
    <!-- comments -->
288
    <section class="form-section">
289
        <h1 class="form-section-title"><?php echo $langs->trans('Commentaires') ?></h1>
290
        <table class="border" width="50%">
291
            <!-- commentaires -->
292
            <tr class="">
293
                <td class="fieldrequired"> Commentaire </td><td>
294
                    <textarea rows="2" cols="60" class="flat" name="comm" placeholder="RAS"><?php print $_POST['comm']; ?></textarea>
295
                </td>
296
            </tr>
297
298
            <!-- incidents -->
299
            <tr class="">
300
                <td class="fieldrequired"> incidents </td><td>
301
                    <textarea rows="2" cols="60" class="flat" name="inci" placeholder="RAS"><?php print $_POST['inci']; ?></textarea>
302
                </td>
303
            </tr>
304
        </table>
305
    </section>
306
<?php
307
308
print '<br><input class="button" type="submit" value="' . $langs->trans("Save") . '"> &nbsp; &nbsp; ';
309
print '<input class="button" type="submit" name="cancel" value="' . $langs->trans("Cancel") . '">';
310
311
print '</form>';
312
313
$db->close();
314
?>
315
316
<script type="text/html" id="orderRow">
317
    <tr class="js-detail-order">
318
        <td class="js-order-ref"></td>
319
        <td class="js-order-passenger">
320
            <input type="text" name="order_passengers_count[]" class="flat" value="1"/> Passager(s)
321
        </td>
322
    </tr>
323
</script>
324
325
<script type="application/javascript">
326
327
    <?php if(!empty(GETPOST('order_passengers_count', 'array', 2))): ?>
328
        var orders = {};
329
        <?php foreach( GETPOST('order_passengers_count', 'array', 2) as $currentOrderId=>$nbrPaxForOrder): ?>
330
        orders[<?php echo $currentOrderId; ?>] = <?php echo $nbrPaxForOrder; ?>;
331
        <?php endforeach; ?>
332
    <?php endif; ?>
333
334
    function hideOrderInformation (){
335
        var $this = $(this);
336
337
        // Hide - unhide
338
        if($this.val().length > 0){
339
            $('.js-hide-order').hide();
340
        }else{
341
            $('.js-hide-order').show();
342
        }
343
344
        $('tr.js-detail-order').remove();
345
346
        //Multi orders
347
        $this.find('option:selected').each(function(){
348
            var $option = $(this);
349
            var $addingElement = $($('#orderRow').html());
350
            var $input = $addingElement.find('.js-order-passenger input');
351
352
            $addingElement.find('.js-order-ref').html($option.html());
353
354
            $input.attr('name' , 'order_passengers_count['+$option.val()+']');
355
            if(typeof orders !== "undefined" && typeof orders[$option.val()] !== 'undefined'){
356
                $input.val(orders[$option.val()]);
357
            }
358
359
            $addingElement.insertAfter($('#list_order'));
360
        });
361
    }
362
363
    /**
364
     * get the flight type object from an id.
365
     */
366
    function getFlightType(flightTypeId){
367
        var types = {
368
            1:{
369
                'billable' : 1,
370
                'expensable' : 1,
371
                'id' : 1
372
            },
373
            2:{
374
                'billable' : 1,
375
                'expensable' : 1,
376
                'id' : 2
377
            },
378
            3:{
379
                'billable' : 0,
380
                'expensable' : 0,
381
                'id' : 3
382
            },
383
            4:{
384
                'billable' : 0,
385
                'expensable' : 0,
386
                'id' : 4
387
            },
388
            5:{
389
                'billable' : 0,
390
                'expensable' : 0,
391
                'id' : 5
392
            },
393
            6:{
394
                'billable' : 0,
395
                'expensable' : 0,
396
                'id' : 6
397
            },
398
            7:{
399
                'billable' : 0,
400
                'expensable' : 0,
401
                'id' : 7
402
            }
403
        };
404
405
        var flightTypeNull = {
406
            'billable' : 0,
407
            'expensable' : 0,
408
            'id' : 0
409
        };
410
411
        return typeof types[flightTypeId] === 'undefined' ? flightTypeNull : types[flightTypeId];
412
    }
413
414
    function flightTypeChanged(){
415
        var $this = $(this);
416
        var typeId = $this.val();
417
        var flightType = getFlightType(typeId);
418
419
        if(flightType.billable === 1){
420
            $('.js-form .js-billable-field').removeClass('hidden');
421
        }else{
422
            $('.js-form .js-billable-field').addClass('hidden');
423
        }
424
425
        if(flightType.expensable === 1){
426
            $('.js-form .js-expensable-field').removeClass('hidden');
427
        }else{
428
            $('.js-form .js-expensable-field').addClass('hidden');
429
        }
430
431
        if(flightType.id === 6){
432
            //instruction flight
433
            $('.js-form .js-instructor-field').removeClass('hidden');
434
            $('.js-form .js-organisator-field').addClass('hidden');
435
        }else{
436
            $('.js-form .js-instructor-field').addClass('hidden');
437
            $('.js-form .js-organisator-field').removeClass('hidden');
438
        }
439
440
    }
441
442
    $(function(){
443
        $('.js-order select').on('change', hideOrderInformation);
444
        $('.js-order select').each(hideOrderInformation);
445
446
        $('.js-flight-type').on('change', flightTypeChanged);
447
        $('.js-flight-type').each(flightTypeChanged);
448
    });
449
</script>
450