31 require_once DOL_DOCUMENT_ROOT.
'/core/class/notify.class.php';
32 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
42 const TRIGGER_PREFIX =
'BILLREC';
46 public $element =
'facturerec';
51 public $table_element =
'facture_rec';
56 public $table_element_line =
'facturedet_rec';
61 public $fk_element =
'fk_facture';
66 public $picto =
'bill';
93 public $remise_absolue;
94 public $remise_percent;
108 public $date_last_gen;
123 public $unit_frequency;
126 public $special_code;
128 public $usenewprice = 0;
130 public $date_lim_reglement;
131 public $cond_reglement_code;
132 public $mode_reglement_code;
136 public $auto_validate;
137 public $generate_pdf;
174 public $fields = array(
175 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
176 'titre' =>array(
'type'=>
'varchar(100)',
'label'=>
'Titre',
'enabled'=>1,
'showoncombobox' => 1,
'visible'=>-1,
'position'=>15),
177 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
178 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'isModEnabled("societe")',
'visible'=>-1,
'notnull'=>1,
'position'=>25),
179 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>30),
181 'remise' =>array(
'type'=>
'double',
'label'=>
'Remise',
'enabled'=>1,
'visible'=>-1,
'position'=>40),
184 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Tva',
'enabled'=>1,
'visible'=>-1,
'position'=>55,
'isameasure'=>1),
185 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax1',
'enabled'=>1,
'visible'=>-1,
'position'=>60,
'isameasure'=>1),
186 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax2',
'enabled'=>1,
'visible'=>-1,
'position'=>65,
'isameasure'=>1),
187 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'Total',
'enabled'=>1,
'visible'=>-1,
'position'=>70,
'isameasure'=>1),
188 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'Total ttc',
'enabled'=>1,
'visible'=>-1,
'position'=>75,
'isameasure'=>1),
189 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'Fk user author',
'enabled'=>1,
'visible'=>-1,
'position'=>80),
190 '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),
191 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'Fk cond reglement',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
192 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'Fk mode reglement',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
193 'date_lim_reglement' =>array(
'type'=>
'date',
'label'=>
'Date lim reglement',
'enabled'=>1,
'visible'=>-1,
'position'=>100),
194 'note_private' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>105),
195 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>110),
196 'modelpdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Modelpdf',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
197 'date_when' =>array(
'type'=>
'datetime',
'label'=>
'Date when',
'enabled'=>1,
'visible'=>-1,
'position'=>130),
198 'date_last_gen' =>array(
'type'=>
'datetime',
'label'=>
'Date last gen',
'enabled'=>1,
'visible'=>-1,
'position'=>135),
199 'nb_gen_done' =>array(
'type'=>
'integer',
'label'=>
'Nb gen done',
'enabled'=>1,
'visible'=>-1,
'position'=>140),
200 'nb_gen_max' =>array(
'type'=>
'integer',
'label'=>
'Nb gen max',
'enabled'=>1,
'visible'=>-1,
'position'=>145),
201 'frequency' =>array(
'type'=>
'integer',
'label'=>
'Frequency',
'enabled'=>1,
'visible'=>-1,
'position'=>150),
202 'unit_frequency' =>array(
'type'=>
'varchar(2)',
'label'=>
'UnitFrequency',
'enabled'=>1,
'visible'=>-1,
'position'=>152),
203 'usenewprice' =>array(
'type'=>
'integer',
'label'=>
'UseNewPrice',
'enabled'=>1,
'visible'=>0,
'position'=>155),
204 'revenuestamp' =>array(
'type'=>
'double(24,8)',
'label'=>
'RevenueStamp',
'enabled'=>1,
'visible'=>-1,
'position'=>160,
'isameasure'=>1),
205 'auto_validate' =>array(
'type'=>
'integer',
'label'=>
'Auto validate',
'enabled'=>1,
'visible'=>-1,
'position'=>165),
206 'generate_pdf' =>array(
'type'=>
'integer',
'label'=>
'Generate pdf',
'enabled'=>1,
'visible'=>-1,
'position'=>170),
207 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'Fk account',
'enabled'=>
'isModEnabled("banque")',
'visible'=>-1,
'position'=>175),
208 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
209 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'Multicurrency code',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
210 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency tx',
'enabled'=>1,
'visible'=>-1,
'position'=>190,
'isameasure'=>1),
211 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total ht',
'enabled'=>1,
'visible'=>-1,
'position'=>195,
'isameasure'=>1),
212 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total tva',
'enabled'=>1,
'visible'=>-1,
'position'=>200,
'isameasure'=>1),
213 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total ttc',
'enabled'=>1,
'visible'=>-1,
'position'=>205,
'isameasure'=>1),
214 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>210),
215 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>215),
216 'suspended' =>array(
'type'=>
'integer',
'label'=>
'Suspended',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
220 const STATUS_NOTSUSPENDED = 0;
221 const STATUS_SUSPENDED = 1;
243 public function create($user, $facid, $notrigger = 0)
251 $this->titre = trim(isset($this->titre) ? $this->titre : $this->title);
252 $this->title = trim($this->title);
253 $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
254 if (empty($this->suspended)) {
255 $this->suspended = 0;
259 if (empty($this->frequency)) {
260 $this->frequency = 0;
261 $this->date_when =
null;
264 $this->frequency = abs($this->frequency);
265 $this->nb_gen_done = 0;
266 $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
267 $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
268 $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
273 $facsrc =
new Facture($this->db);
274 $result = $facsrc->fetch($facid);
277 $this->brouillon = 1;
278 $this->fk_soc = $facsrc->socid;
280 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture_rec (";
287 $sql .=
", note_private";
288 $sql .=
", note_public";
289 $sql .=
", modelpdf";
290 $sql .=
", fk_user_author";
291 $sql .=
", fk_projet";
292 $sql .=
", fk_account";
293 $sql .=
", fk_cond_reglement";
294 $sql .=
", fk_mode_reglement";
295 $sql .=
", usenewprice";
296 $sql .=
", frequency";
297 $sql .=
", unit_frequency";
298 $sql .=
", date_when";
299 $sql .=
", date_last_gen";
300 $sql .=
", nb_gen_done";
301 $sql .=
", nb_gen_max";
302 $sql .=
", auto_validate";
303 $sql .=
", generate_pdf";
304 $sql .=
", fk_multicurrency";
305 $sql .=
", multicurrency_code";
306 $sql .=
", multicurrency_tx";
307 $sql .=
", suspended";
308 $sql .=
") VALUES (";
309 $sql .=
"'".$this->db->escape($this->titre ? $this->titre : $this->title).
"'";
310 $sql .=
", ".((int) $this->fk_soc);
311 $sql .=
", ".((int) $conf->entity);
312 $sql .=
", '".$this->db->idate($now).
"'";
313 $sql .=
", ".(!empty($facsrc->total_ttc) ? ((
float) $facsrc->total_ttc) :
'0');
314 $sql .=
", ".(!empty($facsrc->remise_absolue) ? ((
float) $this->remise_absolue) :
'0');
315 $sql .=
", ".(!empty($this->note_private) ? (
"'".$this->db->escape($this->note_private).
"'") :
"NULL");
316 $sql .=
", ".(!empty($this->note_public) ? (
"'".$this->db->escape($this->note_public).
"'") :
"NULL");
317 $sql .=
", ".(!empty($this->model_pdf) ? (
"'".$this->db->escape($this->model_pdf).
"'") :
"NULL");
318 $sql .=
", ".((int) $user->id);
319 $sql .=
", ".(!empty($this->fk_project) ? ((int) $this->fk_project) :
"null");
320 $sql .=
", ".(!empty($facsrc->fk_account) ? ((int) $facsrc->fk_account) :
"null");
321 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
322 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
323 $sql .=
", ".((int) $this->usenewprice);
324 $sql .=
", ".((int) $this->frequency);
325 $sql .=
", '".$this->db->escape($this->unit_frequency).
"'";
326 $sql .=
", ".(!empty($this->date_when) ?
"'".$this->db->idate($this->date_when).
"'" :
'NULL');
327 $sql .=
", ".(!empty($this->date_last_gen) ?
"'".$this->db->idate($this->date_last_gen).
"'" :
'NULL');
328 $sql .=
", ".((int) $this->nb_gen_done);
329 $sql .=
", ".((int) $this->nb_gen_max);
330 $sql .=
", ".((int) $this->auto_validate);
331 $sql .=
", ".((int) $this->generate_pdf);
332 $sql .=
", ".((int) $facsrc->fk_multicurrency);
333 $sql .=
", '".$this->db->escape($facsrc->multicurrency_code).
"'";
334 $sql .=
", ".((float) $facsrc->multicurrency_tx);
335 $sql .=
", ".((int) $this->suspended);
338 if ($this->db->query(
$sql)) {
339 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
"facture_rec");
342 $this->fk_multicurrency = $facsrc->fk_multicurrency;
343 $this->multicurrency_code = $facsrc->multicurrency_code;
344 $this->multicurrency_tx = $facsrc->multicurrency_tx;
347 $num = count($facsrc->lines);
348 for ($i = 0; $i < $num; $i++) {
349 $tva_tx = $facsrc->lines[$i]->tva_tx;
350 if (!empty($facsrc->lines[$i]->vat_src_code) && !preg_match(
'/\(/', $tva_tx)) {
351 $tva_tx .=
' ('.$facsrc->lines[$i]->vat_src_code.
')';
354 $default_start_fill =
getDolGlobalInt(
'INVOICEREC_SET_AUTOFILL_DATE_START');
355 $default_end_fill =
getDolGlobalInt(
'INVOICEREC_SET_AUTOFILL_DATE_END');
357 $result_insert = $this->
addline(
358 $facsrc->lines[$i]->desc,
359 $facsrc->lines[$i]->subprice,
360 $facsrc->lines[$i]->qty,
362 $facsrc->lines[$i]->localtax1_tx,
363 $facsrc->lines[$i]->localtax2_tx,
364 $facsrc->lines[$i]->fk_product,
365 $facsrc->lines[$i]->remise_percent,
367 $facsrc->lines[$i]->info_bits,
370 $facsrc->lines[$i]->product_type,
371 $facsrc->lines[$i]->rang,
372 $facsrc->lines[$i]->special_code,
373 $facsrc->lines[$i]->label,
374 $facsrc->lines[$i]->fk_unit,
375 $facsrc->lines[$i]->multicurrency_subprice,
379 $facsrc->lines[$i]->pa_ht
382 if ($result_insert < 0) {
387 $result2 = $objectline->fetch($result_insert);
390 if (method_exists($facsrc->lines[$i],
'fetch_optionals')) {
391 $facsrc->lines[$i]->fetch_optionals($facsrc->lines[$i]->id);
392 $objectline->array_options = $facsrc->lines[$i]->array_options;
395 $result = $objectline->insertExtraFields();
399 } elseif ($result2 < 0) {
400 $this->errors[] = $objectline->error;
406 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
407 $this->linked_objects = $this->linkedObjectsIds;
411 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
412 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
413 if (is_array($tmp_origin_id)) {
414 foreach ($tmp_origin_id as $origin_id) {
417 $this->error = $this->db->lasterror();
423 $origin_id = $tmp_origin_id;
426 $this->error = $this->db->lasterror();
440 if (!$error && !$notrigger) {
444 $this->db->rollback();
451 $this->db->rollback();
458 $this->error = $this->db->lasterror();
459 $this->db->rollback();
463 $this->db->rollback();
480 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_rec SET";
481 $sql .=
" entity = ".((int) $this->entity).
",";
482 $sql .=
" titre = '".$this->db->escape($this->title).
"',";
483 $sql .=
" suspended = ".((int) $this->suspended).
",";
484 $sql .=
" fk_soc = ".((int) $this->socid).
",";
485 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
486 $sql .=
" localtax1 = ".((float) $this->total_localtax1).
",";
487 $sql .=
" localtax2 = ".((float) $this->total_localtax2).
",";
488 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
489 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
490 $sql .=
" remise_percent = ".((float) $this->remise_percent);
492 $sql .=
" WHERE rowid = ".((int) $this->
id);
496 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
498 $resql = $this->db->query(
$sql);
507 if (!$error && !$notrigger) {
511 $this->db->rollback();
519 $this->error = $this->db->lasterror();
520 $this->db->rollback();
533 public function fetch($rowid, $ref =
'', $ref_ext =
'')
537 $sql =
'SELECT f.rowid, f.entity, f.titre as title, f.suspended, f.fk_soc, f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc';
538 $sql .=
', f.remise_percent, f.remise_absolue, f.remise';
539 $sql .=
', f.date_lim_reglement as dlr';
540 $sql .=
', f.note_private, f.note_public, f.fk_user_author';
541 $sql .=
', f.modelpdf as model_pdf';
542 $sql .=
', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project';
543 $sql .=
', f.fk_account';
544 $sql .=
', f.frequency, f.unit_frequency, f.date_when, f.date_last_gen, f.nb_gen_done, f.nb_gen_max, f.usenewprice, f.auto_validate';
545 $sql .=
', f.generate_pdf';
546 $sql .=
", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
547 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
548 $sql .=
', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
550 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_rec as f';
551 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
552 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON f.fk_mode_reglement = p.id';
554 $sql .=
' WHERE f.entity IN ('.getEntity(
'invoice').
')';
556 $sql .=
' AND f.rowid = '.((int) $rowid);
558 $sql .=
" AND f.titre = '".$this->db->escape($ref).
"'";
560 $sql .=
' AND f.rowid = 0';
566 $result = $this->db->query(
$sql);
568 if ($this->db->num_rows($result)) {
569 $obj = $this->db->fetch_object($result);
571 $this->
id = $obj->rowid;
572 $this->entity = $obj->entity;
573 $this->titre = $obj->title;
574 $this->title = $obj->title;
575 $this->
ref = $obj->title;
576 $this->suspended = $obj->suspended;
577 $this->remise_percent = $obj->remise_percent;
578 $this->remise_absolue = $obj->remise_absolue;
579 $this->remise = $obj->remise;
580 $this->total_ht = $obj->total_ht;
581 $this->total_tva = $obj->total_tva;
582 $this->total_localtax1 = $obj->localtax1;
583 $this->total_localtax2 = $obj->localtax2;
584 $this->total_ttc = $obj->total_ttc;
585 $this->socid = $obj->fk_soc;
586 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
587 $this->mode_reglement_id = $obj->fk_mode_reglement;
588 $this->mode_reglement_code = $obj->mode_reglement_code;
589 $this->mode_reglement = $obj->mode_reglement_libelle;
590 $this->cond_reglement_id = $obj->fk_cond_reglement;
591 $this->cond_reglement_code = $obj->cond_reglement_code;
592 $this->cond_reglement = $obj->cond_reglement_libelle;
593 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
594 $this->fk_project = $obj->fk_project;
595 $this->fk_account = $obj->fk_account;
596 $this->note_private = $obj->note_private;
597 $this->note_public = $obj->note_public;
598 $this->user_author = $obj->fk_user_author;
599 $this->modelpdf = $obj->model_pdf;
600 $this->model_pdf = $obj->model_pdf;
602 $this->frequency = $obj->frequency;
603 $this->unit_frequency = $obj->unit_frequency;
604 $this->date_when = $this->db->jdate($obj->date_when);
605 $this->date_last_gen = $this->db->jdate($obj->date_last_gen);
606 $this->nb_gen_done = $obj->nb_gen_done;
607 $this->nb_gen_max = $obj->nb_gen_max;
608 $this->usenewprice = $obj->usenewprice;
609 $this->auto_validate = $obj->auto_validate;
610 $this->generate_pdf = $obj->generate_pdf;
613 $this->fk_multicurrency = $obj->fk_multicurrency;
614 $this->multicurrency_code = $obj->multicurrency_code;
615 $this->multicurrency_tx = $obj->multicurrency_tx;
616 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
617 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
618 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
620 if ($this->statut == self::STATUS_DRAFT) {
621 $this->brouillon = 1;
633 $this->error = $this->db->lasterror();
638 $this->error =
'Bill with id '.$rowid.
' or ref '.$ref.
' not found';
639 dol_syslog(
'Facture::Fetch Error '.$this->error, LOG_ERR);
643 $this->error = $this->db->error();
670 $this->lines = array();
681 dol_syslog(
'FactureRec::fetch_lines', LOG_DEBUG);
683 $sql =
'SELECT l.rowid, l.fk_product, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx, ';
684 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
685 $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,';
686 $sql .=
' l.rang, l.special_code,';
687 $sql .=
' l.fk_unit, l.fk_contract_line,';
688 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
689 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
690 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet_rec as l';
691 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
692 $sql .=
' WHERE l.fk_facture = '.((int) $this->
id);
693 $sql .=
' ORDER BY l.rang';
695 $result = $this->db->query(
$sql);
697 $num = $this->db->num_rows($result);
700 $objp = $this->db->fetch_object($result);
703 $line->id = $objp->rowid;
704 $line->rowid = $objp->rowid;
705 $line->desc = $objp->description;
706 $line->description = $objp->description;
707 $line->ref = $objp->product_ref;
708 $line->product_ref = $objp->product_ref;
709 $line->libelle = $objp->product_label;
710 $line->product_label = $objp->product_label;
711 $line->product_desc = $objp->product_desc;
712 $line->product_type = $objp->product_type;
713 $line->fk_product_type = $objp->fk_product_type;
714 $line->qty = $objp->qty;
715 $line->subprice = $objp->subprice;
717 $line->label = $objp->custom_label;
719 $line->vat_src_code = $objp->vat_src_code;
720 $line->tva_tx = $objp->tva_tx;
721 $line->localtax1_tx = $objp->localtax1_tx;
722 $line->localtax2_tx = $objp->localtax2_tx;
723 $line->localtax1_type = $objp->localtax1_type;
724 $line->localtax2_type = $objp->localtax2_type;
725 $line->remise_percent = $objp->remise_percent;
727 $line->fk_product = $objp->fk_product;
728 $line->date_start_fill = $objp->date_start_fill;
729 $line->date_end_fill = $objp->date_end_fill;
730 $line->info_bits = $objp->info_bits;
731 $line->total_ht = $objp->total_ht;
732 $line->total_tva = $objp->total_tva;
733 $line->total_ttc = $objp->total_ttc;
737 $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
738 $line->fk_fournprice = $objp->fk_product_fournisseur_price;
740 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
742 $line->buyprice = $marginInfos[0];
743 $line->pa_ht = $marginInfos[0];
744 $line->marge_tx = $marginInfos[1];
745 $line->marque_tx = $marginInfos[2];
746 $line->rang = $objp->rang;
747 $line->special_code = $objp->special_code;
748 $line->fk_unit = $objp->fk_unit;
749 $line->fk_contract_line = $objp->fk_contract_line;
752 $line->price = $objp->price;
753 $line->remise = $objp->remise;
755 $line->fetch_optionals();
758 $line->fk_multicurrency = $objp->fk_multicurrency;
759 $line->multicurrency_code = $objp->multicurrency_code;
760 $line->multicurrency_subprice = $objp->multicurrency_subprice;
761 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
762 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
763 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
765 $this->lines[$i] = $line;
770 $this->db->free($result);
773 $this->error = $this->db->lasterror();
787 public function delete(
User $user, $notrigger = 0, $idwarehouse = -1)
791 dol_syslog(get_class($this).
"::delete rowid=".((
int) $rowid), LOG_DEBUG);
796 $main = MAIN_DB_PREFIX.
'facturedet_rec';
797 $ef = $main.
"_extrafields";
799 $sqlef =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_facture = ".((int) $rowid).
")";
800 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facturedet_rec WHERE fk_facture = ".((int) $rowid);
802 if ($this->db->query($sqlef) && $this->db->query(
$sql)) {
803 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facture_rec WHERE rowid = ".((int) $rowid);
805 if ($this->db->query(
$sql)) {
817 $this->error = $this->db->lasterror();
821 $this->error = $this->db->lasterror();
824 if (!$error && !$notrigger) {
836 $this->db->rollback();
869 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 =
'', $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)
875 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);
876 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
888 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
889 $vat_src_code = $reg[1];
890 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
893 if ($this->brouillon) {
895 $remise_percent =
price2num($remise_percent);
896 if (empty($remise_percent)) {
902 if (!preg_match(
'/\((.*)\)/', $txtva)) {
910 if (empty($txlocaltax1)) {
913 if (empty($txlocaltax2)) {
916 if (empty($info_bits)) {
920 if ($price_base_type ==
'HT') {
931 $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);
932 $total_ht = $tabprice[0];
933 $total_tva = $tabprice[1];
934 $total_ttc = $tabprice[2];
935 $total_localtax1 = $tabprice[9];
936 $total_localtax2 = $tabprice[10];
937 $pu_ht = $tabprice[3];
940 $multicurrency_total_ht = $tabprice[16];
941 $multicurrency_total_tva = $tabprice[17];
942 $multicurrency_total_ttc = $tabprice[18];
943 $pu_ht_devise = $tabprice[19];
945 $product_type = $type;
947 $product =
new Product($this->db);
948 $result = $product->fetch($fk_product);
949 $product_type = $product->type;
954 if ($ranktouse == -1) {
956 $ranktouse = $rangmax + 1;
959 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facturedet_rec (";
960 $sql .=
"fk_facture";
962 $sql .=
", description";
966 $sql .=
", vat_src_code";
967 $sql .=
", localtax1_tx";
968 $sql .=
", localtax1_type";
969 $sql .=
", localtax2_tx";
970 $sql .=
", localtax2_type";
971 $sql .=
", fk_product";
972 $sql .=
", product_type";
973 $sql .=
", remise_percent";
974 $sql .=
", subprice";
976 $sql .=
", total_ht";
977 $sql .=
", total_tva";
978 $sql .=
", total_localtax1";
979 $sql .=
", total_localtax2";
980 $sql .=
", total_ttc";
981 $sql .=
", date_start_fill";
982 $sql .=
", date_end_fill";
983 $sql .=
", fk_product_fournisseur_price";
984 $sql .=
", buy_price_ht";
985 $sql .=
", info_bits";
987 $sql .=
", special_code";
989 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
990 $sql .=
") VALUES (";
991 $sql .=
" ".((int) $facid);
992 $sql .=
", ".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
993 $sql .=
", '".$this->db->escape($desc).
"'";
994 $sql .=
", ".price2num($pu_ht);
995 $sql .=
", ".price2num($qty);
996 $sql .=
", ".price2num($txtva);
997 $sql .=
", '".$this->db->escape($vat_src_code).
"'";
998 $sql .=
", ".price2num($txlocaltax1);
999 $sql .=
", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] :
'').
"'";
1000 $sql .=
", ".price2num($txlocaltax2);
1001 $sql .=
", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] :
'').
"'";
1002 $sql .=
", ".(!empty($fk_product) ?
"'".$this->db->escape($fk_product).
"'" :
"null");
1003 $sql .=
", ".((int) $product_type);
1004 $sql .=
", ".price2num($remise_percent);
1005 $sql .=
", ".price2num($pu_ht);
1007 $sql .=
", ".price2num($total_ht);
1008 $sql .=
", ".price2num($total_tva);
1009 $sql .=
", ".price2num($total_localtax1);
1010 $sql .=
", ".price2num($total_localtax2);
1011 $sql .=
", ".price2num($total_ttc);
1012 $sql .=
", ".(int) $date_start_fill;
1013 $sql .=
", ".(int) $date_end_fill;
1014 $sql .=
", ".($fk_fournprice > 0 ? $fk_fournprice :
'null');
1016 $sql .=
", ".((int) $info_bits);
1017 $sql .=
", ".((int) $ranktouse);
1018 $sql .=
", ".((int) $special_code);
1019 $sql .=
", ".($fk_unit ? ((int) $fk_unit) :
"null");
1020 $sql .=
", ".(int) $this->fk_multicurrency;
1021 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1022 $sql .=
", ".price2num($pu_ht_devise,
'CU');
1023 $sql .=
", ".price2num($multicurrency_total_ht,
'CT');
1024 $sql .=
", ".price2num($multicurrency_total_tva,
'CT');
1025 $sql .=
", ".price2num($multicurrency_total_ttc,
'CT');
1028 dol_syslog(get_class($this).
"::addline", LOG_DEBUG);
1029 if ($this->db->query(
$sql)) {
1030 $lineId = $this->db->last_insert_id(MAIN_DB_PREFIX.
"facturedet_rec");
1035 $this->error = $this->db->lasterror();
1039 $this->error =
'Recurring Invoice is suspended. adding lines not allowed.';
1074 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 =
'', $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)
1080 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);
1081 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1084 if (empty($remise_percent)) {
1085 $remise_percent = 0;
1093 if ($this->brouillon) {
1095 $remise_percent =
price2num($remise_percent);
1097 if (empty($info_bits)) {
1102 $pu_ht_devise =
price2num($pu_ht_devise);
1103 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1108 if (empty($txlocaltax1)) {
1111 if (empty($txlocaltax2)) {
1115 if (empty($this->multicurrency_subprice)) {
1116 $this->multicurrency_subprice = 0;
1118 if (empty($this->multicurrency_total_ht)) {
1119 $this->multicurrency_total_ht = 0;
1121 if (empty($this->multicurrency_total_tva)) {
1122 $this->multicurrency_total_tva = 0;
1124 if (empty($this->multicurrency_total_ttc)) {
1125 $this->multicurrency_total_ttc = 0;
1128 if ($price_base_type ==
'HT') {
1143 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1144 $vat_src_code = $reg[1];
1145 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1148 $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);
1150 $total_ht = $tabprice[0];
1151 $total_tva = $tabprice[1];
1152 $total_ttc = $tabprice[2];
1153 $total_localtax1 = $tabprice[9];
1154 $total_localtax2 = $tabprice[10];
1155 $pu_ht = $tabprice[3];
1156 $pu_tva = $tabprice[4];
1157 $pu_ttc = $tabprice[5];
1160 $multicurrency_total_ht = $tabprice[16];
1161 $multicurrency_total_tva = $tabprice[17];
1162 $multicurrency_total_ttc = $tabprice[18];
1163 $pu_ht_devise = $tabprice[19];
1165 $product_type = $type;
1167 $product =
new Product($this->db);
1168 $result = $product->fetch($fk_product);
1169 $product_type = $product->type;
1172 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet_rec SET ";
1173 $sql .=
"fk_facture = ".((int) $facid);
1174 $sql .=
", label=".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
1175 $sql .=
", description='".$this->db->escape($desc).
"'";
1176 $sql .=
", price=".price2num($pu_ht);
1177 $sql .=
", qty=".price2num($qty);
1178 $sql .=
", tva_tx=".price2num($txtva);
1179 $sql .=
", vat_src_code='".$this->db->escape($vat_src_code).
"'";
1180 $sql .=
", localtax1_tx=".((float) $txlocaltax1);
1181 $sql .=
", localtax1_type='".$this->db->escape($localtaxes_type[0]).
"'";
1182 $sql .=
", localtax2_tx=".((float) $txlocaltax2);
1183 $sql .=
", localtax2_type='".$this->db->escape($localtaxes_type[2]).
"'";
1184 $sql .=
", fk_product=".(!empty($fk_product) ?
"'".$this->db->escape($fk_product).
"'" :
"null");
1185 $sql .=
", product_type=".((int) $product_type);
1186 $sql .=
", remise_percent='".price2num($remise_percent).
"'";
1187 $sql .=
", subprice='".price2num($pu_ht).
"'";
1188 $sql .=
", total_ht='".price2num($total_ht).
"'";
1189 $sql .=
", total_tva='".price2num($total_tva).
"'";
1190 $sql .=
", total_localtax1='".price2num($total_localtax1).
"'";
1191 $sql .=
", total_localtax2='".price2num($total_localtax2).
"'";
1192 $sql .=
", total_ttc='".price2num($total_ttc).
"'";
1193 $sql .=
", date_start_fill=".((int) $date_start_fill);
1194 $sql .=
", date_end_fill=".((int) $date_end_fill);
1195 $sql .=
", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice :
'null');
1197 $sql .=
", info_bits=".((int) $info_bits);
1198 $sql .=
", rang=".((int) $rang);
1199 $sql .=
", special_code=".((int) $special_code);
1200 $sql .=
", fk_unit=".($fk_unit ?
"'".$this->db->escape($fk_unit).
"'" :
"null");
1201 $sql .=
', multicurrency_subprice = '.price2num($pu_ht_devise);
1202 $sql .=
', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1203 $sql .=
', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1204 $sql .=
', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1205 $sql .=
" WHERE rowid = ".((int) $rowid);
1207 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1208 if ($this->db->query(
$sql)) {
1213 $this->error = $this->db->lasterror();
1228 if (empty($this->date_when)) {
1242 if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1257 return ($this->
isMaxNbGenReached() ?
'<strike>' :
'').$ret.($this->isMaxNbGenReached() ?
'</strike>' :
'');
1273 global $conf, $langs, $db, $user, $hookmanager;
1279 $langs->loadLangs(array(
"main",
"bills"));
1283 $today =
dol_mktime(23, 59, 59, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
1285 $this->output =
null;
1287 dol_syslog(
"createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid.
" forcevalidation=".$forcevalidation);
1289 $sql =
'SELECT rowid FROM '.MAIN_DB_PREFIX.
'facture_rec';
1290 $sql .=
' WHERE frequency > 0';
1291 $sql .=
" AND (date_when IS NULL OR date_when <= '".$this->db->idate($today).
"')";
1292 $sql .=
' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1293 $sql .=
' AND suspended = 0';
1294 $sql .=
' AND entity = '.$conf->entity;
1295 if ($restrictioninvoiceid > 0) {
1296 $sql .=
' AND rowid = '.((int) $restrictioninvoiceid);
1298 $sql .= $this->db->order(
'entity',
'ASC');
1300 $parameters = array(
1301 'restrictioninvoiceid' => $restrictioninvoiceid,
1302 'forcevalidation' => $forcevalidation,
1304 $reshook = $hookmanager->executeHooks(
'beforeCreationOfRecurringInvoices', $parameters,
$sql);
1306 $resql = $this->db->query(
$sql);
1309 $num = $this->db->num_rows($resql);
1312 $this->output .= $langs->trans(
"FoundXQualifiedRecurringInvoiceTemplate", $num).
"\n";
1314 $this->output .= $langs->trans(
"NoQualifiedRecurringInvoiceTemplateFound");
1317 $saventity = $conf->entity;
1320 $line = $this->db->fetch_object($resql);
1324 $invoiceidgenerated = 0;
1328 $facturerec->fetch($line->rowid);
1330 if ($facturerec->id > 0) {
1332 $conf->entity = $facturerec->entity;
1334 dol_syslog(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref.
", entity=".$facturerec->entity);
1336 $facture =
new Facture($this->db);
1337 $facture->fac_rec = $facturerec->id;
1338 $facture->fk_fac_rec_source = $facturerec->id;
1341 $facture->brouillon = 1;
1344 $facture->date = (empty($facturerec->date_when) ? $now : $facturerec->date_when);
1345 $facture->socid = $facturerec->socid;
1346 if (!empty($facturerec->fk_multicurrency)) {
1347 $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1348 $facture->multicurrency_code = $facturerec->multicurrency_code;
1349 $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1352 $invoiceidgenerated = $facture->create($user);
1353 if ($invoiceidgenerated <= 0) {
1354 $this->errors = $facture->errors;
1355 $this->error = $facture->error;
1360 if (!$error && ($facturerec->auto_validate || $forcevalidation)) {
1361 $result = $facture->validate($user);
1363 $this->errors = $facture->errors;
1364 $this->error = $facture->error;
1368 if (!$error && $facturerec->generate_pdf) {
1370 $facture->fetch($facture->id);
1371 $result = $facture->generateDocument($facturerec->model_pdf, $langs);
1373 $this->errors = $facture->errors;
1374 $this->error = $facture->error;
1378 if (!$error && !$notrigger) {
1380 $result = $facturerec->call_trigger(
'BILLREC_CREATEBILL', $user);
1382 $this->errors = $facturerec->errors;
1383 $this->error = $facturerec->error;
1390 $this->error =
"Failed to load invoice template with id=".$line->rowid.
", entity=".$conf->entity.
"\n";
1391 $this->errors[] =
"Failed to load invoice template with id=".$line->rowid.
", entity=".$conf->entity;
1392 dol_syslog(
"createRecurringInvoices Failed to load invoice template with id=".$line->rowid.
", entity=".$conf->entity);
1395 if (!$error && $invoiceidgenerated >= 0) {
1396 $this->db->commit(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref);
1397 dol_syslog(
"createRecurringInvoices Process invoice template ".$facturerec->ref.
" is finished with a success generation");
1399 $this->output .= $langs->trans(
"InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref).
"\n";
1401 $this->db->rollback(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref);
1404 $parameters = array(
1407 'errorCount' => $error,
1408 'invoiceidgenerated' => $invoiceidgenerated,
1409 'facturerec' => $facturerec,
1412 $reshook = $hookmanager->executeHooks(
'afterCreationOfRecurringInvoice', $parameters, $facture);
1417 $conf->entity = $saventity;
1422 $this->output = trim($this->output);
1424 return $error ? $error : 0;
1439 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip =
'', $save_lastsearch_value = -1)
1441 global $langs, $hookmanager;
1445 $label =
'<u>'.$langs->trans(
"RepeatableInvoice").
'</u>';
1446 if (!empty($this->
ref)) {
1447 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1449 if ($this->frequency > 0) {
1450 $label .=
'<br><b>'.$langs->trans(
'Frequency').
':</b> '.$langs->trans(
'FrequencyPer_'.$this->unit_frequency, $this->frequency);
1452 if (!empty($this->date_last_gen)) {
1453 $label .=
'<br><b>'.$langs->trans(
'DateLastGeneration').
':</b> '.
dol_print_date($this->date_last_gen,
'dayhour');
1455 if ($this->frequency > 0) {
1456 if (!empty($this->date_when)) {
1457 $label .=
'<br><b>'.$langs->trans(
'NextDateToExecution').
':</b> ';
1458 $label .= (empty($this->suspended) ?
'' :
'<strike>').
dol_print_date($this->date_when,
'day').(empty($this->suspended) ?
'' :
'</strike>');
1459 if (!empty($this->suspended)) {
1460 $label .=
' ('.$langs->trans(
"Disabled").
')';
1465 $url = DOL_URL_ROOT.
'/compta/facture/card-rec.php?facid='.$this->id;
1471 if ($option !=
'nolink') {
1473 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1474 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1475 $add_save_lastsearch_values = 1;
1477 if ($add_save_lastsearch_values) {
1478 $url .=
'&save_lastsearch_values=1';
1482 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
1485 $result .= $linkstart;
1487 $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);
1489 if ($withpicto != 2) {
1490 $result .= $this->ref;
1492 $result .= $linkend;
1494 $hookmanager->initHooks(array($this->element .
'dao'));
1495 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1496 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1498 $result = $hookmanager->resPrint;
1500 $result .= $hookmanager->resPrint;
1514 return $this->
LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->
type) ? 0 : $this->
type);
1528 public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0)
1532 $langs->load(
'bills');
1534 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1535 $statusType =
'status0';
1540 if ($status == self::STATUS_SUSPENDED) {
1541 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1543 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1546 if ($status == self::STATUS_SUSPENDED) {
1547 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1549 $labelStatus = $langs->transnoentitiesnoconv(
"Draft");
1552 } elseif ($mode == 1) {
1555 if ($status == self::STATUS_SUSPENDED) {
1556 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1558 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1561 if ($status == self::STATUS_SUSPENDED) {
1562 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1564 $labelStatus = $langs->transnoentitiesnoconv(
"Draft");
1567 } elseif ($mode == 2) {
1569 if ($status == self::STATUS_SUSPENDED) {
1570 $statusType =
'status6';
1571 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1573 $statusType =
'status4';
1574 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1577 if ($status == self::STATUS_SUSPENDED) {
1578 $statusType =
'status6';
1579 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1581 $statusType =
'status0';
1582 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1585 } elseif ($mode == 3) {
1588 if ($status == self::STATUS_SUSPENDED) {
1589 $statusType =
'status6';
1590 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1592 $statusType =
'status4';
1593 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1596 if ($status == self::STATUS_SUSPENDED) {
1597 $statusType =
'status6';
1598 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1600 $statusType =
'status0';
1601 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1604 } elseif ($mode == 4) {
1607 if ($status == self::STATUS_SUSPENDED) {
1608 $statusType =
'status6';
1609 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1611 $statusType =
'status4';
1612 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1615 if ($status == self::STATUS_SUSPENDED) {
1616 $statusType =
'status6';
1617 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1619 $statusType =
'status0';
1620 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1623 } elseif ($mode == 5 || $mode == 6) {
1629 if ($status == self::STATUS_SUSPENDED) {
1630 $statusType =
'status6';
1631 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1633 $statusType =
'status4';
1634 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1637 if ($status == self::STATUS_SUSPENDED) {
1638 $statusType =
'status6';
1639 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1641 $statusType =
'status0';
1642 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1647 $labelStatusShort = $labelStatus;
1649 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
1662 global $user, $langs, $conf;
1666 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
1672 $sql =
"SELECT rowid";
1673 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
1674 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
1675 $sql .= $this->db->plimit(100);
1677 $resql = $this->db->query(
$sql);
1679 $num_prods = $this->db->num_rows($resql);
1681 while ($i < $num_prods) {
1683 $row = $this->db->fetch_row($resql);
1684 $prodids[$i] = $row[0];
1690 $this->
ref =
'SPECIMEN';
1691 $this->title =
'SPECIMEN';
1692 $this->specimen = 1;
1694 $this->date = $nownotime;
1695 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
1696 $this->cond_reglement_id = 1;
1697 $this->cond_reglement_code =
'RECEP';
1699 $this->mode_reglement_id = 0;
1700 $this->mode_reglement_code =
'';
1701 $this->note_public =
'This is a comment (public)';
1702 $this->note_private =
'This is a comment (private)';
1703 $this->note =
'This is a comment (private)';
1704 $this->fk_incoterms = 0;
1705 $this->location_incoterms =
'';
1707 if (empty($option) || $option !=
'nolines') {
1711 while ($xnbp < $nbp) {
1713 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
1715 $line->subprice = 100;
1716 $line->tva_tx = 19.6;
1717 $line->localtax1_tx = 0;
1718 $line->localtax2_tx = 0;
1719 $line->remise_percent = 0;
1721 $prodid = mt_rand(1, $num_prods);
1722 $line->fk_product = $prodids[$prodid];
1724 $line->total_ht = -100;
1725 $line->total_ttc = -119.6;
1726 $line->total_tva = -19.6;
1727 } elseif ($xnbp == 2) {
1728 $line->subprice = -100;
1729 $line->total_ht = -100;
1730 $line->total_ttc = -119.6;
1731 $line->total_tva = -19.6;
1732 $line->remise_percent = 0;
1733 } elseif ($xnbp == 3) {
1734 $prodid = mt_rand(1, $num_prods);
1735 $line->fk_product = $prodids[$prodid];
1736 $line->total_ht = 50;
1737 $line->total_ttc = 59.8;
1738 $line->total_tva = 9.8;
1739 $line->remise_percent = 50;
1742 $prodid = mt_rand(1, $num_prods);
1743 $line->fk_product = $prodids[$prodid];
1744 $line->total_ht = 100;
1745 $line->total_ttc = 119.6;
1746 $line->total_tva = 19.6;
1747 $line->remise_percent = 00;
1750 $this->lines[$xnbp] = $line;
1753 $this->total_ht += $line->total_ht;
1754 $this->total_tva += $line->total_tva;
1755 $this->total_ttc += $line->total_ttc;
1757 $this->revenuestamp = 0;
1761 $line->desc = $langs->trans(
"Description").
" (offered line)";
1763 $line->subprice = 100;
1764 $line->tva_tx = 19.6;
1765 $line->localtax1_tx = 0;
1766 $line->localtax2_tx = 0;
1767 $line->remise_percent = 100;
1768 $line->total_ht = 0;
1769 $line->total_ttc = 0;
1770 $line->total_tva = 0;
1771 $prodid = mt_rand(1, $num_prods);
1772 $line->fk_product = $prodids[$prodid];
1774 $this->lines[$xnbp] = $line;
1778 $this->usenewprice = 0;
1827 if (!$this->table_element) {
1828 dol_syslog(get_class($this).
"::setFrequencyAndUnit was called on objet with property table_element not defined", LOG_ERR);
1832 if (!empty($frequency) && empty($unit)) {
1833 dol_syslog(get_class($this).
"::setFrequencyAndUnit was called on objet with params frequency defined but unit not defined", LOG_ERR);
1837 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1838 $sql .=
' SET frequency = '.($frequency ? $this->db->escape($frequency) :
'null');
1839 if (!empty($unit)) {
1840 $sql .=
', unit_frequency = \''.$this->db->escape($unit).
'\'';
1842 $sql .=
" WHERE rowid = ".((int) $this->
id);
1844 dol_syslog(get_class($this).
"::setFrequencyAndUnit", LOG_DEBUG);
1845 if ($this->db->query(
$sql)) {
1846 $this->frequency = $frequency;
1847 if (!empty($unit)) {
1848 $this->unit_frequency = $unit;
1853 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
1875 public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
1880 if (!$this->table_element) {
1881 dol_syslog(get_class($this).
"::setNextDate was called on objet with property table_element not defined", LOG_ERR);
1884 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1885 $sql .=
" SET date_when = ".($date ?
"'".$this->db->idate($date).
"'" :
"null");
1886 if ($increment_nb_gen_done > 0) {
1887 $sql .=
', nb_gen_done = nb_gen_done + 1';
1889 $sql .=
" WHERE rowid = ".((int) $this->
id);
1891 dol_syslog(get_class($this).
"::setNextDate", LOG_DEBUG);
1892 if ($this->db->query(
$sql)) {
1893 $this->date_when = $date;
1894 if ($increment_nb_gen_done > 0) {
1895 $this->nb_gen_done++;
1900 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
1925 if (!$this->table_element) {
1926 dol_syslog(get_class($this).
"::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR);
1934 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1935 $sql .=
' SET nb_gen_max = '.((int) $nb);
1936 $sql .=
" WHERE rowid = ".((int) $this->
id);
1938 dol_syslog(get_class($this).
"::setMaxPeriod", LOG_DEBUG);
1939 if ($this->db->query(
$sql)) {
1940 $this->nb_gen_max = $nb;
1944 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
1969 if (!$this->table_element) {
1970 dol_syslog(get_class($this).
"::setAutoValidate was called on objet with property table_element not defined", LOG_ERR);
1974 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1975 $sql .=
' SET auto_validate = '.((int) $validate);
1976 $sql .=
" WHERE rowid = ".((int) $this->
id);
1978 dol_syslog(get_class($this).
"::setAutoValidate", LOG_DEBUG);
1979 if ($this->db->query(
$sql)) {
1980 $this->auto_validate = $validate;
1984 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
2009 if (!$this->table_element) {
2010 dol_syslog(get_class($this).
"::setGeneratePdf was called on objet with property table_element not defined", LOG_ERR);
2014 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2015 $sql .=
' SET generate_pdf = '.((int) $validate);
2016 $sql .=
" WHERE rowid = ".((int) $this->
id);
2018 dol_syslog(get_class($this).
"::setGeneratePdf", LOG_DEBUG);
2019 if ($this->db->query(
$sql)) {
2020 $this->generate_pdf = $validate;
2024 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
2048 if (!$this->table_element) {
2049 dol_syslog(get_class($this).
"::setModelPdf was called on objet with property table_element not defined", LOG_ERR);
2053 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2054 $sql .=
" SET modelpdf = '".$this->db->escape($model).
"'";
2055 $sql .=
" WHERE rowid = ".((int) $this->
id);
2057 dol_syslog(get_class($this).
"::setModelPdf", LOG_DEBUG);
2058 if ($this->db->query(
$sql)) {
2059 $this->model_pdf = $model;
2063 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
2089 public $element =
'facturedetrec';
2094 public $table_element =
'facturedet_rec';
2097 public $fk_product_fournisseur_price;
2098 public $fk_fournprice;
2103 public $description;
2105 public $fk_product_type;
2107 public $fk_contract_line;
2117 public function delete(
User $user, $notrigger =
false)
2126 $result = $this->
call_trigger(
'LINEBILLREC_DELETE', $user);
2142 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
' WHERE rowid='.((int) $this->
id);
2144 $res = $this->db->query(
$sql);
2147 $this->errors[] = $this->db->lasterror();
2153 $this->db->rollback();
2156 $this->db->commit();
2170 $sql =
'SELECT l.rowid, l.fk_facture ,l.fk_product, l.product_type, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx,';
2171 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2172 $sql .=
' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2173 $sql .=
' l.rang, l.special_code,';
2174 $sql .=
' l.fk_unit, l.fk_contract_line,';
2175 $sql .=
' l.import_key, l.fk_multicurrency,';
2176 $sql .=
' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2177 $sql .=
' l.buy_price_ht, l.fk_product_fournisseur_price,';
2178 $sql .=
' l.fk_user_author, l.fk_user_modif,';
2179 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2180 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet_rec as l';
2181 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
2182 $sql .=
' WHERE l.rowid = '.((int) $rowid);
2183 $sql .=
' ORDER BY l.rang';
2186 $result = $this->db->query(
$sql);
2188 $objp = $this->db->fetch_object($result);
2190 $this->
id = $objp->rowid;
2191 $this->label = $objp->custom_label;
2192 $this->desc = $objp->description;
2194 $this->product_type = $objp->product_type;
2195 $this->
ref = $objp->product_ref;
2196 $this->product_ref = $objp->product_ref;
2197 $this->libelle = $objp->product_label;
2198 $this->product_label = $objp->product_label;
2199 $this->product_desc = $objp->product_desc;
2200 $this->fk_product_type = $objp->fk_product_type;
2201 $this->qty = $objp->qty;
2202 $this->
price = $objp->price;
2203 $this->subprice = $objp->subprice;
2204 $this->fk_facture = $objp->fk_facture;
2205 $this->vat_src_code = $objp->vat_src_code;
2206 $this->tva_tx = $objp->tva_tx;
2207 $this->localtax1_tx = $objp->localtax1_tx;
2208 $this->localtax2_tx = $objp->localtax2_tx;
2209 $this->localtax1_type = $objp->localtax1_type;
2210 $this->localtax2_type = $objp->localtax2_type;
2211 $this->remise_percent = $objp->remise_percent;
2213 $this->fk_product = $objp->fk_product;
2214 $this->date_start_fill = $objp->date_start_fill;
2215 $this->date_end_fill = $objp->date_end_fill;
2216 $this->info_bits = $objp->info_bits;
2217 $this->total_ht = $objp->total_ht;
2218 $this->total_tva = $objp->total_tva;
2219 $this->total_ttc = $objp->total_ttc;
2221 $this->rang = $objp->rang;
2222 $this->special_code = $objp->special_code;
2223 $this->fk_unit = $objp->fk_unit;
2224 $this->fk_contract_line = $objp->fk_contract_line;
2225 $this->import_key = $objp->import_key;
2226 $this->fk_multicurrency = $objp->fk_multicurrency;
2227 $this->multicurrency_code = $objp->multicurrency_code;
2228 $this->multicurrency_subprice = $objp->multicurrency_subprice;
2229 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2230 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2231 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2233 $this->buy_price_ht = $objp->buy_price_ht;
2235 $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2236 $this->fk_user_author = $objp->fk_user_author;
2237 $this->fk_user_modif = $objp->fk_user_modif;
2239 $this->db->free($result);
2242 $this->error = $this->db->lasterror();
2261 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2263 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet_rec SET";
2264 $sql .=
" fk_facture = ".((int) $this->fk_facture);
2265 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
2266 $sql .=
", description='".$this->db->escape($this->desc).
"'";
2267 $sql .=
", price=".price2num($this->
price);
2268 $sql .=
", qty=".price2num($this->qty);
2269 $sql .=
", tva_tx=".price2num($this->tva_tx);
2270 $sql .=
", vat_src_code='".$this->db->escape($this->vat_src_code).
"'";
2271 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
2272 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
2273 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
2274 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
2275 $sql .=
", fk_product=".($this->fk_product > 0 ? $this->fk_product :
"null");
2276 $sql .=
", product_type=".((int) $this->product_type);
2277 $sql .=
", remise_percent=".price2num($this->remise_percent);
2278 $sql .=
", subprice=".price2num($this->subprice);
2279 $sql .=
", info_bits=".price2num($this->info_bits);
2280 $sql .=
", date_start_fill=".(int) $this->date_start_fill;
2281 $sql .=
", date_end_fill=".(int) $this->date_end_fill;
2282 if (empty($this->skip_update_total)) {
2283 $sql .=
", total_ht=".price2num($this->total_ht);
2284 $sql .=
", total_tva=".price2num($this->total_tva);
2285 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
2286 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
2287 $sql .=
", total_ttc=".price2num($this->total_ttc);
2289 $sql .=
", rang=".((int) $this->rang);
2290 $sql .=
", special_code=".((int) $this->special_code);
2291 $sql .=
", fk_unit=".($this->fk_unit ?
"'".$this->db->escape($this->fk_unit).
"'" :
"null");
2292 $sql .=
", fk_contract_line=".($this->fk_contract_line ? $this->fk_contract_line :
"null");
2293 $sql .=
" WHERE rowid = ".((int) $this->
id);
2297 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
2299 $resql = $this->db->query(
$sql);
2308 if (!$error && !$notrigger) {
2310 $result = $this->
call_trigger(
'LINEBILLREC_MODIFY', $user);
2318 $this->db->rollback();
2321 $this->db->commit();
2325 $this->error = $this->db->lasterror();
2326 $this->db->rollback();