Completed
Push — feature/multi_order ( eb7707 )
by Laurent
01:37
created

Bbcvols::getOrderId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
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
    /**
46
     * @var string Id to identify managed objects
47
     */
48
    public $element = 'flightlog_bbcvols';
49
50
    /**
51
     * @var string Name of table without prefix where object is stored
52
     */
53
    public $table_element = 'bbc_vols';
54
55
    public $idBBC_vols;
56
    public $date = '';
57
    public $lieuD;
58
    public $lieuA;
59
    public $heureD;
60
    public $heureA;
61
    public $BBC_ballons_idBBC_ballons;
62
    public $nbrPax;
63
    public $remarque;
64
    public $incidents;
65
    public $fk_type;
66
    public $fk_pilot;
67
    public $fk_organisateur;
68
    public $is_facture;
69
    public $kilometers;
70
    public $cost;
71
    public $fk_receiver;
72
    public $justif_kilometers;
73
    public $date_creation;
74
    public $date_update;
75
76
    /**
77
     * @var Bbc_ballons
78
     */
79
    private $balloon;
80
81
    /**
82
     * @var User
83
     */
84
    private $pilot;
85
86
    /**
87
     * @var string
88
     */
89
    private $passengerNames;
90
91
    /**
92
     * @var int[]|array
93
     */
94
    private $orderIds;
95
96
    /**
97
     * @var Commande[]|array
98
     */
99
    private $orders;
100
101
    /**
102
     * @return int
103
     */
104
    public function getIdBBCVols()
105
    {
106
        return (int) $this->idBBC_vols;
107
    }
108
109
    /**
110
     * @return int
111
     */
112
    public function getId()
113
    {
114
        return (int) $this->getIdBBCVols();
115
    }
116
117
    /**
118
     * @param string|int $ref
119
     *
120
     * @return $this
121
     */
122
    public function setRef($ref)
123
    {
124
        $this->ref = $ref;
125
        return $this;
126
    }
127
128
    /**
129
     * Constructor
130
     *
131
     * @param DoliDb $db Database handler
132
     */
133
    public function __construct(DoliDB $db)
134
    {
135
        $this->db = $db;
136
        $this->cost = 0;
137
138
        $this->passengerNames = '';
139
        $this->orderIds = [];
140
    }
141
142
    /**
143
     * Create a flight
144
     *
145
     * @param  User $user      User that creates
146
     * @param  bool $notrigger false=launch triggers after, true=disable triggers
147
     *
148
     * @return int <0 if KO, Id of created object if OK
149
     * @throws Exception
150
     */
151
    public function create(User $user, $notrigger = false)
152
    {
153
        dol_syslog(__METHOD__, LOG_DEBUG);
154
155
        $error = 0;
156
157
        // Clean parameters
158
159
        if (isset($this->idBBC_vols)) {
160
            $this->idBBC_vols = trim($this->idBBC_vols);
161
        }
162
        if (isset($this->lieuD)) {
163
            $this->lieuD = trim($this->lieuD);
164
        }
165
        if (isset($this->lieuA)) {
166
            $this->lieuA = trim($this->lieuA);
167
        }
168
        if (isset($this->heureD)) {
169
            $this->heureD = trim($this->heureD) . '00';
170
        }
171
        if (isset($this->heureA)) {
172
            $this->heureA = trim($this->heureA) . '00';
173
        }
174
        if (isset($this->BBC_ballons_idBBC_ballons)) {
175
            $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons);
176
        }
177
        if (isset($this->nbrPax)) {
178
            $this->nbrPax = trim($this->nbrPax);
179
        }
180
        if (isset($this->remarque)) {
181
            $this->remarque = trim($this->remarque);
182
        }
183
        if (isset($this->incidents)) {
184
            $this->incidents = trim($this->incidents);
185
        }
186
        if (isset($this->fk_type)) {
187
            $this->fk_type = trim($this->fk_type);
188
        }
189
        if (isset($this->fk_pilot)) {
190
            $this->fk_pilot = trim($this->fk_pilot);
191
        }
192
        if (isset($this->fk_organisateur)) {
193
            $this->fk_organisateur = trim($this->fk_organisateur);
194
        }
195
        if (isset($this->is_facture)) {
196
            $this->is_facture = trim($this->is_facture);
197
        }
198
        if (isset($this->kilometers)) {
199
            $this->kilometers = trim($this->kilometers);
200
        }
201
        if (isset($this->cost)) {
202
            $this->cost = trim($this->cost);
203
        }
204
        if (isset($this->fk_receiver)) {
205
            $this->fk_receiver = trim($this->fk_receiver);
206
        }
207
        if (isset($this->justif_kilometers)) {
208
            $this->justif_kilometers = trim($this->justif_kilometers);
209
        }
210
        if (isset($this->passengerNames)) {
211
            $this->passengerNames = trim($this->passengerNames);
212
        }
213
214
        // Insert request
215
        $sql = 'INSERT INTO ' . MAIN_DB_PREFIX . $this->table_element . '(';
216
217
        $sql .= 'date,';
218
        $sql .= 'lieuD,';
219
        $sql .= 'lieuA,';
220
        $sql .= 'heureD,';
221
        $sql .= 'heureA,';
222
        $sql .= 'BBC_ballons_idBBC_ballons,';
223
        $sql .= 'nbrPax,';
224
        $sql .= 'remarque,';
225
        $sql .= 'incidents,';
226
        $sql .= 'fk_type,';
227
        $sql .= 'fk_pilot,';
228
        $sql .= 'fk_organisateur,';
229
        $sql .= 'is_facture,';
230
        $sql .= 'kilometers,';
231
        $sql .= 'cost,';
232
        $sql .= 'fk_receiver,';
233
        $sql .= 'justif_kilometers,';
234
        $sql .= 'date_creation,';
235
        $sql .= 'date_update,';
236
        $sql .= 'passenger_names';
237
        $sql .= ') VALUES (';
238
239
        $sql .= ' ' . (!isset($this->date) || dol_strlen($this->date) == 0 ? 'NULL' : "'" . $this->db->idate($this->date) . "'") . ',';
240
        $sql .= ' ' . (!isset($this->lieuD) ? 'NULL' : "'" . $this->db->escape($this->lieuD) . "'") . ',';
241
        $sql .= ' ' . (!isset($this->lieuA) ? 'NULL' : "'" . $this->db->escape($this->lieuA) . "'") . ',';
242
        $sql .= ' ' . (!isset($this->heureD) ? 'NULL' : "'" . $this->heureD . "'") . ',';
243
        $sql .= ' ' . (!isset($this->heureA) ? 'NULL' : "'" . $this->heureA . "'") . ',';
244
        $sql .= ' ' . (!isset($this->BBC_ballons_idBBC_ballons) ? 'NULL' : $this->BBC_ballons_idBBC_ballons) . ',';
245
        $sql .= ' ' . (!isset($this->nbrPax) ? 'NULL' : "'" . $this->db->escape($this->nbrPax) . "'") . ',';
246
        $sql .= ' ' . (!isset($this->remarque) ? 'NULL' : "'" . $this->db->escape($this->remarque) . "'") . ',';
247
        $sql .= ' ' . (!isset($this->incidents) ? 'NULL' : "'" . $this->db->escape($this->incidents) . "'") . ',';
248
        $sql .= ' ' . (!isset($this->fk_type) || (int) $this->fk_type === -1 ? 'NULL' : $this->fk_type) . ',';
249
        $sql .= ' ' . (!isset($this->fk_pilot) || (int) $this->fk_pilot === -1 ? 'NULL' : $this->fk_pilot) . ',';
250
        $sql .= ' ' . (!isset($this->fk_organisateur) || (int) $this->fk_organisateur === -1 ? 'NULL' : $this->fk_organisateur) . ',';
251
        $sql .= ' ' . (!isset($this->is_facture) ? '0' : $this->is_facture) . ',';
252
        $sql .= ' ' . (!isset($this->kilometers) || empty($this->kilometers) ? '0' : $this->kilometers) . ',';
253
        $sql .= ' ' . (!isset($this->cost) ? 'NULL' : "'" . $this->db->escape($this->cost) . "'") . ',';
254
        $sql .= ' ' . (!isset($this->fk_receiver) || (int) $this->fk_receiver === -1 ? 'NULL' : $this->fk_receiver) . ',';
255
        $sql .= ' ' . (!isset($this->justif_kilometers) ? 'NULL' : "'" . $this->db->escape($this->justif_kilometers) . "'") . ',';
256
        $sql .= ' ' . "'" . date('Y-m-d H:i:s') . "'" . ',';
257
        $sql .= ' ' . "'" . date('Y-m-d H:i:s') . "'" . ',';
258
        $sql .= ' ' . "'" . $this->passengerNames . "'" ;
259
        $sql .= ')';
260
261
        $this->db->begin();
262
263
        $resql = $this->db->query($sql);
264
        if (!$resql) {
265
            $error++;
266
            $this->errors[] = 'Error ' . $this->db->lasterror();
267
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
268
        }
269
270
        if (!$error) {
271
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . $this->table_element);
272
            if(!$this->insertOrders()){
273
                $error++;
274
            }
275
276
            if (!$notrigger) {
277
                $result = $this->call_trigger('BBC_FLIGHT_CREATED', $user);
278
                if ($result < 0) {
279
                    $error++;
280
                }
281
            }
282
        }
283
284
        // Commit or rollback
285
        if ($error) {
286
            $this->db->rollback();
287
288
            return -1 * $error;
289
        }
290
291
        $this->db->commit();
292
        return $this->id;
293
    }
294
295
    /**
296
     * Inserts the order linked to the flight
297
     */
298
    private function insertOrders(){
299
300
        if(empty($this->orderIds)){
301
            return true;
302
        }
303
304
        $valueTemplate = '(%s, %s, %s)';
305
        
306
        // Insert request
307
        $sql = 'INSERT INTO llx_bbc_vols_orders(';
308
        $sql .= 'order_id,';
309
        $sql .= 'flight_id,';
310
        $sql .= 'nbr_passengers';
311
        $sql .= ') VALUES %s';
312
        
313
        $values = [];
314
315
        foreach($this->orderIds as $orderId=>$nbrPassengers){
316
            $values[] = sprintf(
317
                $valueTemplate,
318
                $orderId,
319
                $this->db->escape($this->id),
320
                (!isset($nbrPassengers) ? '0' :  $this->db->escape($nbrPassengers))
321
            );
322
        }
323
324
        $resql = $this->db->query(sprintf($sql, implode(',', $values)));
325
326 View Code Duplication
        if (!$resql) {
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...
327
            $this->errors[] = 'Error ' . $this->db->lasterror();
328
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
329
            return false;
330
        }
331
332
        return true;
333
    }
334
335
    /**
336
     * Inserts the order linked to the flight
337
     */
338
    private function updateOrders(){
339
340
        if(empty($this->orderIds)){
341
            return true;
342
        }
343
344
        $this->deleteOrders();
345
        $this->insertOrders();
346
    }
347
348
    /**
349
     * Inserts the order linked to the flight
350
     */
351
    private function deleteOrders(){
352
353
        if(empty($this->orderIds)){
354
            return true;
355
        }
356
357
        $sql = 'DELETE FROM `llx_bbc_vols_orders` WHERE flight_id = %s';
358
        $resql = $this->db->query(sprintf($sql, $this->db->escape($this->id)));
359
360 View Code Duplication
        if (!$resql) {
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...
361
            $this->errors[] = 'Error ' . $this->db->lasterror();
362
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
363
            return false;
364
        }
365
366
        return true;
367
    }
368
369
    /**
370
     * Load object in memory from the database
371
     *
372
     * @param int    $id  Id object
373
     * @param string $ref Ref
374
     *
375
     * @return int <0 if KO, 0 if not found, >0 if OK
376
     * @throws Exception
377
     */
378
    public function fetch($id, $ref = null)
379
    {
380
        dol_syslog(__METHOD__, LOG_DEBUG);
381
382
        $sql = 'SELECT';
383
        $sql .= " t.idBBC_vols,";
384
        $sql .= " t.date,";
385
        $sql .= " t.lieuD,";
386
        $sql .= " t.lieuA,";
387
        $sql .= " t.heureD,";
388
        $sql .= " t.heureA,";
389
        $sql .= " t.BBC_ballons_idBBC_ballons,";
390
        $sql .= " t.nbrPax,";
391
        $sql .= " t.remarque,";
392
        $sql .= " t.incidents,";
393
        $sql .= " t.fk_type,";
394
        $sql .= " t.fk_pilot,";
395
        $sql .= " t.fk_organisateur,";
396
        $sql .= " t.is_facture,";
397
        $sql .= " t.kilometers,";
398
        $sql .= " t.cost,";
399
        $sql .= " t.fk_receiver,";
400
        $sql .= " t.justif_kilometers,";
401
        $sql .= " t.date_creation,";
402
        $sql .= " t.date_update,";
403
        $sql .= " t.passenger_names";
404
405
406
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
407 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...
408
            $sql .= ' WHERE t.ref = ' . '\'' . $ref . '\'';
409
        } else {
410
            $sql .= ' WHERE t.idBBC_vols = ' . $id;
411
        }
412
413
        $resql = $this->db->query($sql);
414
        if ($resql) {
415
            $numrows = $this->db->num_rows($resql);
416
            if ($numrows) {
417
                $obj = $this->db->fetch_object($resql);
418
419
                $this->id = $obj->idBBC_vols;
420
421
                $this->idBBC_vols = (int) $obj->idBBC_vols;
422
                $this->date = $this->db->jdate($obj->date);
423
                $this->lieuD = $obj->lieuD;
424
                $this->lieuA = $obj->lieuA;
425
                $this->heureD = $obj->heureD;
426
                $this->heureA = $obj->heureA;
427
                $this->BBC_ballons_idBBC_ballons = (int) $obj->BBC_ballons_idBBC_ballons;
428
                $this->nbrPax = $obj->nbrPax;
429
                $this->remarque = $obj->remarque;
430
                $this->incidents = $obj->incidents;
431
                $this->fk_type = (int) $obj->fk_type;
432
                $this->fk_pilot = (int) $obj->fk_pilot;
433
                $this->fk_organisateur = (int) $obj->fk_organisateur;
434
                $this->is_facture = (int) $obj->is_facture;
435
                $this->kilometers = $obj->kilometers;
436
                $this->cost = $obj->cost;
437
                $this->fk_receiver = (int) $obj->fk_receiver;
438
                $this->justif_kilometers = $obj->justif_kilometers;
439
                $this->date_creation = $obj->date_creation;
440
                $this->date_update = $obj->date_update;
441
                $this->passengerNames = $obj->passenger_names;
442
443
                $this->balloon = $this->fetchBalloon();
444
                $this->pilot = $this->fetchUser($this->fk_pilot);
445
                $this->fetchOrder();
446
            }
447
            $this->db->free($resql);
448
449
            if ($numrows) {
450
                return 1;
451
            } else {
452
                return 0;
453
            }
454
        }
455
456
        $this->errors[] = 'Error ' . $this->db->lasterror();
457
        dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
458
        return -1;
459
    }
460
461
    /**
462
     * @param  User $user      User that modifies
463
     * @param  bool $notrigger false=launch triggers after, true=disable triggers
464
     *
465
     * @return int <0 if KO, >0 if OK
466
     * @throws Exception
467
     */
468
    public function update(User $user, $notrigger = false)
469
    {
470
        $error = 0;
471
472
        dol_syslog(__METHOD__, LOG_DEBUG);
473
474
        // Clean parameters
475
476
        if (isset($this->idBBC_vols)) {
477
            $this->idBBC_vols = trim($this->idBBC_vols);
478
        }
479
        if (isset($this->lieuD)) {
480
            $this->lieuD = trim($this->lieuD);
481
        }
482
        if (isset($this->lieuA)) {
483
            $this->lieuA = trim($this->lieuA);
484
        }
485
        if (isset($this->heureD)) {
486
            $this->heureD = trim($this->heureD);
487
        }
488
        if (isset($this->heureA)) {
489
            $this->heureA = trim($this->heureA);
490
        }
491
        if (isset($this->BBC_ballons_idBBC_ballons)) {
492
            $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons);
493
        }
494
        if (isset($this->nbrPax)) {
495
            $this->nbrPax = trim($this->nbrPax);
496
        }
497
        if (isset($this->remarque)) {
498
            $this->remarque = trim($this->remarque);
499
        }
500
        if (isset($this->incidents)) {
501
            $this->incidents = trim($this->incidents);
502
        }
503
        if (isset($this->fk_type)) {
504
            $this->fk_type = trim($this->fk_type);
505
        }
506
        if (isset($this->fk_pilot)) {
507
            $this->fk_pilot = trim($this->fk_pilot);
508
        }
509
        if (isset($this->fk_organisateur)) {
510
            $this->fk_organisateur = trim($this->fk_organisateur);
511
        }
512
        if (isset($this->is_facture)) {
513
            $this->is_facture = trim($this->is_facture);
514
        }
515
        if (isset($this->kilometers)) {
516
            $this->kilometers = trim($this->kilometers);
517
        }
518
        if (isset($this->cost)) {
519
            $this->cost = trim($this->cost);
520
        }
521
        if (isset($this->fk_receiver)) {
522
            $this->fk_receiver = trim($this->fk_receiver);
523
        }
524
        if (isset($this->justif_kilometers)) {
525
            $this->justif_kilometers = trim($this->justif_kilometers);
526
        }
527
        if (isset($this->passengerNames)) {
528
            $this->passengerNames = trim($this->passengerNames);
529
        }
530
531
532
        // Check parameters
533
        // Put here code to add a control on parameters values
534
535
        // Update request
536
        $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
537
538
        $sql .= ' date = ' . (!isset($this->date) || dol_strlen($this->date) != 0 ? "'" . $this->db->idate($this->date) . "'" : 'null') . ',';
539
        $sql .= ' lieuD = ' . (isset($this->lieuD) ? "'" . $this->db->escape($this->lieuD) . "'" : "null") . ',';
540
        $sql .= ' lieuA = ' . (isset($this->lieuA) ? "'" . $this->db->escape($this->lieuA) . "'" : "null") . ',';
541
        $sql .= ' heureD = ' . (isset($this->heureD) ? "'" . $this->heureD . "'" : "null") . ',';
542
        $sql .= ' heureA = ' . (isset($this->heureA) ? "'" . $this->heureA . "'" : "null") . ',';
543
        $sql .= ' BBC_ballons_idBBC_ballons = ' . (isset($this->BBC_ballons_idBBC_ballons) ? $this->BBC_ballons_idBBC_ballons : "null") . ',';
544
        $sql .= ' nbrPax = ' . (isset($this->nbrPax) ? "'" . $this->db->escape($this->nbrPax) . "'" : "null") . ',';
545
        $sql .= ' remarque = ' . (isset($this->remarque) ? "'" . $this->db->escape($this->remarque) . "'" : "null") . ',';
546
        $sql .= ' incidents = ' . (isset($this->incidents) ? "'" . $this->db->escape($this->incidents) . "'" : "null") . ',';
547
        $sql .= ' fk_type = ' . (isset($this->fk_type) && (int) $this->fk_type > 0 ? $this->fk_type : "null") . ',';
548
        $sql .= ' fk_pilot = ' . (isset($this->fk_pilot) && (int) $this->fk_pilot > 0 ? $this->fk_pilot : "null") . ',';
549
        $sql .= ' fk_organisateur = ' . (isset($this->fk_organisateur) && (int) $this->fk_organisateur > 0 ? $this->fk_organisateur : "null") . ',';
550
        $sql .= ' is_facture = ' . (isset($this->is_facture) ? $this->is_facture : "0") . ',';
551
        $sql .= ' kilometers = ' . (!empty($this->kilometers) ? $this->kilometers : "0") . ',';
552
        $sql .= ' cost = ' . (isset($this->cost) ? "'" . $this->db->escape($this->cost) . "'" : "''") . ',';
553
        $sql .= ' fk_receiver = ' . (isset($this->fk_receiver) && (int) $this->fk_receiver > 0 ? $this->fk_receiver : "null") . ',';
554
        $sql .= ' justif_kilometers = ' . (isset($this->justif_kilometers) ? "'" . $this->db->escape($this->justif_kilometers) . "'," : "'',");
555
        $sql .= ' date_update = ' . "'" . date('Y-m-d H:i:s') . "',";
556
        $sql .= ' passenger_names = ' . "'" . trim($this->passengerNames);
557
558
        $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols;
559
560
        $this->db->begin();
561
562
        $resql = $this->db->query($sql);
563
        if (!$resql) {
564
            $error++;
565
            $this->errors[] = 'Error ' . $this->db->lasterror();
566
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
567
        }
568
569
        if (!$error && !$notrigger) {
570
            $result = $this->call_trigger('BBC_FLIGHT_UPDATED', $user);
571
            if ($result < 0) {
572
                $error++;
573
            }
574
        }
575
576
        // Commit or rollback
577
        if ($error) {
578
            $this->db->rollback();
579
580
            return -1 * $error;
581
        }
582
583
        $this->updateOrders();
584
585
        $this->db->commit();
586
        return 1;
587
    }
588
589
    /**
590
     * Delete object in database
591
     *
592
     * @param User $user      User that deletes
593
     * @param bool $notrigger false=launch triggers after, true=disable triggers
594
     *
595
     * @return int <0 if KO, >0 if OK
596
     * @throws Exception
597
     */
598
    public function delete(User $user, $notrigger = false)
599
    {
600
        dol_syslog(__METHOD__, LOG_DEBUG);
601
602
        $error = 0;
603
604
        $this->db->begin();
605
606
        if (!$error) {
607
            if (!$notrigger) {
608
                $result = $this->call_trigger('BBC_FLIGHT_DELETED', $user);
609
                if ($result < 0) {
610
                    $error++;
611
                }
612
            }
613
        }
614
615 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...
616
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
617
            $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols;
618
619
            $resql = $this->db->query($sql);
620
            if (!$resql) {
621
                $error++;
622
                $this->errors[] = 'Error ' . $this->db->lasterror();
623
                dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
624
            }
625
        }
626
627
        // Commit or rollback
628
        if ($error) {
629
            $this->db->rollback();
630
            return -1 * $error;
631
        }
632
633
        $this->db->commit();
634
        return 1;
635
    }
636
637
    /**
638
     *  Return a link to the user card (with optionaly the picto)
639
     *    Use this->id,this->lastname, this->firstname
640
     *
641
     * @param    int     $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
642
     * @param    string  $option    On what the link point to
643
     * @param    integer $notooltip 1=Disable tooltip
644
     * @param    int     $maxlen    Max length of visible user name
645
     * @param  string    $morecss   Add more css on link
646
     *
647
     * @return    string                        String with URL
648
     */
649
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
650
    {
651
        global $langs;
652
653
        $result = '';
654
655
        $label = '<u>' . $langs->trans("MyModule") . '</u>';
656
        $label .= '<div width="100%">';
657
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->idBBC_vols . '<br>';
658
        $label .= '<b>' . $langs->trans('Date') . ':</b> ' . dol_print_date($this->date, '%d-%m-%Y') . '<br/>';
659
        $label .= '<b>' . $langs->trans('From') . ':</b> ' . $this->lieuD . '<br/>';
660
        $label .= '<b>' . $langs->trans('To') . ':</b> ' . $this->lieuA . '<br/>';
661
        $label .= '</div>';
662
663
        $link = '<a href="' . DOL_URL_ROOT . '/flightlog/card.php?id=' . $this->idBBC_vols . '"';
664
        $link .= ($notooltip ? '' : ' title="' . dol_escape_htmltag($label,
665
                1) . '" class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"');
666
        $link .= '>';
667
        $linkend = '</a>';
668
669 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...
670
            $result .= ($link . img_object(($notooltip ? '' : $label), 'label',
671
                    ($notooltip ? '' : 'class="classfortooltip"')) . $linkend);
672
            if ($withpicto != 2) {
673
                $result .= ' ';
674
            }
675
        }
676
        $result .= $link . $this->idBBC_vols . $linkend;
677
        return $result;
678
    }
679
680
    /**
681
     *  Retourne le libelle du status d'un user (actif, inactif)
682
     *
683
     * @param    int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
684
     *
685
     * @return    string                   Label of status
686
     */
687
    public function getLibStatut($mode = 0)
688
    {
689
        return $this->LibStatut($this->is_facture, $mode);
690
    }
691
692
    /**
693
     * Renvoi le libelle d'un status donne
694
     *
695
     * @param int $status Id status
696
     * @param int $mode   0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
697
     *
698
     * @return string                    Label of status
699
     */
700
    private function LibStatut($status, $mode = 0)
701
    {
702
        global $langs;
703
704
        $billDone = $langs->trans('Facturé');
705
        $billNotDone = $langs->trans('Ouvert');
706
707 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...
708
            if ($status == 1) {
709
                return $billDone;
710
            }
711
            if ($status == 0) {
712
                return $billNotDone;
713
            }
714
        }
715 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...
716
            if ($status == 1) {
717
                return $billDone;
718
            }
719
            if ($status == 0) {
720
                return $billNotDone;
721
            }
722
        }
723 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...
724
            if ($status == 1) {
725
                return img_picto($billDone, 'statut4') . ' ' . $billDone;
726
            }
727
            if ($status == 0) {
728
                return img_picto($billNotDone, 'statut5') . ' ' . $billNotDone;
729
            }
730
        }
731
        if ($mode == 3) {
732
            if ($status == 1) {
733
                return img_picto($billDone, 'statut4');
734
            }
735
            if ($status == 0) {
736
                return img_picto($billNotDone, 'statut5');
737
            }
738
        }
739 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...
740
            if ($status == 1) {
741
                return img_picto($billDone, 'statut4') . ' ' . $billDone;
742
            }
743
            if ($status == 0) {
744
                return img_picto($billNotDone, 'statut5') . ' ' . $billNotDone;
745
            }
746
        }
747 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...
748
            if ($status == 1) {
749
                return $billDone . ' ' . img_picto($billDone, 'statut4');
750
            }
751
            if ($status == 0) {
752
                return $billNotDone . ' ' . img_picto($billNotDone, 'statut5');
753
            }
754
        }
755
756
        return "";
757
    }
758
759
    /**
760
     * @return string
761
     */
762
    public function __toString()
763
    {
764
        return $this->idBBC_vols . " " . $this->getPlaces();
765
    }
766
767
    /**
768
     * @return string
769
     */
770
    public function toString()
771
    {
772
        return "" . $this;
773
    }
774
775
    /**
776
     * @return string
777
     */
778
    public function getStatus()
779
    {
780
        return $this->statut;
781
    }
782
783
    /**
784
     * @return boolean
785
     */
786
    public function hasFacture()
787
    {
788
        $this->fetchObjectLinked($this->id, $this->element);
789
790
        return isset($this->linkedObjectsIds['facture']) && count($this->linkedObjectsIds['facture']) > 0;
791
    }
792
793
    /**
794
     * @param int $userId
795
     *
796
     * @return User
797
     */
798
    private function fetchUser($userId)
799
    {
800
        $user = new User($this->db);
801
        $user->fetch($userId);
802
803
        return $user;
804
    }
805
806
    /**
807
     * @return Bbc_ballons
808
     */
809
    private function fetchBalloon()
810
    {
811
        $balloon = new Bbc_ballons($this->db);
812
        $balloon->fetch($this->BBC_ballons_idBBC_ballons);
813
814
        return $balloon;
815
    }
816
817
    /**
818
     * @return Bbc_ballons
819
     */
820
    public function getBalloon()
821
    {
822
        if (!$this->balloon) {
823
            $this->balloon = $this->fetchBalloon();
824
        }
825
826
        return $this->balloon;
827
    }
828
829
    /**
830
     * @return User
831
     */
832
    public function getPilot()
833
    {
834
        if (!$this->pilot) {
835
            $this->pilot = $this->fetchUser($this->fk_pilot);
836
        }
837
838
        return $this->pilot;
839
    }
840
841
    /**
842
     * @return int
843
     */
844
    public function getPilotId()
845
    {
846
        return (int) $this->fk_pilot;
847
    }
848
849
    /**
850
     * @return int
851
     */
852
    public function getOrganisatorId()
853
    {
854
        return (int) $this->fk_organisateur;
855
    }
856
857
    /**
858
     * @return Bbctypes
859
     */
860
    public function getFlightType()
861
    {
862
        $flightType = new Bbctypes($this->db);
863
        $flightType->fetch($this->fk_type);
864
865
        return $flightType;
866
    }
867
868
    /**
869
     * @return string
870
     */
871
    public function getComment()
872
    {
873
        return $this->remarque;
874
    }
875
876
    /**
877
     * @return string
878
     */
879
    public function getIncident()
880
    {
881
        return $this->incidents;
882
    }
883
884
    /**
885
     * Return true if the number of pax is greater than 0
886
     *
887
     * @return boolean
888
     */
889
    public function hasPax()
890
    {
891
        return (int) $this->nbrPax > 0;
892
    }
893
894
    /**
895
     * Regarding the type of the flight give an indication if the flight must have pax to be valid.
896
     *
897
     * @return boolean
898
     */
899
    public function mustHavePax()
900
    {
901
        return $this->getFlightType()->isPaxRequired();
902
    }
903
904
    /**
905
     * Returns true if the amount requested by the flight is 0.
906
     *
907
     * @return boolean
908
     */
909
    public function isFree()
910
    {
911
        return empty($this->cost) || intval($this->cost) === 0;
912
    }
913
914
    /**
915
     * @return int
916
     */
917
    public function getAmountReceived()
918
    {
919
        return $this->cost;
920
    }
921
922
    /**
923
     * @return float
924
     */
925
    public function getAmountPerPassenger()
926
    {
927
        $nbrPax = $this->nbrPax > 0 ? $this->nbrPax : 1;
928
        return $this->cost / $nbrPax;
929
    }
930
931
    /**
932
     * @return boolean
933
     */
934
    public function hasReceiver()
935
    {
936
        return !empty($this->fk_receiver);
937
    }
938
939
    /**
940
     * @return boolean
941
     */
942
    public function hasKilometers()
943
    {
944
        return !empty($this->kilometers);
945
    }
946
947
    /**
948
     * @return boolean
949
     */
950
    public function hasKilometersDescription()
951
    {
952
        return !empty(trim($this->justif_kilometers));
953
    }
954
955
    /**
956
     * @return int
957
     */
958
    public function getKilometers()
959
    {
960
        return (int) $this->kilometers;
961
    }
962
963
    /**
964
     * @return string
965
     */
966
    public function getPlaces()
967
    {
968
        return $this->lieuD . ' -> ' . $this->lieuA;
969
    }
970
971
    /**
972
     * @return string
973
     */
974
    public function getDescription()
975
    {
976
        return $this->__toString() . ' - ' . $this->passengerNames;
977
    }
978
979
    /**
980
     * @return string
981
     */
982
    public function getPassengerNames()
983
    {
984
        return $this->passengerNames;
985
    }
986
987
    /**
988
     * @param string $passengerNames
989
     *
990
     * @return Bbcvols
991
     */
992
    public function setPassengerNames($passengerNames)
993
    {
994
        $this->passengerNames = $passengerNames;
995
        return $this;
996
    }
997
998
    /**
999
     * @return int
1000
     */
1001
    public function getNumberOfPassengers()
1002
    {
1003
        return (int) $this->nbrPax;
1004
    }
1005
1006
    /**
1007
     * @return int[]|array
1008
     */
1009
    public function getOrderIds()
1010
    {
1011
        return $this->orderIds;
1012
    }
1013
1014
    /**
1015
     * @param int $orderId
1016
     * @param int $nbrPassengers
1017
     */
1018
    public function addOrderId($orderId, $nbrPassengers){
1019
        if($nbrPassengers <= 0){
1020
            throw new InvalidArgumentException('Nbr of passenger must be positive number');
1021
        }
1022
1023
        if(!isset($this->orderIds)){
1024
            $this->orderIds = [];
1025
        }
1026
1027
        $this->orderIds[$orderId] = $nbrPassengers;
1028
    }
1029
1030
    /**
1031
     * Is an instruction flight (T6/T7)
1032
     */
1033
    public function isInstruction()
1034
    {
1035
        return $this->getFlightType()->isInstruction();
1036
    }
1037
1038
    /**
1039
     * @return bool
1040
     */
1041
    public function isLinkedToOrder()
1042
    {
1043
        return isset($this->orderIds) && !empty($this->orderIds);
1044
    }
1045
1046
    /**
1047
     * Fetch the orders based on the order ids.
1048
     */
1049
    public function fetchOrder()
1050
    {
1051
        $sql = 'SELECT';
1052
        $sql .= " t.order_id,";
1053
        $sql .= " t.flight_id,";
1054
        $sql .= " t.nbr_passengers";
1055
1056
        $sql .= ' FROM llx_bbc_vols_orders as t';
1057
        $sql .= sprintf(' WHERE t.flight_id = %s', $this->id);
1058
1059
        $resql = $this->db->query($sql);
1060
        if ($resql) {
1061
            $numrows = $this->db->num_rows($resql);
1062
            if ($numrows) {
1063
                for($i = 0 ; $i < $numrows ; $i++){
1064
                    $obj = $this->db->fetch_object($resql);
1065
                    $this->orderIds[$obj->order_id] = $obj->nbr_passengers;
1066
1067
                    $order = new Commande($this->db);
1068
                    $order->fetch($obj->order_id);
1069
1070
                    $this->orders[$obj->order_id] = $order;
1071
                }
1072
1073
            }
1074
        }
1075
1076
        return $this;
1077
    }
1078
1079
    /**
1080
     * @return Commande[]|array
1081
     */
1082
    public function getOrders()
1083
    {
1084
        if(!isset($this->orders) || empty($this->orders)){
1085
            $this->fetchOrder();
1086
        }
1087
        return $this->orders;
1088
    }
1089
1090
    /**
1091
     * Flag the flight as billed
1092
     *
1093
     * @return $this
1094
     */
1095
    public function bill()
1096
    {
1097
        $this->is_facture = true;
1098
        return $this;
1099
    }
1100
1101
    /**
1102
     * @return boolean
1103
     */
1104
    public function isBilled()
1105
    {
1106
        return !empty($this->is_facture);
1107
    }
1108
1109
    /**
1110
     * @return DateTime
1111
     */
1112
    public function getDate()
1113
    {
1114
        return (new DateTime())->setTimestamp($this->date);
1115
    }
1116
1117
    /**
1118
     * @param string $date
1119
     *
1120
     * @return Bbcvols
1121
     */
1122
    public function setDate($date)
1123
    {
1124
        $this->date = $date;
1125
        return $this;
1126
    }
1127
1128
    /**
1129
     * @return mixed
1130
     */
1131
    public function getLieuD()
1132
    {
1133
        return $this->lieuD;
1134
    }
1135
1136
    /**
1137
     * @param mixed $lieuD
1138
     *
1139
     * @return Bbcvols
1140
     */
1141
    public function setLieuD($lieuD)
1142
    {
1143
        $this->lieuD = $lieuD;
1144
        return $this;
1145
    }
1146
1147
    /**
1148
     * @return mixed
1149
     */
1150
    public function getLieuA()
1151
    {
1152
        return $this->lieuA;
1153
    }
1154
1155
    /**
1156
     * @param mixed $lieuA
1157
     *
1158
     * @return Bbcvols
1159
     */
1160
    public function setLieuA($lieuA)
1161
    {
1162
        $this->lieuA = $lieuA;
1163
        return $this;
1164
    }
1165
1166
    /**
1167
     * @return mixed
1168
     */
1169
    public function getHeureD()
1170
    {
1171
        return $this->heureD;
1172
    }
1173
1174
    /**
1175
     * @param mixed $heureD
1176
     *
1177
     * @return Bbcvols
1178
     */
1179
    public function setHeureD($heureD)
1180
    {
1181
        $this->heureD = $heureD;
1182
        return $this;
1183
    }
1184
1185
    /**
1186
     * @return mixed
1187
     */
1188
    public function getHeureA()
1189
    {
1190
        return $this->heureA;
1191
    }
1192
1193
    /**
1194
     * @param mixed $heureA
1195
     *
1196
     * @return Bbcvols
1197
     */
1198
    public function setHeureA($heureA)
1199
    {
1200
        $this->heureA = $heureA;
1201
        return $this;
1202
    }
1203
1204
    /**
1205
     * @return mixed
1206
     */
1207
    public function getBBCBallonsIdBBCBallons()
1208
    {
1209
        return $this->BBC_ballons_idBBC_ballons;
1210
    }
1211
1212
    /**
1213
     * @param mixed $BBC_ballons_idBBC_ballons
1214
     *
1215
     * @return Bbcvols
1216
     */
1217
    public function setBBCBallonsIdBBCBallons($BBC_ballons_idBBC_ballons)
1218
    {
1219
        $this->BBC_ballons_idBBC_ballons = $BBC_ballons_idBBC_ballons;
1220
        return $this;
1221
    }
1222
1223
    /**
1224
     * @return mixed
1225
     */
1226
    public function getNbrPax()
1227
    {
1228
        return $this->nbrPax;
1229
    }
1230
1231
    /**
1232
     * @param mixed $nbrPax
1233
     *
1234
     * @return Bbcvols
1235
     */
1236
    public function setNbrPax($nbrPax)
1237
    {
1238
        $this->nbrPax = $nbrPax;
1239
        return $this;
1240
    }
1241
1242
    /**
1243
     * @return mixed
1244
     */
1245
    public function getRemarque()
1246
    {
1247
        return $this->remarque;
1248
    }
1249
1250
    /**
1251
     * @param mixed $remarque
1252
     *
1253
     * @return Bbcvols
1254
     */
1255
    public function setRemarque($remarque)
1256
    {
1257
        $this->remarque = $remarque;
1258
        return $this;
1259
    }
1260
1261
    /**
1262
     * @return mixed
1263
     */
1264
    public function getIncidents()
1265
    {
1266
        return $this->incidents;
1267
    }
1268
1269
    /**
1270
     * @param mixed $incidents
1271
     *
1272
     * @return Bbcvols
1273
     */
1274
    public function setIncidents($incidents)
1275
    {
1276
        $this->incidents = $incidents;
1277
        return $this;
1278
    }
1279
1280
    /**
1281
     * @return mixed
1282
     */
1283
    public function getFkType()
1284
    {
1285
        return $this->fk_type;
1286
    }
1287
1288
    /**
1289
     * @param mixed $fk_type
1290
     *
1291
     * @return Bbcvols
1292
     */
1293
    public function setFkType($fk_type)
1294
    {
1295
        $this->fk_type = $fk_type;
1296
        return $this;
1297
    }
1298
1299
    /**
1300
     * @return mixed
1301
     */
1302
    public function getFkPilot()
1303
    {
1304
        return $this->fk_pilot;
1305
    }
1306
1307
    /**
1308
     * @param mixed $fk_pilot
1309
     *
1310
     * @return Bbcvols
1311
     */
1312
    public function setFkPilot($fk_pilot)
1313
    {
1314
        $this->fk_pilot = $fk_pilot;
1315
        return $this;
1316
    }
1317
1318
    /**
1319
     * @return mixed
1320
     */
1321
    public function getFkOrganisateur()
1322
    {
1323
        return $this->fk_organisateur;
1324
    }
1325
1326
    /**
1327
     * @param mixed $fk_organisateur
1328
     *
1329
     * @return Bbcvols
1330
     */
1331
    public function setFkOrganisateur($fk_organisateur)
1332
    {
1333
        $this->fk_organisateur = $fk_organisateur;
1334
        return $this;
1335
    }
1336
1337
    /**
1338
     * @return mixed
1339
     */
1340
    public function getisFacture()
1341
    {
1342
        return $this->is_facture;
1343
    }
1344
1345
    /**
1346
     * @param mixed $is_facture
1347
     *
1348
     * @return Bbcvols
1349
     */
1350
    public function setIsFacture($is_facture)
1351
    {
1352
        $this->is_facture = $is_facture;
1353
        return $this;
1354
    }
1355
1356
    /**
1357
     * @return int
1358
     */
1359
    public function getCost()
1360
    {
1361
        return $this->cost;
1362
    }
1363
1364
    /**
1365
     * @param int $cost
1366
     *
1367
     * @return Bbcvols
1368
     */
1369
    public function setCost($cost)
1370
    {
1371
        $this->cost = $cost;
1372
        return $this;
1373
    }
1374
1375
    /**
1376
     * @return mixed
1377
     */
1378
    public function getFkReceiver()
1379
    {
1380
        return $this->fk_receiver;
1381
    }
1382
1383
    /**
1384
     * @param mixed $fk_receiver
1385
     *
1386
     * @return Bbcvols
1387
     */
1388
    public function setFkReceiver($fk_receiver)
1389
    {
1390
        $this->fk_receiver = $fk_receiver;
1391
        return $this;
1392
    }
1393
1394
    /**
1395
     * @return mixed
1396
     */
1397
    public function getJustifKilometers()
1398
    {
1399
        return $this->justif_kilometers;
1400
    }
1401
1402
    /**
1403
     * @param mixed $justif_kilometers
1404
     *
1405
     * @return Bbcvols
1406
     */
1407
    public function setJustifKilometers($justif_kilometers)
1408
    {
1409
        $this->justif_kilometers = $justif_kilometers;
1410
        return $this;
1411
    }
1412
1413
1414
}
1415