Passed
Push — EXTRACT_CLASSES ( 9f3ede...ff35ec )
by Rafael
76:09 queued 20:57
created

Loan::getNomUrl()   F

Complexity

Conditions 23
Paths 2592

Size

Total Lines 62
Code Lines 41

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 23
eloc 41
nc 2592
nop 6
dl 0
loc 62
rs 0
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) 2014-2018  Alexandre Spangaro          <[email protected]>
4
 * Copyright (C) 2015-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\Loan\Classes;
22
23
use Dolibarr\Core\Base\CommonObject;
24
25
/**
26
 *  \file       htdocs/loan/class/loan.class.php
27
 *  \ingroup    loan
28
 *  \brief      Class for loan module
29
 */
30
31
/**
32
 *  Loan
33
 */
34
class Loan extends CommonObject
35
{
36
    /**
37
     * @var string ID to identify managed object
38
     */
39
    public $element = 'loan';
40
41
    public $table = 'loan';
42
43
    /**
44
     * @var string Name of table without prefix where object is stored
45
     */
46
    public $table_element = 'loan';
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 = 'money-bill-alt';
52
53
    /**
54
     * @var int ID
55
     */
56
    public $rowid;
57
58
    public $datestart;
59
    public $dateend;
60
61
    /**
62
     * @var string Loan label
63
     */
64
    public $label;
65
66
    public $capital;
67
    public $nbterm;
68
    public $rate;
69
    public $paid;
70
    public $account_capital;
71
    public $account_insurance;
72
    public $account_interest;
73
    public $accountancy_account_capital;
74
    public $accountancy_account_insurance;
75
    public $accountancy_account_interest;
76
77
    /**
78
     * @var integer|string date_creation
79
     */
80
    public $date_creation;
81
82
    /**
83
     * @var integer|string date_modification
84
     */
85
    public $date_modification;
86
87
    /**
88
     * @var integer|string date_validation
89
     */
90
    public $date_validation;
91
92
    public $insurance_amount;
93
94
    /**
95
     * @var int Bank ID
96
     */
97
    public $fk_bank;
98
99
    /**
100
     * @var int ID
101
     */
102
    public $fk_user_creat;
103
104
    /**
105
     * @var int ID
106
     */
107
    public $fk_user_modif;
108
109
    /**
110
     * @var int ID
111
     */
112
    public $fk_project;
113
114
    /**
115
     * @var float totalpaid
116
     */
117
    public $totalpaid;
118
119
    const STATUS_UNPAID = 0;
120
    const STATUS_PAID = 1;
121
    const STATUS_STARTED = 2;
122
123
124
    /**
125
     * Constructor
126
     *
127
     * @param   DoliDB      $db     Database handler
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Loan\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
128
     */
129
    public function __construct($db)
130
    {
131
        $this->db = $db;
132
    }
133
134
    /**
135
     *  Load object in memory from database
136
     *
137
     *  @param  int     $id      id object
138
     *  @return int              Return integer <0 error , >=0 no error
139
     */
140
    public function fetch($id)
141
    {
142
        $sql = "SELECT l.rowid, l.label, l.capital, l.datestart, l.dateend, l.nbterm, l.rate, l.note_private, l.note_public, l.insurance_amount,";
143
        $sql .= " l.paid, l.fk_bank, l.accountancy_account_capital, l.accountancy_account_insurance, l.accountancy_account_interest, l.fk_projet as fk_project";
144
        $sql .= " FROM " . MAIN_DB_PREFIX . "loan as l";
145
        $sql .= " WHERE l.rowid = " . ((int) $id);
146
147
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
148
        $resql = $this->db->query($sql);
149
        if ($resql) {
150
            if ($this->db->num_rows($resql)) {
151
                $obj = $this->db->fetch_object($resql);
152
153
                $this->id = $obj->rowid;
154
                $this->ref = $obj->rowid;
155
                $this->datestart = $this->db->jdate($obj->datestart);
156
                $this->dateend              = $this->db->jdate($obj->dateend);
157
                $this->label                = $obj->label;
158
                $this->capital              = $obj->capital;
159
                $this->nbterm = $obj->nbterm;
160
                $this->rate                 = $obj->rate;
161
                $this->note_private = $obj->note_private;
162
                $this->note_public = $obj->note_public;
163
                $this->insurance_amount = $obj->insurance_amount;
164
                $this->paid = $obj->paid;
165
                $this->fk_bank = $obj->fk_bank;
166
167
                $this->account_capital = $obj->accountancy_account_capital;
168
                $this->account_insurance    = $obj->accountancy_account_insurance;
169
                $this->account_interest     = $obj->accountancy_account_interest;
170
                $this->fk_project = $obj->fk_project;
171
172
                $this->db->free($resql);
173
                return 1;
174
            } else {
175
                $this->db->free($resql);
176
                return 0;
177
            }
178
        } else {
179
            $this->error = $this->db->lasterror();
180
            return -1;
181
        }
182
    }
183
184
185
    /**
186
     *  Create a loan into database
187
     *
188
     *  @param  User    $user   User making creation
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Loan\Classes\User was not found. Did you mean User? If so, make sure to prefix the type with \.
Loading history...
189
     *  @return int             Return integer <0 if KO, id if OK
190
     */
191
    public function create($user)
192
    {
193
        global $conf, $langs;
194
195
        $error = 0;
196
197
        $now = dol_now();
198
199
        // clean parameters
200
        $newcapital = price2num($this->capital, 'MT');
201
        if (empty($this->insurance_amount)) {
202
            $this->insurance_amount = 0;
203
        }
204
        $newinsuranceamount = price2num($this->insurance_amount, 'MT');
205
        if (isset($this->note_private)) {
206
            $this->note_private = trim($this->note_private);
207
        }
208
        if (isset($this->note_public)) {
209
            $this->note_public = trim($this->note_public);
210
        }
211
        if (isset($this->account_capital)) {
212
            $this->account_capital = trim($this->account_capital);
213
        }
214
        if (isset($this->account_insurance)) {
215
            $this->account_insurance = trim($this->account_insurance);
216
        }
217
        if (isset($this->account_interest)) {
218
            $this->account_interest = trim($this->account_interest);
219
        }
220
        if (isset($this->fk_bank)) {
221
            $this->fk_bank = (int) $this->fk_bank;
222
        }
223
        if (isset($this->fk_user_creat)) {
224
            $this->fk_user_creat = (int) $this->fk_user_creat;
225
        }
226
        if (isset($this->fk_user_modif)) {
227
            $this->fk_user_modif = (int) $this->fk_user_modif;
228
        }
229
        if (isset($this->fk_project)) {
230
            $this->fk_project = (int) $this->fk_project;
231
        }
232
233
        // Check parameters
234
        if (!($newcapital > 0) || empty($this->datestart) || empty($this->dateend)) {
235
            $this->error = "ErrorBadParameter";
236
            return -2;
237
        }
238
        if (isModEnabled('accounting') && empty($this->account_capital)) {
239
            $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyCapitalCode"));
240
            return -2;
241
        }
242
        if (isModEnabled('accounting') && empty($this->account_insurance)) {
243
            $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInsuranceCode"));
244
            return -2;
245
        }
246
        if (isModEnabled('accounting') && empty($this->account_interest)) {
247
            $this->error = $langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("LoanAccountancyInterestCode"));
248
            return -2;
249
        }
250
251
        $this->db->begin();
252
253
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . "loan (label, fk_bank, capital, datestart, dateend, nbterm, rate, note_private, note_public,";
254
        $sql .= " accountancy_account_capital, accountancy_account_insurance, accountancy_account_interest, entity,";
255
        $sql .= " datec, fk_projet, fk_user_author, insurance_amount)";
256
        $sql .= " VALUES ('" . $this->db->escape($this->label) . "',";
257
        $sql .= " '" . $this->db->escape($this->fk_bank) . "',";
258
        $sql .= " '" . price2num($newcapital) . "',";
259
        $sql .= " '" . $this->db->idate($this->datestart) . "',";
260
        $sql .= " '" . $this->db->idate($this->dateend) . "',";
261
        $sql .= " '" . $this->db->escape($this->nbterm) . "',";
262
        $sql .= " '" . $this->db->escape($this->rate) . "',";
263
        $sql .= " '" . $this->db->escape($this->note_private) . "',";
264
        $sql .= " '" . $this->db->escape($this->note_public) . "',";
265
        $sql .= " '" . $this->db->escape($this->account_capital) . "',";
266
        $sql .= " '" . $this->db->escape($this->account_insurance) . "',";
267
        $sql .= " '" . $this->db->escape($this->account_interest) . "',";
268
        $sql .= " " . $conf->entity . ",";
269
        $sql .= " '" . $this->db->idate($now) . "',";
270
        $sql .= " " . (empty($this->fk_project) ? 'NULL' : $this->fk_project) . ",";
271
        $sql .= " " . $user->id . ",";
272
        $sql .= " '" . price2num($newinsuranceamount) . "'";
273
        $sql .= ")";
274
275
        dol_syslog(get_class($this) . "::create", LOG_DEBUG);
276
        $resql = $this->db->query($sql);
277
        if ($resql) {
278
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "loan");
279
280
            //dol_syslog("Loans::create this->id=".$this->id);
281
            $this->db->commit();
282
            return $this->id;
283
        } else {
284
            $this->error = $this->db->error();
285
            $this->db->rollback();
286
            return -1;
287
        }
288
    }
289
290
291
    /**
292
     *  Delete a loan
293
     *
294
     *  @param  User    $user   Object user making delete
295
     *  @return int             Return integer <0 if KO, >0 if OK
296
     */
297
    public function delete($user)
298
    {
299
        $error = 0;
300
301
        $this->db->begin();
302
303
        // Get bank transaction lines for this loan
304
        include_once DOL_DOCUMENT_ROOT . '/compta/bank/class/account.class.php';
305
        $account = new Account($this->db);
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Loan\Classes\Account was not found. Did you mean Account? If so, make sure to prefix the type with \.
Loading history...
306
        $lines_url = $account->get_url('', $this->id, 'loan');
307
308
        // Delete bank urls
309
        foreach ($lines_url as $line_url) {
310
            if (!$error) {
311
                $accountline = new AccountLine($this->db);
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Loan\Classes\AccountLine was not found. Did you mean AccountLine? If so, make sure to prefix the type with \.
Loading history...
312
                $accountline->fetch($line_url['fk_bank']);
313
                $result = $accountline->delete_urls($user);
314
                if ($result < 0) {
315
                    $error++;
316
                }
317
            }
318
        }
319
320
        // Delete payments
321
        if (!$error) {
322
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "payment_loan where fk_loan=" . ((int) $this->id);
323
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
324
            $resql = $this->db->query($sql);
325
            if (!$resql) {
326
                $error++;
327
                $this->error = $this->db->lasterror();
328
            }
329
        }
330
331
        if (!$error) {
332
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "loan where rowid=" . ((int) $this->id);
333
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
334
            $resql = $this->db->query($sql);
335
            if (!$resql) {
336
                $error++;
337
                $this->error = $this->db->lasterror();
338
            }
339
        }
340
341
        if (!$error) {
342
            $this->db->commit();
343
            return 1;
344
        } else {
345
            $this->db->rollback();
346
            return -1;
347
        }
348
    }
349
350
351
    /**
352
     *  Update loan
353
     *
354
     *  @param  User    $user   User who modified
355
     *  @return int             Return integer <0 if error, >0 if ok
356
     */
357
    public function update($user)
358
    {
359
        $this->db->begin();
360
361
        if (!is_numeric($this->nbterm)) {
362
            $this->error = 'BadValueForParameterForNbTerm';
363
            return -1;
364
        }
365
366
        $sql = "UPDATE " . MAIN_DB_PREFIX . "loan";
367
        $sql .= " SET label='" . $this->db->escape($this->label) . "',";
368
        $sql .= " capital='" . price2num($this->db->escape($this->capital)) . "',";
369
        $sql .= " datestart='" . $this->db->idate($this->datestart) . "',";
370
        $sql .= " dateend='" . $this->db->idate($this->dateend) . "',";
371
        $sql .= " nbterm=" . ((float) $this->nbterm) . ",";
372
        $sql .= " rate=" . ((float) $this->rate) . ",";
373
        $sql .= " accountancy_account_capital = '" . $this->db->escape($this->account_capital) . "',";
374
        $sql .= " accountancy_account_insurance = '" . $this->db->escape($this->account_insurance) . "',";
375
        $sql .= " accountancy_account_interest = '" . $this->db->escape($this->account_interest) . "',";
376
        $sql .= " fk_projet=" . (empty($this->fk_project) ? 'NULL' : ((int) $this->fk_project)) . ",";
377
        $sql .= " fk_user_modif = " . $user->id . ",";
378
        $sql .= " insurance_amount = '" . price2num($this->db->escape($this->insurance_amount)) . "'";
379
        $sql .= " WHERE rowid=" . ((int) $this->id);
380
381
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
382
        $resql = $this->db->query($sql);
383
        if ($resql) {
384
            $this->db->commit();
385
            return 1;
386
        } else {
387
            $this->error = $this->db->error();
388
            $this->db->rollback();
389
            return -1;
390
        }
391
    }
392
393
    /**
394
     *  Tag loan as paid completely
395
     *
396
     *  @param  User    $user   Object user making change
397
     *  @return int             Return integer <0 if KO, >0 if OK
398
     */
399
    public function setPaid($user)
400
    {
401
        $sql = "UPDATE " . MAIN_DB_PREFIX . "loan SET";
402
        $sql .= " paid = " . $this::STATUS_PAID;
403
        $sql .= " WHERE rowid = " . ((int) $this->id);
404
405
        $return = $this->db->query($sql);
406
407
        if ($return) {
408
            $this->paid = $this::STATUS_PAID;
409
            return 1;
410
        } else {
411
            $this->error = $this->db->lasterror();
412
            return -1;
413
        }
414
    }
415
416
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
417
    /**
418
     *  Tag loan as payment started
419
     *
420
     *  @deprecated
421
     *  @see setStarted()
422
     *  @param  User    $user   Object user making change
423
     *  @return int             Return integer <0 if KO, >0 if OK
424
     */
425
    public function set_started($user)
426
    {
427
		// phpcs:enable
428
        dol_syslog(get_class($this) . "::set_started is deprecated, use setStarted instead", LOG_NOTICE);
429
        return $this->setStarted($user);
430
    }
431
432
    /**
433
     *  Tag loan as payment started
434
     *
435
     *  @param  User    $user   Object user making change
436
     *  @return int             Return integer <0 if KO, >0 if OK
437
     */
438
    public function setStarted($user)
439
    {
440
        $sql = "UPDATE " . MAIN_DB_PREFIX . "loan SET";
441
        $sql .= " paid = " . $this::STATUS_STARTED;
442
        $sql .= " WHERE rowid = " . ((int) $this->id);
443
444
        $return = $this->db->query($sql);
445
446
        if ($return) {
447
            $this->paid = $this::STATUS_STARTED;
448
            return 1;
449
        } else {
450
            $this->error = $this->db->lasterror();
451
            return -1;
452
        }
453
    }
454
455
    /**
456
     *  Tag loan as payment as unpaid
457
     *
458
     *  @param  User    $user   Object user making change
459
     *  @return int             Return integer <0 if KO, >0 if OK
460
     */
461
    public function setUnpaid($user)
462
    {
463
        $sql = "UPDATE " . MAIN_DB_PREFIX . "loan SET";
464
        $sql .= " paid = " . $this::STATUS_UNPAID;
465
        $sql .= " WHERE rowid = " . ((int) $this->id);
466
467
        $return = $this->db->query($sql);
468
469
        if ($return) {
470
            $this->paid = $this::STATUS_UNPAID;
471
            return 1;
472
        } else {
473
            $this->error = $this->db->lasterror();
474
            return -1;
475
        }
476
    }
477
478
    /**
479
     *  Return label of loan status (unpaid, paid)
480
     *
481
     *  @param  int     $mode           0=label, 1=short label, 2=Picto + Short label, 3=Picto, 4=Picto + Label
482
     *  @param  integer $alreadypaid    0=No payment already done, >0=Some payments were already done (we recommend to put here amount paid if you have it, 1 otherwise)
483
     *  @return string                  Label
484
     */
485
    public function getLibStatut($mode = 0, $alreadypaid = -1)
486
    {
487
        return $this->LibStatut($this->paid, $mode, $alreadypaid);
488
    }
489
490
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
491
    /**
492
     *  Return label for given status
493
     *
494
     *  @param  int     $status         Id status
495
     *  @param  int     $mode           0=Label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Label, 5=Short label + Picto
496
     *  @param  integer $alreadypaid    0=No payment already done, >0=Some payments were already done (we recommend to put here amount paid if you have it, 1 otherwise)
497
     *  @return string                  Label
498
     */
499
    public function LibStatut($status, $mode = 0, $alreadypaid = -1)
500
    {
501
		// phpcs:enable
502
        global $langs;
503
504
        // Load translation files required by the page
505
        $langs->loadLangs(array("customers", "bills"));
506
507
        unset($this->labelStatus); // Force to reset the array of status label, because label can change depending on parameters
508
        // Always true because of 'unset':
509
        // if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
510
        global $langs;
511
        $this->labelStatus = array();
512
        $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
513
        $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Paid');
514
        $this->labelStatus[self::STATUS_STARTED] = $langs->transnoentitiesnoconv("BillStatusStarted");
515
        if ($status == 0 && $alreadypaid > 0) {
516
            $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
517
        }
518
        $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
519
        $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Paid');
520
        $this->labelStatusShort[self::STATUS_STARTED] = $langs->transnoentitiesnoconv("BillStatusStarted");
521
        if ($status == 0 && $alreadypaid > 0) {
522
            $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
523
        }
524
        // }  // End of empty(labelStatus,labelStatusShort)
525
526
        $statusType = 'status1';
527
        if (($status == 0 && $alreadypaid > 0) || $status == self::STATUS_STARTED) {
528
            $statusType = 'status3';
529
        }
530
        if ($status == 1) {
531
            $statusType = 'status6';
532
        }
533
534
        return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
535
    }
536
537
538
    /**
539
     *  Return clicable name (with eventually the picto)
540
     *
541
     *  @param  int     $withpicto                  0=No picto, 1=Include picto into link, 2=Only picto
542
     *  @param  int     $maxlen                     Label max length
543
     *  @param  string  $option                     On what the link point to ('nolink', ...)
544
     *  @param  int     $notooltip                  1=Disable tooltip
545
     *  @param  string  $morecss                    Add more css on link
546
     *  @param  int     $save_lastsearch_value      -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
547
     *  @return string                              Chaine with URL
548
     */
549
    public function getNomUrl($withpicto = 0, $maxlen = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
550
    {
551
        global $conf, $langs, $hookmanager;
552
553
        $result = '';
554
555
        $label = '<u>' . $langs->trans("ShowLoan") . '</u>';
556
        if (!empty($this->ref)) {
557
            $label .= '<br><strong>' . $langs->trans('Ref') . ':</strong> ' . $this->ref;
558
        }
559
        if (!empty($this->label)) {
560
            $label .= '<br><strong>' . $langs->trans('Label') . ':</strong> ' . $this->label;
561
        }
562
563
        $url = constant('BASE_URL') . '/loan/card.php?id=' . $this->id;
564
565
        if ($option != 'nolink') {
566
            // Add param to save lastsearch_values or not
567
            $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
568
            if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
569
                $add_save_lastsearch_values = 1;
570
            }
571
            if ($add_save_lastsearch_values) {
572
                $url .= '&save_lastsearch_values=1';
573
            }
574
        }
575
576
        $linkclose = '';
577
        if (empty($notooltip)) {
578
            if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
579
                $label = $langs->trans("ShowMyObject");
580
                $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
581
            }
582
            $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
583
            $linkclose .= ' class="classfortooltip' . ($morecss ? ' ' . $morecss : '') . '"';
584
        } else {
585
            $linkclose = ($morecss ? ' class="' . $morecss . '"' : '');
586
        }
587
588
        $linkstart = '<a href="' . $url . '"';
589
        $linkstart .= $linkclose . '>';
590
        $linkend = '</a>';
591
592
        $result .= $linkstart;
593
        if ($withpicto) {
594
            $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="' . (($withpicto != 2) ? 'paddingright ' : '') . 'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
595
        }
596
        if ($withpicto != 2) {
597
            $result .= ($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref);
598
        }
599
        $result .= $linkend;
600
601
        global $action;
602
        $hookmanager->initHooks(array($this->element . 'dao'));
603
        $parameters = array('id' => $this->id, 'getnomurl' => &$result);
604
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
605
        if ($reshook > 0) {
606
            $result = $hookmanager->resPrint;
607
        } else {
608
            $result .= $hookmanager->resPrint;
609
        }
610
        return $result;
611
    }
612
613
    /**
614
     *  Initialise an instance with random values.
615
     *  Used to build previews or test instances.
616
     *  id must be 0 if object instance is a specimen.
617
     *
618
     *  @return int
619
     */
620
    public function initAsSpecimen()
621
    {
622
        global $user, $langs, $conf;
623
624
        $now = dol_now();
625
626
        // Initialise parameters
627
        $this->id = 0;
628
        $this->fk_bank = 1;
629
        $this->label = 'SPECIMEN';
630
        $this->specimen = 1;
631
        $this->account_capital = 16;
632
        $this->account_insurance = 616;
633
        $this->account_interest = 518;
634
        $this->datestart = $now;
635
        $this->dateend = $now + (3600 * 24 * 365);
636
        $this->note_public = 'SPECIMEN';
637
        $this->capital = 20000;
638
        $this->nbterm = 48;
639
        $this->rate = 4.3;
640
641
        return 1;
642
    }
643
644
    /**
645
     *  Return amount of payments already done
646
     *
647
     *  @return     int     Amount of payment already done, <0 if KO
648
     */
649
    public function getSumPayment()
650
    {
651
        $table = 'payment_loan';
652
        $field = 'fk_loan';
653
654
        $sql = 'SELECT sum(amount_capital) as amount';
655
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $table;
656
        $sql .= " WHERE " . $field . " = " . ((int) $this->id);
657
658
        dol_syslog(get_class($this) . "::getSumPayment", LOG_DEBUG);
659
        $resql = $this->db->query($sql);
660
        if ($resql) {
661
            $amount = 0;
662
663
            $obj = $this->db->fetch_object($resql);
664
            if ($obj) {
665
                $amount = $obj->amount ? $obj->amount : 0;
666
            }
667
668
            $this->db->free($resql);
669
            return $amount;
670
        } else {
671
            $this->error = $this->db->lasterror();
672
            return -1;
673
        }
674
    }
675
676
    /**
677
     *  Information on record
678
     *
679
     *  @param  int         $id     Id of record
680
     *  @return integer|null
681
     */
682
    public function info($id)
683
    {
684
        $sql = 'SELECT l.rowid, l.datec, l.fk_user_author, l.fk_user_modif,';
685
        $sql .= ' l.tms as datem';
686
        $sql .= ' WHERE l.rowid = ' . ((int) $id);
687
688
        dol_syslog(get_class($this) . '::info', LOG_DEBUG);
689
        $result = $this->db->query($sql);
690
691
        if ($result) {
692
            if ($this->db->num_rows($result)) {
693
                $obj = $this->db->fetch_object($result);
694
695
                $this->id = $obj->rowid;
696
697
                $this->user_creation_id = $obj->fk_user_author;
698
                $this->user_modification_id = $obj->fk_user_modif;
699
                $this->date_creation     = $this->db->jdate($obj->datec);
700
                $this->date_modification = empty($obj->datem) ? '' : $this->db->jdate($obj->datem);
701
702
                $this->db->free($result);
703
                return 1;
704
            } else {
705
                $this->db->free($result);
706
                return 0;
707
            }
708
        } else {
709
            $this->error = $this->db->lasterror();
710
            return -1;
711
        }
712
    }
713
714
    /**
715
     *  Return clicable link of object (with eventually picto)
716
     *
717
     *  @param      string      $option                 Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
718
     *  @param      array       $arraydata              Array of data
719
     *  @return     string      HTML Code for Kanban thumb.
720
     */
721
    public function getKanbanView($option = '', $arraydata = null)
722
    {
723
        global $langs;
724
725
        $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
726
727
        $return = '<div class="box-flex-item box-flex-grow-zero">';
728
        $return .= '<div class="info-box info-box-sm">';
729
        $return .= '<span class="info-box-icon bg-infobox-action">';
730
        $return .= img_picto('', $this->picto);
731
        $return .= '</span>';
732
        $return .= '<div class="info-box-content">';
733
        $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . (method_exists($this, 'getNomUrl') ? $this->getNomUrl(1) : $this->ref) . '</span>';
734
        if ($selected >= 0) {
735
            $return .= '<input id="cb' . $this->id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this->id . '"' . ($selected ? ' checked="checked"' : '') . '>';
736
        }
737
        if (property_exists($this, 'capital')) {
738
            $return .= ' | <span class="opacitymedium">' . $langs->trans("Amount") . '</span> : <span class="info-box-label amount">' . price($this->capital) . '</span>';
739
        }
740
        if (property_exists($this, 'datestart')) {
741
            $return .= '<br><span class="opacitymedium">' . $langs->trans("DateStart") . '</span> : <span class="info-box-label">' . dol_print_date($this->db->jdate($this->datestart), 'day') . '</span>';
742
        }
743
        if (property_exists($this, 'dateend')) {
744
            $return .= '<br><span class="opacitymedium">' . $langs->trans("DateEnd") . '</span> : <span class="info-box-label">' . dol_print_date($this->db->jdate($this->dateend), 'day') . '</span>';
745
        }
746
747
        if (method_exists($this, 'LibStatut')) {
748
            $return .= '<br><div class="info-box-status">' . $this->getLibStatut(3, $this->alreadypaid) . '</div>';
0 ignored issues
show
Bug Best Practice introduced by
The property $alreadypaid is declared private in Dolibarr\Core\Base\CommonObject. Since you implement __get, consider adding a @property or @property-read.
Loading history...
749
        }
750
        $return .= '</div>';
751
        $return .= '</div>';
752
        $return .= '</div>';
753
        return $return;
754
    }
755
}
756