Passed
Branch develop (7fd3bb)
by Laurent
38:31
created

FichinterRec::fetch_lines()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 66
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 54
nc 3
nop 1
dl 0
loc 66
rs 9.0036
c 0
b 0
f 0

How to fix   Long Method   

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) 2003-2005  Rodolphe Quiedeville	<[email protected]>
3
 * Copyright (C) 2004-2015  Laurent Destailleur		<[email protected]>
4
 * Copyright (C) 2009-2012  Regis Houssin			<[email protected]>
5
 * Copyright (C) 2010-2011  Juanjo Menent			<[email protected]>
6
 * Copyright (C) 2012       Cedric Salvador			<[email protected]>
7
 * Copyright (C) 2013       Florian Henry			<[email protected]>
8
 * Copyright (C) 2015       Marcos García			<[email protected]>
9
 * Copyright (C) 2016-2018  Charlie Benke			<[email protected]>
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
/**
26
 *  \file       htdocs/fichinter/class/fichinterrec.class.php
27
 *  \ingroup    facture
28
 *  \brief      Fichier de la classe des factures recurentes
29
 */
30
31
require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
32
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
33
require_once DOL_DOCUMENT_ROOT.'/fichinter/class/fichinter.class.php';
34
35
36
/**
37
 *	Classe de gestion des factures recurrentes/Modeles
38
 */
39
class FichinterRec extends Fichinter
40
{
41
	public $element = 'fichinterrec';
42
	public $table_element = 'fichinter_rec';
43
	public $table_element_line = 'fichinterdet_rec';
44
45
	/**
46
	 * @var string Fieldname with ID of parent key if this field has a parent
47
	 */
48
	public $fk_element = 'fk_fichinter';
49
50
	/**
51
	 * {@inheritdoc}
52
	 */
53
	protected $table_ref_field = 'titre';
54
55
	/**
56
	 * @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png
57
	 */
58
	public $picto = 'intervention';
59
60
	/**
61
	 * @var string title
62
	 */
63
	public $title;
64
	public $number;
65
	public $date;
66
	public $amount;
67
	public $remise;
68
	public $tva;
69
	public $total;
70
71
	/**
72
	 * @var int Proposal Id
73
	 */
74
	public $propalid;
75
76
	public $date_last_gen;
77
	public $date_when;
78
79
	/**
80
	 * @var int number of generation done
81
	 */
82
	public $nb_gen_done;
83
84
	/**
85
	 * @var int number of maximum generation
86
	 */
87
	public $nb_gen_max;
88
89
	/**
90
	 * int rank
91
	 */
92
	public $rang;
93
	public $special_code;
94
95
	public $usenewprice = 0;
96
97
	/**
98
	 *	Constructor
99
	 *
100
	 * 	@param		DoliDB		$db		Database handler
101
	 */
102
	public function __construct($db)
103
	{
104
		$this->db = $db;
105
106
		//status dans l'ordre de l'intervention
107
		$this->statuts[0] = 'Draft';
108
		$this->statuts[1] = 'Closed';
109
110
		$this->statuts_short[0] = 'Draft';
111
		$this->statuts_short[1] = 'Closed';
112
113
		$this->statuts_logo[0] = 'statut0';
114
		$this->statuts_logo[1] = 'statut1';
115
	}
116
117
	/**
118
	 *	Returns the label status
119
	 *
120
	 *	@param      int		$mode       0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto
121
	 *	@return     string              Label
122
	 */
123
	public function getLibStatut($mode = 0)
124
	{
125
		return $this->LibStatut($this->statut, $mode);
126
	}
127
128
129
	/**
130
	 *  Create a predefined fichinter
131
	 *
132
	 *  @param      User    $user       User object
133
	 *  @param      int     $notrigger  no trigger
134
	 *  @return     int                 <0 if KO, id of fichinter if OK
135
	 */
136
	public function create($user, $notrigger = 0)
137
	{
138
		global $conf;
139
140
		$error = 0;
141
		$now = dol_now();
142
143
		// Clean parameters
144
		$this->title = trim($this->title);
145
		$this->description = trim($this->description);
146
147
148
		$this->db->begin();
149
150
		// Load fichinter model
151
		$fichintsrc = new Fichinter($this->db);
152
153
		$result = $fichintsrc->fetch($this->id_origin);
0 ignored issues
show
Bug introduced by
The property id_origin does not exist on FichinterRec. Did you mean origin?
Loading history...
154
		$result = $fichintsrc->fetch_lines(1); // to get all lines
155
156
157
		if ($result > 0) {
158
			// On positionne en mode brouillon la facture
159
			$this->brouillon = 1;
160
161
			$sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinter_rec (";
162
			$sql .= "titre";
163
			$sql .= ", fk_soc";
164
			$sql .= ", entity";
165
			$sql .= ", datec";
166
			$sql .= ", duree";
167
			$sql .= ", description";
168
			$sql .= ", note_private";
169
			$sql .= ", note_public";
170
			$sql .= ", fk_user_author";
171
			$sql .= ", fk_projet";
172
			$sql .= ", fk_contrat";
173
			$sql .= ", modelpdf";
174
175
			$sql .= ", frequency";
176
			$sql .= ", unit_frequency";
177
			$sql .= ", date_when";
178
			$sql .= ", date_last_gen";
179
			$sql .= ", nb_gen_done";
180
			$sql .= ", nb_gen_max";
181
			// $sql.= ", auto_validate";
182
183
			$sql .= ") VALUES (";
184
			$sql .= "'".$this->db->escape($this->title)."'";
185
			$sql .= ", ".($this->socid > 0 ? ((int) $this->socid) : 'null');
186
			$sql .= ", ".((int) $conf->entity);
187
			$sql .= ", '".$this->db->idate($now)."'";
188
			$sql .= ", ".(!empty($fichintsrc->duration) ? ((int) $fichintsrc->duration) : '0');
189
			$sql .= ", ".(!empty($this->description) ? ("'".$this->db->escape($this->description)."'") : "null");
190
			$sql .= ", ".(!empty($fichintsrc->note_private) ? ("'".$this->db->escape($fichintsrc->note_private)."'") : "null");
191
			$sql .= ", ".(!empty($fichintsrc->note_public) ? ("'".$this->db->escape($fichintsrc->note_public)."'") : "null");
192
			$sql .= ", ".((int) $user->id);
193
			// si c'est la même société on conserve les liens vers le projet et le contrat
194
			if ($this->socid == $fichintsrc->socid) {
195
				$sql .= ", ".(!empty($fichintsrc->fk_project) ? ((int) $fichintsrc->fk_project) : "null");
196
				$sql .= ", ".(!empty($fichintsrc->fk_contrat) ? ((int) $fichintsrc->fk_contrat) : "null");
197
			} else {
198
				$sql .= ", null, null";
199
			}
200
201
			$sql .= ", ".(!empty($fichintsrc->model_pdf) ? "'".$this->db->escape($fichintsrc->model_pdf)."'" : "''");
202
203
			// récurrence
204
			$sql .= ", ".(!empty($this->frequency) ? ((int) $this->frequency) : "null");
205
			$sql .= ", '".$this->db->escape($this->unit_frequency)."'";
206
			$sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'null');
207
			$sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'null');
208
			$sql .= ", 0"; // we start à 0
209
			$sql .= ", ".((int) $this->nb_gen_max);
210
			// $sql.= ", ".$this->auto_validate;
211
			$sql .= ")";
212
213
			if ($this->db->query($sql)) {
214
				$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.$this->table_element);
215
216
				/*
217
				 * Lines
218
				 */
219
				$num = count($fichintsrc->lines);
220
				for ($i = 0; $i < $num; $i++) {
221
					//var_dump($fichintsrc->lines[$i]);
222
					$result_insert = $this->addline(
223
						$fichintsrc->lines[$i]->desc,
224
						$fichintsrc->lines[$i]->duration,
225
						$fichintsrc->lines[$i]->datei,
226
						$fichintsrc->lines[$i]->rang,
227
						$fichintsrc->lines[$i]->subprice,
228
						$fichintsrc->lines[$i]->qty,
229
						$fichintsrc->lines[$i]->tva_tx,
230
						$fichintsrc->lines[$i]->fk_product,
231
						$fichintsrc->lines[$i]->remise_percent,
232
						'HT',
233
						0,
234
						'',
235
						0,
236
						$fichintsrc->lines[$i]->product_type,
237
						$fichintsrc->lines[$i]->special_code,
238
						$fichintsrc->lines[$i]->label,
239
						$fichintsrc->lines[$i]->fk_unit
240
					);
241
242
					if ($result_insert < 0) {
243
						$error++;
244
					}
245
				}
246
247
				if ($error) {
248
					$this->db->rollback();
249
				} else {
250
					$this->db->commit();
251
					return $this->id;
252
				}
253
			} else {
254
				$this->error = $this->db->error().' sql='.$sql;
255
				$this->db->rollback();
256
				return -2;
257
			}
258
		} else {
259
			$this->db->rollback();
260
			return -1;
261
		}
262
	}
263
264
265
	/**
266
	 *	Get the template of intervention object and lines
267
	 *
268
	 *	@param	  int		$rowid	   	Id of object to load
269
	 * 	@param		string	$ref			Reference of fichinter
270
	 * 	@param		string	$ref_ext		External reference of fichinter
271
	 *	@return	 int		 			>0 if OK, <0 if KO, 0 if not found
272
	 */
273
	public function fetch($rowid = 0, $ref = '', $ref_ext = '')
274
	{
275
		$sql = 'SELECT f.titre as title, f.fk_soc';
276
		$sql .= ', f.datec, f.duree, f.fk_projet, f.fk_contrat, f.description';
277
		$sql .= ', f.note_private, f.note_public, f.fk_user_author';
278
		$sql .= ', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.auto_validate';
279
		$sql .= ', f.note_private, f.note_public, f.fk_user_author';
280
		$sql .= ' FROM '.MAIN_DB_PREFIX.'fichinter_rec as f';
281
		if ($rowid > 0) {
282
			$sql .= " WHERE f.rowid = ".((int) $rowid);
283
		} elseif ($ref) {
284
			$sql .= " WHERE f.titre = '".$this->db->escape($ref)."'";
285
		}
286
287
		dol_syslog(get_class($this)."::fetch rowid=".$rowid, LOG_DEBUG);
288
289
		$result = $this->db->query($sql);
290
		if ($result) {
291
			if ($this->db->num_rows($result)) {
292
				$obj = $this->db->fetch_object($result);
293
294
				$this->id = $rowid;
295
				$this->titre				= $obj->title;
296
				$this->title				= $obj->title;
297
				$this->ref = $obj->title;
298
				$this->description = $obj->description;
299
				$this->datec				= $obj->datec;
300
				$this->duration = $obj->duree;
301
				$this->socid				= $obj->fk_soc;
302
				$this->statut = 0;
303
				$this->fk_project			= $obj->fk_projet;
304
				$this->fk_contrat			= $obj->fk_contrat;
305
				$this->note_private = $obj->note_private;
306
				$this->note_public			= $obj->note_public;
307
				$this->user_author			= $obj->fk_user_author;
308
				$this->model_pdf			= $obj->model_pdf;
309
				$this->modelpdf				= $obj->model_pdf; // deprecated
310
				$this->rang = $obj->rang;
311
				$this->special_code = $obj->special_code;
312
				$this->frequency			= $obj->frequency;
313
				$this->unit_frequency = $obj->unit_frequency;
314
				$this->date_when			= $this->db->jdate($obj->date_when);
315
				$this->date_last_gen		= $this->db->jdate($obj->date_last_gen);
316
				$this->nb_gen_done = $obj->nb_gen_done;
317
				$this->nb_gen_max = $obj->nb_gen_max;
318
				$this->auto_validate		= $obj->auto_validate;
319
320
				$this->brouillon = 1;
321
322
				// Lines
323
				$result = $this->fetch_lines();
324
				if ($result < 0) {
325
					$this->error = $this->db->error();
326
					return -3;
327
				}
328
				return 1;
329
			} else {
330
				$this->error = 'Interventional with id '.$rowid.' not found sql='.$sql;
331
				dol_syslog(get_class($this).'::Fetch Error '.$this->error, LOG_ERR);
332
				return -2;
333
			}
334
		} else {
335
			$this->error = $this->db->error();
336
			return -1;
337
		}
338
	}
339
340
341
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
342
	/**
343
	 *  Load all lines of template of intervention into this->lines
344
	 *
345
	 *  @param   int	$sall   sall
346
	 *  @return	 int            1 if OK, < 0 if KO
347
	 */
348
	public function fetch_lines($sall = 0)
349
	{
350
		// phpcs:enable
351
		$this->lines = array();
352
353
		$sql = 'SELECT l.rowid, l.fk_product, l.product_type, l.label as custom_label, l.description, ';
354
		$sql .= ' l.price, l.qty, l.tva_tx, l.remise, l.remise_percent, l.subprice, l.duree, ';
355
		$sql .= ' l.total_ht, l.total_tva, l.total_ttc,';
356
		$sql .= ' l.rang, l.special_code,';
357
		$sql .= ' l.fk_unit, p.ref as product_ref, p.fk_product_type as fk_product_type,';
358
		$sql .= ' p.label as product_label, p.description as product_desc';
359
		$sql .= ' FROM '.MAIN_DB_PREFIX.'fichinterdet_rec as l';
360
		$sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
361
		$sql .= ' WHERE l.fk_fichinter = '.((int) $this->id);
362
363
		dol_syslog('FichinterRec::fetch_lines', LOG_DEBUG);
364
365
		$result = $this->db->query($sql);
366
		if ($result) {
367
			$num = $this->db->num_rows($result);
368
			$i = 0;
369
			while ($i < $num) {
370
				$objp = $this->db->fetch_object($result);
371
372
				$line = new FichinterLigne($this->db);
373
374
				$line->id = $objp->rowid;
375
				$line->label = $objp->custom_label; // Label line
376
				$line->desc = $objp->description; // Description line
377
				$line->product_type = $objp->product_type; // Type of line
378
				$line->product_ref = $objp->product_ref; // Ref product
379
				$line->product_label = $objp->product_label; // Label product
380
				$line->product_desc = $objp->product_desc; // Description product
381
				$line->fk_product_type = $objp->fk_product_type; // Type of product
0 ignored issues
show
Bug introduced by
The property fk_product_type does not exist on FichinterLigne. Did you mean product_type?
Loading history...
382
				$line->qty = $objp->qty;
383
				$line->duree = $objp->duree;
384
				$line->duration = $objp->duree;
385
				$line->datei = $objp->date;
386
				$line->subprice = $objp->subprice;
387
				$line->tva_tx = $objp->tva_tx;
0 ignored issues
show
Bug introduced by
The property tva_tx does not seem to exist on FichinterLigne.
Loading history...
388
				$line->remise_percent = $objp->remise_percent;
389
				$line->fk_remise_except = $objp->fk_remise_except;
0 ignored issues
show
Bug introduced by
The property fk_remise_except does not seem to exist on FichinterLigne.
Loading history...
390
				$line->fk_product = $objp->fk_product;
0 ignored issues
show
Bug introduced by
The property fk_product does not exist on FichinterLigne. Did you mean product?
Loading history...
391
				$line->date_start = $objp->date_start;
0 ignored issues
show
Bug introduced by
The property date_start does not seem to exist on FichinterLigne.
Loading history...
392
				$line->date_end = $objp->date_end;
0 ignored issues
show
Bug introduced by
The property date_end does not seem to exist on FichinterLigne.
Loading history...
393
				$line->date_start = $objp->date_start;
394
				$line->date_end = $objp->date_end;
395
				$line->info_bits = $objp->info_bits;
396
				$line->total_ht = $objp->total_ht;
397
				$line->total_tva = $objp->total_tva;
398
				$line->total_ttc = $objp->total_ttc;
399
				$line->code_ventilation = $objp->fk_code_ventilation;
0 ignored issues
show
Bug introduced by
The property code_ventilation does not seem to exist on FichinterLigne.
Loading history...
400
				$line->rang = $objp->rang;
401
				$line->special_code = $objp->special_code;
402
				$line->fk_unit = $objp->fk_unit;
403
404
				$this->lines[$i] = $line;
405
406
				$i++;
407
			}
408
409
			$this->db->free($result);
410
			return 1;
411
		} else {
412
			$this->error = $this->db->error();
413
			return -3;
414
		}
415
	}
416
417
418
	/**
419
	 * 	Delete template fichinter rec
420
	 *
421
	 *	@param	 	int		$rowid	  	    Id of fichinter rec to delete. If empty, we delete current instance of fichinter rec
422
	 *	@param		int		$notrigger	    1=Does not execute triggers, 0= execute triggers
423
	 *	@param		int		$idwarehouse    Id warehouse to use for stock change.
424
	 *	@return		int						<0 if KO, >0 if OK
425
	 */
426
	public function delete($rowid = 0, $notrigger = 0, $idwarehouse = -1)
427
	{
428
		if (empty($rowid)) {
429
			$rowid = $this->id;
430
		}
431
432
		dol_syslog(get_class($this)."::delete rowid=".$rowid, LOG_DEBUG);
433
434
		$error = 0;
435
		$this->db->begin();
436
437
		$sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinterdet_rec WHERE fk_fichinter = ".((int) $rowid);
438
		dol_syslog($sql);
439
		if ($this->db->query($sql)) {
440
			$sql = "DELETE FROM ".MAIN_DB_PREFIX."fichinter_rec WHERE rowid = ".((int) $rowid);
441
			dol_syslog($sql);
442
			if (!$this->db->query($sql)) {
443
				$this->error = $this->db->lasterror();
444
				$error = -1;
445
			}
446
		} else {
447
			$this->error = $this->db->lasterror();
448
			$error = -2;
449
		}
450
451
		if (!$error) {
452
			$this->db->commit();
453
			return 1;
454
		} else {
455
			$this->db->rollback();
456
			return $error;
457
		}
458
	}
459
460
461
	/**
462
	 *  Add a line to fichinter rec
463
	 *
464
	 *  @param		string		$desc               Description de la ligne
465
	 *  @param		integer		$duration           Durée
466
	 *  @param		string	    $datei				Date
467
	 *  @param	    int			$rang			    Position of line
468
	 *  @param		double		$pu_ht			    Unit price without tax (> 0 even for credit note)
469
	 *  @param		double		$qty			 	Quantity
470
	 *  @param		double		$txtva		   	    Forced VAT rate, otherwise -1
471
	 *  @param		int			$fk_product	  	    Id of predefined product/service
472
	 *  @param		double		$remise_percent  	Percentage of discount on line
473
	 *  @param		string		$price_base_type	HT or TTC
474
	 *  @param		int			$info_bits			Bits for type of lines
475
	 *  @param		int			$fk_remise_except   Id discount
476
	 *  @param		double		$pu_ttc			    Unit price with tax (> 0 even for credit note)
477
	 *  @param		int			$type				Type of line (0=product, 1=service)
478
	 *  @param		int			$special_code		Special code
479
	 *  @param		string		$label				Label of the line
480
	 *  @param		string		$fk_unit			Unit
481
	 *  @return		int			 				    <0 if KO, Id of line if OK
482
	 */
483
	public function addline($desc, $duration, $datei, $rang = -1, $pu_ht = 0, $qty = 0, $txtva = 0, $fk_product = 0, $remise_percent = 0, $price_base_type = 'HT', $info_bits = 0, $fk_remise_except = '', $pu_ttc = 0, $type = 0, $special_code = 0, $label = '', $fk_unit = null)
484
	{
485
		global $mysoc;
486
487
		include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
488
489
		// Check parameters
490
		if ($type < 0) {
491
			return -1;
492
		}
493
494
		if ($this->brouillon) {
495
			// Clean parameters
496
			$remise_percent = price2num($remise_percent);
497
			$qty = price2num($qty);
498
			if (!$qty) {
499
				$qty = 1;
500
			}
501
			if (!$info_bits) {
502
				$info_bits = 0;
503
			}
504
			$pu_ht = price2num($pu_ht);
505
			$pu_ttc = price2num($pu_ttc);
506
			if (!preg_match('/\((.*)\)/', $txtva)) {
507
				$txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
508
			}
509
510
			if ($price_base_type == 'HT') {
511
				$pu = $pu_ht;
512
			} else {
513
				$pu = $pu_ttc;
514
			}
515
516
			// Calcul du total TTC et de la TVA pour la ligne a partir de
517
			// qty, pu, remise_percent et txtva
518
			// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
519
			// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
520
			$tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, 0, 0, 0, $price_base_type, $info_bits, $type, $mysoc);
521
522
			$total_ht  = $tabprice[0];
523
			$total_tva = $tabprice[1];
524
			$total_ttc = $tabprice[2];
525
526
			$product_type = $type;
527
			if ($fk_product) {
528
				$product = new Product($this->db);
529
				$result = $product->fetch($fk_product);
530
				$product_type = $product->type;
531
			}
532
533
			$sql = "INSERT INTO ".MAIN_DB_PREFIX."fichinterdet_rec (";
534
			$sql .= "fk_fichinter";
535
			$sql .= ", label";
536
			$sql .= ", description";
537
			$sql .= ", date";
538
			$sql .= ", duree";
539
			//$sql.= ", price";
540
			//$sql.= ", qty";
541
			//$sql.= ", tva_tx";
542
			$sql .= ", fk_product";
543
			$sql .= ", product_type";
544
			$sql .= ", remise_percent";
545
			//$sql.= ", subprice";
546
			$sql .= ", remise";
547
			$sql .= ", total_ht";
548
			$sql .= ", total_tva";
549
			$sql .= ", total_ttc";
550
			$sql .= ", rang";
551
			//$sql.= ", special_code";
552
			$sql .= ", fk_unit";
553
			$sql .= ") VALUES (";
554
			$sql .= (int) $this->id;
555
			$sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
556
			$sql .= ", ".(!empty($desc) ? "'".$this->db->escape($desc)."'" : "null");
557
			$sql .= ", ".(!empty($datei) ? "'".$this->db->idate($datei)."'" : "null");
558
			$sql .= ", ".$duration;
559
			//$sql.= ", ".price2num($pu_ht);
560
			//$sql.= ", ".(!empty($qty)? $qty :(!empty($duration)? $duration :"null"));
561
			//$sql.= ", ".price2num($txtva);
562
			$sql .= ", ".(!empty($fk_product) ? $fk_product : "null");
563
			$sql .= ", ".$product_type;
564
			$sql .= ", ".(!empty($remise_percent) ? $remise_percent : "null");
565
			//$sql.= ", '".price2num($pu_ht)."'";
566
			$sql .= ", null";
567
			$sql .= ", '".price2num($total_ht)."'";
568
			$sql .= ", '".price2num($total_tva)."'";
569
			$sql .= ", '".price2num($total_ttc)."'";
570
			$sql .= ", ".(int) $rang;
571
			//$sql.= ", ".$special_code;
572
			$sql .= ", ".(!empty($fk_unit) ? $fk_unit : "null");
573
			$sql .= ")";
574
575
			dol_syslog(get_class($this)."::addline", LOG_DEBUG);
576
			if ($this->db->query($sql)) {
577
				return 1;
578
			} else {
579
				$this->error = $this->db->lasterror();
580
				return -1;
581
			}
582
		}
583
	}
584
585
586
	// phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
587
	/**
588
	 *	Rend la fichinter automatique
589
	 *
590
	 *	@param		User	$user		User object
591
	 *	@param		int		$freq		Freq
592
	 *	@param		string	$courant	Courant
593
	 *	@return		int					0 if OK, <0 if KO
594
	 */
595
	public function set_auto($user, $freq, $courant)
596
	{
597
		// phpcs:enable
598
		if ($user->rights->fichinter->creer) {
599
			$sql = "UPDATE ".MAIN_DB_PREFIX."fichinter_rec ";
600
			$sql .= " SET frequency='".$this->db->escape($freq)."'";
601
			$sql .= ", date_last_gen='".$this->db->escape($courant)."'";
602
			$sql .= " WHERE rowid = ".((int) $this->id);
603
604
			$resql = $this->db->query($sql);
605
606
			if ($resql) {
607
				$this->frequency = $freq;
608
				$this->date_last_gen = $courant;
609
				return 0;
610
			} else {
611
				dol_print_error($this->db);
612
				return -1;
613
			}
614
		} else {
615
			return -2;
616
		}
617
	}
618
619
	/**
620
	 *  Return clicable name (with picto eventually)
621
	 *
622
	 *  @param	int		$withpicto      Add picto into link
623
	 *  @param  string	$option		    Where point the link
624
	 *  @param  int		$max			Maxlength of ref
625
	 *  @param  int		$short		    1=Return just URL
626
	 *  @param  string   $moretitle     Add more text to title tooltip
627
	 *  @return string 					String with URL
628
	 */
629
	public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '')
630
	{
631
		global $langs, $hookmanager;
632
633
		$result = '';
634
		$label = $langs->trans("ShowInterventionModel").': '.$this->ref;
635
636
		$url = DOL_URL_ROOT.'/fichinter/card-rec.php?id='.$this->id;
637
638
		if ($short) {
639
			return $url;
640
		}
641
642
		$picto = 'intervention';
643
644
		$link = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
645
		$linkend = '</a>';
646
647
		if ($withpicto) {
648
			$result .= $link.img_object($label, $picto, 'class="classfortooltip"').$linkend;
649
		}
650
		if ($withpicto && $withpicto != 2) {
651
			$result .= ' ';
652
		}
653
		if ($withpicto != 2) {
654
			$result .= $link.$this->ref.$linkend;
655
		}
656
		global $action;
657
		$hookmanager->initHooks(array($this->element . 'dao'));
658
		$parameters = array('id'=>$this->id, 'getnomurl' => &$result);
659
		$reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
660
		if ($reshook > 0) {
661
			$result = $hookmanager->resPrint;
662
		} else {
663
			$result .= $hookmanager->resPrint;
664
		}
665
		return $result;
666
	}
667
668
669
	/**
670
	 *  Initialise an instance with random values.
671
	 *  Used to build previews or test instances.
672
	 *	id must be 0 if object instance is a specimen.
673
	 *
674
	 *	@param	string		$option		''=Create a specimen fichinter with lines, 'nolines'=No lines
675
	 *  @return	void
676
	 */
677
	public function initAsSpecimen($option = '')
678
	{
679
		global $user, $langs, $conf;
680
681
		$now = dol_now();
682
		$arraynow = dol_getdate($now);
683
		$nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
684
685
		parent::initAsSpecimen($option);
686
687
		$this->usenewprice = 1;
688
	}
689
690
	/**
691
	 * Function used to replace a thirdparty id with another one.
692
	 *
693
	 * @param DoliDB $db Database handler
694
	 * @param int $origin_id Old thirdparty id
695
	 * @param int $dest_id New thirdparty id
696
	 * @return bool
697
	 */
698
	public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id)
699
	{
700
		$tables = array('fichinter_rec');
701
702
		return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables);
703
	}
704
705
	/**
706
	 * Function used to replace a product id with another one.
707
	 *
708
	 * @param DoliDB $db Database handler
709
	 * @param int $origin_id Old product id
710
	 * @param int $dest_id New product id
711
	 * @return bool
712
	 */
713
	public static function replaceProduct(DoliDB $db, $origin_id, $dest_id)
714
	{
715
		$tables = array(
716
			'fichinterdet_rec'
717
		);
718
719
		return CommonObject::commonReplaceProduct($db, $origin_id, $dest_id, $tables);
720
	}
721
722
	/**
723
	 *	Update frequency and unit
724
	 *
725
	 *	@param	 	int		$frequency		value of frequency
726
	 *	@param	 	string	$unit 			unit of frequency  (d, m, y)
727
	 *	@return		int						<0 if KO, >0 if OK
728
	 */
729
	public function setFrequencyAndUnit($frequency, $unit)
730
	{
731
		if (!$this->table_element) {
732
			dol_syslog(get_class($this)."::setFrequencyAndUnit called with table_element not defined", LOG_ERR);
733
			return -1;
734
		}
735
736
		if (!empty($frequency) && empty($unit)) {
737
			dol_syslog(get_class($this)."::setFrequencyAndUnit called with frequency defined but unit not ", LOG_ERR);
738
			return -2;
739
		}
740
741
		$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
742
		$sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null');
743
		if (!empty($unit)) {
744
			$sql .= ', unit_frequency = "'.$this->db->escape($unit).'"';
745
		}
746
		$sql .= " WHERE rowid = ".((int) $this->id);
747
748
		dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG);
749
		if ($this->db->query($sql)) {
750
			$this->frequency = $frequency;
751
			if (!empty($unit)) {
752
				$this->unit_frequency = $unit;
753
			}
754
			return 1;
755
		} else {
756
			dol_print_error($this->db);
757
			return -1;
758
		}
759
	}
760
761
	/**
762
	 *	Update the next date of execution
763
	 *
764
	 *	@param	 	datetime	$date					date of execution
765
	 *	@param	 	int			$increment_nb_gen_done	0 do nothing more, >0 increment nb_gen_done
766
	 *	@return		int									<0 if KO, >0 if OK
767
	 */
768
	public function setNextDate($date, $increment_nb_gen_done = 0)
769
	{
770
		if (!$this->table_element) {
771
			dol_syslog(get_class($this)."::setNextDate was called on objet with property table_element not defined", LOG_ERR);
772
			return -1;
773
		}
774
		$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
775
		$sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
776
		if ($increment_nb_gen_done > 0) {
777
			$sql .= ', nb_gen_done = nb_gen_done + 1';
778
		}
779
		$sql .= " WHERE rowid = ".((int) $this->id);
780
781
		dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG);
782
		if ($this->db->query($sql)) {
783
			$this->date_when = $date;
784
			if ($increment_nb_gen_done > 0) {
785
				$this->nb_gen_done++;
786
			}
787
			return 1;
788
		} else {
789
			dol_print_error($this->db);
790
			return -1;
791
		}
792
	}
793
794
	/**
795
	 *	Update the maximum period
796
	 *
797
	 *	@param	 	int		$nb		number of maximum period
798
	 *	@return		int				<0 if KO, >0 if OK
799
	 */
800
	public function setMaxPeriod($nb)
801
	{
802
		if (!$this->table_element) {
803
			dol_syslog(get_class($this)."::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR);
804
			return -1;
805
		}
806
807
		if (empty($nb)) {
808
			$nb = 0;
809
		}
810
811
		$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
812
		$sql .= ' SET nb_gen_max = '.((int) $nb);
813
		$sql .= ' WHERE rowid = '.((int) $this->id);
814
815
		dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG);
816
		if ($this->db->query($sql)) {
817
			$this->nb_gen_max = $nb;
818
			return 1;
819
		} else {
820
			dol_print_error($this->db);
821
			return -1;
822
		}
823
	}
824
825
	/**
826
	 *	Update the auto validate fichinter
827
	 *
828
	 *	@param	 	int		$validate		0 to create in draft, 1 to create and validate fichinter
829
	 *	@return		int						<0 if KO, >0 if OK
830
	 */
831
	public function setAutoValidate($validate)
832
	{
833
		if (!$this->table_element) {
834
			dol_syslog(get_class($this)."::setAutoValidate called with property table_element not defined", LOG_ERR);
835
			return -1;
836
		}
837
838
		$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
839
		$sql .= ' SET auto_validate = '.((int) $validate);
840
		$sql .= ' WHERE rowid = '.((int) $this->id);
841
842
		dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
843
		if ($this->db->query($sql)) {
844
			$this->auto_validate = $validate;
845
			return 1;
846
		} else {
847
			dol_print_error($this->db);
848
			return -1;
849
		}
850
	}
851
852
	/**
853
	 *	Update the Number of Generation Done
854
	 *
855
	 *	@return		int						<0 if KO, >0 if OK
856
	 */
857
	public function updateNbGenDone()
858
	{
859
		if (!$this->table_element) {
860
			dol_syslog(get_class($this)."::updateNbGenDone called with property table_element not defined", LOG_ERR);
861
			return -1;
862
		}
863
864
		$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
865
		$sql .= ' SET nb_gen_done = nb_gen_done + 1';
866
		$sql .= ' , date_last_gen = now()';
867
		// si on et arrivé à la fin des génération
868
		if ($this->nb_gen_max == $this->nb_gen_done + 1) {
869
			$sql .= ' , statut = 1';
870
		}
871
872
		$sql .= " WHERE rowid = ".((int) $this->id);
873
874
		dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
875
		if ($this->db->query($sql)) {
876
			$this->nb_gen_done = $this->nb_gen_done + 1;
877
			$this->nb_gen_done = dol_now();
878
			return 1;
879
		} else {
880
			dol_print_error($this->db);
881
			return -1;
882
		}
883
	}
884
}
885