dolibarr 20.0.4
facture-rec.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2003-2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004-2019 Laurent Destailleur <eldy@users.sourceforge.net>
4 * Copyright (C) 2009-2012 Regis Houssin <regis.houssin@inodbox.com>
5 * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
6 * Copyright (C) 2012 Cedric Salvador <csalvador@gpcsolutions.fr>
7 * Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
8 * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
9 * Copyright (C) 2017-2024 Frédéric France <frederic.france@free.fr>
10 * Copyright (C) 2023 Nick Fragoulis
11 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <https://www.gnu.org/licenses/>.
25 */
26
33require_once DOL_DOCUMENT_ROOT.'/core/class/commoninvoice.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/class/notify.class.php';
35require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
36require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
37require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
38
39
44{
45 const TRIGGER_PREFIX = 'BILLREC';
49 public $element = 'facturerec';
50
54 public $table_element = 'facture_rec';
55
59 public $table_element_line = 'facturedet_rec';
60
64 public $fk_element = 'fk_facture';
65
69 public $picto = 'bill';
70
74 public $entity;
75
79 protected $table_ref_field = 'titre';
80
84 public $title;
85
90 public $titre;
91
95 public $multicurrency_subprice;
96 public $socid;
97 public $number;
98 public $date;
99 //public $remise;
100 //public $remise_absolue;
101 //public $remise_percent;
102
107 public $total;
108
113 public $tva;
114
115 public $date_last_gen;
116 public $date_when;
117 public $nb_gen_done;
118 public $nb_gen_max;
119
120 public $user_author;
121
125 public $frequency;
126
130 public $unit_frequency;
131
132 public $rang;
133
137 public $special_code;
138
139 public $usenewprice = 0;
140
144 public $date_lim_reglement;
145 public $cond_reglement_code; // Code in llx_c_paiement
146 public $mode_reglement_code; // Code in llx_c_paiement
147
148 public $suspended; // status
149
150 public $auto_validate; // 0 to create in draft, 1 to create and validate the new invoice
151 public $generate_pdf; // 1 to generate PDF on invoice generation (default)
152
153
178 // BEGIN MODULEBUILDER PROPERTIES
182 public $fields = array(
183 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
184 'titre' => array('type' => 'varchar(100)', 'label' => 'Titre', 'enabled' => 1, 'showoncombobox' => 1, 'visible' => -1, 'position' => 15),
185 'entity' => array('type' => 'integer', 'label' => 'Entity', 'default' => '1', 'enabled' => 1, 'visible' => -2, 'notnull' => 1, 'position' => 20, 'index' => 1),
186 'fk_soc' => array('type' => 'integer:Societe:societe/class/societe.class.php', 'label' => 'ThirdParty', 'enabled' => 'isModEnabled("societe")', 'visible' => -1, 'notnull' => 1, 'position' => 25),
187 'subtype' => array('type' => 'smallint(6)', 'label' => 'InvoiceSubtype', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 30),
188 'datec' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'position' => 35),
189 'total_tva' => array('type' => 'double(24,8)', 'label' => 'Tva', 'enabled' => 1, 'visible' => -1, 'position' => 55, 'isameasure' => 1),
190 'localtax1' => array('type' => 'double(24,8)', 'label' => 'Localtax1', 'enabled' => 1, 'visible' => -1, 'position' => 60, 'isameasure' => 1),
191 'localtax2' => array('type' => 'double(24,8)', 'label' => 'Localtax2', 'enabled' => 1, 'visible' => -1, 'position' => 65, 'isameasure' => 1),
192 'total_ht' => array('type' => 'double(24,8)', 'label' => 'Total', 'enabled' => 1, 'visible' => -1, 'position' => 70, 'isameasure' => 1),
193 'total_ttc' => array('type' => 'double(24,8)', 'label' => 'Total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 75, 'isameasure' => 1),
194 'fk_user_author' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'Fk user author', 'enabled' => 1, 'visible' => -1, 'position' => 80),
195 'fk_projet' => array('type' => 'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)', 'label' => 'Fk projet', 'enabled' => "isModEnabled('project')", 'visible' => -1, 'position' => 85),
196 'fk_cond_reglement' => array('type' => 'integer', 'label' => 'Fk cond reglement', 'enabled' => 1, 'visible' => -1, 'position' => 90),
197 'fk_mode_reglement' => array('type' => 'integer', 'label' => 'Fk mode reglement', 'enabled' => 1, 'visible' => -1, 'position' => 95),
198 'date_lim_reglement' => array('type' => 'date', 'label' => 'Date lim reglement', 'enabled' => 1, 'visible' => -1, 'position' => 100),
199 'note_private' => array('type' => 'html', 'label' => 'NotePrivate', 'enabled' => 1, 'visible' => 0, 'position' => 105),
200 'note_public' => array('type' => 'html', 'label' => 'NotePublic', 'enabled' => 1, 'visible' => 0, 'position' => 110),
201 'modelpdf' => array('type' => 'varchar(255)', 'label' => 'Modelpdf', 'enabled' => 1, 'visible' => -1, 'position' => 115),
202 'date_when' => array('type' => 'datetime', 'label' => 'Date when', 'enabled' => 1, 'visible' => -1, 'position' => 130),
203 'date_last_gen' => array('type' => 'datetime', 'label' => 'Date last gen', 'enabled' => 1, 'visible' => -1, 'position' => 135),
204 'nb_gen_done' => array('type' => 'integer', 'label' => 'Nb gen done', 'enabled' => 1, 'visible' => -1, 'position' => 140),
205 'nb_gen_max' => array('type' => 'integer', 'label' => 'Nb gen max', 'enabled' => 1, 'visible' => -1, 'position' => 145),
206 'frequency' => array('type' => 'integer', 'label' => 'Frequency', 'enabled' => 1, 'visible' => -1, 'position' => 150),
207 'unit_frequency' => array('type' => 'varchar(2)', 'label' => 'UnitFrequency', 'enabled' => 1, 'visible' => -1, 'position' => 152),
208 'usenewprice' => array('type' => 'integer', 'label' => 'UseNewPrice', 'enabled' => 1, 'visible' => 0, 'position' => 155),
209 'revenuestamp' => array('type' => 'double(24,8)', 'label' => 'RevenueStamp', 'enabled' => 1, 'visible' => -1, 'position' => 160, 'isameasure' => 1),
210 'auto_validate' => array('type' => 'integer', 'label' => 'Auto validate', 'enabled' => 1, 'visible' => -1, 'position' => 165),
211 'generate_pdf' => array('type' => 'integer', 'label' => 'Generate pdf', 'enabled' => 1, 'visible' => -1, 'position' => 170),
212 'fk_account' => array('type' => 'integer', 'label' => 'Fk account', 'enabled' => 'isModEnabled("bank")', 'visible' => -1, 'position' => 175),
213 'fk_multicurrency' => array('type' => 'integer', 'label' => 'Fk multicurrency', 'enabled' => 1, 'visible' => -1, 'position' => 180),
214 'multicurrency_code' => array('type' => 'varchar(255)', 'label' => 'Multicurrency code', 'enabled' => 1, 'visible' => -1, 'position' => 185),
215 'multicurrency_tx' => array('type' => 'double(24,8)', 'label' => 'Multicurrency tx', 'enabled' => 1, 'visible' => -1, 'position' => 190, 'isameasure' => 1),
216 'multicurrency_total_ht' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ht', 'enabled' => 1, 'visible' => -1, 'position' => 195, 'isameasure' => 1),
217 'multicurrency_total_tva' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total tva', 'enabled' => 1, 'visible' => -1, 'position' => 200, 'isameasure' => 1),
218 'multicurrency_total_ttc' => array('type' => 'double(24,8)', 'label' => 'Multicurrency total ttc', 'enabled' => 1, 'visible' => -1, 'position' => 205, 'isameasure' => 1),
219 'fk_user_modif' => array('type' => 'integer:User:user/class/user.class.php', 'label' => 'UserModif', 'enabled' => 1, 'visible' => -2, 'notnull' => -1, 'position' => 210),
220 'tms' => array('type' => 'timestamp', 'label' => 'DateModification', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 215),
221 'suspended' => array('type' => 'integer', 'label' => 'Suspended', 'enabled' => 1, 'visible' => -1, 'position' => 225),
222 );
223 // END MODULEBUILDER PROPERTIES
224
225 const STATUS_NOTSUSPENDED = 0;
226 const STATUS_SUSPENDED = 1;
227
228
229
235 public function __construct(DoliDB $db)
236 {
237 $this->db = $db;
238 }
239
249 public function create($user, $facid, $notrigger = 0, $onlylines = array())
250 {
251 global $conf;
252
253 $error = 0;
254 $now = dol_now();
255
256 // Clean parameters
257 $this->titre = trim(isset($this->titre) ? $this->titre : $this->title); // deprecated
258 $this->title = trim($this->title);
259 $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
260 if (empty($this->suspended)) {
261 $this->suspended = 0;
262 }
263
264 // No frequency defined then no next date to execution
265 if (empty($this->frequency)) {
266 $this->frequency = 0;
267 $this->date_when = null;
268 }
269
270 $this->frequency = abs($this->frequency);
271 $this->nb_gen_done = 0;
272 $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
273 $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
274 $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
275
276 $this->db->begin();
277
278 // Load invoice template
279 $facsrc = new Facture($this->db);
280 $result = $facsrc->fetch($facid);
281 if ($result > 0) {
282 $this->socid = $facsrc->socid;
283
284 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_rec (";
285 $sql .= "titre";
286 $sql .= ", fk_soc";
287 $sql .= ", subtype";
288 $sql .= ", entity";
289 $sql .= ", datec";
290 $sql .= ", amount";
291 //$sql .= ", remise";
292 $sql .= ", note_private";
293 $sql .= ", note_public";
294 $sql .= ", modelpdf";
295 $sql .= ", fk_user_author";
296 $sql .= ", fk_projet";
297 $sql .= ", fk_account";
298 $sql .= ", fk_cond_reglement";
299 $sql .= ", fk_mode_reglement";
300 $sql .= ", usenewprice";
301 $sql .= ", frequency";
302 $sql .= ", unit_frequency";
303 $sql .= ", date_when";
304 $sql .= ", date_last_gen";
305 $sql .= ", nb_gen_done";
306 $sql .= ", nb_gen_max";
307 $sql .= ", auto_validate";
308 $sql .= ", generate_pdf";
309 $sql .= ", fk_multicurrency";
310 $sql .= ", multicurrency_code";
311 $sql .= ", multicurrency_tx";
312 $sql .= ", suspended";
313 $sql .= ") VALUES (";
314 $sql .= "'".$this->db->escape($this->titre ? $this->titre : $this->title)."'";
315 $sql .= ", ".((int) $this->socid);
316 $sql .= ", ".($this->subtype ? "'".$this->db->escape($this->subtype)."'" : "null");
317 $sql .= ", ".((int) $conf->entity);
318 $sql .= ", '".$this->db->idate($now)."'";
319 $sql .= ", ".(!empty($facsrc->total_ttc) ? ((float) $facsrc->total_ttc) : '0');
320 //$sql .= ", ".(!empty($facsrc->remise_absolue) ? ((float) $this->remise_absolue) : '0');
321 $sql .= ", ".(!empty($this->note_private) ? ("'".$this->db->escape($this->note_private)."'") : "NULL");
322 $sql .= ", ".(!empty($this->note_public) ? ("'".$this->db->escape($this->note_public)."'") : "NULL");
323 $sql .= ", ".(!empty($this->model_pdf) ? ("'".$this->db->escape($this->model_pdf)."'") : "NULL");
324 $sql .= ", ".((int) $user->id);
325 $sql .= ", ".(!empty($this->fk_project) ? ((int) $this->fk_project) : "null");
326 $sql .= ", ".(!empty($facsrc->fk_account) ? ((int) $facsrc->fk_account) : "null");
327 $sql .= ", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) : "null");
328 $sql .= ", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) : "null");
329 $sql .= ", ".((int) $this->usenewprice);
330 $sql .= ", ".((int) $this->frequency);
331 $sql .= ", '".$this->db->escape($this->unit_frequency)."'";
332 $sql .= ", ".(!empty($this->date_when) ? "'".$this->db->idate($this->date_when)."'" : 'NULL');
333 $sql .= ", ".(!empty($this->date_last_gen) ? "'".$this->db->idate($this->date_last_gen)."'" : 'NULL');
334 $sql .= ", ".((int) $this->nb_gen_done);
335 $sql .= ", ".((int) $this->nb_gen_max);
336 $sql .= ", ".((int) $this->auto_validate);
337 $sql .= ", ".((int) $this->generate_pdf);
338 $sql .= ", ".((int) $facsrc->fk_multicurrency);
339 $sql .= ", '".$this->db->escape($facsrc->multicurrency_code)."'";
340 $sql .= ", ".((float) $facsrc->multicurrency_tx);
341 $sql .= ", ".((int) $this->suspended);
342 $sql .= ")";
343
344 if ($this->db->query($sql)) {
345 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_rec");
346
347 // Fields used into addline later
348 $this->fk_multicurrency = $facsrc->fk_multicurrency;
349 $this->multicurrency_code = $facsrc->multicurrency_code;
350 $this->multicurrency_tx = $facsrc->multicurrency_tx;
351
352 // Add lines
353 $fk_parent_line = 0;
354
355 $num = count($facsrc->lines);
356 for ($i = 0; $i < $num; $i++) {
357 if (!empty($onlylines) && !in_array($facsrc->lines[$i]->id, $onlylines)) {
358 continue; // Skip unselected lines
359 }
360
361 // Reset fk_parent_line for no child products and special product
362 if (($facsrc->lines[$i]->product_type != 9 && empty($facsrc->lines[$i]->fk_parent_line)) || $facsrc->lines[$i]->product_type == 9) {
363 $fk_parent_line = 0;
364 }
365
366 $tva_tx = $facsrc->lines[$i]->tva_tx;
367 if (!empty($facsrc->lines[$i]->vat_src_code) && !preg_match('/\‍(/', (string) $tva_tx)) {
368 $tva_tx .= ' ('.$facsrc->lines[$i]->vat_src_code.')';
369 }
370
371 $default_start_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_START');
372 $default_end_fill = getDolGlobalInt('INVOICEREC_SET_AUTOFILL_DATE_END');
373
374 $result_insert = $this->addline(
375 $facsrc->lines[$i]->desc,
376 $facsrc->lines[$i]->subprice,
377 $facsrc->lines[$i]->qty,
378 $tva_tx,
379 $facsrc->lines[$i]->localtax1_tx,
380 $facsrc->lines[$i]->localtax2_tx,
381 $facsrc->lines[$i]->fk_product,
382 $facsrc->lines[$i]->remise_percent,
383 'HT',
384 $facsrc->lines[$i]->info_bits,
385 '',
386 0,
387 $facsrc->lines[$i]->product_type,
388 $facsrc->lines[$i]->rang,
389 $facsrc->lines[$i]->special_code,
390 $facsrc->lines[$i]->label,
391 $facsrc->lines[$i]->fk_unit,
392 $facsrc->lines[$i]->multicurrency_subprice,
393 $default_start_fill,
394 $default_end_fill,
395 null,
396 $facsrc->lines[$i]->pa_ht,
397 $fk_parent_line
398 );
399
400 // Defined the new fk_parent_line
401 if ($result_insert > 0 && $facsrc->lines[$i]->product_type == 9) {
402 $fk_parent_line = $result_insert;
403 }
404
405 if ($result_insert < 0) {
406 $error++;
407 } else {
408 $objectline = new FactureLigneRec($this->db);
409
410 $result2 = $objectline->fetch($result_insert);
411 if ($result2 > 0) {
412 // Extrafields
413 if (method_exists($facsrc->lines[$i], 'fetch_optionals')) {
414 $facsrc->lines[$i]->fetch_optionals($facsrc->lines[$i]->id);
415 $objectline->array_options = $facsrc->lines[$i]->array_options;
416 }
417
418 $result = $objectline->insertExtraFields();
419 if ($result < 0) {
420 $error++;
421 }
422 } elseif ($result2 < 0) {
423 $this->errors[] = $objectline->error;
424 $error++;
425 }
426 }
427 }
428
429 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) { // To use new linkedObjectsIds instead of old linked_objects
430 $this->linked_objects = $this->linkedObjectsIds; // TODO Replace linked_objects with linkedObjectsIds
431 }
432
433 // Add object linked
434 if (!$error && $this->id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
435 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
436 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, ...))
437 foreach ($tmp_origin_id as $origin_id) {
438 $ret = $this->add_object_linked($origin, $origin_id);
439 if (!$ret) {
440 $this->error = $this->db->lasterror();
441 $error++;
442 }
443 }
444 } else { // Old behaviour, if linked_object has only one link per type, so is something like array('contract'=>id1))
445 $origin_id = $tmp_origin_id;
446 $ret = $this->add_object_linked($origin, $origin_id);
447 if (!$ret) {
448 $this->error = $this->db->lasterror();
449 $error++;
450 }
451 }
452 }
453 }
454
455 if (!$error) {
456 $result = $this->insertExtraFields();
457 if ($result < 0) {
458 $error++;
459 }
460 }
461
462 if (!$error && !$notrigger) {
463 // Call trigger
464 $result = $this->call_trigger('BILLREC_CREATE', $user);
465 if ($result < 0) {
466 $this->db->rollback();
467 return -2;
468 }
469 // End call triggers
470 }
471
472 if ($error) {
473 $this->db->rollback();
474 return -3;
475 } else {
476 $this->db->commit();
477 return $this->id;
478 }
479 } else {
480 $this->error = $this->db->lasterror();
481 $this->db->rollback();
482 return -2;
483 }
484 } else {
485 $this->db->rollback();
486 return -1;
487 }
488 }
489
490
498 public function update(User $user, $notrigger = 0)
499 {
500 $error = 0;
501
502 $sql = "UPDATE ".MAIN_DB_PREFIX."facture_rec SET";
503 $sql .= " entity = ".((int) $this->entity).",";
504 $sql .= " titre = '".$this->db->escape($this->title)."',";
505 $sql .= " suspended = ".((int) $this->suspended).",";
506 $sql .= " fk_soc = ".((int) $this->socid).",";
507 $sql .= " total_tva = ".((float) $this->total_tva).",";
508 $sql .= " localtax1 = ".((float) $this->total_localtax1).",";
509 $sql .= " localtax2 = ".((float) $this->total_localtax2).",";
510 $sql .= " total_ht = ".((float) $this->total_ht).",";
511 $sql .= " total_ttc = ".((float) $this->total_ttc);
512 // TODO Add missing fields
513 $sql .= " WHERE rowid = ".((int) $this->id);
514
515 $this->db->begin();
516
517 dol_syslog(get_class($this)."::update", LOG_DEBUG);
518
519 $resql = $this->db->query($sql);
520 if ($resql) {
521 if (!$error) {
522 $result = $this->insertExtraFields();
523 if ($result < 0) {
524 $error++;
525 }
526 }
527
528 if (!$error && !$notrigger) {
529 // Call trigger
530 $result = $this->call_trigger('BILLREC_MODIFY', $user);
531 if ($result < 0) {
532 $this->db->rollback();
533 return -2;
534 }
535 // End call triggers
536 }
537 $this->db->commit();
538 return 1;
539 } else {
540 $this->error = $this->db->lasterror();
541 $this->db->rollback();
542 return -1;
543 }
544 }
545
556 public function fetch($rowid, $ref = '', $ref_ext = '', $noextrafields = 0, $nolines = 0)
557 {
558 dol_syslog('FactureRec::fetch', LOG_DEBUG);
559
560 $sql = 'SELECT f.rowid, f.entity, f.titre as title, f.suspended, f.fk_soc, f.subtype, f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc';
561 $sql .= ', f.date_lim_reglement as dlr';
562 $sql .= ', f.note_private, f.note_public, f.fk_user_author';
563 $sql .= ', f.modelpdf as model_pdf';
564 $sql .= ', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project';
565 $sql .= ', f.fk_account';
566 $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';
567 $sql .= ', f.generate_pdf';
568 $sql .= ", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
569 $sql .= ', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
570 $sql .= ', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
571 //$sql.= ', el.fk_source';
572 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as f';
573 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
574 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'c_paiement as p ON f.fk_mode_reglement = p.id';
575 //$sql.= " LEFT JOIN ".MAIN_DB_PREFIX."element_element as el ON el.fk_target = f.rowid AND el.targettype = 'facture'";
576 $sql .= ' WHERE f.entity IN ('.getEntity('invoice').')';
577 if ($rowid) {
578 $sql .= ' AND f.rowid = '.((int) $rowid);
579 } elseif ($ref) {
580 $sql .= " AND f.titre = '".$this->db->escape($ref)."'";
581 } else {
582 $sql .= ' AND f.rowid = 0';
583 }
584 /* This field are not used for template invoice
585 if ($ref_ext) $sql.= " AND f.ref_ext='".$this->db->escape($ref_ext)."'";
586 */
587
588 $result = $this->db->query($sql);
589 if ($result) {
590 if ($this->db->num_rows($result)) {
591 $obj = $this->db->fetch_object($result);
592
593 $this->id = $obj->rowid;
594 $this->entity = $obj->entity;
595 $this->titre = $obj->title; // deprecated
596 $this->title = $obj->title;
597 $this->ref = $obj->title;
598 $this->subtype = $obj->subtype;
599 $this->suspended = $obj->suspended;
600 $this->total_ht = $obj->total_ht;
601 $this->total_tva = $obj->total_tva;
602 $this->total_localtax1 = $obj->localtax1;
603 $this->total_localtax2 = $obj->localtax2;
604 $this->total_ttc = $obj->total_ttc;
605 $this->socid = $obj->fk_soc;
606 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
607 $this->mode_reglement_id = $obj->fk_mode_reglement;
608 $this->mode_reglement_code = $obj->mode_reglement_code;
609 $this->mode_reglement = $obj->mode_reglement_libelle;
610 $this->cond_reglement_id = $obj->fk_cond_reglement;
611 $this->cond_reglement_code = $obj->cond_reglement_code;
612 $this->cond_reglement = $obj->cond_reglement_libelle;
613 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
614 $this->fk_project = $obj->fk_project;
615 $this->fk_account = $obj->fk_account;
616 $this->note_private = $obj->note_private;
617 $this->note_public = $obj->note_public;
618 $this->user_author = $obj->fk_user_author;
619 $this->model_pdf = $obj->model_pdf;
620 //$this->special_code = $obj->special_code;
621 $this->frequency = $obj->frequency;
622 $this->unit_frequency = $obj->unit_frequency;
623 $this->date_when = $this->db->jdate($obj->date_when);
624 $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
625 $this->nb_gen_done = $obj->nb_gen_done;
626 $this->nb_gen_max = $obj->nb_gen_max;
627 $this->usenewprice = $obj->usenewprice;
628 $this->auto_validate = $obj->auto_validate;
629 $this->generate_pdf = $obj->generate_pdf;
630
631 // Multicurrency
632 $this->fk_multicurrency = $obj->fk_multicurrency;
633 $this->multicurrency_code = $obj->multicurrency_code;
634 $this->multicurrency_tx = $obj->multicurrency_tx;
635 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
636 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
637 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
638
639 // Retrieve all extrafield
640 // fetch optionals attributes and labels
641 if (empty($noextrafields)) {
642 $result = $this->fetch_optionals();
643 if ($result < 0) {
644 $this->error = $this->db->lasterror();
645 return -4;
646 }
647 }
648
649 // Retrieve lines
650 if (empty($nolines)) {
651 $result = $this->fetch_lines();
652 if ($result < 0) {
653 $this->error = $this->db->lasterror();
654 return -3;
655 }
656 }
657
658 return 1;
659 } else {
660 $this->error = 'Bill with id '.$rowid.' or ref '.$ref.' not found';
661 dol_syslog('Facture::Fetch Error '.$this->error, LOG_ERR);
662 return -2;
663 }
664 } else {
665 $this->error = $this->db->error();
666 return -1;
667 }
668 }
669
670
676 public function getLinesArray()
677 {
678 return $this->fetch_lines();
679 }
680
681
682 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
688 public function fetch_lines()
689 {
690 // phpcs:enable
691
692 $this->lines = array();
693
694 dol_syslog('FactureRec::fetch_lines', LOG_DEBUG);
695
696 $sql = 'SELECT l.rowid, l.fk_facture, l.fk_product, l.fk_parent_line, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx, ';
697 $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
698 $sql .= ' l.info_bits, l.date_start_fill, l.date_end_fill, l.total_ht, l.total_tva, l.total_ttc, l.fk_product_fournisseur_price, l.buy_price_ht as pa_ht,';
699 $sql .= ' l.rang, l.special_code,';
700 $sql .= ' l.fk_unit, l.fk_contract_line,';
701 $sql .= ' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
702 $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
703 $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
704 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
705 $sql .= ' WHERE l.fk_facture = '.((int) $this->id);
706 $sql .= ' ORDER BY l.rang';
707
708 $result = $this->db->query($sql);
709 if ($result) {
710 $num = $this->db->num_rows($result);
711 $i = 0;
712 while ($i < $num) {
713 $objp = $this->db->fetch_object($result);
714 $line = new FactureLigneRec($this->db);
715
716 $line->id = $objp->rowid;
717 $line->rowid = $objp->rowid;
718 $line->fk_facture = $objp->fk_facture;
719 $line->fk_parent_line = $objp->fk_parent_line;
720 $line->desc = $objp->description; // Description line
721 $line->description = $objp->description; // Description line
722 $line->ref = $objp->product_ref; // Ref product
723 $line->product_ref = $objp->product_ref; // Ref product
724 $line->libelle = $objp->product_label; // deprecated
725 $line->product_label = $objp->product_label; // Label product
726 $line->product_desc = $objp->product_desc; // Description product
727 $line->product_type = $objp->product_type; // Type of line
728 $line->fk_product_type = $objp->fk_product_type; // Type of product
729 $line->qty = $objp->qty;
730 $line->subprice = $objp->subprice;
731
732 $line->label = $objp->custom_label; // @deprecated
733
734 $line->vat_src_code = $objp->vat_src_code;
735 $line->tva_tx = $objp->tva_tx;
736 $line->localtax1_tx = $objp->localtax1_tx;
737 $line->localtax2_tx = $objp->localtax2_tx;
738 $line->localtax1_type = $objp->localtax1_type;
739 $line->localtax2_type = $objp->localtax2_type;
740 $line->remise_percent = $objp->remise_percent;
741 //$line->fk_remise_except = $objp->fk_remise_except;
742 $line->fk_product = $objp->fk_product;
743 $line->date_start_fill = $objp->date_start_fill;
744 $line->date_end_fill = $objp->date_end_fill;
745 $line->info_bits = $objp->info_bits;
746 $line->total_ht = $objp->total_ht;
747 $line->total_tva = $objp->total_tva;
748 $line->total_ttc = $objp->total_ttc;
749
750 $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
751 $line->fk_fournprice = $objp->fk_product_fournisseur_price; // For backward compatibility
752
753 $marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
754
755 $line->buyprice = $marginInfos[0];
756 $line->pa_ht = $marginInfos[0]; // For backward compatibility
757 $line->marge_tx = $marginInfos[1];
758 $line->marque_tx = $marginInfos[2];
759 $line->rang = $objp->rang;
760 $line->special_code = $objp->special_code;
761 $line->fk_unit = $objp->fk_unit;
762 $line->fk_contract_line = $objp->fk_contract_line;
763
764 // Ne plus utiliser
765 $line->price = $objp->price;
766 $line->remise = $objp->remise;
767
768 $line->fetch_optionals();
769
770 // Multicurrency
771 $line->fk_multicurrency = $objp->fk_multicurrency;
772 $line->multicurrency_code = $objp->multicurrency_code;
773 $line->multicurrency_subprice = $objp->multicurrency_subprice;
774 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
775 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
776 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
777
778 $this->lines[$i] = $line;
779
780 $i++;
781 }
782
783 $this->db->free($result);
784 return 1;
785 } else {
786 $this->error = $this->db->lasterror();
787 return -3;
788 }
789 }
790
791
800 public function delete(User $user, $notrigger = 0, $idwarehouse = -1)
801 {
802 $rowid = $this->id;
803
804 dol_syslog(get_class($this)."::delete rowid=".((int) $rowid), LOG_DEBUG);
805
806 $error = 0;
807 $this->db->begin();
808
809 $main = MAIN_DB_PREFIX.'facturedet_rec';
810 $ef = $main."_extrafields";
811
812 $sqlef = "DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main." WHERE fk_facture = ".((int) $rowid).")";
813 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facturedet_rec WHERE fk_facture = ".((int) $rowid);
814
815 if ($this->db->query($sqlef) && $this->db->query($sql)) {
816 $sql = "DELETE FROM ".MAIN_DB_PREFIX."facture_rec WHERE rowid = ".((int) $rowid);
817 dol_syslog($sql);
818 if ($this->db->query($sql)) {
819 // Delete linked object
820 $res = $this->deleteObjectLinked();
821 if ($res < 0) {
822 $error = -3;
823 }
824 // Delete extrafields
825 $res = $this->deleteExtraFields();
826 if ($res < 0) {
827 $error = -4;
828 }
829 } else {
830 $this->error = $this->db->lasterror();
831 $error = -1;
832 }
833 } else {
834 $this->error = $this->db->lasterror();
835 $error = -2;
836 }
837 if (!$error && !$notrigger) {
838 // Call trigger
839 $result = $this->call_trigger('BILLREC_DELETE', $user);
840 if ($result < 0) {
841 $error++;
842 }
843 // End call triggers
844 }
845 if (!$error) {
846 $this->db->commit();
847 return 1;
848 } else {
849 $this->db->rollback();
850 return $error;
851 }
852 }
853
854
883 public 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 = 0, $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, $fk_fournprice = null, $pa_ht = 0, $fk_parent_line = 0)
884 {
885 global $mysoc;
886
887 $facid = $this->id;
888
889 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,pa_ht=$pa_ht", LOG_DEBUG);
890 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
891
892 // Check parameters
893 if ($type < 0) {
894 return -1;
895 }
896
897 $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
898
899 // Clean vat code
900 $reg = array();
901 $vat_src_code = '';
902 if (preg_match('/\‍((.*)\‍)/', (string) $txtva, $reg)) {
903 $vat_src_code = $reg[1];
904 $txtva = preg_replace('/\s*\‍(.*\‍)/', '', (string) $txtva); // Remove code from vatrate.
905 }
906
907
908 // Clean parameters
909 $remise_percent = price2num($remise_percent);
910 if (empty($remise_percent)) {
911 $remise_percent = 0;
912 }
913 $qty = price2num($qty);
914 $pu_ht = price2num($pu_ht);
915 $pu_ttc = price2num($pu_ttc);
916 if (!preg_match('/\‍((.*)\‍)/', $txtva)) {
917 $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
918 }
919 $txlocaltax1 = price2num($txlocaltax1);
920 $txlocaltax2 = price2num($txlocaltax2);
921 if (empty($txtva)) {
922 $txtva = 0;
923 }
924 if (empty($txlocaltax1)) {
925 $txlocaltax1 = 0;
926 }
927 if (empty($txlocaltax2)) {
928 $txlocaltax2 = 0;
929 }
930 if (empty($info_bits)) {
931 $info_bits = 0;
932 }
933
934 if ($price_base_type == 'HT') {
935 $pu = $pu_ht;
936 } else {
937 $pu = $pu_ttc;
938 }
939
940 // Calcul du total TTC et de la TVA pour la ligne a partir de
941 // qty, pu, remise_percent et txtva
942 // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
943 // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
944
945 $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);
946 $total_ht = $tabprice[0];
947 $total_tva = $tabprice[1];
948 $total_ttc = $tabprice[2];
949 $total_localtax1 = $tabprice[9];
950 $total_localtax2 = $tabprice[10];
951 $pu_ht = $tabprice[3];
952
953 // MultiCurrency
954 $multicurrency_total_ht = $tabprice[16];
955 $multicurrency_total_tva = $tabprice[17];
956 $multicurrency_total_ttc = $tabprice[18];
957 $pu_ht_devise = $tabprice[19];
958
959 $product_type = $type;
960 if ($fk_product) {
961 $product = new Product($this->db);
962 $result = $product->fetch($fk_product);
963 $product_type = $product->type;
964 }
965
966 if (empty($fk_parent_line) || $fk_parent_line < 0) {
967 $fk_parent_line = 0;
968 }
969
970 // Rank to use
971 $ranktouse = $rang;
972 if ($ranktouse == -1) {
973 $rangmax = $this->line_max(0);
974 $ranktouse = $rangmax + 1;
975 }
976
977 $sql = "INSERT INTO ".MAIN_DB_PREFIX."facturedet_rec (";
978 $sql .= "fk_facture";
979 $sql .= ", fk_parent_line";
980 $sql .= ", label";
981 $sql .= ", description";
982 $sql .= ", price";
983 $sql .= ", qty";
984 $sql .= ", tva_tx";
985 $sql .= ", vat_src_code";
986 $sql .= ", localtax1_tx";
987 $sql .= ", localtax1_type";
988 $sql .= ", localtax2_tx";
989 $sql .= ", localtax2_type";
990 $sql .= ", fk_product";
991 $sql .= ", product_type";
992 $sql .= ", remise_percent";
993 $sql .= ", subprice";
994 $sql .= ", remise";
995 $sql .= ", total_ht";
996 $sql .= ", total_tva";
997 $sql .= ", total_localtax1";
998 $sql .= ", total_localtax2";
999 $sql .= ", total_ttc";
1000 $sql .= ", date_start_fill";
1001 $sql .= ", date_end_fill";
1002 $sql .= ", fk_product_fournisseur_price";
1003 $sql .= ", buy_price_ht";
1004 $sql .= ", info_bits";
1005 $sql .= ", rang";
1006 $sql .= ", special_code";
1007 $sql .= ", fk_unit";
1008 $sql .= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
1009 $sql .= ") VALUES (";
1010 $sql .= " ".((int) $facid);
1011 $sql .= ", ".($fk_parent_line > 0 ? ((int) $fk_parent_line) : "null");
1012 $sql .= ", ".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1013 $sql .= ", '".$this->db->escape($desc)."'";
1014 $sql .= ", ".price2num($pu_ht);
1015 $sql .= ", ".price2num($qty);
1016 $sql .= ", ".price2num($txtva);
1017 $sql .= ", '".$this->db->escape($vat_src_code)."'";
1018 $sql .= ", ".price2num($txlocaltax1);
1019 $sql .= ", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] : '')."'";
1020 $sql .= ", ".price2num($txlocaltax2);
1021 $sql .= ", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] : '')."'";
1022 $sql .= ", ".(!empty($fk_product) ? "'".$this->db->escape($fk_product)."'" : "null");
1023 $sql .= ", ".((int) $product_type);
1024 $sql .= ", ".price2num($remise_percent);
1025 $sql .= ", ".price2num($pu_ht);
1026 $sql .= ", null";
1027 $sql .= ", ".price2num($total_ht);
1028 $sql .= ", ".price2num($total_tva);
1029 $sql .= ", ".price2num($total_localtax1);
1030 $sql .= ", ".price2num($total_localtax2);
1031 $sql .= ", ".price2num($total_ttc);
1032 $sql .= ", ".(int) $date_start_fill;
1033 $sql .= ", ".(int) $date_end_fill;
1034 $sql .= ", ".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1035 $sql .= ", ".($pa_ht ? price2num($pa_ht) : 0);
1036 $sql .= ", ".((int) $info_bits);
1037 $sql .= ", ".((int) $ranktouse);
1038 $sql .= ", ".((int) $special_code);
1039 $sql .= ", ".($fk_unit ? ((int) $fk_unit) : "null");
1040 $sql .= ", ".(int) $this->fk_multicurrency;
1041 $sql .= ", '".$this->db->escape($this->multicurrency_code)."'";
1042 $sql .= ", ".price2num($pu_ht_devise, 'CU');
1043 $sql .= ", ".price2num($multicurrency_total_ht, 'CT');
1044 $sql .= ", ".price2num($multicurrency_total_tva, 'CT');
1045 $sql .= ", ".price2num($multicurrency_total_ttc, 'CT');
1046 $sql .= ")";
1047
1048 dol_syslog(get_class($this)."::addline", LOG_DEBUG);
1049 if ($this->db->query($sql)) {
1050 $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX."facturedet_rec");
1051 $this->id = $facid;
1052 $this->update_price(1);
1053 return $lineId;
1054 } else {
1055 $this->error = $this->db->lasterror();
1056 return -1;
1057 }
1058 }
1059
1090 public 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 = 0, $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, $fk_fournprice = null, $pa_ht = 0, $fk_parent_line = 0)
1091 {
1092 global $mysoc;
1093
1094 $facid = $this->id;
1095
1096 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);
1097 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
1098
1099 // Clean parameters
1100 if (empty($remise_percent)) {
1101 $remise_percent = 0;
1102 }
1103
1104 // Check parameters
1105 if ($type < 0) {
1106 return -1;
1107 }
1108
1109 // Clean parameters
1110 $remise_percent = price2num($remise_percent);
1111 $qty = price2num($qty);
1112 if (empty($info_bits)) {
1113 $info_bits = 0;
1114 }
1115 $pu_ht = price2num($pu_ht);
1116 $pu_ttc = price2num($pu_ttc);
1117 $pu_ht_devise = price2num($pu_ht_devise);
1118 if (!preg_match('/\‍((.*)\‍)/', (string) $txtva)) {
1119 $txtva = price2num($txtva); // $txtva can have format '5.0(XXX)' or '5'
1120 }
1121 $txlocaltax1 = price2num($txlocaltax1);
1122 $txlocaltax2 = price2num($txlocaltax2);
1123 if (empty($txlocaltax1)) {
1124 $txlocaltax1 = 0;
1125 }
1126 if (empty($txlocaltax2)) {
1127 $txlocaltax2 = 0;
1128 }
1129
1130 if (empty($this->multicurrency_subprice)) {
1131 $this->multicurrency_subprice = 0;
1132 }
1133 if (empty($this->multicurrency_total_ht)) {
1134 $this->multicurrency_total_ht = 0;
1135 }
1136 if (empty($this->multicurrency_total_tva)) {
1137 $this->multicurrency_total_tva = 0;
1138 }
1139 if (empty($this->multicurrency_total_ttc)) {
1140 $this->multicurrency_total_ttc = 0;
1141 }
1142
1143 if ($price_base_type == 'HT') {
1144 $pu = $pu_ht;
1145 } else {
1146 $pu = $pu_ttc;
1147 }
1148
1149 // Calculate total with, without tax and tax from qty, pu, remise_percent and txtva
1150 // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
1151 // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
1152
1153 $localtaxes_type = getLocalTaxesFromRate($txtva, 0, $this->thirdparty, $mysoc);
1154
1155 // Clean vat code
1156 $vat_src_code = '';
1157 $reg = array();
1158 if (preg_match('/\‍((.*)\‍)/', $txtva, $reg)) {
1159 $vat_src_code = $reg[1];
1160 $txtva = preg_replace('/\s*\‍(.*\‍)/', '', $txtva); // Remove code into vatrate.
1161 }
1162
1163 $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);
1164
1165 $total_ht = $tabprice[0];
1166 $total_tva = $tabprice[1];
1167 $total_ttc = $tabprice[2];
1168 $total_localtax1 = $tabprice[9];
1169 $total_localtax2 = $tabprice[10];
1170 $pu_ht = $tabprice[3];
1171 $pu_tva = $tabprice[4];
1172 $pu_ttc = $tabprice[5];
1173
1174 // MultiCurrency
1175 $multicurrency_total_ht = $tabprice[16];
1176 $multicurrency_total_tva = $tabprice[17];
1177 $multicurrency_total_ttc = $tabprice[18];
1178 $pu_ht_devise = $tabprice[19];
1179
1180 $product_type = $type;
1181 if ($fk_product) {
1182 $product = new Product($this->db);
1183 $result = $product->fetch($fk_product);
1184 $product_type = $product->type;
1185 }
1186
1187 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1188 $fk_parent_line = 0;
1189 }
1190
1191 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET ";
1192 $sql .= "fk_facture = ".((int) $facid);
1193 $sql .= ", fk_parent_line = ".($fk_parent_line > 0 ? ((int) $fk_parent_line) : "null");
1194 $sql .= ", label=".(!empty($label) ? "'".$this->db->escape($label)."'" : "null");
1195 $sql .= ", description='".$this->db->escape($desc)."'";
1196 $sql .= ", price=".price2num($pu_ht);
1197 $sql .= ", qty=".price2num($qty);
1198 $sql .= ", tva_tx=".price2num($txtva);
1199 $sql .= ", vat_src_code='".$this->db->escape($vat_src_code)."'";
1200 $sql .= ", localtax1_tx=".((float) $txlocaltax1);
1201 $sql .= ", localtax1_type='".$this->db->escape($localtaxes_type[0])."'";
1202 $sql .= ", localtax2_tx=".((float) $txlocaltax2);
1203 $sql .= ", localtax2_type='".$this->db->escape($localtaxes_type[2])."'";
1204 $sql .= ", fk_product=".(!empty($fk_product) ? "'".$this->db->escape($fk_product)."'" : "null");
1205 $sql .= ", product_type=".((int) $product_type);
1206 $sql .= ", remise_percent='".price2num($remise_percent)."'";
1207 $sql .= ", subprice='".price2num($pu_ht)."'";
1208 $sql .= ", total_ht='".price2num($total_ht)."'";
1209 $sql .= ", total_tva='".price2num($total_tva)."'";
1210 $sql .= ", total_localtax1='".price2num($total_localtax1)."'";
1211 $sql .= ", total_localtax2='".price2num($total_localtax2)."'";
1212 $sql .= ", total_ttc='".price2num($total_ttc)."'";
1213 $sql .= ", date_start_fill=".((int) $date_start_fill);
1214 $sql .= ", date_end_fill=".((int) $date_end_fill);
1215 $sql .= ", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice : 'null');
1216 $sql .= ", buy_price_ht=".($pa_ht ? price2num($pa_ht) : 0);
1217 $sql .= ", info_bits=".((int) $info_bits);
1218 $sql .= ", rang=".((int) $rang);
1219 $sql .= ", special_code=".((int) $special_code);
1220 $sql .= ", fk_unit=".($fk_unit ? "'".$this->db->escape($fk_unit)."'" : "null");
1221 $sql .= ', multicurrency_subprice = '.price2num($pu_ht_devise);
1222 $sql .= ', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1223 $sql .= ', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1224 $sql .= ', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1225 $sql .= " WHERE rowid = ".((int) $rowid);
1226
1227 dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
1228 if ($this->db->query($sql)) {
1229 $this->id = $facid;
1230 $this->update_price(1);
1231 return 1;
1232 } else {
1233 $this->error = $this->db->lasterror();
1234 return -1;
1235 }
1236 }
1237
1238
1244 public function getNextDate()
1245 {
1246 if (empty($this->date_when)) {
1247 return false;
1248 }
1249 return dol_time_plus_duree($this->date_when, $this->frequency, $this->unit_frequency);
1250 }
1251
1257 public function isMaxNbGenReached()
1258 {
1259 $ret = false;
1260 if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1261 $ret = true;
1262 }
1263 return $ret;
1264 }
1265
1272 public function strikeIfMaxNbGenReached($ret)
1273 {
1274 return $this->isMaxNbGenReached() ? '<strike>'.$ret.'</strike>' : $ret;
1275 }
1276
1288 public function createRecurringInvoices($restrictioninvoiceid = 0, $forcevalidation = 0, $notrigger = 0)
1289 {
1290 global $conf, $langs, $db, $user, $hookmanager;
1291
1292 $error = 0;
1293 $nb_create = 0;
1294
1295 // Load translation files required by the page
1296 $langs->loadLangs(array("main", "bills"));
1297
1298 $now = dol_now();
1299 $tmparray = dol_getdate($now);
1300 $today = dol_mktime(23, 59, 59, $tmparray['mon'], $tmparray['mday'], $tmparray['year']); // Today is last second of current day
1301
1302 $this->output = '';
1303
1304 dol_syslog("createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid." forcevalidation=".$forcevalidation);
1305
1306 $sql = 'SELECT rowid FROM '.MAIN_DB_PREFIX.'facture_rec';
1307 $sql .= ' WHERE frequency > 0'; // A recurring invoice is an invoice with a frequency
1308 $sql .= " AND (date_when IS NULL OR date_when <= '".$this->db->idate($today)."')";
1309 $sql .= ' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1310 $sql .= ' AND suspended = 0';
1311 $sql .= ' AND entity = '.$conf->entity; // MUST STAY = $conf->entity here
1312 if ($restrictioninvoiceid > 0) {
1313 $sql .= ' AND rowid = '.((int) $restrictioninvoiceid);
1314 }
1315 $sql .= $this->db->order('entity', 'ASC');
1316 //print $sql;exit;
1317 $parameters = array(
1318 'restrictioninvoiceid' => $restrictioninvoiceid,
1319 'forcevalidation' => $forcevalidation,
1320 );
1321 $reshook = $hookmanager->executeHooks('beforeCreationOfRecurringInvoices', $parameters, $sql); // note that $sql might be modified by hooks
1322
1323 $resql = $this->db->query($sql);
1324 if ($resql) {
1325 $i = 0;
1326 $num = $this->db->num_rows($resql);
1327
1328 if ($num) {
1329 $this->output .= $langs->trans("FoundXQualifiedRecurringInvoiceTemplate", $num)."\n";
1330 } else {
1331 $this->output .= $langs->trans("NoQualifiedRecurringInvoiceTemplateFound");
1332 }
1333
1334 $saventity = $conf->entity;
1335
1336 while ($i < $num) { // Loop on each template invoice. If $num = 0, test is false at first pass.
1337 $line = $this->db->fetch_object($resql);
1338
1339 $this->db->begin();
1340
1341 $invoiceidgenerated = 0;
1342
1343 $facture = null;
1344 $facturerec = new FactureRec($this->db);
1345 $facturerec->fetch($line->rowid);
1346
1347 if ($facturerec->id > 0) {
1348 // Set entity context
1349 $conf->entity = $facturerec->entity;
1350
1351 dol_syslog("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref.", entity=".$facturerec->entity);
1352
1353 $facture = new Facture($this->db);
1354 $facture->fac_rec = $facturerec->id; // We will create $facture from this recurring invoice
1355 $facture->fk_fac_rec_source = $facturerec->id; // We will create $facture from this recurring invoice
1356
1357 $facture->type = self::TYPE_STANDARD;
1358 $facture->subtype = $facturerec->subtype;
1359 $facture->statut = self::STATUS_DRAFT; // deprecated
1360 $facture->status = self::STATUS_DRAFT;
1361 $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.
1362 $facture->socid = $facturerec->socid;
1363 if (!empty($facturerec->fk_multicurrency)) {
1364 $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1365 $facture->multicurrency_code = $facturerec->multicurrency_code;
1366 $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1367 }
1368
1369 if (isset($facture->array_options) && isset($facturerec->array_options)) {
1370 foreach ($facturerec->array_options as $key => $value) {
1371 if (isset($facture->array_options[$key])) {
1372 $facture->array_options[$key] = $value;
1373 }
1374 }
1375 }
1376
1377 $invoiceidgenerated = $facture->create($user);
1378 if ($invoiceidgenerated <= 0) {
1379 $this->errors = $facture->errors;
1380 $this->error = $facture->error;
1381 $error++;
1382 }
1383
1384
1385 if (!$error && ($facturerec->auto_validate || $forcevalidation)) {
1386 $result = $facture->validate($user);
1387 if ($result <= 0) {
1388 $this->errors = $facture->errors;
1389 $this->error = $facture->error;
1390 $error++;
1391 }
1392 }
1393 if (!$error && $facturerec->generate_pdf) {
1394 // We refresh the object in order to have all necessary data (like date_lim_reglement)
1395 $facture->fetch($facture->id);
1396 $result = $facture->generateDocument($facturerec->model_pdf, $langs);
1397 if ($result <= 0) {
1398 $this->errors = $facture->errors;
1399 $this->error = $facture->error;
1400 $error++;
1401 }
1402 }
1403 } else {
1404 $error++;
1405 $this->error = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity."\n";
1406 $this->errors[] = "Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity;
1407 dol_syslog("createRecurringInvoices Failed to load invoice template with id=".$line->rowid.", entity=".$conf->entity);
1408 }
1409
1410 if (!$error && $invoiceidgenerated >= 0) {
1411 $this->db->commit("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1412 dol_syslog("createRecurringInvoices Process invoice template ".$facturerec->ref." is finished with a success generation");
1413 $nb_create++;
1414 $this->output .= $langs->trans("InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref)."\n";
1415 } else {
1416 $this->output .= $langs->trans("InvoiceGeneratedFromTemplateError", $facture->ref, $facturerec->ref, $this->error)."\n";
1417 $this->db->rollback("createRecurringInvoices Process invoice template id=".$facturerec->id.", ref=".$facturerec->ref);
1418 }
1419
1420 $parameters = array(
1421 'cpt' => $i,
1422 'total' => $num,
1423 'errorCount' => $error,
1424 'invoiceidgenerated' => $invoiceidgenerated,
1425 'facturerec' => $facturerec, // it's an object which PHP passes by "reference", so modifiable by hooks.
1426 'this' => $this, // it's an object which PHP passes by "reference", so modifiable by hooks.
1427 );
1428 $reshook = $hookmanager->executeHooks('afterCreationOfRecurringInvoice', $parameters, $facture); // note: $facture can be modified by hooks (warning: $facture can be null)
1429
1430 $i++;
1431 }
1432
1433 $conf->entity = $saventity; // Restore entity context
1434 } else {
1435 dol_print_error($this->db);
1436 }
1437
1438 $this->output = trim($this->output);
1439
1440 return $error ? $error : 0;
1441 }
1442
1455 public function getNomUrl($withpicto = 0, $option = '', $max = 0, $short = 0, $moretitle = '', $notooltip = 0, $save_lastsearch_value = -1)
1456 {
1457 global $langs, $hookmanager;
1458
1459 $result = '';
1460
1461 $label = img_picto('', $this->picto).' <u class="paddingrightonly">'.$langs->trans("RepeatableInvoice").'</u>';
1462 if (!empty($this->ref)) {
1463 $label .= '<br><b>'.$langs->trans('Ref').':</b> '.$this->ref;
1464 }
1465 if ($this->frequency > 0) {
1466 $label .= '<br><b>'.$langs->trans('Frequency').':</b> '.$langs->trans('FrequencyPer_'.$this->unit_frequency, $this->frequency);
1467 }
1468 if (!empty($this->date_last_gen)) {
1469 $label .= '<br><b>'.$langs->trans('DateLastGeneration').':</b> '.dol_print_date($this->date_last_gen, 'dayhour');
1470 }
1471 if ($this->frequency > 0) {
1472 if (!empty($this->date_when)) {
1473 $label .= '<br><b>'.$langs->trans('NextDateToExecution').':</b> ';
1474 $label .= (empty($this->suspended) ? '' : '<strike>').dol_print_date($this->date_when, 'day').(empty($this->suspended) ? '' : '</strike>'); // No hour for this property
1475 if (!empty($this->suspended)) {
1476 $label .= ' ('.$langs->trans("Disabled").')';
1477 }
1478 }
1479 }
1480
1481 $url = DOL_URL_ROOT.'/compta/facture/card-rec.php?facid='.$this->id;
1482
1483 if ($short) {
1484 return $url;
1485 }
1486
1487 if ($option != 'nolink') {
1488 // Add param to save lastsearch_values or not
1489 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1490 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
1491 $add_save_lastsearch_values = 1;
1492 }
1493 if ($add_save_lastsearch_values) {
1494 $url .= '&save_lastsearch_values=1';
1495 }
1496 }
1497
1498 $linkstart = '<a href="'.$url.'" title="'.dol_escape_htmltag($label, 1).'" class="classfortooltip">';
1499 $linkend = '</a>';
1500
1501 $result .= $linkstart;
1502 if ($withpicto) {
1503 $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);
1504 }
1505 if ($withpicto != 2) {
1506 $result .= dol_trunc($this->ref, $max);
1507 }
1508 $result .= $linkend;
1509 global $action;
1510 $hookmanager->initHooks(array($this->element . 'dao'));
1511 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
1512 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
1513 if ($reshook > 0) {
1514 $result = $hookmanager->resPrint;
1515 } else {
1516 $result .= $hookmanager->resPrint;
1517 }
1518 return $result;
1519 }
1520
1528 public function getLibStatut($mode = 0, $alreadypaid = -1)
1529 {
1530 return $this->LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->type) ? 0 : $this->type);
1531 }
1532
1533 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
1544 public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0)
1545 {
1546 // phpcs:enable
1547 global $langs;
1548 $langs->load('bills');
1549
1550 $labelStatus = $langs->transnoentitiesnoconv('Active');
1551 $statusType = 'status0';
1552
1553 //print "$recur,$status,$mode,$alreadypaid,$type";
1554 if ($mode == 0) {
1555 if ($recur) {
1556 if ($status == self::STATUS_SUSPENDED) {
1557 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1558 } else {
1559 $labelStatus = $langs->transnoentitiesnoconv('Active');
1560 }
1561 } else {
1562 if ($status == self::STATUS_SUSPENDED) {
1563 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1564 } else {
1565 $labelStatus = $langs->transnoentitiesnoconv("Draft");
1566 }
1567 }
1568 } elseif ($mode == 1) {
1569 $prefix = 'Short';
1570 if ($recur) {
1571 if ($status == self::STATUS_SUSPENDED) {
1572 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1573 } else {
1574 $labelStatus = $langs->transnoentitiesnoconv('Active');
1575 }
1576 } else {
1577 if ($status == self::STATUS_SUSPENDED) {
1578 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1579 } else {
1580 $labelStatus = $langs->transnoentitiesnoconv("Draft");
1581 }
1582 }
1583 } elseif ($mode == 2) {
1584 if ($recur) {
1585 if ($status == self::STATUS_SUSPENDED) {
1586 $statusType = 'status6';
1587 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1588 } else {
1589 $statusType = 'status4';
1590 $labelStatus = $langs->transnoentitiesnoconv('Active');
1591 }
1592 } else {
1593 if ($status == self::STATUS_SUSPENDED) {
1594 $statusType = 'status6';
1595 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1596 } else {
1597 $statusType = 'status0';
1598 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1599 }
1600 }
1601 } elseif ($mode == 3) {
1602 if ($recur) {
1603 $prefix = 'Short';
1604 if ($status == self::STATUS_SUSPENDED) {
1605 $statusType = 'status6';
1606 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1607 } else {
1608 $statusType = 'status4';
1609 $labelStatus = $langs->transnoentitiesnoconv('Active');
1610 }
1611 } else {
1612 if ($status == self::STATUS_SUSPENDED) {
1613 $statusType = 'status6';
1614 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1615 } else {
1616 $statusType = 'status0';
1617 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1618 }
1619 }
1620 } elseif ($mode == 4) {
1621 $prefix = '';
1622 if ($recur) {
1623 if ($status == self::STATUS_SUSPENDED) {
1624 $statusType = 'status6';
1625 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1626 } else {
1627 $statusType = 'status4';
1628 $labelStatus = $langs->transnoentitiesnoconv('Active');
1629 }
1630 } else {
1631 if ($status == self::STATUS_SUSPENDED) {
1632 $statusType = 'status6';
1633 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1634 } else {
1635 $statusType = 'status0';
1636 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1637 }
1638 }
1639 } elseif ($mode == 5 || $mode == 6) {
1640 $prefix = '';
1641 if ($mode == 5) {
1642 $prefix = 'Short';
1643 }
1644 if ($recur) {
1645 if ($status == self::STATUS_SUSPENDED) {
1646 $statusType = 'status6';
1647 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1648 } else {
1649 $statusType = 'status4';
1650 $labelStatus = $langs->transnoentitiesnoconv('Active');
1651 }
1652 } else {
1653 if ($status == self::STATUS_SUSPENDED) {
1654 $statusType = 'status6';
1655 $labelStatus = $langs->transnoentitiesnoconv('Disabled');
1656 } else {
1657 $statusType = 'status0';
1658 $labelStatus = $langs->transnoentitiesnoconv('Draft');
1659 }
1660 }
1661 }
1662
1663 $labelStatusShort = $labelStatus;
1664
1665 return dolGetStatus($labelStatus, $labelStatusShort, '', $statusType, $mode);
1666 }
1667
1675 public function getNextNumRef($soc, $mode = 'next')
1676 {
1677 // Not used for recurring invoices
1678 return '';
1679 }
1680
1687 public function info($id)
1688 {
1689 $sql = 'SELECT c.rowid, datec, tms as datem,';
1690 $sql .= ' fk_user_author, fk_user_modif';
1691 $sql .= ' FROM '.MAIN_DB_PREFIX.'facture_rec as c';
1692 $sql .= ' WHERE c.rowid = '.((int) $id);
1693
1694 $result = $this->db->query($sql);
1695 if ($result) {
1696 if ($this->db->num_rows($result)) {
1697 $obj = $this->db->fetch_object($result);
1698
1699 $this->id = $obj->rowid;
1700
1701 $this->user_creation_id = $obj->fk_user_author;
1702 $this->user_modification_id = $obj->fk_user_modif;
1703 $this->date_creation = $this->db->jdate($obj->datec);
1704 $this->date_modification = $this->db->jdate($obj->datem);
1705 }
1706 $this->db->free($result);
1707 } else {
1708 dol_print_error($this->db);
1709 }
1710 }
1711
1720 public function initAsSpecimen($option = '')
1721 {
1722 global $langs;
1723
1724 $now = dol_now();
1725 $arraynow = dol_getdate($now);
1726 $nownotime = dol_mktime(0, 0, 0, $arraynow['mon'], $arraynow['mday'], $arraynow['year']);
1727
1728 // Load array of products prodids
1729 $num_prods = 0;
1730 $prodids = array();
1731
1732 $sql = "SELECT rowid";
1733 $sql .= " FROM ".MAIN_DB_PREFIX."product";
1734 $sql .= " WHERE entity IN (".getEntity('product').")";
1735 $sql .= $this->db->plimit(100);
1736
1737 $resql = $this->db->query($sql);
1738 if ($resql) {
1739 $num_prods = $this->db->num_rows($resql);
1740 $i = 0;
1741 while ($i < $num_prods) {
1742 $i++;
1743 $row = $this->db->fetch_row($resql);
1744 $prodids[$i] = $row[0];
1745 }
1746 }
1747
1748 // Initialize parameters
1749 $this->id = 0;
1750 $this->ref = 'SPECIMEN';
1751 $this->title = 'SPECIMEN';
1752 $this->specimen = 1;
1753 $this->socid = 1;
1754 $this->date = $nownotime;
1755 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
1756 $this->cond_reglement_id = 1;
1757 $this->cond_reglement_code = 'RECEP';
1758 $this->date_lim_reglement = $this->calculate_date_lim_reglement();
1759 $this->mode_reglement_id = 0; // Not forced to show payment mode CHQ + VIR
1760 $this->mode_reglement_code = ''; // Not forced to show payment mode CHQ + VIR
1761 $this->note_public = 'This is a comment (public)';
1762 $this->note_private = 'This is a comment (private)';
1763 $this->note = 'This is a comment (private)';
1764 $this->fk_incoterms = 0;
1765 $this->location_incoterms = '';
1766
1767 if (empty($option) || $option != 'nolines') {
1768 // Lines
1769 $nbp = 5;
1770 $xnbp = 0;
1771 while ($xnbp < $nbp) {
1772 $line = new FactureLigne($this->db);
1773 $line->desc = $langs->trans("Description")." ".$xnbp;
1774 $line->qty = 1;
1775 $line->subprice = 100;
1776 $line->tva_tx = 19.6;
1777 $line->localtax1_tx = 0;
1778 $line->localtax2_tx = 0;
1779 $line->remise_percent = 0;
1780 if ($xnbp == 1) { // Qty is negative (product line)
1781 $prodid = mt_rand(1, $num_prods);
1782 $line->fk_product = $prodids[$prodid];
1783 $line->qty = -1;
1784 $line->total_ht = -100;
1785 $line->total_ttc = -119.6;
1786 $line->total_tva = -19.6;
1787 } elseif ($xnbp == 2) { // UP is negative (free line)
1788 $line->subprice = -100;
1789 $line->total_ht = -100;
1790 $line->total_ttc = -119.6;
1791 $line->total_tva = -19.6;
1792 $line->remise_percent = 0;
1793 } elseif ($xnbp == 3) { // Discount is 50% (product line)
1794 $prodid = mt_rand(1, $num_prods);
1795 $line->fk_product = $prodids[$prodid];
1796 $line->total_ht = 50;
1797 $line->total_ttc = 59.8;
1798 $line->total_tva = 9.8;
1799 $line->remise_percent = 50;
1800 } else { // (product line)
1801 $prodid = mt_rand(1, $num_prods);
1802 $line->fk_product = $prodids[$prodid];
1803 $line->total_ht = 100;
1804 $line->total_ttc = 119.6;
1805 $line->total_tva = 19.6;
1806 $line->remise_percent = 00;
1807 }
1808
1809 $this->lines[$xnbp] = $line;
1810 $xnbp++;
1811
1812 $this->total_ht += $line->total_ht;
1813 $this->total_tva += $line->total_tva;
1814 $this->total_ttc += $line->total_ttc;
1815 }
1816 $this->revenuestamp = 0;
1817
1818 // Add a line "offered"
1819 $line = new FactureLigne($this->db);
1820 $line->desc = $langs->trans("Description")." (offered line)";
1821 $line->qty = 1;
1822 $line->subprice = 100;
1823 $line->tva_tx = 19.6;
1824 $line->localtax1_tx = 0;
1825 $line->localtax2_tx = 0;
1826 $line->remise_percent = 100;
1827 $line->total_ht = 0;
1828 $line->total_ttc = 0; // 90 * 1.196
1829 $line->total_tva = 0;
1830 $prodid = mt_rand(1, $num_prods);
1831 $line->fk_product = $prodids[$prodid];
1832
1833 $this->lines[$xnbp] = $line;
1834 $xnbp++;
1835 }
1836
1837 $this->usenewprice = 0;
1838
1839 return 1;
1840 }
1841
1850 public static function replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
1851 {
1852 $tables = array(
1853 'facture_rec'
1854 );
1855
1856 return CommonObject::commonReplaceThirdparty($dbs, $origin_id, $dest_id, $tables);
1857 }
1858
1867 public static function replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
1868 {
1869 $tables = array(
1870 'facturedet_rec'
1871 );
1872
1873 return CommonObject::commonReplaceProduct($dbs, $origin_id, $dest_id, $tables);
1874 }
1875
1884 public function setFrequencyAndUnit($frequency, $unit, $notrigger = 0)
1885 {
1886 global $user;
1887
1888 if (!$this->table_element) {
1889 dol_syslog(get_class($this)."::setFrequencyAndUnit was called on object with property table_element not defined", LOG_ERR);
1890 return -1;
1891 }
1892
1893 if (!empty($frequency) && empty($unit)) {
1894 dol_syslog(get_class($this)."::setFrequencyAndUnit was called on object with params frequency defined but unit not defined", LOG_ERR);
1895 return -2;
1896 }
1897
1898 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1899 $sql .= ' SET frequency = '.($frequency ? $this->db->escape($frequency) : 'null');
1900 if (!empty($unit)) {
1901 $sql .= ', unit_frequency = \''.$this->db->escape($unit).'\'';
1902 }
1903 $sql .= " WHERE rowid = ".((int) $this->id);
1904
1905 dol_syslog(get_class($this)."::setFrequencyAndUnit", LOG_DEBUG);
1906 if ($this->db->query($sql)) {
1907 $this->frequency = $frequency;
1908 if (!empty($unit)) {
1909 $this->unit_frequency = $unit;
1910 }
1911
1912 if (!$notrigger) {
1913 // Call trigger
1914 $result = $this->call_trigger('BILLREC_MODIFY', $user);
1915 if ($result < 0) {
1916 return $result;
1917 }
1918 // End call triggers
1919 }
1920
1921 return 1;
1922 } else {
1923 dol_print_error($this->db);
1924 return -1;
1925 }
1926 }
1927
1936 public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
1937 {
1938 global $user;
1939
1940 if (!$this->table_element) {
1941 dol_syslog(get_class($this)."::setNextDate was called on object with property table_element not defined", LOG_ERR);
1942 return -1;
1943 }
1944 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1945 $sql .= " SET date_when = ".($date ? "'".$this->db->idate($date)."'" : "null");
1946 if ($increment_nb_gen_done > 0) {
1947 $sql .= ', nb_gen_done = nb_gen_done + 1';
1948 }
1949 $sql .= " WHERE rowid = ".((int) $this->id);
1950
1951 dol_syslog(get_class($this)."::setNextDate", LOG_DEBUG);
1952 if ($this->db->query($sql)) {
1953 $this->date_when = $date;
1954 if ($increment_nb_gen_done > 0) {
1955 $this->nb_gen_done++;
1956 }
1957
1958 if (!$notrigger) {
1959 // Call trigger
1960 $result = $this->call_trigger('BILLREC_MODIFY', $user);
1961 if ($result < 0) {
1962 return $result;
1963 }
1964 // End call triggers
1965 }
1966 return 1;
1967 } else {
1968 dol_print_error($this->db);
1969 return -1;
1970 }
1971 }
1972
1980 public function setMaxPeriod($nb, $notrigger = 0)
1981 {
1982 global $user;
1983
1984 if (!$this->table_element) {
1985 dol_syslog(get_class($this)."::setMaxPeriod was called on object with property table_element not defined", LOG_ERR);
1986 return -1;
1987 }
1988
1989 if (empty($nb)) {
1990 $nb = 0;
1991 }
1992
1993 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1994 $sql .= ' SET nb_gen_max = '.((int) $nb);
1995 $sql .= " WHERE rowid = ".((int) $this->id);
1996
1997 dol_syslog(get_class($this)."::setMaxPeriod", LOG_DEBUG);
1998 if ($this->db->query($sql)) {
1999 $this->nb_gen_max = $nb;
2000
2001 if (!$notrigger) {
2002 // Call trigger
2003 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2004 if ($result < 0) {
2005 return $result;
2006 }
2007 // End call triggers
2008 }
2009
2010 return 1;
2011 } else {
2012 dol_print_error($this->db);
2013 return -1;
2014 }
2015 }
2016
2024 public function setAutoValidate($validate, $notrigger = 0)
2025 {
2026 global $user;
2027
2028 if (!$this->table_element) {
2029 dol_syslog(get_class($this)."::setAutoValidate was called on object with property table_element not defined", LOG_ERR);
2030 return -1;
2031 }
2032
2033 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2034 $sql .= ' SET auto_validate = '.((int) $validate);
2035 $sql .= " WHERE rowid = ".((int) $this->id);
2036
2037 dol_syslog(get_class($this)."::setAutoValidate", LOG_DEBUG);
2038 if ($this->db->query($sql)) {
2039 $this->auto_validate = $validate;
2040
2041 if (!$notrigger) {
2042 // Call trigger
2043 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2044 if ($result < 0) {
2045 return $result;
2046 }
2047 // End call triggers
2048 }
2049
2050 return 1;
2051 } else {
2052 dol_print_error($this->db);
2053 return -1;
2054 }
2055 }
2056
2064 public function setGeneratePdf($validate, $notrigger = 0)
2065 {
2066 global $user;
2067
2068 if (!$this->table_element) {
2069 dol_syslog(get_class($this)."::setGeneratePdf was called on object with property table_element not defined", LOG_ERR);
2070 return -1;
2071 }
2072
2073 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2074 $sql .= ' SET generate_pdf = '.((int) $validate);
2075 $sql .= " WHERE rowid = ".((int) $this->id);
2076
2077 dol_syslog(get_class($this)."::setGeneratePdf", LOG_DEBUG);
2078 if ($this->db->query($sql)) {
2079 $this->generate_pdf = $validate;
2080
2081 if (!$notrigger) {
2082 // Call trigger
2083 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2084 if ($result < 0) {
2085 return $result;
2086 }
2087 // End call triggers
2088 }
2089
2090 return 1;
2091 } else {
2092 dol_print_error($this->db);
2093 return -1;
2094 }
2095 }
2096
2104 public function setModelPdf($model, $notrigger = 0)
2105 {
2106 global $user;
2107 if (!$this->table_element) {
2108 dol_syslog(get_class($this)."::setModelPdf was called on object with property table_element not defined", LOG_ERR);
2109 return -1;
2110 }
2111
2112 $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2113 $sql .= " SET modelpdf = '".$this->db->escape($model)."'";
2114 $sql .= " WHERE rowid = ".((int) $this->id);
2115
2116 dol_syslog(get_class($this)."::setModelPdf", LOG_DEBUG);
2117 if ($this->db->query($sql)) {
2118 $this->model_pdf = $model;
2119
2120 if (!$notrigger) {
2121 // Call trigger
2122 $result = $this->call_trigger('BILLREC_MODIFY', $user);
2123 if ($result < 0) {
2124 return $result;
2125 }
2126 // End call triggers
2127 }
2128
2129 return 1;
2130 } else {
2131 dol_print_error($this->db);
2132 return -1;
2133 }
2134 }
2135}
2136
2137
2138
2144{
2148 public $element = 'facturedetrec';
2149
2153 public $table_element = 'facturedet_rec';
2154
2158 public $parent_element = 'facturerec';
2159
2163 public $fk_parent_attribute = 'fk_facture';
2164
2170
2171 public $fk_product_fournisseur_price;
2172 public $fk_fournprice; // For backward compatibility
2173
2174 public $rang;
2175 //public $situation_percent; // Not supported on recurring invoice line
2176
2177 public $desc;
2178 public $description;
2179
2180 public $fk_product_type; // Use instead product_type
2181
2182 public $fk_contract_line;
2183
2184
2192 public function delete(User $user, $notrigger = 0)
2193 {
2194 $error = 0;
2195
2196 $this->db->begin();
2197
2198 if (!$error) {
2199 if (!$notrigger) {
2200 // Call triggers
2201 $result = $this->call_trigger('LINEBILLREC_DELETE', $user);
2202 if ($result < 0) {
2203 $error++;
2204 } // Do also here what you must do to rollback action if trigger fail
2205 // End call triggers
2206 }
2207 }
2208
2209 if (!$error) {
2210 $result = $this->deleteExtraFields();
2211 if ($result < 0) {
2212 $error++;
2213 }
2214 }
2215
2216 if (!$error) {
2217 $sql = 'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.' WHERE rowid='.((int) $this->id);
2218
2219 $res = $this->db->query($sql);
2220 if (!$res) {
2221 $error++;
2222 $this->errors[] = $this->db->lasterror();
2223 }
2224 }
2225
2226 // Commit or rollback
2227 if ($error) {
2228 $this->db->rollback();
2229 return -1;
2230 } else {
2231 $this->db->commit();
2232 return 1;
2233 }
2234 }
2235
2236
2243 public function fetch($rowid)
2244 {
2245 $sql = 'SELECT l.rowid, l.fk_facture, l.fk_parent_line, 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,';
2246 $sql .= ' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2247 $sql .= ' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2248 $sql .= ' l.rang, l.special_code,';
2249 $sql .= ' l.fk_unit, l.fk_contract_line,';
2250 $sql .= ' l.import_key, l.fk_multicurrency,';
2251 $sql .= ' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2252 $sql .= ' l.buy_price_ht, l.fk_product_fournisseur_price,';
2253 $sql .= ' l.fk_user_author, l.fk_user_modif,';
2254 $sql .= ' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2255 $sql .= ' FROM '.MAIN_DB_PREFIX.'facturedet_rec as l';
2256 $sql .= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
2257 $sql .= ' WHERE l.rowid = '.((int) $rowid);
2258 $sql .= ' ORDER BY l.rang';
2259
2260 dol_syslog('FactureRec::fetch', LOG_DEBUG);
2261 $result = $this->db->query($sql);
2262 if ($result) {
2263 $objp = $this->db->fetch_object($result);
2264
2265 $this->id = $objp->rowid;
2266 $this->fk_facture = $objp->fk_facture;
2267 $this->fk_parent_line = $objp->fk_parent_line;
2268 $this->label = $objp->custom_label; // Label line
2269 $this->desc = $objp->description; // Description line
2270 $this->description = $objp->description; // Description line
2271 $this->product_type = $objp->product_type; // Type of line
2272 $this->ref = $objp->product_ref; // Ref product
2273 $this->product_ref = $objp->product_ref; // Ref product
2274 $this->libelle = $objp->product_label; // deprecated
2275 $this->product_label = $objp->product_label; // Label product
2276 $this->product_desc = $objp->product_desc; // Description product
2277 $this->fk_product_type = $objp->fk_product_type; // Type of product
2278 $this->qty = $objp->qty;
2279 $this->price = $objp->price;
2280 $this->subprice = $objp->subprice;
2281 $this->vat_src_code = $objp->vat_src_code;
2282 $this->tva_tx = $objp->tva_tx;
2283 $this->localtax1_tx = $objp->localtax1_tx;
2284 $this->localtax2_tx = $objp->localtax2_tx;
2285 $this->localtax1_type = $objp->localtax1_type;
2286 $this->localtax2_type = $objp->localtax2_type;
2287 $this->remise_percent = $objp->remise_percent;
2288 //$this->fk_remise_except = $objp->fk_remise_except;
2289 $this->fk_product = $objp->fk_product;
2290 $this->date_start_fill = $objp->date_start_fill;
2291 $this->date_end_fill = $objp->date_end_fill;
2292 $this->info_bits = $objp->info_bits;
2293 $this->total_ht = $objp->total_ht;
2294 $this->total_tva = $objp->total_tva;
2295 $this->total_ttc = $objp->total_ttc;
2296
2297 $this->rang = $objp->rang;
2298 $this->special_code = $objp->special_code;
2299 $this->fk_unit = $objp->fk_unit;
2300 $this->fk_contract_line = $objp->fk_contract_line;
2301 $this->import_key = $objp->import_key;
2302 $this->fk_multicurrency = $objp->fk_multicurrency;
2303 $this->multicurrency_code = $objp->multicurrency_code;
2304 $this->multicurrency_subprice = $objp->multicurrency_subprice;
2305 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2306 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2307 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2308
2309 $this->buy_price_ht = $objp->buy_price_ht;
2310
2311 $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2312 $this->fk_user_author = $objp->fk_user_author;
2313 $this->fk_user_modif = $objp->fk_user_modif;
2314
2315 $this->db->free($result);
2316 return 1;
2317 } else {
2318 $this->error = $this->db->lasterror();
2319 return -3;
2320 }
2321 }
2322
2323
2331 public function update(User $user, $notrigger = 0)
2332 {
2333 global $conf;
2334
2335 $error = 0;
2336
2337 // Clean parameters
2338 if (empty($this->fk_parent_line)) {
2339 $this->fk_parent_line = 0;
2340 }
2341
2342 include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
2343
2344 $sql = "UPDATE ".MAIN_DB_PREFIX."facturedet_rec SET";
2345 $sql .= " fk_facture = ".((int) $this->fk_facture);
2346 $sql .= ", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line : "null");
2347 $sql .= ", label=".(!empty($this->label) ? "'".$this->db->escape($this->label)."'" : "null");
2348 $sql .= ", description='".$this->db->escape($this->desc)."'";
2349 $sql .= ", price=".price2num($this->price);
2350 $sql .= ", qty=".price2num($this->qty);
2351 $sql .= ", tva_tx=".price2num($this->tva_tx);
2352 $sql .= ", vat_src_code='".$this->db->escape($this->vat_src_code)."'";
2353 $sql .= ", localtax1_tx=".price2num($this->localtax1_tx);
2354 $sql .= ", localtax1_type='".$this->db->escape($this->localtax1_type)."'";
2355 $sql .= ", localtax2_tx=".price2num($this->localtax2_tx);
2356 $sql .= ", localtax2_type='".$this->db->escape($this->localtax2_type)."'";
2357 $sql .= ", fk_product=".($this->fk_product > 0 ? $this->fk_product : "null");
2358 $sql .= ", product_type=".((int) $this->product_type);
2359 $sql .= ", remise_percent=".price2num($this->remise_percent);
2360 $sql .= ", subprice=".price2num($this->subprice);
2361 $sql .= ", info_bits=".price2num($this->info_bits);
2362 $sql .= ", date_start_fill=".(int) $this->date_start_fill;
2363 $sql .= ", date_end_fill=".(int) $this->date_end_fill;
2364 if (empty($this->skip_update_total)) {
2365 $sql .= ", total_ht=".price2num($this->total_ht);
2366 $sql .= ", total_tva=".price2num($this->total_tva);
2367 $sql .= ", total_localtax1=".price2num($this->total_localtax1);
2368 $sql .= ", total_localtax2=".price2num($this->total_localtax2);
2369 $sql .= ", total_ttc=".price2num($this->total_ttc);
2370 }
2371 $sql .= ", rang=".((int) $this->rang);
2372 $sql .= ", special_code=".((int) $this->special_code);
2373 $sql .= ", fk_unit=".($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'" : "null");
2374 $sql .= ", fk_contract_line=".($this->fk_contract_line ? $this->fk_contract_line : "null");
2375 $sql .= " WHERE rowid = ".((int) $this->id);
2376
2377 $this->db->begin();
2378
2379 dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
2380
2381 $resql = $this->db->query($sql);
2382 if ($resql) {
2383 if (!$error) {
2384 $result = $this->insertExtraFields();
2385 if ($result < 0) {
2386 $error++;
2387 }
2388 }
2389
2390 if (!$error && !$notrigger) {
2391 // Call trigger
2392 $result = $this->call_trigger('LINEBILLREC_MODIFY', $user);
2393 if ($result < 0) {
2394 $error++;
2395 }
2396 // End call triggers
2397 }
2398
2399 if ($error) {
2400 $this->db->rollback();
2401 return -2;
2402 } else {
2403 $this->db->commit();
2404 return 1;
2405 }
2406 } else {
2407 $this->error = $this->db->lasterror();
2408 $this->db->rollback();
2409 return -2;
2410 }
2411 }
2412}
$object ref
Definition info.php:79
Superclass for invoices classes.
const TYPE_STANDARD
Standard invoice.
calculate_date_lim_reglement($cond_reglement=0)
Returns an invoice payment deadline based on the invoice settlement conditions and billing date.
const STATUS_DRAFT
Draft status.
Parent class of all other business classes for details of elements (invoices, contracts,...
fetch_optionals($rowid=null, $optionsArray=null)
Function to get extra fields of an object into $this->array_options This method is in most cases call...
update_price($exclspec=0, $roundingadjust='auto', $nodatabaseupdate=0, $seller=null)
Update total_ht, total_ttc, total_vat, total_localtax1, total_localtax2 for an object (sum of lines).
add_object_linked($origin=null, $origin_id=null, $f_user=null, $notrigger=0)
Add an object link into llx_element_element.
deleteObjectLinked($sourceid=null, $sourcetype='', $targetid=null, $targettype='', $rowid=0, $f_user=null, $notrigger=0)
Delete all links between an object $this.
deleteExtraFields()
Delete all extra fields values for the current object.
static commonReplaceThirdparty(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a thirdparty id with another one.
static commonReplaceProduct(DoliDB $dbs, $origin_id, $dest_id, array $tables, $ignoreerrors=0)
Function used to replace a product id with another one.
line_max($fk_parent_line=0)
Get max value used for position of line (rang)
insertExtraFields($trigger='', $userused=null)
Add/Update all extra fields values for the current object.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage invoices.
Class to manage invoice lines.
Class to manage invoice lines of templates.
$fk_parent_line
Id parent line.
fetch($rowid)
Get line of template invoice.
$fk_facture
From llx_facturedet_rec Id facture.
update(User $user, $notrigger=0)
Update a line to invoice_rec.
Class to manage invoice templates.
setFrequencyAndUnit($frequency, $unit, $notrigger=0)
Update frequency and unit.
getLibStatut($mode=0, $alreadypaid=-1)
Return label of object status.
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=0, $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, $fk_fournprice=null, $pa_ht=0, $fk_parent_line=0)
Add a line to invoice.
LibStatut($recur, $status, $mode=0, $alreadypaid=-1, $type=0)
Return label of a status.
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=0, $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, $fk_fournprice=null, $pa_ht=0, $fk_parent_line=0)
Update a line to invoice.
fetch_lines()
Get lines of template invoices into this->lines.
info($id)
Load miscellaneous information for tab "Info".
getLinesArray()
Create an array of invoice lines.
static replaceThirdparty(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a thirdparty id with another one.
getNomUrl($withpicto=0, $option='', $max=0, $short=0, $moretitle='', $notooltip=0, $save_lastsearch_value=-1)
Return clicable name (with picto eventually)
isMaxNbGenReached()
Return if maximum number of generation is reached.
setModelPdf($model, $notrigger=0)
Update the model for documents.
getNextNumRef($soc, $mode='next')
Return next reference of invoice not already used (or last reference)
__construct(DoliDB $db)
Constructor.
setNextDate($date, $increment_nb_gen_done=0, $notrigger=0)
Update the next date of execution.
setAutoValidate($validate, $notrigger=0)
Update the auto validate flag of invoice.
initAsSpecimen($option='')
Initialise an instance with random values.
setMaxPeriod($nb, $notrigger=0)
Update the maximum period.
update(User $user, $notrigger=0)
Update a line invoice_rec.
create($user, $facid, $notrigger=0, $onlylines=array())
Create a predefined invoice.
strikeIfMaxNbGenReached($ret)
Format string to output with by striking the string if max number of generation was reached.
fetch($rowid, $ref='', $ref_ext='', $noextrafields=0, $nolines=0)
Load object and lines.
setGeneratePdf($validate, $notrigger=0)
Update the auto generate documents.
static replaceProduct(DoliDB $dbs, $origin_id, $dest_id)
Function used to replace a product id with another one.
getNextDate()
Return the next date of.
createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0, $notrigger=0)
Create all recurrents invoices (for all entities if multicompany is used).
Class to manage products or services.
Class to manage Dolibarr users.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
Definition date.lib.php:125
print $script_file $mode $langs defaultlang(is_numeric($duration_value) ? " delay=". $duration_value :"").(is_numeric($duration_value2) ? " after cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_mktime($hour, $minute, $second, $month, $day, $year, $gm='auto', $check=1)
Return a timestamp date built from detailed information (by default a local PHP server timestamp) Rep...
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
price($amount, $form=0, $outlangs='', $trunc=1, $rounding=-1, $forcerounding=-1, $currency_code='')
Function to format a value into an amount for visual output Function used into PDF and HTML pages.
dol_now($mode='auto')
Return date for now.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
getLocalTaxesFromRate($vatrate, $local, $buyer, $seller, $firstparamisid=0)
Get type and rate of localtaxes for a particular vat rate/country of a thirdparty.
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false)
Output date in a string format according to outputlangs (or langs if not defined).
dol_print_error($db=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_trunc($string, $size=40, $trunc='right', $stringencoding='UTF-8', $nodot=0, $display=0)
Truncate a string to a particular length adding '…' if string larger than length.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dol_getdate($timestamp, $fast=false, $forcetimezone='')
Return an array with locale date info.
dol_escape_htmltag($stringtoescape, $keepb=0, $keepn=0, $noescapetags='', $escapeonlyhtmltags=0, $cleanalsojavascript=0)
Returns text escaped for inclusion in HTML alt or title or value tags, or into values of HTML input f...
getMarginInfos($pv_ht, $remise_percent, $tva_tx, $localtax1_tx, $localtax2_tx, $fk_pa, $pa_ht)
Return an array with margins information of a line.
calcul_price_total($qty, $pu, $remise_percent_ligne, $txtva, $uselocaltax1_rate, $uselocaltax2_rate, $remise_percent_global, $price_base_type, $info_bits, $type, $seller='', $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
Definition price.lib.php:88
if(preg_match('/crypted:/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
Definition repair.php:137