PaymentExpenseReport::create()   F
last analyzed

Complexity

Conditions 18
Paths 14337

Size

Total Lines 85
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 18
eloc 56
nc 14337
nop 1
dl 0
loc 85
rs 0.7
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/* Copyright (C) 2015-2017  Alexandre Spangaro  <[email protected]>
4
 * Copyright (C) 2018       Nicolas ZABOURI  <[email protected]>
5
 * Copyright (C) 2024       Frédéric France             <[email protected]>
6
 * Copyright (C) 2024		MDW						<[email protected]>
7
 * Copyright (C) 2024       Rafael San José         <[email protected]>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 3 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21
 */
22
23
namespace DoliModules\ExpenseReport\Model;
24
25
/**
26
 *  \file       htdocs/expensereport/class/paymentexpensereport.class.php
27
 *  \ingroup    Expense Report
28
 *  \brief      File of class to manage payment of expense report
29
 */
30
31
use DoliCore\Base\GenericDocument;
32
33
/**
34
 *  Class to manage payments of expense report
35
 */
36
class PaymentExpenseReport extends GenericDocument
37
{
38
    /**
39
     * @var string ID to identify managed object
40
     */
41
    public $element = 'payment_expensereport';
42
43
    /**
44
     * @var string Name of table without prefix where object is stored
45
     */
46
    public $table_element = 'payment_expensereport';
47
48
    /**
49
     * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
50
     */
51
    public $picto = 'payment';
52
53
    /**
54
     * @var int ID
55
     */
56
    public $rowid;
57
58
    /**
59
     * @var int ID
60
     */
61
    public $fk_expensereport;
62
63
    /**
64
     * @var int|string
65
     */
66
    public $datec = '';
67
68
    /**
69
     * @var int|string
70
     */
71
    public $datep = '';
72
    /**
73
     * @var float|int
74
     */
75
    public $amount; // Total amount of payment
76
    /**
77
     * @var array<float|int>
78
     */
79
    public $amounts = []; // Array of amounts
80
81
    /**
82
     * @var int ID
83
     */
84
    public $fk_typepayment;
85
86
    /**
87
     * @var string      Payment reference
88
     *                  (Cheque or bank transfer reference. Can be "ABC123")
89
     */
90
    public $num_payment;
91
92
    /**
93
     * @var int ID
94
     */
95
    public $fk_bank;
96
97
    /**
98
     * @var int ID
99
     */
100
    public $fk_user_creat;
101
102
    /**
103
     * @var int ID
104
     */
105
    public $fk_user_modif;
106
107
    /**
108
     * @var string
109
     */
110
    public $type_code;
111
112
    /**
113
     * @var string
114
     */
115
    public $type_label;
116
117
    /**
118
     * @var int
119
     */
120
    public $bank_account;
121
122
    /**
123
     * @var int
124
     */
125
    public $bank_line;
126
127
    /**
128
     * @var string
129
     */
130
    public $label;
131
132
133
    /**
134
     *  Constructor
135
     *
136
     * @param DoliDB $db Database handler
0 ignored issues
show
Bug introduced by
The type DoliModules\ExpenseReport\Model\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
137
     */
138
    public function __construct($db)
139
    {
140
        $this->db = $db;
141
    }
142
143
    /**
144
     *  Update database
145
     *
146
     * @param User $user      User that modify
147
     * @param int  $notrigger 0=launch triggers after, 1=disable triggers
148
     *
149
     * @return int                     Return integer <0 if KO, >0 if OK
150
     */
151
    public function update($user = null, $notrigger = 0)
152
    {
153
        // phpcs:enable
154
        $error = 0;
155
156
        // Clean parameters
157
158
        if (isset($this->fk_expensereport)) {
159
            $this->fk_expensereport = (int) $this->fk_expensereport;
160
        }
161
        if (isset($this->amount)) {
162
            $this->amount = trim($this->amount);
0 ignored issues
show
Documentation Bug introduced by
It seems like trim($this->amount) of type string is incompatible with the declared type double|integer of property $amount.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
163
        }
164
        if (isset($this->fk_typepayment)) {
165
            $this->fk_typepayment = (int) $this->fk_typepayment;
166
        }
167
        if (isset($this->num_payment)) {
168
            $this->num_payment = trim($this->num_payment);
169
        }
170
        if (isset($this->note)) {
171
            $this->note = trim($this->note);
172
        }
173
        if (isset($this->fk_bank)) {
174
            $this->fk_bank = (int) $this->fk_bank;
175
        }
176
        if (isset($this->fk_user_creat)) {
177
            $this->fk_user_creat = (int) $this->fk_user_creat;
178
        }
179
        if (isset($this->fk_user_modif)) {
180
            $this->fk_user_modif = (int) $this->fk_user_modif;
181
        }
182
183
        // Update request
184
        $sql = "UPDATE " . MAIN_DB_PREFIX . "payment_expensereport SET";
185
        $sql .= " fk_expensereport=" . (isset($this->fk_expensereport) ? $this->fk_expensereport : "null") . ",";
186
        $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ",";
187
        $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ",";
188
        $sql .= " datep=" . (dol_strlen($this->datep) != 0 ? "'" . $this->db->idate($this->datep) . "'" : 'null') . ",";
189
        $sql .= " amount=" . (isset($this->amount) ? $this->amount : "null") . ",";
190
        $sql .= " fk_typepayment=" . (isset($this->fk_typepayment) ? $this->fk_typepayment : "null") . ",";
191
        $sql .= " num_payment=" . (isset($this->num_payment) ? "'" . $this->db->escape($this->num_payment) . "'" : "null") . ",";
192
        $sql .= " note=" . (isset($this->note) ? "'" . $this->db->escape($this->note) . "'" : "null") . ",";
193
        $sql .= " fk_bank=" . (isset($this->fk_bank) ? $this->fk_bank : "null") . ",";
194
        $sql .= " fk_user_creat=" . (isset($this->fk_user_creat) ? $this->fk_user_creat : "null") . ",";
195
        $sql .= " fk_user_modif=" . (isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
196
        $sql .= " WHERE rowid=" . ((int) $this->id);
197
198
        $this->db->begin();
199
200
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
201
        $resql = $this->db->query($sql);
202
        if (!$resql) {
203
            $error++;
204
            $this->errors[] = "Error " . $this->db->lasterror();
205
        }
206
207
        // Commit or rollback
208
        if ($error) {
209
            foreach ($this->errors as $errmsg) {
210
                dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
211
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
212
            }
213
            $this->db->rollback();
214
            return -1 * $error;
215
        } else {
216
            $this->db->commit();
217
            return 1;
218
        }
219
    }
220
221
    /**
222
     *  Delete object in database
223
     *
224
     * @param User $user      User that delete
225
     * @param int  $notrigger 0=launch triggers after, 1=disable triggers
226
     *
227
     * @return int                     Return integer <0 if KO, >0 if OK
228
     */
229
    public function delete($user, $notrigger = 0)
230
    {
231
        // phpcs:enable
232
        $error = 0;
233
234
        $this->db->begin();
235
236
        if (!$error) {
237
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "bank_url";
238
            $sql .= " WHERE type='payment_expensereport' AND url_id=" . ((int) $this->id);
239
240
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
241
            $resql = $this->db->query($sql);
242
            if (!$resql) {
243
                $error++;
244
                $this->errors[] = "Error " . $this->db->lasterror();
245
            }
246
        }
247
248
        if (!$error) {
249
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "payment_expensereport";
250
            $sql .= " WHERE rowid=" . ((int) $this->id);
251
252
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
253
            $resql = $this->db->query($sql);
254
            if (!$resql) {
255
                $error++;
256
                $this->errors[] = "Error " . $this->db->lasterror();
257
            }
258
        }
259
260
        // Commit or rollback
261
        if ($error) {
262
            foreach ($this->errors as $errmsg) {
263
                dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
264
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
265
            }
266
            $this->db->rollback();
267
            return -1 * $error;
268
        } else {
269
            $this->db->commit();
270
            return 1;
271
        }
272
    }
273
274
    // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
275
276
    /**
277
     *  Load an object from its id and create a new one in database
278
     *
279
     * @param User $user   User making the clone
280
     * @param int  $fromid Id of object to clone
281
     *
282
     * @return int                     New id of clone
283
     */
284
    public function createFromClone(User $user, $fromid)
285
    {
286
        $error = 0;
287
288
        $object = new PaymentExpenseReport($this->db);
289
290
        $this->db->begin();
291
292
        // Load source object
293
        $object->fetch($fromid);
294
        $object->id = 0;
295
        $object->statut = 0;
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocument::$statut has been deprecated: Use $status instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

295
        /** @scrutinizer ignore-deprecated */ $object->statut = 0;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
296
297
        // Clear fields
298
        // ...
299
300
        // Create clone
301
        $object->context['createfromclone'] = 'createfromclone';
302
        $result = $object->create($user);
303
304
        // Other options
305
        if ($result < 0) {
306
            $this->error = $object->error;
307
            $error++;
308
        }
309
310
        unset($object->context['createfromclone']);
311
312
        // End
313
        if (!$error) {
314
            $this->db->commit();
315
            return $object->id;
316
        } else {
317
            $this->db->rollback();
318
            return -1;
319
        }
320
    }
321
322
    // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter
323
324
    /**
325
     *  Load object in memory from database
326
     *
327
     * @param int $id Id object
328
     *
329
     * @return int                 Return integer <0 if KO, >0 if OK
330
     */
331
    public function fetch($id)
332
    {
333
        $sql = "SELECT";
334
        $sql .= " t.rowid,";
335
        $sql .= " t.fk_expensereport,";
336
        $sql .= " t.datec,";
337
        $sql .= " t.tms,";
338
        $sql .= " t.datep,";
339
        $sql .= " t.amount,";
340
        $sql .= " t.fk_typepayment,";
341
        $sql .= " t.num_payment,";
342
        $sql .= " t.note as note_public,";
343
        $sql .= " t.fk_bank,";
344
        $sql .= " t.fk_user_creat,";
345
        $sql .= " t.fk_user_modif,";
346
        $sql .= " pt.code as type_code, pt.libelle as type_label,";
347
        $sql .= ' b.fk_account';
348
        $sql .= " FROM " . MAIN_DB_PREFIX . "payment_expensereport as t";
349
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_paiement as pt ON t.fk_typepayment = pt.id";
350
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON t.fk_bank = b.rowid';
351
        $sql .= " WHERE t.rowid = " . ((int) $id);
352
353
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
354
        $resql = $this->db->query($sql);
355
        if ($resql) {
356
            if ($this->db->num_rows($resql)) {
357
                $obj = $this->db->fetch_object($resql);
358
359
                $this->id = $obj->rowid;
360
                $this->ref = $obj->rowid;
361
362
                $this->fk_expensereport = $obj->fk_expensereport;
363
                $this->datec = $this->db->jdate($obj->datec);
364
                $this->tms = $this->db->jdate($obj->tms);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->jdate($obj->tms) can also be of type string. However, the property $tms is declared as type integer. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Deprecated Code introduced by
The property DoliCore\Base\GenericDocument::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

364
                /** @scrutinizer ignore-deprecated */ $this->tms = $this->db->jdate($obj->tms);

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
365
                $this->datep = $this->db->jdate($obj->datep);
366
                $this->amount = $obj->amount;
367
                $this->fk_typepayment = $obj->fk_typepayment;
368
                $this->num_payment = $obj->num_payment;
369
                $this->note_public = $obj->note_public;
370
                $this->fk_bank = $obj->fk_bank;
371
                $this->fk_user_creat = $obj->fk_user_creat;
372
                $this->fk_user_modif = $obj->fk_user_modif;
373
374
                $this->type_code = $obj->type_code;
375
                $this->type_label = $obj->type_label;
376
377
                $this->bank_account = $obj->fk_account;
378
                $this->bank_line = $obj->fk_bank;
379
            }
380
            $this->db->free($resql);
381
382
            return 1;
383
        } else {
384
            $this->error = "Error " . $this->db->lasterror();
385
            return -1;
386
        }
387
    }
388
389
    /**
390
     *  Create payment of expense report into database.
391
     *  Use this->amounts to have list of lines for the payment
392
     *
393
     * @param User $user User making payment
394
     *
395
     * @return     int                 Return integer <0 if KO, id of payment if OK
396
     */
397
    public function create($user)
398
    {
399
        $error = 0;
400
401
        $now = dol_now();
402
        // Validate parameters
403
        if (!$this->datep) {
404
            $this->error = 'ErrorBadValueForParameterCreatePaymentExpenseReport';
405
            return -1;
406
        }
407
408
        // Clean parameters
409
        if (isset($this->fk_expensereport)) {
410
            $this->fk_expensereport = (int) $this->fk_expensereport;
411
        }
412
        if (isset($this->amount)) {
413
            $this->amount = trim($this->amount);
0 ignored issues
show
Documentation Bug introduced by
It seems like trim($this->amount) of type string is incompatible with the declared type double|integer of property $amount.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
414
        }
415
        if (isset($this->fk_typepayment)) {
416
            $this->fk_typepayment = (int) $this->fk_typepayment;
417
        }
418
        if (isset($this->num_payment)) {
419
            $this->num_payment = trim($this->num_payment);
420
        }
421
        if (isset($this->note)) {
422
            $this->note = trim($this->note);
423
        }
424
        if (isset($this->note_public)) {
425
            $this->note_public = trim($this->note_public);
426
        }
427
        if (isset($this->note_private)) {
428
            $this->note_private = trim($this->note_private);
429
        }
430
        if (isset($this->fk_bank)) {
431
            $this->fk_bank = ((int) $this->fk_bank);
432
        }
433
        if (isset($this->fk_user_creat)) {
434
            $this->fk_user_creat = ((int) $this->fk_user_creat);
435
        }
436
        if (isset($this->fk_user_modif)) {
437
            $this->fk_user_modif = ((int) $this->fk_user_modif);
438
        }
439
440
        $totalamount = 0;
441
        foreach ($this->amounts as $key => $value) {  // How payment is dispatch
442
            $newvalue = price2num($value, 'MT');
443
            $this->amounts[$key] = $newvalue;
444
            $totalamount += $newvalue;
445
        }
446
        $totalamount = price2num($totalamount);
447
448
        // Check parameters
449
        if ($totalamount == 0) {
450
            return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
451
        }
452
453
454
        $this->db->begin();
455
456
        if ($totalamount != 0) {
457
            $sql = "INSERT INTO " . MAIN_DB_PREFIX . "payment_expensereport (fk_expensereport, datec, datep, amount,";
458
            $sql .= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
459
            $sql .= " VALUES ($this->fk_expensereport, '" . $this->db->idate($now) . "',";
460
            $sql .= " '" . $this->db->idate($this->datep) . "',";
461
            $sql .= " " . price2num($totalamount) . ",";
462
            $sql .= " " . ((int) $this->fk_typepayment) . ", '" . $this->db->escape($this->num_payment) . "', '" . $this->db->escape($this->note_public) . "', " . ((int) $user->id) . ",";
463
            $sql .= " 0)";  // fk_bank is ID of transaction into ll_bank
464
465
            dol_syslog(get_class($this) . "::create", LOG_DEBUG);
466
            $resql = $this->db->query($sql);
467
            if ($resql) {
468
                $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "payment_expensereport");
469
            } else {
470
                $error++;
471
            }
472
        }
473
474
        if ($totalamount != 0 && !$error) {
475
            $this->amount = $totalamount;
476
            $this->db->commit();
477
            return $this->id;
478
        } else {
479
            $this->error = $this->db->error();
480
            $this->db->rollback();
481
            return -1;
482
        }
483
    }
484
485
    /**
486
     *  Return the label of a given status
487
     *
488
     * @param int $status Id status
489
     * @param int $mode   0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short
490
     *                    label + Picto, 6=Long label + Picto
491
     *
492
     * @return string                 Label of status
493
     */
494
    public function LibStatut($status, $mode = 0)
495
    {
496
        // phpcs:enable
497
        //global $langs;
498
499
        return '';
500
    }
501
502
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
503
504
    /**
505
     *  Initialise an instance with random values.
506
     *  Used to build previews or test instances.
507
     *  id must be 0 if object instance is a specimen.
508
     *
509
     * @return int
510
     */
511
    public function initAsSpecimen()
512
    {
513
        $this->id = 0;
514
515
        $this->fk_expensereport = 0;
516
        $this->datec = dol_now();
517
        $this->tms = dol_now();
0 ignored issues
show
Deprecated Code introduced by
The property DoliCore\Base\GenericDocument::$tms has been deprecated: Use $date_modification ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

517
        /** @scrutinizer ignore-deprecated */ $this->tms = dol_now();

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
518
        $this->datep = dol_now();
519
        $this->amount = 100;
520
        $this->fk_typepayment = 0;
521
        $this->num_payment = '123456';
522
        $this->note_public = 'Public note';
523
        $this->note_private = 'Private note';
524
        $this->fk_bank = 0;
525
        $this->fk_user_creat = 0;
526
        $this->fk_user_modif = 0;
527
528
        return 1;
529
    }
530
531
    /**
532
     *      Add record into bank for payment with links between this bank record and invoices of payment.
533
     *      All payment properties must have been set first like after a call to create().
534
     *
535
     * @param User   $user            Object of user making payment
536
     * @param string $mode            'payment_expensereport'
537
     * @param string $label           Label to use in bank record
538
     * @param int    $accountid       Id of bank account to do link with
539
     * @param string $emetteur_nom    Name of transmitter
540
     * @param string $emetteur_banque Name of bank
541
     *
542
     * @return int                         Return integer <0 if KO, >0 if OK
543
     */
544
    public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
545
    {
546
        global $langs;
547
548
        $error = 0;
549
550
        if (isModEnabled("bank")) {
551
            $acc = new Account($this->db);
552
            $acc->fetch($accountid);
553
554
            //Fix me field
555
            $total = $this->amount;
556
557
            if ($mode == 'payment_expensereport') {
558
                $amount = $total;
559
            }
560
561
            // Insert payment into llx_bank
562
            $bank_line_id = $acc->addline(
563
                $this->datep,
564
                $this->fk_typepayment, // Payment mode id or code ("CHQ or VIR for example")
565
                $label,
566
                -$amount,
567
                $this->num_payment,
568
                '',
569
                $user,
570
                $emetteur_nom,
571
                $emetteur_banque
572
            );
573
574
            // Update fk_bank in llx_paiement.
575
            // So we will know the payment that has generated the bank transaction
576
            if ($bank_line_id > 0) {
577
                $result = $this->update_fk_bank($bank_line_id);
578
                if ($result <= 0) {
579
                    $error++;
580
                    dol_print_error($this->db);
581
                }
582
583
                // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction
584
                $url = '';
585
                if ($mode == 'payment_expensereport') {
586
                    $url = DOL_URL_ROOT . '/expensereport/payment/card.php?rowid=';
587
                }
588
                if ($url) {
589
                    $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
590
                    if ($result <= 0) {
591
                        $error++;
592
                        dol_print_error($this->db);
593
                    }
594
                }
595
596
                // Add link 'user' in bank_url between user and bank transaction
597
                if (!$error) {
598
                    foreach ($this->amounts as $key => $value) {  // We should have always same user but we loop in case of.
599
                        if ($mode == 'payment_expensereport') {
600
                            $fuser = new User($this->db);
601
                            $fuser->fetch($key);
602
603
                            $result = $acc->add_url_line(
604
                                $bank_line_id,
605
                                $fuser->id,
606
                                DOL_URL_ROOT . '/user/card.php?id=',
607
                                $fuser->getFullName($langs),
608
                                'user'
609
                            );
610
                            if ($result <= 0) {
611
                                $this->error = $this->db->lasterror();
612
                                dol_syslog(get_class($this) . '::addPaymentToBank ' . $this->error);
613
                                $error++;
614
                            }
615
                        }
616
                    }
617
                }
618
            } else {
619
                $this->error = $acc->error;
620
                $this->errors = $acc->errors;
621
                $error++;
622
            }
623
        }
624
625
        if (!$error) {
626
            return 1;
627
        } else {
628
            return -1;
629
        }
630
    }
631
632
    /**
633
     *  Update link between the expense report payment and the generated line in llx_bank
634
     *
635
     * @param int $id_bank Id if bank
636
     *
637
     * @return int                      >0 if OK, <=0 if KO
638
     */
639
    public function update_fk_bank($id_bank)
640
    {
641
        // phpcs:enable
642
        $sql = "UPDATE " . MAIN_DB_PREFIX . "payment_expensereport SET fk_bank = " . ((int) $id_bank) . " WHERE rowid = " . ((int) $this->id);
643
644
        dol_syslog(get_class($this) . "::update_fk_bank", LOG_DEBUG);
645
        $result = $this->db->query($sql);
646
        if ($result) {
647
            return 1;
648
        } else {
649
            $this->error = $this->db->error();
650
            return 0;
651
        }
652
    }
653
654
655
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
656
657
    /**
658
     *    Tab information on object
659
     *
660
     * @param int $id Payment id
661
     *
662
     * @return  void
663
     */
664
    public function info($id)
665
    {
666
        $sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms';
667
        $sql .= ' FROM ' . MAIN_DB_PREFIX . 'payment_expensereport as e';
668
        $sql .= ' WHERE e.rowid = ' . ((int) $id);
669
670
        dol_syslog(get_class($this) . '::info', LOG_DEBUG);
671
        $result = $this->db->query($sql);
672
673
        if ($result) {
674
            if ($this->db->num_rows($result)) {
675
                $obj = $this->db->fetch_object($result);
676
677
                $this->id = $obj->rowid;
678
679
                $this->user_creation_id = $obj->fk_user_creat;
680
                $this->user_modification_id = $obj->fk_user_modif;
681
                $this->date_creation = $this->db->jdate($obj->datec);
682
                $this->date_modification = $this->db->jdate($obj->tms);
683
            }
684
            $this->db->free($result);
685
        } else {
686
            dol_print_error($this->db);
687
        }
688
    }
689
690
    /**
691
     *  Return clicable link of object (with eventually picto)
692
     *
693
     * @param string $option    Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
694
     * @param array  $arraydata Array of data
695
     *
696
     * @return     string                              HTML Code for Kanban thumb.
697
     */
698
    public function getKanbanView($option = '', $arraydata = null)
699
    {
700
        global $langs;
701
702
        $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
703
704
        $return = '<div class="box-flex-item box-flex-grow-zero">';
705
        $return .= '<div class="info-box info-box-sm">';
706
        $return .= '<span class="info-box-icon bg-infobox-action">';
707
        $return .= img_picto('', $this->picto);
708
        $return .= '</span>';
709
        $return .= '<div class="info-box-content">';
710
        $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . (method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref) . '</span>';
711
        if ($selected >= 0) {
712
            $return .= '<input id="cb' . $this->id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this->id . '"' . ($selected ? ' checked="checked"' : '') . '>';
713
        }
714
        if (property_exists($this, 'datep')) {
715
            $return .= '<br><span class="opacitymedium">' . $langs->trans("Date") . '</span> : <span class="info-box-label">' . dol_print_date($this->db->jdate($this->datep), 'dayhour') . '</span>';
716
        }
717
        if (property_exists($this, 'fk_typepayment')) {
718
            $return .= '<br><span class="opacitymedium">' . $langs->trans("Type") . '</span> : <span class="info-box-label">' . $this->fk_typepayment . '</span>';
719
        }
720
        if (property_exists($this, 'fk_bank') && !is_null($this->fk_bank)) {
721
            $return .= '<br><span class="opacitymedium">' . $langs->trans("BankAccount") . '</span> : <span class="info-box-label">' . $this->fk_bank . '</span>';
722
        }
723
        if (property_exists($this, 'amount')) {
724
            $return .= '<br><span class="opacitymedium">' . $langs->trans("Amount") . '</span> : <span class="info-box-label amount">' . price($this->amount) . '</span>';
725
        }
726
        if (method_exists($this, 'getLibStatut')) {
727
            $return .= '<br><div class="info-box-status">' . $this->getLibStatut(3) . '</div>';
728
        }
729
        $return .= '</div>';
730
        $return .= '</div>';
731
        $return .= '</div>';
732
        return $return;
733
    }
734
735
    /**
736
     *  Return clicable name (with picto eventually)
737
     *
738
     * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
739
     * @param int $maxlen    Longueur max libelle
740
     *
741
     * @return string                  Chaine avec URL
742
     */
743
    public function getNomUrl($withpicto = 0, $maxlen = 0)
744
    {
745
        global $langs, $hookmanager;
746
747
        $result = '';
748
749
        if (empty($this->ref)) {
750
            $this->ref = $this->label;
751
        }
752
        $label = img_picto('', $this->picto) . ' <u class="paddingrightonly">' . $langs->trans("Payment") . '</u>';
753
        if (isset($this->status)) {
754
            $label .= ' ' . $this->getLibStatut(5);
755
        }
756
        if (!empty($this->ref)) {
757
            $label .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
758
        }
759
        if (!empty($this->datep)) {
760
            $label .= '<br><b>' . $langs->trans('Date') . ':</b> ' . dol_print_date($this->datep, 'dayhour');
761
        }
762
763
        if (!empty($this->id)) {
764
            $link = '<a href="' . DOL_URL_ROOT . '/expensereport/payment/card.php?id=' . $this->id . '" title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip">';
765
            $linkend = '</a>';
766
767
            if ($withpicto) {
768
                $result .= ($link . img_object($label, 'payment', 'class="classfortooltip"') . $linkend . ' ');
769
            }
770
            if ($withpicto && $withpicto != 2) {
771
                $result .= ' ';
772
            }
773
            if ($withpicto != 2) {
774
                $result .= $link . ($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref) . $linkend;
775
            }
776
        }
777
        global $action;
778
        $hookmanager->initHooks([$this->element . 'dao']);
779
        $parameters = ['id' => $this->id, 'getnomurl' => &$result];
780
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
781
        if ($reshook > 0) {
782
            $result = $hookmanager->resPrint;
783
        } else {
784
            $result .= $hookmanager->resPrint;
785
        }
786
        return $result;
787
    }
788
789
    /**
790
     *  Return the label of the status
791
     *
792
     * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short
793
     *                  label + Picto, 6=Long label + Picto
794
     *
795
     * @return string                 Label of status
796
     */
797
    public function getLibStatut($mode = 0)
798
    {
799
        return '';
800
    }
801
}
802