Passed
Push — GENERAL_BUG_REVIEW_240911 ( 776d89...c757bd )
by Rafael
50:33
created

ChargeSociales::getNomUrl()   F

Complexity

Conditions 26
Paths 13826

Size

Total Lines 77
Code Lines 49

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 49
nc 13826
nop 5
dl 0
loc 77
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) 2002       Rodolphe Quiedeville        <[email protected]>
4
 * Copyright (C) 2004-2007  Laurent Destailleur         <[email protected]>
5
 * Copyright (C) 2016-2024  Frédéric France             <[email protected]>
6
 * Copyright (C) 2017       Alexandre Spangaro	        <[email protected]>
7
 * Copyright (C) 2021       Gauthier VERDOL		        <[email protected]>
8
 * Copyright (C) 2024		MDW							<[email protected]>
9
 * Copyright (C) 2024       Rafael San José             <[email protected]>
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23
 */
24
25
namespace Dolibarr\Code\Compta\Classes;
26
27
/**
28
 *      \file       htdocs/compta/sociales/class/chargesociales.class.php
29
 *      \ingroup    invoice
30
 *      \brief      File for the ChargesSociales class
31
 */
32
33
use Dolibarr\Core\Base\CommonObject;
34
use DoliDB;
35
36
37
/**
38
 *  Class for managing the social charges.
39
 *  The collected VAT is computed only on the paid invoices/charges
40
 */
41
class ChargeSociales extends CommonObject
42
{
43
    /**
44
     * @var string ID to identify managed object
45
     */
46
    public $element = 'chargesociales';
47
48
    public $table = 'chargesociales';
49
50
    /**
51
     * @var string Name of table without prefix where object is stored
52
     */
53
    public $table_element = 'chargesociales';
54
55
    /**
56
     * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
57
     */
58
    public $picto = 'bill';
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    protected $table_ref_field = 'ref';
64
65
    /**
66
     * @var integer|string $date_ech
67
     */
68
    public $date_ech;
69
70
71
    public $label;
72
    public $type;
73
    public $type_label;
74
75
    public $amount;
76
    public $paye;
77
    /**
78
     * @deprecated
79
     */
80
    public $periode;
81
    public $period;
82
83
    /**
84
     * @var integer|string date_creation
85
     */
86
    public $date_creation;
87
88
    /**
89
     * @var integer|string $date_modification
90
     */
91
    public $date_modification;
92
93
    /**
94
     * @var integer|string $date_validation
95
     */
96
    public $date_validation;
97
98
    /**
99
     * @deprecated Use label instead
100
     */
101
    public $lib;
102
103
    /**
104
     * @var int account ID
105
     */
106
    public $fk_account;
107
108
    /**
109
     * @var int account ID (identical to fk_account)
110
     */
111
    public $accountid;
112
113
    /**
114
     * @var int payment type (identical to mode_reglement_id in commonobject class)
115
     */
116
    public $paiementtype;
117
118
    public $mode_reglement_id;
119
    public $mode_reglement_code;
120
    public $mode_reglement;
121
122
    /**
123
     * @var int ID
124
     */
125
    public $fk_project;
126
127
    /**
128
     * @var int ID
129
     */
130
    public $fk_user;
131
132
    /**
133
     * @var double total
134
     */
135
    public $total;
136
137
    public $totalpaid;
138
139
140
    const STATUS_UNPAID = 0;
141
    const STATUS_PAID = 1;
142
143
144
    /**
145
     * Constructor
146
     *
147
     * @param   DoliDB      $db     Database handler
148
     */
149
    public function __construct(DoliDB $db)
150
    {
151
        $this->db = $db;
152
    }
153
154
    /**
155
     *  Retrouve et charge une charge sociale
156
     *
157
     *  @param  int     $id     Id
158
     *  @param  string  $ref    Ref
159
     *  @return int Return integer <0 KO >0 OK
160
     */
161
    public function fetch($id, $ref = '')
162
    {
163
        $sql = "SELECT cs.rowid, cs.date_ech";
164
        $sql .= ", cs.libelle as label, cs.fk_type, cs.amount, cs.fk_projet as fk_project, cs.paye, cs.periode as period, cs.import_key";
165
        $sql .= ", cs.fk_account, cs.fk_mode_reglement, cs.fk_user, note_public, note_private";
166
        $sql .= ", c.libelle as type_label";
167
        $sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
168
        $sql .= " FROM " . MAIN_DB_PREFIX . "chargesociales as cs";
169
        $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_chargesociales as c ON cs.fk_type = c.id";
170
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_paiement as p ON cs.fk_mode_reglement = p.id';
171
        $sql .= ' WHERE cs.entity IN (' . getEntity('tax') . ')';
172
        if ($ref) {
173
            $sql .= " AND cs.ref = '" . $this->db->escape($ref) . "'";
174
        } else {
175
            $sql .= " AND cs.rowid = " . ((int) $id);
176
        }
177
178
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
179
        $resql = $this->db->query($sql);
180
        if ($resql) {
181
            if ($this->db->num_rows($resql)) {
182
                $obj = $this->db->fetch_object($resql);
183
184
                $this->id = $obj->rowid;
185
                $this->ref                  = $obj->rowid;
186
                $this->date_ech = $this->db->jdate($obj->date_ech);
187
                $this->lib                  = $obj->label;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Code\Compta\Classes\ChargeSociales::$lib has been deprecated: Use label instead ( Ignorable by Annotation )

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

187
                /** @scrutinizer ignore-deprecated */ $this->lib                  = $obj->label;

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...
188
                $this->label                = $obj->label;
189
                $this->type                 = $obj->fk_type;
190
                $this->type_label           = $obj->type_label;
191
                $this->fk_account           = $obj->fk_account;
192
                $this->mode_reglement_id = $obj->fk_mode_reglement;
193
                $this->mode_reglement_code = $obj->mode_reglement_code;
194
                $this->mode_reglement = $obj->mode_reglement_libelle;
195
                $this->amount = $obj->amount;
196
                $this->fk_project = $obj->fk_project;
197
                $this->fk_user = $obj->fk_user;
198
                $this->note_public = $obj->note_public;
199
                $this->note_private = $obj->note_private;
200
                $this->paye = $obj->paye;
201
                $this->periode = $this->db->jdate($obj->period);
202
                $this->period = $this->db->jdate($obj->period);
203
                $this->import_key = $obj->import_key;
204
205
                $this->db->free($resql);
206
207
                return 1;
208
            } else {
209
                return 0;
210
            }
211
        } else {
212
            $this->error = $this->db->lasterror();
213
            return -1;
214
        }
215
    }
216
217
    /**
218
     * Check if a social contribution can be created into database
219
     *
220
     * @return  boolean     True or false
221
     */
222
    public function check()
223
    {
224
        $newamount = price2num($this->amount, 'MT');
225
226
        // Validation of parameters
227
        if ($newamount == 0 || empty($this->date_ech) || (empty($this->period) && empty($this->periode))) {
228
            return false;
229
        }
230
231
        return true;
232
    }
233
234
    /**
235
     *      Create a social contribution into database
236
     *
237
     *      @param  User    $user   User making creation
238
     *      @return int             Return integer <0 if KO, id if OK
239
     */
240
    public function create($user)
241
    {
242
        global $conf;
243
        $error = 0;
244
245
        $now = dol_now();
246
247
        // Nettoyage parameters
248
        $newamount = price2num($this->amount, 'MT');
249
250
        if (!$this->check()) {
251
            $this->error = "ErrorBadParameter";
252
            return -2;
253
        }
254
255
        $this->db->begin();
256
257
        $sql = "INSERT INTO " . MAIN_DB_PREFIX . "chargesociales (fk_type, fk_account, fk_mode_reglement, libelle, date_ech, periode, amount, fk_projet, entity, fk_user_author, fk_user, date_creation)";
258
        $sql .= " VALUES (" . ((int) $this->type);
259
        $sql .= ", " . ($this->fk_account > 0 ? ((int) $this->fk_account) : 'NULL');
260
        $sql .= ", " . ($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "NULL");
261
        $sql .= ", '" . $this->db->escape($this->label ? $this->label : $this->lib) . "'";
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Code\Compta\Classes\ChargeSociales::$lib has been deprecated: Use label instead ( Ignorable by Annotation )

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

261
        $sql .= ", '" . $this->db->escape($this->label ? $this->label : /** @scrutinizer ignore-deprecated */ $this->lib) . "'";

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...
262
        $sql .= ", '" . $this->db->idate($this->date_ech) . "'";
263
        $sql .= ", '" . $this->db->idate($this->periode) . "'";
264
        $sql .= ", '" . price2num($newamount) . "'";
265
        $sql .= ", " . ($this->fk_project > 0 ? ((int) $this->fk_project) : 'NULL');
266
        $sql .= ", " . ((int) $conf->entity);
267
        $sql .= ", " . ((int) $user->id);
268
        $sql .= ", " . ($this->fk_user > 0 ? ((int) $this->fk_user) : 'NULL');
269
        $sql .= ", '" . $this->db->idate($now) . "'";
270
        $sql .= ")";
271
272
        dol_syslog(get_class($this) . "::create", LOG_DEBUG);
273
        $resql = $this->db->query($sql);
274
        if ($resql) {
275
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "chargesociales");
276
277
            //dol_syslog("ChargesSociales::create this->id=".$this->id);
278
            $result = $this->call_trigger('SOCIALCONTRIBUTION_CREATE', $user);
279
            if ($result < 0) {
280
                $error++;
281
            }
282
283
            if (empty($error)) {
284
                $this->db->commit();
285
                return $this->id;
286
            } else {
287
                $this->db->rollback();
288
                return -1 * $error;
289
            }
290
        } else {
291
            $this->error = $this->db->error();
292
            $this->db->rollback();
293
            return -1;
294
        }
295
    }
296
297
298
    /**
299
     *      Delete a social contribution
300
     *
301
     *      @param      User    $user   Object user making delete
302
     *      @return             int     Return integer <0 if KO, >0 if OK
303
     */
304
    public function delete($user)
305
    {
306
        $error = 0;
307
308
        $this->db->begin();
309
310
        // Get bank transaction lines for this social contributions
311
        $account = new Account($this->db);
312
        $lines_url = $account->get_url('', $this->id, 'sc');
313
314
        // Delete bank urls
315
        foreach ($lines_url as $line_url) {
316
            if (!$error) {
317
                $accountline = new AccountLine($this->db);
318
                $accountline->fetch($line_url['fk_bank']);
319
                $result = $accountline->delete_urls($user);
320
                if ($result < 0) {
321
                    $error++;
322
                }
323
            }
324
        }
325
326
        // Delete payments
327
        if (!$error) {
328
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "paiementcharge WHERE fk_charge=" . ((int) $this->id);
329
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
330
            $resql = $this->db->query($sql);
331
            if (!$resql) {
332
                $error++;
333
                $this->error = $this->db->lasterror();
334
            }
335
        }
336
337
        if (!$error) {
338
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "chargesociales WHERE rowid=" . ((int) $this->id);
339
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
340
            $resql = $this->db->query($sql);
341
            if (!$resql) {
342
                $error++;
343
                $this->error = $this->db->lasterror();
344
            }
345
        }
346
347
        if (!$error) {
348
            $this->db->commit();
349
            return 1;
350
        } else {
351
            $this->db->rollback();
352
            return -1;
353
        }
354
    }
355
356
357
    /**
358
     *      Update social or fiscal contribution
359
     *
360
     *      @param  User    $user           User that modify
361
     *      @param  int     $notrigger      0=launch triggers after, 1=disable triggers
362
     *      @return int                     Return integer <0 if KO, >0 if OK
363
     */
364
    public function update($user, $notrigger = 0)
365
    {
366
        $error = 0;
367
        $this->db->begin();
368
369
        $sql = "UPDATE " . MAIN_DB_PREFIX . "chargesociales";
370
        $sql .= " SET libelle='" . $this->db->escape($this->label ? $this->label : $this->lib) . "'";
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Code\Compta\Classes\ChargeSociales::$lib has been deprecated: Use label instead ( Ignorable by Annotation )

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

370
        $sql .= " SET libelle='" . $this->db->escape($this->label ? $this->label : /** @scrutinizer ignore-deprecated */ $this->lib) . "'";

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...
371
        $sql .= ", date_ech='" . $this->db->idate($this->date_ech) . "'";
372
        $sql .= ", periode='" . $this->db->idate($this->periode) . "'";
373
        $sql .= ", amount='" . price2num($this->amount, 'MT') . "'";
374
        $sql .= ", fk_projet=" . ($this->fk_project > 0 ? $this->db->escape($this->fk_project) : "NULL");
375
        $sql .= ", fk_user=" . ($this->fk_user > 0 ? $this->db->escape($this->fk_user) : "NULL");
376
        $sql .= ", fk_user_modif=" . $user->id;
377
        $sql .= " WHERE rowid=" . ((int) $this->id);
378
379
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
380
        $resql = $this->db->query($sql);
381
382
        if (!$resql) {
383
            $error++;
384
            $this->errors[] = "Error " . $this->db->lasterror();
385
        }
386
387
        if (!$error) {
388
            if (!$notrigger) {
389
                // Call trigger
390
                $result = $this->call_trigger('SOCIALCONTRIBUTION_MODIFY', $user);
391
                if ($result < 0) {
392
                    $error++;
393
                }
394
                // End call triggers
395
            }
396
        }
397
398
        // Commit or rollback
399
        if ($error) {
400
            foreach ($this->errors as $errmsg) {
401
                dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
402
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
403
            }
404
            $this->db->rollback();
405
            return -1 * $error;
406
        } else {
407
            $this->db->commit();
408
            return 1;
409
        }
410
    }
411
412
    /**
413
     * Calculate amount remaining to pay by year
414
     *
415
     * @param   int         $year       Year
416
     * @return  int|float               Returns -1 when error (Note: could be mistaken with an amount)
417
     */
418
    public function solde($year = 0)
419
    {
420
        global $conf;
421
422
        $sql = "SELECT SUM(f.amount) as amount";
423
        $sql .= " FROM " . MAIN_DB_PREFIX . "chargesociales as f";
424
        $sql .= " WHERE f.entity = " . $conf->entity;
425
        $sql .= " AND paye = 0";
426
427
        if ($year) {
428
            $sql .= " AND f.datev >= '" . ((int) $year) . "-01-01' AND f.datev <= '" . ((int) $year) . "-12-31' ";
429
        }
430
431
        $result = $this->db->query($sql);
432
        if ($result) {
433
            if ($this->db->num_rows($result)) {
434
                $obj = $this->db->fetch_object($result);
435
                $this->db->free($result);
436
                return $obj->amount;
437
            } else {
438
                return 0;
439
            }
440
        } else {
441
            print $this->db->error();
442
            return -1;
443
        }
444
    }
445
446
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
447
    /**
448
     *  Tag social contribution as paid completely
449
     *
450
     *  @deprecated
451
     *  @see setPaid()
452
     *  @param    User    $user       Object user making change
453
     *  @return   int                   Return integer <0 if KO, >0 if OK
454
     */
455
    public function set_paid($user)
456
    {
457
		// phpcs:enable
458
        dol_syslog(get_class($this) . "::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
459
        return $this->setPaid($user);
460
    }
461
462
    /**
463
     *    Tag social contribution as paid completely
464
     *
465
     *    @param    User    $user       Object user making change
466
     *    @return   int                 Return integer <0 if KO, >0 if OK
467
     */
468
    public function setPaid($user)
469
    {
470
        $sql = "UPDATE " . MAIN_DB_PREFIX . "chargesociales SET";
471
        $sql .= " paye = 1";
472
        $sql .= " WHERE rowid = " . ((int) $this->id);
473
474
        $return = $this->db->query($sql);
475
476
        if ($return) {
477
            $this->paye = 1;
478
479
            return 1;
480
        } else {
481
            return -1;
482
        }
483
    }
484
485
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
486
    /**
487
     *    Remove tag paid on social contribution
488
     *
489
     *  @deprecated
490
     *  @see setUnpaid()
491
     *  @param  User    $user       Object user making change
492
     *  @return int                 Return integer <0 if KO, >0 if OK
493
     */
494
    public function set_unpaid($user)
495
    {
496
		// phpcs:enable
497
        dol_syslog(get_class($this) . "::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
498
        return $this->setUnpaid($user);
499
    }
500
501
    /**
502
     *    Remove tag paid on social contribution
503
     *
504
     *    @param    User    $user       Object user making change
505
     *    @return   int                 Return integer <0 if KO, >0 if OK
506
     */
507
    public function setUnpaid($user)
508
    {
509
        $sql = "UPDATE " . MAIN_DB_PREFIX . "chargesociales SET";
510
        $sql .= " paye = 0";
511
        $sql .= " WHERE rowid = " . ((int) $this->id);
512
513
        $return = $this->db->query($sql);
514
515
        if ($return) {
516
            $this->paye = 0;
517
518
            return 1;
519
        } else {
520
            return -1;
521
        }
522
    }
523
524
    /**
525
     *  Retourne le libelle du statut d'une charge (impaye, payee)
526
     *
527
     *  @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
528
     *  @param  double  $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)
529
     *  @return string                  Label
530
     */
531
    public function getLibStatut($mode = 0, $alreadypaid = -1)
532
    {
533
        return $this->LibStatut($this->paye, $mode, $alreadypaid);
534
    }
535
536
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
537
    /**
538
     *  Renvoi le libelle d'un statut donne
539
     *
540
     *  @param  int     $status         Id status
541
     *  @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
542
     *  @param  double  $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)
543
     *  @return string                  Label
544
     */
545
    public function LibStatut($status, $mode = 0, $alreadypaid = -1)
546
    {
547
		// phpcs:enable
548
        global $langs;
549
550
        // Load translation files required by the page
551
        $langs->loadLangs(array("customers", "bills"));
552
553
        // We reinit status array to force to redefine them because label may change according to properties values.
554
        $this->labelStatus = array();
555
        $this->labelStatusShort = array();
556
557
        if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
558
            global $langs;
559
            //$langs->load("mymodule");
560
            $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
561
            $this->labelStatus[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Paid');
562
            if ($status == self::STATUS_UNPAID && $alreadypaid > 0) {
563
                $this->labelStatus[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
564
            }
565
            $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv('Unpaid');
566
            $this->labelStatusShort[self::STATUS_PAID] = $langs->transnoentitiesnoconv('Paid');
567
            if ($status == self::STATUS_UNPAID && $alreadypaid > 0) {
568
                $this->labelStatusShort[self::STATUS_UNPAID] = $langs->transnoentitiesnoconv("BillStatusStarted");
569
            }
570
        }
571
572
        $statusType = 'status1';
573
        if ($status == 0 && $alreadypaid > 0) {
574
            $statusType = 'status3';
575
        }
576
        if ($status == 1) {
577
            $statusType = 'status6';
578
        }
579
580
        return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
581
    }
582
583
584
    /**
585
     *  Return a link to the object card (with optionally the picto)
586
     *
587
     *  @param  int     $withpicto                  Include picto in link (0=No picto, 1=Include picto into link, 2=Only picto)
588
     *  @param  string  $option                     On what the link point to ('nolink', ...)
589
     *  @param  int     $notooltip                  1=Disable tooltip
590
     *  @param  int     $short                      1=Return just URL
591
     *  @param  int     $save_lastsearch_value      -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
592
     *  @return string                              String with link
593
     */
594
    public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $short = 0, $save_lastsearch_value = -1)
595
    {
596
        global $langs, $conf, $user, $form, $hookmanager;
597
598
        if (!empty($conf->dol_no_mouse_hover)) {
599
            $notooltip = 1; // Force disable tooltips
600
        }
601
602
        $result = '';
603
604
        $url = constant('BASE_URL') . '/compta/sociales/card.php?id=' . $this->id;
605
606
        if ($short) {
607
            return $url;
608
        }
609
610
        if ($option !== 'nolink') {
611
            // Add param to save lastsearch_values or not
612
            $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
613
            if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
614
                $add_save_lastsearch_values = 1;
615
            }
616
            if ($add_save_lastsearch_values) {
617
                $url .= '&save_lastsearch_values=1';
618
            }
619
        }
620
621
        if (empty($this->ref)) {
622
            $this->ref = $this->label;
623
        }
624
625
        $label = img_picto('', $this->picto, 'class="pictofixedwidth"') . '<u class="paddingrightonly">' . $langs->trans("SocialContribution") . '</u>';
626
        if (isset($this->paye)) {
627
            $label .= ' ' . $this->getLibStatut(5);
628
        }
629
        if (!empty($this->ref)) {
630
            $label .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
631
        }
632
        if (!empty($this->label)) {
633
            $label .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $this->label;
634
        }
635
        if (!empty($this->type_label)) {
636
            $label .= '<br><b>' . $langs->trans('Type') . ':</b> ' . $this->type_label;
637
        }
638
639
        $linkclose = '';
640
        if (empty($notooltip) && $user->hasRight("facture", "read")) {
641
            if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
642
                $label = $langs->trans("SocialContribution");
643
                $linkclose .= ' alt="' . dol_escape_htmltag($label, 1) . '"';
644
            }
645
            $linkclose .= ' title="' . dol_escape_htmltag($label, 1) . '"';
646
            $linkclose .= ' class="classfortooltip"';
647
        }
648
649
        $linkstart = '<a href="' . $url . '"';
650
        $linkstart .= $linkclose . '>';
651
        $linkend = '</a>';
652
653
        $result .= $linkstart;
654
        if ($withpicto) {
655
            $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);
656
        }
657
        if ($withpicto != 2) {
658
            $result .= $this->ref;
659
        }
660
        $result .= $linkend;
661
        global $action;
662
        $hookmanager->initHooks(array($this->element . 'dao'));
663
        $parameters = array('id' => $this->id, 'getnomurl' => &$result);
664
        $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
665
        if ($reshook > 0) {
666
            $result = $hookmanager->resPrint;
667
        } else {
668
            $result .= $hookmanager->resPrint;
669
        }
670
        return $result;
671
    }
672
673
    /**
674
     *  Return amount of payments already done
675
     *
676
     *  @return     int     Amount of payment already done, <0 if KO
677
     */
678
    public function getSommePaiement()
679
    {
680
        $table = 'paiementcharge';
681
        $field = 'fk_charge';
682
683
        $sql = 'SELECT sum(amount) as amount';
684
        $sql .= ' FROM ' . MAIN_DB_PREFIX . $table;
685
        $sql .= " WHERE " . $field . " = " . ((int) $this->id);
686
687
        dol_syslog(get_class($this) . "::getSommePaiement", LOG_DEBUG);
688
        $resql = $this->db->query($sql);
689
        if ($resql) {
690
            $amount = 0;
691
692
            $obj = $this->db->fetch_object($resql);
693
            if ($obj) {
694
                $amount = $obj->amount ? $obj->amount : 0;
695
            }
696
697
            $this->db->free($resql);
698
            return $amount;
699
        } else {
700
            return -1;
701
        }
702
    }
703
704
    /**
705
     *  Charge l'information d'ordre info dans l'objet entrepot
706
     *
707
     *  @param  int     $id     Id of social contribution
708
     *  @return int             Return integer <0 if KO, >0 if OK
709
     */
710
    public function info($id)
711
    {
712
        $sql = "SELECT e.rowid, e.tms as datem, e.date_creation as datec, e.date_valid as datev, e.import_key,";
713
        $sql .= " e.fk_user_author, e.fk_user_modif, e.fk_user_valid";
714
        $sql .= " FROM " . MAIN_DB_PREFIX . "chargesociales as e";
715
        $sql .= " WHERE e.rowid = " . ((int) $id);
716
717
        dol_syslog(get_class($this) . "::info", LOG_DEBUG);
718
        $result = $this->db->query($sql);
719
        if ($result) {
720
            if ($this->db->num_rows($result)) {
721
                $obj = $this->db->fetch_object($result);
722
723
                $this->id = $obj->rowid;
724
725
                $this->user_creation_id = $obj->fk_user_author;
726
                $this->user_modification_id = $obj->fk_user_modif;
727
                $this->user_validation_id = $obj->fk_user_valid;
728
                $this->date_creation     = $this->db->jdate($obj->datec);
729
                $this->date_modification = $this->db->jdate($obj->datem);
730
                $this->date_validation   = $this->db->jdate($obj->datev);
731
                $this->import_key        = $obj->import_key;
732
            }
733
734
            $this->db->free($result);
735
            return 1;
736
        } else {
737
            dol_print_error($this->db);
738
            return -1;
739
        }
740
    }
741
742
    /**
743
     *  Initialise an instance with random values.
744
     *  Used to build previews or test instances.
745
     *  id must be 0 if object instance is a specimen.
746
     *
747
     *  @return int
748
     */
749
    public function initAsSpecimen()
750
    {
751
        // Initialize parameters
752
        $this->id = 0;
753
        $this->ref = 'SPECIMEN';
754
        $this->specimen = 1;
755
        $this->paye = 0;
756
        $this->date_creation = dol_now();
757
        $this->date_ech = $this->date_creation + 3600 * 24 * 30;
758
        $this->periode = $this->date_creation + 3600 * 24 * 30;
759
        $this->period = $this->date_creation + 3600 * 24 * 30;
760
        $this->amount = 100;
761
        $this->label = 'Social contribution label';
762
        $this->type = 1;
763
        $this->type_label = 'Type of social contribution';
764
765
        return 1;
766
    }
767
768
    /**
769
     *  Return clicable link of object (with eventually picto)
770
     *
771
     *  @param      string      $option                 Where point the link (0=> main card, 1,2 => shipment, 'nolink'=>No link)
772
     *  @param      array       $arraydata              Array of data
773
     *  @return     string                              HTML Code for Kanban thumb.
774
     */
775
    public function getKanbanView($option = '', $arraydata = null)
776
    {
777
        global $conf, $langs;
778
779
        $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
780
781
        $return = '<div class="box-flex-item box-flex-grow-zero">';
782
        $return .= '<div class="info-box info-box-sm">';
783
        $return .= '<span class="info-box-icon bg-infobox-action">';
784
        $return .= img_picto('', $this->picto);
785
        $return .= '</span>';
786
        $return .= '<div class="info-box-content">';
787
        $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">' . (method_exists($this, 'getNomUrl') ? $this->getNomUrl(0) : $this->ref) . '</span>';
788
        if ($selected >= 0) {
789
            $return .= '<input id="cb' . $this->id . '" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="' . $this->id . '"' . ($selected ? ' checked="checked"' : '') . '>';
790
        }
791
        if (property_exists($this, 'label')) {
792
            $return .= ' &nbsp; <div class="inline-block opacitymedium valignmiddle tdoverflowmax100">' . $this->label . '</div>';
793
        }
794
        if (!empty($arraydata['project']) && $arraydata['project']->id > 0) {
795
            $return .= '<br><span class="info-box-label">' . $arraydata['project']->getNomUrl(1) . '</span>';
796
        }
797
        if (property_exists($this, 'date_ech')) {
798
            $return .= '<br><span class="opacitymedium">' . $langs->trans("DateEnd") . '</span> : <span class="info-box-label">' . dol_print_date($this->date_ech, 'day') . '</span>';
799
        }
800
        if (property_exists($this, 'amount')) {
801
            $return .= '<br>';
802
            $return .= '<span class="info-box-label amount">' . price($this->amount, 0, $langs, 1, -1, -1, $conf->currency) . '</span>';
803
        }
804
        if (method_exists($this, 'LibStatut')) {
805
            $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...
806
        }
807
        $return .= '</div>';
808
        $return .= '</div>';
809
        $return .= '</div>';
810
        return $return;
811
    }
812
}
813