Completed
Push — master ( da5e92...3bfafa )
by Laurent
02:26
created

Bbcvols::bill()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/* Copyright (C) 2007-2012  Laurent Destailleur <[email protected]>
3
 * Copyright (C) 2014       Juanjo Menent       <[email protected]>
4
 * Copyright (C) 2015       Florian Henry       <[email protected]>
5
 * Copyright (C) 2015       Raphaël Doursenaud  <[email protected]>
6
 * Copyright (C) ---Put here your own copyright and developer email---
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/**
23
 * \file    flightlog/bbcvols.class.php
24
 * \ingroup flightlog
25
 * \brief   This file is an example for a CRUD class file (Create/Read/Update/Delete)
26
 *          Put some comments here
27
 */
28
29
// Put here all includes required by your class file
30
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php';
31
require_once DOL_DOCUMENT_ROOT . '/flightballoon/class/bbc_ballons.class.php';
32
require_once DOL_DOCUMENT_ROOT . '/flightlog/class/bbctypes.class.php';
33
require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php';
34
35
/**
36
 * Class Bbcvols
37
 *
38
 * Put here description of your class
39
 *
40
 * @see CommonObject
41
 */
42
class Bbcvols extends CommonObject
43
{
44
    /**
45
     * @var string Id to identify managed objects
46
     */
47
    public $element = 'flightlog_bbcvols';
48
    /**
49
     * @var string Name of table without prefix where object is stored
50
     */
51
    public $table_element = 'bbc_vols';
52
53
    public $idBBC_vols;
54
    public $date = '';
55
    public $lieuD;
56
    public $lieuA;
57
    public $heureD;
58
    public $heureA;
59
    public $BBC_ballons_idBBC_ballons;
60
    public $nbrPax;
61
    public $remarque;
62
    public $incidents;
63
    public $fk_type;
64
    public $fk_pilot;
65
    public $fk_organisateur;
66
    public $is_facture;
67
    public $kilometers;
68
    public $cost;
69
    public $fk_receiver;
70
    public $justif_kilometers;
71
    public $date_creation;
72
    public $date_update;
73
74
    /**
75
     * @var Bbc_ballons
76
     */
77
    private $balloon;
78
79
    /**
80
     * @var User
81
     */
82
    private $pilot;
83
84
    /**
85
     * @var string
86
     */
87
    private $passengerNames;
88
89
    /**
90
     * @var int
91
     */
92
    private $orderId;
93
94
    /**
95
     * @var Commande
96
     */
97
    private $order;
98
99
    /**
100
     * @return int
101
     */
102
    public function getIdBBCVols()
103
    {
104
        return (int) $this->idBBC_vols;
105
    }
106
107
    /**
108
     * @return int
109
     */
110
    public function getId()
111
    {
112
        return (int) $this->getIdBBCVols();
113
    }
114
115
    /**
116
     * @param string|int $ref
117
     *
118
     * @return $this
119
     */
120
    public function setRef($ref)
121
    {
122
        $this->ref = $ref;
123
        return $this;
124
    }
125
126
    /**
127
     * Constructor
128
     *
129
     * @param DoliDb $db Database handler
130
     */
131
    public function __construct(DoliDB $db)
132
    {
133
        $this->db = $db;
134
        $this->cost = 0;
135
136
        $this->passengerNames = '';
137
    }
138
139
    /**
140
     * Create a flight
141
     *
142
     * @param  User $user      User that creates
143
     * @param  bool $notrigger false=launch triggers after, true=disable triggers
144
     *
145
     * @return int <0 if KO, Id of created object if OK
146
     * @throws Exception
147
     */
148
    public function create(User $user, $notrigger = false)
149
    {
150
        dol_syslog(__METHOD__, LOG_DEBUG);
151
152
        $error = 0;
153
154
        // Clean parameters
155
156
        if (isset($this->idBBC_vols)) {
157
            $this->idBBC_vols = trim($this->idBBC_vols);
158
        }
159
        if (isset($this->lieuD)) {
160
            $this->lieuD = trim($this->lieuD);
161
        }
162
        if (isset($this->lieuA)) {
163
            $this->lieuA = trim($this->lieuA);
164
        }
165
        if (isset($this->heureD)) {
166
            $this->heureD = trim($this->heureD) . '00';
167
        }
168
        if (isset($this->heureA)) {
169
            $this->heureA = trim($this->heureA) . '00';
170
        }
171
        if (isset($this->BBC_ballons_idBBC_ballons)) {
172
            $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons);
173
        }
174
        if (isset($this->nbrPax)) {
175
            $this->nbrPax = trim($this->nbrPax);
176
        }
177
        if (isset($this->remarque)) {
178
            $this->remarque = trim($this->remarque);
179
        }
180
        if (isset($this->incidents)) {
181
            $this->incidents = trim($this->incidents);
182
        }
183
        if (isset($this->fk_type)) {
184
            $this->fk_type = trim($this->fk_type);
185
        }
186
        if (isset($this->fk_pilot)) {
187
            $this->fk_pilot = trim($this->fk_pilot);
188
        }
189
        if (isset($this->fk_organisateur)) {
190
            $this->fk_organisateur = trim($this->fk_organisateur);
191
        }
192
        if (isset($this->is_facture)) {
193
            $this->is_facture = trim($this->is_facture);
194
        }
195
        if (isset($this->kilometers)) {
196
            $this->kilometers = trim($this->kilometers);
197
        }
198
        if (isset($this->cost)) {
199
            $this->cost = trim($this->cost);
200
        }
201
        if (isset($this->fk_receiver)) {
202
            $this->fk_receiver = trim($this->fk_receiver);
203
        }
204
        if (isset($this->justif_kilometers)) {
205
            $this->justif_kilometers = trim($this->justif_kilometers);
206
        }
207
        if (isset($this->passengerNames)) {
208
            $this->passengerNames = trim($this->passengerNames);
209
        }
210
        if (isset($this->orderId)) {
211
            $this->orderId = trim($this->orderId);
0 ignored issues
show
Documentation Bug introduced by
The property $orderId was declared of type integer, but trim($this->orderId) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
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 .= 'order_id';
238
239
        $sql .= ') VALUES (';
240
241
        $sql .= ' ' . (!isset($this->date) || dol_strlen($this->date) == 0 ? 'NULL' : "'" . $this->db->idate($this->date) . "'") . ',';
242
        $sql .= ' ' . (!isset($this->lieuD) ? 'NULL' : "'" . $this->db->escape($this->lieuD) . "'") . ',';
243
        $sql .= ' ' . (!isset($this->lieuA) ? 'NULL' : "'" . $this->db->escape($this->lieuA) . "'") . ',';
244
        $sql .= ' ' . (!isset($this->heureD) ? 'NULL' : "'" . $this->heureD . "'") . ',';
245
        $sql .= ' ' . (!isset($this->heureA) ? 'NULL' : "'" . $this->heureA . "'") . ',';
246
        $sql .= ' ' . (!isset($this->BBC_ballons_idBBC_ballons) ? 'NULL' : $this->BBC_ballons_idBBC_ballons) . ',';
247
        $sql .= ' ' . (!isset($this->nbrPax) ? 'NULL' : "'" . $this->db->escape($this->nbrPax) . "'") . ',';
248
        $sql .= ' ' . (!isset($this->remarque) ? 'NULL' : "'" . $this->db->escape($this->remarque) . "'") . ',';
249
        $sql .= ' ' . (!isset($this->incidents) ? 'NULL' : "'" . $this->db->escape($this->incidents) . "'") . ',';
250
        $sql .= ' ' . (!isset($this->fk_type) ? 'NULL' : $this->fk_type) . ',';
251
        $sql .= ' ' . (!isset($this->fk_pilot) ? 'NULL' : $this->fk_pilot) . ',';
252
        $sql .= ' ' . (!isset($this->fk_organisateur) ? 'NULL' : $this->fk_organisateur) . ',';
253
        $sql .= ' ' . (!isset($this->is_facture) ? '0' : $this->is_facture) . ',';
254
        $sql .= ' ' . (!isset($this->kilometers) || empty($this->kilometers) ? '0' : $this->kilometers) . ',';
255
        $sql .= ' ' . (!isset($this->cost) ? 'NULL' : "'" . $this->db->escape($this->cost) . "'") . ',';
256
        $sql .= ' ' . (!isset($this->fk_receiver) ? 'NULL' : $this->fk_receiver) . ',';
257
        $sql .= ' ' . (!isset($this->justif_kilometers) ? 'NULL' : "'" . $this->db->escape($this->justif_kilometers) . "'") . ',';
258
        $sql .= ' ' . "'" . date('Y-m-d H:i:s') . "'" . ',';
259
        $sql .= ' ' . "'" . date('Y-m-d H:i:s') . "'" . ',';
260
        $sql .= ' ' . "'" . $this->passengerNames . "'" . ',';
261
        $sql .= ' ' . (!isset($this->orderId) ? 'NULL' : $this->orderId) . '';
262
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
277
            if (!$notrigger) {
278
                $result = $this->call_trigger('BBC_FLIGHT_CREATED', $user);
279
                if ($result < 0) {
280
                    $error++;
281
                }
282
            }
283
        }
284
285
        // Commit or rollback
286
        if ($error) {
287
            $this->db->rollback();
288
289
            return -1 * $error;
290
        }
291
292
        $this->db->commit();
293
        return $this->id;
294
    }
295
296
    /**
297
     * Load object in memory from the database
298
     *
299
     * @param int    $id  Id object
300
     * @param string $ref Ref
301
     *
302
     * @return int <0 if KO, 0 if not found, >0 if OK
303
     * @throws Exception
304
     */
305
    public function fetch($id, $ref = null)
306
    {
307
        dol_syslog(__METHOD__, LOG_DEBUG);
308
309
        $sql = 'SELECT';
310
        $sql .= " t.idBBC_vols,";
311
        $sql .= " t.date,";
312
        $sql .= " t.lieuD,";
313
        $sql .= " t.lieuA,";
314
        $sql .= " t.heureD,";
315
        $sql .= " t.heureA,";
316
        $sql .= " t.BBC_ballons_idBBC_ballons,";
317
        $sql .= " t.nbrPax,";
318
        $sql .= " t.remarque,";
319
        $sql .= " t.incidents,";
320
        $sql .= " t.fk_type,";
321
        $sql .= " t.fk_pilot,";
322
        $sql .= " t.fk_organisateur,";
323
        $sql .= " t.is_facture,";
324
        $sql .= " t.kilometers,";
325
        $sql .= " t.cost,";
326
        $sql .= " t.fk_receiver,";
327
        $sql .= " t.justif_kilometers,";
328
        $sql .= " t.date_creation,";
329
        $sql .= " t.date_update,";
330
        $sql .= " t.passenger_names,";
331
        $sql .= " t.order_id";
332
333
334
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $this->table_element . ' as t';
335 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...
336
            $sql .= ' WHERE t.ref = ' . '\'' . $ref . '\'';
337
        } else {
338
            $sql .= ' WHERE t.idBBC_vols = ' . $id;
339
        }
340
341
        $resql = $this->db->query($sql);
342
        if ($resql) {
343
            $numrows = $this->db->num_rows($resql);
344
            if ($numrows) {
345
                $obj = $this->db->fetch_object($resql);
346
347
                $this->id = $obj->idBBC_vols;
348
349
                $this->idBBC_vols = $obj->idBBC_vols;
350
                $this->date = $this->db->jdate($obj->date);
351
                $this->lieuD = $obj->lieuD;
352
                $this->lieuA = $obj->lieuA;
353
                $this->heureD = $obj->heureD;
354
                $this->heureA = $obj->heureA;
355
                $this->BBC_ballons_idBBC_ballons = $obj->BBC_ballons_idBBC_ballons;
356
                $this->nbrPax = $obj->nbrPax;
357
                $this->remarque = $obj->remarque;
358
                $this->incidents = $obj->incidents;
359
                $this->fk_type = $obj->fk_type;
360
                $this->fk_pilot = $obj->fk_pilot;
361
                $this->fk_organisateur = $obj->fk_organisateur;
362
                $this->is_facture = $obj->is_facture;
363
                $this->kilometers = $obj->kilometers;
364
                $this->cost = $obj->cost;
365
                $this->fk_receiver = $obj->fk_receiver;
366
                $this->justif_kilometers = $obj->justif_kilometers;
367
                $this->date_creation = $obj->date_creation;
368
                $this->date_update = $obj->date_update;
369
                $this->passengerNames = $obj->passenger_names;
370
                $this->orderId = $obj->order_id;
371
372
                $this->balloon = $this->fetchBalloon();
373
                $this->pilot = $this->fetchUser($this->fk_pilot);
374
                $this->fetchOrder();
375
            }
376
            $this->db->free($resql);
377
378
            if ($numrows) {
379
                return 1;
380
            } else {
381
                return 0;
382
            }
383
        }
384
385
        $this->errors[] = 'Error ' . $this->db->lasterror();
386
        dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
387
        return -1;
388
    }
389
390
    /**
391
     * @param  User $user      User that modifies
392
     * @param  bool $notrigger false=launch triggers after, true=disable triggers
393
     *
394
     * @return int <0 if KO, >0 if OK
395
     * @throws Exception
396
     */
397
    public function update(User $user, $notrigger = false)
398
    {
399
        $error = 0;
400
401
        dol_syslog(__METHOD__, LOG_DEBUG);
402
403
        // Clean parameters
404
405
        if (isset($this->idBBC_vols)) {
406
            $this->idBBC_vols = trim($this->idBBC_vols);
407
        }
408
        if (isset($this->lieuD)) {
409
            $this->lieuD = trim($this->lieuD);
410
        }
411
        if (isset($this->lieuA)) {
412
            $this->lieuA = trim($this->lieuA);
413
        }
414
        if (isset($this->heureD)) {
415
            $this->heureD = trim($this->heureD);
416
        }
417
        if (isset($this->heureA)) {
418
            $this->heureA = trim($this->heureA);
419
        }
420
        if (isset($this->BBC_ballons_idBBC_ballons)) {
421
            $this->BBC_ballons_idBBC_ballons = trim($this->BBC_ballons_idBBC_ballons);
422
        }
423
        if (isset($this->nbrPax)) {
424
            $this->nbrPax = trim($this->nbrPax);
425
        }
426
        if (isset($this->remarque)) {
427
            $this->remarque = trim($this->remarque);
428
        }
429
        if (isset($this->incidents)) {
430
            $this->incidents = trim($this->incidents);
431
        }
432
        if (isset($this->fk_type)) {
433
            $this->fk_type = trim($this->fk_type);
434
        }
435
        if (isset($this->fk_pilot)) {
436
            $this->fk_pilot = trim($this->fk_pilot);
437
        }
438
        if (isset($this->fk_organisateur)) {
439
            $this->fk_organisateur = trim($this->fk_organisateur);
440
        }
441
        if (isset($this->is_facture)) {
442
            $this->is_facture = trim($this->is_facture);
443
        }
444
        if (isset($this->kilometers)) {
445
            $this->kilometers = trim($this->kilometers);
446
        }
447
        if (isset($this->cost)) {
448
            $this->cost = trim($this->cost);
449
        }
450
        if (isset($this->fk_receiver)) {
451
            $this->fk_receiver = trim($this->fk_receiver);
452
        }
453
        if (isset($this->justif_kilometers)) {
454
            $this->justif_kilometers = trim($this->justif_kilometers);
455
        }
456
        if (isset($this->passengerNames)) {
457
            $this->passengerNames = trim($this->passengerNames);
458
        }
459
        if (isset($this->orderId)) {
460
            $this->orderId = trim($this->orderId);
0 ignored issues
show
Documentation Bug introduced by
The property $orderId was declared of type integer, but trim($this->orderId) is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
461
        }
462
463
464
        // Check parameters
465
        // Put here code to add a control on parameters values
466
467
        // Update request
468
        $sql = 'UPDATE ' . MAIN_DB_PREFIX . $this->table_element . ' SET';
469
470
        $sql .= ' date = ' . (!isset($this->date) || dol_strlen($this->date) != 0 ? "'" . $this->db->idate($this->date) . "'" : 'null') . ',';
471
        $sql .= ' lieuD = ' . (isset($this->lieuD) ? "'" . $this->db->escape($this->lieuD) . "'" : "null") . ',';
472
        $sql .= ' lieuA = ' . (isset($this->lieuA) ? "'" . $this->db->escape($this->lieuA) . "'" : "null") . ',';
473
        $sql .= ' heureD = ' . (isset($this->heureD) ? "'" . $this->heureD . "'" : "null") . ',';
474
        $sql .= ' heureA = ' . (isset($this->heureA) ? "'" . $this->heureA . "'" : "null") . ',';
475
        $sql .= ' BBC_ballons_idBBC_ballons = ' . (isset($this->BBC_ballons_idBBC_ballons) ? $this->BBC_ballons_idBBC_ballons : "null") . ',';
476
        $sql .= ' nbrPax = ' . (isset($this->nbrPax) ? "'" . $this->db->escape($this->nbrPax) . "'" : "null") . ',';
477
        $sql .= ' remarque = ' . (isset($this->remarque) ? "'" . $this->db->escape($this->remarque) . "'" : "null") . ',';
478
        $sql .= ' incidents = ' . (isset($this->incidents) ? "'" . $this->db->escape($this->incidents) . "'" : "null") . ',';
479
        $sql .= ' fk_type = ' . (isset($this->fk_type) ? $this->fk_type : "null") . ',';
480
        $sql .= ' fk_pilot = ' . (isset($this->fk_pilot) ? $this->fk_pilot : "null") . ',';
481
        $sql .= ' fk_organisateur = ' . (isset($this->fk_organisateur) ? $this->fk_organisateur : "null") . ',';
482
        $sql .= ' is_facture = ' . (isset($this->is_facture) ? $this->is_facture : "0") . ',';
483
        $sql .= ' kilometers = ' . (!empty($this->kilometers) ? $this->kilometers : "0") . ',';
484
        $sql .= ' cost = ' . (isset($this->cost) ? "'" . $this->db->escape($this->cost) . "'" : "''") . ',';
485
        $sql .= ' fk_receiver = ' . (isset($this->fk_receiver) ? $this->fk_receiver : "null") . ',';
486
        $sql .= ' justif_kilometers = ' . (isset($this->justif_kilometers) ? "'" . $this->db->escape($this->justif_kilometers) . "'," : "'',");
487
        $sql .= ' date_update = ' . "'" . date('Y-m-d H:i:s') . "',";
488
        $sql .= ' passenger_names = ' . "'" . trim($this->passengerNames) . "',";
489
        $sql .= ' order_id = ' . "'" . (!isset($this->orderId) ? 'null' : $this->orderId) . "'";
490
491
        $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols;
492
493
        $this->db->begin();
494
495
        $resql = $this->db->query($sql);
496
        if (!$resql) {
497
            $error++;
498
            $this->errors[] = 'Error ' . $this->db->lasterror();
499
            dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
500
        }
501
502
        if (!$error && !$notrigger) {
503
            $result = $this->call_trigger('BBC_FLIGHT_UPDATED', $user);
504
            if ($result < 0) {
505
                $error++;
506
            }
507
        }
508
509
        // Commit or rollback
510
        if ($error) {
511
            $this->db->rollback();
512
513
            return -1 * $error;
514
        }
515
516
        $this->db->commit();
517
        return 1;
518
    }
519
520
    /**
521
     * Delete object in database
522
     *
523
     * @param User $user      User that deletes
524
     * @param bool $notrigger false=launch triggers after, true=disable triggers
525
     *
526
     * @return int <0 if KO, >0 if OK
527
     * @throws Exception
528
     */
529
    public function delete(User $user, $notrigger = false)
530
    {
531
        dol_syslog(__METHOD__, LOG_DEBUG);
532
533
        $error = 0;
534
535
        $this->db->begin();
536
537
        if (!$error) {
538
            if (!$notrigger) {
539
                $result = $this->call_trigger('BBC_FLIGHT_DELETED', $user);
540
                if ($result < 0) {
541
                    $error++;
542
                }
543
            }
544
        }
545
546 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...
547
            $sql = 'DELETE FROM ' . MAIN_DB_PREFIX . $this->table_element;
548
            $sql .= ' WHERE idBBC_vols=' . $this->idBBC_vols;
549
550
            $resql = $this->db->query($sql);
551
            if (!$resql) {
552
                $error++;
553
                $this->errors[] = 'Error ' . $this->db->lasterror();
554
                dol_syslog(__METHOD__ . ' ' . join(',', $this->errors), LOG_ERR);
555
            }
556
        }
557
558
        // Commit or rollback
559
        if ($error) {
560
            $this->db->rollback();
561
            return -1 * $error;
562
        }
563
564
        $this->db->commit();
565
        return 1;
566
    }
567
568
    /**
569
     *  Return a link to the user card (with optionaly the picto)
570
     *    Use this->id,this->lastname, this->firstname
571
     *
572
     * @param    int     $withpicto Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
573
     * @param    string  $option    On what the link point to
574
     * @param    integer $notooltip 1=Disable tooltip
575
     * @param    int     $maxlen    Max length of visible user name
576
     * @param  string    $morecss   Add more css on link
577
     *
578
     * @return    string                        String with URL
579
     */
580
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $maxlen = 24, $morecss = '')
581
    {
582
        global $langs;
583
584
        $result = '';
585
586
        $label = '<u>' . $langs->trans("MyModule") . '</u>';
587
        $label .= '<div width="100%">';
588
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->idBBC_vols . '<br>';
589
        $label .= '<b>' . $langs->trans('Date') . ':</b> ' . dol_print_date($this->date, '%d-%m-%Y') . '<br/>';
590
        $label .= '<b>' . $langs->trans('From') . ':</b> ' . $this->lieuD . '<br/>';
591
        $label .= '<b>' . $langs->trans('To') . ':</b> ' . $this->lieuA . '<br/>';
592
        $label .= '</div>';
593
594
        $link = '<a href="' . DOL_URL_ROOT . '/flightlog/card.php?id=' . $this->idBBC_vols . '"';
595
        $link .= ($notooltip ? '' : ' title="' . dol_escape_htmltag($label,
596
                1) . '" class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"');
597
        $link .= '>';
598
        $linkend = '</a>';
599
600 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...
601
            $result .= ($link . img_object(($notooltip ? '' : $label), 'label',
602
                    ($notooltip ? '' : 'class="classfortooltip"')) . $linkend);
603
            if ($withpicto != 2) {
604
                $result .= ' ';
605
            }
606
        }
607
        $result .= $link . $this->idBBC_vols . $linkend;
608
        return $result;
609
    }
610
611
    /**
612
     *  Retourne le libelle du status d'un user (actif, inactif)
613
     *
614
     * @param    int $mode 0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
615
     *
616
     * @return    string                   Label of status
617
     */
618
    public function getLibStatut($mode = 0)
619
    {
620
        return $this->LibStatut($this->is_facture, $mode);
621
    }
622
623
    /**
624
     * Renvoi le libelle d'un status donne
625
     *
626
     * @param int $status Id status
627
     * @param int $mode   0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
628
     *
629
     * @return string                    Label of status
630
     */
631
    private function LibStatut($status, $mode = 0)
632
    {
633
        global $langs;
634
635
        $billDone = $langs->trans('Facturé');
636
        $billNotDone = $langs->trans('Ouvert');
637
638 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...
639
            if ($status == 1) {
640
                return $billDone;
641
            }
642
            if ($status == 0) {
643
                return $billNotDone;
644
            }
645
        }
646 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...
647
            if ($status == 1) {
648
                return $billDone;
649
            }
650
            if ($status == 0) {
651
                return $billNotDone;
652
            }
653
        }
654 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...
655
            if ($status == 1) {
656
                return img_picto($billDone, 'statut4') . ' ' . $billDone;
657
            }
658
            if ($status == 0) {
659
                return img_picto($billNotDone, 'statut5') . ' ' . $billNotDone;
660
            }
661
        }
662
        if ($mode == 3) {
663
            if ($status == 1) {
664
                return img_picto($billDone, 'statut4');
665
            }
666
            if ($status == 0) {
667
                return img_picto($billNotDone, 'statut5');
668
            }
669
        }
670 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...
671
            if ($status == 1) {
672
                return img_picto($billDone, 'statut4') . ' ' . $billDone;
673
            }
674
            if ($status == 0) {
675
                return img_picto($billNotDone, 'statut5') . ' ' . $billNotDone;
676
            }
677
        }
678 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...
679
            if ($status == 1) {
680
                return $billDone . ' ' . img_picto($billDone, 'statut4');
681
            }
682
            if ($status == 0) {
683
                return $billNotDone . ' ' . img_picto($billNotDone, 'statut5');
684
            }
685
        }
686
687
        return "";
688
    }
689
690
    /**
691
     * @return string
692
     */
693
    public function __toString()
694
    {
695
        return $this->idBBC_vols . " " . $this->getPlaces();
696
    }
697
698
    /**
699
     * @return string
700
     */
701
    public function toString()
702
    {
703
        return "" . $this;
704
    }
705
706
    /**
707
     * @return string
708
     */
709
    public function getStatus()
710
    {
711
        return $this->statut;
712
    }
713
714
    /**
715
     * @return boolean
716
     */
717
    public function hasFacture()
718
    {
719
        return count($this->linkedObjectsIds) > 0;
720
    }
721
722
    /**
723
     * @param int $userId
724
     *
725
     * @return User
726
     */
727
    private function fetchUser($userId)
728
    {
729
        $user = new User($this->db);
730
        $user->fetch($userId);
731
732
        return $user;
733
    }
734
735
    /**
736
     * @return Bbc_ballons
737
     */
738
    private function fetchBalloon()
739
    {
740
        $balloon = new Bbc_ballons($this->db);
741
        $balloon->fetch($this->BBC_ballons_idBBC_ballons);
742
743
        return $balloon;
744
    }
745
746
    /**
747
     * @return Bbc_ballons
748
     */
749
    public function getBalloon()
750
    {
751
        if (!$this->balloon) {
752
            $this->balloon = $this->fetchBalloon();
753
        }
754
755
        return $this->balloon;
756
    }
757
758
    /**
759
     * @return User
760
     */
761
    public function getPilot()
762
    {
763
        if (!$this->pilot) {
764
            $this->pilot = $this->fetchUser($this->fk_pilot);
765
        }
766
767
        return $this->pilot;
768
    }
769
770
    /**
771
     * @return int
772
     */
773
    public function getPilotId()
774
    {
775
        return (int) $this->fk_pilot;
776
    }
777
778
    /**
779
     * @return int
780
     */
781
    public function getOrganisatorId()
782
    {
783
        return (int) $this->fk_organisateur;
784
    }
785
786
    /**
787
     * @return Bbctypes
788
     */
789
    public function getFlightType()
790
    {
791
        $flightType = new Bbctypes($this->db);
792
        $flightType->fetch($this->fk_type);
793
794
        return $flightType;
795
    }
796
797
    /**
798
     * @return string
799
     */
800
    public function getComment()
801
    {
802
        return $this->remarque;
803
    }
804
805
    /**
806
     * @return string
807
     */
808
    public function getIncident()
809
    {
810
        return $this->incidents;
811
    }
812
813
    /**
814
     * Return true if the number of pax is greater than 0
815
     *
816
     * @return boolean
817
     */
818
    public function hasPax()
819
    {
820
        return (int) $this->nbrPax > 0;
821
    }
822
823
    /**
824
     * Regarding the type of the flight give an indication if the flight must have pax to be valid.
825
     *
826
     * @return boolean
827
     */
828
    public function mustHavePax()
829
    {
830
        return $this->getFlightType()->isPaxRequired();
831
    }
832
833
    /**
834
     * Returns true if the amount requested by the flight is 0.
835
     *
836
     * @return boolean
837
     */
838
    public function isFree()
839
    {
840
        return empty($this->cost);
841
    }
842
843
    /**
844
     * @return int
845
     */
846
    public function getAmountReceived()
847
    {
848
        return $this->cost;
849
    }
850
851
    /**
852
     * @return int
853
     */
854
    public function getAmountPerPassenger()
855
    {
856
        return $this->cost / $this->nbrPax;
857
    }
858
859
    /**
860
     * @return boolean
861
     */
862
    public function hasReceiver()
863
    {
864
        return !empty($this->fk_receiver);
865
    }
866
867
    /**
868
     * @return boolean
869
     */
870
    public function hasKilometers()
871
    {
872
        return !empty($this->kilometers);
873
    }
874
875
    /**
876
     * @return boolean
877
     */
878
    public function hasKilometersDescription()
879
    {
880
        return !empty(trim($this->justif_kilometers));
881
    }
882
883
    /**
884
     * @return int
885
     */
886
    public function getKilometers()
887
    {
888
        return (int) $this->kilometers;
889
    }
890
891
    /**
892
     * @return string
893
     */
894
    public function getPlaces()
895
    {
896
        return $this->lieuD . ' -> ' . $this->lieuA;
897
    }
898
899
    /**
900
     * @return string
901
     */
902
    public function getDescription()
903
    {
904
        return $this->__toString() . ' - ' . $this->passengerNames;
905
    }
906
907
    /**
908
     * @return string
909
     */
910
    public function getPassengerNames()
911
    {
912
        return $this->passengerNames;
913
    }
914
915
    /**
916
     * @param string $passengerNames
917
     *
918
     * @return Bbcvols
919
     */
920
    public function setPassengerNames($passengerNames)
921
    {
922
        $this->passengerNames = $passengerNames;
923
        return $this;
924
    }
925
926
    /**
927
     * @return int
928
     */
929
    public function getNumberOfPassengers()
930
    {
931
        return (int) $this->nbrPax;
932
    }
933
934
    /**
935
     * @return int
936
     */
937
    public function getOrderId()
938
    {
939
        return $this->orderId;
940
    }
941
942
    /**
943
     * @param int $orderId
944
     *
945
     * @return Bbcvols
946
     */
947
    public function setOrderId($orderId)
948
    {
949
        $this->orderId = $orderId;
950
        return $this;
951
    }
952
953
    /**
954
     * Is an instruction flight (T6/T7)
955
     */
956
    public function isInstruction()
957
    {
958
        return $this->getFlightType()->isInstruction();
959
    }
960
961
    /**
962
     * @return bool
963
     */
964
    public function isLinkedToOrder()
965
    {
966
        return isset($this->orderId) && $this->orderId > 0;
967
    }
968
969
    /**
970
     * Fetch the order based on the order id.
971
     */
972
    public function fetchOrder()
973
    {
974
        if (!$this->isLinkedToOrder()) {
975
            return $this;
976
        }
977
978
        $this->order = new Commande($this->db);
979
        $this->order->fetch($this->orderId);
980
981
        return $this;
982
    }
983
984
    /**
985
     * @return Commande
986
     */
987
    public function getOrder()
988
    {
989
        return $this->order;
990
    }
991
992
    /**
993
     * Flag the flight as billed
994
     *
995
     * @return $this
996
     */
997
    public function bill()
998
    {
999
        $this->is_facture = true;
1000
        return $this;
1001
    }
1002
1003
    /**
1004
     * @return boolean
1005
     */
1006
    public function isBilled()
1007
    {
1008
        return !empty($this->is_facture);
1009
    }
1010
1011
}
1012