Passed
Push — dev ( f7d146...05f415 )
by Rafael
60:50
created

PaymentSocialContribution::create()   F

Complexity

Conditions 22
Paths 6657

Size

Total Lines 115
Code Lines 73

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 22
eloc 73
nc 6657
nop 2
dl 0
loc 115
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
use Dolibarr\Code\User\Classes\User;
27
use Dolibarr\Core\Base\CommonObject;
28
use DoliDB;
29
30
/**
31
 *      \file       htdocs/compta/sociales/class/paymentsocialcontribution.class.php
32
 *      \ingroup    invoice
33
 *      \brief      File of class to manage payment of social contributions
34
 */
35
36
/**
37
 *  Class to manage payments of social contributions
38
 * Model Paiementcharge
39
 */
40
class PaymentSocialContribution extends CommonObject
41
{
42
    /**
43
     * @var string ID to identify managed object
44
     */
45
    public $element = 'paiementcharge';
46
47
    /**
48
     * @var string Name of table without prefix where object is stored
49
     */
50
    public $table_element = 'paiementcharge';
51
52
    /**
53
     * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
54
     */
55
    public $picto = 'payment';
56
57
    /**
58
     * @var string  Label
59
     */
60
    public $label;
61
62
    /**
63
     * @var int ID
64
     */
65
    public $fk_charge;
66
67
    public $datec = '';
68
    public $datep = '';
69
70
    /**
71
     * @var string
72
     */
73
    public $type_code;
74
75
    /**
76
     * @var string
77
     */
78
    public $type_label;
79
80
    /**
81
     * @var int
82
     */
83
    public $bank_account;
84
85
    /**
86
     * @var int
87
     */
88
    public $bank_line;
89
90
    /**
91
     * @deprecated  Use $amount instead.
92
     * @see $amount
93
     * @var float|int
94
     */
95
    public $total;
96
97
    /**
98
     * @var float|int
99
     */
100
    public $amount; // Total amount of payment
101
    /**
102
     * @var array<float|int>
103
     */
104
    public $amounts = array(); // Array of amounts
105
106
    /**
107
     * @var int ID
108
     */
109
    public $fk_typepaiement;
110
111
    /**
112
     * @var string
113
     * @deprecated Use $num_payment instead
114
     * @see $num_payment
115
     */
116
    public $num_paiement;
117
118
    /**
119
     * @var string      Payment reference
120
     *                  (Cheque or bank transfer reference. Can be "ABC123")
121
     */
122
    public $num_payment;
123
124
    /**
125
     * @var int ID
126
     */
127
    public $fk_bank;
128
129
    /**
130
     * @var int ID
131
     */
132
    public $fk_user_creat;
133
134
    /**
135
     * @var int ID
136
     */
137
    public $fk_user_modif;
138
139
    /**
140
     * @var int ID
141
     */
142
    public $chid;
143
144
    /**
145
     * @var integer|string datepaye
146
     */
147
    public $datepaye;
148
149
    /**
150
     * @var integer|string paiementtype
151
     */
152
    public $paiementtype;
153
154
    /**
155
     *  Constructor
156
     *
157
     *  @param      DoliDB      $db      Database handler
158
     */
159
    public function __construct(DoliDB $db)
160
    {
161
        $this->db = $db;
162
    }
163
164
    /**
165
     *  Create payment of social contribution into database.
166
     *  Use this->amounts to have list of lines for the payment
167
     *
168
     *  @param      User    $user                   User making payment
169
     *  @param      int     $closepaidcontrib       1=Also close paid contributions to paid, 0=Do nothing more
170
     *  @return     int                             Return integer <0 if KO, id of payment if OK
171
     */
172
    public function create($user, $closepaidcontrib = 0)
173
    {
174
        global $conf, $langs;
175
176
        $error = 0;
177
178
        $now = dol_now();
179
180
        dol_syslog(get_only_class($this) . "::create", LOG_DEBUG);
181
182
        // Validate parameters
183
        if (!$this->datepaye) {
184
            $this->error = 'ErrorBadValueForParameterCreatePaymentSocialContrib';
185
            return -1;
186
        }
187
188
        // Clean parameters
189
        if (isset($this->fk_charge)) {
190
            $this->fk_charge = (int) $this->fk_charge;
191
        }
192
        if (isset($this->amount)) {
193
            $this->amount = (float) $this->amount;
194
        }
195
        if (isset($this->fk_typepaiement)) {
196
            $this->fk_typepaiement = (int) $this->fk_typepaiement;
197
        }
198
        if (isset($this->num_payment)) {
199
            $this->num_payment = trim($this->num_payment);
200
        }
201
        if (isset($this->note_private)) {
202
            $this->note_private = trim($this->note_private);
203
        }
204
        if (isset($this->fk_bank)) {
205
            $this->fk_bank = (int) $this->fk_bank;
206
        }
207
        if (isset($this->fk_user_creat)) {
208
            $this->fk_user_creat = (int) $this->fk_user_creat;
209
        }
210
        if (isset($this->fk_user_modif)) {
211
            $this->fk_user_modif = (int) $this->fk_user_modif;
212
        }
213
214
        $totalamount = 0;
215
        foreach ($this->amounts as $key => $value) {  // How payment is dispatch
216
            $newvalue = (float) price2num($value, 'MT');
217
            $this->amounts[$key] = $newvalue;
218
            $totalamount += $newvalue;
219
        }
220
        $totalamount = (float) price2num($totalamount);
221
222
        // Check parameters
223
        if ($totalamount == 0) {
224
            return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
225
        }
226
227
228
        $this->db->begin();
229
230
        if ($totalamount != 0) {
231
            $sql = "INSERT INTO " . MAIN_DB_PREFIX . "paiementcharge (fk_charge, datec, datep, amount,";
232
            $sql .= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
233
            $sql .= " VALUES ($this->chid, '" . $this->db->idate($now) . "',";
234
            $sql .= " '" . $this->db->idate($this->datepaye) . "',";
235
            $sql .= " " . ((float) $totalamount) . ",";
236
            $sql .= " " . ((int) $this->paiementtype) . ", '" . $this->db->escape($this->num_payment) . "', '" . $this->db->escape($this->note) . "', " . $user->id . ",";
237
            $sql .= " 0)";
238
239
            $resql = $this->db->query($sql);
240
            if ($resql) {
241
                $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX . "paiementcharge");
242
243
                // Insere tableau des montants / factures
244
                foreach ($this->amounts as $key => $amount) {
245
                    $contribid = $key;
246
                    if (is_numeric($amount) && $amount != 0) {
247
                        $amount = (float) price2num($amount);
248
249
                        // If we want to closed paid invoices
250
                        if ($closepaidcontrib) {
251
                            $contrib = new ChargeSociales($this->db);
252
                            $contrib->fetch($contribid);
253
                            $paiement = $contrib->getSommePaiement();
254
                            //$creditnotes=$contrib->getSumCreditNotesUsed();
255
                            $creditnotes = 0;
256
                            //$deposits=$contrib->getSumDepositsUsed();
257
                            $deposits = 0;
258
                            $alreadypayed = (float) price2num($paiement + $creditnotes + $deposits, 'MT');
259
                            $remaintopay = (float) price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
260
                            if ($remaintopay == 0) {
261
                                $result = $contrib->setPaid($user);
262
                            } else {
263
                                dol_syslog("Remain to pay for conrib " . $contribid . " not null. We do nothing.");
264
                            }
265
                        }
266
                    }
267
                }
268
            } else {
269
                $error++;
270
            }
271
        }
272
273
        $result = $this->call_trigger('PAYMENTSOCIALCONTRIBUTION_CREATE', $user);
274
        if ($result < 0) {
275
            $error++;
276
        }
277
278
        if ($totalamount != 0 && !$error) {
279
            $this->amount = $totalamount;
280
            $this->total = $totalamount; // deprecated
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
     *  Load object in memory from database
292
     *
293
     *  @param  int     $id         Id object
294
     *  @return int                 Return integer <0 if KO, >0 if OK
295
     */
296
    public function fetch($id)
297
    {
298
        global $langs;
299
        $sql = "SELECT";
300
        $sql .= " t.rowid,";
301
        $sql .= " t.fk_charge,";
302
        $sql .= " t.datec,";
303
        $sql .= " t.tms,";
304
        $sql .= " t.datep,";
305
        $sql .= " t.amount,";
306
        $sql .= " t.fk_typepaiement,";
307
        $sql .= " t.num_paiement as num_payment,";
308
        $sql .= " t.note,";
309
        $sql .= " t.fk_bank,";
310
        $sql .= " t.fk_user_creat,";
311
        $sql .= " t.fk_user_modif,";
312
        $sql .= " pt.code as type_code, pt.libelle as type_label,";
313
        $sql .= ' b.fk_account';
314
        $sql .= " FROM " . MAIN_DB_PREFIX . "paiementcharge as t LEFT JOIN " . MAIN_DB_PREFIX . "c_paiement as pt ON t.fk_typepaiement = pt.id";
315
        $sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'bank as b ON t.fk_bank = b.rowid';
316
        $sql .= " WHERE t.rowid = " . ((int) $id);
317
        // TODO link on entity of tax;
318
319
        dol_syslog(get_only_class($this) . "::fetch", LOG_DEBUG);
320
        $resql = $this->db->query($sql);
321
        if ($resql) {
322
            if ($this->db->num_rows($resql)) {
323
                $obj = $this->db->fetch_object($resql);
324
325
                $this->id    = $obj->rowid;
326
                $this->ref   = $obj->rowid;
327
328
                $this->fk_charge = $obj->fk_charge;
329
                $this->datec = $this->db->jdate($obj->datec);
330
                $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

330
                /** @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...
331
                $this->datep = $this->db->jdate($obj->datep);
332
                $this->amount = $obj->amount;
333
                $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

333
                /** @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...
334
                $this->fk_typepaiement = $obj->fk_typepaiement;
335
                $this->num_payment = $obj->num_payment;
336
                $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

336
                /** @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...
337
                $this->note_private = $obj->note;
338
                $this->fk_bank = $obj->fk_bank;
339
                $this->fk_user_creat = $obj->fk_user_creat;
340
                $this->fk_user_modif = $obj->fk_user_modif;
341
342
                $this->type_code = $obj->type_code;
343
                $this->type_label = $obj->type_label;
344
345
                $this->bank_account   = $obj->fk_account;
346
                $this->bank_line      = $obj->fk_bank;
347
            }
348
            $this->db->free($resql);
349
350
            return 1;
351
        } else {
352
            $this->error = "Error " . $this->db->lasterror();
353
            return -1;
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_only_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_only_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_only_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_only_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_only_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_only_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