Passed
Push — master ( fa5a21...043752 )
by Alxarafe
27:01
created

expensereport/class/paymentexpensereport.class.php (10 issues)

1
<?php
2
/* Copyright (C) 2015-2017  Alexandre Spangaro  <[email protected]>
3
 * Copyright (C) 2018       Nicolas ZABOURI  <[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/expensereport/class/paymentexpensereport.class.php
21
 *  \ingroup    Expense Report
22
 *  \brief      File of class to manage payment of expense report
23
 */
24
25
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
26
27
28
/**     \class      PaymentExpenseReport
29
 *		\brief      Class to manage payments of expense report
30
 */
31
class PaymentExpenseReport extends CommonObject
32
{
33
	/**
34
	 * @var string ID to identify managed object
35
	 */
36
	public $element='payment_expensereport';
37
38
	/**
39
	 * @var string Name of table without prefix where object is stored
40
	 */
41
	public $table_element='payment_expensereport';
42
43
    /**
44
	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
45
	 */
46
	public $picto = 'payment';
47
48
	/**
49
	 * @var int ID
50
	 */
51
	public $rowid;
52
53
	/**
54
     * @var int ID
55
     */
56
	public $fk_expensereport;
57
58
	public $datec='';
59
	public $tms='';
60
	public $datep='';
61
    public $amount;            // Total amount of payment
62
    public $amounts=array();   // Array of amounts
63
64
    /**
65
     * @var int ID
66
     */
67
	public $fk_typepayment;
68
69
	public $num_payment;
70
71
	/**
72
     * @var int ID
73
     */
74
	public $fk_bank;
75
76
	/**
77
     * @var int ID
78
     */
79
	public $fk_user_creat;
80
81
	/**
82
     * @var int ID
83
     */
84
	public $fk_user_modif;
85
86
    //Unknow field
87
    public $chid;
88
    public $total;
89
90
	/**
91
	 *	Constructor
92
	 *
93
	 *  @param		DoliDB		$db      Database handler
94
	 */
95
	function __construct($db)
96
	{
97
		$this->db = $db;
98
	}
99
100
	/**
101
	 *  Create payment of expense report into database.
102
     *  Use this->amounts to have list of lines for the payment
103
     *
104
	 *  @param      User		$user   User making payment
105
	 *  @return     int     			<0 if KO, id of payment if OK
106
	 */
107
	function create($user)
108
	{
109
		global $conf, $langs;
110
111
		$error=0;
112
113
        $now=dol_now();
114
115
        // Validate parameters
116
		if (! $this->datepaid)
117
		{
118
			$this->error='ErrorBadValueForParameterCreatePaymentExpenseReport';
119
			return -1;
120
		}
121
122
		// Clean parameters
123
		if (isset($this->fk_expensereport)) $this->fk_expensereport=trim($this->fk_expensereport);
0 ignored issues
show
Documentation Bug introduced by
The property $fk_expensereport was declared of type integer, but trim($this->fk_expensereport) 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...
124
		if (isset($this->amount))			$this->amount=trim($this->amount);
125
		if (isset($this->fk_typepayment))	$this->fk_typepayment=trim($this->fk_typepayment);
0 ignored issues
show
Documentation Bug introduced by
The property $fk_typepayment was declared of type integer, but trim($this->fk_typepayment) 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...
126
		if (isset($this->num_payment))		$this->num_payment=trim($this->num_payment);
127
		if (isset($this->note))				$this->note=trim($this->note);
128
		if (isset($this->fk_bank))			$this->fk_bank=trim($this->fk_bank);
0 ignored issues
show
Documentation Bug introduced by
The property $fk_bank was declared of type integer, but trim($this->fk_bank) 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...
129
		if (isset($this->fk_user_creat))	$this->fk_user_creat=trim($this->fk_user_creat);
0 ignored issues
show
Documentation Bug introduced by
The property $fk_user_creat was declared of type integer, but trim($this->fk_user_creat) 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...
130
		if (isset($this->fk_user_modif))	$this->fk_user_modif=trim($this->fk_user_modif);
131
		if (! empty($this->fk_expensereport)) $this->chid = $this->fk_expensereport;
132
133
        $totalamount = 0;
134
        foreach ($this->amounts as $key => $value)  // How payment is dispatch
135
        {
136
            $newvalue = price2num($value,'MT');
137
            $this->amounts[$key] = $newvalue;
138
            $totalamount += $newvalue;
139
        }
140
        $totalamount = price2num($totalamount);
141
142
        // Check parameters
143
        if ($totalamount == 0) return -1; // On accepte les montants negatifs pour les rejets de prelevement mais pas null
144
145
146
		$this->db->begin();
147
148
		if ($totalamount != 0)
149
		{
150
			$sql = "INSERT INTO ".MAIN_DB_PREFIX."payment_expensereport (fk_expensereport, datec, datep, amount,";
151
			$sql.= " fk_typepayment, num_payment, note, fk_user_creat, fk_bank)";
152
			$sql.= " VALUES ($this->chid, '".$this->db->idate($now)."',";
153
			$sql.= " '".$this->db->idate($this->datepaid)."',";
154
			$sql.= " ".$totalamount.",";
155
			$sql.= " ".$this->fk_typepayment.", '".$this->db->escape($this->num_payment)."', '".$this->db->escape($this->note)."', ".$user->id.",";
156
			$sql.= " 0)";
157
158
			dol_syslog(get_class($this)."::create", LOG_DEBUG);
159
			$resql=$this->db->query($sql);
160
			if ($resql)
161
			{
162
				$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."payment_expensereport");
163
			}
164
			else
165
			{
166
				$error++;
167
			}
168
		}
169
170
		if ($totalamount != 0 && ! $error)
171
		{
172
		    $this->amount=$totalamount;
173
		    $this->db->commit();
174
			return $this->id;
175
		}
176
		else
177
		{
178
			$this->error=$this->db->error();
179
			$this->db->rollback();
180
			return -1;
181
		}
182
	}
183
184
	/**
185
	 *  Load object in memory from database
186
	 *
187
	 *  @param	int		$id         Id object
188
	 *  @return int         		<0 if KO, >0 if OK
189
	 */
190
	function fetch($id)
191
	{
192
		global $langs;
193
		$sql = "SELECT";
194
		$sql.= " t.rowid,";
195
		$sql.= " t.fk_expensereport,";
196
		$sql.= " t.datec,";
197
		$sql.= " t.tms,";
198
		$sql.= " t.datep,";
199
		$sql.= " t.amount,";
200
		$sql.= " t.fk_typepayment,";
201
		$sql.= " t.num_payment,";
202
		$sql.= " t.note,";
203
		$sql.= " t.fk_bank,";
204
		$sql.= " t.fk_user_creat,";
205
		$sql.= " t.fk_user_modif,";
206
		$sql.= " pt.code as type_code, pt.libelle as type_libelle,";
207
		$sql.= ' b.fk_account';
208
		$sql.= " FROM ".MAIN_DB_PREFIX."payment_expensereport as t";
209
		$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as pt ON t.fk_typepayment = pt.id";
210
		$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'bank as b ON t.fk_bank = b.rowid';
211
		$sql.= " WHERE t.rowid = ".$id;
212
213
		dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
214
		$resql=$this->db->query($sql);
215
		if ($resql)
216
		{
217
			if ($this->db->num_rows($resql))
218
			{
219
				$obj = $this->db->fetch_object($resql);
220
221
				$this->id    = $obj->rowid;
222
				$this->ref   = $obj->rowid;
223
224
				$this->fk_expensereport	= $obj->fk_expensereport;
225
				$this->datec			= $this->db->jdate($obj->datec);
226
				$this->tms				= $this->db->jdate($obj->tms);
227
				$this->datep			= $this->db->jdate($obj->datep);
228
				$this->amount			= $obj->amount;
229
				$this->fk_typepayment  	= $obj->fk_typepayment;
230
				$this->num_payment		= $obj->num_payment;
231
				$this->note				= $obj->note;
232
				$this->fk_bank			= $obj->fk_bank;
233
				$this->fk_user_creat	= $obj->fk_user_creat;
234
				$this->fk_user_modif	= $obj->fk_user_modif;
235
236
				$this->type_code		= $obj->type_code;
237
				$this->type_libelle		= $obj->type_libelle;
238
239
				$this->bank_account		= $obj->fk_account;
240
				$this->bank_line		= $obj->fk_bank;
241
			}
242
			$this->db->free($resql);
243
244
			return 1;
245
		}
246
		else
247
		{
248
			$this->error="Error ".$this->db->lasterror();
249
			return -1;
250
		}
251
	}
252
253
254
	/**
255
	 *  Update database
256
	 *
257
	 *  @param	User	$user        	User that modify
258
	 *  @param  int		$notrigger	    0=launch triggers after, 1=disable triggers
259
	 *  @return int         			<0 if KO, >0 if OK
260
	 */
261
	function update($user=null, $notrigger=0)
262
	{
263
		global $conf, $langs;
264
		$error=0;
265
266
		// Clean parameters
267
268
		if (isset($this->fk_expensereport))	$this->fk_expensereport=trim($this->fk_expensereport);
0 ignored issues
show
Documentation Bug introduced by
The property $fk_expensereport was declared of type integer, but trim($this->fk_expensereport) 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...
269
		if (isset($this->amount))			$this->amount=trim($this->amount);
270
		if (isset($this->fk_typepayment))	$this->fk_typepayment=trim($this->fk_typepayment);
0 ignored issues
show
Documentation Bug introduced by
The property $fk_typepayment was declared of type integer, but trim($this->fk_typepayment) 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...
271
		if (isset($this->num_payment))		$this->num_payment=trim($this->num_payment);
272
		if (isset($this->note))				$this->note=trim($this->note);
273
		if (isset($this->fk_bank))			$this->fk_bank=trim($this->fk_bank);
0 ignored issues
show
Documentation Bug introduced by
The property $fk_bank was declared of type integer, but trim($this->fk_bank) 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...
274
		if (isset($this->fk_user_creat))	$this->fk_user_creat=trim($this->fk_user_creat);
0 ignored issues
show
Documentation Bug introduced by
The property $fk_user_creat was declared of type integer, but trim($this->fk_user_creat) 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...
275
		if (isset($this->fk_user_modif))	$this->fk_user_modif=trim($this->fk_user_modif);
276
277
278
		// Check parameters
279
		// Put here code to add control on parameters values
280
281
		// Update request
282
		$sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET";
283
284
		$sql.= " fk_expensereport=".(isset($this->fk_expensereport)?$this->fk_expensereport:"null").",";
285
		$sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
286
		$sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
287
		$sql.= " datep=".(dol_strlen($this->datep)!=0 ? "'".$this->db->idate($this->datep)."'" : 'null').",";
288
		$sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
289
		$sql.= " fk_typepayment=".(isset($this->fk_typepayment)?$this->fk_typepayment:"null").",";
290
		$sql.= " num_payment=".(isset($this->num_payment)?"'".$this->db->escape($this->num_payment)."'":"null").",";
291
		$sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
292
		$sql.= " fk_bank=".(isset($this->fk_bank)?$this->fk_bank:"null").",";
293
		$sql.= " fk_user_creat=".(isset($this->fk_user_creat)?$this->fk_user_creat:"null").",";
294
		$sql.= " fk_user_modif=".(isset($this->fk_user_modif)?$this->fk_user_modif:"null")."";
295
296
297
		$sql.= " WHERE rowid=".$this->id;
298
299
		$this->db->begin();
300
301
		dol_syslog(get_class($this)."::update", LOG_DEBUG);
302
		$resql = $this->db->query($sql);
303
		if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
304
305
		if (! $error)
306
		{
307
			if (! $notrigger)
308
			{
309
				// Uncomment this and change MYOBJECT to your own tag if you
310
				// want this action call a trigger.
311
312
				//// Call triggers
313
				//include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
314
				//$interface=new Interfaces($this->db);
315
				//$result=$interface->run_triggers('MYOBJECT_MODIFY',$this,$user,$langs,$conf);
316
				//if ($result < 0) { $error++; $this->errors=$interface->errors; }
317
				//// End call triggers
318
			}
319
		}
320
321
		// Commit or rollback
322
		if ($error)
323
		{
324
			foreach($this->errors as $errmsg)
325
			{
326
				dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
327
				$this->error.=($this->error?', '.$errmsg:$errmsg);
328
			}
329
			$this->db->rollback();
330
			return -1*$error;
331
		}
332
		else
333
		{
334
			$this->db->commit();
335
			return 1;
336
		}
337
	}
338
339
340
	/**
341
	 *  Delete object in database
342
	 *
343
	 *  @param	User	$user        	User that delete
344
	 *  @param  int		$notrigger		0=launch triggers after, 1=disable triggers
345
	 *  @return int						<0 if KO, >0 if OK
346
	 */
347
	function delete($user, $notrigger=0)
348
	{
349
		global $conf, $langs;
350
		$error=0;
351
352
		$this->db->begin();
353
354
	    if (! $error)
355
        {
356
            $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url";
357
            $sql.= " WHERE type='payment_expensereport' AND url_id=".$this->id;
358
359
            dol_syslog(get_class($this)."::delete", LOG_DEBUG);
360
            $resql = $this->db->query($sql);
361
            if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
362
        }
363
364
		if (! $error)
365
		{
366
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."payment_expensereport";
367
			$sql.= " WHERE rowid=".$this->id;
368
369
			dol_syslog(get_class($this)."::delete", LOG_DEBUG);
370
			$resql = $this->db->query($sql);
371
			if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
372
		}
373
374
		if (! $error)
375
		{
376
			if (! $notrigger)
377
			{
378
				// Uncomment this and change MYOBJECT to your own tag if you
379
				// want this action call a trigger.
380
381
				//// Call triggers
382
				//include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
383
				//$interface=new Interfaces($this->db);
384
				//$result=$interface->run_triggers('MYOBJECT_DELETE',$this,$user,$langs,$conf);
385
				//if ($result < 0) { $error++; $this->errors=$interface->errors; }
386
				//// End call triggers
387
			}
388
		}
389
390
		// Commit or rollback
391
		if ($error)
392
		{
393
			foreach($this->errors as $errmsg)
394
			{
395
				dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
396
				$this->error.=($this->error?', '.$errmsg:$errmsg);
397
			}
398
			$this->db->rollback();
399
			return -1*$error;
400
		}
401
		else
402
		{
403
			$this->db->commit();
404
			return 1;
405
		}
406
	}
407
408
409
410
	/**
411
	 *	Load an object from its id and create a new one in database
412
	 *
413
	 *	@param	int		$fromid     	Id of object to clone
414
	 * 	@return	int						New id of clone
415
	 */
416
	function createFromClone($fromid)
417
	{
418
		global $user,$langs;
419
420
		$error=0;
421
422
		$object=new PaymentExpenseReport($this->db);
423
424
		$object->context['createfromclone'] = 'createfromclone';
425
426
		$this->db->begin();
427
428
		// Load source object
429
		$object->fetch($fromid);
430
		$object->id=0;
431
		$object->statut=0;
432
433
		// Clear fields
434
		// ...
435
436
		// Create clone
437
		$result=$object->create($user);
438
439
		// Other options
440
		if ($result < 0)
441
		{
442
			$this->error=$object->error;
443
			$error++;
444
		}
445
446
		if (! $error)
447
		{
448
449
450
451
		}
452
453
		unset($this->context['createfromclone']);
454
455
		// End
456
		if (! $error)
457
		{
458
			$this->db->commit();
459
			return $object->id;
460
		}
461
		else
462
		{
463
			$this->db->rollback();
464
			return -1;
465
		}
466
	}
467
468
469
	/**
470
	 * 	Retourne le libelle du statut d'un don (brouillon, validee, abandonnee, payee)
471
	 *
472
	 *  @param	int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long
473
	 *  @return string        		Libelle
474
	 */
475
	function getLibStatut($mode=0)
476
	{
477
	    return '';
478
	}
479
480
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
481
	/**
482
	 *  Renvoi le libelle d'un statut donne
483
	 *
484
	 *  @param  int		$statut        	Id statut
485
	 *  @param  int		$mode          	0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
486
	 *  @return string 			       	Libelle du statut
487
	 */
488
	function LibStatut($statut,$mode=0)
489
	{
490
        // phpcs:enable
491
	    global $langs;
492
493
	    return '';
494
	}
495
496
497
	/**
498
     *  Initialise an instance with random values.
499
     *  Used to build previews or test instances.
500
     *	id must be 0 if object instance is a specimen.
501
     *
502
     *  @return	void
503
	 */
504
	function initAsSpecimen()
505
	{
506
		$this->id=0;
507
508
		$this->fk_expensereport='';
509
		$this->datec='';
510
		$this->tms='';
511
		$this->datep='';
512
		$this->amount='';
513
		$this->fk_typepayment='';
514
		$this->num_payment='';
515
		$this->note='';
516
		$this->fk_bank='';
517
		$this->fk_user_creat='';
518
		$this->fk_user_modif='';
519
	}
520
521
522
    /**
523
     *      Add record into bank for payment with links between this bank record and invoices of payment.
524
     *      All payment properties must have been set first like after a call to create().
525
     *
526
     *      @param	User	$user               Object of user making payment
527
     *      @param  string	$mode               'payment_expensereport'
528
     *      @param  string	$label              Label to use in bank record
529
     *      @param  int		$accountid          Id of bank account to do link with
530
     *      @param  string	$emetteur_nom       Name of transmitter
531
     *      @param  string	$emetteur_banque    Name of bank
532
     *      @return int                 		<0 if KO, >0 if OK
533
     */
534
    function addPaymentToBank($user,$mode,$label,$accountid,$emetteur_nom,$emetteur_banque)
535
    {
536
        global $langs,$conf;
537
538
        $error=0;
539
540
        if (! empty($conf->banque->enabled))
541
        {
542
            include_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
543
544
            $acc = new Account($this->db);
545
            $acc->fetch($accountid);
546
547
            //Fix me field
548
            $this->total = $this->amount;
549
            $total = $this->total;
550
551
            if ($mode == 'payment_expensereport') $amount=$total;
552
553
            // Insert payment into llx_bank
554
            $bank_line_id = $acc->addline(
555
                $this->datepaid,
0 ignored issues
show
The property datepaid does not exist on PaymentExpenseReport. Did you mean datep?
Loading history...
556
                $this->fk_typepayment,  // Payment mode id or code ("CHQ or VIR for example")
557
                $label,
558
                -$amount,
559
                $this->num_payment,
560
                '',
561
                $user,
562
                $emetteur_nom,
563
                $emetteur_banque
564
            );
565
566
            // Update fk_bank in llx_paiement.
567
            // On connait ainsi le paiement qui a genere l'ecriture bancaire
568
            if ($bank_line_id > 0)
569
            {
570
                $result=$this->update_fk_bank($bank_line_id);
571
                if ($result <= 0)
572
                {
573
                    $error++;
574
                    dol_print_error($this->db);
575
                }
576
577
                // Add link 'payment', 'payment_supplier', 'payment_expensereport' in bank_url between payment and bank transaction
578
                $url='';
579
                if ($mode == 'payment_expensereport') $url=DOL_URL_ROOT.'/expensereport/payment/card.php?rowid=';
580
                if ($url)
581
                {
582
                    $result=$acc->add_url_line($bank_line_id, $this->id, $url, '(paiement)', $mode);
583
                    if ($result <= 0)
584
                    {
585
                        $error++;
586
                        dol_print_error($this->db);
587
                    }
588
                }
589
590
                // Add link 'user' in bank_url between user and bank transaction
591
                if (! $error)
592
                {
593
                    foreach ($this->amounts as $key => $value)  // We should have always same user but we loop in case of.
594
                    {
595
                    	if ($mode == 'payment_expensereport')
596
                        {
597
                        	$fuser = new User($this->db);
598
                            $fuser->fetch($key);
599
600
                            $result=$acc->add_url_line(
601
                                $bank_line_id,
602
                                $fuser->id,
603
                                DOL_URL_ROOT.'/user/card.php?id=',
604
                                $fuser->getFullName($langs),
605
                                'user'
606
                            );
607
                            if ($result <= 0)
608
                            {
609
                            	$this->error=$this->db->lasterror();
610
                            	dol_syslog(get_class($this).'::addPaymentToBank '.$this->error);
611
                            	$error++;
612
                            }
613
                        }
614
                    }
615
                }
616
            }
617
            else
618
            {
619
                $this->error=$acc->error;
620
                $error++;
621
            }
622
        }
623
624
        if (! $error)
625
        {
626
            return 1;
627
        }
628
        else
629
        {
630
            return -1;
631
        }
632
    }
633
634
635
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps
636
	/**
637
	 *  Update link between the expense report payment and the generated line in llx_bank
638
	 *
639
	 *  @param	int		$id_bank         Id if bank
640
	 *  @return	int			             >0 if OK, <=0 if KO
641
	 */
642
	function update_fk_bank($id_bank)
643
	{
644
        // phpcs:enable
645
		$sql = "UPDATE ".MAIN_DB_PREFIX."payment_expensereport SET fk_bank = ".$id_bank." WHERE rowid = ".$this->id;
646
647
		dol_syslog(get_class($this)."::update_fk_bank", LOG_DEBUG);
648
		$result = $this->db->query($sql);
649
		if ($result)
650
		{
651
			return 1;
652
		}
653
		else
654
		{
655
			$this->error=$this->db->error();
656
			return 0;
657
		}
658
	}
659
660
	/**
661
	 *  Return clicable name (with picto eventually)
662
	 *
663
	 *	@param	int		$withpicto		0=No picto, 1=Include picto into link, 2=Only picto
664
	 * 	@param	int		$maxlen			Longueur max libelle
665
	 *	@return	string					Chaine avec URL
666
	 */
667
	function getNomUrl($withpicto=0,$maxlen=0)
668
	{
669
		global $langs;
670
671
		$result='';
672
673
		if (empty($this->ref)) $this->ref=$this->lib;
0 ignored issues
show
Bug Best Practice introduced by
The property lib does not exist on PaymentExpenseReport. Did you maybe forget to declare it?
Loading history...
674
        $label = $langs->trans("ShowPayment").': '.$this->ref;
675
676
		if (!empty($this->id))
677
		{
678
			$link = '<a href="'.DOL_URL_ROOT.'/expensereport/payment/card.php?id='.$this->id.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
679
			$linkend='</a>';
680
681
            if ($withpicto) $result.=($link.img_object($label, 'payment', 'class="classfortooltip"').$linkend.' ');
682
			if ($withpicto && $withpicto != 2) $result.=' ';
683
			if ($withpicto != 2) $result.=$link.($maxlen?dol_trunc($this->ref,$maxlen):$this->ref).$linkend;
684
		}
685
686
		return $result;
687
	}
688
689
	/**
690
	 *    Tab information on object
691
	 *
692
	 *    @param   int     $id      Payment id
693
	 *    @return  void
694
	 */
695
	function info($id)
696
	{
697
		$sql = 'SELECT e.rowid, e.datec, e.fk_user_creat, e.fk_user_modif, e.tms';
698
		$sql.= ' FROM '.MAIN_DB_PREFIX.'payment_expensereport as e';
699
		$sql.= ' WHERE e.rowid = '.$id;
700
701
		dol_syslog(get_class($this).'::info', LOG_DEBUG);
702
		$result = $this->db->query($sql);
703
704
		if ($result)
705
		{
706
			if ($this->db->num_rows($result))
707
			{
708
				$obj = $this->db->fetch_object($result);
709
				$this->id = $obj->rowid;
710
				if ($obj->fk_user_creat)
711
				{
712
					$cuser = new User($this->db);
713
					$cuser->fetch($obj->fk_user_creat);
714
					$this->user_creation     = $cuser;
715
				}
716
				if ($obj->fk_user_modif)
717
				{
718
					$muser = new User($this->db);
719
					$muser->fetch($obj->fk_user_modif);
720
					$this->user_modification = $muser;
721
				}
722
				$this->date_creation     = $this->db->jdate($obj->datec);
723
				$this->date_modification = $this->db->jdate($obj->tms);
724
			}
725
			$this->db->free($result);
726
		}
727
		else
728
		{
729
			dol_print_error($this->db);
730
		}
731
	}
732
}
733