Passed
Branch develop (5cbde9)
by
unknown
26:38
created

PaymentSocialContribution::update()   F

Complexity

Conditions 24
Paths > 20000

Size

Total Lines 76
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 36
c 0
b 0
f 0
nc 4194304
nop 2
dl 0
loc 76
rs 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
/* Copyright (C) 2002      Rodolphe Quiedeville <[email protected]>
3
 * Copyright (C) 2004-2007 Laurent Destailleur  <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
/**
20
 *      \file       htdocs/compta/sociales/class/paymentsocialcontribution.class.php
21
 *		\ingroup    facture
22
 *		\brief      File of class to manage payment of social contributions
23
 */
24
25
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
require_once DOL_DOCUMENT_ROOT.'/compta/sociales/class/chargesociales.class.php';
27
28
29
/**
30
 *	Class to manage payments of social contributions
31
 */
32
class PaymentSocialContribution extends CommonObject
33
{
34
	/**
35
	 * @var string ID to identify managed object
36
	 */
37
	public $element='paiementcharge';
38
39
	/**
40
	 * @var string Name of table without prefix where object is stored
41
	 */
42
	public $table_element='paiementcharge';
43
44
	/**
45
	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
46
	 */
47
	public $picto = 'payment';
48
49
	/**
50
     * @var int ID
51
     */
52
	public $fk_charge;
53
54
	public $datec='';
55
	public $tms='';
56
	public $datep='';
57
58
	/**
59
	 * @deprecated
60
	 * @see amount
61
	 */
62
	public $total;
63
64
    public $amount;            // Total amount of payment
65
    public $amounts=array();   // Array of amounts
66
67
    /**
68
     * @var int ID
69
     */
70
	public $fk_typepaiement;
71
72
	public $num_paiement;
73
74
	/**
75
     * @var int ID
76
     */
77
	public $fk_bank;
78
79
	/**
80
     * @var int ID
81
     */
82
	public $fk_user_creat;
83
84
	/**
85
     * @var int ID
86
     */
87
	public $fk_user_modif;
88
89
	/**
90
	 *	Constructor
91
	 *
92
	 *  @param		DoliDB		$db      Database handler
93
	 */
94
	public function __construct($db)
95
	{
96
		$this->db = $db;
97
	}
98
99
	/**
100
	 *  Create payment of social contribution into database.
101
     *  Use this->amounts to have list of lines for the payment
102
     *
103
	 *  @param      User	$user   				User making payment
104
	 *	@param		int		$closepaidcontrib   	1=Also close payed contributions to paid, 0=Do nothing more
105
	 *  @return     int     						<0 if KO, id of payment if OK
106
	 */
107
	public function create($user, $closepaidcontrib = 0)
108
	{
109
		global $conf, $langs;
110
111
		$error=0;
112
113
        $now=dol_now();
114
115
		dol_syslog(get_class($this)."::create", LOG_DEBUG);
116
117
		// Validate parametres
118
		if (! $this->datepaye)
0 ignored issues
show
Bug introduced by
The property datepaye does not exist on PaymentSocialContribution. Did you mean datep?
Loading history...
119
		{
120
			$this->error='ErrorBadValueForParameterCreatePaymentSocialContrib';
121
			return -1;
122
		}
123
124
		// Clean parameters
125
		if (isset($this->fk_charge)) $this->fk_charge= (int) $this->fk_charge;
126
		if (isset($this->amount)) $this->amount=trim($this->amount);
127
		if (isset($this->fk_typepaiement)) $this->fk_typepaiement= (int) $this->fk_typepaiement;
128
		if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement);
129
		if (isset($this->note)) $this->note=trim($this->note);
130
		if (isset($this->fk_bank)) $this->fk_bank= (int) $this->fk_bank;
131
		if (isset($this->fk_user_creat)) $this->fk_user_creat= (int) $this->fk_user_creat;
132
		if (isset($this->fk_user_modif)) $this->fk_user_modif= (int) $this->fk_user_modif;
133
134
        $totalamount = 0;
135
        foreach ($this->amounts as $key => $value)  // How payment is dispatch
136
        {
137
            $newvalue = price2num($value, 'MT');
138
            $this->amounts[$key] = $newvalue;
139
            $totalamount += $newvalue;
140
        }
141
        $totalamount = price2num($totalamount);
142
143
        // Check parameters
144
        if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
145
146
147
		$this->db->begin();
148
149
		if ($totalamount != 0)
150
		{
151
			$sql = "INSERT INTO ".MAIN_DB_PREFIX."paiementcharge (fk_charge, datec, datep, amount,";
152
			$sql.= " fk_typepaiement, num_paiement, note, fk_user_creat, fk_bank)";
153
			$sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
154
			$sql.= " '".$this->db->idate($this->datepaye)."',";
155
			$sql.= " ".$totalamount.",";
156
			$sql.= " ".$this->paiementtype.", '".$this->db->escape($this->num_paiement)."', '".$this->db->escape($this->note)."', ".$user->id.",";
157
			$sql.= " 0)";
158
159
			$resql=$this->db->query($sql);
160
			if ($resql)
161
			{
162
				$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."paiementcharge");
163
164
				// Insere tableau des montants / factures
165
				foreach ($this->amounts as $key => $amount)
166
				{
167
					$contribid = $key;
168
					if (is_numeric($amount) && $amount <> 0)
169
					{
170
						$amount = price2num($amount);
171
172
						// If we want to closed payed invoices
173
						if ($closepaidcontrib)
174
						{
175
176
							$contrib=new ChargeSociales($this->db);
177
							$contrib->fetch($contribid);
178
							$paiement = $contrib->getSommePaiement();
179
							//$creditnotes=$contrib->getSumCreditNotesUsed();
180
							$creditnotes=0;
181
							//$deposits=$contrib->getSumDepositsUsed();
182
							$deposits=0;
183
							$alreadypayed=price2num($paiement + $creditnotes + $deposits, 'MT');
184
							$remaintopay=price2num($contrib->amount - $paiement - $creditnotes - $deposits, 'MT');
185
							if ($remaintopay == 0)
186
							{
187
								$result=$contrib->set_paid($user, '', '');
0 ignored issues
show
Unused Code introduced by
The call to ChargeSociales::set_paid() has too many arguments starting with ''. ( Ignorable by Annotation )

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

187
								/** @scrutinizer ignore-call */ 
188
        $result=$contrib->set_paid($user, '', '');

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
188
							}
189
							else dol_syslog("Remain to pay for conrib ".$contribid." not null. We do nothing.");
190
						}
191
					}
192
				}
193
			}
194
			else
195
			{
196
				$error++;
197
			}
198
		}
199
200
		$result = $this->call_trigger('PAYMENTSOCIALCONTRIBUTION_CREATE', $user);
201
		if($result < 0) $error++;
202
203
		if ($totalamount != 0 && ! $error)
204
		{
205
		    $this->amount=$totalamount;
206
            $this->total=$totalamount;    // deprecated
207
		    $this->db->commit();
208
			return $this->id;
209
		}
210
		else
211
		{
212
			$this->error=$this->db->error();
213
			$this->db->rollback();
214
			return -1;
215
		}
216
	}
217
218
	/**
219
	 *  Load object in memory from database
220
	 *
221
	 *  @param	int		$id         Id object
222
	 *  @return int         		<0 if KO, >0 if OK
223
	 */
224
	public function fetch($id)
225
	{
226
		global $langs;
227
		$sql = "SELECT";
228
		$sql.= " t.rowid,";
229
		$sql.= " t.fk_charge,";
230
		$sql.= " t.datec,";
231
		$sql.= " t.tms,";
232
		$sql.= " t.datep,";
233
		$sql.= " t.amount,";
234
		$sql.= " t.fk_typepaiement,";
235
		$sql.= " t.num_paiement,";
236
		$sql.= " t.note,";
237
		$sql.= " t.fk_bank,";
238
		$sql.= " t.fk_user_creat,";
239
		$sql.= " t.fk_user_modif,";
240
		$sql.= " pt.code as type_code, pt.libelle as type_libelle,";
241
		$sql.= ' b.fk_account';
242
		$sql.= " FROM ".MAIN_DB_PREFIX."paiementcharge as t LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepaiement = pt.id";
243
		$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
244
		$sql.= " WHERE t.rowid = ".$id;
245
		// TODO link on entity of tax;
246
247
		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
248
		$resql=$this->db->query($sql);
249
		if ($resql)
250
		{
251
			if ($this->db->num_rows($resql))
252
			{
253
				$obj = $this->db->fetch_object($resql);
254
255
				$this->id    = $obj->rowid;
256
				$this->ref   = $obj->rowid;
257
258
				$this->fk_charge = $obj->fk_charge;
259
				$this->datec = $this->db->jdate($obj->datec);
260
				$this->tms = $this->db->jdate($obj->tms);
261
				$this->datep = $this->db->jdate($obj->datep);
262
				$this->amount = $obj->amount;
263
				$this->fk_typepaiement = $obj->fk_typepaiement;
264
				$this->num_paiement = $obj->num_paiement;
265
				$this->note = $obj->note;
266
				$this->fk_bank = $obj->fk_bank;
267
				$this->fk_user_creat = $obj->fk_user_creat;
268
				$this->fk_user_modif = $obj->fk_user_modif;
269
270
				$this->type_code = $obj->type_code;
271
				$this->type_libelle = $obj->type_libelle;
272
273
				$this->bank_account   = $obj->fk_account;
274
				$this->bank_line      = $obj->fk_bank;
275
			}
276
			$this->db->free($resql);
277
278
			return 1;
279
		}
280
		else
281
		{
282
			$this->error="Error ".$this->db->lasterror();
283
			return -1;
284
		}
285
	}
286
287
288
	/**
289
	 *  Update database
290
	 *
291
	 *  @param	User	$user        	User that modify
292
	 *  @param  int		$notrigger	    0=launch triggers after, 1=disable triggers
293
	 *  @return int         			<0 if KO, >0 if OK
294
	 */
295
	public function update($user = null, $notrigger = 0)
296
	{
297
		global $conf, $langs;
298
		$error=0;
299
300
		// Clean parameters
301
302
		if (isset($this->fk_charge)) $this->fk_charge= (int) $this->fk_charge;
303
		if (isset($this->amount)) $this->amount=trim($this->amount);
304
		if (isset($this->fk_typepaiement)) $this->fk_typepaiement= (int) $this->fk_typepaiement;
305
		if (isset($this->num_paiement)) $this->num_paiement=trim($this->num_paiement);
306
		if (isset($this->note)) $this->note=trim($this->note);
307
		if (isset($this->fk_bank)) $this->fk_bank= (int) $this->fk_bank;
308
		if (isset($this->fk_user_creat)) $this->fk_user_creat= (int) $this->fk_user_creat;
309
		if (isset($this->fk_user_modif)) $this->fk_user_modif= (int) $this->fk_user_modif;
310
311
312
313
		// Check parameters
314
		// Put here code to add control on parameters values
315
316
		// Update request
317
		$sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET";
318
319
		$sql.= " fk_charge=".(isset($this->fk_charge)?$this->fk_charge:"null").",";
320
		$sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
321
		$sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
322
		$sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
323
		$sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
324
		$sql.= " fk_typepaiement=".(isset($this->fk_typepaiement)?$this->fk_typepaiement:"null").",";
325
		$sql.= " num_paiement=".(isset($this->num_paiement)?"'".$this->db->escape($this->num_paiement)."'":"null").",";
326
		$sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
327
		$sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
328
		$sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
329
		$sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
330
331
332
		$sql.= " WHERE rowid=".$this->id;
333
334
		$this->db->begin();
335
336
		dol_syslog(get_class($this)."::update", LOG_DEBUG);
337
		$resql = $this->db->query($sql);
338
		if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
339
340
		//if (! $error)
341
		//{
342
		//	if (! $notrigger)
343
		//	{
344
				// Uncomment this and change MYOBJECT to your own tag if you
345
				// want this action call a trigger.
346
347
				//// Call triggers
348
				//include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
349
				//$interface=new Interfaces($this->db);
350
				//$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
351
				//if ($result < 0) { $error++; $this->errors=$interface->errors; }
352
				//// End call triggers
353
		//	}
354
		//}
355
356
		// Commit or rollback
357
		if ($error)
358
		{
359
			foreach($this->errors as $errmsg)
360
			{
361
				dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
362
				$this->error.=($this->error?', '.$errmsg:$errmsg);
363
			}
364
			$this->db->rollback();
365
			return -1*$error;
366
		}
367
		else
368
		{
369
			$this->db->commit();
370
			return 1;
371
		}
372
	}
373
374
375
	/**
376
	 *  Delete object in database
377
	 *
378
	 *  @param	User	$user        	User that delete
379
	 *  @param  int		$notrigger		0=launch triggers after, 1=disable triggers
380
	 *  @return int						<0 if KO, >0 if OK
381
	 */
382
	public function delete($user, $notrigger = 0)
383
	{
384
		global $conf, $langs;
385
		$error=0;
386
387
		dol_syslog(get_class($this)."::delete");
388
389
		$this->db->begin();
390
391
	    if ($this->bank_line > 0)
392
        {
393
        	$accline = new AccountLine($this->db);
394
			$accline->fetch($this->bank_line);
395
			$result = $accline->delete();
396
			if($result < 0) {
397
				$this->errors[] = $accline->error;
398
				$error++;
399
			}
400
        }
401
402
		if (! $error)
403
		{
404
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."paiementcharge";
405
			$sql.= " WHERE rowid=".$this->id;
406
407
			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
408
			$resql = $this->db->query($sql);
409
			if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
410
		}
411
412
		//if (! $error)
413
		//{
414
		//	if (! $notrigger)
415
		//	{
416
				// Uncomment this and change MYOBJECT to your own tag if you
417
				// want this action call a trigger.
418
419
				//// Call triggers
420
				//include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
421
				//$interface=new Interfaces($this->db);
422
				//$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
423
				//if ($result < 0) { $error++; $this->errors=$interface->errors; }
424
				//// End call triggers
425
		//	}
426
		//}
427
428
		// Commit or rollback
429
		if ($error)
430
		{
431
			foreach($this->errors as $errmsg)
432
			{
433
				dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
434
				$this->error.=($this->error?', '.$errmsg:$errmsg);
435
			}
436
			$this->db->rollback();
437
			return -1*$error;
438
		}
439
		else
440
		{
441
			$this->db->commit();
442
			return 1;
443
		}
444
	}
445
446
447
448
	/**
449
	 *	Load an object from its id and create a new one in database
450
	 *
451
	 *  @param	User	$user		    User making the clone
452
	 *	@param	int		$fromid     	Id of object to clone
453
	 * 	@return	int						New id of clone
454
	 */
455
	public function createFromClone(User $user, $fromid)
456
	{
457
		$error=0;
458
459
		$object=new PaymentSocialContribution($this->db);
460
461
		$this->db->begin();
462
463
		// Load source object
464
		$object->fetch($fromid);
465
		$object->id=0;
466
		$object->statut=0;
467
468
		// Clear fields
469
		// ...
470
471
		// Create clone
472
		$object->context['createfromclone'] = 'createfromclone';
473
		$result=$object->create($user);
474
475
		// Other options
476
		if ($result < 0)
477
		{
478
			$this->error=$object->error;
479
			$error++;
480
		}
481
482
		unset($object->context['createfromclone']);
483
484
		// End
485
		if (! $error)
486
		{
487
			$this->db->commit();
488
			return $object->id;
489
		}
490
		else
491
		{
492
			$this->db->rollback();
493
			return -1;
494
		}
495
	}
496
497
498
	/**
499
     *  Initialise an instance with random values.
500
     *  Used to build previews or test instances.
501
     *	id must be 0 if object instance is a specimen.
502
     *
503
     *  @return	void
504
	 */
505
	public function initAsSpecimen()
506
	{
507
		$this->id=0;
508
509
		$this->fk_charge='';
1 ignored issue
show
Documentation Bug introduced by
The property $fk_charge was declared of type integer, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
510
		$this->datec='';
511
		$this->tms='';
512
		$this->datep='';
513
		$this->amount='';
514
		$this->fk_typepaiement='';
1 ignored issue
show
Documentation Bug introduced by
The property $fk_typepaiement was declared of type integer, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
515
		$this->num_paiement='';
516
		$this->note='';
1 ignored issue
show
Deprecated Code introduced by
The property CommonObject::$note has been deprecated. ( Ignorable by Annotation )

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

516
		/** @scrutinizer ignore-deprecated */ $this->note='';
Loading history...
517
		$this->fk_bank='';
1 ignored issue
show
Documentation Bug introduced by
The property $fk_bank was declared of type integer, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
518
		$this->fk_user_creat='';
1 ignored issue
show
Documentation Bug introduced by
The property $fk_user_creat was declared of type integer, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
519
		$this->fk_user_modif='';
1 ignored issue
show
Documentation Bug introduced by
The property $fk_user_modif was declared of type integer, but '' is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
520
	}
521
522
523
    /**
524
     *      Add record into bank for payment with links between this bank record and invoices of payment.
525
     *      All payment properties must have been set first like after a call to create().
526
     *
527
     *      @param	User	$user               Object of user making payment
528
     *      @param  string	$mode               'payment_sc'
529
     *      @param  string	$label              Label to use in bank record
530
     *      @param  int		$accountid          Id of bank account to do link with
531
     *      @param  string	$emetteur_nom       Name of transmitter
532
     *      @param  string	$emetteur_banque    Name of bank
533
     *      @return int                 		<0 if KO, >0 if OK
534
     */
535
    public function addPaymentToBank($user, $mode, $label, $accountid, $emetteur_nom, $emetteur_banque)
536
    {
537
        global $conf;
538
539
        $error=0;
540
541
        if (! empty($conf->banque->enabled))
542
        {
543
            include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
544
545
            $acc = new Account($this->db);
546
            $acc->fetch($accountid);
547
548
            $total=$this->total;
1 ignored issue
show
Deprecated Code introduced by
The property PaymentSocialContribution::$total has been deprecated. ( Ignorable by Annotation )

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

548
            $total=/** @scrutinizer ignore-deprecated */ $this->total;
Loading history...
549
            if ($mode == 'payment_sc') $total=-$total;
550
551
            // Insert payment into llx_bank
552
            $bank_line_id = $acc->addline(
553
                $this->datepaye,
0 ignored issues
show
Bug introduced by
The property datepaye does not exist on PaymentSocialContribution. Did you mean datep?
Loading history...
554
                $this->paiementtype,  // Payment mode id or code ("CHQ or VIR for example")
0 ignored issues
show
Bug Best Practice introduced by
The property paiementtype does not exist on PaymentSocialContribution. Did you maybe forget to declare it?
Loading history...
555
                $label,
556
                $total,
557
                $this->num_paiement,
558
                '',
559
                $user,
560
                $emetteur_nom,
561
                $emetteur_banque
562
            );
563
564
            // Mise a jour fk_bank dans llx_paiement.
565
            // On connait ainsi le paiement qui a genere l'ecriture bancaire
566
            if ($bank_line_id > 0)
567
            {
568
                $result=$this->update_fk_bank($bank_line_id);
569
                if ($result <= 0)
570
                {
571
                    $error++;
572
                    dol_print_error($this->db);
573
                }
574
575
                // Add link 'payment', 'payment_supplier', 'payment_sc' in bank_url between payment and bank transaction
576
                $url='';
577
                if ($mode == 'payment_sc') $url=DOL_URL_ROOT.'/compta/payment_sc/card.php?id=';
578
                if ($url)
579
                {
580
                    $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
581
                    if ($result <= 0)
582
                    {
583
                        $error++;
584
                        dol_print_error($this->db);
585
                    }
586
                }
587
588
                // Add link 'company' in bank_url between invoice and bank transaction (for each invoice concerned by payment)
589
                $linkaddedforthirdparty=array();
590
                foreach ($this->amounts as $key => $value)
591
                {
592
                    if ($mode == 'payment_sc')
593
                    {
594
                        $socialcontrib = new ChargeSociales($this->db);
595
                        $socialcontrib->fetch($key);
596
                        $result=$acc->add_url_line($bank_line_id, $socialcontrib->id, DOL_URL_ROOT.'/compta/charges.php?id=', $socialcontrib->type_libelle.(($socialcontrib->lib && $socialcontrib->lib!=$socialcontrib->type_libelle)?' ('.$socialcontrib->lib.')':''), 'sc');
597
                        if ($result <= 0) dol_print_error($this->db);
598
                    }
599
                }
600
            }
601
            else
602
            {
603
                $this->error=$acc->error;
604
                $error++;
605
            }
606
        }
607
608
        if (! $error)
609
        {
610
            return 1;
611
        }
612
        else
613
        {
614
            return -1;
615
        }
616
    }
617
618
619
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
620
	/**
621
	 *  Mise a jour du lien entre le paiement de  charge et la ligne dans llx_bank generee
622
	 *
623
	 *  @param	int		$id_bank         Id if bank
624
	 *  @return	int			             >0 if OK, <=0 if KO
625
	 */
626
	public function update_fk_bank($id_bank)
627
	{
628
        // phpcs:enable
629
		$sql = "UPDATE ".MAIN_DB_PREFIX."paiementcharge SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
630
631
		dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
632
		$result = $this->db->query($sql);
633
		if ($result)
634
		{
635
			return 1;
636
		}
637
		else
638
		{
639
			$this->error=$this->db->error();
640
			return 0;
641
		}
642
	}
643
644
645
	/**
646
	 * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
647
	 *
648
	 * @param	int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
649
	 * @return  string				Libelle
650
	 */
651
	public function getLibStatut($mode = 0)
652
	{
653
		return $this->LibStatut($this->statut, $mode);
654
	}
655
656
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
657
	/**
658
	 * Renvoi le libelle d'un statut donne
659
	 *
660
	 * @param   int		$status     Statut
661
	 * @param   int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
662
	 * @return	string  		    Libelle du statut
663
	 */
664
	public function LibStatut($status, $mode = 0)
665
	{
666
        // phpcs:enable
667
		global $langs;	// TODO Renvoyer le libelle anglais et faire traduction a affichage
668
669
		$langs->load('compta');
670
		/*if ($mode == 0)
671
			{
672
			if ($status == 0) return $langs->trans('ToValidate');
673
			if ($status == 1) return $langs->trans('Validated');
674
			}
675
			if ($mode == 1)
676
			{
677
			if ($status == 0) return $langs->trans('ToValidate');
678
			if ($status == 1) return $langs->trans('Validated');
679
			}
680
			if ($mode == 2)
681
			{
682
			if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
683
			if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
684
			}
685
			if ($mode == 3)
686
			{
687
			if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1');
688
			if ($status == 1) return img_picto($langs->trans('Validated'),'statut4');
689
			}
690
			if ($mode == 4)
691
			{
692
			if ($status == 0) return img_picto($langs->trans('ToValidate'),'statut1').' '.$langs->trans('ToValidate');
693
			if ($status == 1) return img_picto($langs->trans('Validated'),'statut4').' '.$langs->trans('Validated');
694
			}
695
			if ($mode == 5)
696
			{
697
			if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
698
			if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
699
			}
700
			if ($mode == 6)
701
			{
702
			if ($status == 0) return $langs->trans('ToValidate').' '.img_picto($langs->trans('ToValidate'),'statut1');
703
			if ($status == 1) return $langs->trans('Validated').' '.img_picto($langs->trans('Validated'),'statut4');
704
			}*/
705
		return '';
706
	}
707
708
	/**
709
	 *  Return clicable name (with picto eventually)
710
	 *
711
	 *	@param	int		$withpicto		0=No picto, 1=Include picto into link, 2=Only picto
712
	 * 	@param	int		$maxlen			Longueur max libelle
713
	 *	@return	string					Chaine avec URL
714
	 */
715
	public function getNomUrl($withpicto = 0, $maxlen = 0)
716
	{
717
		global $langs;
718
719
		$result='';
720
721
		if (empty($this->ref)) $this->ref=$this->lib;
0 ignored issues
show
Bug Best Practice introduced by
The property lib does not exist on PaymentSocialContribution. Did you maybe forget to declare it?
Loading history...
722
        $label = $langs->trans("ShowPayment").': '.$this->ref;
723
724
        if (!empty($this->id)) {
725
            $link = '<a href="'.DOL_URL_ROOT.'/compta/payment_sc/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
726
            $linkend='</a>';
727
728
            if ($withpicto) $result.=($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
729
            if ($withpicto && $withpicto != 2) $result.=' ';
730
            if ($withpicto != 2) $result.=$link.($maxlen?dol_trunc($this->ref, $maxlen):$this->ref).$linkend;
731
        }
732
733
        return $result;
734
    }
735
}
736