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';
87 public $remise_absolue;
88 public $remise_percent;
102 public $date_last_gen;
117 public $unit_frequency;
120 public $special_code;
122 public $usenewprice = 0;
124 public $date_lim_reglement;
125 public $cond_reglement_code;
126 public $mode_reglement_code;
130 public $auto_validate;
131 public $generate_pdf;
168 public $fields = array(
169 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>10),
170 'titre' =>array(
'type'=>
'varchar(100)',
'label'=>
'Titre',
'enabled'=>1,
'showoncombobox' => 1,
'visible'=>-1,
'position'=>15),
171 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
172 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>
'$conf->societe->enabled',
'visible'=>-1,
'notnull'=>1,
'position'=>25),
173 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>30),
175 'remise' =>array(
'type'=>
'double',
'label'=>
'Remise',
'enabled'=>1,
'visible'=>-1,
'position'=>40),
178 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Tva',
'enabled'=>1,
'visible'=>-1,
'position'=>55,
'isameasure'=>1),
179 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax1',
'enabled'=>1,
'visible'=>-1,
'position'=>60,
'isameasure'=>1),
180 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'Localtax2',
'enabled'=>1,
'visible'=>-1,
'position'=>65,
'isameasure'=>1),
181 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'Total',
'enabled'=>1,
'visible'=>-1,
'position'=>70,
'isameasure'=>1),
182 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'Total ttc',
'enabled'=>1,
'visible'=>-1,
'position'=>75,
'isameasure'=>1),
183 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'Fk user author',
'enabled'=>1,
'visible'=>-1,
'position'=>80),
184 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label'=>
'Fk projet',
'enabled'=>
'$conf->project->enabled',
'visible'=>-1,
'position'=>85),
185 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'Fk cond reglement',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
186 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'Fk mode reglement',
'enabled'=>1,
'visible'=>-1,
'position'=>95),
187 'date_lim_reglement' =>array(
'type'=>
'date',
'label'=>
'Date lim reglement',
'enabled'=>1,
'visible'=>-1,
'position'=>100),
188 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>105),
189 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>110),
190 'modelpdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Modelpdf',
'enabled'=>1,
'visible'=>-1,
'position'=>115),
191 'date_last_gen' =>array(
'type'=>
'varchar(7)',
'label'=>
'Last gen',
'enabled'=>1,
'visible'=>-1,
'position'=>120),
192 'unit_frequency' =>array(
'type'=>
'varchar(2)',
'label'=>
'Unit frequency',
'enabled'=>1,
'visible'=>-1,
'position'=>125),
193 'date_when' =>array(
'type'=>
'datetime',
'label'=>
'Date when',
'enabled'=>1,
'visible'=>-1,
'position'=>130),
194 'date_last_gen' =>array(
'type'=>
'datetime',
'label'=>
'Date last gen',
'enabled'=>1,
'visible'=>-1,
'position'=>135),
195 'nb_gen_done' =>array(
'type'=>
'integer',
'label'=>
'Nb gen done',
'enabled'=>1,
'visible'=>-1,
'position'=>140),
196 'nb_gen_max' =>array(
'type'=>
'integer',
'label'=>
'Nb gen max',
'enabled'=>1,
'visible'=>-1,
'position'=>145),
197 'frequency' =>array(
'type'=>
'integer',
'label'=>
'Frequency',
'enabled'=>1,
'visible'=>-1,
'position'=>150),
198 'usenewprice' =>array(
'type'=>
'integer',
'label'=>
'UseNewPrice',
'enabled'=>1,
'visible'=>0,
'position'=>155),
199 'revenuestamp' =>array(
'type'=>
'double(24,8)',
'label'=>
'RevenueStamp',
'enabled'=>1,
'visible'=>-1,
'position'=>160,
'isameasure'=>1),
200 'auto_validate' =>array(
'type'=>
'integer',
'label'=>
'Auto validate',
'enabled'=>1,
'visible'=>-1,
'position'=>165),
201 'generate_pdf' =>array(
'type'=>
'integer',
'label'=>
'Generate pdf',
'enabled'=>1,
'visible'=>-1,
'position'=>170),
202 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'Fk account',
'enabled'=>
'$conf->banque->enabled',
'visible'=>-1,
'position'=>175),
203 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'Fk multicurrency',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
204 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'Multicurrency code',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
205 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency tx',
'enabled'=>1,
'visible'=>-1,
'position'=>190,
'isameasure'=>1),
206 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total ht',
'enabled'=>1,
'visible'=>-1,
'position'=>195,
'isameasure'=>1),
207 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total tva',
'enabled'=>1,
'visible'=>-1,
'position'=>200,
'isameasure'=>1),
208 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'Multicurrency total ttc',
'enabled'=>1,
'visible'=>-1,
'position'=>205,
'isameasure'=>1),
209 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>210),
210 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModification',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>215),
211 'suspended' =>array(
'type'=>
'integer',
'label'=>
'Suspended',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
215 const STATUS_NOTSUSPENDED = 0;
216 const STATUS_SUSPENDED = 1;
238 public function create($user, $facid, $notrigger = 0)
246 $this->titre = trim(isset($this->titre) ? $this->titre : $this->title);
247 $this->title = trim($this->title);
248 $this->usenewprice = empty($this->usenewprice) ? 0 : $this->usenewprice;
249 if (empty($this->suspended)) {
250 $this->suspended = 0;
254 if (empty($this->frequency)) {
255 $this->frequency = 0;
256 $this->date_when =
null;
259 $this->frequency = abs($this->frequency);
260 $this->nb_gen_done = 0;
261 $this->nb_gen_max = empty($this->nb_gen_max) ? 0 : $this->nb_gen_max;
262 $this->auto_validate = empty($this->auto_validate) ? 0 : $this->auto_validate;
263 $this->generate_pdf = empty($this->generate_pdf) ? 0 : $this->generate_pdf;
269 $result = $facsrc->fetch($facid);
272 $this->brouillon = 1;
273 $this->fk_soc = $facsrc->socid;
275 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture_rec (";
282 $sql .=
", note_private";
283 $sql .=
", note_public";
284 $sql .=
", modelpdf";
285 $sql .=
", fk_user_author";
286 $sql .=
", fk_projet";
287 $sql .=
", fk_account";
288 $sql .=
", fk_cond_reglement";
289 $sql .=
", fk_mode_reglement";
290 $sql .=
", usenewprice";
291 $sql .=
", frequency";
292 $sql .=
", unit_frequency";
293 $sql .=
", date_when";
294 $sql .=
", date_last_gen";
295 $sql .=
", nb_gen_done";
296 $sql .=
", nb_gen_max";
297 $sql .=
", auto_validate";
298 $sql .=
", generate_pdf";
299 $sql .=
", fk_multicurrency";
300 $sql .=
", multicurrency_code";
301 $sql .=
", multicurrency_tx";
302 $sql .=
", suspended";
303 $sql .=
") VALUES (";
304 $sql .=
"'".$this->db->escape($this->titre ? $this->titre : $this->title).
"'";
305 $sql .=
", ".((int) $this->fk_soc);
306 $sql .=
", ".((int) $conf->entity);
307 $sql .=
", '".$this->db->idate($now).
"'";
308 $sql .=
", ".(!empty($facsrc->total_ttc) ? ((
float) $facsrc->total_ttc) :
'0');
309 $sql .=
", ".(!empty($facsrc->remise_absolue) ? ((
float) $this->remise_absolue) :
'0');
310 $sql .=
", ".(!empty($this->note_private) ? (
"'".$this->db->escape($this->note_private).
"'") :
"NULL");
311 $sql .=
", ".(!empty($this->note_public) ? (
"'".$this->db->escape($this->note_public).
"'") :
"NULL");
312 $sql .=
", ".(!empty($this->model_pdf) ? (
"'".$this->db->escape($this->model_pdf).
"'") :
"NULL");
313 $sql .=
", ".((int) $user->id);
314 $sql .=
", ".(!empty($this->fk_project) ? ((int) $this->fk_project) :
"null");
315 $sql .=
", ".(!empty($facsrc->fk_account) ? ((int) $facsrc->fk_account) :
"null");
316 $sql .=
", ".($this->cond_reglement_id > 0 ? ((int) $this->cond_reglement_id) :
"null");
317 $sql .=
", ".($this->mode_reglement_id > 0 ? ((int) $this->mode_reglement_id) :
"null");
318 $sql .=
", ".((int) $this->usenewprice);
319 $sql .=
", ".((int) $this->frequency);
320 $sql .=
", '".$this->db->escape($this->unit_frequency).
"'";
321 $sql .=
", ".(!empty($this->date_when) ?
"'".$this->db->idate($this->date_when).
"'" :
'NULL');
322 $sql .=
", ".(!empty($this->date_last_gen) ?
"'".$this->db->idate($this->date_last_gen).
"'" :
'NULL');
323 $sql .=
", ".((int) $this->nb_gen_done);
324 $sql .=
", ".((int) $this->nb_gen_max);
325 $sql .=
", ".((int) $this->auto_validate);
326 $sql .=
", ".((int) $this->generate_pdf);
327 $sql .=
", ".((int) $facsrc->fk_multicurrency);
328 $sql .=
", '".$this->db->escape($facsrc->multicurrency_code).
"'";
329 $sql .=
", ".((float) $facsrc->multicurrency_tx);
330 $sql .=
", ".((int) $this->suspended);
333 if ($this->
db->query($sql)) {
334 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"facture_rec");
337 $this->fk_multicurrency = $facsrc->fk_multicurrency;
338 $this->multicurrency_code = $facsrc->multicurrency_code;
339 $this->multicurrency_tx = $facsrc->multicurrency_tx;
342 $num = count($facsrc->lines);
343 for ($i = 0; $i < $num; $i++) {
344 $tva_tx = $facsrc->lines[$i]->tva_tx;
345 if (!empty($facsrc->lines[$i]->vat_src_code) && !preg_match(
'/\(/', $tva_tx)) {
346 $tva_tx .=
' ('.$facsrc->lines[$i]->vat_src_code.
')';
349 $default_start_fill =
getDolGlobalInt(
'INVOICEREC_SET_AUTOFILL_DATE_START');
350 $default_end_fill =
getDolGlobalInt(
'INVOICEREC_SET_AUTOFILL_DATE_END');
352 $result_insert = $this->
addline(
353 $facsrc->lines[$i]->desc,
354 $facsrc->lines[$i]->subprice,
355 $facsrc->lines[$i]->qty,
357 $facsrc->lines[$i]->localtax1_tx,
358 $facsrc->lines[$i]->localtax2_tx,
359 $facsrc->lines[$i]->fk_product,
360 $facsrc->lines[$i]->remise_percent,
362 $facsrc->lines[$i]->info_bits,
365 $facsrc->lines[$i]->product_type,
366 $facsrc->lines[$i]->rang,
367 $facsrc->lines[$i]->special_code,
368 $facsrc->lines[$i]->label,
369 $facsrc->lines[$i]->fk_unit,
370 $facsrc->lines[$i]->multicurrency_subprice,
374 $facsrc->lines[$i]->pa_ht
377 if ($result_insert < 0) {
382 $result2 = $objectline->fetch($result_insert);
385 if (method_exists($facsrc->lines[$i],
'fetch_optionals')) {
386 $facsrc->lines[$i]->fetch_optionals($facsrc->lines[$i]->id);
387 $objectline->array_options = $facsrc->lines[$i]->array_options;
390 $result = $objectline->insertExtraFields();
394 } elseif ($result2 < 0) {
395 $this->errors[] = $objectline->error;
401 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
402 $this->linked_objects = $this->linkedObjectsIds;
406 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
407 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
408 if (is_array($tmp_origin_id)) {
409 foreach ($tmp_origin_id as $origin_id) {
412 $this->error = $this->
db->lasterror();
418 $origin_id = $tmp_origin_id;
421 $this->error = $this->
db->lasterror();
435 if (!$error && !$notrigger) {
439 $this->
db->rollback();
446 $this->
db->rollback();
453 $this->error = $this->
db->lasterror();
454 $this->
db->rollback();
458 $this->
db->rollback();
477 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture_rec SET";
478 $sql .=
" entity = ".((int) $this->entity).
",";
479 $sql .=
" titre = '".$this->db->escape($this->title).
"',";
480 $sql .=
" suspended = ".((int) $this->suspended).
",";
481 $sql .=
" fk_soc = ".((int) $this->socid).
",";
482 $sql .=
" total_tva = ".((float) $this->total_tva).
",";
483 $sql .=
" localtax1 = ".((float) $this->localtax1).
",";
484 $sql .=
" localtax2 = ".((float) $this->localtax2).
",";
485 $sql .=
" total_ht = ".((float) $this->total_ht).
",";
486 $sql .=
" total_ttc = ".((float) $this->total_ttc).
",";
487 $sql .=
" remise_percent = ".((float) $this->remise_percent);
489 $sql .=
" WHERE rowid = ".((int) $this->
id);
493 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
504 if (!$error && !$notrigger) {
508 $this->
db->rollback();
516 $this->error = $this->
db->lasterror();
517 $this->
db->rollback();
530 public function fetch($rowid, $ref =
'', $ref_ext =
'')
534 $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';
535 $sql .=
', f.remise_percent, f.remise_absolue, f.remise';
536 $sql .=
', f.date_lim_reglement as dlr';
537 $sql .=
', f.note_private, f.note_public, f.fk_user_author';
538 $sql .=
', f.modelpdf as model_pdf';
539 $sql .=
', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project';
540 $sql .=
', f.fk_account';
541 $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';
542 $sql .=
', f.generate_pdf';
543 $sql .=
", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
544 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
545 $sql .=
', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
547 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture_rec as f';
548 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
549 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON f.fk_mode_reglement = p.id';
551 $sql .=
' WHERE f.entity IN ('.getEntity(
'invoice').
')';
553 $sql .=
' AND f.rowid = '.((int) $rowid);
555 $sql .=
" AND f.titre = '".$this->db->escape($ref).
"'";
557 $sql .=
' AND f.rowid = 0';
563 $result = $this->
db->query($sql);
565 if ($this->
db->num_rows($result)) {
566 $obj = $this->
db->fetch_object($result);
568 $this->
id = $obj->rowid;
569 $this->entity = $obj->entity;
570 $this->titre = $obj->title;
571 $this->title = $obj->title;
572 $this->
ref = $obj->title;
573 $this->suspended = $obj->suspended;
574 $this->remise_percent = $obj->remise_percent;
575 $this->remise_absolue = $obj->remise_absolue;
576 $this->remise = $obj->remise;
577 $this->total_ht = $obj->total_ht;
578 $this->total_tva = $obj->total_tva;
579 $this->total_localtax1 = $obj->localtax1;
580 $this->total_localtax2 = $obj->localtax2;
581 $this->total_ttc = $obj->total_ttc;
582 $this->socid = $obj->fk_soc;
583 $this->date_lim_reglement = $this->
db->jdate($obj->dlr);
584 $this->mode_reglement_id = $obj->fk_mode_reglement;
585 $this->mode_reglement_code = $obj->mode_reglement_code;
586 $this->mode_reglement = $obj->mode_reglement_libelle;
587 $this->cond_reglement_id = $obj->fk_cond_reglement;
588 $this->cond_reglement_code = $obj->cond_reglement_code;
589 $this->cond_reglement = $obj->cond_reglement_libelle;
590 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
591 $this->fk_project = $obj->fk_project;
592 $this->fk_account = $obj->fk_account;
593 $this->note_private = $obj->note_private;
594 $this->note_public = $obj->note_public;
595 $this->user_author = $obj->fk_user_author;
596 $this->modelpdf = $obj->model_pdf;
597 $this->model_pdf = $obj->model_pdf;
599 $this->frequency = $obj->frequency;
600 $this->unit_frequency = $obj->unit_frequency;
601 $this->date_when = $this->
db->jdate($obj->date_when);
602 $this->date_last_gen = $this->
db->jdate($obj->date_last_gen);
603 $this->nb_gen_done = $obj->nb_gen_done;
604 $this->nb_gen_max = $obj->nb_gen_max;
605 $this->usenewprice = $obj->usenewprice;
606 $this->auto_validate = $obj->auto_validate;
607 $this->generate_pdf = $obj->generate_pdf;
610 $this->fk_multicurrency = $obj->fk_multicurrency;
611 $this->multicurrency_code = $obj->multicurrency_code;
612 $this->multicurrency_tx = $obj->multicurrency_tx;
613 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
614 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
615 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
617 if ($this->statut == self::STATUS_DRAFT) {
618 $this->brouillon = 1;
630 $this->error = $this->
db->lasterror();
635 $this->error =
'Bill with id '.$rowid.
' or ref '.$ref.
' not found';
636 dol_syslog(
'Facture::Fetch Error '.$this->error, LOG_ERR);
640 $this->error = $this->
db->error();
667 $this->lines = array();
678 dol_syslog(
'FactureRec::fetch_lines', LOG_DEBUG);
680 $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, ';
681 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
682 $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,';
683 $sql .=
' l.rang, l.special_code,';
684 $sql .=
' l.fk_unit, l.fk_contract_line,';
685 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
686 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
687 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet_rec as l';
688 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
689 $sql .=
' WHERE l.fk_facture = '.((int) $this->
id);
690 $sql .=
' ORDER BY l.rang';
692 $result = $this->
db->query($sql);
694 $num = $this->
db->num_rows($result);
697 $objp = $this->
db->fetch_object($result);
700 $line->id = $objp->rowid;
701 $line->rowid = $objp->rowid;
702 $line->desc = $objp->description;
703 $line->description = $objp->description;
704 $line->ref = $objp->product_ref;
705 $line->product_ref = $objp->product_ref;
706 $line->libelle = $objp->product_label;
707 $line->product_label = $objp->product_label;
708 $line->product_desc = $objp->product_desc;
709 $line->product_type = $objp->product_type;
710 $line->fk_product_type = $objp->fk_product_type;
711 $line->qty = $objp->qty;
712 $line->subprice = $objp->subprice;
714 $line->label = $objp->custom_label;
716 $line->vat_src_code = $objp->vat_src_code;
717 $line->tva_tx = $objp->tva_tx;
718 $line->localtax1_tx = $objp->localtax1_tx;
719 $line->localtax2_tx = $objp->localtax2_tx;
720 $line->localtax1_type = $objp->localtax1_type;
721 $line->localtax2_type = $objp->localtax2_type;
722 $line->remise_percent = $objp->remise_percent;
724 $line->fk_product = $objp->fk_product;
725 $line->date_start_fill = $objp->date_start_fill;
726 $line->date_end_fill = $objp->date_end_fill;
727 $line->info_bits = $objp->info_bits;
728 $line->total_ht = $objp->total_ht;
729 $line->total_tva = $objp->total_tva;
730 $line->total_ttc = $objp->total_ttc;
734 $line->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
735 $line->fk_fournprice = $objp->fk_product_fournisseur_price;
737 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $objp->fk_product_fournisseur_price, $objp->pa_ht);
739 $line->buyprice = $marginInfos[0];
740 $line->pa_ht = $marginInfos[0];
741 $line->marge_tx = $marginInfos[1];
742 $line->marque_tx = $marginInfos[2];
743 $line->rang = $objp->rang;
744 $line->special_code = $objp->special_code;
745 $line->fk_unit = $objp->fk_unit;
746 $line->fk_contract_line = $objp->fk_contract_line;
749 $line->price = $objp->price;
750 $line->remise = $objp->remise;
752 $line->fetch_optionals();
755 $line->fk_multicurrency = $objp->fk_multicurrency;
756 $line->multicurrency_code = $objp->multicurrency_code;
757 $line->multicurrency_subprice = $objp->multicurrency_subprice;
758 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
759 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
760 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
762 $this->lines[$i] = $line;
767 $this->
db->free($result);
770 $this->error = $this->
db->lasterror();
784 public function delete(
User $user, $notrigger = 0, $idwarehouse = -1)
788 dol_syslog(get_class($this).
"::delete rowid=".((
int) $rowid), LOG_DEBUG);
793 $main = MAIN_DB_PREFIX.
'facturedet_rec';
794 $ef = $main.
"_extrafields";
796 $sqlef =
"DELETE FROM $ef WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_facture = ".((int) $rowid).
")";
797 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facturedet_rec WHERE fk_facture = ".((int) $rowid);
799 if ($this->
db->query($sqlef) && $this->
db->query($sql)) {
800 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facture_rec WHERE rowid = ".((int) $rowid);
802 if ($this->
db->query($sql)) {
814 $this->error = $this->
db->lasterror();
818 $this->error = $this->
db->lasterror();
821 if (!$error && !$notrigger) {
833 $this->
db->rollback();
866 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)
872 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);
873 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
885 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
886 $vat_src_code = $reg[1];
887 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
890 if ($this->brouillon) {
892 $remise_percent =
price2num($remise_percent);
893 if (empty($remise_percent)) {
899 if (!preg_match(
'/\((.*)\)/', $txtva)) {
907 if (empty($txlocaltax1)) {
910 if (empty($txlocaltax2)) {
913 if (empty($info_bits)) {
917 if ($price_base_type ==
'HT') {
928 $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);
929 $total_ht = $tabprice[0];
930 $total_tva = $tabprice[1];
931 $total_ttc = $tabprice[2];
932 $total_localtax1 = $tabprice[9];
933 $total_localtax2 = $tabprice[10];
934 $pu_ht = $tabprice[3];
937 $multicurrency_total_ht = $tabprice[16];
938 $multicurrency_total_tva = $tabprice[17];
939 $multicurrency_total_ttc = $tabprice[18];
940 $pu_ht_devise = $tabprice[19];
942 $product_type = $type;
945 $result = $product->fetch($fk_product);
946 $product_type = $product->type;
951 if ($ranktouse == -1) {
953 $ranktouse = $rangmax + 1;
956 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facturedet_rec (";
957 $sql .=
"fk_facture";
959 $sql .=
", description";
963 $sql .=
", vat_src_code";
964 $sql .=
", localtax1_tx";
965 $sql .=
", localtax1_type";
966 $sql .=
", localtax2_tx";
967 $sql .=
", localtax2_type";
968 $sql .=
", fk_product";
969 $sql .=
", product_type";
970 $sql .=
", remise_percent";
971 $sql .=
", subprice";
973 $sql .=
", total_ht";
974 $sql .=
", total_tva";
975 $sql .=
", total_localtax1";
976 $sql .=
", total_localtax2";
977 $sql .=
", total_ttc";
978 $sql .=
", date_start_fill";
979 $sql .=
", date_end_fill";
980 $sql .=
", fk_product_fournisseur_price";
981 $sql .=
", buy_price_ht";
982 $sql .=
", info_bits";
984 $sql .=
", special_code";
986 $sql .=
', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
987 $sql .=
") VALUES (";
988 $sql .=
" ".((int) $facid);
989 $sql .=
", ".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
990 $sql .=
", '".$this->db->escape($desc).
"'";
991 $sql .=
", ".price2num($pu_ht);
992 $sql .=
", ".price2num($qty);
993 $sql .=
", ".price2num($txtva);
994 $sql .=
", '".$this->db->escape($vat_src_code).
"'";
995 $sql .=
", ".price2num($txlocaltax1);
996 $sql .=
", '".$this->db->escape(isset($localtaxes_type[0]) ? $localtaxes_type[0] :
'').
"'";
997 $sql .=
", ".price2num($txlocaltax2);
998 $sql .=
", '".$this->db->escape(isset($localtaxes_type[2]) ? $localtaxes_type[2] :
'').
"'";
999 $sql .=
", ".(!empty($fk_product) ?
"'".$this->db->escape($fk_product).
"'" :
"null");
1000 $sql .=
", ".((int) $product_type);
1001 $sql .=
", ".price2num($remise_percent);
1002 $sql .=
", ".price2num($pu_ht);
1004 $sql .=
", ".price2num($total_ht);
1005 $sql .=
", ".price2num($total_tva);
1006 $sql .=
", ".price2num($total_localtax1);
1007 $sql .=
", ".price2num($total_localtax2);
1008 $sql .=
", ".price2num($total_ttc);
1009 $sql .=
", ".(int) $date_start_fill;
1010 $sql .=
", ".(int) $date_end_fill;
1011 $sql .=
", ".($fk_fournprice > 0 ? $fk_fournprice :
'null');
1012 $sql .=
", ".($pa_ht ?
price2num($pa_ht) : 0);
1013 $sql .=
", ".((int) $info_bits);
1014 $sql .=
", ".((int) $ranktouse);
1015 $sql .=
", ".((int) $special_code);
1016 $sql .=
", ".($fk_unit ? ((int) $fk_unit) :
"null");
1017 $sql .=
", ".(int) $this->fk_multicurrency;
1018 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
1019 $sql .=
", ".price2num($pu_ht_devise,
'CU');
1020 $sql .=
", ".price2num($multicurrency_total_ht,
'CT');
1021 $sql .=
", ".price2num($multicurrency_total_tva,
'CT');
1022 $sql .=
", ".price2num($multicurrency_total_ttc,
'CT');
1025 dol_syslog(get_class($this).
"::addline", LOG_DEBUG);
1026 if ($this->
db->query($sql)) {
1027 $lineId = $this->
db->last_insert_id(MAIN_DB_PREFIX.
"facturedet_rec");
1032 $this->error = $this->
db->lasterror();
1067 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)
1073 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);
1074 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
1077 if (empty($remise_percent)) {
1078 $remise_percent = 0;
1086 if ($this->brouillon) {
1088 $remise_percent =
price2num($remise_percent);
1090 if (empty($info_bits)) {
1095 $pu_ht_devise =
price2num($pu_ht_devise);
1096 if (!preg_match(
'/\((.*)\)/', $txtva)) {
1101 if (empty($txlocaltax1)) {
1104 if (empty($txlocaltax2)) {
1108 if (empty($this->multicurrency_subprice)) {
1109 $this->multicurrency_subprice = 0;
1111 if (empty($this->multicurrency_total_ht)) {
1112 $this->multicurrency_total_ht = 0;
1114 if (empty($this->multicurrency_total_tva)) {
1115 $this->multicurrency_total_tva = 0;
1117 if (empty($this->multicurrency_total_ttc)) {
1118 $this->multicurrency_total_ttc = 0;
1121 if ($price_base_type ==
'HT') {
1136 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
1137 $vat_src_code = $reg[1];
1138 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
1141 $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);
1143 $total_ht = $tabprice[0];
1144 $total_tva = $tabprice[1];
1145 $total_ttc = $tabprice[2];
1146 $total_localtax1 = $tabprice[9];
1147 $total_localtax2 = $tabprice[10];
1148 $pu_ht = $tabprice[3];
1149 $pu_tva = $tabprice[4];
1150 $pu_ttc = $tabprice[5];
1153 $multicurrency_total_ht = $tabprice[16];
1154 $multicurrency_total_tva = $tabprice[17];
1155 $multicurrency_total_ttc = $tabprice[18];
1156 $pu_ht_devise = $tabprice[19];
1158 $product_type = $type;
1161 $result = $product->fetch($fk_product);
1162 $product_type = $product->type;
1165 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet_rec SET ";
1166 $sql .=
"fk_facture = ".((int) $facid);
1167 $sql .=
", label=".(!empty($label) ?
"'".$this->db->escape($label).
"'" :
"null");
1168 $sql .=
", description='".$this->db->escape($desc).
"'";
1169 $sql .=
", price=".price2num($pu_ht);
1170 $sql .=
", qty=".price2num($qty);
1171 $sql .=
", tva_tx=".price2num($txtva);
1172 $sql .=
", vat_src_code='".$this->db->escape($vat_src_code).
"'";
1173 $sql .=
", localtax1_tx=".((float) $txlocaltax1);
1174 $sql .=
", localtax1_type='".$this->db->escape($localtaxes_type[0]).
"'";
1175 $sql .=
", localtax2_tx=".((float) $txlocaltax2);
1176 $sql .=
", localtax2_type='".$this->db->escape($localtaxes_type[2]).
"'";
1177 $sql .=
", fk_product=".(!empty($fk_product) ?
"'".$this->db->escape($fk_product).
"'" :
"null");
1178 $sql .=
", product_type=".((int) $product_type);
1179 $sql .=
", remise_percent='".price2num($remise_percent).
"'";
1180 $sql .=
", subprice='".price2num($pu_ht).
"'";
1181 $sql .=
", total_ht='".price2num($total_ht).
"'";
1182 $sql .=
", total_tva='".price2num($total_tva).
"'";
1183 $sql .=
", total_localtax1='".price2num($total_localtax1).
"'";
1184 $sql .=
", total_localtax2='".price2num($total_localtax2).
"'";
1185 $sql .=
", total_ttc='".price2num($total_ttc).
"'";
1186 $sql .=
", date_start_fill=".((int) $date_start_fill);
1187 $sql .=
", date_end_fill=".((int) $date_end_fill);
1188 $sql .=
", fk_product_fournisseur_price=".($fk_fournprice > 0 ? $fk_fournprice :
'null');
1189 $sql .=
", buy_price_ht=".($pa_ht ?
price2num($pa_ht) : 0);
1190 $sql .=
", info_bits=".((int) $info_bits);
1191 $sql .=
", rang=".((int) $rang);
1192 $sql .=
", special_code=".((int) $special_code);
1193 $sql .=
", fk_unit=".($fk_unit ?
"'".$this->db->escape($fk_unit).
"'" :
"null");
1194 $sql .=
', multicurrency_subprice = '.price2num($pu_ht_devise);
1195 $sql .=
', multicurrency_total_ht = '.price2num($multicurrency_total_ht);
1196 $sql .=
', multicurrency_total_tva = '.price2num($multicurrency_total_tva);
1197 $sql .=
', multicurrency_total_ttc = '.price2num($multicurrency_total_ttc);
1198 $sql .=
" WHERE rowid = ".((int) $rowid);
1200 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
1201 if ($this->
db->query($sql)) {
1206 $this->error = $this->
db->lasterror();
1220 if (empty($this->date_when)) {
1234 if ($this->nb_gen_max > 0 && ($this->nb_gen_done >= $this->nb_gen_max)) {
1249 return ($this->
isMaxNbGenReached() ?
'<strike>' :
'').$ret.($this->isMaxNbGenReached() ?
'</strike>' :
'');
1265 global $conf, $langs, $db, $user, $hookmanager;
1271 $langs->loadLangs(array(
"main",
"bills"));
1275 $today =
dol_mktime(23, 59, 59, $tmparray[
'mon'], $tmparray[
'mday'], $tmparray[
'year']);
1277 $this->output =
null;
1279 dol_syslog(
"createRecurringInvoices restrictioninvoiceid=".$restrictioninvoiceid.
" forcevalidation=".$forcevalidation);
1281 $sql =
'SELECT rowid FROM '.MAIN_DB_PREFIX.
'facture_rec';
1282 $sql .=
' WHERE frequency > 0';
1283 $sql .=
" AND (date_when IS NULL OR date_when <= '".$this->db->idate($today).
"')";
1284 $sql .=
' AND (nb_gen_done < nb_gen_max OR nb_gen_max = 0)';
1285 $sql .=
' AND suspended = 0';
1286 $sql .=
' AND entity = '.$conf->entity;
1287 if ($restrictioninvoiceid > 0) {
1288 $sql .=
' AND rowid = '.((int) $restrictioninvoiceid);
1290 $sql .= $this->
db->order(
'entity',
'ASC');
1292 $parameters = array(
1293 'restrictioninvoiceid' => $restrictioninvoiceid,
1294 'forcevalidation' => $forcevalidation,
1296 $reshook = $hookmanager->executeHooks(
'beforeCreationOfRecurringInvoices', $parameters, $sql);
1301 $num = $this->
db->num_rows(
$resql);
1304 $this->output .= $langs->trans(
"FoundXQualifiedRecurringInvoiceTemplate", $num).
"\n";
1306 $this->output .= $langs->trans(
"NoQualifiedRecurringInvoiceTemplateFound");
1309 $saventity = $conf->entity;
1312 $line = $this->
db->fetch_object(
$resql);
1316 $invoiceidgenerated = 0;
1320 $facturerec->fetch($line->rowid);
1322 if ($facturerec->id > 0) {
1324 $conf->entity = $facturerec->entity;
1326 dol_syslog(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref.
", entity=".$facturerec->entity);
1329 $facture->fac_rec = $facturerec->id;
1330 $facture->fk_fac_rec_source = $facturerec->id;
1333 $facture->brouillon = 1;
1336 $facture->date = (empty($facturerec->date_when) ? $now : $facturerec->date_when);
1337 $facture->socid = $facturerec->socid;
1338 if (!empty($facturerec->fk_multicurrency)) {
1339 $facture->fk_multicurrency = $facturerec->fk_multicurrency;
1340 $facture->multicurrency_code = $facturerec->multicurrency_code;
1341 $facture->multicurrency_tx = $facturerec->multicurrency_tx;
1344 $invoiceidgenerated = $facture->create($user);
1345 if ($invoiceidgenerated <= 0) {
1346 $this->errors = $facture->errors;
1347 $this->error = $facture->error;
1352 if (!$error && ($facturerec->auto_validate || $forcevalidation)) {
1353 $result = $facture->validate($user);
1355 $this->errors = $facture->errors;
1356 $this->error = $facture->error;
1360 if (!$error && $facturerec->generate_pdf) {
1362 $facture->fetch($facture->id);
1363 $result = $facture->generateDocument($facturerec->model_pdf, $langs);
1365 $this->errors = $facture->errors;
1366 $this->error = $facture->error;
1370 if (!$error && !$notrigger) {
1372 $result = $facturerec->call_trigger(
'BILLREC_CREATEBILL', $user);
1374 $this->errors = $facturerec->errors;
1375 $this->error = $facturerec->error;
1382 $this->error =
"Failed to load invoice template with id=".$line->rowid.
", entity=".$conf->entity.
"\n";
1383 $this->errors[] =
"Failed to load invoice template with id=".$line->rowid.
", entity=".$conf->entity;
1384 dol_syslog(
"createRecurringInvoices Failed to load invoice template with id=".$line->rowid.
", entity=".$conf->entity);
1387 if (!$error && $invoiceidgenerated >= 0) {
1388 $this->
db->commit(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref);
1389 dol_syslog(
"createRecurringInvoices Process invoice template ".$facturerec->ref.
" is finished with a success generation");
1391 $this->output .= $langs->trans(
"InvoiceGeneratedFromTemplate", $facture->ref, $facturerec->ref).
"\n";
1393 $this->
db->rollback(
"createRecurringInvoices Process invoice template id=".$facturerec->id.
", ref=".$facturerec->ref);
1396 $parameters = array(
1399 'errorCount' => $error,
1400 'invoiceidgenerated' => $invoiceidgenerated,
1401 'facturerec' => $facturerec,
1404 $reshook = $hookmanager->executeHooks(
'afterCreationOfRecurringInvoice', $parameters, $facture);
1409 $conf->entity = $saventity;
1414 $this->output = trim($this->output);
1416 return $error ? $error : 0;
1431 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip =
'', $save_lastsearch_value = -1)
1433 global $langs, $hookmanager;
1437 $label =
'<u>'.$langs->trans(
"RepeatableInvoice").
'</u>';
1438 if (!empty($this->
ref)) {
1439 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1441 if ($this->frequency > 0) {
1442 $label .=
'<br><b>'.$langs->trans(
'Frequency').
':</b> '.$langs->trans(
'FrequencyPer_'.$this->unit_frequency, $this->frequency);
1444 if (!empty($this->date_last_gen)) {
1445 $label .=
'<br><b>'.$langs->trans(
'DateLastGeneration').
':</b> '.
dol_print_date($this->date_last_gen,
'dayhour');
1447 if ($this->frequency > 0) {
1448 if (!empty($this->date_when)) {
1449 $label .=
'<br><b>'.$langs->trans(
'NextDateToExecution').
':</b> ';
1450 $label .= (empty($this->suspended) ?
'' :
'<strike>').
dol_print_date($this->date_when,
'day').(empty($this->suspended) ?
'' :
'</strike>');
1451 if (!empty($this->suspended)) {
1452 $label .=
' ('.$langs->trans(
"Disabled").
')';
1457 $url = DOL_URL_ROOT.
'/compta/facture/card-rec.php?facid='.$this->id;
1463 if ($option !=
'nolink') {
1465 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1466 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1467 $add_save_lastsearch_values = 1;
1469 if ($add_save_lastsearch_values) {
1470 $url .=
'&save_lastsearch_values=1';
1474 $linkstart =
'<a href="'.$url.
'" title="'.
dol_escape_htmltag($label, 1).
'" class="classfortooltip">';
1477 $result .= $linkstart;
1479 $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);
1481 if ($withpicto != 2) {
1482 $result .= $this->ref;
1484 $result .= $linkend;
1486 $hookmanager->initHooks(array($this->element .
'dao'));
1487 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result);
1488 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1490 $result = $hookmanager->resPrint;
1492 $result .= $hookmanager->resPrint;
1506 return $this->
LibStatut($this->frequency ? 1 : 0, $this->suspended, $mode, $alreadypaid, empty($this->
type) ? 0 : $this->
type);
1520 public function LibStatut($recur, $status, $mode = 0, $alreadypaid = -1, $type = 0)
1524 $langs->load(
'bills');
1526 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1527 $statusType =
'status0';
1532 if ($status == self::STATUS_SUSPENDED) {
1533 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1535 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1538 if ($status == self::STATUS_SUSPENDED) {
1539 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1541 $labelStatus = $langs->transnoentitiesnoconv(
"Draft");
1544 } elseif ($mode == 1) {
1547 if ($status == self::STATUS_SUSPENDED) {
1548 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1550 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1553 if ($status == self::STATUS_SUSPENDED) {
1554 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1556 $labelStatus = $langs->transnoentitiesnoconv(
"Draft");
1559 } elseif ($mode == 2) {
1561 if ($status == self::STATUS_SUSPENDED) {
1562 $statusType =
'status6';
1563 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1565 $statusType =
'status4';
1566 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1569 if ($status == self::STATUS_SUSPENDED) {
1570 $statusType =
'status6';
1571 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1573 $statusType =
'status0';
1574 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1577 } elseif ($mode == 3) {
1580 if ($status == self::STATUS_SUSPENDED) {
1581 $statusType =
'status6';
1582 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1584 $statusType =
'status4';
1585 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1588 if ($status == self::STATUS_SUSPENDED) {
1589 $statusType =
'status6';
1590 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1592 $statusType =
'status0';
1593 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1596 } elseif ($mode == 4) {
1599 if ($status == self::STATUS_SUSPENDED) {
1600 $statusType =
'status6';
1601 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1603 $statusType =
'status4';
1604 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1607 if ($status == self::STATUS_SUSPENDED) {
1608 $statusType =
'status6';
1609 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1611 $statusType =
'status0';
1612 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1615 } elseif ($mode == 5 || $mode == 6) {
1621 if ($status == self::STATUS_SUSPENDED) {
1622 $statusType =
'status6';
1623 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1625 $statusType =
'status4';
1626 $labelStatus = $langs->transnoentitiesnoconv(
'Active');
1629 if ($status == self::STATUS_SUSPENDED) {
1630 $statusType =
'status6';
1631 $labelStatus = $langs->transnoentitiesnoconv(
'Disabled');
1633 $statusType =
'status0';
1634 $labelStatus = $langs->transnoentitiesnoconv(
'Draft');
1639 $labelStatusShort = $labelStatus;
1641 return dolGetStatus($labelStatus, $labelStatusShort,
'', $statusType, $mode);
1654 global $user, $langs, $conf;
1658 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
1664 $sql =
"SELECT rowid";
1665 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
1666 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
1667 $sql .= $this->
db->plimit(100);
1671 $num_prods = $this->
db->num_rows(
$resql);
1673 while ($i < $num_prods) {
1675 $row = $this->
db->fetch_row(
$resql);
1676 $prodids[$i] = $row[0];
1682 $this->
ref =
'SPECIMEN';
1683 $this->title =
'SPECIMEN';
1684 $this->specimen = 1;
1686 $this->date = $nownotime;
1687 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
1688 $this->cond_reglement_id = 1;
1689 $this->cond_reglement_code =
'RECEP';
1691 $this->mode_reglement_id = 0;
1692 $this->mode_reglement_code =
'';
1693 $this->note_public =
'This is a comment (public)';
1694 $this->note_private =
'This is a comment (private)';
1695 $this->note =
'This is a comment (private)';
1696 $this->fk_incoterms = 0;
1697 $this->location_incoterms =
'';
1699 if (empty($option) || $option !=
'nolines') {
1703 while ($xnbp < $nbp) {
1705 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
1707 $line->subprice = 100;
1708 $line->tva_tx = 19.6;
1709 $line->localtax1_tx = 0;
1710 $line->localtax2_tx = 0;
1711 $line->remise_percent = 0;
1713 $prodid = mt_rand(1, $num_prods);
1714 $line->fk_product = $prodids[$prodid];
1716 $line->total_ht = -100;
1717 $line->total_ttc = -119.6;
1718 $line->total_tva = -19.6;
1719 } elseif ($xnbp == 2) {
1720 $line->subprice = -100;
1721 $line->total_ht = -100;
1722 $line->total_ttc = -119.6;
1723 $line->total_tva = -19.6;
1724 $line->remise_percent = 0;
1725 } elseif ($xnbp == 3) {
1726 $prodid = mt_rand(1, $num_prods);
1727 $line->fk_product = $prodids[$prodid];
1728 $line->total_ht = 50;
1729 $line->total_ttc = 59.8;
1730 $line->total_tva = 9.8;
1731 $line->remise_percent = 50;
1734 $prodid = mt_rand(1, $num_prods);
1735 $line->fk_product = $prodids[$prodid];
1736 $line->total_ht = 100;
1737 $line->total_ttc = 119.6;
1738 $line->total_tva = 19.6;
1739 $line->remise_percent = 00;
1742 $this->lines[$xnbp] = $line;
1745 $this->total_ht += $line->total_ht;
1746 $this->total_tva += $line->total_tva;
1747 $this->total_ttc += $line->total_ttc;
1749 $this->revenuestamp = 0;
1753 $line->desc = $langs->trans(
"Description").
" (offered line)";
1755 $line->subprice = 100;
1756 $line->tva_tx = 19.6;
1757 $line->localtax1_tx = 0;
1758 $line->localtax2_tx = 0;
1759 $line->remise_percent = 100;
1760 $line->total_ht = 0;
1761 $line->total_ttc = 0;
1762 $line->total_tva = 0;
1763 $prodid = mt_rand(1, $num_prods);
1764 $line->fk_product = $prodids[$prodid];
1766 $this->lines[$xnbp] = $line;
1770 $this->usenewprice = 0;
1819 if (!$this->table_element) {
1820 dol_syslog(get_class($this).
"::setFrequencyAndUnit was called on objet with property table_element not defined", LOG_ERR);
1824 if (!empty($frequency) && empty($unit)) {
1825 dol_syslog(get_class($this).
"::setFrequencyAndUnit was called on objet with params frequency defined but unit not defined", LOG_ERR);
1829 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1830 $sql .=
' SET frequency = '.($frequency ? $this->
db->escape($frequency) :
'null');
1831 if (!empty($unit)) {
1832 $sql .=
', unit_frequency = \''.$this->db->escape($unit).
'\'';
1834 $sql .=
" WHERE rowid = ".((int) $this->
id);
1836 dol_syslog(get_class($this).
"::setFrequencyAndUnit", LOG_DEBUG);
1837 if ($this->
db->query($sql)) {
1838 $this->frequency = $frequency;
1839 if (!empty($unit)) {
1840 $this->unit_frequency = $unit;
1845 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
1867 public function setNextDate($date, $increment_nb_gen_done = 0, $notrigger = 0)
1872 if (!$this->table_element) {
1873 dol_syslog(get_class($this).
"::setNextDate was called on objet with property table_element not defined", LOG_ERR);
1876 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1877 $sql .=
" SET date_when = ".($date ?
"'".$this->db->idate($date).
"'" :
"null");
1878 if ($increment_nb_gen_done > 0) {
1879 $sql .=
', nb_gen_done = nb_gen_done + 1';
1881 $sql .=
" WHERE rowid = ".((int) $this->
id);
1883 dol_syslog(get_class($this).
"::setNextDate", LOG_DEBUG);
1884 if ($this->
db->query($sql)) {
1885 $this->date_when = $date;
1886 if ($increment_nb_gen_done > 0) {
1887 $this->nb_gen_done++;
1892 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
1917 if (!$this->table_element) {
1918 dol_syslog(get_class($this).
"::setMaxPeriod was called on objet with property table_element not defined", LOG_ERR);
1926 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1927 $sql .=
' SET nb_gen_max = '.((int) $nb);
1928 $sql .=
" WHERE rowid = ".((int) $this->
id);
1930 dol_syslog(get_class($this).
"::setMaxPeriod", LOG_DEBUG);
1931 if ($this->
db->query($sql)) {
1932 $this->nb_gen_max = $nb;
1936 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
1961 if (!$this->table_element) {
1962 dol_syslog(get_class($this).
"::setAutoValidate was called on objet with property table_element not defined", LOG_ERR);
1966 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
1967 $sql .=
' SET auto_validate = '.((int) $validate);
1968 $sql .=
" WHERE rowid = ".((int) $this->
id);
1970 dol_syslog(get_class($this).
"::setAutoValidate", LOG_DEBUG);
1971 if ($this->
db->query($sql)) {
1972 $this->auto_validate = $validate;
1976 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
2001 if (!$this->table_element) {
2002 dol_syslog(get_class($this).
"::setGeneratePdf was called on objet with property table_element not defined", LOG_ERR);
2006 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2007 $sql .=
' SET generate_pdf = '.((int) $validate);
2008 $sql .=
" WHERE rowid = ".((int) $this->
id);
2010 dol_syslog(get_class($this).
"::setGeneratePdf", LOG_DEBUG);
2011 if ($this->
db->query($sql)) {
2012 $this->generate_pdf = $validate;
2016 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
2040 if (!$this->table_element) {
2041 dol_syslog(get_class($this).
"::setModelPdf was called on objet with property table_element not defined", LOG_ERR);
2045 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
2046 $sql .=
" SET modelpdf = '".$this->db->escape($model).
"'";
2047 $sql .=
" WHERE rowid = ".((int) $this->
id);
2049 dol_syslog(get_class($this).
"::setModelPdf", LOG_DEBUG);
2050 if ($this->
db->query($sql)) {
2051 $this->model_pdf = $model;
2055 $result = $this->
call_trigger(
'BILLREC_MODIFY', $user);
2081 public $element =
'facturedetrec';
2086 public $table_element =
'facturedet_rec';
2089 public $fk_product_fournisseur_price;
2090 public $fk_fournprice;
2095 public $description;
2097 public $fk_product_type;
2099 public $fk_contract_line;
2109 public function delete(
User $user, $notrigger =
false)
2118 $result = $this->
call_trigger(
'LINEBILLREC_DELETE', $user);
2134 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.$this->table_element.
' WHERE rowid='.((int) $this->
id);
2136 $res = $this->
db->query($sql);
2139 $this->errors[] = $this->
db->lasterror();
2145 $this->
db->rollback();
2148 $this->
db->commit();
2162 $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,';
2163 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise, l.remise_percent, l.subprice,';
2164 $sql .=
' l.date_start_fill, l.date_end_fill, l.info_bits, l.total_ht, l.total_tva, l.total_ttc,';
2165 $sql .=
' l.rang, l.special_code,';
2166 $sql .=
' l.fk_unit, l.fk_contract_line,';
2167 $sql .=
' l.import_key, l.fk_multicurrency,';
2168 $sql .=
' l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2169 $sql .=
' l.buy_price_ht, l.fk_product_fournisseur_price,';
2170 $sql .=
' l.fk_user_author, l.fk_user_modif,';
2171 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2172 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet_rec as l';
2173 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
2174 $sql .=
' WHERE l.rowid = '.((int) $rowid);
2175 $sql .=
' ORDER BY l.rang';
2178 $result = $this->
db->query($sql);
2180 $objp = $this->
db->fetch_object($result);
2182 $this->
id = $objp->rowid;
2183 $this->label = $objp->custom_label;
2184 $this->desc = $objp->description;
2186 $this->product_type = $objp->product_type;
2187 $this->
ref = $objp->product_ref;
2188 $this->product_ref = $objp->product_ref;
2189 $this->libelle = $objp->product_label;
2190 $this->product_label = $objp->product_label;
2191 $this->product_desc = $objp->product_desc;
2192 $this->fk_product_type = $objp->fk_product_type;
2193 $this->qty = $objp->qty;
2194 $this->
price = $objp->price;
2195 $this->subprice = $objp->subprice;
2196 $this->fk_facture = $objp->fk_facture;
2197 $this->vat_src_code = $objp->vat_src_code;
2198 $this->tva_tx = $objp->tva_tx;
2199 $this->localtax1_tx = $objp->localtax1_tx;
2200 $this->localtax2_tx = $objp->localtax2_tx;
2201 $this->localtax1_type = $objp->localtax1_type;
2202 $this->localtax2_type = $objp->localtax2_type;
2203 $this->remise_percent = $objp->remise_percent;
2205 $this->fk_product = $objp->fk_product;
2206 $this->date_start_fill = $objp->date_start_fill;
2207 $this->date_end_fill = $objp->date_end_fill;
2208 $this->info_bits = $objp->info_bits;
2209 $this->total_ht = $objp->total_ht;
2210 $this->total_tva = $objp->total_tva;
2211 $this->total_ttc = $objp->total_ttc;
2213 $this->rang = $objp->rang;
2214 $this->special_code = $objp->special_code;
2215 $this->fk_unit = $objp->fk_unit;
2216 $this->fk_contract_line = $objp->fk_contract_line;
2217 $this->import_key = $objp->import_key;
2218 $this->fk_multicurrency = $objp->fk_multicurrency;
2219 $this->multicurrency_code = $objp->multicurrency_code;
2220 $this->multicurrency_subprice = $objp->multicurrency_subprice;
2221 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
2222 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
2223 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2225 $this->buy_price_ht = $objp->buy_price_ht;
2227 $this->fk_product_fournisseur_price = $objp->fk_product_fournisseur_price;
2228 $this->fk_user_author = $objp->fk_user_author;
2229 $this->fk_user_modif = $objp->fk_user_modif;
2231 $this->
db->free($result);
2234 $this->error = $this->
db->lasterror();
2253 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2255 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet_rec SET";
2256 $sql .=
" fk_facture = ".((int) $this->fk_facture);
2257 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
2258 $sql .=
", description='".$this->db->escape($this->desc).
"'";
2259 $sql .=
", price=".price2num($this->
price);
2260 $sql .=
", qty=".price2num($this->qty);
2261 $sql .=
", tva_tx=".price2num($this->tva_tx);
2262 $sql .=
", vat_src_code='".$this->db->escape($this->vat_src_code).
"'";
2263 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
2264 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
2265 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
2266 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
2267 $sql .=
", fk_product=".($this->fk_product > 0 ? $this->fk_product :
"null");
2268 $sql .=
", product_type=".((int) $this->product_type);
2269 $sql .=
", remise_percent=".price2num($this->remise_percent);
2270 $sql .=
", subprice=".price2num($this->subprice);
2271 $sql .=
", info_bits=".price2num($this->info_bits);
2272 $sql .=
", date_start_fill=".(int) $this->date_start_fill;
2273 $sql .=
", date_end_fill=".(int) $this->date_end_fill;
2274 if (empty($this->skip_update_total)) {
2275 $sql .=
", total_ht=".price2num($this->total_ht);
2276 $sql .=
", total_tva=".price2num($this->total_tva);
2277 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
2278 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
2279 $sql .=
", total_ttc=".price2num($this->total_ttc);
2281 $sql .=
", rang=".((int) $this->rang);
2282 $sql .=
", special_code=".((int) $this->special_code);
2283 $sql .=
", fk_unit=".($this->fk_unit ?
"'".$this->db->escape($this->fk_unit).
"'" :
"null");
2284 $sql .=
", fk_contract_line=".($this->fk_contract_line ? $this->fk_contract_line :
"null");
2285 $sql .=
" WHERE rowid = ".((int) $this->
id);
2289 dol_syslog(get_class($this).
"::updateline", LOG_DEBUG);
2300 if (!$error && !$notrigger) {
2302 $result = $this->
call_trigger(
'LINEBILLREC_MODIFY', $user);
2310 $this->
db->rollback();
2313 $this->
db->commit();
2317 $this->error = $this->
db->lasterror();
2318 $this->
db->rollback();