Completed
Push — feature/select_existing_custom... ( a1e51a )
by Laurent
01:57
created

Bbcvols::insertOrders()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 36

Duplication

Lines 5
Ratio 13.89 %

Importance

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