Passed
Push — EXTRACT_CLASSES ( c25e41...9f3ede )
by Rafael
55:18
created

PaymentDonation   F

Complexity

Total Complexity 95

Size/Duplication

Total Lines 673
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 330
dl 0
loc 673
rs 2
c 0
b 0
f 0
wmc 95

12 Methods

Rating   Name   Duplication   Size   Complexity  
B getNomUrl() 0 35 8
C delete() 0 55 13
A LibStatut() 0 6 1
A createFromClone() 0 38 4
F update() 0 83 29
A fetch() 0 57 3
A getLibStatut() 0 3 1
A update_fk_bank() 0 12 2
A initAsSpecimen() 0 18 1
F create() 0 100 23
A __construct() 0 3 1
B addPaymentToBank() 0 60 9

How to fix   Complexity   

Complex Class

Complex classes like PaymentDonation often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use PaymentDonation, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/* Copyright (C) 2015       Alexandre Spangaro      <[email protected]>
4
 * Copyright (C) 2024       Frédéric France             <[email protected]>
5
 * Copyright (C) 2024       Rafael San José             <[email protected]>
6
 *
7
 * This program is free software; you can redistribute it and/or modify
8
 * it under the terms of the GNU General Public License as published by
9
 * the Free Software Foundation; either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
namespace Dolibarr\Code\Don\Classes;
22
23
use Dolibarr\Core\Base\CommonObject;
24
25
/**
26
 *  \file       htdocs/don/class/paymentdonation.class.php
27
 *  \ingroup    Donation
28
 *  \brief      File of class to manage payment of donations
29
 */
30
31
/**
32
 *  Class to manage payments of donations
33
 */
34
class PaymentDonation extends CommonObject
35
{
36
    /**
37
     * @var string ID to identify managed object
38
     */
39
    public $element = 'payment_donation';
40
41
    /**
42
     * @var string Name of table without prefix where object is stored
43
     */
44
    public $table_element = 'payment_donation';
45
46
    /**
47
     * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
48
     */
49
    public $picto = 'payment';
50
51
    /**
52
     * @var int ID
53
     */
54
    public $rowid;
55
56
    /**
57
     * @var int ID
58
     */
59
    public $fk_donation;
60
61
    public $datec = '';
62
63
    public $datep = '';
64
65
    public $amount; // Total amount of payment
66
67
    public $amounts = array(); // Array of amounts
68
69
    public $fk_typepayment; // Payment mode ID
70
    public $paymenttype;    // Payment mode ID or Code. TODO Use only the code in this field.
71
72
    /**
73
     * @var string      Payment reference
74
     *                  (Cheque or bank transfer reference. Can be "ABC123")
75
     */
76
    public $num_payment;
77
78
    /**
79
     * @var int ID
80
     */
81
    public $fk_bank;
82
83
    /**
84
     * @var int ID
85
     */
86
    public $fk_user_creat;
87
88
    /**
89
     * @var int ID
90
     */
91
    public $fk_user_modif;
92
93
    /**
94
     * @deprecated
95
     * @see $amount, $amounts
96
     */
97
    public $total;
98
99
    public $type_code;
100
    public $type_label;
101
    public $chid;
102
    public $datepaid;
103
    public $bank_account;
104
    public $bank_line;
105
106
    /**
107
     * @var string Id of external payment mode
108
     */
109
    public $ext_payment_id;
110
111
    /**
112
     * @var string Name of external payment mode
113
     */
114
    public $ext_payment_site;
115
116
    /**
117
     *  Constructor
118
     *
119
     * @param DoliDB $db Database handler
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Don\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
120
     */
121
    public function __construct($db)
122
    {
123
        $this->db = $db;
124
    }
125
126
    /**
127
     *  Update database
128
     *
129
     * @param User $user User that modify
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Don\Classes\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
130
     * @param int $notrigger 0=launch triggers after, 1=disable triggers
131
     * @return int                     Return integer <0 if KO, >0 if OK
132
     */
133
    public function update($user, $notrigger = 0)
134
    {
135
        global $conf, $langs;
136
        $error = 0;
137
138
        // Clean parameters
139
140
        if (isset($this->fk_donation)) {
141
            $this->fk_donation = (int)$this->fk_donation;
142
        }
143
        if (isset($this->amount)) {
144
            $this->amount = trim($this->amount);
145
        }
146
        if (isset($this->fk_typepayment)) {
147
            $this->fk_typepayment = trim($this->fk_typepayment);
148
        }
149
        if (isset($this->num_payment)) {
150
            $this->num_payment = trim($this->num_payment);
151
        }
152
        if (isset($this->note_public)) {
153
            $this->note_public = trim($this->note_public);
154
        }
155
        if (isset($this->fk_bank)) {
156
            $this->fk_bank = (int)$this->fk_bank;
157
        }
158
        if (isset($this->fk_user_creat)) {
159
            $this->fk_user_creat = (int)$this->fk_user_creat;
160
        }
161
        if (isset($this->fk_user_modif)) {
162
            $this->fk_user_modif = (int)$this->fk_user_modif;
163
        }
164
165
        // Check parameters
166
        // Put here code to add control on parameters values
167
168
        // Update request
169
        $sql = "UPDATE " . MAIN_DB_PREFIX . "payment_donation SET";
170
        $sql .= " fk_donation=" . (isset($this->fk_donation) ? $this->fk_donation : "null") . ",";
171
        $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ",";
172
        $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ",";
173
        $sql .= " datep=" . (dol_strlen($this->datep) != 0 ? "'" . $this->db->idate($this->datep) . "'" : 'null') . ",";
174
        $sql .= " amount=" . (isset($this->amount) ? $this->amount : "null") . ",";
175
        $sql .= " fk_typepayment=" . (isset($this->fk_typepayment) ? $this->fk_typepayment : "null") . ",";
176
        $sql .= " num_payment=" . (isset($this->num_payment) ? "'" . $this->db->escape($this->num_payment) . "'" : "null") . ",";
177
        $sql .= " note=" . (isset($this->note_public) ? "'" . $this->db->escape($this->note_public) . "'" : "null") . ",";
178
        $sql .= " fk_bank=" . (isset($this->fk_bank) ? $this->fk_bank : "null") . ",";
179
        $sql .= " fk_user_creat=" . (isset($this->fk_user_creat) ? $this->fk_user_creat : "null") . ",";
180
        $sql .= " fk_user_modif=" . (isset($this->fk_user_modif) ? $this->fk_user_modif : "null");
181
        $sql .= " WHERE rowid=" . (int)$this->id;
182
183
        $this->db->begin();
184
185
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
186
        $resql = $this->db->query($sql);
187
        if (!$resql) {
188
            $error++;
189
            $this->errors[] = "Error " . $this->db->lasterror();
190
        }
191
192
        if (!$error) {
193
            if (!$notrigger) {
194
                if (!$error && !$notrigger) {
195
                    // Call triggers
196
                    $result = $this->call_trigger('DONATION_PAYMENT_MODIFY', $user);
197
                    if ($result < 0) {
198
                        $error++;
199
                    }
200
                    // End call triggers
201
                }
202
            }
203
        }
204
205
        // Commit or rollback
206
        if ($error) {
207
            foreach ($this->errors as $errmsg) {
208
                dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
209
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
210
            }
211
            $this->db->rollback();
212
            return -1 * $error;
213
        } else {
214
            $this->db->commit();
215
            return 1;
216
        }
217
    }
218
219
    /**
220
     *  Delete object in database
221
     *
222
     * @param User $user User that delete
223
     * @param int $notrigger 0=launch triggers after, 1=disable triggers
224
     * @return int                     Return integer <0 if KO, >0 if OK
225
     */
226
    public function delete($user, $notrigger = 0)
227
    {
228
        global $conf, $langs;
229
        $error = 0;
230
231
        $this->db->begin();
232
233
        if (!$error) {
234
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "bank_url";
235
            $sql .= " WHERE type='payment_donation' AND url_id=" . (int)$this->id;
236
237
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
238
            $resql = $this->db->query($sql);
239
            if (!$resql) {
240
                $error++;
241
                $this->errors[] = "Error " . $this->db->lasterror();
242
            }
243
        }
244
245
        if (!$error) {
246
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "payment_donation";
247
            $sql .= " WHERE rowid=" . ((int)$this->id);
248
249
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
250
            $resql = $this->db->query($sql);
251
            if (!$resql) {
252
                $error++;
253
                $this->errors[] = "Error " . $this->db->lasterror();
254
            }
255
        }
256
257
        if (!$error) {
258
            if (!$notrigger) {
259
                if (!$error && !$notrigger) {
260
                    // Call triggers
261
                    $result = $this->call_trigger('DONATION_PAYMENT_DELETE', $user);
262
                    if ($result < 0) {
263
                        $error++;
264
                    }
265
                    // End call triggers
266
                }
267
            }
268
        }
269
270
        // Commit or rollback
271
        if ($error) {
272
            foreach ($this->errors as $errmsg) {
273
                dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
274
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
275
            }
276
            $this->db->rollback();
277
            return -1 * $error;
278
        } else {
279
            $this->db->commit();
280
            return 1;
281
        }
282
    }
283
284
    /**
285
     *  Load an object from its id and create a new one in database
286
     *
287
     * @param User $user User making the clone
288
     * @param int $fromid Id of object to clone
289
     * @return int                     New id of clone
290
     */
291
    public function createFromClone(User $user, $fromid)
292
    {
293
        $error = 0;
294
295
        $object = new PaymentDonation($this->db);
296
297
        $this->db->begin();
298
299
        // Load source object
300
        $object->fetch($fromid);
301
        $object->id = 0;
302
        $object->statut = 0;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$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

302
        /** @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...
303
304
        // Clear fields
305
        // ...
306
307
        // Create clone
308
        $object->context['createfromclone'] = 'createfromclone';
309
        $result = $object->create($user);
310
311
        // Other options
312
        if ($result < 0) {
313
            $this->error = $object->error;
314
            $error++;
315
        }
316
317
        if (!$error) {
318
        }
319
320
        unset($object->context['createfromclone']);
321
322
        // End
323
        if (!$error) {
324
            $this->db->commit();
325
            return $object->id;
326
        } else {
327
            $this->db->rollback();
328
            return -1;
329
        }
330
    }
331
332
    /**
333
     *  Load object in memory from database
334
     *
335
     * @param int $id Id object
336
     * @return int                 Return integer <0 if KO, >0 if OK
337
     */
338
    public function fetch($id)
339
    {
340
        global $langs;
341
        $sql = "SELECT";
342
        $sql .= " t.rowid,";
343
        $sql .= " t.fk_donation,";
344
        $sql .= " t.datec,";
345
        $sql .= " t.tms,";
346
        $sql .= " t.datep,";
347
        $sql .= " t.amount,";
348
        $sql .= " t.fk_typepayment,";
349
        $sql .= " t.num_payment,";
350
        $sql .= " t.note as note_public,";
351
        $sql .= " t.fk_bank,";
352
        $sql .= " t.fk_user_creat,";
353
        $sql .= " t.fk_user_modif,";
354
        $sql .= " pt.code as type_code, pt.libelle as type_label,";
355
        $sql .= ' b.fk_account';
356
        $sql .= " FROM " . MAIN_DB_PREFIX . "payment_donation as t";
357
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_paiement as pt ON t.fk_typepayment = pt.id";
358
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON t.fk_bank = b.rowid';
359
        $sql .= " WHERE t.rowid = " . ((int)$id);
360
361
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
362
        $resql = $this->db->query($sql);
363
        if ($resql) {
364
            if ($this->db->num_rows($resql)) {
365
                $obj = $this->db->fetch_object($resql);
366
367
                $this->id = $obj->rowid;
368
                $this->ref = $obj->rowid;
369
370
                $this->fk_donation = $obj->fk_donation;
371
                $this->datec = $this->db->jdate($obj->datec);
372
                $this->tms = $this->db->jdate($obj->tms);
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$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

372
                /** @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...
373
                $this->datep = $this->db->jdate($obj->datep);
374
                $this->amount = $obj->amount;
375
                $this->fk_typepayment = $obj->fk_typepayment;   // Id on type of payent
376
                $this->paymenttype = $obj->fk_typepayment;   // Id on type of payment. We should store the code into paymenttype.
377
                $this->num_payment = $obj->num_payment;
378
                $this->note_public = $obj->note_public;
379
                $this->fk_bank = $obj->fk_bank;
380
                $this->fk_user_creat = $obj->fk_user_creat;
381
                $this->fk_user_modif = $obj->fk_user_modif;
382
383
                $this->type_code = $obj->type_code;
384
                $this->type_label = $obj->type_label;
385
386
                $this->bank_account = $obj->fk_account;
387
                $this->bank_line = $obj->fk_bank;
388
            }
389
            $this->db->free($resql);
390
391
            return 1;
392
        } else {
393
            $this->error = "Error " . $this->db->lasterror();
394
            return -1;
395
        }
396
    }
397
398
    /**
399
     *  Create payment of donation into database.
400
     *  Use this->amounts to have list of lines for the payment
401
     *
402
     * @param User $user User making payment
403
     * @param int $notrigger 0=launch triggers after, 1=disable triggers
404
     * @return     int                         Return integer <0 if KO, id of payment if OK
405
     */
406
    public function create($user, $notrigger = 0)
407
    {
408
        $error = 0;
409
410
        $now = dol_now();
411
412
        // Validate parameters
413
        if (!$this->datep) {
414
            $this->error = 'ErrorBadValueForParameterCreatePaymentDonation';
415
            return -1;
416
        }
417
418
        // Clean parameters
419
        if (isset($this->chid)) {
420
            $this->chid = (int)$this->chid;
421
        } elseif (isset($this->fk_donation)) {
422
            // NOTE : The property used in INSERT for fk_donation is not fk_donation but chid
423
            //        (keep priority to chid property)
424
            $this->chid = (int)$this->fk_donation;
425
        }
426
        if (isset($this->fk_donation)) {
427
            $this->fk_donation = (int)$this->fk_donation;
428
        }
429
        if (isset($this->amount)) {
430
            $this->amount = trim($this->amount);
431
        }
432
        if (isset($this->fk_typepayment)) {
433
            $this->fk_typepayment = trim($this->fk_typepayment);
434
        }
435
        if (isset($this->num_payment)) {
436
            $this->num_payment = trim($this->num_payment);
437
        }
438
        if (isset($this->note_public)) {
439
            $this->note_public = trim($this->note_public);
440
        }
441
        if (isset($this->fk_bank)) {
442
            $this->fk_bank = (int)$this->fk_bank;
443
        }
444
        if (isset($this->fk_user_creat)) {
445
            $this->fk_user_creat = (int)$this->fk_user_creat;
446
        }
447
        if (isset($this->fk_user_modif)) {
448
            $this->fk_user_modif = (int)$this->fk_user_modif;
449
        }
450
451
        $totalamount = 0;
452
        foreach ($this->amounts as $key => $value) {  // How payment is dispatch
453
            $newvalue = price2num($value, 'MT');
454
            $this->amounts[$key] = $newvalue;
455
            $totalamount += $newvalue;
456
        }
457
        $totalamount = price2num($totalamount);
458
459
        // Check parameters
460
        if ($totalamount == 0) {
461
            return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
462
        }
463
464
465
        $this->db->begin();
466
467
        if ($totalamount != 0) {
468
            $sql = "INSERT INTO " . MAIN_DB_PREFIX . "payment_donation (fk_donation, datec, datep, amount,";
469
            $sql .= " fk_typepayment, num_payment, note, ext_payment_id, ext_payment_site,";
470
            $sql .= " fk_user_creat, fk_bank)";
471
            $sql .= " VALUES (" . ((int)$this->chid) . ", '" . $this->db->idate($now) . "',";
472
            $sql .= " '" . $this->db->idate($this->datep) . "',";
473
            $sql .= " " . ((float)price2num($totalamount)) . ",";
474
            $sql .= " " . ((int)$this->paymenttype) . ", '" . $this->db->escape($this->num_payment) . "', '" . $this->db->escape($this->note_public) . "', ";
475
            $sql .= " " . ($this->ext_payment_id ? "'" . $this->db->escape($this->ext_payment_id) . "'" : "null") . ", " . ($this->ext_payment_site ? "'" . $this->db->escape($this->ext_payment_site) . "'" : "null") . ",";
476
            $sql .= " " . ((int)$user->id) . ", 0)";
477
478
            dol_syslog(get_class($this) . "::create", LOG_DEBUG);
479
            $resql = $this->db->query($sql);
480
            if ($resql) {
481
                $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "payment_donation");
482
                $this->ref = (string)$this->id;
483
            } else {
484
                $error++;
485
            }
486
        }
487
488
        if (!$error && !$notrigger) {
489
            // Call triggers
490
            $result = $this->call_trigger('DONATION_PAYMENT_CREATE', $user);
491
            if ($result < 0) {
492
                $error++;
493
            }
494
            // End call triggers
495
        }
496
497
        if ($totalamount != 0 && !$error) {
498
            $this->amount = $totalamount;
499
            $this->total = $totalamount; // deprecated
500
            $this->db->commit();
501
            return $this->id;
502
        } else {
503
            $this->error = $this->db->error();
504
            $this->db->rollback();
505
            return -1;
506
        }
507
    }
508
509
    /**
510
     *  Return the label of the status
511
     *
512
     * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
513
     * @return string                 Label of status
514
     */
515
    public function getLibStatut($mode = 0)
516
    {
517
        return '';
518
    }
519
520
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
521
522
    /**
523
     *  Return the label of a given status
524
     *
525
     * @param int $status Id status
526
     * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
527
     * @return string                 Label of status
528
     */
529
    public function LibStatut($status, $mode = 0)
530
    {
531
        // phpcs:enable
532
        global $langs;
533
534
        return '';
535
    }
536
537
538
    /**
539
     *  Initialise an instance with random values.
540
     *  Used to build previews or test instances.
541
     *  id must be 0 if object instance is a specimen.
542
     *
543
     * @return int
544
     */
545
    public function initAsSpecimen()
546
    {
547
        $this->id = 0;
548
549
        $this->fk_donation = 0;
550
        $this->datec = '';
551
        $this->tms = dol_now();
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Core\Base\CommonObject::$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

551
        /** @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...
552
        $this->datep = '';
553
        $this->amount = '';
554
        $this->fk_typepayment = '';
555
        $this->paymenttype = '';
556
        $this->num_payment = '';
557
        $this->note_public = '';
558
        $this->fk_bank = 0;
559
        $this->fk_user_creat = dol_now();
560
        $this->fk_user_modif = dol_now();
561
562
        return 1;
563
    }
564
565
566
    /**
567
     *      Add record into bank for payment with links between this bank record and invoices of payment.
568
     *      All payment properties must have been set first like after a call to create().
569
     *
570
     * @param User $user Object of user making payment
571
     * @param string $mode 'payment_donation'
572
     * @param string $label Label to use in bank record
573
     * @param int $accountid Id of bank account to do link with
574
     * @param string $emetteur_nom Name of transmitter
575
     * @param string $emetteur_banque Name of bank
576
     * @return int                         Return integer <0 if KO, >0 if OK
577
     */
578
    public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
579
    {
580
        global $conf;
581
582
        $error = 0;
583
584
        if (isModEnabled("bank")) {
585
            require_once constant('DOL_DOCUMENT_ROOT') . '/compta/bank/class/account.class.php';
586
587
            $acc = new Account($this->db);
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Don\Classes\Account was not found. Did you mean Account? If so, make sure to prefix the type with \.
Loading history...
588
            $acc->fetch($accountid);
589
590
            $total = $this->total;
591
            if ($mode == 'payment_donation') {
592
                $amount = $total;
593
            }
594
            // Insert payment into llx_bank
595
            $bank_line_id = $acc->addline(
596
                $this->datep,
597
                $this->paymenttype, // Payment mode id or code ("CHQ or VIR for example")
598
                $label,
599
                $amount,
600
                $this->num_payment,
601
                '',
602
                $user,
603
                $emetteur_nom,
604
                $emetteur_banque
605
            );
606
607
            // Update fk_bank in llx_paiement.
608
            // On connait ainsi le paiement qui a genere l'ecriture bancaire
609
            if ($bank_line_id > 0) {
610
                $result = $this->update_fk_bank($bank_line_id);
611
                if ($result <= 0) {
612
                    $error++;
613
                    dol_print_error($this->db);
614
                }
615
616
                // Add link 'payment', 'payment_supplier', 'payment_donation' in bank_url between payment and bank transaction
617
                $url = '';
618
                if ($mode == 'payment_donation') {
619
                    $url = constant('BASE_URL') . '/don/payment/card.php?rowid=';
620
                }
621
                if ($url) {
622
                    $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
623
                    if ($result <= 0) {
624
                        $error++;
625
                        dol_print_error($this->db);
626
                    }
627
                }
628
            } else {
629
                $this->error = $acc->error;
630
                $error++;
631
            }
632
        }
633
634
        if (!$error) {
635
            return 1;
636
        } else {
637
            return -1;
638
        }
639
    }
640
641
642
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
643
644
    /**
645
     *  Update link between the donation payment and the generated line in llx_bank
646
     *
647
     * @param int $id_bank Id if bank
648
     * @return int                      >0 if OK, <=0 if KO
649
     */
650
    public function update_fk_bank($id_bank)
651
    {
652
        // phpcs:enable
653
        $sql = "UPDATE " . MAIN_DB_PREFIX . "payment_donation SET fk_bank = " . (int)$id_bank . " WHERE rowid = " . (int)$this->id;
654
655
        dol_syslog(get_class($this) . "::update_fk_bank", LOG_DEBUG);
656
        $result = $this->db->query($sql);
657
        if ($result) {
658
            return 1;
659
        } else {
660
            $this->error = $this->db->error();
661
            return 0;
662
        }
663
    }
664
665
    /**
666
     *  Return clicable name (with picto eventually)
667
     *
668
     * @param int $withpicto 0=No picto, 1=Include picto into link, 2=Only picto
669
     * @param int $maxlen Max length
670
     * @return string                  String with URL
671
     */
672
    public function getNomUrl($withpicto = 0, $maxlen = 0)
673
    {
674
        global $langs, $hookmanager;
675
676
        $result = '';
677
678
        $label = '<u>' . $langs->trans("DonationPayment") . '</u>';
679
        $label .= '<br>';
680
        $label .= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
681
682
        if (!empty($this->id)) {
683
            $link = '<a href="' . constant('BASE_URL') . '/don/payment/card.php?id=' . $this->id . '" title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip">';
684
            $linkend = '</a>';
685
686
            if ($withpicto) {
687
                $result .= ($link . img_object($label, 'payment', 'class="classfortooltip"') . $linkend . ' ');
688
            }
689
            if ($withpicto && $withpicto != 2) {
690
                $result .= ' ';
691
            }
692
            if ($withpicto != 2) {
693
                $result .= $link . ($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref) . $linkend;
694
            }
695
        }
696
697
        global $action;
698
        $hookmanager->initHooks(array($this->element . 'dao'));
699
        $parameters = array('id' => $this->id, 'getnomurl' => &$result);
700
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
701
        if ($reshook > 0) {
702
            $result = $hookmanager->resPrint;
703
        } else {
704
            $result .= $hookmanager->resPrint;
705
        }
706
        return $result;
707
    }
708
}
709