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

PaymentSocialContribution::update()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 72
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 46
nc 4194304
nop 2
dl 0
loc 72
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) 2022       Alexandre Spangaro      <[email protected]>
6
 * Copyright (C) 2024       Frédéric France             <[email protected]>
7
 * Copyright (C) 2024		MDW							<[email protected]>
8
 * Copyright (C) 2024       Rafael San José             <[email protected]>
9
 *
10
 * This program is free software; you can redistribute it and/or modify
11
 * it under the terms of the GNU General Public License as published by
12
 * the Free Software Foundation; either version 3 of the License, or
13
 * (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU General Public License
21
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22
 */
23
24
namespace Dolibarr\Code\Compta\Classes;
25
26
/**
27
 *      \file       htdocs/compta/sociales/class/paymentsocialcontribution.class.php
28
 *      \ingroup    invoice
29
 *      \brief      File of class to manage payment of social contributions
30
 */
31
32
use Dolibarr\Core\Base\CommonObject;
33
34
35
/**
36
 *  Class to manage payments of social contributions
37
 */
38
class PaymentSocialContribution extends CommonObject
39
{
40
    /**
41
     * @var string ID to identify managed object
42
     */
43
    public $element = 'paiementcharge';
44
45
    /**
46
     * @var string Name of table without prefix where object is stored
47
     */
48
    public $table_element = 'paiementcharge';
49
50
    /**
51
     * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
52
     */
53
    public $picto = 'payment';
54
55
    /**
56
     * @var string  Label
57
     */
58
    public $label;
59
60
    /**
61
     * @var int ID
62
     */
63
    public $fk_charge;
64
65
    public $datec = '';
66
    public $datep = '';
67
68
    /**
69
     * @var string
70
     */
71
    public $type_code;
72
73
    /**
74
     * @var string
75
     */
76
    public $type_label;
77
78
    /**
79
     * @var int
80
     */
81
    public $bank_account;
82
83
    /**
84
     * @var int
85
     */
86
    public $bank_line;
87
88
    /**
89
     * @deprecated  Use $amount instead.
90
     * @see $amount
91
     * @var float|int
92
     */
93
    public $total;
94
95
    /**
96
     * @var float|int
97
     */
98
    public $amount; // Total amount of payment
99
    /**
100
     * @var array<float|int>
101
     */
102
    public $amounts = array(); // Array of amounts
103
104
    /**
105
     * @var int ID
106
     */
107
    public $fk_typepaiement;
108
109
    /**
110
     * @var string
111
     * @deprecated Use $num_payment instead
112
     * @see $num_payment
113
     */
114
    public $num_paiement;
115
116
    /**
117
     * @var string      Payment reference
118
     *                  (Cheque or bank transfer reference. Can be "ABC123")
119
     */
120
    public $num_payment;
121
122
    /**
123
     * @var int ID
124
     */
125
    public $fk_bank;
126
127
    /**
128
     * @var int ID
129
     */
130
    public $fk_user_creat;
131
132
    /**
133
     * @var int ID
134
     */
135
    public $fk_user_modif;
136
137
    /**
138
     * @var int ID
139
     */
140
    public $chid;
141
142
    /**
143
     * @var integer|string datepaye
144
     */
145
    public $datepaye;
146
147
    /**
148
     * @var integer|string paiementtype
149
     */
150
    public $paiementtype;
151
152
153
    /**
154
     *  Constructor
155
     *
156
     *  @param      DoliDB      $db      Database handler
157
     */
158
    public function __construct(DoliDB $db)
0 ignored issues
show
Bug introduced by
The type Dolibarr\Code\Compta\Classes\DoliDB was not found. Did you mean DoliDB? If so, make sure to prefix the type with \.
Loading history...
159
    {
160
        $this->db = $db;
161
    }
162
163
    /**
164
     *  Create payment of social contribution into database.
165
     *  Use this->amounts to have list of lines for the payment
166
     *
167
     *  @param      User    $user                   User making payment
168
     *  @param      int     $closepaidcontrib       1=Also close paid contributions to paid, 0=Do nothing more
169
     *  @return     int                             Return integer <0 if KO, id of payment if OK
170
     */
171
    public function create($user, $closepaidcontrib = 0)
172
    {
173
        global $conf, $langs;
174
175
        $error = 0;
176
177
        $now = dol_now();
178
179
        dol_syslog(get_class($this) . "::create", LOG_DEBUG);
180
181
        // Validate parameters
182
        if (!$this->datepaye) {
183
            $this->error = 'ErrorBadValueForParameterCreatePaymentSocialContrib';
184
            return -1;
185
        }
186
187
        // Clean parameters
188
        if (isset($this->fk_charge)) {
189
            $this->fk_charge = (int) $this->fk_charge;
190
        }
191
        if (isset($this->amount)) {
192
            $this->amount = (float) $this->amount;
193
        }
194
        if (isset($this->fk_typepaiement)) {
195
            $this->fk_typepaiement = (int) $this->fk_typepaiement;
196
        }
197
        if (isset($this->num_payment)) {
198
            $this->num_payment = trim($this->num_payment);
199
        }
200
        if (isset($this->note_private)) {
201
            $this->note_private = trim($this->note_private);
202
        }
203
        if (isset($this->fk_bank)) {
204
            $this->fk_bank = (int) $this->fk_bank;
205
        }
206
        if (isset($this->fk_user_creat)) {
207
            $this->fk_user_creat = (int) $this->fk_user_creat;
208
        }
209
        if (isset($this->fk_user_modif)) {
210
            $this->fk_user_modif = (int) $this->fk_user_modif;
211
        }
212
213
        $totalamount = 0;
214
        foreach ($this->amounts as $key => $value) {  // How payment is dispatch
215
            $newvalue = (float) price2num($value, 'MT');
216
            $this->amounts[$key] = $newvalue;
217
            $totalamount += $newvalue;
218
        }
219
        $totalamount = (float) price2num($totalamount);
220
221
        // Check parameters
222
        if ($totalamount == 0) {
223
            return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
224
        }
225
226
227
        $this->db->begin();
228
229
        if ($totalamount != 0) {
230
            $sql = "INSERT INTO " . MAIN_DB_PREFIX . "paiementcharge (fk_charge, datec, datep, amount,";
231
            $sql .= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
232
            $sql .= " VALUES ($this->chid, '" . $this->db->idate($now) . "',";
233
            $sql .= " '" . $this->db->idate($this->datepaye) . "',";
234
            $sql .= " " . ((float) $totalamount) . ",";
235
            $sql .= " " . ((int) $this->paiementtype) . ", '" . $this->db->escape($this->num_payment) . "', '" . $this->db->escape($this->note) . "', " . $user->id . ",";
236
            $sql .= " 0)";
237
238
            $resql = $this->db->query($sql);
239
            if ($resql) {
240
                $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "paiementcharge");
241
242
                // Insere tableau des montants / factures
243
                foreach ($this->amounts as $key => $amount) {
244
                    $contribid = $key;
245
                    if (is_numeric($amount) && $amount != 0) {
246
                        $amount = (float) price2num($amount);
247
248
                        // If we want to closed paid invoices
249
                        if ($closepaidcontrib) {
250
                            $contrib = new ChargeSociales($this->db);
251
                            $contrib->fetch($contribid);
252
                            $paiement = $contrib->getSommePaiement();
253
                            //$creditnotes=$contrib->getSumCreditNotesUsed();
254
                            $creditnotes = 0;
255
                            //$deposits=$contrib->getSumDepositsUsed();
256
                            $deposits = 0;
257
                            $alreadypayed = (float) price2num($paiement + $creditnotes + $deposits, 'MT');
258
                            $remaintopay = (float) price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
259
                            if ($remaintopay == 0) {
260
                                $result = $contrib->setPaid($user);
261
                            } else {
262
                                dol_syslog("Remain to pay for conrib " . $contribid . " not null. We do nothing.");
263
                            }
264
                        }
265
                    }
266
                }
267
            } else {
268
                $error++;
269
            }
270
        }
271
272
        $result = $this->call_trigger('PAYMENTSOCIALCONTRIBUTION_CREATE', $user);
273
        if ($result < 0) {
274
            $error++;
275
        }
276
277
        if ($totalamount != 0 && !$error) {
278
            $this->amount = $totalamount;
279
            $this->total = $totalamount; // deprecated
280
            $this->db->commit();
281
            return $this->id;
282
        } else {
283
            $this->error = $this->db->error();
284
            $this->db->rollback();
285
            return -1;
286
        }
287
    }
288
289
    /**
290
     *  Load object in memory from database
291
     *
292
     *  @param  int     $id         Id object
293
     *  @return int                 Return integer <0 if KO, >0 if OK
294
     */
295
    public function fetch($id)
296
    {
297
        global $langs;
298
        $sql = "SELECT";
299
        $sql .= " t.rowid,";
300
        $sql .= " t.fk_charge,";
301
        $sql .= " t.datec,";
302
        $sql .= " t.tms,";
303
        $sql .= " t.datep,";
304
        $sql .= " t.amount,";
305
        $sql .= " t.fk_typepaiement,";
306
        $sql .= " t.num_paiement as num_payment,";
307
        $sql .= " t.note,";
308
        $sql .= " t.fk_bank,";
309
        $sql .= " t.fk_user_creat,";
310
        $sql .= " t.fk_user_modif,";
311
        $sql .= " pt.code as type_code, pt.libelle as type_label,";
312
        $sql .= ' b.fk_account';
313
        $sql .= " FROM " . MAIN_DB_PREFIX . "paiementcharge as t LEFT JOIN " . MAIN_DB_PREFIX . "c_paiement as pt ON t.fk_typepaiement = pt.id";
314
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON t.fk_bank = b.rowid';
315
        $sql .= " WHERE t.rowid = " . ((int) $id);
316
        // TODO link on entity of tax;
317
318
        dol_syslog(get_class($this) . "::fetch", LOG_DEBUG);
319
        $resql = $this->db->query($sql);
320
        if ($resql) {
321
            if ($this->db->num_rows($resql)) {
322
                $obj = $this->db->fetch_object($resql);
323
324
                $this->id    = $obj->rowid;
325
                $this->ref   = $obj->rowid;
326
327
                $this->fk_charge = $obj->fk_charge;
328
                $this->datec = $this->db->jdate($obj->datec);
329
                $this->tms = $this->db->jdate($obj->tms);
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->db->jdate($obj->tms) can also be of type string. However, the property $tms is declared as type integer. Maybe add an additional type check?

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

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

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

class Id
{
    public $id;

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

}

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

$account_id = false;

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

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
Deprecated Code introduced by
The property 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

329
                /** @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...
330
                $this->datep = $this->db->jdate($obj->datep);
331
                $this->amount = $obj->amount;
332
                $this->total = $obj->amount;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Code\Compta\Cla...ialContribution::$total has been deprecated: Use $amount instead. ( Ignorable by Annotation )

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

332
                /** @scrutinizer ignore-deprecated */ $this->total = $obj->amount;

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...
333
                $this->fk_typepaiement = $obj->fk_typepaiement;
334
                $this->num_payment = $obj->num_payment;
335
                $this->num_paiement = $obj->num_payment;
0 ignored issues
show
Deprecated Code introduced by
The property Dolibarr\Code\Compta\Cla...ribution::$num_paiement has been deprecated: Use $num_payment instead ( Ignorable by Annotation )

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

335
                /** @scrutinizer ignore-deprecated */ $this->num_paiement = $obj->num_payment;

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...
336
                $this->note_private = $obj->note;
337
                $this->fk_bank = $obj->fk_bank;
338
                $this->fk_user_creat = $obj->fk_user_creat;
339
                $this->fk_user_modif = $obj->fk_user_modif;
340
341
                $this->type_code = $obj->type_code;
342
                $this->type_label = $obj->type_label;
343
344
                $this->bank_account   = $obj->fk_account;
345
                $this->bank_line      = $obj->fk_bank;
346
            }
347
            $this->db->free($resql);
348
349
            return 1;
350
        } else {
351
            $this->error = "Error " . $this->db->lasterror();
352
            return -1;
353
        }
354
    }
355
356
357
    /**
358
     *  Update database
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 = null, $notrigger = 0)
365
    {
366
        global $conf, $langs;
367
        $error = 0;
368
369
        // Clean parameters
370
371
        if (isset($this->fk_charge)) {
372
            $this->fk_charge = (int) $this->fk_charge;
373
        }
374
        if (isset($this->amount)) {
375
            $this->amount = (float) $this->amount;
376
        }
377
        if (isset($this->fk_typepaiement)) {
378
            $this->fk_typepaiement = (int) $this->fk_typepaiement;
379
        }
380
        if (isset($this->num_payment)) {
381
            $this->num_payment = trim($this->num_payment);
382
        }
383
        if (isset($this->note_private)) {
384
            $this->note_private = trim($this->note_private);
385
        }
386
        if (isset($this->fk_bank)) {
387
            $this->fk_bank = (int) $this->fk_bank;
388
        }
389
        if (isset($this->fk_user_creat)) {
390
            $this->fk_user_creat = (int) $this->fk_user_creat;
391
        }
392
        if (isset($this->fk_user_modif)) {
393
            $this->fk_user_modif = (int) $this->fk_user_modif;
394
        }
395
396
397
398
        // Check parameters
399
        // Put here code to add control on parameters values
400
401
        // Update request
402
        $sql = "UPDATE " . MAIN_DB_PREFIX . "paiementcharge SET";
403
        $sql .= " fk_charge=" . (isset($this->fk_charge) ? ((int) $this->fk_charge) : "null") . ",";
404
        $sql .= " datec=" . (dol_strlen($this->datec) != 0 ? "'" . $this->db->idate($this->datec) . "'" : 'null') . ",";
405
        $sql .= " tms=" . (dol_strlen($this->tms) != 0 ? "'" . $this->db->idate($this->tms) . "'" : 'null') . ",";
406
        $sql .= " datep=" . (dol_strlen($this->datep) != 0 ? "'" . $this->db->idate($this->datep) . "'" : 'null') . ",";
407
        $sql .= " amount=" . (isset($this->amount) ? price2num($this->amount) : "null") . ",";
408
        $sql .= " fk_typepaiement=" . (isset($this->fk_typepaiement) ? ((int) $this->fk_typepaiement) : "null") . ",";
409
        $sql .= " num_paiement=" . (isset($this->num_payment) ? "'" . $this->db->escape($this->num_payment) . "'" : "null") . ",";
410
        $sql .= " note=" . (isset($this->note) ? "'" . $this->db->escape($this->note) . "'" : "null") . ",";
411
        $sql .= " fk_bank=" . (isset($this->fk_bank) ? ((int) $this->fk_bank) : "null") . ",";
412
        $sql .= " fk_user_creat=" . (isset($this->fk_user_creat) ? ((int) $this->fk_user_creat) : "null") . ",";
413
        $sql .= " fk_user_modif=" . (isset($this->fk_user_modif) ? ((int) $this->fk_user_modif) : "null");
414
        $sql .= " WHERE rowid=" . ((int) $this->id);
415
416
        $this->db->begin();
417
418
        dol_syslog(get_class($this) . "::update", LOG_DEBUG);
419
        $resql = $this->db->query($sql);
420
        if (!$resql) {
421
            $error++;
422
            $this->errors[] = "Error " . $this->db->lasterror();
423
        }
424
425
        // Commit or rollback
426
        if ($error) {
427
            foreach ($this->errors as $errmsg) {
428
                dol_syslog(get_class($this) . "::update " . $errmsg, LOG_ERR);
429
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
430
            }
431
            $this->db->rollback();
432
            return -1 * $error;
433
        } else {
434
            $this->db->commit();
435
            return 1;
436
        }
437
    }
438
439
440
    /**
441
     *  Delete object in database
442
     *
443
     *  @param  User    $user           User that delete
444
     *  @param  int     $notrigger      0=launch triggers after, 1=disable triggers
445
     *  @return int                     Return integer <0 if KO, >0 if OK
446
     */
447
    public function delete($user, $notrigger = 0)
448
    {
449
        $error = 0;
450
451
        dol_syslog(get_class($this) . "::delete");
452
453
        $this->db->begin();
454
455
        if ($this->bank_line > 0) {
456
            $accline = new AccountLine($this->db);
457
            $accline->fetch($this->bank_line);
458
            $result = $accline->delete($user);
459
            if ($result < 0) {
460
                $this->errors[] = $accline->error;
461
                $error++;
462
            }
463
        }
464
465
        if (!$error) {
466
            $sql = "DELETE FROM " . MAIN_DB_PREFIX . "paiementcharge";
467
            $sql .= " WHERE rowid=" . ((int) $this->id);
468
469
            dol_syslog(get_class($this) . "::delete", LOG_DEBUG);
470
            $resql = $this->db->query($sql);
471
            if (!$resql) {
472
                $error++;
473
                $this->errors[] = "Error " . $this->db->lasterror();
474
            }
475
        }
476
477
        // Commit or rollback
478
        if ($error) {
479
            foreach ($this->errors as $errmsg) {
480
                dol_syslog(get_class($this) . "::delete " . $errmsg, LOG_ERR);
481
                $this->error .= ($this->error ? ', ' . $errmsg : $errmsg);
482
            }
483
            $this->db->rollback();
484
            return -1 * $error;
485
        } else {
486
            $this->db->commit();
487
            return 1;
488
        }
489
    }
490
491
492
493
    /**
494
     *  Load an object from its id and create a new one in database
495
     *
496
     *  @param  User    $user           User making the clone
497
     *  @param  int     $fromid         Id of object to clone
498
     *  @return int                     New id of clone
499
     */
500
    public function createFromClone(User $user, $fromid)
501
    {
502
        $error = 0;
503
504
        $object = new PaymentSocialContribution($this->db);
505
506
        $this->db->begin();
507
508
        // Load source object
509
        $object->fetch($fromid);
510
        $object->id = 0;
511
        $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

511
        /** @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...
512
513
        // Clear fields
514
        // ...
515
516
        // Create clone
517
        $object->context['createfromclone'] = 'createfromclone';
518
        $result = $object->create($user);
519
520
        // Other options
521
        if ($result < 0) {
522
            $this->error = $object->error;
523
            $error++;
524
        }
525
526
        unset($object->context['createfromclone']);
527
528
        // End
529
        if (!$error) {
530
            $this->db->commit();
531
            return $object->id;
532
        } else {
533
            $this->db->rollback();
534
            return -1;
535
        }
536
    }
537
538
539
    /**
540
     *  Initialise an instance with random values.
541
     *  Used to build previews or test instances.
542
     *  id must be 0 if object instance is a specimen.
543
     *
544
     *  @return int
545
     */
546
    public function initAsSpecimen()
547
    {
548
        $this->id = 0;
549
        $this->fk_charge = 0;
550
        $this->datec = dol_now();
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 = dol_now();
553
        $this->amount = 100;
554
        $this->fk_typepaiement = 0;
555
        $this->num_payment = 'ABC123';
556
        $this->note_private = '';
557
        $this->note_public = '';
558
        $this->fk_bank = 0;
559
        $this->fk_user_creat = 0;
560
        $this->fk_user_modif = 0;
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_sc'
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, $langs;
581
582
        // Clean data
583
        $this->num_payment = trim($this->num_payment);
584
585
        $error = 0;
586
587
        if (isModEnabled("bank")) {
588
589
            $acc = new Account($this->db);
590
            $acc->fetch($accountid);
591
592
            $total = $this->amount;
593
            if ($mode == 'payment_sc') {
594
                $total = -$total;
595
            }
596
597
            // Insert payment into llx_bank
598
            $bank_line_id = $acc->addline(
599
                $this->datepaye,
600
                $this->paiementtype, // Payment mode id or code ("CHQ or VIR for example")
601
                $label,
602
                $total,
603
                $this->num_payment,
604
                '',
605
                $user,
606
                $emetteur_nom,
607
                $emetteur_banque
608
            );
609
610
            // Mise a jour fk_bank dans llx_paiement.
611
            // On connait ainsi le paiement qui a genere l'ecriture bancaire
612
            if ($bank_line_id > 0) {
613
                $result = $this->update_fk_bank($bank_line_id);
614
                if ($result <= 0) {
615
                    $error++;
616
                    dol_print_error($this->db);
617
                }
618
619
                // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
620
                $url = '';
621
                if ($mode == 'payment_sc') {
622
                    $url = constant('BASE_URL') . '/compta/payment_sc/card.php?id=';
623
                }
624
                if ($url) {
625
                    $result = $acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
626
                    if ($result <= 0) {
627
                        $error++;
628
                        dol_print_error($this->db);
629
                    }
630
                }
631
632
                // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
633
                $linkaddedforthirdparty = array();
634
                foreach ($this->amounts as $key => $value) {
635
                    if ($mode == 'payment_sc') {
636
                        $socialcontrib = new ChargeSociales($this->db);
637
                        $socialcontrib->fetch($key);
638
                        $result = $acc->add_url_line($bank_line_id, $socialcontrib->id, constant('BASE_URL') . '/compta/charges.php?id=', $socialcontrib->type_label . (($socialcontrib->lib && $socialcontrib->lib != $socialcontrib->type_label) ? ' (' . $socialcontrib->lib . ')' : ''), 'sc');
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

638
                        $result = $acc->add_url_line($bank_line_id, $socialcontrib->id, constant('BASE_URL') . '/compta/charges.php?id=', $socialcontrib->type_label . (($socialcontrib->lib && $socialcontrib->lib != $socialcontrib->type_label) ? ' (' . /** @scrutinizer ignore-deprecated */ $socialcontrib->lib . ')' : ''), 'sc');

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...
639
                        if ($result <= 0) {
640
                            dol_print_error($this->db);
641
                        }
642
643
                        if ($socialcontrib->fk_user) {
644
                            $fuser = new User($this->db);
645
                            $fuser->fetch($socialcontrib->fk_user);
646
647
                            // Add link 'user' in bank_url between operation and bank transaction
648
                            $result = $acc->add_url_line(
649
                                $bank_line_id,
650
                                $socialcontrib->fk_user,
651
                                constant('BASE_URL') . '/user/card.php?id=',
652
                                $fuser->getFullName($langs),
653
                                'user'
654
                            );
655
656
                            if ($result <= 0) {
657
                                $this->error = $acc->error;
658
                                $error++;
659
                            }
660
                        }
661
                    }
662
                }
663
            } else {
664
                $this->error = $acc->error;
665
                $error++;
666
            }
667
        }
668
669
        if (!$error) {
670
            return 1;
671
        } else {
672
            return -1;
673
        }
674
    }
675
676
677
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
678
    /**
679
     *  Mise a jour du lien entre le paiement de  charge et la ligne dans llx_bank generee
680
     *
681
     *  @param  int     $id_bank         Id if bank
682
     *  @return int                      >0 if OK, <=0 if KO
683
     */
684
    public function update_fk_bank($id_bank)
685
    {
686
		// phpcs:enable
687
        $sql = "UPDATE " . MAIN_DB_PREFIX . "paiementcharge SET fk_bank = " . ((int) $id_bank) . " WHERE rowid = " . ((int) $this->id);
688
689
        dol_syslog(get_class($this) . "::update_fk_bank", LOG_DEBUG);
690
        $result = $this->db->query($sql);
691
        if ($result) {
692
            return 1;
693
        } else {
694
            $this->error = $this->db->error();
695
            return 0;
696
        }
697
    }
698
699
700
    /**
701
     *  Return the label of the status
702
     *
703
     *  @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
704
     *  @return string                 Label of status
705
     */
706
    public function getLibStatut($mode = 0)
707
    {
708
        return $this->LibStatut($this->statut, $mode);
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

708
        return $this->LibStatut(/** @scrutinizer ignore-deprecated */ $this->statut, $mode);

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...
709
    }
710
711
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
712
    /**
713
     *  Return the label of a given status
714
     *
715
     *  @param  int     $status        Id status
716
     *  @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
717
     *  @return string                 Label of status
718
     */
719
    public function LibStatut($status, $mode = 0)
720
    {
721
		// phpcs:enable
722
        global $langs; // TODO Renvoyer le libelle anglais et faire traduction a affichage
723
724
        $langs->load('compta');
725
        /*if ($mode == 0)
726
            {
727
            if ($status == 0) return $langs->trans('ToValidate');
728
            if ($status == 1) return $langs->trans('Validated');
729
            }
730
            if ($mode == 1)
731
            {
732
            if ($status == 0) return $langs->trans('ToValidate');
733
            if ($status == 1) return $langs->trans('Validated');
734
            }
735
            if ($mode == 2)
736
            {
737
            if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
738
            if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
739
            }
740
            if ($mode == 3)
741
            {
742
            if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
743
            if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
744
            }
745
            if ($mode == 4)
746
            {
747
            if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
748
            if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
749
            }
750
            if ($mode == 5)
751
            {
752
            if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
753
            if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
754
            }
755
            if ($mode == 6)
756
            {
757
            if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
758
            if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
759
            }*/
760
        return '';
761
    }
762
763
    /**
764
     *  Return clicable name (with picto eventually)
765
     *
766
     *  @param  int     $withpicto      0=No picto, 1=Include picto into link, 2=Only picto
767
     *  @param  int     $maxlen         Longueur max libelle
768
     *  @return string                  Chaine avec URL
769
     */
770
    public function getNomUrl($withpicto = 0, $maxlen = 0)
771
    {
772
        global $langs;
773
774
        $result = '';
775
776
        if (empty($this->ref)) {
777
            $this->ref = $this->label;
778
        }
779
780
        $label = img_picto('', $this->picto) . ' <u>' . $langs->trans("SocialContributionPayment") . '</u>';
781
        $label .= '<br><b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
782
        if (!empty($this->label)) {
783
            $labeltoshow = $this->label;
784
            $reg = array();
785
            if (preg_match('/^\((.*)\)$/i', $this->label, $reg)) {
786
                // Label generique car entre parentheses. On l'affiche en le traduisant
787
                if ($reg[1] == 'paiement') {
788
                    $reg[1] = 'Payment';
789
                }
790
                $labeltoshow = $langs->trans($reg[1]);
791
            }
792
            $label .= '<br><b>' . $langs->trans('Label') . ':</b> ' . $labeltoshow;
793
        }
794
        if ($this->datep) {
795
            $label .= '<br><b>' . $langs->trans('Date') . ':</b> ' . dol_print_date($this->datep, 'day');
796
        }
797
798
        if (!empty($this->id)) {
799
            $link = '<a href="' . constant('BASE_URL') . '/compta/payment_sc/card.php?id=' . $this->id . '" title="' . dol_escape_htmltag($label, 1) . '" class="classfortooltip">';
800
            $linkend = '</a>';
801
802
            if ($withpicto) {
803
                $result .= ($link . img_object($label, 'payment', 'class="classfortooltip"') . $linkend . ' ');
804
            }
805
            if ($withpicto && $withpicto != 2) {
806
                $result .= ' ';
807
            }
808
            if ($withpicto != 2) {
809
                $result .= $link . ($maxlen ? dol_trunc($this->ref, $maxlen) : $this->ref) . $linkend;
810
            }
811
        }
812
813
        return $result;
814
    }
815
816
817
    /**
818
     *  Return if object was dispatched into bookkeeping
819
     *
820
     *  @return     int         Return integer <0 if KO, 0=no, 1=yes
821
     */
822
    public function getVentilExportCompta()
823
    {
824
        $alreadydispatched = 0;
825
826
        $type = 'bank';
827
828
        $sql = " SELECT COUNT(ab.rowid) as nb FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='" . $this->db->escape($type) . "' AND ab.fk_doc = " . ((int) $this->bank_line);
829
        $resql = $this->db->query($sql);
830
        if ($resql) {
831
            $obj = $this->db->fetch_object($resql);
832
            if ($obj) {
833
                $alreadydispatched = $obj->nb;
834
            }
835
        } else {
836
            $this->error = $this->db->lasterror();
837
            return -1;
838
        }
839
840
        if ($alreadydispatched) {
841
            return 1;
842
        }
843
        return 0;
844
    }
845
}
846