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

Tva::addPayment()   F

Complexity

Conditions 24
Paths 2280

Size

Total Lines 141
Code Lines 88

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 24
eloc 88
c 0
b 0
f 0
nc 2280
nop 1
dl 0
loc 141
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-2003  Rodolphe Quiedeville    <[email protected]>
3
 * Copyright (C) 2004-2008  Laurent Destailleur     <[email protected]>
4
 * Copyright (C) 2011-2017  Alexandre Spangaro      <[email protected]>
5
 * Copyright (C) 2018       Philippe Grand          <[email protected]>
6
 * Copyright (C) 2018       Frédéric France         <[email protected]>
7
 *
8
 * This program is free software; you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by
10
 * the Free Software Foundation; either version 3 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
/**
23
 *      \file       htdocs/compta/tva/class/tva.class.php
24
 *      \ingroup    tax
25
 *      \author		Laurent Destailleur
26
 */
27
28
// Put here all includes required by your class file
29
require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php';
30
31
32
/**
33
 *  Put here description of your class
34
 */
35
class Tva extends CommonObject
36
{
37
	/**
38
	 * @var string ID to identify managed object
39
	 */
40
	public $element='tva';
41
42
	/**
43
	 * @var string Name of table without prefix where object is stored
44
	 */
45
	public $table_element='tva';
46
47
	/**
48
	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
49
	 */
50
	public $picto='payment';
51
52
	public $tms;
53
	public $datep;
54
	public $datev;
55
	public $amount;
56
	public $type_payment;
57
	public $num_payment;
58
59
	/**
60
     * @var string label
61
     */
62
    public $label;
63
64
    /**
65
     * @var int ID
66
     */
67
	public $fk_bank;
68
69
	/**
70
     * @var int ID
71
     */
72
	public $fk_user_creat;
73
74
	/**
75
     * @var int ID
76
     */
77
	public $fk_user_modif;
78
79
    /**
80
	 *	Constructor
81
	 *
82
	 *  @param		DoliDB		$db      Database handler
83
     */
84
    public function __construct($db)
85
    {
86
        $this->db = $db;
87
    }
88
89
90
    /**
91
     *  Create in database
92
     *
93
     *  @param      User	$user       User that create
94
     *  @return     int      			<0 if KO, >0 if OK
95
     */
96
    public function create($user)
97
    {
98
    	global $conf, $langs;
99
100
		$error=0;
101
		$now=dol_now();
102
103
		// Clean parameters
104
		$this->amount=trim($this->amount);
105
		$this->label=trim($this->label);
106
		$this->note=trim($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

106
		$this->note=trim(/** @scrutinizer ignore-deprecated */ $this->note);
Loading history...
107
		$this->fk_bank = (int) $this->fk_bank;
108
		$this->fk_user_creat = (int) $this->fk_user_creat;
109
		$this->fk_user_modif = (int) $this->fk_user_modif;
110
111
		// Check parameters
112
		// Put here code to add control on parameters values
113
114
		$this->db->begin();
115
116
		// Insert request
117
		$sql = "INSERT INTO ".MAIN_DB_PREFIX."tva(";
118
		$sql.= "datec,";
119
		$sql.= "datep,";
120
		$sql.= "datev,";
121
		$sql.= "amount,";
122
		$sql.= "label,";
123
		$sql.= "note,";
124
		$sql.= "fk_bank,";
125
		$sql.= "fk_user_creat,";
126
		$sql.= "fk_user_modif";
127
        $sql.= ") VALUES (";
128
		$sql.= " '".$this->db->idate($now)."',";
129
		$sql.= " '".$this->db->idate($this->datep)."',";
130
		$sql.= " '".$this->db->idate($this->datev)."',";
131
		$sql.= " '".$this->db->escape($this->amount)."',";
132
		$sql.= " '".$this->db->escape($this->label)."',";
133
		$sql.= " '".$this->db->escape($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

133
		$sql.= " '".$this->db->escape(/** @scrutinizer ignore-deprecated */ $this->note)."',";
Loading history...
134
		$sql.= " ".($this->fk_bank <= 0 ? "NULL" : "'".$this->db->escape($this->fk_bank)."'").",";
135
		$sql.= " '".$this->db->escape($this->fk_user_creat)."',";
136
		$sql.= " '".$this->db->escape($this->fk_user_modif)."'";
137
		$sql.= ")";
138
139
	   	dol_syslog(get_class($this)."::create", LOG_DEBUG);
140
        $resql=$this->db->query($sql);
141
        if ($resql)
142
        {
143
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva");
144
145
            // Call trigger
146
            $result=$this->call_trigger('TVA_CREATE', $user);
147
            if ($result < 0) $error++;
148
            // End call triggers
149
150
            if (! $error)
151
            {
152
            	$this->db->commit();
153
            	return $this->id;
154
            }
155
            else
156
			{
157
				$this->db->rollback();
158
				return -1;
159
            }
160
        }
161
        else
162
		{
163
			$this->error="Error ".$this->db->lasterror();
164
			$this->db->rollback();
165
			return -1;
166
        }
167
    }
168
169
    /**
170
     * Update database
171
     *
172
     * @param   User	$user        	User that modify
173
     * @param	int		$notrigger	    0=no, 1=yes (no update trigger)
174
     * @return  int         			<0 if KO, >0 if OK
175
     */
176
    public function update($user, $notrigger = 0)
177
    {
178
    	global $conf, $langs;
179
180
		$error=0;
181
182
		// Clean parameters
183
		$this->amount=trim($this->amount);
184
		$this->label=trim($this->label);
185
		$this->note=trim($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

185
		$this->note=trim(/** @scrutinizer ignore-deprecated */ $this->note);
Loading history...
186
		$this->fk_bank = (int) $this->fk_bank;
187
		$this->fk_user_creat = (int) $this->fk_user_creat;
188
		$this->fk_user_modif = (int) $this->fk_user_modif;
189
190
		// Check parameters
191
		// Put here code to add control on parameters values
192
193
		$this->db->begin();
194
195
		// Update request
196
        $sql = "UPDATE ".MAIN_DB_PREFIX."tva SET";
197
		$sql.= " tms='".$this->db->idate($this->tms)."',";
198
		$sql.= " datep='".$this->db->idate($this->datep)."',";
199
		$sql.= " datev='".$this->db->idate($this->datev)."',";
200
		$sql.= " amount=".price2num($this->amount).",";
201
		$sql.= " label='".$this->db->escape($this->label)."',";
202
		$sql.= " note='".$this->db->escape($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

202
		$sql.= " note='".$this->db->escape(/** @scrutinizer ignore-deprecated */ $this->note)."',";
Loading history...
203
		$sql.= " fk_bank=".$this->fk_bank.",";
204
		$sql.= " fk_user_creat=".$this->fk_user_creat.",";
205
		$sql.= " fk_user_modif=".($this->fk_user_modif>0?$this->fk_user_modif:$user->id)."";
206
        $sql.= " WHERE rowid=".$this->id;
207
208
        dol_syslog(get_class($this)."::update", LOG_DEBUG);
209
        $resql = $this->db->query($sql);
210
        if (! $resql)
211
        {
212
            $this->error="Error ".$this->db->lasterror();
213
            $error++;
214
        }
215
216
		if (! $error && ! $notrigger)
217
		{
218
            // Call trigger
219
            $result=$this->call_trigger('TVA_MODIFY', $user);
220
            if ($result < 0) $error++;
221
            // End call triggers
222
    	}
223
224
        if (! $error)
225
    	{
226
    		$this->db->commit();
227
    		return 1;
228
    	}
229
    	else
230
    	{
231
    		$this->db->rollback();
232
    		return -1;
233
    	}
234
    }
235
236
237
    /**
238
     *  Load object in memory from database
239
     *
240
     *  @param	int		$id         id object
241
     *  @param  User	$user       User that load
242
     *  @return int         		<0 if KO, >0 if OK
243
     */
244
    public function fetch($id, $user = null)
245
    {
246
    	global $langs;
247
        $sql = "SELECT";
248
		$sql.= " t.rowid,";
249
250
		$sql.= " t.tms,";
251
		$sql.= " t.datep,";
252
		$sql.= " t.datev,";
253
		$sql.= " t.amount,";
254
		$sql.= " t.fk_typepayment,";
255
		$sql.= " t.num_payment,";
256
		$sql.= " t.label,";
257
		$sql.= " t.note,";
258
		$sql.= " t.fk_bank,";
259
		$sql.= " t.fk_user_creat,";
260
		$sql.= " t.fk_user_modif,";
261
		$sql.= " b.fk_account,";
262
		$sql.= " b.fk_type,";
263
		$sql.= " b.rappro";
264
265
        $sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
266
		$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON t.fk_bank = b.rowid";
267
        $sql.= " WHERE t.rowid = ".$id;
268
269
    	dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
270
        $resql=$this->db->query($sql);
271
        if ($resql)
272
        {
273
            if ($this->db->num_rows($resql))
274
            {
275
                $obj = $this->db->fetch_object($resql);
276
277
                $this->id    = $obj->rowid;
278
                $this->ref   = $obj->rowid;
279
				$this->tms   = $this->db->jdate($obj->tms);
280
				$this->datep = $this->db->jdate($obj->datep);
281
				$this->datev = $this->db->jdate($obj->datev);
282
				$this->amount = $obj->amount;
283
				$this->type_payment = $obj->fk_typepayment;
284
				$this->num_payment = $obj->num_payment;
285
				$this->label = $obj->label;
286
				$this->note  = $obj->note;
287
				$this->fk_bank = $obj->fk_bank;
288
				$this->fk_user_creat = $obj->fk_user_creat;
289
				$this->fk_user_modif = $obj->fk_user_modif;
290
				$this->fk_account = $obj->fk_account;
291
				$this->fk_type = $obj->fk_type;
292
				$this->rappro  = $obj->rappro;
293
            }
294
            $this->db->free($resql);
295
296
            return 1;
297
        }
298
        else
299
        {
300
      	    $this->error="Error ".$this->db->lasterror();
301
            return -1;
302
        }
303
    }
304
305
306
 	/**
307
	 *  Delete object in database
308
	 *
309
     *	@param	User	$user       User that delete
310
	 *	@return	int					<0 if KO, >0 if OK
311
	 */
312
	public function delete($user)
313
	{
314
		global $conf, $langs;
315
316
		$error=0;
317
318
		// Call trigger
319
		$result=$this->call_trigger('TVA_DELETE', $user);
320
		if ($result < 0) return -1;
321
		// End call triggers
322
323
		$sql = "DELETE FROM ".MAIN_DB_PREFIX."tva";
324
		$sql.= " WHERE rowid=".$this->id;
325
326
	   	dol_syslog(get_class($this)."::delete", LOG_DEBUG);
327
		$resql = $this->db->query($sql);
328
		if (! $resql)
329
		{
330
			$this->error="Error ".$this->db->lasterror();
331
			return -1;
332
		}
333
334
335
		return 1;
336
	}
337
338
339
	/**
340
     *  Initialise an instance with random values.
341
     *  Used to build previews or test instances.
342
     *	id must be 0 if object instance is a specimen.
343
     *
344
     *  @return	void
345
	 */
346
	public function initAsSpecimen()
347
	{
348
		$this->id=0;
349
350
		$this->tms='';
351
		$this->datep='';
352
		$this->datev='';
353
		$this->amount='';
354
		$this->label='';
355
		$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

355
		/** @scrutinizer ignore-deprecated */ $this->note='';
Loading history...
356
		$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...
357
		$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...
358
		$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...
359
	}
360
361
362
    /**
363
     *  Balance of VAT
364
     *
365
     *	@param	int		$year		Year
366
     *	@return	double				Amount
367
     */
368
    public function solde($year = 0)
369
    {
370
371
        $reglee = $this->tva_sum_reglee($year);
372
373
        $payee = $this->tva_sum_payee($year);
374
        $collectee = $this->tva_sum_collectee($year);
375
376
        $solde = $reglee - ($collectee - $payee);
377
378
        return $solde;
379
    }
380
381
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
382
    /**
383
     * 	Total of the VAT from invoices emitted by the thirdparty.
384
     *
385
     *	@param	int		$year		Year
386
     *  @return	double				Amount
387
     */
388
    public function tva_sum_collectee($year = 0)
389
    {
390
        // phpcs:enable
391
392
        $sql = "SELECT sum(f.tva) as amount";
393
        $sql .= " FROM ".MAIN_DB_PREFIX."facture as f WHERE f.paye = 1";
394
        if ($year)
395
        {
396
            $sql .= " AND f.datef >= '".$year."-01-01' AND f.datef <= '".$year."-12-31' ";
397
        }
398
399
        $result = $this->db->query($sql);
400
        if ($result)
401
        {
402
            if ($this->db->num_rows($result))
403
            {
404
                $obj = $this->db->fetch_object($result);
405
				$ret = $obj->amount;
406
                $this->db->free($result);
407
                return $ret;
408
            }
409
            else
410
			{
411
                $this->db->free($result);
412
				return 0;
413
            }
414
        }
415
        else
416
        {
417
            print $this->db->lasterror();
418
            return -1;
419
        }
420
    }
421
422
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
423
    /**
424
     * 	VAT payed
425
     *
426
     *	@param	int		$year		Year
427
     *	@return	double				Amount
428
     */
429
    public function tva_sum_payee($year = 0)
430
    {
431
        // phpcs:enable
432
433
        $sql = "SELECT sum(f.total_tva) as total_tva";
434
        $sql .= " FROM ".MAIN_DB_PREFIX."facture_fourn as f";
435
        if ($year)
436
        {
437
            $sql .= " WHERE f.datef >= '".$year."-01-01' AND f.datef <= '".$year."-12-31' ";
438
        }
439
440
        $result = $this->db->query($sql);
441
        if ($result)
442
        {
443
            if ($this->db->num_rows($result))
444
            {
445
                $obj = $this->db->fetch_object($result);
446
                $ret = $obj->total_tva;
447
            	$this->db->free($result);
448
                return $ret;
449
            }
450
            else
451
			{
452
            	$this->db->free($result);
453
				return 0;
454
            }
455
        }
456
        else
457
        {
458
            print $this->db->lasterror();
459
            return -1;
460
        }
461
    }
462
463
464
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
465
    /**
466
     * 	Total of the VAT payed
467
     *
468
     *	@param	int		$year		Year
469
     *	@return	double				Amount
470
     */
471
    public function tva_sum_reglee($year = 0)
472
    {
473
        // phpcs:enable
474
475
        $sql = "SELECT sum(f.amount) as amount";
476
        $sql .= " FROM ".MAIN_DB_PREFIX."tva as f";
477
478
        if ($year)
479
        {
480
            $sql .= " WHERE f.datev >= '".$year."-01-01' AND f.datev <= '".$year."-12-31' ";
481
        }
482
483
        $result = $this->db->query($sql);
484
        if ($result)
485
        {
486
            if ($this->db->num_rows($result))
487
            {
488
                $obj = $this->db->fetch_object($result);
489
                $ret = $obj->amount;
490
            	$this->db->free($result);
491
                return $ret;
492
            }
493
            else
494
			{
495
            	$this->db->free($result);
496
				return 0;
497
            }
498
        }
499
        else
500
        {
501
            print $this->db->lasterror();
502
            return -1;
503
        }
504
    }
505
506
507
    /**
508
     *  Create in database
509
     *
510
	 *	@param	User	$user		Object user that insert
511
	 *	@return	int					<0 if KO, rowid in tva table if OK
512
     */
513
    public function addPayment($user)
514
    {
515
        global $conf,$langs;
516
517
        $this->db->begin();
518
519
        // Clean parameters
520
        $this->amount=price2num(trim($this->amount));
521
        $this->label=trim($this->label);
522
		$this->note=trim($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

522
		/** @scrutinizer ignore-deprecated */ $this->note=trim($this->note);
Loading history...
523
		$this->fk_bank = (int) $this->fk_bank;
524
		$this->fk_user_creat = (int) $this->fk_user_creat;
525
		$this->fk_user_modif = (int) $this->fk_user_modif;
526
		if (empty($this->datec)) $this->datec = dol_now();
527
528
        // Check parameters
529
		if (! $this->label)
530
		{
531
			$this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentities("Label"));
532
			return -3;
533
		}
534
        if ($this->amount == '')
535
        {
536
            $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentities("Amount"));
537
            return -4;
538
        }
539
        if (! empty($conf->banque->enabled) && (empty($this->accountid) || $this->accountid <= 0))
0 ignored issues
show
Bug Best Practice introduced by
The property accountid does not exist on Tva. Did you maybe forget to declare it?
Loading history...
540
        {
541
            $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentities("Account"));
542
            return -5;
543
        }
544
        if (! empty($conf->banque->enabled) && (empty($this->type_payment) || $this->type_payment <= 0))
545
        {
546
            $this->error=$langs->trans("ErrorFieldRequired", $langs->transnoentities("PaymentMode"));
547
            return -5;
548
        }
549
550
        // Insert into llx_tva
551
        $sql = "INSERT INTO ".MAIN_DB_PREFIX."tva (";
552
        $sql.= "datec";
553
        $sql.= ", datep";
554
        $sql.= ", datev";
555
		$sql.= ", amount";
556
		$sql.= ", fk_typepayment";
557
		$sql.= ", num_payment";
558
		if ($this->note)  $sql.= ", 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

558
		if (/** @scrutinizer ignore-deprecated */ $this->note)  $sql.= ", note";
Loading history...
559
        if ($this->label) $sql.= ", label";
560
        $sql.= ", fk_user_creat";
561
		$sql.= ", fk_bank";
562
		$sql.= ", entity";
563
		$sql.= ") ";
564
        $sql.= " VALUES (";
565
        $sql.= " '".$this->db->idate($this->datec)."'";
566
        $sql.= ", '".$this->db->idate($this->datep)."'";
567
        $sql.= ", '".$this->db->idate($this->datev)."'";
568
		$sql.= ", ".$this->amount;
569
        $sql.= ", '".$this->db->escape($this->type_payment)."'";
570
		$sql.= ", '".$this->db->escape($this->num_payment)."'";
571
		if ($this->note)  $sql.=", '".$this->db->escape($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

571
		if (/** @scrutinizer ignore-deprecated */ $this->note)  $sql.=", '".$this->db->escape($this->note)."'";
Loading history...
572
        if ($this->label) $sql.=", '".$this->db->escape($this->label)."'";
573
        $sql.= ", '".$this->db->escape($user->id)."'";
574
		$sql.= ", NULL";
575
		$sql.= ", ".$conf->entity;
576
        $sql.= ")";
577
578
		dol_syslog(get_class($this)."::addPayment", LOG_DEBUG);
579
        $result = $this->db->query($sql);
580
        if ($result)
581
        {
582
            $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."tva");    // TODO should be called 'payment_vat'
583
584
            // Call trigger
585
            //XXX: Should be done just befor commit no ?
586
            $result=$this->call_trigger('TVA_ADDPAYMENT', $user);
587
            if ($result < 0)
588
            {
589
            	$this->id = 0;
590
            	$ok = 0;
591
            }
592
            // End call triggers
593
594
            if ($this->id > 0)
595
            {
596
                $ok=1;
597
				if (! empty($conf->banque->enabled) && ! empty($this->amount))
598
                {
599
                    // Insert into llx_bank
600
                    require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php';
601
602
                    $acc = new Account($this->db);
603
					$result=$acc->fetch($this->accountid);
604
					if ($result <= 0) dol_print_error($this->db);
605
606
					if ($this->amount > 0) {
607
						$bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, -abs($this->amount), '', '', $user);
608
					} else {
609
						$bank_line_id = $acc->addline($this->datep, $this->type_payment, $this->label, abs($this->amount), '', '', $user);
610
					}
611
612
                    // Update fk_bank into llx_tva. So we know vat line used to generate bank transaction
613
                    if ($bank_line_id > 0)
614
					{
615
                        $this->update_fk_bank($bank_line_id);
616
                    }
617
					else
618
					{
619
						$this->error=$acc->error;
620
						$ok=0;
621
					}
622
623
                    // Update links
624
                    $result=$acc->add_url_line($bank_line_id, $this->id, DOL_URL_ROOT.'/compta/tva/card.php?id=', "(VATPayment)", "payment_vat");
625
                    if ($result < 0)
626
                    {
627
                    	$this->error=$acc->error;
628
                    	$ok=0;
629
                    }
630
	            }
631
632
				if ($ok)
633
				{
634
					$this->db->commit();
635
					return $this->id;
636
				}
637
				else
638
				{
639
					$this->db->rollback();
640
					return -3;
641
				}
642
            }
643
            else
644
            {
645
                $this->db->rollback();
646
                return -2;
647
            }
648
        }
649
        else
650
        {
651
            $this->error=$this->db->error();
652
            $this->db->rollback();
653
            return -1;
654
        }
655
    }
656
657
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
658
    /**
659
	 *  Update link between payment tva and line generate into llx_bank
660
     *
661
     *  @param	int		$id_bank    Id bank account
662
	 *	@return	int					<0 if KO, >0 if OK
663
     */
664
	public function update_fk_bank($id_bank)
665
	{
666
        // phpcs:enable
667
		$sql = 'UPDATE '.MAIN_DB_PREFIX.'tva SET fk_bank = '.(int) $id_bank;
668
		$sql.= ' WHERE rowid = '.(int) $this->id;
669
		$result = $this->db->query($sql);
670
		if ($result)
671
		{
672
			return 1;
673
		}
674
		else
675
		{
676
			dol_print_error($this->db);
677
			return -1;
678
		}
679
	}
680
681
	/**
682
	 *	Send name clicable (with possibly the picto)
683
	 *
684
	 *	@param	int		$withpicto		0=No picto, 1=Include picto into link, 2=Only picto
685
	 *	@param	string	$option			link option
686
     *  @param	int  	$notooltip		1=Disable tooltip
687
     *  @param	string	$morecss			More CSS
688
	 *	@return	string					Chaine with URL
689
	 */
690
	public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '')
691
	{
692
		global $langs, $conf;
693
694
		if (! empty($conf->dol_no_mouse_hover)) $notooltip=1;   // Force disable tooltips
695
696
		$result='';
697
698
		$label = '<u>' . $langs->trans("ShowVatPayment") . '</u>';
699
		$label.= '<br>';
700
		$label.= '<b>' . $langs->trans('Ref') . ':</b> ' . $this->ref;
701
702
        $url = DOL_URL_ROOT.'/compta/tva/card.php?id='.$this->id;
703
704
        $linkclose='';
705
        if (empty($notooltip))
706
        {
707
        	if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
708
        	{
709
        		$label=$langs->trans("ShowMyObject");
710
        		$linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
711
        	}
712
        	$linkclose.=' title="'.dol_escape_htmltag($label, 1).'"';
713
        	$linkclose.=' class="classfortooltip'.($morecss?' '.$morecss:'').'"';
714
        }
715
        else $linkclose = ($morecss?' class="'.$morecss.'"':'');
716
717
        $linkstart = '<a href="'.$url.'"';
718
        $linkstart.=$linkclose.'>';
719
        $linkend ='</a>';
720
721
		$picto='payment';
722
723
		$result .= $linkstart;
724
		if ($withpicto) $result.=img_object(($notooltip?'':$label), ($this->picto?$this->picto:'generic'), ($notooltip?(($withpicto != 2) ? 'class="paddingright"' : ''):'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip?0:1);
725
		if ($withpicto != 2) $result.= $this->ref;
726
		$result .= $linkend;
727
728
		return $result;
729
	}
730
731
	/**
732
     * 	Return amount of payments already done
733
     *
734
     *	@return		int		Amount of payment already done, <0 if KO
735
     */
736
    public function getSommePaiement()
737
    {
738
        $table='paiementcharge';
739
        $field='fk_charge';
740
741
        $sql = 'SELECT sum(amount) as amount';
742
        $sql.= ' FROM '.MAIN_DB_PREFIX.$table;
743
        $sql.= ' WHERE '.$field.' = '.$this->id;
744
745
        dol_syslog(get_class($this)."::getSommePaiement", LOG_DEBUG);
746
        $resql=$this->db->query($sql);
747
        if ($resql)
748
        {
749
            $amount=0;
750
751
            $obj = $this->db->fetch_object($resql);
752
            if ($obj) $amount=$obj->amount?$obj->amount:0;
753
754
            $this->db->free($resql);
755
            return $amount;
756
        }
757
        else
758
        {
759
            return -1;
760
        }
761
    }
762
763
	/**
764
	 *	Informations of vat payment object
765
	 *
766
	 *	@param	int		$id     Id of vat payment
767
	 *	@return	int				<0 if KO, >0 if OK
768
	 */
769
	public function info($id)
770
	{
771
		$sql = "SELECT t.rowid, t.tms, t.fk_user_modif, t.datec, t.fk_user_creat";
772
		$sql.= " FROM ".MAIN_DB_PREFIX."tva as t";
773
		$sql.= " WHERE t.rowid = ".(int) $id;
774
775
		dol_syslog(get_class($this)."::info", LOG_DEBUG);
776
		$result=$this->db->query($sql);
777
		if ($result)
778
		{
779
			if ($this->db->num_rows($result))
780
			{
781
				$obj = $this->db->fetch_object($result);
782
783
				$this->id = $obj->rowid;
784
785
				if ($obj->fk_user_creat) {
786
					$cuser = new User($this->db);
787
					$cuser->fetch($obj->fk_user_creat);
788
					$this->user_creation = $cuser;
789
				}
790
791
				if ($obj->fk_user_modif) {
792
					$muser = new User($this->db);
793
					$muser->fetch($obj->fk_user_modif);
794
					$this->user_modification = $muser;
795
				}
796
797
				$this->date_creation     = $this->db->jdate($obj->datec);
798
				$this->date_modification = $this->db->jdate($obj->tms);
799
				$this->import_key        = $obj->import_key;
800
			}
801
802
			$this->db->free($result);
803
		}
804
		else
805
		{
806
			dol_print_error($this->db);
807
		}
808
	}
809
810
	/**
811
	 * Retourne le libelle du statut d'une facture (brouillon, validee, abandonnee, payee)
812
	 *
813
	 * @param	int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
814
	 * @return  string				Libelle
815
	 */
816
	public function getLibStatut($mode = 0)
817
	{
818
	    return $this->LibStatut($this->statut, $mode);
819
	}
820
821
    // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
822
	/**
823
	 * Renvoi le libelle d'un statut donne
824
	 *
825
	 * @param   int		$status     Statut
826
	 * @param   int		$mode       0=libelle long, 1=libelle court, 2=Picto + Libelle court, 3=Picto, 4=Picto + Libelle long, 5=Libelle court + Picto
827
	 * @return	string  		    Libelle du statut
828
	 */
829
    public function LibStatut($status, $mode = 0)
830
    {
831
        // phpcs:enable
832
        global $langs;	// TODO Renvoyer le libelle anglais et faire traduction a affichage
833
834
        return '';
835
    }
836
}
837