34require_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
35require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
36require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
37require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
38require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/factureligne.class.php';
39require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
40require_once DOL_DOCUMENT_ROOT.
'/subtotals/class/commonsubtotal.class.php';
54 public $TRIGGER_PREFIX =
'BILLREC';
59 public $element =
'facturerec';
64 public $table_element =
'facture_rec';
69 public $table_element_line =
'facturedet_rec';
74 public $fk_element =
'fk_facture';
79 public $picto =
'bill';
100 public $multicurrency_subprice;
131 public $date_last_gen;
158 public $unit_frequency;
163 public $rule_for_lines_dates;
173 public $special_code;
178 public $usenewprice = 0;
183 public $date_lim_reglement;
187 public $cond_reglement_code;
191 public $mode_reglement_code;
196 public $fk_societe_rib;
206 public $auto_validate;
211 public $fk_email_template;
216 public $generate_pdf;
221 public $usenewcurrencyrate;
225 const PAYMENTCODETOEDITSOCIETERIB =
"PRE";
256 public $fields = array(
257 'rowid' => array(
'type' =>
'integer',
'label' =>
'TechnicalID',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 10),
258 'titre' => array(
'type' =>
'varchar(100)',
'label' =>
'Titre',
'enabled' => 1,
'showoncombobox' => 1,
'visible' => -1,
'position' => 15),
259 'entity' => array(
'type' =>
'integer',
'label' =>
'Entity',
'default' =>
'1',
'enabled' => 1,
'visible' => -2,
'notnull' => 1,
'position' => 20,
'index' => 1),
260 'fk_soc' => array(
'type' =>
'integer:Societe:societe/class/societe.class.php',
'label' =>
'ThirdParty',
'enabled' =>
'isModEnabled("societe")',
'visible' => -1,
'notnull' => 1,
'position' => 25),
261 'subtype' => array(
'type' =>
'smallint(6)',
'label' =>
'InvoiceSubtype',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 30),
262 'datec' => array(
'type' =>
'datetime',
'label' =>
'DateCreation',
'enabled' => 1,
'visible' => -1,
'position' => 35),
263 'total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'Tva',
'enabled' => 1,
'visible' => -1,
'position' => 55,
'isameasure' => 1),
264 'localtax1' => array(
'type' =>
'double(24,8)',
'label' =>
'Localtax1',
'enabled' => 1,
'visible' => -1,
'position' => 60,
'isameasure' => 1),
265 'localtax2' => array(
'type' =>
'double(24,8)',
'label' =>
'Localtax2',
'enabled' => 1,
'visible' => -1,
'position' => 65,
'isameasure' => 1),
266 'total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'Total',
'enabled' => 1,
'visible' => -1,
'position' => 70,
'isameasure' => 1),
267 'total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'Total ttc',
'enabled' => 1,
'visible' => -1,
'position' => 75,
'isameasure' => 1),
268 'fk_user_author' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserAuthor',
'enabled' => 1,
'visible' => -1,
'position' => 80),
269 '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),
270 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'Fk cond reglement',
'enabled' => 1,
'visible' => -1,
'position' => 90),
271 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'Fk mode reglement',
'enabled' => 1,
'visible' => -1,
'position' => 95),
272 'date_lim_reglement' => array(
'type' =>
'date',
'label' =>
'Date lim reglement',
'enabled' => 1,
'visible' => -1,
'position' => 100),
273 'note_private' => array(
'type' =>
'html',
'label' =>
'NotePrivate',
'enabled' => 1,
'visible' => 0,
'position' => 105),
274 'note_public' => array(
'type' =>
'html',
'label' =>
'NotePublic',
'enabled' => 1,
'visible' => 0,
'position' => 110),
275 'modelpdf' => array(
'type' =>
'varchar(255)',
'label' =>
'Modelpdf',
'enabled' => 1,
'visible' => -1,
'position' => 115),
276 'date_when' => array(
'type' =>
'datetime',
'label' =>
'Date when',
'enabled' => 1,
'visible' => -1,
'position' => 130),
277 'date_last_gen' => array(
'type' =>
'datetime',
'label' =>
'Date last gen',
'enabled' => 1,
'visible' => -1,
'position' => 135),
278 'nb_gen_done' => array(
'type' =>
'integer',
'label' =>
'Nb gen done',
'enabled' => 1,
'visible' => -1,
'position' => 140),
279 'nb_gen_max' => array(
'type' =>
'integer',
'label' =>
'Nb gen max',
'enabled' => 1,
'visible' => -1,
'position' => 145),
280 'frequency' => array(
'type' =>
'integer',
'label' =>
'Frequency',
'enabled' => 1,
'visible' => -1,
'position' => 150),
281 'unit_frequency' => array(
'type' =>
'varchar(2)',
'label' =>
'UnitFrequency',
'enabled' => 1,
'visible' => -1,
'position' => 152),
282 'rule_for_lines_dates' => array(
'type' =>
'varchar(255)',
'label' =>
'RuleForLinesDates',
'enabled' =>
'getDolGlobalInt("FACTUREREC_SUPPORT_RULE_FOR_LINES")',
'visible' => 1,
'position' => 153,
'arrayofkeyval' => array(
'prepaid' =>
"Prepaid",
'postpaid' =>
"Postpaid"),
'default' =>
'prepaid'),
283 'usenewprice' => array(
'type' =>
'integer',
'label' =>
'UseNewPrice',
'enabled' => 1,
'visible' => 0,
'position' => 155),
284 'revenuestamp' => array(
'type' =>
'double(24,8)',
'label' =>
'RevenueStamp',
'enabled' => 1,
'visible' => -1,
'position' => 160,
'isameasure' => 1),
285 'auto_validate' => array(
'type' =>
'integer',
'label' =>
'Auto validate',
'enabled' => 1,
'visible' => -1,
'position' => 165),
286 'fk_email_template' => array(
'type' =>
'integer:CEmailTemplate:core/class/cemailtemplate.class.php',
'label' =>
"Modèle d'e-mail pour l'envoi automatique",
'enabled' => 1,
'visible' => -1,
'position' => 167),
287 'generate_pdf' => array(
'type' =>
'integer',
'label' =>
'Generate pdf',
'enabled' => 1,
'visible' => -1,
'position' => 170),
288 'fk_account' => array(
'type' =>
'integer',
'label' =>
'Fk account',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 175),
289 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'Fk multicurrency',
'enabled' => 1,
'visible' => -1,
'position' => 180),
290 'multicurrency_code' => array(
'type' =>
'varchar(255)',
'label' =>
'Multicurrency code',
'enabled' => 1,
'visible' => -1,
'position' => 185),
291 'multicurrency_tx' => array(
'type' =>
'double(24,8)',
'label' =>
'Multicurrency tx',
'enabled' => 1,
'visible' => -1,
'position' => 190,
'isameasure' => 1),
292 'multicurrency_total_ht' => array(
'type' =>
'double(24,8)',
'label' =>
'Multicurrency total ht',
'enabled' => 1,
'visible' => -1,
'position' => 195,
'isameasure' => 1),
293 'multicurrency_total_tva' => array(
'type' =>
'double(24,8)',
'label' =>
'Multicurrency total tva',
'enabled' => 1,
'visible' => -1,
'position' => 200,
'isameasure' => 1),
294 'multicurrency_total_ttc' => array(
'type' =>
'double(24,8)',
'label' =>
'Multicurrency total ttc',
'enabled' => 1,
'visible' => -1,
'position' => 205,
'isameasure' => 1),
295 'fk_user_modif' => array(
'type' =>
'integer:User:user/class/user.class.php',
'label' =>
'UserModif',
'enabled' => 1,
'visible' => -2,
'notnull' => -1,
'position' => 210),
296 'tms' => array(
'type' =>
'timestamp',
'label' =>
'DateModification',
'enabled' => 1,
'visible' => -1,
'notnull' => 1,
'position' => 215),
297 'suspended' => array(
'type' =>
'integer',
'label' =>
'Suspended',
'enabled' => 1,
'visible' => -1,
'position' => 225),
298 'fk_societe_rib' => array(
'type' =>
'integer',
'label' =>
'Fk Societe RIB',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 175),
302 const STATUS_NOTSUSPENDED = 0;
303 const STATUS_SUSPENDED = 1;
326 public function create($user, $facid, $notrigger = 0, $onlylines = array())
334 $this->titre = trim(isset($this->titre) ? $this->titre : $this->title);
335 $this->title = trim($this->title);
336 $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
337 if (empty($this->suspended)) {
338 $this->suspended = 0;
340 if (empty($this->rule_for_lines_dates)) {
341 $this->rule_for_lines_dates =
'prepaid';
345 if (empty($this->frequency)) {
346 $this->frequency = 0;
347 $this->date_when =
null;
350 $this->frequency = abs($this->frequency);
351 $this->nb_gen_done = 0;
352 $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
353 $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
354 $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
359 $facsrc =
new Facture($this->db);
360 $result = $facsrc->fetch($facid);
362 $this->socid = $facsrc->socid;
364 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture_rec (";
372 $sql .=
", note_private";
373 $sql .=
", note_public";
374 $sql .=
", modelpdf";
375 $sql .=
", fk_user_author";
376 $sql .=
", fk_projet";
377 $sql .=
", fk_account";
378 $sql .=
", fk_cond_reglement";
379 $sql .=
", fk_mode_reglement";
380 $sql .=
", usenewprice";
381 $sql .=
", frequency";
382 $sql .=
", unit_frequency";
383 $sql .=
", date_when";
384 $sql .=
", date_last_gen";
385 $sql .=
", nb_gen_done";
386 $sql .=
", nb_gen_max";
387 $sql .=
", auto_validate";
388 $sql .=
", fk_email_template";
389 $sql .=
", generate_pdf";
390 $sql .=
", fk_multicurrency";
391 $sql .=
", multicurrency_code";
392 $sql .=
", multicurrency_tx";
393 $sql .=
", suspended";
394 $sql .=
", fk_societe_rib";
395 $sql .=
", rule_for_lines_dates";
396 $sql .=
") VALUES (";
397 $sql .=
"'".$this->db->escape($this->titre ? $this->titre : $this->title).
"'";
398 $sql .=
", ".((int) $this->socid);
399 $sql .=
", ".(isset($this->subtype) ? (int) $this->subtype :
"null");
400 $sql .=
", ".((int)
$conf->entity);
401 $sql .=
", '".$this->db->idate($now).
"'";
402 $sql .=
", ".(!empty($facsrc->total_ttc) ? ((float) $facsrc->total_ttc) :
'0');
403 $sql .=
", ".(!empty($this->note_private) ? (
"'".$this->db->escape($this->note_private).
"'") :
"NULL");
404 $sql .=
", ".(!empty($this->note_public) ? (
"'".$this->db->escape($this->note_public).
"'") :
"NULL");
405 $sql .=
", ".(!empty($this->model_pdf) ? (
"'".$this->db->escape($this->model_pdf).
"'") :
"NULL");
406 $sql .=
", ".((int) $user->id);
407 $sql .=
", ".(!empty($this->fk_project) ? ((int) $this->fk_project) :
"null");
408 $sql .=
", ".(!empty($facsrc->fk_account) ? ((int) $facsrc->fk_account) :
"null");
409 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
410 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
411 $sql .=
", ".((int) $this->usenewprice);
412 $sql .=
", ".((int) $this->frequency);
413 $sql .=
", '".$this->db->escape($this->unit_frequency).
"'";
414 $sql .=
", ".(!empty($this->date_when) ?
"'".$this->db->idate($this->date_when).
"'" :
'NULL');
415 $sql .=
", ".(!empty($this->date_last_gen) ?
"'".$this->db->idate($this->date_last_gen).
"'" :
'NULL');
416 $sql .=
", ".((int) $this->nb_gen_done);
417 $sql .=
", ".((int) $this->nb_gen_max);
418 $sql .=
", ".((int) $this->auto_validate);
419 $sql .=
", ".((int) $this->fk_email_template);
420 $sql .=
", ".((int) $this->generate_pdf);
421 $sql .=
", ".((int) $facsrc->fk_multicurrency);
422 $sql .=
", '".$this->db->escape($facsrc->multicurrency_code).
"'";
423 $sql .=
", ".((float) $facsrc->multicurrency_tx);
424 $sql .=
", ".((int) $this->suspended);
425 $sql .=
", ".(!empty($this->fk_societe_rib) ? ((int) $this->fk_societe_rib) :
'NULL');
426 $sql .=
", '".$this->db->escape($this->rule_for_lines_dates).
"'";
429 if ($this->db->query($sql)) {
430 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"facture_rec");
433 $this->fk_multicurrency = $facsrc->fk_multicurrency;
434 $this->multicurrency_code = $facsrc->multicurrency_code;
435 $this->multicurrency_tx = $facsrc->multicurrency_tx;
440 $num = count($facsrc->lines);
441 for ($i = 0; $i < $num; $i++) {
442 $facline = $facsrc->lines[$i];
443 '@phan-var-force FactureLigneRec $facline';
444 if (!empty($onlylines) && !in_array($facline->id, $onlylines)) {
449 if (($facline->product_type != 9 && empty($facline->fk_parent_line)) || $facline->product_type == 9) {
453 $tva_tx = $facline->tva_tx;
454 if (!empty($facline->vat_src_code) && !preg_match(
'/\(/', (
string) $tva_tx)) {
455 $tva_tx .=
' ('.$facline->vat_src_code.
')';
458 $default_start_fill =
getDolGlobalInt(
'INVOICEREC_SET_AUTOFILL_DATE_START');
459 $default_end_fill =
getDolGlobalInt(
'INVOICEREC_SET_AUTOFILL_DATE_END');
461 $result_insert = $this->
addline(
466 $facline->localtax1_tx,
467 $facline->localtax2_tx,
468 $facline->fk_product,
469 $facline->remise_percent,
474 $facline->product_type,
476 $facline->special_code,
479 $facline->multicurrency_subprice,
488 if ($result_insert > 0 && $facline->product_type == 9) {
489 $fk_parent_line = $result_insert;
492 if ($result_insert < 0) {
497 $result2 = $objectline->fetch($result_insert);
500 if (method_exists($facline,
'fetch_optionals')) {
501 $facline->fetch_optionals($facline->id);
502 $objectline->array_options = $facline->array_options;
505 $result = $objectline->insertExtraFields();
510 $objectline->extraparams = $facline->extraparams;
511 $result = $objectline->setExtraParameters();
515 } elseif ($result2 < 0) {
516 $this->errors[] = $objectline->error;
522 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
523 $this->linked_objects = $this->linkedObjectsIds;
527 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
528 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
529 if (is_array($tmp_origin_id)) {
530 foreach ($tmp_origin_id as $origin_id) {
533 $this->error = $this->db->lasterror();
538 $origin_id = $tmp_origin_id;
541 $this->error = $this->db->lasterror();
555 if (!$error && !$notrigger) {
557 $result = $this->call_trigger(
'BILLREC_CREATE', $user);
559 $this->db->rollback();
566 $this->db->rollback();
573 $this->error = $this->db->lasterror();
574 $this->db->rollback();
578 $this->db->rollback();
595 $sql =
"UPDATE ".$this->db->prefix().
"facture_rec SET";
596 $sql .=
" entity = ".((int) $this->entity).
",";
597 $sql .=
" titre = '".$this->db->escape($this->title).
"',";
598 $sql .=
" suspended = ".((int) $this->suspended).
",";
599 $sql .=
" fk_soc = ".((int) $this->socid).
",";
600 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
601 $sql .=
" localtax1 = ".((float) $this->total_localtax1).
",";
602 $sql .=
" localtax2 = ".((float) $this->total_localtax2).
",";
603 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
604 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
605 $sql .=
" fk_societe_rib = ".(!empty($this->fk_societe_rib) ? ((int) $this->fk_societe_rib) :
'NULL').
",";
606 $sql .=
" auto_validate = ".((int) $this->auto_validate).
",";
607 $sql .=
" fk_email_template = ".((int) $this->fk_email_template);
610 $sql .=
" WHERE rowid = ".((int) $this->
id);
614 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
616 $resql = $this->db->query($sql);
625 if (!$error && !$notrigger) {
627 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
629 $this->db->rollback();
637 $this->error = $this->db->lasterror();
638 $this->db->rollback();
653 public function fetch($rowid, $ref =
'', $ref_ext =
'', $noextrafields = 0, $nolines = 0)
657 $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';
658 $sql .=
', f.date_lim_reglement as dlr';
659 $sql .=
', f.note_private, f.note_public, f.fk_user_author';
660 $sql .=
', f.modelpdf as model_pdf';
661 $sql .=
', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project';
662 $sql .=
', f.fk_account, f.fk_societe_rib';
663 $sql .=
', f.frequency, f.unit_frequency, f.rule_for_lines_dates, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.usenewprice, f.auto_validate';
664 $sql .=
', f.fk_email_template';
665 $sql .=
', f.generate_pdf';
666 $sql .=
", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
667 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
668 $sql .=
', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
670 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_rec as f';
671 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
672 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON f.fk_mode_reglement = p.id';
674 $sql .=
' WHERE f.entity IN ('.getEntity(
'invoice').
')';
676 $sql .=
' AND f.rowid = '.((int) $rowid);
678 $sql .=
" AND f.titre = '".$this->db->escape($ref).
"'";
680 $sql .=
' AND f.rowid = 0';
686 $result = $this->db->query($sql);
688 if ($this->db->num_rows($result)) {
689 $obj = $this->db->fetch_object($result);
691 $this->
id = $obj->rowid;
692 $this->entity = $obj->entity;
693 $this->titre = $obj->title;
694 $this->title = $obj->title;
695 $this->
ref = $obj->title;
696 $this->subtype = $obj->subtype;
697 $this->suspended = $obj->suspended;
698 $this->total_ht = $obj->total_ht;
699 $this->total_tva = $obj->total_tva;
700 $this->total_localtax1 = $obj->localtax1;
701 $this->total_localtax2 = $obj->localtax2;
702 $this->total_ttc = $obj->total_ttc;
703 $this->socid = $obj->fk_soc;
704 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
705 $this->mode_reglement_id = $obj->fk_mode_reglement;
706 $this->mode_reglement_code = $obj->mode_reglement_code;
707 $this->mode_reglement = $obj->mode_reglement_libelle;
708 $this->cond_reglement_id = $obj->fk_cond_reglement;
709 $this->cond_reglement_code = $obj->cond_reglement_code;
710 $this->cond_reglement = $obj->cond_reglement_libelle;
711 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
712 $this->fk_project = $obj->fk_project;
713 $this->fk_account = $obj->fk_account;
714 $this->fk_societe_rib = $obj->fk_societe_rib;
715 $this->note_private = $obj->note_private;
716 $this->note_public = $obj->note_public;
717 $this->user_creation_id = $obj->fk_user_author;
718 $this->model_pdf = $obj->model_pdf;
720 $this->frequency = $obj->frequency;
721 $this->unit_frequency = $obj->unit_frequency;
722 $this->rule_for_lines_dates = $obj->rule_for_lines_dates;
723 $this->date_when = $this->db->jdate($obj->date_when);
724 $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
725 $this->nb_gen_done = $obj->nb_gen_done;
726 $this->nb_gen_max = $obj->nb_gen_max;
727 $this->usenewprice = $obj->usenewprice;
728 $this->auto_validate = $obj->auto_validate;
729 $this->fk_email_template = $obj->fk_email_template;
730 $this->generate_pdf = $obj->generate_pdf;
733 $this->fk_multicurrency = $obj->fk_multicurrency;
734 $this->multicurrency_code = $obj->multicurrency_code;
735 $this->multicurrency_tx = $obj->multicurrency_tx;
736 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
737 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
738 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
742 if (empty($noextrafields)) {
745 $this->error = $this->db->lasterror();
751 if (empty($nolines)) {
754 $this->error = $this->db->lasterror();
761 $this->error =
'Bill with id '.$rowid.
' or ref '.$ref.
' not found';
762 dol_syslog(
'Facture::Fetch Error '.$this->error, LOG_ERR);
766 $this->error = $this->db->error();
793 $this->lines = array();
795 dol_syslog(
'FactureRec::fetch_lines', LOG_DEBUG);
797 $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, ';
798 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
799 $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,';
800 $sql .=
' l.rang, l.special_code,';
801 $sql .=
' l.fk_unit, l.fk_contract_line, l.extraparams,';
802 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
803 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
804 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet_rec as l';
805 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
806 $sql .=
' WHERE l.fk_facture = '.((int) $this->
id);
807 $sql .=
' ORDER BY l.rang';
809 $result = $this->db->query($sql);
811 $num = $this->db->num_rows($result);
814 $objp = $this->db->fetch_object($result);
817 $line->id = $objp->rowid;
818 $line->rowid = $objp->rowid;
819 $line->fk_facture = $objp->fk_facture;
820 $line->fk_parent_line = $objp->fk_parent_line;
821 $line->desc = $objp->description;
822 $line->description = $objp->description;
823 $line->ref = $objp->product_ref;
824 $line->product_ref = $objp->product_ref;
825 $line->libelle = $objp->product_label;
826 $line->product_label = $objp->product_label;
827 $line->product_desc = $objp->product_desc;
828 $line->product_type = $objp->product_type;
829 $line->fk_product_type = $objp->fk_product_type;
830 $line->qty = $objp->qty;
831 $line->subprice = $objp->subprice;
833 $line->label = $objp->custom_label;
835 $line->vat_src_code = $objp->vat_src_code;
836 $line->tva_tx = $objp->tva_tx;
837 $line->localtax1_tx = $objp->localtax1_tx;
838 $line->localtax2_tx = $objp->localtax2_tx;
839 $line->localtax1_type = $objp->localtax1_type;
840 $line->localtax2_type = $objp->localtax2_type;
841 $line->remise_percent = $objp->remise_percent;
843 $line->fk_product = $objp->fk_product;
844 $line->date_start_fill = $objp->date_start_fill;
845 $line->date_end_fill = $objp->date_end_fill;
846 $line->info_bits = $objp->info_bits;
847 $line->total_ht = $objp->total_ht;
848 $line->total_tva = $objp->total_tva;
849 $line->total_ttc = $objp->total_ttc;
851 $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
852 $line->fk_fournprice = $objp->fk_product_fournisseur_price;
854 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
856 $line->buyprice = $marginInfos[0];
857 $line->pa_ht = $marginInfos[0];
858 $line->marge_tx = (
string) $marginInfos[1];
859 $line->marque_tx = (
string) $marginInfos[2];
860 $line->rang = $objp->rang;
861 $line->special_code = $objp->special_code;
862 $line->fk_unit = $objp->fk_unit;
863 $line->fk_contract_line = $objp->fk_contract_line;
865 $line->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
868 $line->price = $objp->price;
869 $line->remise = $objp->remise;
871 $line->fetch_optionals();
874 $line->fk_multicurrency = $objp->fk_multicurrency;
875 $line->multicurrency_code = $objp->multicurrency_code;
876 $line->multicurrency_subprice = $objp->multicurrency_subprice;
877 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
878 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
879 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
881 $this->lines[$i] = $line;
886 $this->db->free($result);
889 $this->error = $this->db->lasterror();
903 public function delete(
User $user, $notrigger = 0, $idwarehouse = -1)
907 dol_syslog(get_class($this).
"::delete rowid=".((
int) $rowid), LOG_DEBUG);
912 $main = MAIN_DB_PREFIX.
'facturedet_rec';
913 $ef = $main.
"_extrafields";
915 $sqlef =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_facture = ".((int) $rowid).
")";
916 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facturedet_rec WHERE fk_facture = ".((int) $rowid);
918 if ($this->db->query($sqlef) && $this->db->query($sql)) {
919 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facture_rec WHERE rowid = ".((int) $rowid);
921 if ($this->db->query($sql)) {
933 $this->error = $this->db->lasterror();
937 $this->error = $this->db->lasterror();
940 if (!$error && !$notrigger) {
942 $result = $this->call_trigger(
'BILLREC_DELETE', $user);
952 $this->db->rollback();
986 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)
992 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);
993 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1005 if (preg_match(
'/\((.*)\)/', (
string) $txtva, $reg)) {
1006 $vat_src_code = $reg[1];
1007 $txtva = preg_replace(
'/\s*\(.*\)/',
'', (
string) $txtva);
1012 $remise_percent =
price2num($remise_percent);
1013 if (empty($remise_percent)) {
1014 $remise_percent = 0;
1019 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1022 $txlocaltax1 = (float)
price2num($txlocaltax1);
1023 $txlocaltax2 = (float)
price2num($txlocaltax2);
1024 if (empty($txtva)) {
1027 if (empty($txlocaltax1)) {
1030 if (empty($txlocaltax2)) {
1033 if (empty($info_bits)) {
1037 if ($price_base_type ==
'HT') {
1047 $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);
1048 $total_ht = $tabprice[0];
1049 $total_tva = $tabprice[1];
1050 $total_ttc = $tabprice[2];
1051 $total_localtax1 = $tabprice[9];
1052 $total_localtax2 = $tabprice[10];
1053 $pu_ht = $tabprice[3];
1056 $multicurrency_total_ht = $tabprice[16];
1057 $multicurrency_total_tva = $tabprice[17];
1058 $multicurrency_total_ttc = $tabprice[18];
1059 $pu_ht_devise = $tabprice[19];
1061 $product_type = $type;
1063 $product =
new Product($this->db);
1064 $result = $product->fetch($fk_product);
1065 $product_type = $product->type;
1068 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1069 $fk_parent_line = 0;
1074 if ($ranktouse == -1) {
1076 $ranktouse = $rangmax + 1;
1079 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facturedet_rec (";
1080 $sql .=
"fk_facture";
1081 $sql .=
", fk_parent_line";
1083 $sql .=
", description";
1087 $sql .=
", vat_src_code";
1088 $sql .=
", localtax1_tx";
1089 $sql .=
", localtax1_type";
1090 $sql .=
", localtax2_tx";
1091 $sql .=
", localtax2_type";
1092 $sql .=
", fk_product";
1093 $sql .=
", product_type";
1094 $sql .=
", remise_percent";
1095 $sql .=
", subprice";
1097 $sql .=
", total_ht";
1098 $sql .=
", total_tva";
1099 $sql .=
", total_localtax1";
1100 $sql .=
", total_localtax2";
1101 $sql .=
", total_ttc";
1102 $sql .=
", date_start_fill";
1103 $sql .=
", date_end_fill";
1104 $sql .=
", fk_product_fournisseur_price";
1105 $sql .=
", buy_price_ht";
1106 $sql .=
", info_bits";
1108 $sql .=
", special_code";
1109 $sql .=
", fk_unit";
1110 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
1111 $sql .=
") VALUES (";
1112 $sql .=
" ".((int) $facid);
1113 $sql .=
", ".($fk_parent_line > 0 ? ((int) $fk_parent_line) :
"null");
1114 $sql .=
", ".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
1115 $sql .=
", '".$this->db->escape($desc).
"'";
1116 $sql .=
", ".price2num($pu_ht);
1117 $sql .=
", ".price2num($qty);
1118 $sql .=
", ".price2num($txtva);
1119 $sql .=
", '".$this->db->escape($vat_src_code).
"'";
1120 $sql .=
", ".price2num($txlocaltax1);
1121 $sql .=
", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] :
'').
"'";
1122 $sql .=
", ".price2num($txlocaltax2);
1123 $sql .=
", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] :
'').
"'";
1124 $sql .=
", ".(!empty($fk_product) ?
"'".$this->db->escape((
string) $fk_product).
"'" :
"null");
1125 $sql .=
", ".((int) $product_type);
1126 $sql .=
", ".price2num($remise_percent);
1127 $sql .=
", ".price2num($pu_ht);
1129 $sql .=
", ".price2num($total_ht);
1130 $sql .=
", ".price2num($total_tva);
1131 $sql .=
", ".price2num($total_localtax1);
1132 $sql .=
", ".price2num($total_localtax2);
1133 $sql .=
", ".price2num($total_ttc);
1134 $sql .=
", ".(int) $date_start_fill;
1135 $sql .=
", ".(int) $date_end_fill;
1136 $sql .=
", ".($fk_fournprice > 0 ? $fk_fournprice :
'null');
1137 $sql .=
", ".($pa_ht ?
price2num($pa_ht) : 0);
1138 $sql .=
", ".((int) $info_bits);
1139 $sql .=
", ".((int) $ranktouse);
1140 $sql .=
", ".((int) $special_code);
1141 $sql .=
", ".($fk_unit ? ((int) $fk_unit) :
"null");
1142 $sql .=
", ".(int) $this->fk_multicurrency;
1143 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1144 $sql .=
", ".price2num($pu_ht_devise,
'CU');
1145 $sql .=
", ".price2num($multicurrency_total_ht,
'CT');
1146 $sql .=
", ".price2num($multicurrency_total_tva,
'CT');
1147 $sql .=
", ".price2num($multicurrency_total_ttc,
'CT');
1150 dol_syslog(get_class($this).
"::addline", LOG_DEBUG);
1151 if ($this->db->query($sql)) {
1152 $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX.
"facturedet_rec");
1157 $this->error = $this->db->lasterror();
1192 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)
1198 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);
1199 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1202 if (empty($remise_percent)) {
1203 $remise_percent = 0;
1212 $remise_percent = (float)
price2num($remise_percent);
1214 if (empty($info_bits)) {
1219 $pu_ht_devise = (float)
price2num($pu_ht_devise);
1220 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
1223 $txlocaltax1 = (float)
price2num($txlocaltax1);
1224 $txlocaltax2 = (float)
price2num($txlocaltax2);
1225 if (empty($txlocaltax1)) {
1228 if (empty($txlocaltax2)) {
1232 if (empty($this->multicurrency_subprice)) {
1233 $this->multicurrency_subprice = 0;
1235 if (empty($this->multicurrency_total_ht)) {
1236 $this->multicurrency_total_ht = 0;
1238 if (empty($this->multicurrency_total_tva)) {
1239 $this->multicurrency_total_tva = 0;
1241 if (empty($this->multicurrency_total_ttc)) {
1242 $this->multicurrency_total_ttc = 0;
1245 if ($price_base_type ==
'HT') {
1260 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1261 $vat_src_code = $reg[1];
1262 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1265 $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);
1267 $total_ht = $tabprice[0];
1268 $total_tva = $tabprice[1];
1269 $total_ttc = $tabprice[2];
1270 $total_localtax1 = $tabprice[9];
1271 $total_localtax2 = $tabprice[10];
1272 $pu_ht = $tabprice[3];
1273 $pu_tva = $tabprice[4];
1274 $pu_ttc = $tabprice[5];
1277 $multicurrency_total_ht = $tabprice[16];
1278 $multicurrency_total_tva = $tabprice[17];
1279 $multicurrency_total_ttc = $tabprice[18];
1280 $pu_ht_devise = $tabprice[19];
1282 $product_type = $type;
1284 $product =
new Product($this->db);
1285 $result = $product->fetch($fk_product);
1286 $product_type = $product->type;
1289 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1290 $fk_parent_line = 0;
1293 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet_rec SET ";
1294 $sql .=
"fk_facture = ".((int) $facid);
1295 $sql .=
", fk_parent_line = ".($fk_parent_line > 0 ? ((int) $fk_parent_line) :
"null");
1296 $sql .=
", label=".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
1297 $sql .=
", description='".$this->db->escape($desc).
"'";
1298 $sql .=
", price=".price2num($pu_ht);
1299 $sql .=
", qty=".price2num($qty);
1300 $sql .=
", tva_tx=".price2num($txtva);
1301 $sql .=
", vat_src_code='".$this->db->escape($vat_src_code).
"'";
1302 $sql .=
", localtax1_tx=".((float) $txlocaltax1);
1303 $sql .=
", localtax1_type='".$this->db->escape($localtaxes_type[0]).
"'";
1304 $sql .=
", localtax2_tx=".((float) $txlocaltax2);
1305 $sql .=
", localtax2_type='".$this->db->escape($localtaxes_type[2]).
"'";
1306 $sql .=
", fk_product=".(!empty($fk_product) ?
"'".$this->db->escape((
string) $fk_product).
"'" :
"null");
1307 $sql .=
", product_type=".((int) $product_type);
1308 $sql .=
", remise_percent='".price2num($remise_percent).
"'";
1309 $sql .=
", subprice='".price2num($pu_ht).
"'";
1310 $sql .=
", total_ht='".price2num($total_ht).
"'";
1311 $sql .=
", total_tva='".price2num($total_tva).
"'";
1312 $sql .=
", total_localtax1='".price2num($total_localtax1).
"'";
1313 $sql .=
", total_localtax2='".price2num($total_localtax2).
"'";
1314 $sql .=
", total_ttc='".price2num($total_ttc).
"'";
1315 $sql .=
", date_start_fill=".((int) $date_start_fill);
1316 $sql .=
", date_end_fill=".((int) $date_end_fill);
1317 $sql .=
", fk_product_fournisseur_price=".($fk_fournprice > 0 ? (int) $fk_fournprice :
'null');
1318 $sql .=
", buy_price_ht=".($pa_ht ?
price2num($pa_ht) : 0);
1319 $sql .=
", info_bits=".((int) $info_bits);
1320 $sql .=
", rang=".((int) $rang);
1321 $sql .=
", special_code=".((int) $special_code);
1322 $sql .=
", fk_unit=".($fk_unit ?
"'".$this->db->escape((
string) $fk_unit).
"'" :
"null");
1323 $sql .=
', multicurrency_subprice = '.price2num($pu_ht_devise);
1324 $sql .=
', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1325 $sql .=
', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1326 $sql .=
', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1327 $sql .=
" WHERE rowid = ".((int) $rowid);
1329 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1330 if ($this->db->query($sql)) {
1335 $this->error = $this->db->lasterror();
1351 if (empty($this->date_when)) {
1357 $originalDay = (int) $dateInfo[
'mday'];
1358 $originalMonth = (int) $dateInfo[
'mon'];
1359 $originalYear = (int) $dateInfo[
'year'];
1360 $originalHour = (int) $dateInfo[
'hours'];
1361 $originalMin = (int) $dateInfo[
'minutes'];
1362 $originalSec = (int) $dateInfo[
'seconds'];
1365 if ($originalDay >= 28 && $this->unit_frequency ==
'm') {
1367 $lastDayOfOriginalMonth = (int)
date(
't', $this->date_when);
1370 $targetMonth = $originalMonth + (int) $this->frequency;
1371 $targetYear = $originalYear;
1374 while ($targetMonth > 12) {
1378 while ($targetMonth < 1) {
1384 $lastDayOfTargetMonth = (int)
date(
't',
dol_mktime(0, 0, 0, $targetMonth, 1, $targetYear));
1388 if ($originalDay >= $lastDayOfOriginalMonth || $originalDay >= 29) {
1390 $targetDay = $lastDayOfTargetMonth;
1394 $targetDay = min($originalDay, $lastDayOfTargetMonth);
1398 return dol_mktime($originalHour, $originalMin, $originalSec, $targetMonth, $targetDay, $targetYear);
1413 if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1444 global
$conf, $langs, $user, $hookmanager, $action;
1450 $langs->loadLangs(array(
"main",
"bills"));
1454 $today =
dol_mktime(23, 59, 59, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
1458 dol_syslog(
"createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid.
" forcevalidation=".$forcevalidation.
" forcebuilddoc=".$forcebuilddoc, LOG_DEBUG);
1460 $sql =
'SELECT rowid FROM '.MAIN_DB_PREFIX.
'facture_rec';
1461 $sql .=
' WHERE frequency > 0';
1462 $sql .=
" AND (date_when IS NULL OR date_when <= '".$this->db->idate($today).
"')";
1463 $sql .=
' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1464 $sql .=
' AND suspended = 0';
1465 $sql .=
' AND entity = '.((int)
$conf->entity);
1466 if ($restrictioninvoiceid > 0) {
1467 $sql .=
' AND rowid = '.((int) $restrictioninvoiceid);
1469 $sql .= $this->db->order(
'entity',
'ASC');
1471 $parameters = array(
1472 'restrictioninvoiceid' => $restrictioninvoiceid,
1473 'forcevalidation' => $forcevalidation,
1475 $reshook = $hookmanager->executeHooks(
'beforeCreationOfRecurringInvoices', $parameters, $sql);
1477 $resql = $this->db->query($sql);
1480 $num = $this->db->num_rows($resql);
1483 $this->output .= $langs->trans(
"FoundXQualifiedRecurringInvoiceTemplate", $num).
"\n";
1485 $this->output .= $langs->trans(
"NoQualifiedRecurringInvoiceTemplateFound");
1488 $saventity =
$conf->entity;
1491 $line = $this->db->fetch_object($resql);
1495 $errorforinvoice = 0;
1496 $invoiceidgenerated = 0;
1497 $mailHasSent =
false;
1503 $facturerec->fetch($line->rowid);
1505 if ($facturerec->id > 0) {
1507 $conf->entity = $facturerec->entity;
1509 dol_syslog(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref.
", entity=".$facturerec->entity);
1511 $facture =
new Facture($this->db);
1512 $facture->fac_rec = $facturerec->id;
1513 $facture->fk_fac_rec_source = $facturerec->id;
1516 $facture->subtype = $facturerec->subtype;
1519 $facture->date = (empty($facturerec->date_when) ? $now : $facturerec->date_when);
1520 $facture->socid = $facturerec->socid;
1522 if (!empty($facturerec->fk_multicurrency)) {
1523 $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1524 $facture->multicurrency_code = $facturerec->multicurrency_code;
1525 $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1528 if (isset($facture->array_options) && isset($facturerec->array_options)) {
1529 foreach ($facturerec->array_options as $key => $value) {
1530 if (isset($facture->array_options[$key])) {
1531 $facture->array_options[$key] = $value;
1536 $parameters[
'facture'] = &$facture;
1537 $reshook = $hookmanager->executeHooks(
'beforeCreationOfEachRecurringInvoice', $parameters, $facturerec, $action);
1540 $invoiceidgenerated = $facture->create($user, 0, 0, (
isModEnabled(
'multicurrency') ? $facturerec->usenewcurrencyrate : 0));
1541 if ($invoiceidgenerated <= 0) {
1548 if (!$errorforinvoice && ($facturerec->auto_validate || $forcevalidation)) {
1549 $result = $facture->validate($user);
1556 if (!$errorforinvoice && ($facturerec->generate_pdf || $forcebuilddoc || $facturerec->auto_validate == 2)) {
1558 $facture->fetch($facture->id);
1559 $facture->fetch_thirdparty();
1561 $outputlangs = $langs;
1563 if (!empty($facture->thirdparty->default_lang)) {
1565 $outputlangs->setDefaultLang($facture->thirdparty->default_lang);
1566 $outputlangs->loadLangs(array(
'main',
'bills'));
1571 if ($facturerec->generate_pdf || $forcebuilddoc) {
1572 $result = $facture->generateDocument($facturerec->model_pdf, $outputlangs);
1582 require_once DOL_DOCUMENT_ROOT .
'/core/class/html.formmail.class.php';
1583 require_once DOL_DOCUMENT_ROOT .
'/core/class/CMailFile.class.php';
1584 $formmail =
new FormMail($this->db);
1587 $template = $facturerec->fk_email_template;
1588 $arraymessage = $formmail->getEMailTemplate($this->db,
'facture_send', $user, $outputlangs, (is_numeric($template) ? $template : 0), 1, (is_numeric($template) ?
'' : $template), (($template != 0) ? -1 : 1));
1589 if (is_numeric($arraymessage) && $arraymessage <= 0) {
1590 $langs->load(
"errors");
1591 $this->output .= $langs->trans(
'ErrorFailedToFindEmailTemplate', $template ??
'');
1599 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null, $facture);
1604 $sendTopic =
make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv(
'InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
1607 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
1613 $res = $facture->fetch_thirdparty();
1614 $recipient = $facture->thirdparty;
1616 $tmparraycontact = $facture->liste_contact(-1,
'external', 0,
'BILLING');
1617 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
1618 foreach ($tmparraycontact as $data_email) {
1619 if (!empty($data_email[
'email'])) {
1620 $to[] = $facture->thirdparty->contact_get_property($data_email[
'id'],
'email');
1624 if (empty($to) && !empty($recipient->email)) {
1625 $to[] = $recipient->email;
1628 $errormesg =
"Failed to send invoice to thirdparty id=".$facture->socid.
". No email defined for invoice or customer.";
1632 $errormesg =
"Failed to load recipient with thirdparty id=".$facture->socid;
1638 if (!empty($arraymessage->email_from)) {
1639 $from = (
string) $arraymessage->email_from;
1642 $errormesg =
"Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
1646 if (!$loopError && !empty($to)) {
1647 $to = implode(
',', $to);
1648 if (!empty($arraymessage->email_to)) {
1649 $to = $to .
',' . $arraymessage->email_to;
1655 $trackid =
'inv'.$facture->id;
1656 $sendcontext =
'standard';
1660 if (!empty($arraymessage->email_tocc)) {
1661 $email_tocc = (
string) $arraymessage->email_tocc;
1666 if (!empty($arraymessage->email_tobcc)) {
1667 $email_tobcc = (
string) $arraymessage->email_tobcc;
1674 if ($arraymessage->joinfiles == 1 && !empty($facture->last_main_doc)) {
1675 $joinFile[] = DOL_DATA_ROOT.
'/'.$facture->last_main_doc;
1676 $joinFileName[] = basename($facture->last_main_doc);
1677 $joinFileMime[] = dol_mimetype(DOL_DATA_ROOT.
'/'.$facture->last_main_doc);
1681 $cMailFile =
new CMailFile($sendTopic, $to, $from, $sendContent, $joinFile, $joinFileMime, $joinFileName, $email_tocc, $email_tobcc, 0, 1, $errors_to,
'', $trackid,
'', $sendcontext,
'');
1683 $resultsendmail = $cMailFile->sendfile();
1688 if ($resultsendmail) {
1689 $mailHasSent =
true;
1692 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
1697 $actioncomm->type_code =
'AC_OTH_AUTO';
1698 $actioncomm->socid = $facture->thirdparty->id;
1699 $actioncomm->contact_id = 0;
1701 $actioncomm->code =
'AC_EMAIL';
1702 $actioncomm->label = $langs->trans(
'MailSentByTo', $from, $to);
1703 $actioncomm->note_private = $sendContent;
1704 $actioncomm->fk_project = $facture->fk_project;
1705 $actioncomm->datep =
dol_now();
1706 $actioncomm->datef = $actioncomm->datep;
1707 $actioncomm->percentage = -1;
1708 $actioncomm->authorid = $user->id;
1709 $actioncomm->userownerid = $user->id;
1711 $actioncomm->email_msgid = $cMailFile->msgid;
1712 $actioncomm->email_subject = $sendTopic;
1713 $actioncomm->email_from = $from;
1714 $actioncomm->email_sender =
'';
1715 $actioncomm->email_to = $to;
1719 $actioncomm->errors_to = $errors_to;
1721 $actioncomm->elementtype =
'invoice';
1722 $actioncomm->elementid = $facture->id;
1726 $actioncomm->create($user);
1728 $errormesg = $cMailFile->error.
' : '.$to;
1732 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
1737 $actioncomm->type_code =
'AC_OTH_AUTO';
1738 $actioncomm->socid = $facture->thirdparty->id;
1739 $actioncomm->contact_id = 0;
1741 $actioncomm->code =
'AC_EMAIL';
1742 $actioncomm->label = $langs->trans(
'sendAutoEmailInvoiceKO', $from, $to);
1743 $actioncomm->note_private = $errormesg;
1744 $actioncomm->fk_project = $facture->fk_project;
1745 $actioncomm->datep =
dol_now();
1746 $actioncomm->datef = $actioncomm->datep;
1747 $actioncomm->percentage = -1;
1748 $actioncomm->authorid = $user->id;
1749 $actioncomm->userownerid = $user->id;
1751 $actioncomm->email_msgid = $cMailFile->msgid;
1752 $actioncomm->email_subject = $sendTopic;
1753 $actioncomm->email_from = $from;
1754 $actioncomm->email_sender =
'';
1755 $actioncomm->email_to = $to;
1759 $actioncomm->errors_to = $errors_to;
1761 $actioncomm->elementtype =
'invoice';
1762 $actioncomm->elementid = $facture->id;
1766 $actioncomm->create($user);
1769 $this->db->commit();
1775 $this->error =
"Failed to load invoice template with id=".$line->rowid.
", entity=".
$conf->entity.
"\n";
1776 $this->errors[] =
"Failed to load invoice template with id=".$line->rowid.
", entity=".
$conf->entity;
1777 dol_syslog(
"createRecurringInvoices Failed to load invoice template with id=".$line->rowid.
", entity=".
$conf->entity);
1781 if (!$error && $invoiceidgenerated >= 0) {
1782 $this->db->commit(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref);
1783 dol_syslog(
"createRecurringInvoices Process invoice template ".$facturerec->ref.
" is finished with a success generation");
1785 $this->output .= $langs->trans(
"InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref).
"\n";
1788 if ($loopError > 0) {
1789 $this->output .= $langs->trans(
"InvoiceSentFromTemplateError", $facture->ref, $facturerec->ref, $errormesg ??
'').
"\n";
1794 $this->output .= $langs->trans(
"InvoiceSentFromTemplate", $facture->ref, $facturerec->ref).
"\n";
1797 $this->output .= $langs->trans(
"InvoiceGeneratedFromTemplateError", $facture->ref, $facturerec->ref, $this->error).
"\n";
1798 $this->db->rollback(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref);
1801 $parameters = array(
1804 'errorCount' => $error,
1805 'errorForInvoice' => $errorforinvoice,
1806 'invoiceidgenerated' => $invoiceidgenerated,
1807 'facturerec' => $facturerec,
1811 $reshook = $hookmanager->executeHooks(
'afterCreationOfRecurringInvoice', $parameters, $facture);
1816 $conf->entity = $saventity;
1821 $this->output = trim($this->output);
1823 return $error ? $error : 0;
1838 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1)
1840 global $langs, $hookmanager;
1844 $label =
img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"RepeatableInvoice").
'</u>';
1845 if (!empty($this->
ref)) {
1846 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1848 if ($this->frequency > 0) {
1849 $label .=
'<br><b>'.$langs->trans(
'Frequency').
':</b> '.$langs->trans(
'FrequencyPer_'.$this->unit_frequency, $this->frequency);
1851 if (!empty($this->date_last_gen)) {
1852 $label .=
'<br><b>'.$langs->trans(
'DateLastGeneration').
':</b> '.
dol_print_date($this->date_last_gen,
'dayhour');
1854 if ($this->frequency > 0) {
1855 if (!empty($this->date_when)) {
1856 $label .=
'<br><b>'.$langs->trans(
'NextDateToExecution').
':</b> ';
1857 $label .= (empty($this->suspended) ?
'' :
'<strike>').
dol_print_date($this->date_when,
'day').(empty($this->suspended) ?
'' :
'</strike>');
1858 if (!empty($this->suspended)) {
1859 $label .=
' ('.$langs->trans(
"Disabled").
')';
1864 $url = DOL_URL_ROOT.
'/compta/facture/card-rec.php?id='.$this->id;
1870 if ($option !=
'nolink') {
1872 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1873 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1874 $add_save_lastsearch_values = 1;
1876 if ($add_save_lastsearch_values) {
1877 $url .=
'&save_lastsearch_values=1';
1881 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
1884 $result .= $linkstart;
1886 $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);
1888 if ($withpicto != 2) {
1891 $result .= $linkend;
1893 $hookmanager->initHooks(array($this->element .
'dao'));
1894 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1895 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1897 $result = $hookmanager->resPrint;
1899 $result .= $hookmanager->resPrint;
1913 return $this->
LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->
type) ? 0 : $this->type);
1928 public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0, $nbofopendirectdebitorcredittransfer = 0)
1932 $langs->load(
'bills');
1934 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1935 $statusType =
'status0';
1940 if ($status == self::STATUS_SUSPENDED) {
1941 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1943 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1946 if ($status == self::STATUS_SUSPENDED) {
1947 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1949 $labelStatus = $langs->transnoentitiesnoconv(
"Draft");
1952 } elseif ($mode == 1) {
1955 if ($status == self::STATUS_SUSPENDED) {
1956 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1958 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1961 if ($status == self::STATUS_SUSPENDED) {
1962 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1964 $labelStatus = $langs->transnoentitiesnoconv(
"Draft");
1967 } elseif ($mode == 2) {
1969 if ($status == self::STATUS_SUSPENDED) {
1970 $statusType =
'status6';
1971 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1973 $statusType =
'status4';
1974 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1977 if ($status == self::STATUS_SUSPENDED) {
1978 $statusType =
'status6';
1979 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1981 $statusType =
'status0';
1982 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1985 } elseif ($mode == 3) {
1988 if ($status == self::STATUS_SUSPENDED) {
1989 $statusType =
'status6';
1990 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1992 $statusType =
'status4';
1993 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1996 if ($status == self::STATUS_SUSPENDED) {
1997 $statusType =
'status6';
1998 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2000 $statusType =
'status0';
2001 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
2004 } elseif ($mode == 4) {
2007 if ($status == self::STATUS_SUSPENDED) {
2008 $statusType =
'status6';
2009 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2011 $statusType =
'status4';
2012 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
2015 if ($status == self::STATUS_SUSPENDED) {
2016 $statusType =
'status6';
2017 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2019 $statusType =
'status0';
2020 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
2023 } elseif ($mode == 5 || $mode == 6) {
2029 if ($status == self::STATUS_SUSPENDED) {
2030 $statusType =
'status6';
2031 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2033 $statusType =
'status4';
2034 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
2037 if ($status == self::STATUS_SUSPENDED) {
2038 $statusType =
'status6';
2039 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2041 $statusType =
'status0';
2042 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
2047 $labelStatusShort = $labelStatus;
2049 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
2073 $sql =
'SELECT c.rowid, datec, tms as datem,';
2074 $sql .=
' fk_user_author, fk_user_modif';
2075 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_rec as c';
2076 $sql .=
' WHERE c.rowid = '.((int) $id);
2078 $result = $this->db->query($sql);
2080 if ($this->db->num_rows($result)) {
2081 $obj = $this->db->fetch_object($result);
2083 $this->
id = $obj->rowid;
2085 $this->user_creation_id = $obj->fk_user_author;
2086 $this->user_modification_id = $obj->fk_user_modif;
2087 $this->date_creation = $this->db->jdate($obj->datec);
2088 $this->date_modification = $this->db->jdate($obj->datem);
2090 $this->db->free($result);
2110 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
2116 $sql =
"SELECT rowid";
2117 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2118 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2119 $sql .= $this->db->plimit(100);
2121 $resql = $this->db->query($sql);
2123 $num_prods = $this->db->num_rows($resql);
2125 while ($i < $num_prods) {
2127 $row = $this->db->fetch_row($resql);
2128 $prodids[$i] = $row[0];
2134 $this->
ref =
'SPECIMEN';
2135 $this->title =
'SPECIMEN';
2136 $this->specimen = 1;
2138 $this->
date = $nownotime;
2139 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
2140 $this->cond_reglement_id = 1;
2141 $this->cond_reglement_code =
'RECEP';
2143 $this->mode_reglement_id = 0;
2144 $this->mode_reglement_code =
'';
2145 $this->note_public =
'This is a comment (public)';
2146 $this->note_private =
'This is a comment (private)';
2147 $this->fk_incoterms = 0;
2148 $this->location_incoterms =
'';
2150 if (empty($option) || $option !=
'nolines') {
2154 while ($xnbp < $nbp) {
2156 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
2158 $line->subprice = 100;
2159 $line->tva_tx = 19.6;
2160 $line->localtax1_tx = 0;
2161 $line->localtax2_tx = 0;
2162 $line->remise_percent = 0;
2164 $prodid = mt_rand(1, $num_prods);
2165 $line->fk_product = $prodids[$prodid];
2167 $line->total_ht = -100;
2168 $line->total_ttc = -119.6;
2169 $line->total_tva = -19.6;
2170 } elseif ($xnbp == 2) {
2171 $line->subprice = -100;
2172 $line->total_ht = -100;
2173 $line->total_ttc = -119.6;
2174 $line->total_tva = -19.6;
2175 $line->remise_percent = 0;
2176 } elseif ($xnbp == 3) {
2177 $prodid = mt_rand(1, $num_prods);
2178 $line->fk_product = $prodids[$prodid];
2179 $line->total_ht = 50;
2180 $line->total_ttc = 59.8;
2181 $line->total_tva = 9.8;
2182 $line->remise_percent = 50;
2184 $prodid = mt_rand(1, $num_prods);
2185 $line->fk_product = $prodids[$prodid];
2186 $line->total_ht = 100;
2187 $line->total_ttc = 119.6;
2188 $line->total_tva = 19.6;
2189 $line->remise_percent = 00;
2192 $this->lines[$xnbp] = $line;
2195 $this->total_ht += $line->total_ht;
2196 $this->total_tva += $line->total_tva;
2197 $this->total_ttc += $line->total_ttc;
2199 $this->revenuestamp = 0;
2203 $line->desc = $langs->trans(
"Description").
" (offered line)";
2205 $line->subprice = 100;
2206 $line->tva_tx = 19.6;
2207 $line->localtax1_tx = 0;
2208 $line->localtax2_tx = 0;
2209 $line->remise_percent = 100;
2210 $line->total_ht = 0;
2211 $line->total_ttc = 0;
2212 $line->total_tva = 0;
2213 $prodid = mt_rand(1, $num_prods);
2214 $line->fk_product = $prodids[$prodid];
2216 $this->lines[$xnbp] = $line;
2220 $this->usenewprice = 0;
2271 if (!$this->table_element) {
2272 dol_syslog(get_class($this).
"::setFrequencyAndUnit was called on object with property table_element not defined", LOG_ERR);
2276 if (!empty($frequency) && empty($unit)) {
2277 dol_syslog(get_class($this).
"::setFrequencyAndUnit was called on object with params frequency defined but unit not defined", LOG_ERR);
2281 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2282 $sql .=
' SET frequency = '.($frequency ? $this->db->escape((
string) $frequency) :
'null');
2283 if (!empty($unit)) {
2284 $sql .=
', unit_frequency = \''.$this->db->escape($unit).
'\'';
2286 $sql .=
" WHERE rowid = ".((int) $this->
id);
2288 dol_syslog(get_class($this).
"::setFrequencyAndUnit", LOG_DEBUG);
2289 if ($this->db->query($sql)) {
2290 $this->frequency = $frequency;
2291 if (!empty($unit)) {
2292 $this->unit_frequency = $unit;
2297 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2319 public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
2323 if (!$this->table_element) {
2324 dol_syslog(get_class($this).
"::setNextDate was called on object with property table_element not defined", LOG_ERR);
2327 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2328 $sql .=
" SET date_when = ".($date ?
"'".$this->db->idate($date).
"'" :
"null");
2329 if ($increment_nb_gen_done > 0) {
2330 $sql .=
', nb_gen_done = nb_gen_done + 1';
2332 $sql .=
" WHERE rowid = ".((int) $this->
id);
2334 dol_syslog(get_class($this).
"::setNextDate", LOG_DEBUG);
2335 if ($this->db->query($sql)) {
2336 $this->date_when = $date;
2337 if ($increment_nb_gen_done > 0) {
2338 $this->nb_gen_done++;
2343 if ($resSuspend <= 0) {
2344 dol_syslog(__METHOD__ .
'::setValueFrom Error : ' . $this->error, LOG_ERR);
2351 if (empty($this->context[
'actionmsgmore'])) {
2352 $this->context[
'actionmsgmore'] =
'Trigger called by setNextDate';
2356 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2380 if (!$this->table_element) {
2381 dol_syslog(get_class($this).
"::setMaxPeriod was called on object with property table_element not defined", LOG_ERR);
2389 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2390 $sql .=
' SET nb_gen_max = '.((int) $nb);
2391 $sql .=
" WHERE rowid = ".((int) $this->
id);
2393 dol_syslog(get_class($this).
"::setMaxPeriod", LOG_DEBUG);
2394 if ($this->db->query($sql)) {
2395 $this->nb_gen_max = $nb;
2399 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2424 if (!$this->table_element) {
2425 dol_syslog(get_class($this).
"::setAutoValidate was called on object with property table_element not defined", LOG_ERR);
2429 $this->auto_validate = $validate;
2431 $result = $this->
update($user);
2433 dol_syslog(get_class($this).
"::setAutoValidate", LOG_DEBUG);
2453 if (!$this->table_element) {
2454 dol_syslog(get_class($this).
"::setGeneratePdf was called on object with property table_element not defined", LOG_ERR);
2458 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2459 $sql .=
' SET generate_pdf = '.((int) $validate);
2460 $sql .=
" WHERE rowid = ".((int) $this->
id);
2462 dol_syslog(get_class($this).
"::setGeneratePdf", LOG_DEBUG);
2463 if ($this->db->query($sql)) {
2464 $this->generate_pdf = $validate;
2468 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2492 if (!$this->table_element) {
2493 dol_syslog(get_class($this).
"::setModelPdf was called on object with property table_element not defined", LOG_ERR);
2497 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2498 $sql .=
" SET modelpdf = '".$this->db->escape($model).
"'";
2499 $sql .=
" WHERE rowid = ".((int) $this->
id);
2501 dol_syslog(get_class($this).
"::setModelPdf", LOG_DEBUG);
2502 if ($this->db->query($sql)) {
2503 $this->model_pdf = $model;
2507 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2531 if (!$this->table_element) {
2532 dol_syslog(get_class($this).
"::setMailTemplate was called on object with property table_element not defined", LOG_ERR);
2536 $this->fk_email_template = $idEmailTemplate;
2537 $result = $this->
update($user);
2539 dol_syslog(get_class($this).
"::setMailTemplate", LOG_DEBUG);
2543 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2569 public $element =
'facturedetrec';
2574 public $table_element =
'facturedet_rec';
2594 public $fk_parent_line;
2599 public $fk_product_fournisseur_price;
2604 public $fk_fournprice;
2619 public $description;
2625 public $fk_product_type;
2630 public $fk_contract_line;
2640 public function delete(
User $user, $notrigger = 0)
2649 $result = $this->call_trigger(
'LINEBILLREC_DELETE', $user);
2665 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
' WHERE rowid='.((int) $this->
id);
2667 $res = $this->db->query($sql);
2670 $this->errors[] = $this->db->lasterror();
2676 $this->db->rollback();
2679 $this->db->commit();
2693 $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,';
2694 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2695 $sql .=
' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2696 $sql .=
' l.rang, l.special_code,';
2697 $sql .=
' l.fk_unit, l.fk_contract_line,';
2698 $sql .=
' l.import_key, l.fk_multicurrency,';
2699 $sql .=
' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2700 $sql .=
' l.buy_price_ht, l.fk_product_fournisseur_price,';
2701 $sql .=
' l.fk_user_author, l.fk_user_modif, l.extraparams,';
2702 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2703 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet_rec as l';
2704 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
2705 $sql .=
' WHERE l.rowid = '.((int) $rowid);
2706 $sql .=
' ORDER BY l.rang';
2709 $result = $this->db->query($sql);
2711 $objp = $this->db->fetch_object($result);
2713 $this->
id = $objp->rowid;
2714 $this->fk_facture = $objp->fk_facture;
2715 $this->fk_parent_line = $objp->fk_parent_line;
2716 $this->label = $objp->custom_label;
2717 $this->desc = $objp->description;
2719 $this->product_type = $objp->product_type;
2720 $this->
ref = $objp->product_ref;
2721 $this->product_ref = $objp->product_ref;
2722 $this->libelle = $objp->product_label;
2723 $this->product_label = $objp->product_label;
2724 $this->product_desc = $objp->product_desc;
2725 $this->fk_product_type = $objp->fk_product_type;
2726 $this->qty = $objp->qty;
2727 $this->
price = $objp->price;
2728 $this->subprice = $objp->subprice;
2729 $this->vat_src_code = $objp->vat_src_code;
2730 $this->tva_tx = $objp->tva_tx;
2731 $this->localtax1_tx = $objp->localtax1_tx;
2732 $this->localtax2_tx = $objp->localtax2_tx;
2733 $this->localtax1_type = $objp->localtax1_type;
2734 $this->localtax2_type = $objp->localtax2_type;
2735 $this->remise_percent = $objp->remise_percent;
2737 $this->fk_product = $objp->fk_product;
2738 $this->date_start_fill = $objp->date_start_fill;
2739 $this->date_end_fill = $objp->date_end_fill;
2740 $this->info_bits = $objp->info_bits;
2741 $this->total_ht = $objp->total_ht;
2742 $this->total_tva = $objp->total_tva;
2743 $this->total_ttc = $objp->total_ttc;
2745 $this->rang = $objp->rang;
2746 $this->special_code = $objp->special_code;
2747 $this->fk_unit = $objp->fk_unit;
2748 $this->fk_contract_line = $objp->fk_contract_line;
2749 $this->import_key = $objp->import_key;
2750 $this->fk_multicurrency = $objp->fk_multicurrency;
2751 $this->multicurrency_code = $objp->multicurrency_code;
2752 $this->multicurrency_subprice = $objp->multicurrency_subprice;
2753 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2754 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2755 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2757 $this->buy_price_ht = $objp->buy_price_ht;
2759 $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2760 $this->fk_user_author = $objp->fk_user_author;
2761 $this->fk_user_modif = $objp->fk_user_modif;
2763 $this->db->free($result);
2766 $this->error = $this->db->lasterror();
2786 if (empty($this->fk_parent_line)) {
2787 $this->fk_parent_line = 0;
2790 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2792 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet_rec SET";
2793 $sql .=
" fk_facture = ".((int) $this->fk_facture);
2794 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null");
2795 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
2796 $sql .=
", description='".$this->db->escape($this->desc).
"'";
2797 $sql .=
", price=".price2num($this->
price);
2798 $sql .=
", qty=".price2num($this->qty);
2799 $sql .=
", tva_tx=".price2num($this->tva_tx);
2800 $sql .=
", vat_src_code='".$this->db->escape($this->vat_src_code).
"'";
2801 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
2802 $sql .=
", localtax1_type='".$this->db->escape((
string) $this->localtax1_type).
"'";
2803 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
2804 $sql .=
", localtax2_type='".$this->db->escape((
string) $this->localtax2_type).
"'";
2805 $sql .=
", fk_product=".($this->fk_product > 0 ? $this->fk_product :
"null");
2806 $sql .=
", product_type=".((int) $this->product_type);
2807 $sql .=
", remise_percent=".price2num($this->remise_percent);
2808 $sql .=
", subprice=".price2num($this->subprice);
2809 $sql .=
", info_bits=".price2num($this->info_bits);
2810 $sql .=
", date_start_fill=".(int) $this->date_start_fill;
2811 $sql .=
", date_end_fill=".(int) $this->date_end_fill;
2812 if (empty($this->skip_update_total)) {
2813 $sql .=
", total_ht=".price2num($this->total_ht);
2814 $sql .=
", total_tva=".price2num($this->total_tva);
2815 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
2816 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
2817 $sql .=
", total_ttc=".price2num($this->total_ttc);
2819 $sql .=
", rang=".((int) $this->rang);
2820 $sql .=
", special_code=".((int) $this->special_code);
2821 $sql .=
", fk_unit=".($this->fk_unit ?
"'".$this->db->escape((
string) $this->fk_unit).
"'" :
"null");
2822 $sql .=
", fk_contract_line=".($this->fk_contract_line ? $this->fk_contract_line :
"null");
2823 $sql .=
" WHERE rowid = ".((int) $this->
id);
2827 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
2829 $resql = $this->db->query($sql);
2838 if (!$error && !$notrigger) {
2840 $result = $this->call_trigger(
'LINEBILLREC_MODIFY', $user);
2848 $this->db->rollback();
2851 $this->db->commit();
2855 $this->error = $this->db->lasterror();
2856 $this->db->rollback();
Class to manage agenda events (actions)
Class to send emails (with attachments or not) Usage: $mailfile = new CMailFile($subject,...
Superclass for invoice 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.
setErrorsFromObject($object)
setErrorsFromObject
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.
setValueFrom($field, $value, $table='', $id=null, $format='', $id_field='', $fuser=null, $trigkey='', $fk_user_field='fk_user_modif')
Setter generic.
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.
Class to manage Dolibarr database access.
Class to manage invoices.
const STATUS_VALIDATED
Validated (need to be paid)
Class to manage invoice lines.
Class to manage invoice lines of templates.
fetch($rowid)
Get line of template invoice.
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.
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.
LibStatut($recur, $status, $mode=0, $alreadypaid=-1, $type=0, $nbofopendirectdebitorcredittransfer=0)
Return label of a status.
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 clickable name (with picto eventually)
isMaxNbGenReached()
Return if maximum number of generation is reached.
createRecurringInvoices($restrictioninvoiceid=0, $forcevalidation=0, $notrigger=0, $forcebuilddoc=0)
Create all recurrents invoices (for all entities if multicompany is used).
setModelPdf($model, $notrigger=0)
Update the model for documents.
setMailTemplate($idEmailTemplate, $notrigger=0)
Update the email template.
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 generations 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 execution Handles end-of-month scenarios when day >= 28 for monthly recurring...
Class to manage products or services.
Class to manage translations.
Class to manage Dolibarr users.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
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_now($mode='gmt')
Return date for now.
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_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
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.
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.
complete_substitutions_array(&$substitutionarray, $outputlangs, $object=null, $parameters=null, $callfunc="completesubstitutionarray")
Complete the $substitutionarray with more entries coming from external module that had set the "subst...
make_substitutions($text, $substitutionarray, $outputlangs=null, $converttextinhtmlifnecessary=0)
Make substitution into a text string, replacing keys with vals from $substitutionarray (oldval=>newva...
dol_print_date($time, $format='', $tzoutput='auto', $outputlangs=null, $encodetooutput=false, $decorate=0)
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.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
isModEnabled($module)
Is Dolibarr module enabled.
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...
print $langs trans("Show") . '< td style="' . $timeColor . '" align="center"> s</td > badge status0 badge status4 badge status3 Error badge status8< td align="center">< span class="badge ' . $badge . '"></span ></td >< td align="center">< a href="#" class="button button-small" onclick="openLogModal(this)" data-req="' . dol_escape_htmltag($reqSafe) . '" data-res="' . dol_escape_htmltag($resSafe) . '" data-err="' . dol_escape_htmltag($errSafe) . '">< span class="fa fa-search-plus"></span ></a ></td ></tr >< tr >< td colspan="' . $colspan . '" class="opacitymedium"></td ></tr ></table ></div ></form > logModal none logModal none s a JSON string
buildzip.php
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=null, $localtaxes_array=[], $progress=100, $multicurrency_tx=1, $pu_devise=0, $multicurrency_code='')
Calculate totals (net, vat, ...) of a line.
print $langs trans('Date')." left Ref Label right Qty right Price right TotalHT right TotalTTC right right right right right right right right right centpercent right TotalHT right n right VAT right n right TotalVAT right n No sujeto a RE IRPF right TotalLT1 right n right TotalLT2 right n right TotalTTC right n takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency takeposcustomercurrency right TotalTTC takeposcustomercurrency right takeposcustomercurrency n right Paid right PaymentTypeShortLIQ right SELECT p pos_change as p datep as date
if(preg_match('/(crypted|dolcrypt):/i', $dolibarr_main_db_pass)||!empty($dolibarr_main_db_encrypted_pass)) $conf db type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...