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' =>
'Project',
'enabled' =>
"isModEnabled('project')",
'visible' => -1,
'position' => 85),
270 'fk_cond_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentTerm',
'enabled' => 1,
'visible' => -1,
'position' => 90),
271 'fk_mode_reglement' => array(
'type' =>
'integer',
'label' =>
'PaymentMode',
'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' =>
'BankAccount',
'enabled' =>
'isModEnabled("bank")',
'visible' => -1,
'position' => 175),
289 'fk_multicurrency' => array(
'type' =>
'integer',
'label' =>
'Currency',
'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' =>
'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;
698 $this->
status = $obj->suspended;
699 $this->suspended = $obj->suspended;
701 $this->total_ht = $obj->total_ht;
702 $this->total_tva = $obj->total_tva;
703 $this->total_localtax1 = $obj->localtax1;
704 $this->total_localtax2 = $obj->localtax2;
705 $this->total_ttc = $obj->total_ttc;
706 $this->socid = $obj->fk_soc;
707 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
708 $this->mode_reglement_id = $obj->fk_mode_reglement;
709 $this->mode_reglement_code = $obj->mode_reglement_code;
710 $this->mode_reglement = $obj->mode_reglement_libelle;
711 $this->cond_reglement_id = $obj->fk_cond_reglement;
712 $this->cond_reglement_code = $obj->cond_reglement_code;
713 $this->cond_reglement = $obj->cond_reglement_libelle;
714 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
715 $this->fk_project = $obj->fk_project;
716 $this->fk_account = $obj->fk_account;
717 $this->fk_societe_rib = $obj->fk_societe_rib;
718 $this->note_private = $obj->note_private;
719 $this->note_public = $obj->note_public;
720 $this->user_creation_id = $obj->fk_user_author;
721 $this->model_pdf = $obj->model_pdf;
723 $this->frequency = $obj->frequency;
724 $this->unit_frequency = $obj->unit_frequency;
725 $this->rule_for_lines_dates = $obj->rule_for_lines_dates;
726 $this->date_when = $this->db->jdate($obj->date_when);
727 $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
728 $this->nb_gen_done = $obj->nb_gen_done;
729 $this->nb_gen_max = $obj->nb_gen_max;
730 $this->usenewprice = $obj->usenewprice;
731 $this->auto_validate = $obj->auto_validate;
732 $this->fk_email_template = $obj->fk_email_template;
733 $this->generate_pdf = $obj->generate_pdf;
736 $this->fk_multicurrency = $obj->fk_multicurrency;
737 $this->multicurrency_code = $obj->multicurrency_code;
738 $this->multicurrency_tx = $obj->multicurrency_tx;
739 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
740 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
741 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
745 if (empty($noextrafields)) {
748 $this->error = $this->db->lasterror();
754 if (empty($nolines)) {
757 $this->error = $this->db->lasterror();
764 $this->error =
'Bill with id '.$rowid.
' or ref '.$ref.
' not found';
765 dol_syslog(
'Facture::Fetch Error '.$this->error, LOG_ERR);
769 $this->error = $this->db->error();
796 $this->lines = array();
798 dol_syslog(
'FactureRec::fetch_lines', LOG_DEBUG);
800 $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, ';
801 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
802 $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,';
803 $sql .=
' l.rang, l.special_code,';
804 $sql .=
' l.fk_unit, l.fk_contract_line, l.extraparams,';
805 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
806 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
807 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet_rec as l';
808 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
809 $sql .=
' WHERE l.fk_facture = '.((int) $this->
id);
810 $sql .=
' ORDER BY l.rang';
812 $result = $this->db->query($sql);
814 $num = $this->db->num_rows($result);
817 $objp = $this->db->fetch_object($result);
820 $line->id = $objp->rowid;
821 $line->rowid = $objp->rowid;
822 $line->fk_facture = $objp->fk_facture;
823 $line->fk_parent_line = $objp->fk_parent_line;
824 $line->desc = $objp->description;
825 $line->description = $objp->description;
826 $line->ref = $objp->product_ref;
827 $line->product_ref = $objp->product_ref;
828 $line->libelle = $objp->product_label;
829 $line->product_label = $objp->product_label;
830 $line->product_desc = $objp->product_desc;
831 $line->product_type = $objp->product_type;
832 $line->fk_product_type = $objp->fk_product_type;
833 $line->qty = $objp->qty;
834 $line->subprice = $objp->subprice;
836 $line->label = $objp->custom_label;
838 $line->vat_src_code = $objp->vat_src_code;
839 $line->tva_tx = $objp->tva_tx;
840 $line->localtax1_tx = $objp->localtax1_tx;
841 $line->localtax2_tx = $objp->localtax2_tx;
842 $line->localtax1_type = $objp->localtax1_type;
843 $line->localtax2_type = $objp->localtax2_type;
844 $line->remise_percent = $objp->remise_percent;
846 $line->fk_product = $objp->fk_product;
847 $line->date_start_fill = $objp->date_start_fill;
848 $line->date_end_fill = $objp->date_end_fill;
849 $line->info_bits = $objp->info_bits;
850 $line->total_ht = $objp->total_ht;
851 $line->total_tva = $objp->total_tva;
852 $line->total_ttc = $objp->total_ttc;
854 $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
855 $line->fk_fournprice = $objp->fk_product_fournisseur_price;
857 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
859 $line->buyprice = $marginInfos[0];
860 $line->pa_ht = $marginInfos[0];
861 $line->marge_tx = (string) $marginInfos[1];
862 $line->marque_tx = (string) $marginInfos[2];
863 $line->rang = $objp->rang;
864 $line->special_code = $objp->special_code;
865 $line->fk_unit = $objp->fk_unit;
866 $line->fk_contract_line = $objp->fk_contract_line;
868 $line->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams,
true) : array();
871 $line->price = $objp->price;
872 $line->remise = $objp->remise;
874 $line->fetch_optionals();
877 $line->fk_multicurrency = $objp->fk_multicurrency;
878 $line->multicurrency_code = $objp->multicurrency_code;
879 $line->multicurrency_subprice = $objp->multicurrency_subprice;
880 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
881 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
882 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
884 $this->lines[$i] = $line;
889 $this->db->free($result);
892 $this->error = $this->db->lasterror();
906 public function delete(
User $user, $notrigger = 0, $idwarehouse = -1)
910 dol_syslog(get_class($this).
"::delete rowid=".((
int) $rowid), LOG_DEBUG);
915 $sql_main_table = MAIN_DB_PREFIX.
'facturedet_rec';
916 $sql_ef_table = $sql_main_table.
"_extrafields";
918 $sqlef =
"DELETE FROM $sql_ef_table WHERE fk_object IN (SELECT rowid FROM ".$sql_main_table.
" WHERE fk_facture = ".((int) $rowid).
")";
919 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facturedet_rec WHERE fk_facture = ".((int) $rowid);
921 if ($this->db->query($sqlef) && $this->db->query($sql)) {
922 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facture_rec WHERE rowid = ".((int) $rowid);
924 if ($this->db->query($sql)) {
936 $this->error = $this->db->lasterror();
940 $this->error = $this->db->lasterror();
943 if (!$error && !$notrigger) {
945 $result = $this->call_trigger(
'BILLREC_DELETE', $user);
955 $this->db->rollback();
990 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, $array_options = array())
996 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);
997 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1009 if (preg_match(
'/\((.*)\)/', (
string) $txtva, $reg)) {
1010 $vat_src_code = $reg[1];
1011 $txtva = preg_replace(
'/\s*\(.*\)/',
'', (
string) $txtva);
1016 $remise_percent =
price2num($remise_percent);
1017 if (empty($remise_percent)) {
1018 $remise_percent = 0;
1023 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1026 $txlocaltax1 = (float)
price2num($txlocaltax1);
1027 $txlocaltax2 = (float)
price2num($txlocaltax2);
1028 if (empty($txtva)) {
1031 if (empty($txlocaltax1)) {
1034 if (empty($txlocaltax2)) {
1037 if (empty($info_bits)) {
1041 if ($price_base_type ==
'HT') {
1051 $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);
1052 $total_ht = $tabprice[0];
1053 $total_tva = $tabprice[1];
1054 $total_ttc = $tabprice[2];
1055 $total_localtax1 = $tabprice[9];
1056 $total_localtax2 = $tabprice[10];
1057 $pu_ht = $tabprice[3];
1060 $multicurrency_total_ht = $tabprice[16];
1061 $multicurrency_total_tva = $tabprice[17];
1062 $multicurrency_total_ttc = $tabprice[18];
1063 $pu_ht_devise = $tabprice[19];
1065 $product_type = $type;
1067 $product =
new Product($this->db);
1068 $result = $product->fetch($fk_product);
1069 $product_type = $product->type;
1072 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1073 $fk_parent_line = 0;
1078 if ($ranktouse == -1) {
1080 $ranktouse = $rangmax + 1;
1083 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facturedet_rec (";
1084 $sql .=
"fk_facture";
1085 $sql .=
", fk_parent_line";
1087 $sql .=
", description";
1091 $sql .=
", vat_src_code";
1092 $sql .=
", localtax1_tx";
1093 $sql .=
", localtax1_type";
1094 $sql .=
", localtax2_tx";
1095 $sql .=
", localtax2_type";
1096 $sql .=
", fk_product";
1097 $sql .=
", product_type";
1098 $sql .=
", remise_percent";
1099 $sql .=
", subprice";
1101 $sql .=
", total_ht";
1102 $sql .=
", total_tva";
1103 $sql .=
", total_localtax1";
1104 $sql .=
", total_localtax2";
1105 $sql .=
", total_ttc";
1106 $sql .=
", date_start_fill";
1107 $sql .=
", date_end_fill";
1108 $sql .=
", fk_product_fournisseur_price";
1109 $sql .=
", buy_price_ht";
1110 $sql .=
", info_bits";
1112 $sql .=
", special_code";
1113 $sql .=
", fk_unit";
1114 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
1115 $sql .=
") VALUES (";
1116 $sql .=
" ".((int) $facid);
1117 $sql .=
", ".($fk_parent_line > 0 ? ((int) $fk_parent_line) :
"null");
1118 $sql .=
", ".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
1119 $sql .=
", '".$this->db->escape($desc).
"'";
1120 $sql .=
", ".price2num($pu_ht);
1121 $sql .=
", ".price2num($qty);
1122 $sql .=
", ".price2num($txtva);
1123 $sql .=
", '".$this->db->escape($vat_src_code).
"'";
1124 $sql .=
", ".price2num($txlocaltax1);
1125 $sql .=
", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] :
'').
"'";
1126 $sql .=
", ".price2num($txlocaltax2);
1127 $sql .=
", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] :
'').
"'";
1128 $sql .=
", ".(!empty($fk_product) ?
"'".$this->db->escape((
string) $fk_product).
"'" :
"null");
1129 $sql .=
", ".((int) $product_type);
1130 $sql .=
", ".price2num($remise_percent);
1131 $sql .=
", ".price2num($pu_ht);
1133 $sql .=
", ".price2num($total_ht);
1134 $sql .=
", ".price2num($total_tva);
1135 $sql .=
", ".price2num($total_localtax1);
1136 $sql .=
", ".price2num($total_localtax2);
1137 $sql .=
", ".price2num($total_ttc);
1138 $sql .=
", ".(int) $date_start_fill;
1139 $sql .=
", ".(int) $date_end_fill;
1140 $sql .=
", ".($fk_fournprice > 0 ? ((int) $fk_fournprice) :
'null');
1141 $sql .=
", ".($pa_ht ?
price2num($pa_ht) : 0);
1142 $sql .=
", ".((int) $info_bits);
1143 $sql .=
", ".((int) $ranktouse);
1144 $sql .=
", ".((int) $special_code);
1145 $sql .=
", ".($fk_unit ? ((int) $fk_unit) :
"null");
1146 $sql .=
", ".(int) $this->fk_multicurrency;
1147 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1148 $sql .=
", ".price2num($pu_ht_devise,
'CU');
1149 $sql .=
", ".price2num($multicurrency_total_ht,
'CT');
1150 $sql .=
", ".price2num($multicurrency_total_tva,
'CT');
1151 $sql .=
", ".price2num($multicurrency_total_ttc,
'CT');
1154 dol_syslog(get_class($this).
"::addline", LOG_DEBUG);
1155 if ($this->db->query($sql)) {
1156 $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX.
"facturedet_rec");
1160 if (is_array($array_options) && count($array_options) > 0) {
1162 $factureRecLine->id = $lineId;
1163 $factureRecLine->array_options = $array_options;
1164 $result = $factureRecLine->insertExtraFields();
1166 $this->errors[] = $factureRecLine->error;
1173 $this->error = $this->db->lasterror();
1208 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)
1213 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
1219 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);
1220 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1223 if (empty($remise_percent)) {
1224 $remise_percent = 0;
1233 $remise_percent = (float)
price2num($remise_percent);
1235 if (empty($info_bits)) {
1240 $pu_ht_devise = (float)
price2num($pu_ht_devise);
1241 if (!preg_match(
'/\((.*)\)/', (
string) $txtva)) {
1244 $txlocaltax1 = (float)
price2num($txlocaltax1);
1245 $txlocaltax2 = (float)
price2num($txlocaltax2);
1246 if (empty($txlocaltax1)) {
1249 if (empty($txlocaltax2)) {
1253 if (empty($this->multicurrency_subprice)) {
1254 $this->multicurrency_subprice = 0;
1256 if (empty($this->multicurrency_total_ht)) {
1257 $this->multicurrency_total_ht = 0;
1259 if (empty($this->multicurrency_total_tva)) {
1260 $this->multicurrency_total_tva = 0;
1262 if (empty($this->multicurrency_total_ttc)) {
1263 $this->multicurrency_total_ttc = 0;
1266 if ($price_base_type ==
'HT') {
1281 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1282 $vat_src_code = $reg[1];
1283 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1286 $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);
1288 $total_ht = $tabprice[0];
1289 $total_tva = $tabprice[1];
1290 $total_ttc = $tabprice[2];
1291 $total_localtax1 = $tabprice[9];
1292 $total_localtax2 = $tabprice[10];
1293 $pu_ht = $tabprice[3];
1294 $pu_tva = $tabprice[4];
1295 $pu_ttc = $tabprice[5];
1298 $multicurrency_total_ht = $tabprice[16];
1299 $multicurrency_total_tva = $tabprice[17];
1300 $multicurrency_total_ttc = $tabprice[18];
1301 $pu_ht_devise = $tabprice[19];
1303 $product_type = $type;
1305 $product =
new Product($this->db);
1306 $result = $product->fetch($fk_product);
1307 $product_type = $product->type;
1310 if (empty($fk_parent_line) || $fk_parent_line < 0) {
1311 $fk_parent_line = 0;
1314 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet_rec SET ";
1315 $sql .=
"fk_facture = ".((int) $facid);
1316 $sql .=
", fk_parent_line = ".($fk_parent_line > 0 ? ((int) $fk_parent_line) :
"null");
1317 $sql .=
", label=".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
1318 $sql .=
", description='".$this->db->escape($desc).
"'";
1319 $sql .=
", price=".price2num($pu_ht);
1320 $sql .=
", qty=".price2num($qty);
1321 $sql .=
", tva_tx=".price2num($txtva);
1322 $sql .=
", vat_src_code='".$this->db->escape($vat_src_code).
"'";
1323 $sql .=
", localtax1_tx=".((float) $txlocaltax1);
1324 $sql .=
", localtax1_type='".$this->db->escape($localtaxes_type[0]).
"'";
1325 $sql .=
", localtax2_tx=".((float) $txlocaltax2);
1326 $sql .=
", localtax2_type='".$this->db->escape($localtaxes_type[2]).
"'";
1327 $sql .=
", fk_product=".(!empty($fk_product) ?
"'".$this->db->escape((
string) $fk_product).
"'" :
"null");
1328 $sql .=
", product_type=".((int) $product_type);
1329 $sql .=
", remise_percent='".price2num($remise_percent).
"'";
1330 $sql .=
", subprice='".price2num($pu_ht).
"'";
1331 $sql .=
", total_ht='".price2num($total_ht).
"'";
1332 $sql .=
", total_tva='".price2num($total_tva).
"'";
1333 $sql .=
", total_localtax1='".price2num($total_localtax1).
"'";
1334 $sql .=
", total_localtax2='".price2num($total_localtax2).
"'";
1335 $sql .=
", total_ttc='".price2num($total_ttc).
"'";
1336 $sql .=
", date_start_fill=".((int) $date_start_fill);
1337 $sql .=
", date_end_fill=".((int) $date_end_fill);
1338 $sql .=
", fk_product_fournisseur_price=".($fk_fournprice > 0 ? (int) $fk_fournprice :
'null');
1339 $sql .=
", buy_price_ht=".($pa_ht ?
price2num($pa_ht) : 0);
1340 $sql .=
", info_bits=".((int) $info_bits);
1341 $sql .=
", rang=".((int) $rang);
1342 $sql .=
", special_code=".((int) $special_code);
1343 $sql .=
", fk_unit=".($fk_unit ?
"'".$this->db->escape((
string) $fk_unit).
"'" :
"null");
1344 $sql .=
', multicurrency_subprice = '.price2num($pu_ht_devise);
1345 $sql .=
', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1346 $sql .=
', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1347 $sql .=
', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1348 $sql .=
" WHERE rowid = ".((int) $rowid);
1350 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1351 if ($this->db->query($sql)) {
1356 $this->error = $this->db->lasterror();
1372 if (empty($this->date_when)) {
1378 $originalDay = (int) $dateInfo[
'mday'];
1379 $originalMonth = (int) $dateInfo[
'mon'];
1380 $originalYear = (int) $dateInfo[
'year'];
1381 $originalHour = (int) $dateInfo[
'hours'];
1382 $originalMin = (int) $dateInfo[
'minutes'];
1383 $originalSec = (int) $dateInfo[
'seconds'];
1386 if ($originalDay >= 28 && $this->unit_frequency ==
'm') {
1388 $lastDayOfOriginalMonth = (int)
date(
't', $this->date_when);
1391 $targetMonth = $originalMonth + (int) $this->frequency;
1392 $targetYear = $originalYear;
1395 while ($targetMonth > 12) {
1399 while ($targetMonth < 1) {
1405 $lastDayOfTargetMonth = (int)
date(
't',
dol_mktime(0, 0, 0, $targetMonth, 1, $targetYear));
1409 if ($originalDay >= $lastDayOfOriginalMonth || $originalDay >= 29) {
1411 $targetDay = $lastDayOfTargetMonth;
1415 $targetDay = min($originalDay, $lastDayOfTargetMonth);
1419 return dol_mktime($originalHour, $originalMin, $originalSec, $targetMonth, $targetDay, $targetYear);
1434 if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1465 global
$conf, $langs, $user, $hookmanager, $action;
1471 $langs->loadLangs(array(
"main",
"bills"));
1475 $today =
dol_mktime(23, 59, 59, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
1479 dol_syslog(
"createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid.
" forcevalidation=".$forcevalidation.
" forcebuilddoc=".$forcebuilddoc, LOG_DEBUG);
1481 $sql =
'SELECT rowid FROM '.MAIN_DB_PREFIX.
'facture_rec';
1482 $sql .=
' WHERE frequency > 0';
1483 $sql .=
" AND (date_when IS NULL OR date_when <= '".$this->db->idate($today).
"')";
1484 $sql .=
' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1485 $sql .=
' AND suspended = 0';
1486 $sql .=
' AND entity = '.((int)
$conf->entity);
1487 if ($restrictioninvoiceid > 0) {
1488 $sql .=
' AND rowid = '.((int) $restrictioninvoiceid);
1490 $sql .= $this->db->order(
'entity',
'ASC');
1492 $sql .= $this->db->plimit(
getDolGlobalInt(
'NB_REC_FACT_CUSTOMER_GEN_BY_CALL'));
1495 $parameters = array(
1496 'restrictioninvoiceid' => $restrictioninvoiceid,
1497 'forcevalidation' => $forcevalidation,
1499 $reshook = $hookmanager->executeHooks(
'beforeCreationOfRecurringInvoices', $parameters, $sql);
1501 $resql = $this->db->query($sql);
1504 $num = $this->db->num_rows($resql);
1507 $this->output .= $langs->trans(
"FoundXQualifiedRecurringInvoiceTemplate", $num).
"\n";
1509 $this->output .= $langs->trans(
"NoQualifiedRecurringInvoiceTemplateFound");
1512 $saventity =
$conf->entity;
1515 $line = $this->db->fetch_object($resql);
1519 $errorforinvoice = 0;
1520 $invoiceidgenerated = 0;
1521 $mailHasSent =
false;
1527 $facturerec->fetch($line->rowid);
1529 if ($facturerec->id > 0) {
1531 $conf->entity = $facturerec->entity;
1533 dol_syslog(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref.
", entity=".$facturerec->entity);
1535 $facture =
new Facture($this->db);
1536 $facture->fac_rec = $facturerec->id;
1537 $facture->fk_fac_rec_source = $facturerec->id;
1540 $facture->subtype = $facturerec->subtype;
1543 $facture->date = (empty($facturerec->date_when) ? $now : $facturerec->date_when);
1544 $facture->socid = $facturerec->socid;
1546 if (!empty($facturerec->fk_multicurrency)) {
1547 $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1548 $facture->multicurrency_code = $facturerec->multicurrency_code;
1549 $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1552 if (isset($facture->array_options) && isset($facturerec->array_options)) {
1553 foreach ($facturerec->array_options as $key => $value) {
1554 if (isset($facture->array_options[$key])) {
1555 $facture->array_options[$key] = $value;
1560 $parameters[
'facture'] = &$facture;
1561 $reshook = $hookmanager->executeHooks(
'beforeCreationOfEachRecurringInvoice', $parameters, $facturerec, $action);
1564 $invoiceidgenerated = $facture->create($user, 0, 0, (
isModEnabled(
'multicurrency') ? $facturerec->usenewcurrencyrate : 0));
1565 if ($invoiceidgenerated <= 0) {
1572 if (!$errorforinvoice && ($facturerec->auto_validate || $forcevalidation)) {
1573 $result = $facture->validate($user);
1580 if (!$errorforinvoice && ($facturerec->generate_pdf || $forcebuilddoc || $facturerec->auto_validate == 2)) {
1582 $facture->fetch($facture->id);
1583 $facture->fetch_thirdparty();
1585 $outputlangs = $langs;
1587 if (!empty($facture->thirdparty->default_lang)) {
1589 $outputlangs->setDefaultLang($facture->thirdparty->default_lang);
1590 $outputlangs->loadLangs(array(
'main',
'bills'));
1595 if ($facturerec->generate_pdf || $forcebuilddoc) {
1596 $result = $facture->generateDocument($facturerec->model_pdf, $outputlangs);
1606 require_once DOL_DOCUMENT_ROOT .
'/core/class/html.formmail.class.php';
1607 require_once DOL_DOCUMENT_ROOT .
'/core/class/CMailFile.class.php';
1608 $formmail =
new FormMail($this->db);
1611 $template = $facturerec->fk_email_template;
1612 $arraymessage = $formmail->getEMailTemplate($this->db,
'facture_send', $user, $outputlangs, (is_numeric($template) ? $template : 0), 1, (is_numeric($template) ?
'' : $template), (($template != 0) ? -1 : 1));
1613 if (is_numeric($arraymessage) && $arraymessage <= 0) {
1614 $langs->load(
"errors");
1615 $this->output .= $langs->trans(
'ErrorFailedToFindEmailTemplate', $template ??
'');
1623 $substitutionarray = getCommonSubstitutionArray($outputlangs, 0,
null, $facture);
1628 $sendTopic =
make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv(
'InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
1631 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
1637 $res = $facture->fetch_thirdparty();
1638 $recipient = $facture->thirdparty;
1640 $tmparraycontact = $facture->liste_contact(-1,
'external', 0,
'BILLING');
1641 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
1642 foreach ($tmparraycontact as $data_email) {
1643 if (!empty($data_email[
'email'])) {
1644 $to[] = $facture->thirdparty->contact_get_property($data_email[
'id'],
'email');
1648 if (empty($to) && !empty($recipient->email)) {
1649 $to[] = $recipient->email;
1652 $errormesg =
"Failed to send invoice to thirdparty id=".$facture->socid.
". No email defined for invoice or customer.";
1656 $errormesg =
"Failed to load recipient with thirdparty id=".$facture->socid;
1662 if (!empty($arraymessage->email_from)) {
1663 $email_from = (string) $arraymessage->email_from;
1665 if (empty($email_from)) {
1666 $errormesg =
"Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
1670 if (!$loopError && !empty($to)) {
1671 $to = implode(
',', $to);
1672 if (!empty($arraymessage->email_to)) {
1673 $to = $to .
',' . $arraymessage->email_to;
1679 $trackid =
'inv'.$facture->id;
1680 $sendcontext =
'standard';
1684 if (!empty($arraymessage->email_tocc)) {
1685 $email_tocc = (string) $arraymessage->email_tocc;
1690 if (!empty($arraymessage->email_tobcc)) {
1691 $email_tobcc = (string) $arraymessage->email_tobcc;
1698 if ($arraymessage->joinfiles == 1 && !empty($facture->last_main_doc)) {
1699 $joinFile[] = DOL_DATA_ROOT.
'/'.$facture->last_main_doc;
1700 $joinFileName[] = basename($facture->last_main_doc);
1701 $joinFileMime[] =
dol_mimetype(DOL_DATA_ROOT.
'/'.$facture->last_main_doc);
1705 $cMailFile =
new CMailFile($sendTopic, $to, $email_from, $sendContent, $joinFile, $joinFileMime, $joinFileName, $email_tocc, $email_tobcc, 0, 1, $errors_to,
'', $trackid,
'', $sendcontext,
'');
1707 $resultsendmail = $cMailFile->sendfile();
1712 if ($resultsendmail) {
1713 $mailHasSent =
true;
1716 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
1721 $actioncomm->type_code =
'AC_OTH_AUTO';
1722 $actioncomm->socid = $facture->thirdparty->id;
1723 $actioncomm->contact_id = 0;
1725 $actioncomm->code =
'AC_EMAIL';
1726 $actioncomm->label = $langs->trans(
'MailSentByTo', $email_from, $to);
1727 $actioncomm->note_private = $sendContent;
1728 $actioncomm->fk_project = $facture->fk_project;
1729 $actioncomm->datep =
dol_now();
1730 $actioncomm->datef = $actioncomm->datep;
1731 $actioncomm->percentage = -1;
1732 $actioncomm->authorid = $user->id;
1733 $actioncomm->userownerid = $user->id;
1735 $actioncomm->email_msgid = $cMailFile->msgid;
1736 $actioncomm->email_subject = $sendTopic;
1737 $actioncomm->email_from = $email_from;
1738 $actioncomm->email_sender =
'';
1739 $actioncomm->email_to = $to;
1743 $actioncomm->errors_to = $errors_to;
1745 $actioncomm->elementtype =
'invoice';
1746 $actioncomm->elementid = $facture->id;
1750 $actioncomm->create($user);
1752 $errormesg = $cMailFile->error.
' : '.$to;
1756 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
1761 $actioncomm->type_code =
'AC_OTH_AUTO';
1762 $actioncomm->socid = $facture->thirdparty->id;
1763 $actioncomm->contact_id = 0;
1765 $actioncomm->code =
'AC_EMAIL';
1766 $actioncomm->label = $langs->trans(
'sendAutoEmailInvoiceKO', $email_from, $to);
1767 $actioncomm->note_private = $errormesg;
1768 $actioncomm->fk_project = $facture->fk_project;
1769 $actioncomm->datep =
dol_now();
1770 $actioncomm->datef = $actioncomm->datep;
1771 $actioncomm->percentage = -1;
1772 $actioncomm->authorid = $user->id;
1773 $actioncomm->userownerid = $user->id;
1775 $actioncomm->email_msgid = $cMailFile->msgid;
1776 $actioncomm->email_subject = $sendTopic;
1777 $actioncomm->email_from = $email_from;
1778 $actioncomm->email_sender =
'';
1779 $actioncomm->email_to = $to;
1783 $actioncomm->errors_to = $errors_to;
1785 $actioncomm->elementtype =
'invoice';
1786 $actioncomm->elementid = $facture->id;
1790 $actioncomm->create($user);
1793 $this->db->commit();
1799 $this->error =
"Failed to load invoice template with id=".$line->rowid.
", entity=".
$conf->entity.
"\n";
1800 $this->errors[] =
"Failed to load invoice template with id=".$line->rowid.
", entity=".
$conf->entity;
1801 dol_syslog(
"createRecurringInvoices Failed to load invoice template with id=".$line->rowid.
", entity=".
$conf->entity);
1806 if (!$errorforinvoice && $invoiceidgenerated > 0) {
1807 $this->db->commit(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref);
1808 dol_syslog(
"createRecurringInvoices Process invoice template ".$facturerec->ref.
" is finished with a success generation");
1810 $this->output .= $langs->trans(
"InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref).
"\n";
1813 if ($loopError > 0) {
1814 $this->output .= $langs->trans(
"InvoiceSentFromTemplateError", $facture->ref, $facturerec->ref, $errormesg ??
'').
"\n";
1819 $this->output .= $langs->trans(
"InvoiceSentFromTemplate", $facture->ref, $facturerec->ref).
"\n";
1823 $this->output .= $langs->trans(
"InvoiceGeneratedFromTemplateError", (is_object($facture) ? $facture->ref :
''), $facturerec->ref, $this->error).
"\n";
1824 $this->db->rollback(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref);
1827 $parameters = array(
1830 'errorCount' => $error,
1831 'errorForInvoice' => $errorforinvoice,
1832 'invoiceidgenerated' => $invoiceidgenerated,
1833 'facturerec' => $facturerec,
1837 $reshook = $hookmanager->executeHooks(
'afterCreationOfRecurringInvoice', $parameters, $facture);
1842 $conf->entity = $saventity;
1844 dol_print_error($this->db);
1847 $this->output = trim($this->output);
1849 return $error ? $error : 0;
1864 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $save_lastsearch_value = -1)
1866 global $langs, $hookmanager;
1870 $label = img_picto(
'', $this->picto).
' <u class="paddingrightonly">'.$langs->trans(
"RepeatableInvoice").
'</u>';
1871 if (!empty($this->
ref)) {
1872 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1874 if ($this->frequency > 0) {
1875 $label .=
'<br><b>'.$langs->trans(
'Frequency').
':</b> '.$langs->trans(
'FrequencyPer_'.$this->unit_frequency, $this->frequency);
1877 if (!empty($this->date_last_gen)) {
1878 $label .=
'<br><b>'.$langs->trans(
'DateLastGeneration').
':</b> '.
dol_print_date($this->date_last_gen,
'dayhour');
1880 if ($this->frequency > 0) {
1881 if (!empty($this->date_when)) {
1882 $label .=
'<br><b>'.$langs->trans(
'NextDateToExecution').
':</b> ';
1883 $label .= (empty($this->suspended) ?
'' :
'<strike>').
dol_print_date($this->date_when,
'day').(empty($this->suspended) ?
'' :
'</strike>');
1884 if (!empty($this->suspended)) {
1885 $label .=
' ('.$langs->trans(
"Disabled").
')';
1890 $url = DOL_URL_ROOT.
'/compta/facture/card-rec.php?id='.$this->id;
1896 if ($option !=
'nolink') {
1898 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1899 if ($save_lastsearch_value == -1 && isset($_SERVER[
"PHP_SELF"]) && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1900 $add_save_lastsearch_values = 1;
1902 if ($add_save_lastsearch_values) {
1903 $url .=
'&save_lastsearch_values=1';
1907 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
1910 $result .= $linkstart;
1912 $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);
1914 if ($withpicto != 2) {
1917 $result .= $linkend;
1919 $hookmanager->initHooks(array($this->element .
'dao'));
1920 $parameters = array(
'id' => $this->
id,
'getnomurl' => &$result);
1921 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1923 $result = $hookmanager->resPrint;
1925 $result .= $hookmanager->resPrint;
1939 return $this->
LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->
type) ? 0 : $this->type);
1954 public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0, $nbofopendirectdebitorcredittransfer = 0)
1958 $langs->load(
'bills');
1960 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1961 $statusType =
'status0';
1966 if ($status == self::STATUS_SUSPENDED) {
1967 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1969 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1972 if ($status == self::STATUS_SUSPENDED) {
1973 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1975 $labelStatus = $langs->transnoentitiesnoconv(
"Draft");
1978 } elseif ($mode == 1) {
1981 if ($status == self::STATUS_SUSPENDED) {
1982 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1984 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1987 if ($status == self::STATUS_SUSPENDED) {
1988 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1990 $labelStatus = $langs->transnoentitiesnoconv(
"Draft");
1993 } elseif ($mode == 2) {
1995 if ($status == self::STATUS_SUSPENDED) {
1996 $statusType =
'status6';
1997 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1999 $statusType =
'status4';
2000 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
2003 if ($status == self::STATUS_SUSPENDED) {
2004 $statusType =
'status6';
2005 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2007 $statusType =
'status0';
2008 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
2011 } elseif ($mode == 3) {
2014 if ($status == self::STATUS_SUSPENDED) {
2015 $statusType =
'status6';
2016 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2018 $statusType =
'status4';
2019 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
2022 if ($status == self::STATUS_SUSPENDED) {
2023 $statusType =
'status6';
2024 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2026 $statusType =
'status0';
2027 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
2030 } elseif ($mode == 4) {
2033 if ($status == self::STATUS_SUSPENDED) {
2034 $statusType =
'status6';
2035 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2037 $statusType =
'status4';
2038 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
2041 if ($status == self::STATUS_SUSPENDED) {
2042 $statusType =
'status6';
2043 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2045 $statusType =
'status0';
2046 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
2049 } elseif ($mode == 5 || $mode == 6) {
2055 if ($status == self::STATUS_SUSPENDED) {
2056 $statusType =
'status6';
2057 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2059 $statusType =
'status4';
2060 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
2063 if ($status == self::STATUS_SUSPENDED) {
2064 $statusType =
'status6';
2065 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
2067 $statusType =
'status0';
2068 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
2073 $labelStatusShort = $labelStatus;
2075 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
2099 $sql =
'SELECT c.rowid, datec, tms as datem,';
2100 $sql .=
' fk_user_author, fk_user_modif';
2101 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_rec as c';
2102 $sql .=
' WHERE c.rowid = '.((int) $id);
2104 $result = $this->db->query($sql);
2106 if ($this->db->num_rows($result)) {
2107 $obj = $this->db->fetch_object($result);
2109 $this->
id = $obj->rowid;
2111 $this->user_creation_id = $obj->fk_user_author;
2112 $this->user_modification_id = $obj->fk_user_modif;
2113 $this->date_creation = $this->db->jdate($obj->datec);
2114 $this->date_modification = $this->db->jdate($obj->datem);
2116 $this->db->free($result);
2118 dol_print_error($this->db);
2136 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
2142 $sql =
"SELECT rowid";
2143 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
2144 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
2145 $sql .= $this->db->plimit(100);
2147 $resql = $this->db->query($sql);
2149 $num_prods = $this->db->num_rows($resql);
2151 while ($i < $num_prods) {
2153 $row = $this->db->fetch_row($resql);
2154 $prodids[$i] = $row[0];
2160 $this->
ref =
'SPECIMEN';
2161 $this->title =
'SPECIMEN';
2162 $this->specimen = 1;
2164 $this->
date = $nownotime;
2165 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
2166 $this->cond_reglement_id = 1;
2167 $this->cond_reglement_code =
'RECEP';
2169 $this->mode_reglement_id = 0;
2170 $this->mode_reglement_code =
'';
2171 $this->note_public =
'This is a comment (public)';
2172 $this->note_private =
'This is a comment (private)';
2173 $this->fk_incoterms = 0;
2174 $this->location_incoterms =
'';
2176 if (empty($option) || $option !=
'nolines') {
2180 while ($xnbp < $nbp) {
2182 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
2184 $line->subprice = 100;
2185 $line->tva_tx = 19.6;
2186 $line->localtax1_tx = 0;
2187 $line->localtax2_tx = 0;
2188 $line->remise_percent = 0;
2190 $prodid = mt_rand(1, $num_prods);
2191 $line->fk_product = $prodids[$prodid];
2193 $line->total_ht = -100;
2194 $line->total_ttc = -119.6;
2195 $line->total_tva = -19.6;
2196 } elseif ($xnbp == 2) {
2197 $line->subprice = -100;
2198 $line->total_ht = -100;
2199 $line->total_ttc = -119.6;
2200 $line->total_tva = -19.6;
2201 $line->remise_percent = 0;
2202 } elseif ($xnbp == 3) {
2203 $prodid = mt_rand(1, $num_prods);
2204 $line->fk_product = $prodids[$prodid];
2205 $line->total_ht = 50;
2206 $line->total_ttc = 59.8;
2207 $line->total_tva = 9.8;
2208 $line->remise_percent = 50;
2210 $prodid = mt_rand(1, $num_prods);
2211 $line->fk_product = $prodids[$prodid];
2212 $line->total_ht = 100;
2213 $line->total_ttc = 119.6;
2214 $line->total_tva = 19.6;
2215 $line->remise_percent = 00;
2218 $this->lines[$xnbp] = $line;
2221 $this->total_ht += $line->total_ht;
2222 $this->total_tva += $line->total_tva;
2223 $this->total_ttc += $line->total_ttc;
2225 $this->revenuestamp = 0;
2229 $line->desc = $langs->trans(
"Description").
" (offered line)";
2231 $line->subprice = 100;
2232 $line->tva_tx = 19.6;
2233 $line->localtax1_tx = 0;
2234 $line->localtax2_tx = 0;
2235 $line->remise_percent = 100;
2236 $line->total_ht = 0;
2237 $line->total_ttc = 0;
2238 $line->total_tva = 0;
2239 $prodid = mt_rand(1, $num_prods);
2240 $line->fk_product = $prodids[$prodid];
2242 $this->lines[$xnbp] = $line;
2246 $this->usenewprice = 0;
2297 if (!$this->table_element) {
2298 dol_syslog(get_class($this).
"::setFrequencyAndUnit was called on object with property table_element not defined", LOG_ERR);
2302 if (!empty($frequency) && empty($unit)) {
2303 dol_syslog(get_class($this).
"::setFrequencyAndUnit was called on object with params frequency defined but unit not defined", LOG_ERR);
2307 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2308 $sql .=
' SET frequency = '.($frequency ? $this->db->escape((
string) $frequency) :
'null');
2309 if (!empty($unit)) {
2310 $sql .=
', unit_frequency = \''.$this->db->escape($unit).
'\'';
2312 $sql .=
" WHERE rowid = ".((int) $this->
id);
2314 dol_syslog(get_class($this).
"::setFrequencyAndUnit", LOG_DEBUG);
2315 if ($this->db->query($sql)) {
2316 $this->frequency = $frequency;
2317 if (!empty($unit)) {
2318 $this->unit_frequency = $unit;
2323 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2332 dol_print_error($this->db);
2345 public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
2349 if (!$this->table_element) {
2350 dol_syslog(get_class($this).
"::setNextDate was called on object with property table_element not defined", LOG_ERR);
2353 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2354 $sql .=
" SET date_when = ".($date ?
"'".$this->db->idate($date).
"'" :
"null");
2355 if ($increment_nb_gen_done > 0) {
2356 $sql .=
', nb_gen_done = nb_gen_done + 1';
2358 $sql .=
" WHERE rowid = ".((int) $this->
id);
2360 dol_syslog(get_class($this).
"::setNextDate", LOG_DEBUG);
2361 if ($this->db->query($sql)) {
2362 $this->date_when = $date;
2363 if ($increment_nb_gen_done > 0) {
2364 $this->nb_gen_done++;
2369 if ($resSuspend <= 0) {
2370 dol_syslog(__METHOD__ .
'::setValueFrom Error : ' . $this->error, LOG_ERR);
2377 if (empty($this->context[
'actionmsgmore'])) {
2378 $this->context[
'actionmsgmore'] =
'Trigger called by setNextDate';
2382 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2390 dol_print_error($this->db);
2406 if (!$this->table_element) {
2407 dol_syslog(get_class($this).
"::setMaxPeriod was called on object with property table_element not defined", LOG_ERR);
2415 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2416 $sql .=
' SET nb_gen_max = '.((int) $nb);
2417 $sql .=
" WHERE rowid = ".((int) $this->
id);
2419 dol_syslog(get_class($this).
"::setMaxPeriod", LOG_DEBUG);
2420 if ($this->db->query($sql)) {
2421 $this->nb_gen_max = $nb;
2425 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2434 dol_print_error($this->db);
2450 if (!$this->table_element) {
2451 dol_syslog(get_class($this).
"::setAutoValidate was called on object with property table_element not defined", LOG_ERR);
2455 $this->auto_validate = $validate;
2457 $result = $this->
update($user);
2459 dol_syslog(get_class($this).
"::setAutoValidate", LOG_DEBUG);
2463 dol_print_error($this->db);
2479 if (!$this->table_element) {
2480 dol_syslog(get_class($this).
"::setGeneratePdf was called on object with property table_element not defined", LOG_ERR);
2484 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2485 $sql .=
' SET generate_pdf = '.((int) $validate);
2486 $sql .=
" WHERE rowid = ".((int) $this->
id);
2488 dol_syslog(get_class($this).
"::setGeneratePdf", LOG_DEBUG);
2489 if ($this->db->query($sql)) {
2490 $this->generate_pdf = $validate;
2494 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2503 dol_print_error($this->db);
2518 if (!$this->table_element) {
2519 dol_syslog(get_class($this).
"::setModelPdf was called on object with property table_element not defined", LOG_ERR);
2523 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2524 $sql .=
" SET modelpdf = '".$this->db->escape($model).
"'";
2525 $sql .=
" WHERE rowid = ".((int) $this->
id);
2527 dol_syslog(get_class($this).
"::setModelPdf", LOG_DEBUG);
2528 if ($this->db->query($sql)) {
2529 $this->model_pdf = $model;
2533 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2542 dol_print_error($this->db);
2557 if (!$this->table_element) {
2558 dol_syslog(get_class($this).
"::setMailTemplate was called on object with property table_element not defined", LOG_ERR);
2562 $this->fk_email_template = $idEmailTemplate;
2563 $result = $this->
update($user);
2565 dol_syslog(get_class($this).
"::setMailTemplate", LOG_DEBUG);
2569 $result = $this->call_trigger(
'BILLREC_MODIFY', $user);
2578 dol_print_error($this->db);
2595 public $element =
'facturedetrec';
2600 public $table_element =
'facturedet_rec';
2620 public $fk_parent_line;
2625 public $fk_product_fournisseur_price;
2630 public $fk_fournprice;
2645 public $description;
2651 public $fk_product_type;
2656 public $fk_contract_line;
2661 public $skip_update_total;
2670 public function delete(
User $user, $notrigger = 0)
2679 $result = $this->call_trigger(
'LINEBILLREC_DELETE', $user);
2695 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
' WHERE rowid='.((int) $this->
id);
2697 $res = $this->db->query($sql);
2700 $this->errors[] = $this->db->lasterror();
2706 $this->db->rollback();
2709 $this->db->commit();
2723 $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,';
2724 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2725 $sql .=
' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2726 $sql .=
' l.rang, l.special_code,';
2727 $sql .=
' l.fk_unit, l.fk_contract_line,';
2728 $sql .=
' l.import_key, l.fk_multicurrency,';
2729 $sql .=
' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2730 $sql .=
' l.buy_price_ht, l.fk_product_fournisseur_price,';
2731 $sql .=
' l.fk_user_author, l.fk_user_modif, l.extraparams,';
2732 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2733 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet_rec as l';
2734 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
2735 $sql .=
' WHERE l.rowid = '.((int) $rowid);
2736 $sql .=
' ORDER BY l.rang';
2739 $result = $this->db->query($sql);
2741 $objp = $this->db->fetch_object($result);
2743 $this->
id = $objp->rowid;
2744 $this->fk_facture = $objp->fk_facture;
2745 $this->fk_parent_line = $objp->fk_parent_line;
2746 $this->label = $objp->custom_label;
2747 $this->desc = $objp->description;
2749 $this->product_type = $objp->product_type;
2750 $this->
ref = $objp->product_ref;
2751 $this->product_ref = $objp->product_ref;
2752 $this->libelle = $objp->product_label;
2753 $this->product_label = $objp->product_label;
2754 $this->product_desc = $objp->product_desc;
2755 $this->fk_product_type = $objp->fk_product_type;
2756 $this->qty = $objp->qty;
2757 $this->
price = $objp->price;
2758 $this->subprice = $objp->subprice;
2759 $this->vat_src_code = $objp->vat_src_code;
2760 $this->tva_tx = $objp->tva_tx;
2761 $this->localtax1_tx = $objp->localtax1_tx;
2762 $this->localtax2_tx = $objp->localtax2_tx;
2763 $this->localtax1_type = $objp->localtax1_type;
2764 $this->localtax2_type = $objp->localtax2_type;
2765 $this->remise_percent = $objp->remise_percent;
2767 $this->fk_product = $objp->fk_product;
2768 $this->date_start_fill = $objp->date_start_fill;
2769 $this->date_end_fill = $objp->date_end_fill;
2770 $this->info_bits = $objp->info_bits;
2771 $this->total_ht = $objp->total_ht;
2772 $this->total_tva = $objp->total_tva;
2773 $this->total_ttc = $objp->total_ttc;
2775 $this->rang = $objp->rang;
2776 $this->special_code = $objp->special_code;
2777 $this->fk_unit = $objp->fk_unit;
2778 $this->fk_contract_line = $objp->fk_contract_line;
2779 $this->import_key = $objp->import_key;
2780 $this->fk_multicurrency = $objp->fk_multicurrency;
2781 $this->multicurrency_code = $objp->multicurrency_code;
2782 $this->multicurrency_subprice = $objp->multicurrency_subprice;
2783 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2784 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2785 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2787 $this->buy_price_ht = $objp->buy_price_ht;
2789 $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2790 $this->fk_user_author = $objp->fk_user_author;
2791 $this->fk_user_modif = $objp->fk_user_modif;
2793 $this->db->free($result);
2796 $this->error = $this->db->lasterror();
2814 if (empty($this->fk_parent_line)) {
2815 $this->fk_parent_line = 0;
2818 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2820 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet_rec SET";
2821 $sql .=
" fk_facture = ".((int) $this->fk_facture);
2822 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? ((int) $this->fk_parent_line) :
"null");
2823 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
2824 $sql .=
", description='".$this->db->escape($this->desc).
"'";
2825 $sql .=
", price=".price2num($this->
price);
2826 $sql .=
", qty=".price2num($this->qty);
2827 $sql .=
", tva_tx=".price2num($this->tva_tx);
2828 $sql .=
", vat_src_code='".$this->db->escape($this->vat_src_code).
"'";
2829 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
2830 $sql .=
", localtax1_type='".$this->db->escape((
string) $this->localtax1_type).
"'";
2831 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
2832 $sql .=
", localtax2_type='".$this->db->escape((
string) $this->localtax2_type).
"'";
2833 $sql .=
", fk_product=".($this->fk_product > 0 ? ((int) $this->fk_product) :
"null");
2834 $sql .=
", product_type=".((int) $this->product_type);
2835 $sql .=
", remise_percent=".price2num($this->remise_percent);
2836 $sql .=
", subprice=".price2num($this->subprice);
2837 $sql .=
", info_bits=".price2num($this->info_bits);
2838 $sql .=
", date_start_fill=".(int) $this->date_start_fill;
2839 $sql .=
", date_end_fill=".(int) $this->date_end_fill;
2840 if (empty($this->skip_update_total)) {
2841 $sql .=
", total_ht=".price2num($this->total_ht);
2842 $sql .=
", total_tva=".price2num($this->total_tva);
2843 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
2844 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
2845 $sql .=
", total_ttc=".price2num($this->total_ttc);
2847 $sql .=
", rang=".((int) $this->rang);
2848 $sql .=
", special_code=".((int) $this->special_code);
2849 $sql .=
", fk_unit=".($this->fk_unit ?
"'".$this->db->escape((
string) $this->fk_unit).
"'" :
"null");
2850 $sql .=
", fk_contract_line=".($this->fk_contract_line ? ((int) $this->fk_contract_line) :
"null");
2851 $sql .=
" WHERE rowid = ".((int) $this->
id);
2855 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
2857 $resql = $this->db->query($sql);
2866 if (!$error && !$notrigger) {
2868 $result = $this->call_trigger(
'LINEBILLREC_MODIFY', $user);
2876 $this->db->rollback();
2879 $this->db->commit();
2883 $this->error = $this->db->lasterror();
2884 $this->db->rollback();
$propal type
'integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter[:Sortfield]]]',...
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
isLineOfObject($lineid)
Check that a line belongs to this object, using $this->table_element_line and $this->fk_element.
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.
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.
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, $array_options=array())
Add a line to invoice.
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.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
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...
dol_mimetype($file, $default='application/octet-stream', $mode=0)
Return MIME type of a file from its name with extension.
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.
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...
GETPOSTINT($paramname, $method=0, $nodefault=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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_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)
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, $notused, $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