|
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) 2017 Frédéric France <[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/compta/facture/class/facture-rec.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.'/compta/facture/class/facture.class.php'; |
|
34
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Class to manage invoice templates |
|
39
|
|
|
*/ |
|
40
|
|
|
class FactureRec extends CommonInvoice |
|
41
|
|
|
{ |
|
42
|
|
|
/** |
|
43
|
|
|
* @var string ID to identify managed object |
|
44
|
|
|
*/ |
|
45
|
|
|
public $element='facturerec'; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @var string Name of table without prefix where object is stored |
|
49
|
|
|
*/ |
|
50
|
|
|
public $table_element='facture_rec'; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var int Name of subtable line |
|
54
|
|
|
*/ |
|
55
|
|
|
public $table_element_line='facturedet_rec'; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var int Field with ID of parent key if this field has a parent |
|
59
|
|
|
*/ |
|
60
|
|
|
public $fk_element='fk_facture'; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @var string String with name of icon for myobject. Must be the part after the 'object_' into object_myobject.png |
|
64
|
|
|
*/ |
|
65
|
|
|
public $picto='bill'; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @var int Entity |
|
69
|
|
|
*/ |
|
70
|
|
|
public $entity; |
|
71
|
|
|
|
|
72
|
|
|
public $number; |
|
73
|
|
|
public $date; |
|
74
|
|
|
public $amount; |
|
75
|
|
|
public $remise; |
|
76
|
|
|
public $tva; |
|
77
|
|
|
public $total; |
|
78
|
|
|
public $db_table; |
|
79
|
|
|
public $propalid; |
|
80
|
|
|
|
|
81
|
|
|
public $date_last_gen; |
|
82
|
|
|
public $date_when; |
|
83
|
|
|
public $nb_gen_done; |
|
84
|
|
|
public $nb_gen_max; |
|
85
|
|
|
|
|
86
|
|
|
public $frequency; |
|
87
|
|
|
public $unit_frequency; |
|
88
|
|
|
|
|
89
|
|
|
public $rang; |
|
90
|
|
|
public $special_code; |
|
91
|
|
|
|
|
92
|
|
|
public $usenewprice=0; |
|
93
|
|
|
|
|
94
|
|
|
public $suspended; // status |
|
95
|
|
|
|
|
96
|
|
|
const STATUS_NOTSUSPENDED = 0; |
|
97
|
|
|
const STATUS_SUSPENDED = 1; |
|
98
|
|
|
|
|
99
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* Constructor |
|
103
|
|
|
* |
|
104
|
|
|
* @param DoliDB $db Database handler |
|
105
|
|
|
*/ |
|
106
|
|
|
function __construct($db) |
|
107
|
|
|
{ |
|
108
|
|
|
$this->db = $db; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Create a predefined invoice |
|
113
|
|
|
* |
|
114
|
|
|
* @param User $user User object |
|
115
|
|
|
* @param int $facid Id of source invoice |
|
116
|
|
|
* @return int <0 if KO, id of invoice created if OK |
|
117
|
|
|
*/ |
|
118
|
|
|
function create($user, $facid) |
|
119
|
|
|
{ |
|
120
|
|
|
global $conf; |
|
121
|
|
|
|
|
122
|
|
|
$error=0; |
|
123
|
|
|
$now=dol_now(); |
|
124
|
|
|
|
|
125
|
|
|
// Clean parameters |
|
126
|
|
|
$this->titre=trim($this->titre); |
|
127
|
|
|
$this->usenewprice=empty($this->usenewprice)?0:$this->usenewprice; |
|
128
|
|
|
if (empty($this->suspended)) $this->suspended=0; |
|
129
|
|
|
|
|
130
|
|
|
// No frequency defined then no next date to execution |
|
131
|
|
|
if (empty($this->frequency)) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->frequency=0; |
|
134
|
|
|
$this->date_when=null; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
|
|
138
|
|
|
$this->frequency=abs($this->frequency); |
|
139
|
|
|
$this->nb_gen_done=0; |
|
140
|
|
|
$this->nb_gen_max=empty($this->nb_gen_max)?0:$this->nb_gen_max; |
|
141
|
|
|
$this->auto_validate=empty($this->auto_validate)?0:$this->auto_validate; |
|
142
|
|
|
$this->generate_pdf = empty($this->generate_pdf)?0:$this->generate_pdf; |
|
143
|
|
|
|
|
144
|
|
|
$this->db->begin(); |
|
145
|
|
|
|
|
146
|
|
|
// Charge facture modele |
|
147
|
|
|
$facsrc=new Facture($this->db); |
|
148
|
|
|
$result=$facsrc->fetch($facid); |
|
149
|
|
|
if ($result > 0) |
|
150
|
|
|
{ |
|
151
|
|
|
// On positionne en mode brouillon la facture |
|
152
|
|
|
$this->brouillon = 1; |
|
153
|
|
|
|
|
154
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_rec ("; |
|
155
|
|
|
$sql.= "titre"; |
|
156
|
|
|
$sql.= ", fk_soc"; |
|
157
|
|
|
$sql.= ", entity"; |
|
158
|
|
|
$sql.= ", datec"; |
|
159
|
|
|
$sql.= ", amount"; |
|
160
|
|
|
$sql.= ", remise"; |
|
161
|
|
|
$sql.= ", note_private"; |
|
162
|
|
|
$sql.= ", note_public"; |
|
163
|
|
|
$sql.= ", modelpdf"; |
|
164
|
|
|
$sql.= ", fk_user_author"; |
|
165
|
|
|
$sql.= ", fk_projet"; |
|
166
|
|
|
$sql.= ", fk_account"; |
|
167
|
|
|
$sql.= ", fk_cond_reglement"; |
|
168
|
|
|
$sql.= ", fk_mode_reglement"; |
|
169
|
|
|
$sql.= ", usenewprice"; |
|
170
|
|
|
$sql.= ", frequency"; |
|
171
|
|
|
$sql.= ", unit_frequency"; |
|
172
|
|
|
$sql.= ", date_when"; |
|
173
|
|
|
$sql.= ", date_last_gen"; |
|
174
|
|
|
$sql.= ", nb_gen_done"; |
|
175
|
|
|
$sql.= ", nb_gen_max"; |
|
176
|
|
|
$sql.= ", auto_validate"; |
|
177
|
|
|
$sql.= ", generate_pdf"; |
|
178
|
|
|
$sql.= ", fk_multicurrency"; |
|
179
|
|
|
$sql.= ", multicurrency_code"; |
|
180
|
|
|
$sql.= ", multicurrency_tx"; |
|
181
|
|
|
$sql.= ", suspended"; |
|
182
|
|
|
$sql.= ") VALUES ("; |
|
183
|
|
|
$sql.= "'".$this->db->escape($this->titre)."'"; |
|
184
|
|
|
$sql.= ", ".$facsrc->socid; |
|
185
|
|
|
$sql.= ", ".$conf->entity; |
|
186
|
|
|
$sql.= ", '".$this->db->idate($now)."'"; |
|
187
|
|
|
$sql.= ", ".(!empty($facsrc->amount)?$facsrc->amount:'0'); |
|
188
|
|
|
$sql.= ", ".(!empty($facsrc->remise)?$this->remise:'0'); |
|
189
|
|
|
$sql.= ", ".(!empty($this->note_private)?("'".$this->db->escape($this->note_private)."'"):"NULL"); |
|
190
|
|
|
$sql.= ", ".(!empty($this->note_public)?("'".$this->db->escape($this->note_public)."'"):"NULL"); |
|
191
|
|
|
$sql.= ", ".(!empty($this->modelpdf)?("'".$this->db->escape($this->modelpdf)."'"):"NULL"); |
|
192
|
|
|
$sql.= ", '".$this->db->escape($user->id)."'"; |
|
193
|
|
|
$sql.= ", ".(! empty($facsrc->fk_project)?"'".$facsrc->fk_project."'":"null"); |
|
194
|
|
|
$sql.= ", ".(! empty($facsrc->fk_account)?"'".$facsrc->fk_account."'":"null"); |
|
195
|
|
|
$sql.= ", ".($facsrc->cond_reglement_id > 0 ? $this->db->escape($facsrc->cond_reglement_id) : "null"); |
|
196
|
|
|
$sql.= ", ".($facsrc->mode_reglement_id > 0 ? $this->db->escape($facsrc->mode_reglement_id) : "null"); |
|
197
|
|
|
$sql.= ", ".$this->usenewprice; |
|
198
|
|
|
$sql.= ", ".$this->frequency; |
|
199
|
|
|
$sql.= ", '".$this->db->escape($this->unit_frequency)."'"; |
|
200
|
|
|
$sql.= ", ".(!empty($this->date_when)?"'".$this->db->idate($this->date_when)."'":'NULL'); |
|
201
|
|
|
$sql.= ", ".(!empty($this->date_last_gen)?"'".$this->db->idate($this->date_last_gen)."'":'NULL'); |
|
202
|
|
|
$sql.= ", ".$this->db->escape($this->nb_gen_done); |
|
203
|
|
|
$sql.= ", ".$this->db->escape($this->nb_gen_max); |
|
204
|
|
|
$sql.= ", ".$this->db->escape($this->auto_validate); |
|
205
|
|
|
$sql.= ", ".$this->db->escape($this->generate_pdf); |
|
206
|
|
|
$sql.= ", ".$this->db->escape($facsrc->fk_multicurrency); |
|
207
|
|
|
$sql.= ", '".$this->db->escape($facsrc->multicurrency_code)."'"; |
|
208
|
|
|
$sql.= ", ".$this->db->escape($facsrc->multicurrency_tx); |
|
209
|
|
|
$sql.= ", ".$this->db->escape($this->suspended); |
|
210
|
|
|
$sql.= ")"; |
|
211
|
|
|
|
|
212
|
|
|
if ($this->db->query($sql)) |
|
213
|
|
|
{ |
|
214
|
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec"); |
|
215
|
|
|
|
|
216
|
|
|
// Fields used into addline later |
|
217
|
|
|
$this->fk_multicurrency = $facsrc->fk_multicurrency; |
|
218
|
|
|
$this->multicurrency_code = $facsrc->multicurrency_code; |
|
219
|
|
|
$this->multicurrency_tx = $facsrc->multicurrency_tx; |
|
220
|
|
|
|
|
221
|
|
|
// Add lines |
|
222
|
|
|
$num=count($facsrc->lines); |
|
223
|
|
|
for ($i = 0; $i < $num; $i++) |
|
224
|
|
|
{ |
|
225
|
|
|
$tva_tx = $facsrc->lines[$i]->tva_tx; |
|
226
|
|
|
if (! empty($facsrc->lines[$i]->vat_src_code) && ! preg_match('/\(/', $tva_tx)) $tva_tx .= ' ('.$facsrc->lines[$i]->vat_src_code.')'; |
|
227
|
|
|
|
|
228
|
|
|
$result_insert = $this->addline( |
|
229
|
|
|
$facsrc->lines[$i]->desc, |
|
230
|
|
|
$facsrc->lines[$i]->subprice, |
|
231
|
|
|
$facsrc->lines[$i]->qty, |
|
232
|
|
|
$tva_tx, |
|
233
|
|
|
$facsrc->lines[$i]->localtax1_tx, |
|
234
|
|
|
$facsrc->lines[$i]->localtax2_tx, |
|
235
|
|
|
$facsrc->lines[$i]->fk_product, |
|
236
|
|
|
$facsrc->lines[$i]->remise_percent, |
|
237
|
|
|
'HT', |
|
238
|
|
|
$facsrc->lines[$i]->info_bits, |
|
239
|
|
|
'', |
|
240
|
|
|
0, |
|
241
|
|
|
$facsrc->lines[$i]->product_type, |
|
242
|
|
|
$facsrc->lines[$i]->rang, |
|
243
|
|
|
$facsrc->lines[$i]->special_code, |
|
244
|
|
|
$facsrc->lines[$i]->label, |
|
245
|
|
|
$facsrc->lines[$i]->fk_unit, |
|
246
|
|
|
$facsrc->lines[$i]->multicurrency_subprice |
|
247
|
|
|
); |
|
248
|
|
|
|
|
249
|
|
|
if ($result_insert < 0) |
|
250
|
|
|
{ |
|
251
|
|
|
$error++; |
|
252
|
|
|
} |
|
253
|
|
|
} |
|
254
|
|
|
|
|
255
|
|
|
if (! empty($this->linkedObjectsIds) && empty($this->linked_objects)) // To use new linkedObjectsIds instead of old linked_objects |
|
256
|
|
|
{ |
|
257
|
|
|
$this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
// Add object linked |
|
261
|
|
|
if (! $error && $this->id && is_array($this->linked_objects) && ! empty($this->linked_objects)) |
|
262
|
|
|
{ |
|
263
|
|
|
foreach($this->linked_objects as $origin => $tmp_origin_id) |
|
264
|
|
|
{ |
|
265
|
|
|
if (is_array($tmp_origin_id)) // New behaviour, if linked_object can have several links per type, so is something like array('contract'=>array(id1, id2, ...)) |
|
266
|
|
|
{ |
|
267
|
|
|
foreach($tmp_origin_id as $origin_id) |
|
268
|
|
|
{ |
|
269
|
|
|
$ret = $this->add_object_linked($origin, $origin_id); |
|
270
|
|
|
if (! $ret) |
|
271
|
|
|
{ |
|
272
|
|
|
$this->error=$this->db->lasterror(); |
|
273
|
|
|
$error++; |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
else // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1)) |
|
278
|
|
|
{ |
|
279
|
|
|
$origin_id = $tmp_origin_id; |
|
280
|
|
|
$ret = $this->add_object_linked($origin, $origin_id); |
|
281
|
|
|
if (! $ret) |
|
282
|
|
|
{ |
|
283
|
|
|
$this->error=$this->db->lasterror(); |
|
284
|
|
|
$error++; |
|
285
|
|
|
} |
|
286
|
|
|
} |
|
287
|
|
|
} |
|
288
|
|
|
} |
|
289
|
|
|
|
|
290
|
|
|
if ($error) |
|
291
|
|
|
{ |
|
292
|
|
|
$this->db->rollback(); |
|
293
|
|
|
} |
|
294
|
|
|
else |
|
295
|
|
|
{ |
|
296
|
|
|
$this->db->commit(); |
|
297
|
|
|
return $this->id; |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
else |
|
301
|
|
|
{ |
|
302
|
|
|
$this->error=$this->db->lasterror(); |
|
303
|
|
|
$this->db->rollback(); |
|
304
|
|
|
return -2; |
|
305
|
|
|
} |
|
306
|
|
|
} |
|
307
|
|
|
else |
|
308
|
|
|
{ |
|
309
|
|
|
$this->db->rollback(); |
|
310
|
|
|
return -1; |
|
311
|
|
|
} |
|
312
|
|
|
} |
|
313
|
|
|
|
|
314
|
|
|
|
|
315
|
|
|
/** |
|
316
|
|
|
* Load object and lines |
|
317
|
|
|
* |
|
318
|
|
|
* @param int $rowid Id of object to load |
|
319
|
|
|
* @param string $ref Reference of recurring invoice |
|
320
|
|
|
* @param string $ref_ext External reference of invoice |
|
321
|
|
|
* @param int $ref_int Internal reference of other object |
|
322
|
|
|
* @return int >0 if OK, <0 if KO, 0 if not found |
|
323
|
|
|
*/ |
|
324
|
|
|
function fetch($rowid, $ref='', $ref_ext='', $ref_int='') |
|
325
|
|
|
{ |
|
326
|
|
|
$sql = 'SELECT f.rowid, f.entity, f.titre, f.suspended, f.fk_soc, f.amount, f.tva, f.localtax1, f.localtax2, f.total, f.total_ttc'; |
|
327
|
|
|
$sql.= ', f.remise_percent, f.remise_absolue, f.remise'; |
|
328
|
|
|
$sql.= ', f.date_lim_reglement as dlr'; |
|
329
|
|
|
$sql.= ', f.note_private, f.note_public, f.fk_user_author'; |
|
330
|
|
|
$sql.= ', f.modelpdf'; |
|
331
|
|
|
$sql.= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet'; |
|
332
|
|
|
$sql.= ', f.fk_account'; |
|
333
|
|
|
$sql.= ', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.usenewprice, f.auto_validate'; |
|
334
|
|
|
$sql.= ', f.generate_pdf'; |
|
335
|
|
|
$sql.= ", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc"; |
|
336
|
|
|
$sql.= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle'; |
|
337
|
|
|
$sql.= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc'; |
|
338
|
|
|
//$sql.= ', el.fk_source'; |
|
339
|
|
|
$sql.= ' FROM '.MAIN_DB_PREFIX.'facture_rec as f'; |
|
340
|
|
|
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as c ON f.fk_cond_reglement = c.rowid'; |
|
341
|
|
|
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id'; |
|
342
|
|
|
//$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = f.rowid AND el.targettype = 'facture'"; |
|
343
|
|
|
$sql.= ' WHERE f.entity IN ('.getEntity('invoice').')'; |
|
344
|
|
|
if ($rowid) $sql.= ' AND f.rowid='.$rowid; |
|
345
|
|
|
elseif ($ref) $sql.= " AND f.titre='".$this->db->escape($ref)."'"; |
|
346
|
|
|
/* This field are not used for template invoice |
|
347
|
|
|
if ($ref_ext) $sql.= " AND f.ref_ext='".$this->db->escape($ref_ext)."'"; |
|
348
|
|
|
if ($ref_int) $sql.= " AND f.ref_int='".$this->db->escape($ref_int)."'"; |
|
349
|
|
|
*/ |
|
350
|
|
|
|
|
351
|
|
|
$result = $this->db->query($sql); |
|
352
|
|
|
if ($result) |
|
353
|
|
|
{ |
|
354
|
|
|
if ($this->db->num_rows($result)) |
|
355
|
|
|
{ |
|
356
|
|
|
$obj = $this->db->fetch_object($result); |
|
357
|
|
|
|
|
358
|
|
|
$this->id = $obj->rowid; |
|
359
|
|
|
$this->entity = $obj->entity; |
|
360
|
|
|
$this->titre = $obj->titre; |
|
361
|
|
|
$this->ref = $obj->titre; |
|
362
|
|
|
$this->ref_client = $obj->ref_client; |
|
363
|
|
|
$this->suspended = $obj->suspended; |
|
364
|
|
|
$this->type = $obj->type; |
|
365
|
|
|
$this->datep = $obj->dp; |
|
366
|
|
|
$this->date = $obj->df; |
|
367
|
|
|
$this->amount = $obj->amount; |
|
368
|
|
|
$this->remise_percent = $obj->remise_percent; |
|
369
|
|
|
$this->remise_absolue = $obj->remise_absolue; |
|
370
|
|
|
$this->remise = $obj->remise; |
|
371
|
|
|
$this->total_ht = $obj->total; |
|
372
|
|
|
$this->total_tva = $obj->tva; |
|
373
|
|
|
$this->total_localtax1 = $obj->localtax1; |
|
374
|
|
|
$this->total_localtax2 = $obj->localtax2; |
|
375
|
|
|
$this->total_ttc = $obj->total_ttc; |
|
376
|
|
|
$this->paye = $obj->paye; |
|
377
|
|
|
$this->close_code = $obj->close_code; |
|
378
|
|
|
$this->close_note = $obj->close_note; |
|
379
|
|
|
$this->socid = $obj->fk_soc; |
|
380
|
|
|
$this->date_lim_reglement = $this->db->jdate($obj->dlr); |
|
381
|
|
|
$this->mode_reglement_id = $obj->fk_mode_reglement; |
|
382
|
|
|
$this->mode_reglement_code = $obj->mode_reglement_code; |
|
383
|
|
|
$this->mode_reglement = $obj->mode_reglement_libelle; |
|
384
|
|
|
$this->cond_reglement_id = $obj->fk_cond_reglement; |
|
385
|
|
|
$this->cond_reglement_code = $obj->cond_reglement_code; |
|
386
|
|
|
$this->cond_reglement = $obj->cond_reglement_libelle; |
|
387
|
|
|
$this->cond_reglement_doc = $obj->cond_reglement_libelle_doc; |
|
388
|
|
|
$this->fk_project = $obj->fk_projet; |
|
389
|
|
|
$this->fk_account = $obj->fk_account; |
|
390
|
|
|
$this->fk_facture_source = $obj->fk_facture_source; |
|
391
|
|
|
$this->note_private = $obj->note_private; |
|
392
|
|
|
$this->note_public = $obj->note_public; |
|
393
|
|
|
$this->user_author = $obj->fk_user_author; |
|
394
|
|
|
$this->modelpdf = $obj->modelpdf; |
|
395
|
|
|
$this->rang = $obj->rang; |
|
396
|
|
|
$this->special_code = $obj->special_code; |
|
397
|
|
|
$this->frequency = $obj->frequency; |
|
398
|
|
|
$this->unit_frequency = $obj->unit_frequency; |
|
399
|
|
|
$this->date_when = $this->db->jdate($obj->date_when); |
|
400
|
|
|
$this->date_last_gen = $this->db->jdate($obj->date_last_gen); |
|
401
|
|
|
$this->nb_gen_done = $obj->nb_gen_done; |
|
402
|
|
|
$this->nb_gen_max = $obj->nb_gen_max; |
|
403
|
|
|
$this->usenewprice = $obj->usenewprice; |
|
404
|
|
|
$this->auto_validate = $obj->auto_validate; |
|
405
|
|
|
$this->generate_pdf = $obj->generate_pdf; |
|
406
|
|
|
|
|
407
|
|
|
// Multicurrency |
|
408
|
|
|
$this->fk_multicurrency = $obj->fk_multicurrency; |
|
409
|
|
|
$this->multicurrency_code = $obj->multicurrency_code; |
|
410
|
|
|
$this->multicurrency_tx = $obj->multicurrency_tx; |
|
411
|
|
|
$this->multicurrency_total_ht = $obj->multicurrency_total_ht; |
|
412
|
|
|
$this->multicurrency_total_tva = $obj->multicurrency_total_tva; |
|
413
|
|
|
$this->multicurrency_total_ttc = $obj->multicurrency_total_ttc; |
|
414
|
|
|
|
|
415
|
|
|
if ($this->statut == self::STATUS_DRAFT) $this->brouillon = 1; |
|
416
|
|
|
|
|
417
|
|
|
// Retreive all extrafield |
|
418
|
|
|
// fetch optionals attributes and labels |
|
419
|
|
|
$this->fetch_optionals(); |
|
420
|
|
|
|
|
421
|
|
|
/* |
|
422
|
|
|
* Lines |
|
423
|
|
|
*/ |
|
424
|
|
|
$result=$this->fetch_lines(); |
|
425
|
|
|
if ($result < 0) |
|
426
|
|
|
{ |
|
427
|
|
|
$this->error=$this->db->lasterror(); |
|
428
|
|
|
return -3; |
|
429
|
|
|
} |
|
430
|
|
|
return 1; |
|
431
|
|
|
} |
|
432
|
|
|
else |
|
433
|
|
|
{ |
|
434
|
|
|
$this->error='Bill with id '.$rowid.' or ref '.$ref.' not found sql='.$sql; |
|
435
|
|
|
dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR); |
|
436
|
|
|
return -2; |
|
437
|
|
|
} |
|
438
|
|
|
} |
|
439
|
|
|
else |
|
440
|
|
|
{ |
|
441
|
|
|
$this->error=$this->db->error(); |
|
442
|
|
|
return -1; |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
|
|
447
|
|
|
/** |
|
448
|
|
|
* Create an array of invoice lines |
|
449
|
|
|
* |
|
450
|
|
|
* @return int >0 if OK, <0 if KO |
|
451
|
|
|
*/ |
|
452
|
|
|
function getLinesArray() |
|
453
|
|
|
{ |
|
454
|
|
|
return $this->fetch_lines(); |
|
455
|
|
|
} |
|
456
|
|
|
|
|
457
|
|
|
|
|
458
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
|
459
|
|
|
/** |
|
460
|
|
|
* Recupere les lignes de factures predefinies dans this->lines |
|
461
|
|
|
* |
|
462
|
|
|
* @return int 1 if OK, < 0 if KO |
|
463
|
|
|
*/ |
|
464
|
|
|
function fetch_lines() |
|
465
|
|
|
{ |
|
466
|
|
|
// phpcs:enable |
|
467
|
|
|
$this->lines=array(); |
|
468
|
|
|
|
|
469
|
|
|
// Retreive all extrafield for line |
|
470
|
|
|
// fetch optionals attributes and labels |
|
471
|
|
|
require_once DOL_DOCUMENT_ROOT.'/core/class/extrafields.class.php'; |
|
472
|
|
|
$extrafieldsline=new ExtraFields($this->db); |
|
473
|
|
|
$extrafieldsline=$extrafieldsline->fetch_name_optionals_label('facturedet_rec',true); |
|
474
|
|
|
|
|
475
|
|
|
$sql = 'SELECT l.rowid, l.fk_product, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx, '; |
|
476
|
|
|
$sql.= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,'; |
|
477
|
|
|
$sql.= ' l.info_bits, l.date_start_fill, l.date_end_fill, l.total_ht, l.total_tva, l.total_ttc,'; |
|
478
|
|
|
//$sql.= ' l.situation_percent, l.fk_prev_id,'; |
|
479
|
|
|
//$sql.= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise_percent, l.fk_remise_except, l.subprice,'; |
|
480
|
|
|
$sql.= ' l.rang, l.special_code,'; |
|
481
|
|
|
//$sql.= ' l.info_bits, l.total_ht, l.total_tva, l.total_localtax1, l.total_localtax2, l.total_ttc, l.fk_code_ventilation, l.fk_product_fournisseur_price as fk_fournprice, l.buy_price_ht as pa_ht,'; |
|
482
|
|
|
$sql.= ' l.fk_unit, l.fk_contract_line,'; |
|
483
|
|
|
$sql.= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,'; |
|
484
|
|
|
$sql.= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc'; |
|
485
|
|
|
$sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l'; |
|
486
|
|
|
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid'; |
|
487
|
|
|
$sql.= ' WHERE l.fk_facture = '.$this->id; |
|
488
|
|
|
$sql.= ' ORDER BY l.rang'; |
|
489
|
|
|
|
|
490
|
|
|
dol_syslog('FactureRec::fetch_lines', LOG_DEBUG); |
|
491
|
|
|
$result = $this->db->query($sql); |
|
492
|
|
|
if ($result) |
|
493
|
|
|
{ |
|
494
|
|
|
$num = $this->db->num_rows($result); |
|
495
|
|
|
$i = 0; |
|
496
|
|
|
while ($i < $num) |
|
497
|
|
|
{ |
|
498
|
|
|
$objp = $this->db->fetch_object($result); |
|
499
|
|
|
$line = new FactureLigneRec($this->db); |
|
500
|
|
|
|
|
501
|
|
|
$line->id = $objp->rowid; |
|
502
|
|
|
$line->rowid = $objp->rowid; |
|
503
|
|
|
$line->desc = $objp->description; // Description line |
|
504
|
|
|
$line->description = $objp->description; // Description line |
|
505
|
|
|
$line->product_type = $objp->product_type; // Type of line |
|
506
|
|
|
$line->ref = $objp->product_ref; // Ref product |
|
507
|
|
|
$line->product_ref = $objp->product_ref; // Ref product |
|
508
|
|
|
$line->libelle = $objp->product_label; // deprecated |
|
509
|
|
|
$line->product_label = $objp->product_label; // Label product |
|
510
|
|
|
$line->product_desc = $objp->product_desc; // Description product |
|
511
|
|
|
$line->fk_product_type = $objp->fk_product_type; // Type of product |
|
512
|
|
|
$line->qty = $objp->qty; |
|
513
|
|
|
$line->subprice = $objp->subprice; |
|
514
|
|
|
|
|
515
|
|
|
$line->label = $objp->custom_label; // @deprecated |
|
516
|
|
|
|
|
517
|
|
|
$line->vat_src_code = $objp->vat_src_code; |
|
518
|
|
|
$line->tva_tx = $objp->tva_tx; |
|
519
|
|
|
$line->localtax1_tx = $objp->localtax1_tx; |
|
520
|
|
|
$line->localtax2_tx = $objp->localtax2_tx; |
|
521
|
|
|
$line->localtax1_type = $objp->localtax1_type; |
|
522
|
|
|
$line->localtax2_type = $objp->localtax2_type; |
|
523
|
|
|
$line->remise_percent = $objp->remise_percent; |
|
524
|
|
|
$line->fk_remise_except = $objp->fk_remise_except; |
|
525
|
|
|
$line->fk_product = $objp->fk_product; |
|
526
|
|
|
$line->date_start_fill = $objp->date_start_fill; |
|
527
|
|
|
$line->date_end_fill = $objp->date_end_fill; |
|
528
|
|
|
$line->info_bits = $objp->info_bits; |
|
529
|
|
|
$line->total_ht = $objp->total_ht; |
|
530
|
|
|
$line->total_tva = $objp->total_tva; |
|
531
|
|
|
$line->total_ttc = $objp->total_ttc; |
|
532
|
|
|
$line->code_ventilation = $objp->fk_code_ventilation; |
|
533
|
|
|
$line->rang = $objp->rang; |
|
534
|
|
|
$line->special_code = $objp->special_code; |
|
535
|
|
|
$line->fk_unit = $objp->fk_unit; |
|
536
|
|
|
$line->fk_contract_line = $objp->fk_contract_line; |
|
537
|
|
|
|
|
538
|
|
|
// Ne plus utiliser |
|
539
|
|
|
$line->price = $objp->price; |
|
540
|
|
|
$line->remise = $objp->remise; |
|
541
|
|
|
|
|
542
|
|
|
$extralabelsline = $line->fetch_optionals($line->id); |
|
543
|
|
|
|
|
544
|
|
|
// Multicurrency |
|
545
|
|
|
$line->fk_multicurrency = $objp->fk_multicurrency; |
|
546
|
|
|
$line->multicurrency_code = $objp->multicurrency_code; |
|
547
|
|
|
$line->multicurrency_subprice = $objp->multicurrency_subprice; |
|
548
|
|
|
$line->multicurrency_total_ht = $objp->multicurrency_total_ht; |
|
549
|
|
|
$line->multicurrency_total_tva = $objp->multicurrency_total_tva; |
|
550
|
|
|
$line->multicurrency_total_ttc = $objp->multicurrency_total_ttc; |
|
551
|
|
|
|
|
552
|
|
|
$this->lines[$i] = $line; |
|
553
|
|
|
|
|
554
|
|
|
$i++; |
|
555
|
|
|
} |
|
556
|
|
|
|
|
557
|
|
|
$this->db->free($result); |
|
558
|
|
|
return 1; |
|
559
|
|
|
} |
|
560
|
|
|
else |
|
561
|
|
|
{ |
|
562
|
|
|
$this->error=$this->db->lasterror(); |
|
563
|
|
|
return -3; |
|
564
|
|
|
} |
|
565
|
|
|
} |
|
566
|
|
|
|
|
567
|
|
|
|
|
568
|
|
|
/** |
|
569
|
|
|
* Delete template invoice |
|
570
|
|
|
* |
|
571
|
|
|
* @param User $user User that delete. |
|
572
|
|
|
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers |
|
573
|
|
|
* @param int $idwarehouse Id warehouse to use for stock change. |
|
574
|
|
|
* @return int <0 if KO, >0 if OK |
|
575
|
|
|
*/ |
|
576
|
|
|
function delete(User $user, $notrigger=0, $idwarehouse=-1) |
|
577
|
|
|
{ |
|
578
|
|
|
$rowid=$this->id; |
|
579
|
|
|
|
|
580
|
|
|
dol_syslog(get_class($this)."::delete rowid=".$rowid, LOG_DEBUG); |
|
581
|
|
|
|
|
582
|
|
|
$error=0; |
|
583
|
|
|
$this->db->begin(); |
|
584
|
|
|
|
|
585
|
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = ".$rowid; |
|
586
|
|
|
dol_syslog($sql); |
|
587
|
|
|
if ($this->db->query($sql)) |
|
588
|
|
|
{ |
|
589
|
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_rec WHERE rowid = ".$rowid; |
|
590
|
|
|
dol_syslog($sql); |
|
591
|
|
|
if ($this->db->query($sql)) |
|
592
|
|
|
{ |
|
593
|
|
|
// Delete linked object |
|
594
|
|
|
$res = $this->deleteObjectLinked(); |
|
595
|
|
|
if ($res < 0) $error=-3; |
|
596
|
|
|
} |
|
597
|
|
|
else |
|
598
|
|
|
{ |
|
599
|
|
|
$this->error=$this->db->lasterror(); |
|
600
|
|
|
$error=-1; |
|
601
|
|
|
} |
|
602
|
|
|
} |
|
603
|
|
|
else |
|
604
|
|
|
{ |
|
605
|
|
|
$this->error=$this->db->lasterror(); |
|
606
|
|
|
$error=-2; |
|
607
|
|
|
} |
|
608
|
|
|
|
|
609
|
|
|
if (! $error) |
|
610
|
|
|
{ |
|
611
|
|
|
$this->db->commit(); |
|
612
|
|
|
return 1; |
|
613
|
|
|
} |
|
614
|
|
|
else |
|
615
|
|
|
{ |
|
616
|
|
|
$this->db->rollback(); |
|
617
|
|
|
return $error; |
|
618
|
|
|
} |
|
619
|
|
|
} |
|
620
|
|
|
|
|
621
|
|
|
|
|
622
|
|
|
/** |
|
623
|
|
|
* Add a line to invoice |
|
624
|
|
|
* |
|
625
|
|
|
* @param string $desc Description de la ligne |
|
626
|
|
|
* @param double $pu_ht Prix unitaire HT (> 0 even for credit note) |
|
627
|
|
|
* @param double $qty Quantite |
|
628
|
|
|
* @param double $txtva Taux de tva force, sinon -1 |
|
629
|
|
|
* @param double $txlocaltax1 Local tax 1 rate (deprecated) |
|
630
|
|
|
* @param double $txlocaltax2 Local tax 2 rate (deprecated) |
|
631
|
|
|
* @param int $fk_product Id du produit/service predefini |
|
632
|
|
|
* @param double $remise_percent Pourcentage de remise de la ligne |
|
633
|
|
|
* @param string $price_base_type HT or TTC |
|
634
|
|
|
* @param int $info_bits VAT npr or not ? |
|
635
|
|
|
* @param int $fk_remise_except Id remise |
|
636
|
|
|
* @param double $pu_ttc Prix unitaire TTC (> 0 even for credit note) |
|
637
|
|
|
* @param int $type Type of line (0=product, 1=service) |
|
638
|
|
|
* @param int $rang Position of line |
|
639
|
|
|
* @param int $special_code Special code |
|
640
|
|
|
* @param string $label Label of the line |
|
641
|
|
|
* @param string $fk_unit Unit |
|
642
|
|
|
* @param double $pu_ht_devise Unit price in currency |
|
643
|
|
|
* @param int $date_start_fill 1=Flag to fill start date when generating invoice |
|
644
|
|
|
* @param int $date_end_fill 1=Flag to fill end date when generating invoice |
|
645
|
|
|
* @return int <0 if KO, Id of line if OK |
|
646
|
|
|
*/ |
|
647
|
|
|
function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type='HT', $info_bits=0, $fk_remise_except='', $pu_ttc=0, $type=0, $rang=-1, $special_code=0, $label='', $fk_unit=null, $pu_ht_devise=0, $date_start_fill=0, $date_end_fill=0) |
|
648
|
|
|
{ |
|
649
|
|
|
global $mysoc; |
|
650
|
|
|
|
|
651
|
|
|
$facid=$this->id; |
|
652
|
|
|
|
|
653
|
|
|
dol_syslog(get_class($this)."::addline facid=$facid,desc=$desc,pu_ht=$pu_ht,qty=$qty,txtva=$txtva,txlocaltax1=$txlocaltax1,txlocaltax2=$txlocaltax2,fk_product=$fk_product,remise_percent=$remise_percent,info_bits=$info_bits,fk_remise_except=$fk_remise_except,price_base_type=$price_base_type,pu_ttc=$pu_ttc,type=$type,fk_unit=$fk_unit,pu_ht_devise=$pu_ht_devise,date_start_fill=$date_start_fill,date_end_fill=$date_end_fill", LOG_DEBUG); |
|
654
|
|
|
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; |
|
655
|
|
|
|
|
656
|
|
|
// Check parameters |
|
657
|
|
|
if ($type < 0) return -1; |
|
658
|
|
|
|
|
659
|
|
|
$localtaxes_type=getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc); |
|
660
|
|
|
|
|
661
|
|
|
// Clean vat code |
|
662
|
|
|
$vat_src_code=''; |
|
663
|
|
|
if (preg_match('/\((.*)\)/', $txtva, $reg)) |
|
664
|
|
|
{ |
|
665
|
|
|
$vat_src_code = $reg[1]; |
|
666
|
|
|
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate. |
|
667
|
|
|
} |
|
668
|
|
|
|
|
669
|
|
|
if ($this->brouillon) |
|
670
|
|
|
{ |
|
671
|
|
|
// Clean parameters |
|
672
|
|
|
$remise_percent=price2num($remise_percent); |
|
673
|
|
|
if (empty($remise_percent)) $remise_percent=0; |
|
674
|
|
|
$qty=price2num($qty); |
|
675
|
|
|
$pu_ht = price2num($pu_ht); |
|
676
|
|
|
$pu_ttc = price2num($pu_ttc); |
|
677
|
|
|
$txtva = price2num($txtva); |
|
678
|
|
|
$txlocaltax1 = price2num($txlocaltax1); |
|
679
|
|
|
$txlocaltax2 = price2num($txlocaltax2); |
|
680
|
|
|
if (empty($txtva)) $txtva=0; |
|
681
|
|
|
if (empty($txlocaltax1)) $txlocaltax1=0; |
|
682
|
|
|
if (empty($txlocaltax2)) $txlocaltax2=0; |
|
683
|
|
|
if (empty($info_bits)) $info_bits=0; |
|
684
|
|
|
|
|
685
|
|
|
if ($price_base_type=='HT') |
|
686
|
|
|
{ |
|
687
|
|
|
$pu=$pu_ht; |
|
688
|
|
|
} |
|
689
|
|
|
else |
|
690
|
|
|
{ |
|
691
|
|
|
$pu=$pu_ttc; |
|
692
|
|
|
} |
|
693
|
|
|
|
|
694
|
|
|
// Calcul du total TTC et de la TVA pour la ligne a partir de |
|
695
|
|
|
// qty, pu, remise_percent et txtva |
|
696
|
|
|
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker |
|
697
|
|
|
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. |
|
698
|
|
|
|
|
699
|
|
|
$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise); |
|
700
|
|
|
$total_ht = $tabprice[0]; |
|
701
|
|
|
$total_tva = $tabprice[1]; |
|
702
|
|
|
$total_ttc = $tabprice[2]; |
|
703
|
|
|
$total_localtax1=$tabprice[9]; |
|
704
|
|
|
$total_localtax2=$tabprice[10]; |
|
705
|
|
|
$pu_ht = $tabprice[3]; |
|
706
|
|
|
|
|
707
|
|
|
// MultiCurrency |
|
708
|
|
|
$multicurrency_total_ht = $tabprice[16]; |
|
709
|
|
|
$multicurrency_total_tva = $tabprice[17]; |
|
710
|
|
|
$multicurrency_total_ttc = $tabprice[18]; |
|
711
|
|
|
$pu_ht_devise = $tabprice[19]; |
|
712
|
|
|
|
|
713
|
|
|
$product_type=$type; |
|
714
|
|
|
if ($fk_product) |
|
715
|
|
|
{ |
|
716
|
|
|
$product=new Product($this->db); |
|
717
|
|
|
$result=$product->fetch($fk_product); |
|
718
|
|
|
$product_type=$product->type; |
|
719
|
|
|
} |
|
720
|
|
|
|
|
721
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec ("; |
|
722
|
|
|
$sql.= "fk_facture"; |
|
723
|
|
|
$sql.= ", label"; |
|
724
|
|
|
$sql.= ", description"; |
|
725
|
|
|
$sql.= ", price"; |
|
726
|
|
|
$sql.= ", qty"; |
|
727
|
|
|
$sql.= ", tva_tx"; |
|
728
|
|
|
$sql.= ", vat_src_code"; |
|
729
|
|
|
$sql.= ", localtax1_tx"; |
|
730
|
|
|
$sql.= ", localtax1_type"; |
|
731
|
|
|
$sql.= ", localtax2_tx"; |
|
732
|
|
|
$sql.= ", localtax2_type"; |
|
733
|
|
|
$sql.= ", fk_product"; |
|
734
|
|
|
$sql.= ", product_type"; |
|
735
|
|
|
$sql.= ", remise_percent"; |
|
736
|
|
|
$sql.= ", subprice"; |
|
737
|
|
|
$sql.= ", remise"; |
|
738
|
|
|
$sql.= ", total_ht"; |
|
739
|
|
|
$sql.= ", total_tva"; |
|
740
|
|
|
$sql.= ", total_localtax1"; |
|
741
|
|
|
$sql.= ", total_localtax2"; |
|
742
|
|
|
$sql.= ", total_ttc"; |
|
743
|
|
|
$sql.= ", date_start_fill"; |
|
744
|
|
|
$sql.= ", date_end_fill"; |
|
745
|
|
|
$sql.= ", info_bits"; |
|
746
|
|
|
$sql.= ", rang"; |
|
747
|
|
|
$sql.= ", special_code"; |
|
748
|
|
|
$sql.= ", fk_unit"; |
|
749
|
|
|
$sql.= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc'; |
|
750
|
|
|
$sql.= ") VALUES ("; |
|
751
|
|
|
$sql.= "'".$facid."'"; |
|
752
|
|
|
$sql.= ", ".(! empty($label)?"'".$this->db->escape($label)."'":"null"); |
|
753
|
|
|
$sql.= ", '".$this->db->escape($desc)."'"; |
|
754
|
|
|
$sql.= ", ".price2num($pu_ht); |
|
755
|
|
|
$sql.= ", ".price2num($qty); |
|
756
|
|
|
$sql.= ", ".price2num($txtva); |
|
757
|
|
|
$sql.= ", '".$this->db->escape($vat_src_code)."'"; |
|
758
|
|
|
$sql.= ", ".price2num($txlocaltax1); |
|
759
|
|
|
$sql.= ", '".$this->db->escape($localtaxes_type[0])."'"; |
|
760
|
|
|
$sql.= ", ".price2num($txlocaltax2); |
|
761
|
|
|
$sql.= ", '".$this->db->escape($localtaxes_type[2])."'"; |
|
762
|
|
|
$sql.= ", ".(! empty($fk_product)?"'".$fk_product."'":"null"); |
|
763
|
|
|
$sql.= ", ".$product_type; |
|
764
|
|
|
$sql.= ", ".price2num($remise_percent); |
|
765
|
|
|
$sql.= ", ".price2num($pu_ht); |
|
766
|
|
|
$sql.= ", null"; |
|
767
|
|
|
$sql.= ", ".price2num($total_ht); |
|
768
|
|
|
$sql.= ", ".price2num($total_tva); |
|
769
|
|
|
$sql.= ", ".price2num($total_localtax1); |
|
770
|
|
|
$sql.= ", ".price2num($total_localtax2); |
|
771
|
|
|
$sql.= ", ".price2num($total_ttc); |
|
772
|
|
|
$sql.= ", ".(int) $date_start_fill; |
|
773
|
|
|
$sql.= ", ".(int) $date_end_fill; |
|
774
|
|
|
$sql.= ", ".$info_bits; |
|
775
|
|
|
$sql.= ", ".$rang; |
|
776
|
|
|
$sql.= ", ".$special_code; |
|
777
|
|
|
$sql.= ", ".($fk_unit?"'".$this->db->escape($fk_unit)."'":"null"); |
|
778
|
|
|
$sql.= ", ".(int) $this->fk_multicurrency; |
|
779
|
|
|
$sql.= ", '".$this->db->escape($this->multicurrency_code)."'"; |
|
780
|
|
|
$sql.= ", ".price2num($pu_ht_devise); |
|
781
|
|
|
$sql.= ", ".price2num($multicurrency_total_ht); |
|
782
|
|
|
$sql.= ", ".price2num($multicurrency_total_tva); |
|
783
|
|
|
$sql.= ", ".price2num($multicurrency_total_ttc); |
|
784
|
|
|
$sql.= ")"; |
|
785
|
|
|
|
|
786
|
|
|
dol_syslog(get_class($this)."::addline", LOG_DEBUG); |
|
787
|
|
|
if ($this->db->query($sql)) |
|
788
|
|
|
{ |
|
789
|
|
|
$lineId = $this->db->last_insert_id(MAIN_DB_PREFIX."facturedet_rec"); |
|
790
|
|
|
$this->id=$facid; |
|
791
|
|
|
$this->update_price(); |
|
792
|
|
|
return $lineId; |
|
793
|
|
|
} |
|
794
|
|
|
else |
|
795
|
|
|
{ |
|
796
|
|
|
$this->error=$this->db->lasterror(); |
|
797
|
|
|
return -1; |
|
798
|
|
|
} |
|
799
|
|
|
} |
|
800
|
|
|
} |
|
801
|
|
|
|
|
802
|
|
|
/** |
|
803
|
|
|
* Update a line to invoice |
|
804
|
|
|
* |
|
805
|
|
|
* @param int $rowid Id of line to update |
|
806
|
|
|
* @param string $desc Description de la ligne |
|
807
|
|
|
* @param double $pu_ht Prix unitaire HT (> 0 even for credit note) |
|
808
|
|
|
* @param double $qty Quantite |
|
809
|
|
|
* @param double $txtva Taux de tva force, sinon -1 |
|
810
|
|
|
* @param double $txlocaltax1 Local tax 1 rate (deprecated) |
|
811
|
|
|
* @param double $txlocaltax2 Local tax 2 rate (deprecated) |
|
812
|
|
|
* @param int $fk_product Id du produit/service predefini |
|
813
|
|
|
* @param double $remise_percent Pourcentage de remise de la ligne |
|
814
|
|
|
* @param string $price_base_type HT or TTC |
|
815
|
|
|
* @param int $info_bits Bits de type de lignes |
|
816
|
|
|
* @param int $fk_remise_except Id remise |
|
817
|
|
|
* @param double $pu_ttc Prix unitaire TTC (> 0 even for credit note) |
|
818
|
|
|
* @param int $type Type of line (0=product, 1=service) |
|
819
|
|
|
* @param int $rang Position of line |
|
820
|
|
|
* @param int $special_code Special code |
|
821
|
|
|
* @param string $label Label of the line |
|
822
|
|
|
* @param string $fk_unit Unit |
|
823
|
|
|
* @param double $pu_ht_devise Unit price in currency |
|
824
|
|
|
* @param int $notrigger disable line update trigger |
|
825
|
|
|
* @param int $date_start_fill 1=Flag to fill start date when generating invoice |
|
826
|
|
|
* @param int $date_end_fill 1=Flag to fill end date when generating invoice |
|
827
|
|
|
* @return int <0 if KO, Id of line if OK |
|
828
|
|
|
*/ |
|
829
|
|
|
function updateline($rowid, $desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $remise_percent=0, $price_base_type='HT', $info_bits=0, $fk_remise_except='', $pu_ttc=0, $type=0, $rang=-1, $special_code=0, $label='', $fk_unit=null, $pu_ht_devise = 0, $notrigger=0, $date_start_fill=0, $date_end_fill=0) |
|
830
|
|
|
{ |
|
831
|
|
|
global $mysoc; |
|
832
|
|
|
|
|
833
|
|
|
$facid=$this->id; |
|
834
|
|
|
|
|
835
|
|
|
dol_syslog(get_class($this)."::updateline facid=".$facid." rowid=$rowid, desc=$desc, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, fk_product=$fk_product, remise_percent=$remise_percent, info_bits=$info_bits, fk_remise_except=$fk_remise_except, price_base_type=$price_base_type, pu_ttc=$pu_ttc, type=$type, fk_unit=$fk_unit, pu_ht_devise=$pu_ht_devise", LOG_DEBUG); |
|
836
|
|
|
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; |
|
837
|
|
|
|
|
838
|
|
|
// Clean parameters |
|
839
|
|
|
if (empty($remise_percent)) $remise_percent = 0; |
|
840
|
|
|
|
|
841
|
|
|
// Check parameters |
|
842
|
|
|
if ($type < 0) return -1; |
|
843
|
|
|
|
|
844
|
|
|
$localtaxes_type=getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc); |
|
845
|
|
|
|
|
846
|
|
|
// Clean vat code |
|
847
|
|
|
$vat_src_code=''; |
|
848
|
|
|
if (preg_match('/\((.*)\)/', $txtva, $reg)) |
|
849
|
|
|
{ |
|
850
|
|
|
$vat_src_code = $reg[1]; |
|
851
|
|
|
$txtva = preg_replace('/\s*\(.*\)/', '', $txtva); // Remove code into vatrate. |
|
852
|
|
|
} |
|
853
|
|
|
|
|
854
|
|
|
if ($this->brouillon) |
|
855
|
|
|
{ |
|
856
|
|
|
// Clean parameters |
|
857
|
|
|
$remise_percent=price2num($remise_percent); |
|
858
|
|
|
$qty=price2num($qty); |
|
859
|
|
|
if (empty($info_bits)) $info_bits=0; |
|
860
|
|
|
$pu_ht=price2num($pu_ht); |
|
861
|
|
|
$pu_ttc=price2num($pu_ttc); |
|
862
|
|
|
$txtva=price2num($txtva); |
|
863
|
|
|
$txlocaltax1 = price2num($txlocaltax1); |
|
864
|
|
|
$txlocaltax2 = price2num($txlocaltax2); |
|
865
|
|
|
if (empty($txlocaltax1)) $txlocaltax1=0; |
|
866
|
|
|
if (empty($txlocaltax2)) $txlocaltax2=0; |
|
867
|
|
|
|
|
868
|
|
|
if (empty($this->multicurrency_subprice)) $this->multicurrency_subprice=0; |
|
869
|
|
|
if (empty($this->multicurrency_total_ht)) $this->multicurrency_total_ht=0; |
|
870
|
|
|
if (empty($this->multicurrency_total_tva)) $this->multicurrency_total_tva=0; |
|
871
|
|
|
if (empty($this->multicurrency_total_ttc)) $this->multicurrency_total_ttc=0; |
|
872
|
|
|
|
|
873
|
|
|
if ($price_base_type=='HT') |
|
874
|
|
|
{ |
|
875
|
|
|
$pu=$pu_ht; |
|
876
|
|
|
} |
|
877
|
|
|
else |
|
878
|
|
|
{ |
|
879
|
|
|
$pu=$pu_ttc; |
|
880
|
|
|
} |
|
881
|
|
|
|
|
882
|
|
|
// Calcul du total TTC et de la TVA pour la ligne a partir de |
|
883
|
|
|
// qty, pu, remise_percent et txtva |
|
884
|
|
|
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker |
|
885
|
|
|
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva. |
|
886
|
|
|
$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, 100, $this->multicurrency_tx, $pu_ht_devise); |
|
887
|
|
|
|
|
888
|
|
|
$total_ht = $tabprice[0]; |
|
889
|
|
|
$total_tva = $tabprice[1]; |
|
890
|
|
|
$total_ttc = $tabprice[2]; |
|
891
|
|
|
$total_localtax1=$tabprice[9]; |
|
892
|
|
|
$total_localtax2=$tabprice[10]; |
|
893
|
|
|
$pu_ht = $tabprice[3]; |
|
894
|
|
|
$pu_tva = $tabprice[4]; |
|
895
|
|
|
$pu_ttc = $tabprice[5]; |
|
896
|
|
|
|
|
897
|
|
|
// MultiCurrency |
|
898
|
|
|
$multicurrency_total_ht = $tabprice[16]; |
|
899
|
|
|
$multicurrency_total_tva = $tabprice[17]; |
|
900
|
|
|
$multicurrency_total_ttc = $tabprice[18]; |
|
901
|
|
|
$pu_ht_devise = $tabprice[19]; |
|
902
|
|
|
|
|
903
|
|
|
$product_type=$type; |
|
904
|
|
|
if ($fk_product) |
|
905
|
|
|
{ |
|
906
|
|
|
$product=new Product($this->db); |
|
907
|
|
|
$result=$product->fetch($fk_product); |
|
908
|
|
|
$product_type=$product->type; |
|
909
|
|
|
} |
|
910
|
|
|
|
|
911
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET "; |
|
912
|
|
|
$sql.= "fk_facture = '".$facid."'"; |
|
913
|
|
|
$sql.= ", label=".(! empty($label)?"'".$this->db->escape($label)."'":"null"); |
|
914
|
|
|
$sql.= ", description='".$this->db->escape($desc)."'"; |
|
915
|
|
|
$sql.= ", price=".price2num($pu_ht); |
|
916
|
|
|
$sql.= ", qty=".price2num($qty); |
|
917
|
|
|
$sql.= ", tva_tx=".price2num($txtva); |
|
918
|
|
|
$sql.= ", vat_src_code='".$this->db->escape($vat_src_code)."'"; |
|
919
|
|
|
$sql.= ", localtax1_tx=".$txlocaltax1; |
|
920
|
|
|
$sql.= ", localtax1_type='".$this->db->escape($localtaxes_type[0])."'"; |
|
921
|
|
|
$sql.= ", localtax2_tx=".$txlocaltax2; |
|
922
|
|
|
$sql.= ", localtax2_type='".$this->db->escape($localtaxes_type[2])."'"; |
|
923
|
|
|
$sql.= ", fk_product=".(! empty($fk_product)?"'".$fk_product."'":"null"); |
|
924
|
|
|
$sql.= ", product_type=".$product_type; |
|
925
|
|
|
$sql.= ", remise_percent='".price2num($remise_percent)."'"; |
|
926
|
|
|
$sql.= ", subprice='".price2num($pu_ht)."'"; |
|
927
|
|
|
$sql.= ", total_ht='".price2num($total_ht)."'"; |
|
928
|
|
|
$sql.= ", total_tva='".price2num($total_tva)."'"; |
|
929
|
|
|
$sql.= ", total_localtax1='".price2num($total_localtax1)."'"; |
|
930
|
|
|
$sql.= ", total_localtax2='".price2num($total_localtax2)."'"; |
|
931
|
|
|
$sql.= ", total_ttc='".price2num($total_ttc)."'"; |
|
932
|
|
|
$sql.= ", date_start_fill=".((int) $date_start_fill); |
|
933
|
|
|
$sql.= ", date_end_fill=".((int) $date_end_fill); |
|
934
|
|
|
$sql.= ", info_bits=".$info_bits; |
|
935
|
|
|
$sql.= ", rang=".$rang; |
|
936
|
|
|
$sql.= ", special_code=".$special_code; |
|
937
|
|
|
$sql.= ", fk_unit=".($fk_unit?"'".$this->db->escape($fk_unit)."'":"null"); |
|
938
|
|
|
$sql.= ', multicurrency_subprice = '.$pu_ht_devise; |
|
939
|
|
|
$sql.= ', multicurrency_total_ht = '.$multicurrency_total_ht; |
|
940
|
|
|
$sql.= ', multicurrency_total_tva = '.$multicurrency_total_tva; |
|
941
|
|
|
$sql.= ', multicurrency_total_ttc = '.$multicurrency_total_ttc; |
|
942
|
|
|
$sql.= " WHERE rowid = ".$rowid; |
|
943
|
|
|
|
|
944
|
|
|
dol_syslog(get_class($this)."::updateline", LOG_DEBUG); |
|
945
|
|
|
if ($this->db->query($sql)) |
|
946
|
|
|
{ |
|
947
|
|
|
$this->id=$facid; |
|
948
|
|
|
$this->update_price(); |
|
949
|
|
|
return 1; |
|
950
|
|
|
} |
|
951
|
|
|
else |
|
952
|
|
|
{ |
|
953
|
|
|
$this->error=$this->db->lasterror(); |
|
954
|
|
|
return -1; |
|
955
|
|
|
} |
|
956
|
|
|
} |
|
957
|
|
|
} |
|
958
|
|
|
|
|
959
|
|
|
|
|
960
|
|
|
/** |
|
961
|
|
|
* Return the next date of |
|
962
|
|
|
* |
|
963
|
|
|
* @return timestamp false if KO, timestamp if OK |
|
964
|
|
|
*/ |
|
965
|
|
|
function getNextDate() |
|
966
|
|
|
{ |
|
967
|
|
|
if (empty($this->date_when)) return false; |
|
968
|
|
|
return dol_time_plus_duree($this->date_when, $this->frequency, $this->unit_frequency); |
|
969
|
|
|
} |
|
970
|
|
|
|
|
971
|
|
|
/** |
|
972
|
|
|
* Return if maximum number of generation is reached |
|
973
|
|
|
* |
|
974
|
|
|
* @return boolean False by default, True if maximum number of generation is reached |
|
975
|
|
|
*/ |
|
976
|
|
|
function isMaxNbGenReached() |
|
977
|
|
|
{ |
|
978
|
|
|
$ret = false; |
|
979
|
|
|
if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) $ret = true; |
|
980
|
|
|
return $ret; |
|
981
|
|
|
} |
|
982
|
|
|
|
|
983
|
|
|
/** |
|
984
|
|
|
* Format string to output with by striking the string if max number of generation was reached |
|
985
|
|
|
* |
|
986
|
|
|
* @param string $ret Default value to output |
|
987
|
|
|
* @return boolean False by default, True if maximum number of generation is reached |
|
988
|
|
|
*/ |
|
989
|
|
|
function strikeIfMaxNbGenReached($ret) |
|
990
|
|
|
{ |
|
991
|
|
|
// Special case to strike the date |
|
992
|
|
|
return ($this->isMaxNbGenReached()?'<strike>':'').$ret.($this->isMaxNbGenReached()?'</strike>':''); |
|
993
|
|
|
} |
|
994
|
|
|
|
|
995
|
|
|
/** |
|
996
|
|
|
* Create all recurrents invoices (for all entities if multicompany is used). |
|
997
|
|
|
* A result may also be provided into this->output. |
|
998
|
|
|
* |
|
999
|
|
|
* WARNING: This method change temporarly context $conf->entity to be in correct context for each recurring invoice found. |
|
1000
|
|
|
* |
|
1001
|
|
|
* @param int $restrictioninvoiceid 0=All qualified template invoices found. > 0 = restrict action on invoice ID |
|
1002
|
|
|
* @param int $forcevalidation 1=Force validation of invoice whatever is template auto_validate flag. |
|
1003
|
|
|
* @return int 0 if OK, < 0 if KO (this function is used also by cron so only 0 is OK) |
|
1004
|
|
|
*/ |
|
1005
|
|
|
function createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0) |
|
1006
|
|
|
{ |
|
1007
|
|
|
global $conf, $langs, $db, $user, $hookmanager; |
|
1008
|
|
|
|
|
1009
|
|
|
$error=0; |
|
1010
|
|
|
$nb_create=0; |
|
1011
|
|
|
|
|
1012
|
|
|
// Load translation files required by the page |
|
1013
|
|
|
$langs->loadLangs(array("main","bills")); |
|
1014
|
|
|
|
|
1015
|
|
|
$now = dol_now(); |
|
1016
|
|
|
$tmparray=dol_getdate($now); |
|
1017
|
|
|
$today = dol_mktime(23,59,59,$tmparray['mon'],$tmparray['mday'],$tmparray['year']); // Today is last second of current day |
|
1018
|
|
|
|
|
1019
|
|
|
dol_syslog("createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid." forcevalidation=".$forcevalidation); |
|
1020
|
|
|
|
|
1021
|
|
|
$sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_rec'; |
|
1022
|
|
|
$sql.= ' WHERE frequency > 0'; // A recurring invoice is an invoice with a frequency |
|
1023
|
|
|
$sql.= " AND (date_when IS NULL OR date_when <= '".$db->idate($today)."')"; |
|
1024
|
|
|
$sql.= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)'; |
|
1025
|
|
|
$sql.= ' AND suspended = 0'; |
|
1026
|
|
|
$sql.= ' AND entity = '.$conf->entity; // MUST STAY = $conf->entity here |
|
1027
|
|
|
if ($restrictioninvoiceid > 0) |
|
1028
|
|
|
$sql.=' AND rowid = '.$restrictioninvoiceid; |
|
1029
|
|
|
$sql.= $db->order('entity', 'ASC'); |
|
1030
|
|
|
//print $sql;exit; |
|
1031
|
|
|
$parameters = array( |
|
1032
|
|
|
'restrictioninvoiceid' => $restrictioninvoiceid, |
|
1033
|
|
|
'forcevalidation' => $forcevalidation, |
|
1034
|
|
|
); |
|
1035
|
|
|
$reshook = $hookmanager->executeHooks('beforeCreationOfRecurringInvoices', $parameters, $sql); // note that $sql might be modified by hooks |
|
1036
|
|
|
|
|
1037
|
|
|
$resql = $db->query($sql); |
|
1038
|
|
|
if ($resql) |
|
1039
|
|
|
{ |
|
1040
|
|
|
$i=0; |
|
1041
|
|
|
$num = $db->num_rows($resql); |
|
1042
|
|
|
|
|
1043
|
|
|
if ($num) |
|
1044
|
|
|
$this->output.=$langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n"; |
|
1045
|
|
|
else |
|
1046
|
|
|
$this->output.=$langs->trans("NoQualifiedRecurringInvoiceTemplateFound"); |
|
1047
|
|
|
|
|
1048
|
|
|
$saventity = $conf->entity; |
|
1049
|
|
|
|
|
1050
|
|
|
while ($i < $num) // Loop on each template invoice. If $num = 0, test is false at first pass. |
|
1051
|
|
|
{ |
|
1052
|
|
|
$line = $db->fetch_object($resql); |
|
1053
|
|
|
|
|
1054
|
|
|
$db->begin(); |
|
1055
|
|
|
|
|
1056
|
|
|
$invoiceidgenerated = 0; |
|
1057
|
|
|
|
|
1058
|
|
|
$facture = null; |
|
1059
|
|
|
$facturerec = new FactureRec($db); |
|
1060
|
|
|
$facturerec->fetch($line->rowid); |
|
1061
|
|
|
|
|
1062
|
|
|
if ($facturerec->id > 0) |
|
1063
|
|
|
{ |
|
1064
|
|
|
// Set entity context |
|
1065
|
|
|
$conf->entity = $facturerec->entity; |
|
1066
|
|
|
|
|
1067
|
|
|
dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity); |
|
1068
|
|
|
|
|
1069
|
|
|
$facture = new Facture($db); |
|
1070
|
|
|
$facture->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice |
|
1071
|
|
|
$facture->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice |
|
1072
|
|
|
|
|
1073
|
|
|
$facture->type = self::TYPE_STANDARD; |
|
1074
|
|
|
$facture->brouillon = 1; |
|
1075
|
|
|
$facture->date = (empty($facturerec->date_when)?$now:$facturerec->date_when); // We could also use dol_now here but we prefer date_when so invoice has real date when we would like even if we generate later. |
|
1076
|
|
|
$facture->socid = $facturerec->socid; |
|
1077
|
|
|
|
|
1078
|
|
|
$invoiceidgenerated = $facture->create($user); |
|
1079
|
|
|
if ($invoiceidgenerated <= 0) |
|
1080
|
|
|
{ |
|
1081
|
|
|
$this->errors = $facture->errors; |
|
1082
|
|
|
$this->error = $facture->error; |
|
1083
|
|
|
$error++; |
|
1084
|
|
|
} |
|
1085
|
|
|
if (! $error && ($facturerec->auto_validate || $forcevalidation)) |
|
1086
|
|
|
{ |
|
1087
|
|
|
$result = $facture->validate($user); |
|
1088
|
|
|
if ($result <= 0) |
|
1089
|
|
|
{ |
|
1090
|
|
|
$this->errors = $facture->errors; |
|
1091
|
|
|
$this->error = $facture->error; |
|
1092
|
|
|
$error++; |
|
1093
|
|
|
} |
|
1094
|
|
|
} |
|
1095
|
|
|
if (! $error && $facturerec->generate_pdf) |
|
1096
|
|
|
{ |
|
1097
|
|
|
// We refresh the object in order to have all necessary data (like date_lim_reglement) |
|
1098
|
|
|
$facture->fetch($facture->id); |
|
1099
|
|
|
$result = $facture->generateDocument($facturerec->modelpdf, $langs); |
|
1100
|
|
|
if ($result <= 0) |
|
1101
|
|
|
{ |
|
1102
|
|
|
$this->errors = $facture->errors; |
|
1103
|
|
|
$this->error = $facture->error; |
|
1104
|
|
|
$error++; |
|
1105
|
|
|
} |
|
1106
|
|
|
} |
|
1107
|
|
|
} |
|
1108
|
|
|
else |
|
1109
|
|
|
{ |
|
1110
|
|
|
$error++; |
|
1111
|
|
|
$this->error="Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity."\n"; |
|
1112
|
|
|
$this->errors[]="Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity; |
|
1113
|
|
|
dol_syslog("createRecurringInvoices Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity); |
|
1114
|
|
|
} |
|
1115
|
|
|
|
|
1116
|
|
|
if (! $error && $invoiceidgenerated >= 0) |
|
1117
|
|
|
{ |
|
1118
|
|
|
$db->commit("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref); |
|
1119
|
|
|
dol_syslog("createRecurringInvoices Process invoice template ".$facturerec->ref." is finished with a success generation"); |
|
1120
|
|
|
$nb_create++; |
|
1121
|
|
|
$this->output.=$langs->trans("InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref)."\n"; |
|
1122
|
|
|
} |
|
1123
|
|
|
else |
|
1124
|
|
|
{ |
|
1125
|
|
|
$db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref); |
|
1126
|
|
|
} |
|
1127
|
|
|
|
|
1128
|
|
|
$parameters = array( |
|
1129
|
|
|
'cpt' => $i, |
|
1130
|
|
|
'total' => $num, |
|
1131
|
|
|
'errorCount' => $error, |
|
1132
|
|
|
'invoiceidgenerated' => $invoiceidgenerated, |
|
1133
|
|
|
'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks. |
|
1134
|
|
|
'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks. |
|
1135
|
|
|
); |
|
1136
|
|
|
$reshook = $hookmanager->executeHooks('afterCreationOfRecurringInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null) |
|
1137
|
|
|
|
|
1138
|
|
|
$i++; |
|
1139
|
|
|
} |
|
1140
|
|
|
|
|
1141
|
|
|
$conf->entity = $saventity; // Restore entity context |
|
1142
|
|
|
} |
|
1143
|
|
|
else dol_print_error($db); |
|
1144
|
|
|
|
|
1145
|
|
|
$this->output=trim($this->output); |
|
1146
|
|
|
|
|
1147
|
|
|
return $error?$error:0; |
|
1148
|
|
|
} |
|
1149
|
|
|
|
|
1150
|
|
|
/** |
|
1151
|
|
|
* Return clicable name (with picto eventually) |
|
1152
|
|
|
* |
|
1153
|
|
|
* @param int $withpicto Add picto into link |
|
1154
|
|
|
* @param string $option Where point the link |
|
1155
|
|
|
* @param int $max Maxlength of ref |
|
1156
|
|
|
* @param int $short 1=Return just URL |
|
1157
|
|
|
* @param string $moretitle Add more text to title tooltip |
|
1158
|
|
|
* @param int $notooltip 1=Disable tooltip |
|
1159
|
|
|
* @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking |
|
1160
|
|
|
* @return string String with URL |
|
1161
|
|
|
*/ |
|
1162
|
|
|
function getNomUrl($withpicto=0,$option='',$max=0,$short=0,$moretitle='',$notooltip='',$save_lastsearch_value=-1) |
|
1163
|
|
|
{ |
|
1164
|
|
|
global $langs; |
|
1165
|
|
|
|
|
1166
|
|
|
$result=''; |
|
1167
|
|
|
|
|
1168
|
|
|
$label = '<u>' . $langs->trans("ShowInvoice") . '</u>'; |
|
1169
|
|
|
if (! empty($this->ref)) |
|
1170
|
|
|
$label .= '<br><b>'.$langs->trans('Ref') . ':</b> ' . $this->ref; |
|
1171
|
|
|
if (! empty($this->date_last_gen)) |
|
1172
|
|
|
$label .= '<br><b>'.$langs->trans('DateLastGeneration') . ':</b> ' . dol_print_date($this->date_last_gen, 'dayhour'); |
|
1173
|
|
|
if ($this->frequency > 0) |
|
1174
|
|
|
{ |
|
1175
|
|
|
if (! empty($this->date_when)) |
|
1176
|
|
|
{ |
|
1177
|
|
|
$label .= '<br><b>'.$langs->trans('NextDateToExecution') . ':</b> '; |
|
1178
|
|
|
$label .= (empty($this->suspended)?'':'<strike>'). dol_print_date($this->date_when, 'day').(empty($this->suspended)?'':'</strike>'); // No hour for this property |
|
1179
|
|
|
if (! empty($this->suspended)) $label .= ' ('.$langs->trans("Disabled").')'; |
|
1180
|
|
|
} |
|
1181
|
|
|
} |
|
1182
|
|
|
|
|
1183
|
|
|
$url = DOL_URL_ROOT.'/compta/facture/fiche-rec.php?facid='.$this->id; |
|
1184
|
|
|
|
|
1185
|
|
|
if ($short) return $url; |
|
1186
|
|
|
|
|
1187
|
|
|
if ($option != 'nolink') |
|
1188
|
|
|
{ |
|
1189
|
|
|
// Add param to save lastsearch_values or not |
|
1190
|
|
|
$add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0); |
|
1191
|
|
|
if ($save_lastsearch_value == -1 && preg_match('/list\.php/',$_SERVER["PHP_SELF"])) $add_save_lastsearch_values=1; |
|
1192
|
|
|
if ($add_save_lastsearch_values) $url.='&save_lastsearch_values=1'; |
|
1193
|
|
|
} |
|
1194
|
|
|
|
|
1195
|
|
|
$linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">'; |
|
1196
|
|
|
$linkend='</a>'; |
|
1197
|
|
|
|
|
1198
|
|
|
$result .= $linkstart; |
|
1199
|
|
|
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); |
|
1200
|
|
|
if ($withpicto != 2) $result.= $this->ref; |
|
1201
|
|
|
$result .= $linkend; |
|
1202
|
|
|
|
|
1203
|
|
|
return $result; |
|
1204
|
|
|
} |
|
1205
|
|
|
|
|
1206
|
|
|
/** |
|
1207
|
|
|
* Return label of object status |
|
1208
|
|
|
* |
|
1209
|
|
|
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto, 6=Long label + picto |
|
1210
|
|
|
* @param integer $alreadypaid Not used on recurring invoices |
|
1211
|
|
|
* @return string Label of status |
|
1212
|
|
|
*/ |
|
1213
|
|
|
function getLibStatut($mode=0, $alreadypaid=-1) |
|
1214
|
|
|
{ |
|
1215
|
|
|
|
|
1216
|
|
|
return $this->LibStatut($this->frequency?1:0, $this->suspended, $mode, $alreadypaid, empty($this->type)?0:$this->type); |
|
1217
|
|
|
} |
|
1218
|
|
|
|
|
1219
|
|
|
// phpcs:disable PEAR.NamingConventions.ValidFunctionName.NotCamelCaps |
|
1220
|
|
|
/** |
|
1221
|
|
|
* Return label of a status |
|
1222
|
|
|
* |
|
1223
|
|
|
* @param int $recur Is it a recurring invoice ? |
|
1224
|
|
|
* @param int $status Id status (suspended or not) |
|
1225
|
|
|
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto, 6=long label + picto |
|
1226
|
|
|
* @param integer $alreadypaid Not used for recurring invoices |
|
1227
|
|
|
* @param int $type Type invoice |
|
1228
|
|
|
* @return string Label of status |
|
1229
|
|
|
*/ |
|
1230
|
|
|
function LibStatut($recur, $status, $mode=0, $alreadypaid=-1, $type=0) |
|
1231
|
|
|
{ |
|
1232
|
|
|
// phpcs:enable |
|
1233
|
|
|
global $langs; |
|
1234
|
|
|
$langs->load('bills'); |
|
1235
|
|
|
|
|
1236
|
|
|
//print "$recur,$status,$mode,$alreadypaid,$type"; |
|
1237
|
|
|
if ($mode == 0) |
|
1238
|
|
|
{ |
|
1239
|
|
|
$prefix=''; |
|
1240
|
|
|
if ($recur) |
|
1241
|
|
|
{ |
|
1242
|
|
|
if ($status == self::STATUS_SUSPENDED) return $langs->trans('Disabled'); |
|
1243
|
|
|
else return $langs->trans('Active'); |
|
1244
|
|
|
} |
|
1245
|
|
|
else |
|
1246
|
|
|
{ |
|
1247
|
|
|
if ($status == self::STATUS_SUSPENDED) return $langs->trans('Disabled'); |
|
1248
|
|
|
else return $langs->trans("Draft"); |
|
1249
|
|
|
} |
|
1250
|
|
|
} |
|
1251
|
|
|
elseif ($mode == 1) |
|
1252
|
|
|
{ |
|
1253
|
|
|
$prefix='Short'; |
|
1254
|
|
|
if ($recur) |
|
1255
|
|
|
{ |
|
1256
|
|
|
if ($status == self::STATUS_SUSPENDED) return $langs->trans('Disabled'); |
|
1257
|
|
|
else return $langs->trans('Active'); |
|
1258
|
|
|
} |
|
1259
|
|
|
else |
|
1260
|
|
|
{ |
|
1261
|
|
|
if ($status == self::STATUS_SUSPENDED) return $langs->trans('Disabled'); |
|
1262
|
|
|
else return $langs->trans("Draft"); |
|
1263
|
|
|
} |
|
1264
|
|
|
} |
|
1265
|
|
|
elseif ($mode == 2) |
|
1266
|
|
|
{ |
|
1267
|
|
|
if ($recur) |
|
1268
|
|
|
{ |
|
1269
|
|
|
if ($status == self::STATUS_SUSPENDED) return img_picto($langs->trans('Disabled'),'statut6').' '.$langs->trans('Disabled'); |
|
1270
|
|
|
else return img_picto($langs->trans('Active'),'statut4').' '.$langs->trans('Active'); |
|
1271
|
|
|
} |
|
1272
|
|
|
else |
|
1273
|
|
|
{ |
|
1274
|
|
|
if ($status == self::STATUS_SUSPENDED) return img_picto($langs->trans('Disabled'),'statut6').' '.$langs->trans('Disabled'); |
|
1275
|
|
|
else return img_picto($langs->trans('Draft'),'statut0').' '.$langs->trans('Draft'); |
|
1276
|
|
|
} |
|
1277
|
|
|
} |
|
1278
|
|
|
elseif ($mode == 3) |
|
1279
|
|
|
{ |
|
1280
|
|
|
if ($recur) |
|
1281
|
|
|
{ |
|
1282
|
|
|
$prefix='Short'; |
|
1283
|
|
|
if ($status == self::STATUS_SUSPENDED) return img_picto($langs->trans('Disabled'),'statut6'); |
|
1284
|
|
|
else return img_picto($langs->trans('Active'),'statut4'); |
|
1285
|
|
|
} |
|
1286
|
|
|
else |
|
1287
|
|
|
{ |
|
1288
|
|
|
if ($status == self::STATUS_SUSPENDED) return img_picto($langs->trans('Disabled'),'statut6'); |
|
1289
|
|
|
else return img_picto($langs->trans('Draft'),'statut0'); |
|
1290
|
|
|
} |
|
1291
|
|
|
} |
|
1292
|
|
|
elseif ($mode == 4) |
|
1293
|
|
|
{ |
|
1294
|
|
|
$prefix=''; |
|
1295
|
|
|
if ($recur) |
|
1296
|
|
|
{ |
|
1297
|
|
|
if ($status == self::STATUS_SUSPENDED) return img_picto($langs->trans('Disabled'),'statut6').' '.$langs->trans('Disabled'); |
|
1298
|
|
|
else return img_picto($langs->trans('Active'),'statut4').' '.$langs->trans('Active'); |
|
1299
|
|
|
} |
|
1300
|
|
|
else |
|
1301
|
|
|
{ |
|
1302
|
|
|
if ($status == self::STATUS_SUSPENDED) return img_picto($langs->trans('Disabled'),'statut6').' '.$langs->trans('Disabled'); |
|
1303
|
|
|
else return img_picto($langs->trans('Draft'),'statut0').' '.$langs->trans('Draft'); |
|
1304
|
|
|
} |
|
1305
|
|
|
} |
|
1306
|
|
|
elseif ($mode == 5 || $mode == 6) |
|
1307
|
|
|
{ |
|
1308
|
|
|
$prefix=''; |
|
1309
|
|
|
if ($mode == 5) $prefix='Short'; |
|
1310
|
|
|
if ($recur) |
|
1311
|
|
|
{ |
|
1312
|
|
|
if ($status == self::STATUS_SUSPENDED) return '<span class="xhideonsmartphone">'.$langs->trans('Disabled').' </span>'.img_picto($langs->trans('Disabled'),'statut6'); |
|
1313
|
|
|
else return '<span class="xhideonsmartphone">'.$langs->trans('Active').' </span>'.img_picto($langs->trans('Active'),'statut4'); |
|
1314
|
|
|
} |
|
1315
|
|
|
else |
|
1316
|
|
|
{ |
|
1317
|
|
|
if ($status == self::STATUS_SUSPENDED) return '<span class="xhideonsmartphone">'.$langs->trans('Disabled').' </span>'.img_picto($langs->trans('Disabled'),'statut6'); |
|
1318
|
|
|
else return $langs->trans('Draft').' '.img_picto($langs->trans('Active'),'statut0'); |
|
1319
|
|
|
} |
|
1320
|
|
|
} |
|
1321
|
|
|
} |
|
1322
|
|
|
|
|
1323
|
|
|
/** |
|
1324
|
|
|
* Initialise an instance with random values. |
|
1325
|
|
|
* Used to build previews or test instances. |
|
1326
|
|
|
* id must be 0 if object instance is a specimen. |
|
1327
|
|
|
* |
|
1328
|
|
|
* @param string $option ''=Create a specimen invoice with lines, 'nolines'=No lines |
|
1329
|
|
|
* @return void |
|
1330
|
|
|
*/ |
|
1331
|
|
|
function initAsSpecimen($option='') |
|
1332
|
|
|
{ |
|
1333
|
|
|
global $user,$langs,$conf; |
|
1334
|
|
|
|
|
1335
|
|
|
$now=dol_now(); |
|
1336
|
|
|
$arraynow=dol_getdate($now); |
|
1337
|
|
|
$nownotime=dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']); |
|
1338
|
|
|
|
|
1339
|
|
|
// Load array of products prodids |
|
1340
|
|
|
$num_prods = 0; |
|
1341
|
|
|
$prodids = array(); |
|
1342
|
|
|
|
|
1343
|
|
|
$sql = "SELECT rowid"; |
|
1344
|
|
|
$sql.= " FROM ".MAIN_DB_PREFIX."product"; |
|
1345
|
|
|
$sql.= " WHERE entity IN (".getEntity('product').")"; |
|
1346
|
|
|
$resql = $this->db->query($sql); |
|
1347
|
|
|
if ($resql) |
|
1348
|
|
|
{ |
|
1349
|
|
|
$num_prods = $this->db->num_rows($resql); |
|
1350
|
|
|
$i = 0; |
|
1351
|
|
|
while ($i < $num_prods) |
|
1352
|
|
|
{ |
|
1353
|
|
|
$i++; |
|
1354
|
|
|
$row = $this->db->fetch_row($resql); |
|
1355
|
|
|
$prodids[$i] = $row[0]; |
|
1356
|
|
|
} |
|
1357
|
|
|
} |
|
1358
|
|
|
|
|
1359
|
|
|
// Initialize parameters |
|
1360
|
|
|
$this->id=0; |
|
1361
|
|
|
$this->ref = 'SPECIMEN'; |
|
1362
|
|
|
$this->specimen=1; |
|
1363
|
|
|
$this->socid = 1; |
|
1364
|
|
|
$this->date = $nownotime; |
|
1365
|
|
|
$this->date_lim_reglement = $nownotime + 3600 * 24 *30; |
|
1366
|
|
|
$this->cond_reglement_id = 1; |
|
1367
|
|
|
$this->cond_reglement_code = 'RECEP'; |
|
1368
|
|
|
$this->date_lim_reglement=$this->calculate_date_lim_reglement(); |
|
1369
|
|
|
$this->mode_reglement_id = 0; // Not forced to show payment mode CHQ + VIR |
|
1370
|
|
|
$this->mode_reglement_code = ''; // Not forced to show payment mode CHQ + VIR |
|
1371
|
|
|
$this->note_public='This is a comment (public)'; |
|
1372
|
|
|
$this->note_private='This is a comment (private)'; |
|
1373
|
|
|
$this->note='This is a comment (private)'; |
|
1374
|
|
|
$this->fk_incoterms=0; |
|
1375
|
|
|
$this->location_incoterms=''; |
|
1376
|
|
|
|
|
1377
|
|
|
if (empty($option) || $option != 'nolines') |
|
1378
|
|
|
{ |
|
1379
|
|
|
// Lines |
|
1380
|
|
|
$nbp = 5; |
|
1381
|
|
|
$xnbp = 0; |
|
1382
|
|
|
while ($xnbp < $nbp) |
|
1383
|
|
|
{ |
|
1384
|
|
|
$line=new FactureLigne($this->db); |
|
1385
|
|
|
$line->desc=$langs->trans("Description")." ".$xnbp; |
|
1386
|
|
|
$line->qty=1; |
|
1387
|
|
|
$line->subprice=100; |
|
1388
|
|
|
$line->tva_tx=19.6; |
|
1389
|
|
|
$line->localtax1_tx=0; |
|
1390
|
|
|
$line->localtax2_tx=0; |
|
1391
|
|
|
$line->remise_percent=0; |
|
1392
|
|
|
if ($xnbp == 1) // Qty is negative (product line) |
|
1393
|
|
|
{ |
|
1394
|
|
|
$prodid = mt_rand(1, $num_prods); |
|
1395
|
|
|
$line->fk_product=$prodids[$prodid]; |
|
1396
|
|
|
$line->qty=-1; |
|
1397
|
|
|
$line->total_ht=-100; |
|
1398
|
|
|
$line->total_ttc=-119.6; |
|
1399
|
|
|
$line->total_tva=-19.6; |
|
1400
|
|
|
} |
|
1401
|
|
|
else if ($xnbp == 2) // UP is negative (free line) |
|
1402
|
|
|
{ |
|
1403
|
|
|
$line->subprice=-100; |
|
1404
|
|
|
$line->total_ht=-100; |
|
1405
|
|
|
$line->total_ttc=-119.6; |
|
1406
|
|
|
$line->total_tva=-19.6; |
|
1407
|
|
|
$line->remise_percent=0; |
|
1408
|
|
|
} |
|
1409
|
|
|
else if ($xnbp == 3) // Discount is 50% (product line) |
|
1410
|
|
|
{ |
|
1411
|
|
|
$prodid = mt_rand(1, $num_prods); |
|
1412
|
|
|
$line->fk_product=$prodids[$prodid]; |
|
1413
|
|
|
$line->total_ht=50; |
|
1414
|
|
|
$line->total_ttc=59.8; |
|
1415
|
|
|
$line->total_tva=9.8; |
|
1416
|
|
|
$line->remise_percent=50; |
|
1417
|
|
|
} |
|
1418
|
|
|
else // (product line) |
|
1419
|
|
|
{ |
|
1420
|
|
|
$prodid = mt_rand(1, $num_prods); |
|
1421
|
|
|
$line->fk_product=$prodids[$prodid]; |
|
1422
|
|
|
$line->total_ht=100; |
|
1423
|
|
|
$line->total_ttc=119.6; |
|
1424
|
|
|
$line->total_tva=19.6; |
|
1425
|
|
|
$line->remise_percent=00; |
|
|
|
|
|
|
1426
|
|
|
} |
|
1427
|
|
|
|
|
1428
|
|
|
$this->lines[$xnbp]=$line; |
|
1429
|
|
|
$xnbp++; |
|
1430
|
|
|
|
|
1431
|
|
|
$this->total_ht += $line->total_ht; |
|
1432
|
|
|
$this->total_tva += $line->total_tva; |
|
1433
|
|
|
$this->total_ttc += $line->total_ttc; |
|
1434
|
|
|
} |
|
1435
|
|
|
$this->revenuestamp = 0; |
|
1436
|
|
|
|
|
1437
|
|
|
// Add a line "offered" |
|
1438
|
|
|
$line=new FactureLigne($this->db); |
|
1439
|
|
|
$line->desc=$langs->trans("Description")." (offered line)"; |
|
1440
|
|
|
$line->qty=1; |
|
1441
|
|
|
$line->subprice=100; |
|
1442
|
|
|
$line->tva_tx=19.6; |
|
1443
|
|
|
$line->localtax1_tx=0; |
|
1444
|
|
|
$line->localtax2_tx=0; |
|
1445
|
|
|
$line->remise_percent=100; |
|
1446
|
|
|
$line->total_ht=0; |
|
1447
|
|
|
$line->total_ttc=0; // 90 * 1.196 |
|
1448
|
|
|
$line->total_tva=0; |
|
1449
|
|
|
$prodid = mt_rand(1, $num_prods); |
|
1450
|
|
|
$line->fk_product=$prodids[$prodid]; |
|
1451
|
|
|
|
|
1452
|
|
|
$this->lines[$xnbp]=$line; |
|
1453
|
|
|
$xnbp++; |
|
1454
|
|
|
} |
|
1455
|
|
|
|
|
1456
|
|
|
$this->usenewprice = 1; |
|
1457
|
|
|
} |
|
1458
|
|
|
|
|
1459
|
|
|
/** |
|
1460
|
|
|
* Function used to replace a thirdparty id with another one. |
|
1461
|
|
|
* |
|
1462
|
|
|
* @param DoliDB $db Database handler |
|
1463
|
|
|
* @param int $origin_id Old thirdparty id |
|
1464
|
|
|
* @param int $dest_id New thirdparty id |
|
1465
|
|
|
* @return bool |
|
1466
|
|
|
*/ |
|
1467
|
|
|
public static function replaceThirdparty(DoliDB $db, $origin_id, $dest_id) |
|
1468
|
|
|
{ |
|
1469
|
|
|
$tables = array( |
|
1470
|
|
|
'facture_rec' |
|
1471
|
|
|
); |
|
1472
|
|
|
|
|
1473
|
|
|
return CommonObject::commonReplaceThirdparty($db, $origin_id, $dest_id, $tables); |
|
1474
|
|
|
} |
|
1475
|
|
|
|
|
1476
|
|
|
/** |
|
1477
|
|
|
* Update frequency and unit |
|
1478
|
|
|
* |
|
1479
|
|
|
* @param int $frequency value of frequency |
|
1480
|
|
|
* @param string $unit unit of frequency (d, m, y) |
|
1481
|
|
|
* @return int <0 if KO, >0 if OK |
|
1482
|
|
|
*/ |
|
1483
|
|
|
function setFrequencyAndUnit($frequency,$unit) |
|
1484
|
|
|
{ |
|
1485
|
|
|
if (! $this->table_element) |
|
1486
|
|
|
{ |
|
1487
|
|
|
dol_syslog(get_class($this)."::setFrequencyAndUnit was called on objet with property table_element not defined",LOG_ERR); |
|
1488
|
|
|
return -1; |
|
1489
|
|
|
} |
|
1490
|
|
|
|
|
1491
|
|
|
if (!empty($frequency) && empty($unit)) |
|
1492
|
|
|
{ |
|
1493
|
|
|
dol_syslog(get_class($this)."::setFrequencyAndUnit was called on objet with params frequency defined but unit not defined",LOG_ERR); |
|
1494
|
|
|
return -2; |
|
1495
|
|
|
} |
|
1496
|
|
|
|
|
1497
|
|
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; |
|
1498
|
|
|
$sql.= ' SET frequency = '.($frequency?$this->db->escape($frequency):'null'); |
|
1499
|
|
|
if (!empty($unit)) |
|
1500
|
|
|
{ |
|
1501
|
|
|
$sql.= ', unit_frequency = \''.$this->db->escape($unit).'\''; |
|
1502
|
|
|
} |
|
1503
|
|
|
$sql.= ' WHERE rowid = '.$this->id; |
|
1504
|
|
|
|
|
1505
|
|
|
dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG); |
|
1506
|
|
|
if ($this->db->query($sql)) |
|
1507
|
|
|
{ |
|
1508
|
|
|
$this->frequency = $frequency; |
|
1509
|
|
|
if (!empty($unit)) $this->unit_frequency = $unit; |
|
1510
|
|
|
return 1; |
|
1511
|
|
|
} |
|
1512
|
|
|
else |
|
1513
|
|
|
{ |
|
1514
|
|
|
dol_print_error($this->db); |
|
1515
|
|
|
return -1; |
|
1516
|
|
|
} |
|
1517
|
|
|
} |
|
1518
|
|
|
|
|
1519
|
|
|
/** |
|
1520
|
|
|
* Update the next date of execution |
|
1521
|
|
|
* |
|
1522
|
|
|
* @param datetime $date date of execution |
|
1523
|
|
|
* @param int $increment_nb_gen_done 0 do nothing more, >0 increment nb_gen_done |
|
1524
|
|
|
* @return int <0 if KO, >0 if OK |
|
1525
|
|
|
*/ |
|
1526
|
|
|
function setNextDate($date, $increment_nb_gen_done=0) |
|
1527
|
|
|
{ |
|
1528
|
|
|
if (! $this->table_element) |
|
1529
|
|
|
{ |
|
1530
|
|
|
dol_syslog(get_class($this)."::setNextDate was called on objet with property table_element not defined",LOG_ERR); |
|
1531
|
|
|
return -1; |
|
1532
|
|
|
} |
|
1533
|
|
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; |
|
1534
|
|
|
$sql.= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null"); |
|
1535
|
|
|
if ($increment_nb_gen_done>0) $sql.= ', nb_gen_done = nb_gen_done + 1'; |
|
1536
|
|
|
$sql.= ' WHERE rowid = '.$this->id; |
|
1537
|
|
|
|
|
1538
|
|
|
dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG); |
|
1539
|
|
|
if ($this->db->query($sql)) |
|
1540
|
|
|
{ |
|
1541
|
|
|
$this->date_when = $date; |
|
1542
|
|
|
if ($increment_nb_gen_done>0) $this->nb_gen_done++; |
|
1543
|
|
|
return 1; |
|
1544
|
|
|
} |
|
1545
|
|
|
else |
|
1546
|
|
|
{ |
|
1547
|
|
|
dol_print_error($this->db); |
|
1548
|
|
|
return -1; |
|
1549
|
|
|
} |
|
1550
|
|
|
} |
|
1551
|
|
|
|
|
1552
|
|
|
/** |
|
1553
|
|
|
* Update the maximum period |
|
1554
|
|
|
* |
|
1555
|
|
|
* @param int $nb number of maximum period |
|
1556
|
|
|
* @return int <0 if KO, >0 if OK |
|
1557
|
|
|
*/ |
|
1558
|
|
|
function setMaxPeriod($nb) |
|
1559
|
|
|
{ |
|
1560
|
|
|
if (! $this->table_element) |
|
1561
|
|
|
{ |
|
1562
|
|
|
dol_syslog(get_class($this)."::setMaxPeriod was called on objet with property table_element not defined",LOG_ERR); |
|
1563
|
|
|
return -1; |
|
1564
|
|
|
} |
|
1565
|
|
|
|
|
1566
|
|
|
if (empty($nb)) $nb=0; |
|
1567
|
|
|
|
|
1568
|
|
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; |
|
1569
|
|
|
$sql.= ' SET nb_gen_max = '.$nb; |
|
1570
|
|
|
$sql.= ' WHERE rowid = '.$this->id; |
|
1571
|
|
|
|
|
1572
|
|
|
dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG); |
|
1573
|
|
|
if ($this->db->query($sql)) |
|
1574
|
|
|
{ |
|
1575
|
|
|
$this->nb_gen_max = $nb; |
|
1576
|
|
|
return 1; |
|
1577
|
|
|
} |
|
1578
|
|
|
else |
|
1579
|
|
|
{ |
|
1580
|
|
|
dol_print_error($this->db); |
|
1581
|
|
|
return -1; |
|
1582
|
|
|
} |
|
1583
|
|
|
} |
|
1584
|
|
|
|
|
1585
|
|
|
/** |
|
1586
|
|
|
* Update the auto validate flag of invoice |
|
1587
|
|
|
* |
|
1588
|
|
|
* @param int $validate 0 to create in draft, 1 to create and validate invoice |
|
1589
|
|
|
* @return int <0 if KO, >0 if OK |
|
1590
|
|
|
*/ |
|
1591
|
|
|
function setAutoValidate($validate) |
|
1592
|
|
|
{ |
|
1593
|
|
|
if (! $this->table_element) |
|
1594
|
|
|
{ |
|
1595
|
|
|
dol_syslog(get_class($this)."::setAutoValidate was called on objet with property table_element not defined",LOG_ERR); |
|
1596
|
|
|
return -1; |
|
1597
|
|
|
} |
|
1598
|
|
|
|
|
1599
|
|
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; |
|
1600
|
|
|
$sql.= ' SET auto_validate = '.$validate; |
|
1601
|
|
|
$sql.= ' WHERE rowid = '.$this->id; |
|
1602
|
|
|
|
|
1603
|
|
|
dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG); |
|
1604
|
|
|
if ($this->db->query($sql)) |
|
1605
|
|
|
{ |
|
1606
|
|
|
$this->auto_validate = $validate; |
|
1607
|
|
|
return 1; |
|
1608
|
|
|
} |
|
1609
|
|
|
else |
|
1610
|
|
|
{ |
|
1611
|
|
|
dol_print_error($this->db); |
|
1612
|
|
|
return -1; |
|
1613
|
|
|
} |
|
1614
|
|
|
} |
|
1615
|
|
|
|
|
1616
|
|
|
/** |
|
1617
|
|
|
* Update the auto generate documents |
|
1618
|
|
|
* |
|
1619
|
|
|
* @param int $validate 0 no document, 1 to generate document |
|
1620
|
|
|
* @return int <0 if KO, >0 if OK |
|
1621
|
|
|
*/ |
|
1622
|
|
|
function setGeneratePdf($validate) |
|
1623
|
|
|
{ |
|
1624
|
|
|
if (! $this->table_element) |
|
1625
|
|
|
{ |
|
1626
|
|
|
dol_syslog(get_class($this)."::setGeneratePdf was called on objet with property table_element not defined",LOG_ERR); |
|
1627
|
|
|
return -1; |
|
1628
|
|
|
} |
|
1629
|
|
|
|
|
1630
|
|
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; |
|
1631
|
|
|
$sql.= ' SET generate_pdf = '.$validate; |
|
1632
|
|
|
$sql.= ' WHERE rowid = '.$this->id; |
|
1633
|
|
|
|
|
1634
|
|
|
dol_syslog(get_class($this)."::setGeneratePdf", LOG_DEBUG); |
|
1635
|
|
|
if ($this->db->query($sql)) |
|
1636
|
|
|
{ |
|
1637
|
|
|
$this->generate_pdf = $validate; |
|
1638
|
|
|
return 1; |
|
1639
|
|
|
} |
|
1640
|
|
|
else |
|
1641
|
|
|
{ |
|
1642
|
|
|
dol_print_error($this->db); |
|
1643
|
|
|
return -1; |
|
1644
|
|
|
} |
|
1645
|
|
|
} |
|
1646
|
|
|
|
|
1647
|
|
|
/** |
|
1648
|
|
|
* Update the model for documents |
|
1649
|
|
|
* |
|
1650
|
|
|
* @param string $model model of document generator |
|
1651
|
|
|
* @return int <0 if KO, >0 if OK |
|
1652
|
|
|
*/ |
|
1653
|
|
|
function setModelPdf($model) |
|
1654
|
|
|
{ |
|
1655
|
|
|
if (! $this->table_element) |
|
1656
|
|
|
{ |
|
1657
|
|
|
dol_syslog(get_class($this)."::setModelPdf was called on objet with property table_element not defined",LOG_ERR); |
|
1658
|
|
|
return -1; |
|
1659
|
|
|
} |
|
1660
|
|
|
|
|
1661
|
|
|
$sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; |
|
1662
|
|
|
$sql.= ' SET modelpdf = "' . $model . '"'; |
|
1663
|
|
|
$sql.= ' WHERE rowid = '.$this->id; |
|
1664
|
|
|
|
|
1665
|
|
|
dol_syslog(get_class($this)."::setModelPdf", LOG_DEBUG); |
|
1666
|
|
|
if ($this->db->query($sql)) |
|
1667
|
|
|
{ |
|
1668
|
|
|
$this->modelpdf = $model; |
|
1669
|
|
|
return 1; |
|
1670
|
|
|
} |
|
1671
|
|
|
else |
|
1672
|
|
|
{ |
|
1673
|
|
|
dol_print_error($this->db); |
|
1674
|
|
|
return -1; |
|
1675
|
|
|
} |
|
1676
|
|
|
} |
|
1677
|
|
|
} |
|
1678
|
|
|
|
|
1679
|
|
|
|
|
1680
|
|
|
|
|
1681
|
|
|
/** |
|
1682
|
|
|
* Class to manage invoice lines of templates. |
|
1683
|
|
|
* Saved into database table llx_facturedet_rec |
|
1684
|
|
|
*/ |
|
1685
|
|
|
class FactureLigneRec extends CommonInvoiceLine |
|
1686
|
|
|
{ |
|
1687
|
|
|
/** |
|
1688
|
|
|
* @var string ID to identify managed object |
|
1689
|
|
|
*/ |
|
1690
|
|
|
public $element='facturedetrec'; |
|
1691
|
|
|
|
|
1692
|
|
|
/** |
|
1693
|
|
|
* @var string Name of table without prefix where object is stored |
|
1694
|
|
|
*/ |
|
1695
|
|
|
public $table_element='facturedet_rec'; |
|
1696
|
|
|
|
|
1697
|
|
|
var $date_start_fill; |
|
1698
|
|
|
var $date_end_fill; |
|
1699
|
|
|
|
|
1700
|
|
|
|
|
1701
|
|
|
/** |
|
1702
|
|
|
* Delete line in database |
|
1703
|
|
|
* |
|
1704
|
|
|
* @param User $user Object user |
|
1705
|
|
|
* @param int $notrigger Disable triggers |
|
1706
|
|
|
* @return int <0 if KO, >0 if OK |
|
1707
|
|
|
*/ |
|
1708
|
|
|
function delete(User $user, $notrigger = false) |
|
1709
|
|
|
{ |
|
1710
|
|
|
$error=0; |
|
1711
|
|
|
|
|
1712
|
|
|
$this->db->begin(); |
|
1713
|
|
|
|
|
1714
|
|
|
if (! $error) { |
|
1715
|
|
|
if (! $notrigger) { |
|
1716
|
|
|
// Call triggers |
|
1717
|
|
|
$result=$this->call_trigger('LINEBILLREC_DELETE', $user); |
|
1718
|
|
|
if ($result < 0) { $error++; } // Do also here what you must do to rollback action if trigger fail |
|
1719
|
|
|
// End call triggers |
|
1720
|
|
|
} |
|
1721
|
|
|
} |
|
1722
|
|
|
|
|
1723
|
|
|
if (! $error) |
|
1724
|
|
|
{ |
|
1725
|
|
|
$sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.$this->id; |
|
1726
|
|
|
|
|
1727
|
|
|
$res = $this->db->query($sql); |
|
1728
|
|
|
if($res===false) { |
|
1729
|
|
|
$error++; |
|
1730
|
|
|
$this->errors[] = $this->db->lasterror(); |
|
1731
|
|
|
} |
|
1732
|
|
|
} |
|
1733
|
|
|
|
|
1734
|
|
|
// Commit or rollback |
|
1735
|
|
|
if ($error) { |
|
1736
|
|
|
$this->db->rollback(); |
|
1737
|
|
|
return -1; |
|
1738
|
|
|
} else { |
|
1739
|
|
|
$this->db->commit(); |
|
1740
|
|
|
return 1; |
|
1741
|
|
|
} |
|
1742
|
|
|
} |
|
1743
|
|
|
|
|
1744
|
|
|
|
|
1745
|
|
|
/** |
|
1746
|
|
|
* Recupere les lignes de factures predefinies dans this->lines |
|
1747
|
|
|
* |
|
1748
|
|
|
* @param int $rowid Id of invoice |
|
1749
|
|
|
* @return int 1 if OK, < 0 if KO |
|
1750
|
|
|
*/ |
|
1751
|
|
|
function fetch($rowid) |
|
1752
|
|
|
{ |
|
1753
|
|
|
$sql = 'SELECT l.rowid, l.fk_facture ,l.fk_product, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx,'; |
|
1754
|
|
|
$sql.= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,'; |
|
1755
|
|
|
$sql.= ' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,'; |
|
1756
|
|
|
$sql.= ' l.rang, l.special_code,'; |
|
1757
|
|
|
$sql.= ' l.fk_unit, l.fk_contract_line,'; |
|
1758
|
|
|
$sql.= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc'; |
|
1759
|
|
|
$sql.= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l'; |
|
1760
|
|
|
$sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid'; |
|
1761
|
|
|
$sql.= ' WHERE l.rowid = '.$rowid; |
|
1762
|
|
|
$sql.= ' ORDER BY l.rang'; |
|
1763
|
|
|
|
|
1764
|
|
|
dol_syslog('FactureRec::fetch', LOG_DEBUG); |
|
1765
|
|
|
$result = $this->db->query($sql); |
|
1766
|
|
|
if ($result) |
|
1767
|
|
|
{ |
|
1768
|
|
|
|
|
1769
|
|
|
$objp = $this->db->fetch_object($result); |
|
1770
|
|
|
|
|
1771
|
|
|
$this->id = $objp->rowid; |
|
1772
|
|
|
$this->label = $objp->custom_label; // Label line |
|
1773
|
|
|
$this->desc = $objp->description; // Description line |
|
1774
|
|
|
$this->description = $objp->description; // Description line |
|
1775
|
|
|
$this->product_type = $objp->product_type; // Type of line |
|
1776
|
|
|
$this->ref = $objp->product_ref; // Ref product |
|
1777
|
|
|
$this->product_ref = $objp->product_ref; // Ref product |
|
1778
|
|
|
$this->libelle = $objp->product_label; // deprecated |
|
1779
|
|
|
$this->product_label = $objp->product_label; // Label product |
|
1780
|
|
|
$this->product_desc = $objp->product_desc; // Description product |
|
1781
|
|
|
$this->fk_product_type = $objp->fk_product_type; // Type of product |
|
1782
|
|
|
$this->qty = $objp->qty; |
|
1783
|
|
|
$this->price = $objp->price; |
|
1784
|
|
|
$this->subprice = $objp->subprice; |
|
1785
|
|
|
$this->fk_facture = $objp->fk_facture; |
|
1786
|
|
|
$this->vat_src_code = $objp->vat_src_code; |
|
1787
|
|
|
$this->tva_tx = $objp->tva_tx; |
|
1788
|
|
|
$this->localtax1_tx = $objp->localtax1_tx; |
|
1789
|
|
|
$this->localtax2_tx = $objp->localtax2_tx; |
|
1790
|
|
|
$this->localtax1_type = $objp->localtax1_type; |
|
1791
|
|
|
$this->localtax2_type = $objp->localtax2_type; |
|
1792
|
|
|
$this->remise_percent = $objp->remise_percent; |
|
1793
|
|
|
$this->fk_remise_except = $objp->fk_remise_except; |
|
1794
|
|
|
$this->fk_product = $objp->fk_product; |
|
1795
|
|
|
$this->date_start_fill = $objp->date_start_fill; |
|
1796
|
|
|
$this->date_end_fill = $objp->date_end_fill; |
|
1797
|
|
|
$this->info_bits = $objp->info_bits; |
|
1798
|
|
|
$this->total_ht = $objp->total_ht; |
|
1799
|
|
|
$this->total_tva = $objp->total_tva; |
|
1800
|
|
|
$this->total_ttc = $objp->total_ttc; |
|
1801
|
|
|
$this->code_ventilation = $objp->fk_code_ventilation; |
|
1802
|
|
|
$this->rang = $objp->rang; |
|
1803
|
|
|
$this->special_code = $objp->special_code; |
|
1804
|
|
|
$this->fk_unit = $objp->fk_unit; |
|
1805
|
|
|
$this->fk_contract_line = $objp->fk_contract_line; |
|
1806
|
|
|
|
|
1807
|
|
|
|
|
1808
|
|
|
$this->db->free($result); |
|
1809
|
|
|
return 1; |
|
1810
|
|
|
} |
|
1811
|
|
|
else |
|
1812
|
|
|
{ |
|
1813
|
|
|
$this->error=$this->db->lasterror(); |
|
1814
|
|
|
return -3; |
|
1815
|
|
|
} |
|
1816
|
|
|
} |
|
1817
|
|
|
|
|
1818
|
|
|
|
|
1819
|
|
|
/** |
|
1820
|
|
|
* Update a line to invoice_rec. |
|
1821
|
|
|
* |
|
1822
|
|
|
* @param User $user User |
|
1823
|
|
|
* @param int $notrigger No trigger |
|
1824
|
|
|
* @return int <0 if KO, Id of line if OK |
|
1825
|
|
|
*/ |
|
1826
|
|
|
function update(User $user, $notrigger=0) |
|
1827
|
|
|
{ |
|
1828
|
|
|
global $conf; |
|
1829
|
|
|
|
|
1830
|
|
|
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; |
|
1831
|
|
|
|
|
1832
|
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET"; |
|
1833
|
|
|
$sql.= " fk_facture = ".$this->fk_facture; |
|
1834
|
|
|
$sql.= ", label=".(! empty($this->label)?"'".$this->db->escape($this->label)."'":"null"); |
|
1835
|
|
|
$sql.= ", description='".$this->db->escape($this->desc)."'"; |
|
1836
|
|
|
$sql.= ", price=".price2num($this->price); |
|
1837
|
|
|
$sql.= ", qty=".price2num($this->qty); |
|
1838
|
|
|
$sql.= ", tva_tx=".price2num($this->tva_tx); |
|
1839
|
|
|
$sql.= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'"; |
|
1840
|
|
|
$sql.= ", localtax1_tx=".price2num($this->localtax1_tx); |
|
1841
|
|
|
$sql.= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'"; |
|
1842
|
|
|
$sql.= ", localtax2_tx=".price2num($this->localtax2_tx); |
|
1843
|
|
|
$sql.= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'"; |
|
1844
|
|
|
$sql.= ", fk_product=".($this->fk_product > 0 ? $this->fk_product :"null"); |
|
1845
|
|
|
$sql.= ", product_type=".$this->product_type; |
|
1846
|
|
|
$sql.= ", remise_percent='".price2num($this->remise_percent)."'"; |
|
1847
|
|
|
$sql.= ", subprice='".price2num($this->subprice)."'"; |
|
1848
|
|
|
$sql.= ", info_bits='".price2num($this->info_bits)."'"; |
|
1849
|
|
|
$sql.= ", date_start_fill=".(int) $this->date_start_fill; |
|
1850
|
|
|
$sql.= ", date_end_fill=".(int) $this->date_end_fill; |
|
1851
|
|
|
if (empty($this->skip_update_total)) |
|
1852
|
|
|
{ |
|
1853
|
|
|
$sql.= ", total_ht=".price2num($this->total_ht); |
|
1854
|
|
|
$sql.= ", total_tva=".price2num($this->total_tva); |
|
1855
|
|
|
$sql.= ", total_localtax1=".price2num($this->total_localtax1); |
|
1856
|
|
|
$sql.= ", total_localtax2=".price2num($this->total_localtax2); |
|
1857
|
|
|
$sql.= ", total_ttc=".price2num($this->total_ttc); |
|
1858
|
|
|
} |
|
1859
|
|
|
$sql.= ", rang=".$this->rang; |
|
1860
|
|
|
$sql.= ", special_code=".$this->special_code; |
|
1861
|
|
|
$sql.= ", fk_unit=".($this->fk_unit ?"'".$this->db->escape($this->fk_unit )."'":"null"); |
|
1862
|
|
|
$sql.= ", fk_contract_line=".($this->fk_contract_line?$this->fk_contract_line:"null"); |
|
1863
|
|
|
$sql.= " WHERE rowid = ".$this->id; |
|
1864
|
|
|
|
|
1865
|
|
|
dol_syslog(get_class($this)."::updateline", LOG_DEBUG); |
|
1866
|
|
|
$resql=$this->db->query($sql); |
|
1867
|
|
|
if ($resql) |
|
1868
|
|
|
{ |
|
1869
|
|
|
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used |
|
1870
|
|
|
{ |
|
1871
|
|
|
$result=$this->insertExtraFields(); |
|
1872
|
|
|
if ($result < 0) |
|
1873
|
|
|
{ |
|
1874
|
|
|
$error++; |
|
1875
|
|
|
} |
|
1876
|
|
|
} |
|
1877
|
|
|
|
|
1878
|
|
|
if (! $error && ! $notrigger) |
|
1879
|
|
|
{ |
|
1880
|
|
|
// Call trigger |
|
1881
|
|
|
$result=$this->call_trigger('LINEBILL_REC_UPDATE', $user); |
|
1882
|
|
|
if ($result < 0) |
|
1883
|
|
|
{ |
|
1884
|
|
|
$this->db->rollback(); |
|
1885
|
|
|
return -2; |
|
1886
|
|
|
} |
|
1887
|
|
|
// End call triggers |
|
1888
|
|
|
} |
|
1889
|
|
|
$this->db->commit(); |
|
1890
|
|
|
return 1; |
|
1891
|
|
|
} |
|
1892
|
|
|
else |
|
1893
|
|
|
{ |
|
1894
|
|
|
$this->error=$this->db->lasterror(); |
|
1895
|
|
|
$this->db->rollback(); |
|
1896
|
|
|
return -2; |
|
1897
|
|
|
} |
|
1898
|
|
|
} |
|
1899
|
|
|
} |
|
1900
|
|
|
|