Completed
Push — feature/multiple_order_one_fli... ( 4a4e05...e0cd6a )
by Laurent
03:48 queued 02:03
created

Bbcvols::createLinksWithOrder()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 6
nop 1
dl 0
loc 29
rs 9.456
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2007-2012  Laurent Destailleur <[email protected]>
3
 * Copyright (C) 2014       Juanjo Menent       <[email protected]>
4
 * Copyright (C) 2015       Florian Henry       <[email protected]>
5
 * Copyright (C) 2015       Raphaël Doursenaud  <[email protected]>
6
 * Copyright (C) ---Put here your own copyright and developer email---
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/**
23
 * \file    flightlog/bbcvols.class.php
24
 * \ingroup flightlog
25
 * \brief   This file is an example for a CRUD class file (Create/Read/Update/Delete)
26
 *          Put some comments here
27
 */
28
29
// Put here all includes required by your class file
30
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
31
require_once DOL_DOCUMENT_ROOT . '/flightballoon/class/bbc_ballons.class.php';
32
require_once DOL_DOCUMENT_ROOT . '/flightlog/class/bbctypes.class.php';
33
require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
34
35
/**
36
 * Class Bbcvols
37
 *
38
 * Put here description of your class
39
 *
40
 * @see CommonObject
41
 */
42
class Bbcvols extends CommonObject
43
{
44
    /**
45
     * @var string Id to identify managed objects
46
     */
47
    public $element = 'flightlog_bbcvols';
48
    /**
49
     * @var string Name of table without prefix where object is stored
50
     */
51
    public $table_element = 'bbc_vols';
52
53
    public $idBBC_vols;
54
    public $date = '';
55
    public $lieuD;
56
    public $lieuA;
57
    public $heureD;
58
    public $heureA;
59
    public $BBC_ballons_idBBC_ballons;
60
    public $nbrPax;
61
    public $remarque;
62
    public $incidents;
63
    public $fk_type;
64
    public $fk_pilot;
65
    public $fk_organisateur;
66
    public $is_facture;
67
    public $kilometers;
68
    public $cost;
69
    public $fk_receiver;
70
    public $justif_kilometers;
71
    public $date_creation;
72
    public $date_update;
73
74
    /**
75
     * @var Bbc_ballons
76
     */
77
    private $balloon;
78
79
    /**
80
     * @var User
81
     */
82
    private $pilot;
83
84
    /**
85
     * @var string
86
     */
87
    private $passengerNames;
88
89
    /**
90
     * @var int[]|array
91
     */
92
    private $orderIds;
93
94
    /**
95
     * @var Commande[]|array
96
     */
97
    private $orders;
98
99
    /**
100
     * @return int
101
     */
102
    public function getIdBBCVols()
103
    {
104
        return (int) $this->idBBC_vols;
105
    }
106
107
    /**
108
     * @return int
109
     */
110
    public function getId()
111
    {
112
        return (int) $this->getIdBBCVols();
113
    }
114
115
    /**
116
     * @param string|int $ref
117
     *
118
     * @return $this
119
     */
120
    public function setRef($ref)
121
    {
122
        $this->ref = $ref;
123
        return $this;
124
    }
125
126
    /**
127
     * Constructor
128
     *
129
     * @param DoliDb $db Database handler
130
     */
131
    public function __construct(DoliDB $db)
132
    {
133
        $this->db = $db;
134
        $this->cost = 0;
135
136
        $this->passengerNames = '';
137
        $this->orderIds = [];
138
    }
139
140
    /**
141
     * Create a flight
142
     *
143
     * @param  User $user      User that creates
144
     * @param  bool $notrigger false=launch triggers after, true=disable triggers
145
     *
146
     * @return int <0 if KO, Id of created object if OK
147
     * @throws Exception
148
     */
149
    public function create(User $user, $notrigger = false)
150
    {
151
        dol_syslog(__METHOD__, LOG_DEBUG);
152
153
        $error = 0;
154
155
        // Clean parameters
156
157
        if (isset($this->idBBC_vols)) {
158
            $this->idBBC_vols = trim($this->idBBC_vols);
159
        }
160
        if (isset($this->lieuD)) {
161
            $this->lieuD = trim($this->lieuD);
162
        }
163
        if (isset($this->lieuA)) {
164
            $this->lieuA = trim($this->lieuA);
165
        }
166
        if (isset($this->heureD)) {
167
            $this->heureD = trim($this->heureD) . '00';
168
        }
169
        if (isset($this->heureA)) {
170
            $this->heureA = trim($this->heureA) . '00';
171
        }
172
        if (isset($this->BBC_ballons_idBBC_ballons)) {
173
            $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons);
174
        }
175
        if (isset($this->nbrPax)) {
176
            $this->nbrPax = trim($this->nbrPax);
177
        }
178
        if (isset($this->remarque)) {
179
            $this->remarque = trim($this->remarque);
180
        }
181
        if (isset($this->incidents)) {
182
            $this->incidents = trim($this->incidents);
183
        }
184
        if (isset($this->fk_type)) {
185
            $this->fk_type = trim($this->fk_type);
186
        }
187
        if (isset($this->fk_pilot)) {
188
            $this->fk_pilot = trim($this->fk_pilot);
189
        }
190
        if (isset($this->fk_organisateur)) {
191
            $this->fk_organisateur = trim($this->fk_organisateur);
192
        }
193
        if (isset($this->is_facture)) {
194
            $this->is_facture = trim($this->is_facture);
195
        }
196
        if (isset($this->kilometers)) {
197
            $this->kilometers = trim($this->kilometers);
198
        }
199
        if (isset($this->cost)) {
200
            $this->cost = trim($this->cost);
201
        }
202
        if (isset($this->fk_receiver)) {
203
            $this->fk_receiver = trim($this->fk_receiver);
204
        }
205
        if (isset($this->justif_kilometers)) {
206
            $this->justif_kilometers = trim($this->justif_kilometers);
207
        }
208
        if (isset($this->passengerNames)) {
209
            $this->passengerNames = trim($this->passengerNames);
210
        }
211
212
        // Insert request
213
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
214
215
        $sql .= 'date,';
216
        $sql .= 'lieuD,';
217
        $sql .= 'lieuA,';
218
        $sql .= 'heureD,';
219
        $sql .= 'heureA,';
220
        $sql .= 'BBC_ballons_idBBC_ballons,';
221
        $sql .= 'nbrPax,';
222
        $sql .= 'remarque,';
223
        $sql .= 'incidents,';
224
        $sql .= 'fk_type,';
225
        $sql .= 'fk_pilot,';
226
        $sql .= 'fk_organisateur,';
227
        $sql .= 'is_facture,';
228
        $sql .= 'kilometers,';
229
        $sql .= 'cost,';
230
        $sql .= 'fk_receiver,';
231
        $sql .= 'justif_kilometers,';
232
        $sql .= 'date_creation,';
233
        $sql .= 'date_update,';
234
        $sql .= 'passenger_names';
235
        $sql .= ') VALUES (';
236
237
        $sql .= ' ' . (!isset($this->date) || dol_strlen($this->date) == 0 ? 'NULL' : "'" . $this->db->idate($this->date) . "'") . ',';
238
        $sql .= ' ' . (!isset($this->lieuD) ? 'NULL' : "'" . $this->db->escape($this->lieuD) . "'") . ',';
239
        $sql .= ' ' . (!isset($this->lieuA) ? 'NULL' : "'" . $this->db->escape($this->lieuA) . "'") . ',';
240
        $sql .= ' ' . (!isset($this->heureD) ? 'NULL' : "'" . $this->heureD . "'") . ',';
241
        $sql .= ' ' . (!isset($this->heureA) ? 'NULL' : "'" . $this->heureA . "'") . ',';
242
        $sql .= ' ' . (!isset($this->BBC_ballons_idBBC_ballons) ? 'NULL' : $this->BBC_ballons_idBBC_ballons) . ',';
243
        $sql .= ' ' . (!isset($this->nbrPax) ? 'NULL' : "'" . $this->db->escape($this->nbrPax) . "'") . ',';
244
        $sql .= ' ' . (!isset($this->remarque) ? 'NULL' : "'" . $this->db->escape($this->remarque) . "'") . ',';
245
        $sql .= ' ' . (!isset($this->incidents) ? 'NULL' : "'" . $this->db->escape($this->incidents) . "'") . ',';
246
        $sql .= ' ' . (!isset($this->fk_type) ? 'NULL' : $this->fk_type) . ',';
247
        $sql .= ' ' . (!isset($this->fk_pilot) ? 'NULL' : $this->fk_pilot) . ',';
248
        $sql .= ' ' . (!isset($this->fk_organisateur) ? 'NULL' : $this->fk_organisateur) . ',';
249
        $sql .= ' ' . (!isset($this->is_facture) ? '0' : $this->is_facture) . ',';
250
        $sql .= ' ' . (!isset($this->kilometers) || empty($this->kilometers) ? '0' : $this->kilometers) . ',';
251
        $sql .= ' ' . (!isset($this->cost) ? 'NULL' : "'" . $this->db->escape($this->cost) . "'") . ',';
252
        $sql .= ' ' . (!isset($this->fk_receiver) ? 'NULL' : $this->fk_receiver) . ',';
253
        $sql .= ' ' . (!isset($this->justif_kilometers) ? 'NULL' : "'" . $this->db->escape($this->justif_kilometers) . "'") . ',';
254
        $sql .= ' ' . "'" . date('Y-m-d H:i:s') . "'" . ',';
255
        $sql .= ' ' . "'" . date('Y-m-d H:i:s') . "'" . ',';
256
        $sql .= ' ' . "'" . $this->passengerNames . "'" . '';
257
258
        $sql .= ')';
259
260
        $this->db->begin();
261
262
        $resql = $this->db->query($sql);
263
        if (!$resql) {
264
            $error++;
265
            $this->errors[] = 'Error ' . $this->db->lasterror();
266
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
267
        }
268
269
        if (!$error) {
270
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
271
272
            try{
273
                $this->createLinksWithOrder($this->id);
274
275
                if (!$notrigger) {
276
                    $result = $this->call_trigger('BBC_FLIGHT_CREATED', $user);
277
                    if ($result < 0) {
278
                        $error++;
279
                    }
280
                }
281
            }catch(Exception $e){
282
                $error ++;
283
                $this->errors[] = 'Error ' . $e->getMessage();
284
                dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
285
            }
286
        }
287
288
        // Commit or rollback
289
        if ($error) {
290
            $this->db->rollback();
291
292
            return -1 * $error;
293
        }
294
295
        $this->db->commit();
296
        return $this->id;
297
    }
298
299
    /**
300
     * Load object in memory from the database
301
     *
302
     * @param int    $id  Id object
303
     * @param string $ref Ref
304
     *
305
     * @return int <0 if KO, 0 if not found, >0 if OK
306
     * @throws Exception
307
     */
308
    public function fetch($id, $ref = null)
309
    {
310
        dol_syslog(__METHOD__, LOG_DEBUG);
311
312
        $sql = 'SELECT';
313
        $sql .= " t.idBBC_vols,";
314
        $sql .= " t.date,";
315
        $sql .= " t.lieuD,";
316
        $sql .= " t.lieuA,";
317
        $sql .= " t.heureD,";
318
        $sql .= " t.heureA,";
319
        $sql .= " t.BBC_ballons_idBBC_ballons,";
320
        $sql .= " t.nbrPax,";
321
        $sql .= " t.remarque,";
322
        $sql .= " t.incidents,";
323
        $sql .= " t.fk_type,";
324
        $sql .= " t.fk_pilot,";
325
        $sql .= " t.fk_organisateur,";
326
        $sql .= " t.is_facture,";
327
        $sql .= " t.kilometers,";
328
        $sql .= " t.cost,";
329
        $sql .= " t.fk_receiver,";
330
        $sql .= " t.justif_kilometers,";
331
        $sql .= " t.date_creation,";
332
        $sql .= " t.date_update,";
333
        $sql .= " t.passenger_names";
334
335
336
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
337 View Code Duplication
        if (null !== $ref) {
0 ignored issues
show
Duplication introduced by
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...
338
            $sql .= ' WHERE t.ref = ' . '\'' . $ref . '\'';
339
        } else {
340
            $sql .= ' WHERE t.idBBC_vols = ' . $id;
341
        }
342
343
        $resql = $this->db->query($sql);
344
        if ($resql) {
345
            $numrows = $this->db->num_rows($resql);
346
            if ($numrows) {
347
                $obj = $this->db->fetch_object($resql);
348
349
                $this->id = $obj->idBBC_vols;
350
351
                $this->idBBC_vols = $obj->idBBC_vols;
352
                $this->date = $this->db->jdate($obj->date);
353
                $this->lieuD = $obj->lieuD;
354
                $this->lieuA = $obj->lieuA;
355
                $this->heureD = $obj->heureD;
356
                $this->heureA = $obj->heureA;
357
                $this->BBC_ballons_idBBC_ballons = $obj->BBC_ballons_idBBC_ballons;
358
                $this->nbrPax = $obj->nbrPax;
359
                $this->remarque = $obj->remarque;
360
                $this->incidents = $obj->incidents;
361
                $this->fk_type = $obj->fk_type;
362
                $this->fk_pilot = $obj->fk_pilot;
363
                $this->fk_organisateur = $obj->fk_organisateur;
364
                $this->is_facture = $obj->is_facture;
365
                $this->kilometers = $obj->kilometers;
366
                $this->cost = $obj->cost;
367
                $this->fk_receiver = $obj->fk_receiver;
368
                $this->justif_kilometers = $obj->justif_kilometers;
369
                $this->date_creation = $obj->date_creation;
370
                $this->date_update = $obj->date_update;
371
                $this->passengerNames = $obj->passenger_names;
372
373
                $this->balloon = $this->fetchBalloon();
374
                $this->pilot = $this->fetchUser($this->fk_pilot);
375
                $this->fetchOrder();
376
            }
377
            $this->db->free($resql);
378
379
            if ($numrows) {
380
                return 1;
381
            } else {
382
                return 0;
383
            }
384
        }
385
386
        $this->errors[] = 'Error ' . $this->db->lasterror();
387
        dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
388
        return -1;
389
    }
390
391
    /**
392
     * @param  User $user      User that modifies
393
     * @param  bool $notrigger false=launch triggers after, true=disable triggers
394
     *
395
     * @return int <0 if KO, >0 if OK
396
     * @throws Exception
397
     */
398
    public function update(User $user, $notrigger = false)
399
    {
400
        $error = 0;
401
402
        dol_syslog(__METHOD__, LOG_DEBUG);
403
404
        // Clean parameters
405
406
        if (isset($this->idBBC_vols)) {
407
            $this->idBBC_vols = trim($this->idBBC_vols);
408
        }
409
        if (isset($this->lieuD)) {
410
            $this->lieuD = trim($this->lieuD);
411
        }
412
        if (isset($this->lieuA)) {
413
            $this->lieuA = trim($this->lieuA);
414
        }
415
        if (isset($this->heureD)) {
416
            $this->heureD = trim($this->heureD);
417
        }
418
        if (isset($this->heureA)) {
419
            $this->heureA = trim($this->heureA);
420
        }
421
        if (isset($this->BBC_ballons_idBBC_ballons)) {
422
            $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons);
423
        }
424
        if (isset($this->nbrPax)) {
425
            $this->nbrPax = trim($this->nbrPax);
426
        }
427
        if (isset($this->remarque)) {
428
            $this->remarque = trim($this->remarque);
429
        }
430
        if (isset($this->incidents)) {
431
            $this->incidents = trim($this->incidents);
432
        }
433
        if (isset($this->fk_type)) {
434
            $this->fk_type = trim($this->fk_type);
435
        }
436
        if (isset($this->fk_pilot)) {
437
            $this->fk_pilot = trim($this->fk_pilot);
438
        }
439
        if (isset($this->fk_organisateur)) {
440
            $this->fk_organisateur = trim($this->fk_organisateur);
441
        }
442
        if (isset($this->is_facture)) {
443
            $this->is_facture = trim($this->is_facture);
444
        }
445
        if (isset($this->kilometers)) {
446
            $this->kilometers = trim($this->kilometers);
447
        }
448
        if (isset($this->cost)) {
449
            $this->cost = trim($this->cost);
450
        }
451
        if (isset($this->fk_receiver)) {
452
            $this->fk_receiver = trim($this->fk_receiver);
453
        }
454
        if (isset($this->justif_kilometers)) {
455
            $this->justif_kilometers = trim($this->justif_kilometers);
456
        }
457
        if (isset($this->passengerNames)) {
458
            $this->passengerNames = trim($this->passengerNames);
459
        }
460
461
        // Check parameters
462
        // Put here code to add a control on parameters values
463
464
        // Update request
465
        $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
466
467
        $sql .= ' date = ' . (!isset($this->date) || dol_strlen($this->date) != 0 ? "'" . $this->db->idate($this->date) . "'" : 'null') . ',';
468
        $sql .= ' lieuD = ' . (isset($this->lieuD) ? "'" . $this->db->escape($this->lieuD) . "'" : "null") . ',';
469
        $sql .= ' lieuA = ' . (isset($this->lieuA) ? "'" . $this->db->escape($this->lieuA) . "'" : "null") . ',';
470
        $sql .= ' heureD = ' . (isset($this->heureD) ? "'" . $this->heureD . "'" : "null") . ',';
471
        $sql .= ' heureA = ' . (isset($this->heureA) ? "'" . $this->heureA . "'" : "null") . ',';
472
        $sql .= ' BBC_ballons_idBBC_ballons = ' . (isset($this->BBC_ballons_idBBC_ballons) ? $this->BBC_ballons_idBBC_ballons : "null") . ',';
473
        $sql .= ' nbrPax = ' . (isset($this->nbrPax) ? "'" . $this->db->escape($this->nbrPax) . "'" : "null") . ',';
474
        $sql .= ' remarque = ' . (isset($this->remarque) ? "'" . $this->db->escape($this->remarque) . "'" : "null") . ',';
475
        $sql .= ' incidents = ' . (isset($this->incidents) ? "'" . $this->db->escape($this->incidents) . "'" : "null") . ',';
476
        $sql .= ' fk_type = ' . (isset($this->fk_type) ? $this->fk_type : "null") . ',';
477
        $sql .= ' fk_pilot = ' . (isset($this->fk_pilot) ? $this->fk_pilot : "null") . ',';
478
        $sql .= ' fk_organisateur = ' . (isset($this->fk_organisateur) ? $this->fk_organisateur : "null") . ',';
479
        $sql .= ' is_facture = ' . (isset($this->is_facture) ? $this->is_facture : "0") . ',';
480
        $sql .= ' kilometers = ' . (!empty($this->kilometers) ? $this->kilometers : "0") . ',';
481
        $sql .= ' cost = ' . (isset($this->cost) ? "'" . $this->db->escape($this->cost) . "'" : "''") . ',';
482
        $sql .= ' fk_receiver = ' . (isset($this->fk_receiver) ? $this->fk_receiver : "null") . ',';
483
        $sql .= ' justif_kilometers = ' . (isset($this->justif_kilometers) ? "'" . $this->db->escape($this->justif_kilometers) . "'," : "'',");
484
        $sql .= ' date_update = ' . "'" . date('Y-m-d H:i:s') . "',";
485
        $sql .= ' passenger_names = ' . "'" . trim($this->passengerNames) . "'";
486
487
        $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols;
488
489
        $this->db->begin();
490
491
        $resql = $this->db->query($sql);
492
        if (!$resql) {
493
            $error++;
494
            $this->errors[] = 'Error ' . $this->db->lasterror();
495
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
496
        }
497
498
        if (!$error && !$notrigger) {
499
            $result = $this->call_trigger('BBC_FLIGHT_UPDATED', $user);
500
            if ($result < 0) {
501
                $error++;
502
            }
503
        }
504
505
        // Commit or rollback
506
        if ($error) {
507
            $this->db->rollback();
508
509
            return -1 * $error;
510
        }
511
512
        $this->db->commit();
513
        return 1;
514
    }
515
516
    /**
517
     * Delete object in database
518
     *
519
     * @param User $user      User that deletes
520
     * @param bool $notrigger false=launch triggers after, true=disable triggers
521
     *
522
     * @return int <0 if KO, >0 if OK
523
     * @throws Exception
524
     */
525
    public function delete(User $user, $notrigger = false)
526
    {
527
        dol_syslog(__METHOD__, LOG_DEBUG);
528
529
        $error = 0;
530
531
        $this->db->begin();
532
533
        if (!$error) {
534
            if (!$notrigger) {
535
                $result = $this->call_trigger('BBC_FLIGHT_DELETED', $user);
536
                if ($result < 0) {
537
                    $error++;
538
                }
539
            }
540
        }
541
542 View Code Duplication
        if (!$error) {
0 ignored issues
show
Duplication introduced by
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...
543
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
544
            $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols;
545
546
            $resql = $this->db->query($sql);
547
            if (!$resql) {
548
                $error++;
549
                $this->errors[] = 'Error ' . $this->db->lasterror();
550
                dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
551
            }
552
        }
553
554
        // Commit or rollback
555
        if ($error) {
556
            $this->db->rollback();
557
            return -1 * $error;
558
        }
559
560
        $this->db->commit();
561
        return 1;
562
    }
563
564
    /**
565
     *  Return a link to the user card (with optionaly the picto)
566
     *    Use this->id,this->lastname, this->firstname
567
     *
568
     * @param    int     $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
569
     * @param    string  $option    On what the link point to
570
     * @param    integer $notooltip 1=Disable tooltip
571
     * @param    int     $maxlen    Max length of visible user name
572
     * @param  string    $morecss   Add more css on link
573
     *
574
     * @return    string                        String with URL
575
     */
576
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
577
    {
578
        global $langs;
579
580
        $result = '';
581
582
        $label = '<u>' . $langs->trans("MyModule") . '</u>';
583
        $label .= '<div width="100%">';
584
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->idBBC_vols . '<br>';
585
        $label .= '<b>' . $langs->trans('Date') . ':</b> ' . dol_print_date($this->date, '%d-%m-%Y') . '<br/>';
586
        $label .= '<b>' . $langs->trans('From') . ':</b> ' . $this->lieuD . '<br/>';
587
        $label .= '<b>' . $langs->trans('To') . ':</b> ' . $this->lieuA . '<br/>';
588
        $label .= '</div>';
589
590
        $link = '<a href="' . DOL_URL_ROOT . '/flightlog/card.php?id=' . $this->idBBC_vols . '"';
591
        $link .= ($notooltip ? '' : ' title="' . dol_escape_htmltag($label,
592
                1) . '" class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"');
593
        $link .= '>';
594
        $linkend = '</a>';
595
596 View Code Duplication
        if ($withpicto) {
0 ignored issues
show
Duplication introduced by
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...
597
            $result .= ($link . img_object(($notooltip ? '' : $label), 'label',
598
                    ($notooltip ? '' : 'class="classfortooltip"')) . $linkend);
599
            if ($withpicto != 2) {
600
                $result .= ' ';
601
            }
602
        }
603
        $result .= $link . $this->idBBC_vols . $linkend;
604
        return $result;
605
    }
606
607
    /**
608
     *  Retourne le libelle du status d'un user (actif, inactif)
609
     *
610
     * @param    int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
611
     *
612
     * @return    string                   Label of status
613
     */
614
    public function getLibStatut($mode = 0)
615
    {
616
        return $this->LibStatut($this->is_facture, $mode);
617
    }
618
619
    /**
620
     * Renvoi le libelle d'un status donne
621
     *
622
     * @param int $status Id status
623
     * @param int $mode   0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
624
     *
625
     * @return string                    Label of status
626
     */
627
    private function LibStatut($status, $mode = 0)
628
    {
629
        global $langs;
630
631
        $billDone = $langs->trans('Facturé');
632
        $billNotDone = $langs->trans('Ouvert');
633
634 View Code Duplication
        if ($mode == 0) {
0 ignored issues
show
Duplication introduced by
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...
635
            if ($status == 1) {
636
                return $billDone;
637
            }
638
            if ($status == 0) {
639
                return $billNotDone;
640
            }
641
        }
642 View Code Duplication
        if ($mode == 1) {
0 ignored issues
show
Duplication introduced by
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...
643
            if ($status == 1) {
644
                return $billDone;
645
            }
646
            if ($status == 0) {
647
                return $billNotDone;
648
            }
649
        }
650 View Code Duplication
        if ($mode == 2) {
0 ignored issues
show
Duplication introduced by
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...
651
            if ($status == 1) {
652
                return img_picto($billDone, 'statut4') . ' ' . $billDone;
653
            }
654
            if ($status == 0) {
655
                return img_picto($billNotDone, 'statut5') . ' ' . $billNotDone;
656
            }
657
        }
658
        if ($mode == 3) {
659
            if ($status == 1) {
660
                return img_picto($billDone, 'statut4');
661
            }
662
            if ($status == 0) {
663
                return img_picto($billNotDone, 'statut5');
664
            }
665
        }
666 View Code Duplication
        if ($mode == 4) {
0 ignored issues
show
Duplication introduced by
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...
667
            if ($status == 1) {
668
                return img_picto($billDone, 'statut4') . ' ' . $billDone;
669
            }
670
            if ($status == 0) {
671
                return img_picto($billNotDone, 'statut5') . ' ' . $billNotDone;
672
            }
673
        }
674 View Code Duplication
        if ($mode == 5) {
0 ignored issues
show
Duplication introduced by
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...
675
            if ($status == 1) {
676
                return $billDone . ' ' . img_picto($billDone, 'statut4');
677
            }
678
            if ($status == 0) {
679
                return $billNotDone . ' ' . img_picto($billNotDone, 'statut5');
680
            }
681
        }
682
683
        return "";
684
    }
685
686
    /**
687
     * @return string
688
     */
689
    public function __toString()
690
    {
691
        return $this->idBBC_vols . " " . $this->getPlaces();
692
    }
693
694
    /**
695
     * @return string
696
     */
697
    public function toString()
698
    {
699
        return "" . $this;
700
    }
701
702
    /**
703
     * @return string
704
     */
705
    public function getStatus()
706
    {
707
        return $this->statut;
708
    }
709
710
    /**
711
     * @return boolean
712
     */
713
    public function hasFacture()
714
    {
715
        return count($this->linkedObjectsIds) > 0;
716
    }
717
718
    /**
719
     * @param int $userId
720
     *
721
     * @return User
722
     */
723
    private function fetchUser($userId)
724
    {
725
        $user = new User($this->db);
726
        $user->fetch($userId);
727
728
        return $user;
729
    }
730
731
    /**
732
     * @return Bbc_ballons
733
     */
734
    private function fetchBalloon()
735
    {
736
        $balloon = new Bbc_ballons($this->db);
737
        $balloon->fetch($this->BBC_ballons_idBBC_ballons);
738
739
        return $balloon;
740
    }
741
742
    /**
743
     * @return Bbc_ballons
744
     */
745
    public function getBalloon()
746
    {
747
        if (!$this->balloon) {
748
            $this->balloon = $this->fetchBalloon();
749
        }
750
751
        return $this->balloon;
752
    }
753
754
    /**
755
     * @return User
756
     */
757
    public function getPilot()
758
    {
759
        if (!$this->pilot) {
760
            $this->pilot = $this->fetchUser($this->fk_pilot);
761
        }
762
763
        return $this->pilot;
764
    }
765
766
    /**
767
     * @return int
768
     */
769
    public function getPilotId()
770
    {
771
        return (int) $this->fk_pilot;
772
    }
773
774
    /**
775
     * @return int
776
     */
777
    public function getOrganisatorId()
778
    {
779
        return (int) $this->fk_organisateur;
780
    }
781
782
    /**
783
     * @return Bbctypes
784
     */
785
    public function getFlightType()
786
    {
787
        $flightType = new Bbctypes($this->db);
788
        $flightType->fetch($this->fk_type);
789
790
        return $flightType;
791
    }
792
793
    /**
794
     * @return string
795
     */
796
    public function getComment()
797
    {
798
        return $this->remarque;
799
    }
800
801
    /**
802
     * @return string
803
     */
804
    public function getIncident()
805
    {
806
        return $this->incidents;
807
    }
808
809
    /**
810
     * Return true if the number of pax is greater than 0
811
     *
812
     * @return boolean
813
     */
814
    public function hasPax()
815
    {
816
        return (int) $this->nbrPax > 0;
817
    }
818
819
    /**
820
     * Regarding the type of the flight give an indication if the flight must have pax to be valid.
821
     *
822
     * @return boolean
823
     */
824
    public function mustHavePax()
825
    {
826
        return $this->getFlightType()->isPaxRequired();
827
    }
828
829
    /**
830
     * Returns true if the amount requested by the flight is 0.
831
     *
832
     * @return boolean
833
     */
834
    public function isFree()
835
    {
836
        return empty($this->cost);
837
    }
838
839
    /**
840
     * @return int
841
     */
842
    public function getAmountReceived()
843
    {
844
        return $this->cost;
845
    }
846
847
    /**
848
     * @return int
849
     */
850
    public function getAmountPerPassenger()
851
    {
852
        return $this->cost / $this->nbrPax;
853
    }
854
855
    /**
856
     * @return boolean
857
     */
858
    public function hasReceiver()
859
    {
860
        return !empty($this->fk_receiver);
861
    }
862
863
    /**
864
     * @return boolean
865
     */
866
    public function hasKilometers()
867
    {
868
        return !empty($this->kilometers);
869
    }
870
871
    /**
872
     * @return boolean
873
     */
874
    public function hasKilometersDescription()
875
    {
876
        return !empty(trim($this->justif_kilometers));
877
    }
878
879
    /**
880
     * @return int
881
     */
882
    public function getKilometers()
883
    {
884
        return (int) $this->kilometers;
885
    }
886
887
    /**
888
     * @return string
889
     */
890
    public function getPlaces()
891
    {
892
        return $this->lieuD . ' -> ' . $this->lieuA;
893
    }
894
895
    /**
896
     * @return string
897
     */
898
    public function getDescription()
899
    {
900
        return $this->__toString() . ' - ' . $this->passengerNames;
901
    }
902
903
    /**
904
     * @return string
905
     */
906
    public function getPassengerNames()
907
    {
908
        return $this->passengerNames;
909
    }
910
911
    /**
912
     * @param string $passengerNames
913
     *
914
     * @return Bbcvols
915
     */
916
    public function setPassengerNames($passengerNames)
917
    {
918
        $this->passengerNames = $passengerNames;
919
        return $this;
920
    }
921
922
    /**
923
     * @return int
924
     */
925
    public function getNumberOfPassengers()
926
    {
927
        return (int) $this->nbrPax;
928
    }
929
930
    /**
931
     * @return int[]|array
932
     */
933
    public function getOrderIds()
934
    {
935
        if(empty($this->orderIds)){
936
            $sql = sprintf('SELECT id, order_id FROM %s WHERE flight_id = %s', MAIN_DB_PREFIX . 'bbc_flights_orders' ,$this->getId());
937
            $resql = $this->db->query($sql);
938
            if ($resql) {
939
                $num = $this->db->num_rows($resql);
940
                $i = 0;
941
                if ($num) {
942
                    while ($i < $num) {
943
                        $obj = $this->db->fetch_object($resql);
944
                        $this->orderIds[] = $obj->order_id;
945
                        $i++;
946
                    }
947
                }
948
            }
949
        }
950
951
        return $this->orderIds;
952
    }
953
954
    /**
955
     * @param int[]|array $orderIds
956
     *
957
     * @return Bbcvols
958
     */
959
    public function setOrderIds($orderIds)
960
    {
961
        $this->orderIds = $orderIds;
962
        return $this;
963
    }
964
965
    /**
966
     * Is an instruction flight (T6/T7)
967
     */
968
    public function isInstruction()
969
    {
970
        return $this->getFlightType()->isInstruction();
971
    }
972
973
    /**
974
     * @return bool
975
     */
976
    public function isLinkedToOrder()
977
    {
978
        return !empty($this->orderIds);
979
    }
980
981
    /**
982
     * Fetch the order based on the order id.
983
     */
984
    private function fetchOrder()
985
    {
986
        foreach ($this->getOrderIds() as $currentOrderId) {
987
            $order = new Commande($this->db);
988
            $order->fetch((int) $currentOrderId);
989
990
            $this->orders[] = $order;
991
        }
992
993
994
        return $this;
995
    }
996
997
    /**
998
     * @return Commande[]|array
999
     */
1000
    public function getOrders()
1001
    {
1002
        if(empty($this->orders)){
1003
            $this->fetchOrder();
1004
        }
1005
1006
        return $this->orders;
1007
    }
1008
1009
    /**
1010
     * Flag the flight as billed
1011
     *
1012
     * @return $this
1013
     */
1014
    public function bill()
1015
    {
1016
        $this->is_facture = true;
1017
        return $this;
1018
    }
1019
1020
    /**
1021
     * @return boolean
1022
     */
1023
    public function isBilled()
1024
    {
1025
        return !empty($this->is_facture);
1026
    }
1027
1028
    /**
1029
     * @param int $id
1030
     *
1031
     * @throws Exception
1032
     */
1033
    private function createLinksWithOrder($id)
1034
    {
1035
1036
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . 'bbc_flights_orders' . '(';
1037
1038
        $sql .= 'order_id,';
1039
        $sql .= 'flight_id';
1040
1041
        $sql .= ') VALUES ';
1042
1043
        $i = 0;
1044
        foreach ($this->orderIds as $orderId){
1045
            $sql .= '(';
1046
            $sql .= $orderId.', ';
1047
            $sql .= $id;
1048
            $sql .= ')';
1049
1050
            $i++;
1051
            if(count($this->orderIds) !== $i){
1052
                $sql.=',';
1053
            }
1054
        }
1055
1056
        $resql = $this->db->query($sql);
1057
        if (!$resql) {
1058
            throw new Exception($this->db->lasterror());
1059
        }
1060
1061
    }
1062
1063
}
1064