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

addOrder.php (2 issues)

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('/flightlog/class/bbcvols.class.php');
11
dol_include_once('/flightlog/class/bbctypes.class.php');
12
dol_include_once("/flightlog/lib/flightLog.lib.php");
13
dol_include_once("/flightlog/validators/SimpleOrderValidator.php");
14
dol_include_once("/flightlog/command/CreateOrderCommandHandler.php");
15
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formcompany.class.php';
16
require_once DOL_DOCUMENT_ROOT.'/core/class/html.formadmin.class.php';
17
18
// Load object modCodeTiers
19
$module=(! empty($conf->global->SOCIETE_CODECLIENT_ADDON)?$conf->global->SOCIETE_CODECLIENT_ADDON:'mod_codeclient_leopard');
20 View Code Duplication
if (substr($module, 0, 15) == 'mod_codeclient_' && substr($module, -3) == 'php')
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
21
{
22
    $module = substr($module, 0, dol_strlen($module)-4);
23
}
24
$dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
25
foreach ($dirsociete as $dirroot)
26
{
27
    $res=dol_include_once($dirroot.$module.'.php');
28
    if ($res) break;
29
}
30
$modCodeClient = new $module;
31
// Load object modCodeFournisseur
32
$module=(! empty($conf->global->SOCIETE_CODECLIENT_ADDON)?$conf->global->SOCIETE_CODECLIENT_ADDON:'mod_codeclient_leopard');
33 View Code Duplication
if (substr($module, 0, 15) == 'mod_codeclient_' && substr($module, -3) == 'php')
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
34
{
35
    $module = substr($module, 0, dol_strlen($module)-4);
36
}
37
$dirsociete=array_merge(array('/core/modules/societe/'),$conf->modules_parts['societe']);
38
foreach ($dirsociete as $dirroot)
39
{
40
    $res=dol_include_once($dirroot.$module.'.php');
41
    if ($res) break;
42
}
43
$modCodeFournisseur = new $module;
44
45
// Load translation files required by the page
46
$langs->load("mymodule@flightlog");
47
48
$validator = new SimpleOrderValidator($langs, $db, $conf->global->BBC_FLIGHT_TYPE_CUSTOMER);
49
50
$customer = new Societe($db);
51
52
$successMessage = false;
53
54
/* * *****************************************************************
55
 * ACTIONS
56
 *
57
 * Put here all code to do according to value of "action" parameter
58
 * ****************************************************************** */
59
$msg = '';
60
$socid = null;
61
62
if (GETPOST('socid', 'int') > 0){
63
    $socid = GETPOST('socid', 'int');
64
    $customer->fetch($socid);
65
}
66
67
68
if (GETPOST("action") == 'add') {
69
    if (!$_POST["cancel"]) {
70
71
        $formObject = new stdClass();
72
        $formObject->socid = $socid;
73
        $formObject->name = GETPOST('name','alpha');
74
        $formObject->firstname = GETPOST('firstname','alpha');
75
        $formObject->zip = GETPOST('zipcode','alpha');
76
        $formObject->town = GETPOST('town', 'alpha');
77
        $formObject->state = GETPOST('state_id', 'int');
78
        $formObject->phone = GETPOST('phone', 'alpha');
79
        $formObject->origine = GETPOST('origine', 'int');
80
        $formObject->email = trim(GETPOST('mail', 'custom', 0, FILTER_SANITIZE_EMAIL));
81
        $formObject->tva = GETPOST('tva_intra', 'alpha');
82
        $formObject->nbrPax = GETPOST('nbrPax', 'int');
83
        $formObject->region = GETPOST('region', 'alpha');
84
        $formObject->cost = GETPOST('cost');
85
        $formObject->comment = GETPOST('comm', 'alpha');
86
        $formObject->civilityId = GETPOST('civility', 'alpha');
87
        $formObject->language = GETPOST('default_lang', 'int');
88
        $formObject->isCommentPublic = GETPOST('public_comment', 'int');
89
90
        if ($validator->isValid($formObject, $_REQUEST)) {
91
            $createOrderCommand = new CreateOrderCommand($formObject, $user->id);
92
            try{
93
                $handler = new CreateOrderCommandHandler($db, $conf,$user,$langs,$modCodeClient, $modCodeFournisseur);
94
                $handler->handle($createOrderCommand);
95
96
                $msg = '<div class="success ok">Commande et tiers créés. </div>';
97
                $successMessage = true;
98
            } catch (\Exception $e) {
99
                // Creation KO
100
                $msg = '<div class="error">Erreur lors de l\'ajout de la commande</div>';
101
            }
102
        }
103
    }
104
}
105
106
107
/* * *************************************************
108
 * PAGE
109
 *
110
 * Put here all code to build page
111
 * ************************************************** */
112
113
llxHeader('', 'Creation d\'une commande', '');
114
115
$html = new Form($db);
116
$formcompany = new FormCompany($db);
117
$formAdmin = new FormAdmin($db);
118
119
120
$datec = dol_mktime(12, 0, 0, $_POST["remonth"], $_POST["reday"], $_POST["reyear"]);
121
if ($msg) {
122
    print $msg;
123
}
124
125
?>
126
127
    <!-- Success message with reference -->
128
    <?php if($successMessage): ?>
129
        <div class="bbc-style">
130
            <p class="cadre_msg1">
131
                Vous avez généré la facture et créé un tiers.<br/>
132
                Merci,
133
            </p>
134
135
            <?php if(count($validator->getWarningMessages()) > 0):?>
136
                <div class="box box-warning">
137
                    <p>Ceci sont des messages d'attention mais qui ne bloquent pas la création de la commande</p>
138
                    <ul>
139
                        <?php foreach($validator->getWarningMessages() as $warningMessage): ?>
140
                            <li class="warning"><?php echo $warningMessage; ?></li>
141
                        <?php endforeach; ?>
142
                    </ul>
143
                </div>
144
            <?php endif; ?>
145
146
            <table class="table_resume">
147
148
                <tr>
149
                    <td></td>
150
                    <td>Statut</td>
151
                    <td>Référence</td>
152
                </tr>
153
154
                <!-- tiers -->
155
                <tr>
156
                    <td>Tiers</td>
157
                    <td><span class="dashboardlineok">OK</span></td>
158
                    <td><?php echo $handler->getCustomer()->getNomUrl(); ?></td>
159
                </tr>
160
161
                <!-- Commande -->
162
                <tr>
163
                    <td>Commande</td>
164
                    <td><span class="dashboardlineok">OK</span></td>
165
                    <td><?php echo $handler->getOrder()->getNomUrl(); ?></td>
166
                </tr>
167
168
                <tr>
169
                    <td colspan="3">
170
                        Le passager doit faire le payement avec ce numéro de commande.<br/>
171
                        Cette référence doit aussi être communiquée au(x) pilote(s) qui feront le vol.
172
                    </td>
173
                </tr>
174
            </table>
175
176
            <p>
177
                Si le passager demande un document, merci de me le communiquer, je ferais le nécessaire.
178
            </p>
179
180
        </div>
181
182
        <?php return; ?>
183
    <?php endif; ?>
184
185
186
    <div class="errors error-messages">
187
        <?php
188
        foreach ($validator->getErrors() as $errorMessage) {
189
            print sprintf('<div class="error"><span>%s</span></div>', $errorMessage);
190
        }
191
        ?>
192
    </div>
193
194
    <div>
195
        <p>
196
            Cette page vous permettra de créer une commande. La commande est <b>obligatoire</b> si vous désirez faire payer les passagers directement sur le compte du club.<br>
197
            Si vous avez un doute sur la manière d'encoder la commande, veuillez me contacter AVANT de soumettre le dit formulaire.<br/>
198
            Si vous avez <b>déjà</b> encodé une commande, et que vous voulez la retrouver veuillez vous rendre sur : <a href="<?php echo sprintf(DOL_URL_ROOT.'/commande/list.php?search_sale=%s', $user->id); ?>">mes commandes.</a>
199
        </p>
200
    </div>
201
    <form class="flight-form" name='add' method="post">
202
    <input type="hidden" name="action" value="add"/>
203
    <input type="hidden" name="token" value="<?php echo newToken(); ?>"/>
204
205
    <!-- Commanditaire -->
206
    <section class="form-section">
207
        <h1 class="form-section-title"><?php echo $langs->trans('Commanditaire') ?></h1>
208
        <table class="border" width="100%">
209
210
            <tr>
211
                <td class="">
212
                    <?php echo $langs->trans('Commanditaire'); ?>
213
                </td>
214
215
                <td>
216
                    <?php print $form->select_company($customer->id, 'socid', '((s.client = 1 OR s.client = 3) AND s.status=1)', 'SelectThirdParty', 0, 0, null, 0, 'minwidth300'); ?>
217
                    <script type="text/javascript">
218
                        $(document).ready(function() {
219
                            $("#socid").change(function() {
220
                                var socid = $(this).val();
221
                                window.location.href = "<?php echo $_SERVER["PHP_SELF"].'?socid="+socid' ?>
222
                            });
223
                        });
224
                    </script>
225
                </td>
226
227
            </tr>
228
229
230
            <?php if($socid === null): ?>
231
                <!-- Nom -->
232
                <tr>
233
                    <td class="fieldrequired">
234
                        <?php echo $langs->trans('Nom'); ?>
235
                    </td>
236
                    <td>
237
                        <input type="text"
238
                               name="name"
239
                               class="flat <?php echo $validator->hasError('name') ? 'error' : '' ?>"
240
                               value="<?php echo $formObject->name ?>"/>
241
                    </td>
242
                </tr>
243
244
                <!-- Firstname -->
245
                <tr>
246
                    <td class="">
247
                        <?php echo $langs->trans('Prénom'); ?>
248
                    </td>
249
                    <td>
250
                        <input type="text"
251
                               name="firstname"
252
                               class="flat <?php echo $validator->hasError('firstname') ? 'error' : '' ?>"
253
                               value="<?php echo $formObject->firstname ?>"/>
254
                    </td>
255
                </tr>
256
257
                <!-- civility-->
258
                <tr>
259
                    <td class="">
260
                        <?php echo $langs->trans('UserTitle'); ?>
261
                    </td>
262
                    <td>
263
                        <?php echo $formcompany->select_civility($formObject->civilityId, 'civility'); ?>
264
                    </td>
265
                </tr>
266
267
                <!-- Phone -->
268
                <tr>
269
                    <td class="">
270
                        <?php echo $langs->trans('Téléphone'); ?>
271
                    </td>
272
                    <td>
273
                        <input type="text"
274
                               name="phone"
275
                               class="flat <?php echo $validator->hasError('phone') ? 'error' : '' ?>"
276
                               value="<?php echo $formObject->phone ?>"/>
277
                    </td>
278
                </tr>
279
280
                <!-- Mail -->
281
                <tr>
282
                    <td class="">
283
                        <?php echo $langs->trans('E-mail'); ?>
284
                    </td>
285
                    <td>
286
                        <input type="text"
287
                               name="mail"
288
                               class="flat <?php echo $validator->hasError('email') ? 'error' : '' ?>"
289
                               value="<?php echo $formObject->email; ?>"/>
290
                    </td>
291
                </tr>
292
293
                <!-- Language -->
294
                <tr>
295
                    <td class="fieldrequired">
296
                        <?php echo $langs->trans('DefaultLang'); ?>
297
                    </td>
298
                    <td>
299
                        <?php echo $formAdmin->select_language($conf->global->MAIN_LANG_DEFAULT,'default_lang',0,0,1,0,0,'maxwidth200onsmartphone'); ?>
300
                    </td>
301
                </tr>
302
303
                <!-- Region -->
304
                <tr>
305
                    <td class="">
306
                        <?php echo $langs->trans('Region'); ?>
307
                    </td>
308
                    <td>
309
                        <?php print $formcompany->select_state($formObject->state,'BE'); ?>
310
                    </td>
311
                </tr>
312
313
                <?php
314
                // Zip / Town
315
                print '<tr><td>'.fieldLabel('Zip','zipcode').'</td><td>';
316
                        print $formcompany->select_ziptown($formObject->town,'zipcode',array('town','selectcountry_id','state_id'), 0, 0, '', 'maxwidth100 quatrevingtpercent');
317
                        print '</td><td>'.fieldLabel('Town','town').'</td><td>';
318
                        print $formcompany->select_ziptown($formObject->zip,'town',array('zipcode','selectcountry_id','state_id'), 0, 0, '', 'maxwidth100 quatrevingtpercent');
319
                        print '</td></tr>';
320
                ?>
321
322
                <!-- origine -->
323
                <tr>
324
                    <td class="">
325
                        <?php echo $langs->trans('Origine'); ?>
326
                    </td>
327
                    <td>
328
                        <?php $html->selectInputReason($formObject->origine, 'origine', 1); ?>
329
                    </td>
330
                </tr>
331
332
                <!-- TVA -->
333
                <tr>
334
                    <td class="">
335
                        Numéro de TVA
336
                    </td>
337
                    <td>
338
                        <input type="text" class="flat" name="tva_intra" id="intra_vat" maxlength="20" value="<?php echo $_POST['tva_intra']; ?>">
339
                        <?php
340
                        if (empty($conf->global->MAIN_DISABLEVATCHECK)): ?>
341
342
                            <?php if (! empty($conf->use_javascript_ajax)): ?>
343
                                <script language="JavaScript" type="text/javascript">
344
                                function CheckVAT(a) {
345
                                    <?php print "newpopup('".DOL_URL_ROOT."/societe/checkvat/checkVatPopup.php?vatNumber='+a,'".dol_escape_js($langs->trans("VATIntraCheckableOnEUSite"))."',500,300);"; ?>
346
                                }
347
                                </script>
348
                                <a href="#" class="hideonsmartphone" onclick="javascript: CheckVAT(document.add.tva_intra.value);"><?php echo $langs->trans("VATIntraCheck"); ?></a>
349
                                <?php echo $html->textwithpicto($s,$langs->trans("VATIntraCheckDesc",$langs->trans("VATIntraCheck")),1); ?>
350
                            <?php else: ?>
351
                                <a href="<?php echo $langs->transcountry("VATIntraCheckURL",$object->country_id); ?>" target="_blank"><?php echo img_picto($langs->trans("VATIntraCheckableOnEUSite"),'help'); ?></a>
352
                            <?php endif; ?>
353
                       <?php endif; ?>
354
                    </td>
355
                </tr>
356
357
            <?php endif; ?>
358
359
        </table>
360
    </section>
361
362
    <!-- Passagers -->
363
    <section class="form-section">
364
        <h1 class="form-section-title"><?php echo $langs->trans('Données du vol') ?></h1>
365
        <table class="border js-compute" width="50%">
366
367
            <!-- Nombre -->
368
            <tr>
369
                <td class="fieldrequired"><?php echo $langs->trans('Nombre de passagers'); ?></td>
370
                <td>
371
                    <input type="number"
372
                           name="nbrPax"
373
                           class="flat <?php echo $validator->hasError('nbrPax') ? 'error' : '' ?>"
374
                           value="<?php echo $_POST['nbrPax'] ?>"/>
375
                </td>
376
            </tr>
377
378
            <!-- Flight cost -->
379
            <tr>
380
                <td class="fieldrequired"><?php echo $langs->trans('Montant demandé (au total)') ?></td>
381
                <td>
382
                    <input type="text" name="cost" class="flat  <?php echo $validator->hasError('cost') ? 'error' : '' ?>" value="<?php echo $_POST['cost'] ?> "/>
383
                    &euro;
384
                </td>
385
            </tr>
386
387
            <!-- Total / pax -->
388
            <tr class="cadre_prix_total">
389
                <td class="fieldrequired"><?php echo $langs->trans('Montant / passager') ?></td>
390
                <td>
391
                    <span class="js-total">0</span>&euro;
392
                </td>
393
            </tr>
394
        </table>
395
    </section>
396
397
    <!-- Commentaire -->
398
    <section class="form-section">
399
        <h1 class="form-section-title"><?php echo $langs->trans('Commentaire') ?></h1>
400
        <table class="border" width="50%">
401
402
            <!-- Comment -->
403
            <tr>
404
                <td><?php echo $langs->trans('Le commentaire doit-il figurer sur la commande') ?></td>
405
                <td>
406
                    <input type="radio" id="public_comment" name="public_comment" value="1" <?php echo ($formObject->isCommentPublic == 1)?'checked="checked"' : ''; ?>/>
407
                    <label for="public_comment">Oui</label>
408
                    -
409
                    <input type="radio" id="private_comment" name="public_comment" value="0" <?php echo ($formObject == null || $formObject->isCommentPublic === null || $formObject->isCommentPublic === 0)?'checked="checked"' : ''; ?>/>
410
                    <label for="private_comment">Non</label>
411
                </td>
412
            </tr>
413
414
            <tr>
415
                <td class=""> Commentaire </td>
416
                <td>
417
                    <?php
418
                        print '<textarea rows="2" cols="60" class="flat" name="comm" placeholder="">' . $_POST['comm'] . '</textarea> ';
419
                    ?>
420
                </td>
421
            </tr>
422
        </table>
423
    </section>
424
425
    <script type="application/javascript">
426
        (function($){
427
428
            var cost = $('.js-compute input[name="cost"]').val();
429
            var nbrPax = $('.js-compute input[name="nbrPax"]').val();
430
            compute();
431
432
            $('.js-compute input[name="nbrPax"]').on('change', function(){
433
                nbrPax = $(this).val();
434
                compute();
435
            });
436
437
            $('.js-compute input[name="cost"]').on('change', function(){
438
                cost = $(this).val();
439
                compute();
440
            });
441
442
            function compute(){
443
                if(isNaN(parseInt(nbrPax, 10)) || parseInt(nbrPax, 10) === 0){
444
                    $('.js-compute .js-total').text(0);
445
                    return;
446
                }
447
448
                var total = parseInt((cost / nbrPax)*100, 10    );
449
                $('.js-compute .js-total').text(total/100);
450
            }
451
452
453
        })($);
454
    </script>
455
456
<?php
457
458
print '<br><input class="button" type="submit" value="' . $langs->trans("Save") . '"> &nbsp; &nbsp; ';
459
print '<input class="button" type="submit" name="cancel" value="' . $langs->trans("Cancel") . '">';
460
461
print '</form>';
462
463
$db->close();
464