43 require_once DOL_DOCUMENT_ROOT.
'/core/class/commoninvoice.class.php';
44 require_once DOL_DOCUMENT_ROOT.
'/core/class/commonobjectline.class.php';
45 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
46 require_once DOL_DOCUMENT_ROOT.
'/societe/class/client.class.php';
47 require_once DOL_DOCUMENT_ROOT.
'/margin/lib/margins.lib.php';
48 require_once DOL_DOCUMENT_ROOT.
'/multicurrency/class/multicurrency.class.php';
51 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formaccounting.class.php';
54 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
65 public $element =
'facture';
70 public $table_element =
'facture';
75 public $table_element_line =
'facturedet';
80 public $fk_element =
'fk_facture';
85 public $picto =
'bill';
91 public $ismultientitymanaged = 1;
97 public $restrictiononfksoc = 1;
127 public $fk_user_author;
139 public $fk_user_valid;
146 public $user_modification;
151 public $fk_user_modif;
161 public $date_livraison;
166 public $delivery_date;
178 public $ref_customer;
182 public $remise_absolue;
191 public $total_localtax1;
192 public $total_localtax2;
194 public $revenuestamp;
224 public $linked_objects = array();
226 public $date_lim_reglement;
227 public $cond_reglement_code;
228 public $cond_reglement_doc;
229 public $mode_reglement_code;
239 public $lines = array();
245 public $extraparams = array();
252 public $date_pointoftax;
258 public $fk_multicurrency;
260 public $multicurrency_code;
261 public $multicurrency_tx;
262 public $multicurrency_total_ht;
263 public $multicurrency_total_tva;
264 public $multicurrency_total_ttc;
269 public $situation_cycle_ref;
274 public $situation_counter;
279 public $situation_final;
284 public $tab_previous_situation_invoice = array();
289 public $tab_next_situation_invoice = array();
296 public $retained_warranty;
301 public $retained_warranty_date_limit;
306 public $retained_warranty_fk_cond_reglement;
338 public $fields = array(
339 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>1),
340 'ref' =>array(
'type'=>
'varchar(30)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>5),
341 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
342 'ref_client' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>10),
343 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref ext',
'enabled'=>1,
'visible'=>0,
'position'=>12),
344 'type' =>array(
'type'=>
'smallint(6)',
'label'=>
'Type',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>15),
346 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>50),
347 'datef' =>array(
'type'=>
'date',
'label'=>
'DateInvoice',
'enabled'=>1,
'visible'=>1,
'position'=>20),
348 'date_valid' =>array(
'type'=>
'date',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>22),
349 'date_lim_reglement' =>array(
'type'=>
'date',
'label'=>
'DateDue',
'enabled'=>1,
'visible'=>1,
'position'=>25),
350 'date_closing' =>array(
'type'=>
'datetime',
'label'=>
'Date closing',
'enabled'=>1,
'visible'=>-1,
'position'=>30),
351 'paye' =>array(
'type'=>
'smallint(6)',
'label'=>
'InvoicePaidCompletely',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>80),
354 'remise_absolue' =>array(
'type'=>
'double',
'label'=>
'CustomerRelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>91),
356 'close_code' =>array(
'type'=>
'varchar(16)',
'label'=>
'EarlyClosingReason',
'enabled'=>1,
'visible'=>-1,
'position'=>92),
357 'close_note' =>array(
'type'=>
'varchar(128)',
'label'=>
'EarlyClosingComment',
'enabled'=>1,
'visible'=>-1,
'position'=>93),
358 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'AmountHT',
'enabled'=>1,
'visible'=>1,
'position'=>95,
'isameasure'=>1),
359 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'AmountVAT',
'enabled'=>1,
'visible'=>-1,
'position'=>100,
'isameasure'=>1),
360 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'LT1',
'enabled'=>1,
'visible'=>-1,
'position'=>110,
'isameasure'=>1),
361 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'LT2',
'enabled'=>1,
'visible'=>-1,
'position'=>120,
'isameasure'=>1),
362 'revenuestamp' =>array(
'type'=>
'double(24,8)',
'label'=>
'RevenueStamp',
'enabled'=>1,
'visible'=>-1,
'position'=>115,
'isameasure'=>1),
363 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'AmountTTC',
'enabled'=>1,
'visible'=>1,
'position'=>130,
'isameasure'=>1),
364 'fk_facture_source' =>array(
'type'=>
'integer',
'label'=>
'SourceInvoice',
'enabled'=>1,
'visible'=>-1,
'position'=>170),
365 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:(fk_statut:=:1)',
'label'=>
'Project',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
366 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'Fk account',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
367 'fk_currency' =>array(
'type'=>
'varchar(3)',
'label'=>
'CurrencyCode',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
368 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>190),
369 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
370 'note_private' =>array(
'type'=>
'html',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>205),
371 'note_public' =>array(
'type'=>
'html',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>210),
372 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>215),
373 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
374 'situation_cycle_ref' =>array(
'type'=>
'smallint(6)',
'label'=>
'Situation cycle ref',
'enabled'=>
'$conf->global->INVOICE_USE_SITUATION',
'visible'=>-1,
'position'=>230),
375 'situation_counter' =>array(
'type'=>
'smallint(6)',
'label'=>
'Situation counter',
'enabled'=>
'$conf->global->INVOICE_USE_SITUATION',
'visible'=>-1,
'position'=>235),
376 'situation_final' =>array(
'type'=>
'smallint(6)',
'label'=>
'Situation final',
'enabled'=>
'empty($conf->global->INVOICE_USE_SITUATION) ? 0 : 1',
'visible'=>-1,
'position'=>240),
377 'retained_warranty' =>array(
'type'=>
'double',
'label'=>
'Retained warranty',
'enabled'=>
'$conf->global->INVOICE_USE_RETAINED_WARRANTY',
'visible'=>-1,
'position'=>245),
378 'retained_warranty_date_limit' =>array(
'type'=>
'date',
'label'=>
'Retained warranty date limit',
'enabled'=>
'$conf->global->INVOICE_USE_RETAINED_WARRANTY',
'visible'=>-1,
'position'=>250),
379 'retained_warranty_fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'Retained warranty fk cond reglement',
'enabled'=>
'$conf->global->INVOICE_USE_RETAINED_WARRANTY',
'visible'=>-1,
'position'=>255),
380 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>260),
381 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLabel',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>265),
382 'date_pointoftax' =>array(
'type'=>
'date',
'label'=>
'DatePointOfTax',
'enabled'=>
'$conf->global->INVOICE_POINTOFTAX_DATE',
'visible'=>-1,
'position'=>270),
383 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'MulticurrencyID',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>275),
384 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'Currency',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>280),
385 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'CurrencyRate',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>285,
'isameasure'=>1),
386 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>290,
'isameasure'=>1),
387 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>291,
'isameasure'=>1),
388 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'isModEnabled("multicurrency")',
'visible'=>-1,
'position'=>292,
'isameasure'=>1),
389 'fk_fac_rec_source' =>array(
'type'=>
'integer',
'label'=>
'RecurringInvoiceSource',
'enabled'=>1,
'visible'=>-1,
'position'=>305),
390 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>-1,
'position'=>310),
391 'module_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSModule',
'enabled'=>1,
'visible'=>-1,
'position'=>315),
392 'pos_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSTerminal',
'enabled'=>1,
'visible'=>-1,
'position'=>320),
393 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
394 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModificationShort',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>502),
395 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'position'=>506),
396 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-1,
'notnull'=>-1,
'position'=>508),
397 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>510),
398 'fk_user_closing' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>512),
399 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>900),
400 'fk_statut' =>array(
'type'=>
'smallint(6)',
'label'=>
'Status',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'position'=>1000,
'arrayofkeyval'=>array(0=>
'Draft', 1=>
'Validated', 2=>
'Paid', 3=>
'Abandonned')),
462 const CLOSECODE_DISCOUNTVAT =
'discount_vat';
463 const CLOSECODE_BADDEBT =
'badcustomer';
464 const CLOSECODE_BANKCHARGE =
'bankcharge';
465 const CLOSECODE_OTHER =
'other';
467 const CLOSECODE_ABANDONED =
'abandon';
468 const CLOSECODE_REPLACED =
'replaced';
491 public function create(
User $user, $notrigger = 0, $forceduedate = 0)
493 global $langs, $conf, $mysoc, $hookmanager;
497 if (empty($this->
type)) {
501 $this->ref_client = trim($this->ref_client);
503 $this->note = (isset($this->note) ? trim($this->note) : trim($this->note_private));
504 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note_private));
505 $this->note_public = trim($this->note_public);
506 if (!$this->cond_reglement_id) {
507 $this->cond_reglement_id = 0;
509 if (!$this->mode_reglement_id) {
510 $this->mode_reglement_id = 0;
512 $this->brouillon = 1;
516 if (!empty($this->multicurrency_code)) {
518 if (empty($this->multicurrency_tx)) {
526 $this->fk_multicurrency = 0;
528 if (empty($this->fk_multicurrency)) {
529 $this->multicurrency_code = $conf->currency;
530 $this->fk_multicurrency = 0;
531 $this->multicurrency_tx = 1;
534 dol_syslog(get_class($this).
"::create user=".$user->id.
" date=".$this->date);
537 if (empty($this->date)) {
538 $this->error =
"Try to create an invoice with an empty parameter (date)";
539 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
543 $result = $soc->fetch($this->socid);
545 $this->error =
"Failed to fetch company: ".$soc->error;
546 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
554 $originaldatewhen =
null;
555 $nextdatewhen =
null;
556 $previousdaynextdatewhen =
null;
559 if ($this->fac_rec > 0) {
560 $this->fk_fac_rec_source = $this->fac_rec;
562 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
564 $result = $_facrec->fetch($this->fac_rec);
565 $result = $_facrec->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
568 $originaldatewhen = $_facrec->date_when;
569 $nextdatewhen =
null; $previousdaynextdatewhen =
null;
570 if ($originaldatewhen) {
571 $nextdatewhen =
dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency);
575 if (!empty($_facrec->frequency)) {
576 $this->socid = $_facrec->socid;
578 $this->entity = $_facrec->entity;
581 $this->fk_project =
GETPOST(
'projectid',
'int') > 0 ? ((int)
GETPOST(
'projectid',
'int')) : $_facrec->fk_project;
582 $this->note_public =
GETPOSTISSET(
'note_public') ?
GETPOST(
'note_public',
'restricthtml') : $_facrec->note_public;
583 $this->note_private =
GETPOSTISSET(
'note_private') ?
GETPOST(
'note_private',
'restricthtml') : $_facrec->note_private;
584 $this->model_pdf =
GETPOSTISSET(
'model') ?
GETPOST(
'model',
'alpha') : $_facrec->model_pdf;
585 $this->cond_reglement_id =
GETPOST(
'cond_reglement_id',
'int') > 0 ? ((int)
GETPOST(
'cond_reglement_id',
'int')) : $_facrec->cond_reglement_id;
586 $this->mode_reglement_id =
GETPOST(
'mode_reglement_id',
'int') > 0 ? ((int)
GETPOST(
'mode_reglement_id',
'int')) : $_facrec->mode_reglement_id;
587 $this->fk_account =
GETPOST(
'fk_account') > 0 ? ((int)
GETPOST(
'fk_account')) : $_facrec->fk_account;
590 $this->total_ht = $_facrec->total_ht;
591 $this->total_ttc = $_facrec->total_ttc;
594 $this->remise_absolue = $_facrec->remise_absolue;
595 $this->remise_percent = $_facrec->remise_percent;
596 $this->fk_incoterms = $_facrec->fk_incoterms;
597 $this->location_incoterms = $_facrec->location_incoterms;
603 $this->ref_client = trim($this->ref_client);
604 $this->ref_customer = trim($this->ref_customer);
605 $this->note_public = trim($this->note_public);
606 $this->note_private = trim($this->note_private);
607 $this->note_private =
dol_concatdesc($this->note_private, $langs->trans(
"GeneratedFromRecurringInvoice", $_facrec->ref));
609 $this->array_options = $_facrec->array_options;
611 if (!$this->mode_reglement_id) {
612 $this->mode_reglement_id = 0;
614 $this->brouillon = 1;
618 $this->linked_objects = $_facrec->linkedObjectsIds;
623 if ($_facrec->frequency > 0) {
624 dol_syslog(
"This is a recurring invoice so we set date_last_gen and next date_when");
625 if (empty($_facrec->date_when)) {
626 $_facrec->date_when = $now;
628 $next_date = $_facrec->getNextDate();
629 $result = $_facrec->setValueFrom(
'date_last_gen', $now,
'',
null,
'date',
'', $user,
'');
631 $result = $_facrec->setNextDate($next_date, 1);
635 $outputlangs = $langs;
638 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && empty($newlang) && isset($this->thirdparty->default_lang)) {
639 $newlang = $this->thirdparty->default_lang;
641 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && empty($newlang) && isset($this->default_lang)) {
642 $newlang = $this->default_lang;
644 if (!empty($newlang)) {
646 $outputlangs->setDefaultLang($newlang);
652 $substitutionarray[
'__INVOICE_MONTH__'] =
dol_print_date($this->date,
'%m');
655 $substitutionarray[
'__INVOICE_MONTH_TEXT__'] =
dol_print_date($this->date,
'%B');
658 $substitutionarray[
'__INVOICE_YEAR__'] =
dol_print_date($this->date,
'%Y');
661 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] = (isset($originaldatewhen) ?
dol_print_date($originaldatewhen,
'dayhour') :
'');
662 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] = (isset($nextdatewhen) ?
dol_print_date($nextdatewhen,
'dayhour') :
'');
663 $substitutionarray[
'__INVOICE_PREVIOUS_DATE_NEXT_INVOICE_AFTER_GEN__'] = (isset($previousdaynextdatewhen) ?
dol_print_date($previousdaynextdatewhen,
'dayhour') :
'');
664 $substitutionarray[
'__INVOICE_COUNTER_CURRENT__'] = $_facrec->nb_gen_done;
665 $substitutionarray[
'__INVOICE_COUNTER_MAX__'] = $_facrec->nb_gen_max;
676 if (empty($forceduedate)) {
682 $this->date_lim_reglement = $duedate;
684 $this->date_lim_reglement = $forceduedate;
688 $socid = $this->socid;
690 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture (";
697 $sql .=
", remise_absolue";
698 $sql .=
", remise_percent";
700 $sql .=
", date_pointoftax";
701 $sql .=
", note_private";
702 $sql .=
", note_public";
703 $sql .=
", ref_client";
704 $sql .=
", fk_account";
705 $sql .=
", module_source, pos_source, fk_fac_rec_source, fk_facture_source, fk_user_author, fk_projet";
706 $sql .=
", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf";
707 $sql .=
", situation_cycle_ref, situation_counter, situation_final";
708 $sql .=
", fk_incoterms, location_incoterms";
709 $sql .=
", fk_multicurrency";
710 $sql .=
", multicurrency_code";
711 $sql .=
", multicurrency_tx";
712 $sql .=
", retained_warranty";
713 $sql .=
", retained_warranty_date_limit";
714 $sql .=
", retained_warranty_fk_cond_reglement";
718 $sql .=
", ".setEntity($this);
719 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
720 $sql .=
", '".$this->db->escape($this->
type).
"'";
721 $sql .=
", ".((int) $socid);
722 $sql .=
", '".$this->db->idate($now).
"'";
723 $sql .=
", ".($this->remise_absolue > 0 ? $this->remise_absolue :
'NULL');
724 $sql .=
", ".($this->remise_percent > 0 ? $this->remise_percent :
'NULL');
725 $sql .=
", '".$this->db->idate($this->date).
"'";
726 $sql .=
", ".(empty($this->date_pointoftax) ?
"null" :
"'".$this->db->idate($this->date_pointoftax).
"'");
727 $sql .=
", ".($this->note_private ?
"'".$this->db->escape($this->note_private).
"'" :
"null");
728 $sql .=
", ".($this->note_public ?
"'".$this->db->escape($this->note_public).
"'" :
"null");
729 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
730 $sql .=
", ".($this->fk_account > 0 ? $this->fk_account :
'NULL');
731 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
732 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
733 $sql .=
", ".($this->fk_fac_rec_source ?
"'".$this->db->escape($this->fk_fac_rec_source).
"'" :
"null");
734 $sql .=
", ".($this->fk_facture_source ?
"'".$this->db->escape($this->fk_facture_source).
"'" :
"null");
735 $sql .=
", ".($user->id > 0 ? (int) $user->id :
"null");
736 $sql .=
", ".($this->fk_project ? $this->fk_project :
"null");
737 $sql .=
", ".((int) $this->cond_reglement_id);
738 $sql .=
", ".((int) $this->mode_reglement_id);
739 $sql .=
", '".$this->db->idate($this->date_lim_reglement).
"'";
740 $sql .=
", ".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null");
741 $sql .=
", ".($this->situation_cycle_ref ?
"'".$this->db->escape($this->situation_cycle_ref).
"'" :
"null");
742 $sql .=
", ".($this->situation_counter ?
"'".$this->db->escape($this->situation_counter).
"'" :
"null");
743 $sql .=
", ".($this->situation_final ? $this->situation_final : 0);
744 $sql .=
", ".(int) $this->fk_incoterms;
745 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
746 $sql .=
", ".(int) $this->fk_multicurrency;
747 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
748 $sql .=
", ".(double) $this->multicurrency_tx;
749 $sql .=
", ".(empty($this->retained_warranty) ?
"0" : $this->db->escape($this->retained_warranty));
750 $sql .=
", ".(!empty($this->retained_warranty_date_limit) ?
"'".$this->db->idate($this->retained_warranty_date_limit).
"'" :
'NULL');
751 $sql .=
", ".(int) $this->retained_warranty_fk_cond_reglement;
754 $resql = $this->db->query(
$sql);
756 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facture');
759 $this->
ref =
'(PROV'.$this->id.
')';
760 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"facture SET ref='".$this->db->escape($this->
ref).
"' WHERE rowid=".((int) $this->
id);
762 $resql = $this->db->query(
$sql);
767 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
768 $this->linked_objects = $this->linkedObjectsIds;
772 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
773 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
774 if (is_array($tmp_origin_id)) {
775 foreach ($tmp_origin_id as $origin_id) {
778 $this->error = $this->db->lasterror();
784 $origin_id = $tmp_origin_id;
787 $this->error = $this->db->lasterror();
795 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
796 $originforcontact = $this->origin;
797 $originidforcontact = $this->origin_id;
798 if ($originforcontact ==
'shipping') {
799 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
801 $exp->fetch($this->origin_id);
802 $exp->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
803 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
804 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
805 $originforcontact =
'commande';
806 if (is_object($value)) {
807 $originidforcontact = $value->id;
809 $originidforcontact = $value;
816 $sqlcontact =
"SELECT ctc.code, ctc.source, ec.fk_socpeople FROM ".MAIN_DB_PREFIX.
"element_contact as ec, ".MAIN_DB_PREFIX.
"c_type_contact as ctc";
817 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->db->escape($originforcontact).
"'";
819 $resqlcontact = $this->db->query($sqlcontact);
821 while ($objcontact = $this->db->fetch_object($resqlcontact)) {
823 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
833 if (!$error && empty($this->fac_rec) && count($this->lines) && is_object($this->lines[0])) {
836 dol_syslog(
"There is ".count($this->lines).
" lines that are invoice lines objects");
837 foreach ($this->lines as $i => $val) {
838 $newinvoiceline = $this->lines[$i];
840 $newinvoiceline->context = $this->context;
842 $newinvoiceline->fk_facture = $this->id;
844 $newinvoiceline->origin = $this->lines[$i]->element;
845 $newinvoiceline->origin_id = $this->lines[$i]->id;
848 if ($this->lines[$i]->date_start_fill == 1 && $originaldatewhen) {
849 $newinvoiceline->date_start = $originaldatewhen;
851 if ($this->lines[$i]->date_end_fill == 1 && $previousdaynextdatewhen) {
852 $newinvoiceline->date_end = $previousdaynextdatewhen;
857 if (($newinvoiceline->product_type != 9 && empty($newinvoiceline->fk_parent_line)) || $newinvoiceline->product_type == 9) {
862 $vatrate = $newinvoiceline->tva_tx;
863 if ($newinvoiceline->vat_src_code && ! preg_match(
'/\(.*\)/', $vatrate)) $vatrate.=
' ('.$newinvoiceline->vat_src_code.
')';
865 $newinvoiceline->fk_parent_line = $fk_parent_line;
869 $discount->fetch($newinvoiceline->fk_remise_except);
871 $discountId = $soc->set_remise_except($discount->amount_ht, $user, $discount->description, $discount->tva_tx);
872 $newinvoiceline->fk_remise_except = $discountId;
876 $newinvoiceline->desc,
877 $newinvoiceline->subprice,
878 $newinvoiceline->qty,
880 $newinvoiceline->localtax1_tx,
881 $newinvoiceline->localtax2_tx,
882 $newinvoiceline->fk_product,
883 $newinvoiceline->remise_percent,
884 $newinvoiceline->date_start,
885 $newinvoiceline->date_end,
886 $newinvoiceline->fk_code_ventilation,
887 $newinvoiceline->info_bits,
888 $newinvoiceline->fk_remise_except,
891 $newinvoiceline->product_type,
892 $newinvoiceline->rang,
893 $newinvoiceline->special_code,
894 $newinvoiceline->element,
897 $newinvoiceline->fk_fournprice,
898 $newinvoiceline->pa_ht,
899 $newinvoiceline->label,
900 $newinvoiceline->array_options,
901 $newinvoiceline->situation_percent,
902 $newinvoiceline->fk_prev_id,
903 $newinvoiceline->fk_unit,
904 $newinvoiceline->multicurrency_subprice,
905 $newinvoiceline->ref_ext,
910 if ($result > 0 && $newinvoiceline->product_type == 9) {
911 $fk_parent_line = $result;
915 $this->error = $newinvoiceline->error;
916 $this->errors = $newinvoiceline->errors;
921 } elseif (!$error && empty($this->fac_rec)) {
924 dol_syslog(
"There is ".count($this->lines).
" lines that are array lines");
926 foreach ($this->lines as $i => $val) {
927 $line = $this->lines[$i];
931 if (!is_object($line)) {
932 $line = (object) $line;
937 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
942 $vatrate = $line->tva_tx;
943 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) {
944 $vatrate .=
' ('.$line->vat_src_code.
')';
947 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
948 $originid = $line->origin_id;
949 $origintype = $line->origin;
951 $originid = $line->id;
952 $origintype = $this->element;
956 if (empty($line->ref_ext)) {
968 $line->remise_percent,
971 $line->fk_code_ventilation,
973 $line->fk_remise_except,
982 $line->fk_fournprice,
985 $line->array_options,
986 $line->situation_percent,
989 $line->multicurrency_subprice,
994 $this->error = $this->db->lasterror();
996 $this->db->rollback();
1001 if ($result > 0 && $line->product_type == 9) {
1002 $fk_parent_line = $result;
1011 if (!$error && $this->fac_rec > 0) {
1012 foreach ($_facrec->lines as $i => $val) {
1013 if ($_facrec->lines[$i]->fk_product) {
1014 $prod =
new Product($this->db);
1015 $res = $prod->fetch($_facrec->lines[$i]->fk_product);
1026 $tva_tx = $_facrec->lines[$i]->tva_tx.($_facrec->lines[$i]->vat_src_code ?
'('.$_facrec->lines[$i]->vat_src_code.
')' :
'');
1027 $tva_npr = $_facrec->lines[$i]->info_bits;
1028 if (empty($tva_tx)) {
1031 $localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
1032 $localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
1034 $fk_product_fournisseur_price = empty($_facrec->lines[$i]->fk_product_fournisseur_price) ? null : $_facrec->lines[$i]->fk_product_fournisseur_price;
1035 $buyprice = empty($_facrec->lines[$i]->buyprice) ? 0 : $_facrec->lines[$i]->buyprice;
1038 if (!$buyprice && $_facrec->lines[$i]->fk_product > 0) {
1039 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
1041 $producttmp->fetch($_facrec->lines[$i]->fk_product);
1046 if ($conf->global->MARGIN_TYPE ==
'costprice' && !empty($producttmp->cost_price)) {
1047 $buyprice = $producttmp->cost_price;
1048 } elseif (
isModEnabled(
'stock') && ($conf->global->MARGIN_TYPE ==
'costprice' || $conf->global->MARGIN_TYPE ==
'pmp') && !empty($producttmp->pmp)) {
1049 $buyprice = $producttmp->pmp;
1051 if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0) {
1052 if ($producttmp->product_fourn_price_id > 0) {
1053 $buyprice =
price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent / 100) + $producttmp->fourn_remise,
'MU');
1059 $result_insert = $this->
addline(
1060 $_facrec->lines[$i]->desc,
1061 $_facrec->lines[$i]->subprice,
1062 $_facrec->lines[$i]->qty,
1066 $_facrec->lines[$i]->fk_product,
1067 $_facrec->lines[$i]->remise_percent,
1068 ($_facrec->lines[$i]->date_start_fill == 1 && $originaldatewhen) ? $originaldatewhen :
'',
1069 ($_facrec->lines[$i]->date_end_fill == 1 && $previousdaynextdatewhen) ? $previousdaynextdatewhen :
'',
1075 $_facrec->lines[$i]->product_type,
1076 $_facrec->lines[$i]->rang,
1077 $_facrec->lines[$i]->special_code,
1081 $fk_product_fournisseur_price,
1083 $_facrec->lines[$i]->label,
1084 empty($_facrec->lines[$i]->array_options) ?
null:$_facrec->lines[$i]->array_options,
1085 $_facrec->lines[$i]->situation_percent,
1087 $_facrec->lines[$i]->fk_unit,
1088 $_facrec->lines[$i]->multicurrency_subprice,
1089 $_facrec->lines[$i]->ref_ext,
1093 if ($result_insert < 0) {
1095 $this->error = $this->db->error();
1114 if (!$error && !$notrigger) {
1124 $this->db->commit();
1127 $this->db->rollback();
1131 $this->error = $langs->trans(
'FailedToUpdatePrice');
1132 $this->db->rollback();
1136 dol_syslog(get_class($this).
"::create error ".$this->error, LOG_ERR);
1137 $this->db->rollback();
1141 $this->error = $this->db->error();
1142 $this->db->rollback();
1160 $facture =
new Facture($this->db);
1166 if (!empty($this->array_options)) {
1167 $facture->array_options = $this->array_options;
1170 foreach ($this->lines as &$line) {
1171 $line->fetch_optionals();
1175 $facture->type = $this->type;
1176 $facture->socid = $this->socid;
1177 $facture->date = $this->date;
1178 $facture->date_pointoftax = $this->date_pointoftax;
1179 $facture->note_public = $this->note_public;
1180 $facture->note_private = $this->note_private;
1181 $facture->ref_client = $this->ref_client;
1182 $facture->modelpdf = $this->model_pdf;
1183 $facture->model_pdf = $this->model_pdf;
1184 $facture->fk_project = $this->fk_project;
1185 $facture->cond_reglement_id = $this->cond_reglement_id;
1186 $facture->mode_reglement_id = $this->mode_reglement_id;
1187 $facture->remise_absolue = $this->remise_absolue;
1190 $facture->origin = $this->origin;
1191 $facture->origin_id = $this->origin_id;
1193 $facture->lines = $this->lines;
1194 $facture->situation_counter = $this->situation_counter;
1195 $facture->situation_cycle_ref = $this->situation_cycle_ref;
1196 $facture->situation_final = $this->situation_final;
1198 $facture->retained_warranty = $this->retained_warranty;
1199 $facture->retained_warranty_fk_cond_reglement = $this->retained_warranty_fk_cond_reglement;
1200 $facture->retained_warranty_date_limit = $this->retained_warranty_date_limit;
1202 $facture->fk_user_author = $user->id;
1206 foreach ($facture->lines as $i => $tmpline) {
1207 $facture->lines[$i]->fk_prev_id = $this->lines[$i]->rowid;
1208 if ($invertdetail) {
1209 $facture->lines[$i]->subprice = -$facture->lines[$i]->subprice;
1210 $facture->lines[$i]->total_ht = -$facture->lines[$i]->total_ht;
1211 $facture->lines[$i]->total_tva = -$facture->lines[$i]->total_tva;
1212 $facture->lines[$i]->total_localtax1 = -$facture->lines[$i]->total_localtax1;
1213 $facture->lines[$i]->total_localtax2 = -$facture->lines[$i]->total_localtax2;
1214 $facture->lines[$i]->total_ttc = -$facture->lines[$i]->total_ttc;
1215 $facture->lines[$i]->ref_ext =
'';
1219 dol_syslog(get_class($this).
"::createFromCurrent invertdetail=".$invertdetail.
" socid=".$this->socid.
" nboflines=".count($facture->lines));
1221 $facid = $facture->create($user);
1223 $this->error = $facture->error;
1224 $this->errors = $facture->errors;
1225 } elseif ($this->
type == self::TYPE_SITUATION && !empty($conf->global->INVOICE_USE_SITUATION)) {
1228 foreach ($this->linkedObjectsIds as $typeObject => $Tfk_object) {
1229 foreach ($Tfk_object as $fk_object) {
1230 $facture->add_object_linked($typeObject, $fk_object);
1234 $facture->add_object_linked(
'facture', $this->fk_facture_source);
1250 global $conf, $hookmanager;
1254 $object =
new Facture($this->db);
1258 $object->fetch($fromid);
1261 $objFrom = clone $object;
1264 if (!empty($this->socid) && $this->socid != $object->socid) {
1265 $objsoc =
new Societe($this->db);
1267 if ($objsoc->fetch($this->socid) > 0) {
1268 $object->socid = $objsoc->id;
1269 $object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1270 $object->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1271 $object->fk_project =
'';
1272 $object->fk_delivery_address =
'';
1283 $object->date = (empty($this->date) ?
dol_now() : $this->date);
1284 $object->user_author = $user->id;
1285 $object->user_valid =
null;
1286 $object->fk_user_author = $user->id;
1287 $object->fk_user_valid =
null;
1288 $object->fk_facture_source = 0;
1289 $object->date_creation =
'';
1290 $object->date_modification =
'';
1291 $object->date_validation =
'';
1292 $object->ref_client =
'';
1293 $object->close_code =
'';
1294 $object->close_note =
'';
1296 $object->note_private =
'';
1297 $object->note_public =
'';
1301 foreach ($object->lines as $i => $line) {
1302 if (($object->lines[$i]->info_bits & 0x02) == 0x02) {
1303 unset($object->lines[$i]);
1309 if (!empty($conf->global->INVOICE_AUTO_NEXT_MONTH_ON_LINES) && !empty($line->date_start) && !empty($line->date_end)) {
1321 if (
dol_mktime(0, 0, 0, $start[
'mon'], $start[
'mday'], $start[
'year'],
'gmt') == $first
1322 &&
dol_mktime(23, 59, 59, $end[
'mon'], $end[
'mday'], $end[
'year'],
'gmt') == $last) {
1326 $object->lines[$i]->date_start = $newFirst;
1327 $object->lines[$i]->date_end = $newLast;
1331 $object->lines[$i]->ref_ext =
'';
1335 $object->context[
'createfromclone'] =
'createfromclone';
1336 $result = $object->create($user);
1339 $this->error = $object->error;
1340 $this->errors = $object->errors;
1343 if ($object->copy_linked_contact($objFrom,
'internal') < 0) {
1345 $this->error = $object->error;
1346 $this->errors = $object->errors;
1347 } elseif ($object->socid == $objFrom->socid) {
1349 if ($object->copy_linked_contact($objFrom,
'external') < 0) {
1351 $this->error = $object->error;
1352 $this->errors = $object->errors;
1359 if (is_object($hookmanager)) {
1360 $parameters = array(
'objFrom'=>$objFrom);
1362 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $object, $action);
1370 unset($object->context[
'createfromclone']);
1374 $this->db->commit();
1377 $this->db->rollback();
1391 global $conf, $hookmanager;
1399 $num = count($object->lines);
1400 for ($i = 0; $i < $num; $i++) {
1403 $line->libelle = $object->lines[$i]->libelle;
1404 $line->label = $object->lines[$i]->label;
1405 $line->desc = $object->lines[$i]->desc;
1406 $line->subprice = $object->lines[$i]->subprice;
1407 $line->total_ht = $object->lines[$i]->total_ht;
1408 $line->total_tva = $object->lines[$i]->total_tva;
1409 $line->total_localtax1 = $object->lines[$i]->total_localtax1;
1410 $line->total_localtax2 = $object->lines[$i]->total_localtax2;
1411 $line->total_ttc = $object->lines[$i]->total_ttc;
1412 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1413 $line->tva_tx = $object->lines[$i]->tva_tx;
1414 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1415 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1416 $line->qty = $object->lines[$i]->qty;
1417 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1418 $line->remise_percent = $object->lines[$i]->remise_percent;
1419 $line->fk_product = $object->lines[$i]->fk_product;
1420 $line->info_bits = $object->lines[$i]->info_bits;
1421 $line->product_type = $object->lines[$i]->product_type;
1422 $line->rang = $object->lines[$i]->rang;
1423 $line->special_code = $object->lines[$i]->special_code;
1424 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1425 $line->fk_unit = $object->lines[$i]->fk_unit;
1426 $line->date_start = $object->lines[$i]->date_start;
1427 $line->date_end = $object->lines[$i]->date_end;
1430 $line->fk_multicurrency = $object->lines[$i]->fk_multicurrency;
1431 $line->multicurrency_code = $object->lines[$i]->multicurrency_code;
1432 $line->multicurrency_subprice = $object->lines[$i]->multicurrency_subprice;
1433 $line->multicurrency_total_ht = $object->lines[$i]->multicurrency_total_ht;
1434 $line->multicurrency_total_tva = $object->lines[$i]->multicurrency_total_tva;
1435 $line->multicurrency_total_ttc = $object->lines[$i]->multicurrency_total_ttc;
1437 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1438 $marginInfos =
getMarginInfos($object->lines[$i]->subprice, $object->lines[$i]->remise_percent, $object->lines[$i]->tva_tx, $object->lines[$i]->localtax1_tx, $object->lines[$i]->localtax2_tx, $object->lines[$i]->fk_fournprice, $object->lines[$i]->pa_ht);
1439 $line->pa_ht = $marginInfos[0];
1442 $object->lines[$i]->fetch_optionals();
1443 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1444 $line->array_options[$options_key] = $value;
1447 $this->lines[$i] = $line;
1450 $this->socid = $object->socid;
1451 $this->fk_project = $object->fk_project;
1452 $this->fk_account = $object->fk_account;
1453 $this->cond_reglement_id = $object->cond_reglement_id;
1454 $this->mode_reglement_id = $object->mode_reglement_id;
1455 $this->availability_id = $object->availability_id;
1456 $this->demand_reason_id = $object->demand_reason_id;
1457 $this->delivery_date = (empty($object->delivery_date) ? $object->date_livraison : $object->delivery_date);
1458 $this->date_livraison = $object->delivery_date;
1459 $this->fk_delivery_address = $object->fk_delivery_address;
1460 $this->contact_id = $object->contact_id;
1461 $this->ref_client = $object->ref_client;
1463 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1464 $this->note_private = $object->note_private;
1465 $this->note_public = $object->note_public;
1468 $this->module_source = $object->module_source;
1469 $this->pos_source = $object->pos_source;
1471 $this->origin = $object->element;
1472 $this->origin_id = $object->id;
1474 $this->fk_user_author = $user->id;
1477 $object->fetch_optionals();
1478 foreach ($object->array_options as $options_key => $value) {
1479 $this->array_options[$options_key] = $value;
1483 $this->linked_objects[$this->origin] = $this->origin_id;
1484 if (!empty($object->other_linked_objects) && is_array($object->other_linked_objects)) {
1485 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1488 $ret = $this->
create($user);
1492 $hookmanager->initHooks(array(
'invoicedao'));
1494 $parameters = array(
'objFrom'=>$object);
1496 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1522 global $conf, $hookmanager;
1530 $use_all_lines = empty($lines);
1531 $num = count($object->lines);
1532 for ($i = 0; $i < $num; $i++) {
1533 if (!$use_all_lines && !in_array($object->lines[$i]->id, $lines)) {
1539 $line->libelle = $object->lines[$i]->libelle;
1540 $line->label = $object->lines[$i]->label;
1541 $line->desc = $object->lines[$i]->desc;
1542 $line->subprice = $object->lines[$i]->subprice;
1543 $line->total_ht = $object->lines[$i]->total_ht;
1544 $line->total_tva = $object->lines[$i]->total_tva;
1545 $line->total_localtax1 = $object->lines[$i]->total_localtax1;
1546 $line->total_localtax2 = $object->lines[$i]->total_localtax2;
1547 $line->total_ttc = $object->lines[$i]->total_ttc;
1548 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1549 $line->tva_tx = $object->lines[$i]->tva_tx;
1550 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1551 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1552 $line->qty = $object->lines[$i]->qty;
1553 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1554 $line->remise_percent = $object->lines[$i]->remise_percent;
1555 $line->fk_product = $object->lines[$i]->fk_product;
1556 $line->info_bits = $object->lines[$i]->info_bits;
1557 $line->product_type = $object->lines[$i]->product_type;
1558 $line->rang = $object->lines[$i]->rang;
1559 $line->special_code = $object->lines[$i]->special_code;
1560 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1561 $line->fk_unit = $object->lines[$i]->fk_unit;
1562 $line->date_start = $object->lines[$i]->date_start;
1563 $line->date_end = $object->lines[$i]->date_end;
1566 $line->fk_multicurrency = $object->lines[$i]->fk_multicurrency;
1567 $line->multicurrency_code = $object->lines[$i]->multicurrency_code;
1568 $line->multicurrency_subprice = $object->lines[$i]->multicurrency_subprice;
1569 $line->multicurrency_total_ht = $object->lines[$i]->multicurrency_total_ht;
1570 $line->multicurrency_total_tva = $object->lines[$i]->multicurrency_total_tva;
1571 $line->multicurrency_total_ttc = $object->lines[$i]->multicurrency_total_ttc;
1573 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1574 $marginInfos =
getMarginInfos($object->lines[$i]->subprice, $object->lines[$i]->remise_percent, $object->lines[$i]->tva_tx, $object->lines[$i]->localtax1_tx, $object->lines[$i]->localtax2_tx, $object->lines[$i]->fk_fournprice, $object->lines[$i]->pa_ht);
1575 $line->pa_ht = $marginInfos[0];
1578 $object->lines[$i]->fetch_optionals();
1579 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1580 $line->array_options[$options_key] = $value;
1583 $this->lines[$i] = $line;
1586 $this->socid = $object->socid;
1587 $this->fk_project = $object->fk_project;
1588 $this->fk_account = $object->fk_account;
1589 $this->cond_reglement_id = $object->cond_reglement_id;
1590 $this->mode_reglement_id = $object->mode_reglement_id;
1591 $this->availability_id = $object->availability_id;
1592 $this->demand_reason_id = $object->demand_reason_id;
1593 $this->delivery_date = (empty($object->delivery_date) ? $object->date_livraison : $object->delivery_date);
1594 $this->date_livraison = $object->delivery_date;
1595 $this->fk_delivery_address = $object->fk_delivery_address;
1596 $this->contact_id = $object->contact_id;
1597 $this->ref_client = $object->ref_client;
1599 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1600 $this->note_private = $object->note_private;
1601 $this->note_public = $object->note_public;
1604 $this->module_source = $object->module_source;
1605 $this->pos_source = $object->pos_source;
1607 $this->origin = $object->element;
1608 $this->origin_id = $object->id;
1610 $this->fk_user_author = $user->id;
1613 $object->fetch_optionals();
1614 foreach ($object->array_options as $options_key => $value) {
1615 $this->array_options[$options_key] = $value;
1619 $this->linked_objects[$this->origin] = $this->origin_id;
1620 if (!empty($object->other_linked_objects) && is_array($object->other_linked_objects)) {
1621 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1624 $ret = $this->
create($user);
1628 $hookmanager->initHooks(array(
'invoicedao'));
1630 $parameters = array(
'objFrom'=>$object);
1632 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1662 global $conf, $langs, $hookmanager, $action;
1664 if (! in_array($origin->element, array(
'propal',
'commande'))) {
1665 $origin->error =
'ErrorCanOnlyAutomaticallyGenerateADepositFromProposalOrOrder';
1670 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'DateInvoice'));
1674 require_once DOL_DOCUMENT_ROOT .
'/core/lib/date.lib.php';
1676 if ($date > (
dol_get_last_hour(
dol_now(
'tzuserrel')) + (empty($conf->global->INVOICE_MAX_FUTURE_DELAY) ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
1677 $origin->error =
'ErrorDateIsInFuture';
1681 if ($payment_terms_id <= 0) {
1682 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'PaymentConditionsShort'));
1686 $payment_conditions_deposit_percent =
getDictionaryValue(
'c_payment_term',
'deposit_percent', $origin->cond_reglement_id);
1688 if (empty($payment_conditions_deposit_percent)) {
1689 $origin->error =
'ErrorPaymentConditionsNotEligibleToDepositCreation';
1693 if (empty($origin->deposit_percent)) {
1694 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'DepositPercent'));
1698 $deposit =
new self($origin->db);
1699 $deposit->socid = $origin->socid;
1701 $deposit->fk_project = $origin->fk_project;
1702 $deposit->ref_client = $origin->ref_client;
1703 $deposit->date = $date;
1704 $deposit->mode_reglement_id = $origin->mode_reglement_id;
1705 $deposit->cond_reglement_id = $payment_terms_id;
1706 $deposit->availability_id = $origin->availability_id;
1707 $deposit->demand_reason_id = $origin->demand_reason_id;
1708 $deposit->fk_account = $origin->fk_account;
1709 $deposit->fk_incoterms = $origin->fk_incoterms;
1710 $deposit->location_incoterms = $origin->location_incoterms;
1711 $deposit->fk_multicurrency = $origin->fk_multicurrency;
1712 $deposit->multicurrency_code = $origin->multicurrency_code;
1713 $deposit->multicurrency_tx = $origin->multicurrency_tx;
1714 $deposit->module_source = $origin->module_source;
1715 $deposit->pos_source = $origin->pos_source;
1716 $deposit->model_pdf =
'crabe';
1718 $modelByTypeConfName =
'FACTURE_ADDON_PDF_' . $deposit->type;
1720 if (!empty($conf->global->$modelByTypeConfName)) {
1721 $deposit->model_pdf = $conf->global->$modelByTypeConfName;
1722 } elseif (!empty($conf->global->FACTURE_ADDON_PDF)) {
1723 $deposit->model_pdf = $conf->global->FACTURE_ADDON_PDF;
1726 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1727 $deposit->note_private = $origin->note_private;
1728 $deposit->note_public = $origin->note_public;
1731 $deposit->origin = $origin->element;
1732 $deposit->origin_id = $origin->id;
1734 $origin->fetch_optionals();
1736 foreach ($origin->array_options as $extrakey => $value) {
1737 $deposit->array_options[$extrakey] = $value;
1740 $deposit->linked_objects[$deposit->origin] = $deposit->origin_id;
1742 foreach ($overrideFields as $key => $value) {
1743 $deposit->$key = $value;
1746 $deposit->context[
'createdepositfromorigin'] =
'createdepositfromorigin';
1748 $origin->db->begin();
1751 $createReturn = $deposit->create($user, $notrigger);
1753 if ($createReturn <= 0) {
1754 $origin->db->rollback();
1755 $origin->error = $deposit->error;
1756 $origin->errors = $deposit->errors;
1760 $amount_ttc_diff = 0;
1761 $amountdeposit = array();
1762 $descriptions = array();
1764 if (!empty($conf->global->MAIN_DEPOSIT_MULTI_TVA)) {
1765 $amount = $origin->total_ttc * ($origin->deposit_percent / 100);
1767 $TTotalByTva = array();
1768 foreach ($origin->lines as &$line) {
1769 if (!empty($line->special_code)) {
1772 $TTotalByTva[$line->tva_tx] += $line->total_ttc;
1773 $descriptions[$line->tva_tx] .=
'<li>' . (!empty($line->product_ref) ? $line->product_ref .
' - ' :
'');
1774 $descriptions[$line->tva_tx] .= (!empty($line->product_label) ? $line->product_label .
' - ' :
'');
1775 $descriptions[$line->tva_tx] .= $langs->trans(
'Qty') .
' : ' . $line->qty;
1776 $descriptions[$line->tva_tx] .=
' - ' . $langs->trans(
'TotalHT') .
' : ' .
price($line->total_ht) .
'</li>';
1779 foreach ($TTotalByTva as $tva => &$total) {
1780 $coef = $total / $origin->total_ttc;
1781 $am = $amount * $coef;
1782 $amount_ttc_diff += $am;
1783 $amountdeposit[$tva] += $am / (1 + $tva / 100);
1787 $lines = $origin->lines;
1788 $numlines = count($lines);
1789 for ($i = 0; $i < $numlines; $i++) {
1790 if (empty($lines[$i]->qty)) {
1793 if (!empty($lines[$i]->special_code)) {
1797 $totalamount += $lines[$i]->total_ht;
1798 $tva_tx = $lines[$i]->tva_tx;
1799 $amountdeposit[$tva_tx] += ($lines[$i]->total_ht * $origin->deposit_percent) / 100;
1800 $descriptions[$tva_tx] .=
'<li>' . (!empty($lines[$i]->product_ref) ? $lines[$i]->product_ref .
' - ' :
'');
1801 $descriptions[$tva_tx] .= (!empty($lines[$i]->product_label) ? $lines[$i]->product_label .
' - ' :
'');
1802 $descriptions[$tva_tx] .= $langs->trans(
'Qty') .
' : ' . $lines[$i]->qty;
1803 $descriptions[$tva_tx] .=
' - ' . $langs->trans(
'TotalHT') .
' : ' .
price($lines[$i]->total_ht) .
'</li>';
1806 if ($totalamount == 0) {
1807 $amountdeposit[0] = 0;
1810 $amount_ttc_diff = $amountdeposit[0];
1813 foreach ($amountdeposit as $tva => $amount) {
1814 if (empty($amount)) {
1818 $descline =
'(DEPOSIT) ('. $origin->deposit_percent .
'%) - '.$origin->ref;
1821 if (!empty($conf->global->INVOICE_DEPOSIT_VARIABLE_MODE_DETAIL_LINES_IN_DESCRIPTION) && !empty($descriptions[$tva])) {
1822 $descline .=
'<ul>' . $descriptions[$tva] .
'</ul>';
1825 $addlineResult = $deposit->addline(
1832 (empty($conf->global->INVOICE_PRODUCTID_DEPOSIT) ? 0 : $conf->global->INVOICE_PRODUCTID_DEPOSIT),
1852 if ($addlineResult < 0) {
1853 $origin->db->rollback();
1854 $origin->error = $deposit->error;
1855 $origin->errors = $deposit->errors;
1860 $diff = $deposit->total_ttc - $amount_ttc_diff;
1862 if (!empty($conf->global->MAIN_DEPOSIT_MULTI_TVA) && $diff != 0) {
1863 $deposit->fetch_lines();
1864 $subprice_diff = $deposit->lines[0]->subprice - $diff / (1 + $deposit->lines[0]->tva_tx / 100);
1866 $updatelineResult = $deposit->updateline(
1867 $deposit->lines[0]->id,
1868 $deposit->lines[0]->desc,
1870 $deposit->lines[0]->qty,
1871 $deposit->lines[0]->remise_percent,
1872 $deposit->lines[0]->date_start,
1873 $deposit->lines[0]->date_end,
1874 $deposit->lines[0]->tva_tx,
1878 $deposit->lines[0]->info_bits,
1879 $deposit->lines[0]->product_type,
1883 $deposit->lines[0]->pa_ht,
1884 $deposit->lines[0]->label,
1890 if ($updatelineResult < 0) {
1891 $origin->db->rollback();
1892 $origin->error = $deposit->error;
1893 $origin->errors = $deposit->errors;
1898 $hookmanager->initHooks(array(
'invoicedao'));
1900 $parameters = array(
'objFrom' => $origin);
1901 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $deposit, $action);
1904 $origin->db->rollback();
1905 $origin->error = $hookmanager->error;
1906 $origin->errors = $hookmanager->errors;
1910 if (!empty($autoValidateDeposit)) {
1911 $validateReturn = $deposit->validate($user,
'', 0, $notrigger);
1913 if ($validateReturn < 0) {
1914 $origin->db->rollback();
1915 $origin->error = $deposit->error;
1916 $origin->errors = $deposit->errors;
1921 unset($deposit->context[
'createdepositfromorigin']);
1923 $origin->db->commit();
1937 global $conf, $langs, $mysoc, $user;
1939 $langs->load(
'bills');
1942 $moretitle = $params[
'moretitle'] ??
'';
1943 $picto = $this->picto;
1944 if ($this->
type == self::TYPE_REPLACEMENT) {
1947 if ($this->
type == self::TYPE_CREDIT_NOTE) {
1950 if ($this->
type == self::TYPE_DEPOSIT) {
1954 if ($user->hasRight(
"facture",
"read")) {
1955 $datas[
'picto'] =
img_picto(
'', $picto).
' <u class="paddingrightonly">'.$langs->trans(
"Invoice").
'</u>';
1957 $datas[
'picto'] .=
' '.$this->getLibType(1);
1960 if (!empty($params[
'fromajaxtooltip']) && !isset($this->alreadypaid)) {
1964 if (isset($this->statut) && isset($this->alreadypaid)) {
1965 $datas[
'picto'] .=
' '.$this->getLibStatut(5, $this->alreadypaid);
1968 $datas[
'picto'] .=
' - '.$moretitle;
1970 if (!empty($this->
ref)) {
1971 $datas[
'ref'] =
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1973 if (!empty($this->ref_customer)) {
1974 $datas[
'refcustomer'] =
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.$this->ref_customer;
1976 if (!empty($this->date)) {
1977 $datas[
'date'] =
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
1979 if (!empty($this->total_ht)) {
1980 $datas[
'amountht'] =
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
1982 if (!empty($this->total_tva)) {
1983 $datas[
'amountvat'] =
'<br><b>'.$langs->trans(
'AmountVAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
1985 if (!empty($this->total_localtax1) && $this->total_localtax1 != 0) {
1987 $datas[
'amountlt1'] =
'<br><b>'.$langs->transcountry(
'AmountLT1', $mysoc->country_code).
':</b> '.
price($this->total_localtax1, 0, $langs, 0, -1, -1, $conf->currency);
1989 if (!empty($this->total_localtax2) && $this->total_localtax2 != 0) {
1990 $datas[
'amountlt2'] =
'<br><b>'.$langs->transcountry(
'AmountLT2', $mysoc->country_code).
':</b> '.
price($this->total_localtax2, 0, $langs, 0, -1, -1, $conf->currency);
1992 if (!empty($this->total_ttc)) {
1993 $datas[
'amountttc'] =
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
2014 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $addlinktonotes = 0, $save_lastsearch_value = -1, $target =
'')
2016 global $langs, $conf, $user, $mysoc;
2018 if (!empty($conf->dol_no_mouse_hover)) {
2024 if ($option ==
'withdraw') {
2025 $url = DOL_URL_ROOT.
'/compta/facture/prelevement.php?facid='.$this->id;
2027 $url = DOL_URL_ROOT.
'/compta/facture/card.php?facid='.$this->id;
2030 if (!$user->hasRight(
"facture",
"read")) {
2034 if ($option !==
'nolink') {
2036 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
2037 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
2038 $add_save_lastsearch_values = 1;
2040 if ($add_save_lastsearch_values) {
2041 $url .=
'&save_lastsearch_values=1';
2049 $picto = $this->picto;
2050 if ($this->
type == self::TYPE_REPLACEMENT) {
2053 if ($this->
type == self::TYPE_CREDIT_NOTE) {
2056 if ($this->
type == self::TYPE_DEPOSIT) {
2061 'objecttype' => $this->element,
2062 'moretitle' => $moretitle,
2063 'option' => $option,
2065 $classfortooltip =
'classfortooltip';
2068 $classfortooltip =
'classforajaxtooltip';
2069 $dataparams =
' data-params="'.dol_escape_htmltag(json_encode($params)).
'"';
2075 $linkclose = ($target ?
' target="'.$target.
'"' :
'');
2076 if (empty($notooltip) && $user->hasRight(
"facture",
"read")) {
2077 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
2078 $label = $langs->trans(
"Invoice");
2079 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
2081 $linkclose .= ($label ?
' title="'.dol_escape_htmltag($label, 1).
'"' :
' title="tocomplete"');
2082 $linkclose .= $dataparams.
' class="'.$classfortooltip.
'"';
2085 $linkstart =
'<a href="'.$url.
'"';
2086 $linkstart .= $linkclose.
'>';
2089 if ($option ==
'nolink') {
2094 $result .= $linkstart;
2096 $result .=
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') : $dataparams.
' class="'.(($withpicto != 2) ?
'paddingright ' :
'').$classfortooltip.
'"'), 0, 0, $notooltip ? 0 : 1);
2098 if ($withpicto != 2) {
2099 $result .= ($max ?
dol_trunc($this->
ref, $max) : $this->ref);
2101 $result .= $linkend;
2103 if ($addlinktonotes) {
2104 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
2107 $notetoshow = $langs->trans(
"ViewPrivateNote").
':<br>'.$txttoshow;
2108 $result .=
' <span class="note inline-block">';
2109 $result .=
'<a href="'.DOL_URL_ROOT.
'/compta/facture/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow, 1, 1).
'">';
2114 $result .=
'</span>';
2118 global $action, $hookmanager;
2119 $hookmanager->initHooks(array(
'invoicedao'));
2120 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result,
'notooltip' => $notooltip,
'addlinktonotes' => $addlinktonotes,
'save_lastsearch_value'=> $save_lastsearch_value,
'target' => $target);
2121 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
2123 $result = $hookmanager->resPrint;
2125 $result .= $hookmanager->resPrint;
2141 public function fetch($rowid, $ref =
'', $ref_ext =
'', $notused =
'', $fetch_situation =
false)
2143 if (empty($rowid) && empty($ref) && empty($ref_ext)) {
2147 $sql =
'SELECT f.rowid, f.entity, f.ref, f.ref_client, f.ref_ext, f.type, f.fk_soc';
2148 $sql .=
', f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc, f.revenuestamp';
2149 $sql .=
', f.remise_percent, f.remise_absolue, f.remise';
2150 $sql .=
', f.datef as df, f.date_pointoftax';
2151 $sql .=
', f.date_lim_reglement as dlr';
2152 $sql .=
', f.datec as datec';
2153 $sql .=
', f.date_valid as datev';
2154 $sql .=
', f.tms as datem';
2155 $sql .=
', f.note_private, f.note_public, f.fk_statut, f.paye, f.close_code, f.close_note, f.fk_user_author, f.fk_user_valid, f.fk_user_modif, f.model_pdf, f.last_main_doc';
2156 $sql .=
', f.fk_facture_source, f.fk_fac_rec_source';
2157 $sql .=
', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project, f.extraparams';
2158 $sql .=
', f.situation_cycle_ref, f.situation_counter, f.situation_final';
2159 $sql .=
', f.fk_account';
2160 $sql .=
", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
2161 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
2162 $sql .=
', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
2163 $sql .=
', f.fk_incoterms, f.location_incoterms';
2164 $sql .=
', f.module_source, f.pos_source';
2165 $sql .=
", i.libelle as label_incoterms";
2166 $sql .=
", f.retained_warranty as retained_warranty, f.retained_warranty_date_limit as retained_warranty_date_limit, f.retained_warranty_fk_cond_reglement as retained_warranty_fk_cond_reglement";
2167 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
2168 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
2169 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON f.fk_mode_reglement = p.id';
2170 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON f.fk_incoterms = i.rowid';
2173 $sql .=
" WHERE f.rowid = ".((int) $rowid);
2175 $sql .=
' WHERE f.entity IN ('.getEntity(
'invoice').
')';
2177 $sql .=
" AND f.ref = '".$this->db->escape($ref).
"'";
2180 $sql .=
" AND f.ref_ext = '".$this->db->escape($ref_ext).
"'";
2184 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
2185 $resql = $this->db->query(
$sql);
2187 if ($this->db->num_rows($resql)) {
2188 $obj = $this->db->fetch_object($resql);
2190 $this->
id = $obj->rowid;
2191 $this->entity = $obj->entity;
2193 $this->
ref = $obj->ref;
2194 $this->ref_client = $obj->ref_client;
2195 $this->ref_customer = $obj->ref_client;
2196 $this->ref_ext = $obj->ref_ext;
2197 $this->
type = $obj->type;
2198 $this->date = $this->db->jdate($obj->df);
2199 $this->date_pointoftax = $this->db->jdate($obj->date_pointoftax);
2200 $this->date_creation = $this->db->jdate($obj->datec);
2201 $this->date_validation = $this->db->jdate($obj->datev);
2202 $this->date_modification = $this->db->jdate($obj->datem);
2203 $this->datem = $this->db->jdate($obj->datem);
2204 $this->remise_percent = $obj->remise_percent;
2205 $this->remise_absolue = $obj->remise_absolue;
2206 $this->total_ht = $obj->total_ht;
2207 $this->total_tva = $obj->total_tva;
2208 $this->total_localtax1 = $obj->localtax1;
2209 $this->total_localtax2 = $obj->localtax2;
2210 $this->total_ttc = $obj->total_ttc;
2211 $this->revenuestamp = $obj->revenuestamp;
2212 $this->paye = $obj->paye;
2213 $this->close_code = $obj->close_code;
2214 $this->close_note = $obj->close_note;
2216 $this->socid = $obj->fk_soc;
2217 $this->thirdparty =
null;
2219 $this->fk_project = $obj->fk_project;
2220 $this->project =
null;
2222 $this->statut = $obj->fk_statut;
2223 $this->status = $obj->fk_statut;
2225 $this->date_lim_reglement = $this->db->jdate($obj->dlr);
2226 $this->mode_reglement_id = $obj->fk_mode_reglement;
2227 $this->mode_reglement_code = $obj->mode_reglement_code;
2228 $this->mode_reglement = $obj->mode_reglement_libelle;
2229 $this->cond_reglement_id = $obj->fk_cond_reglement;
2230 $this->cond_reglement_code = $obj->cond_reglement_code;
2231 $this->cond_reglement = $obj->cond_reglement_libelle;
2232 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
2233 $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account :
null;
2234 $this->fk_facture_source = $obj->fk_facture_source;
2235 $this->fk_fac_rec_source = $obj->fk_fac_rec_source;
2236 $this->note = $obj->note_private;
2237 $this->note_private = $obj->note_private;
2238 $this->note_public = $obj->note_public;
2239 $this->user_author = $obj->fk_user_author;
2240 $this->user_valid = $obj->fk_user_valid;
2241 $this->user_modification = $obj->fk_user_modif;
2242 $this->fk_user_author = $obj->fk_user_author;
2243 $this->fk_user_valid = $obj->fk_user_valid;
2244 $this->fk_user_modif = $obj->fk_user_modif;
2245 $this->model_pdf = $obj->model_pdf;
2246 $this->modelpdf = $obj->model_pdf;
2247 $this->last_main_doc = $obj->last_main_doc;
2248 $this->situation_cycle_ref = $obj->situation_cycle_ref;
2249 $this->situation_counter = $obj->situation_counter;
2250 $this->situation_final = $obj->situation_final;
2251 $this->retained_warranty = $obj->retained_warranty;
2252 $this->retained_warranty_date_limit = $this->db->jdate($obj->retained_warranty_date_limit);
2253 $this->retained_warranty_fk_cond_reglement = $obj->retained_warranty_fk_cond_reglement;
2255 $this->extraparams = !empty($obj->extraparams) ? (array) json_decode($obj->extraparams,
true) : array();
2258 $this->fk_incoterms = $obj->fk_incoterms;
2259 $this->location_incoterms = $obj->location_incoterms;
2260 $this->label_incoterms = $obj->label_incoterms;
2262 $this->module_source = $obj->module_source;
2263 $this->pos_source = $obj->pos_source;
2266 $this->fk_multicurrency = $obj->fk_multicurrency;
2267 $this->multicurrency_code = $obj->multicurrency_code;
2268 $this->multicurrency_tx = $obj->multicurrency_tx;
2269 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
2270 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
2271 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
2273 if (($this->
type == self::TYPE_SITUATION || ($this->
type == self::TYPE_CREDIT_NOTE && $this->situation_cycle_ref > 0)) && $fetch_situation) {
2277 if ($this->status == self::STATUS_DRAFT) {
2278 $this->brouillon = 1;
2286 $this->lines = array();
2290 $this->error = $this->db->error();
2294 $this->db->free($resql);
2298 $this->error =
'Invoice with id='.$rowid.
' or ref='.$ref.
' or ref_ext='.$ref_ext.
' not found';
2300 dol_syslog(__METHOD__.$this->error, LOG_WARNING);
2304 $this->error = $this->db->lasterror();
2319 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2322 global $langs, $conf;
2324 $this->lines = array();
2326 $sql =
'SELECT l.rowid, l.fk_facture, l.fk_product, l.fk_parent_line, l.label as custom_label, l.description, l.product_type, l.price, l.qty, l.vat_src_code, l.tva_tx,';
2327 $sql .=
' l.localtax1_tx, l.localtax2_tx, l.localtax1_type, l.localtax2_type, l.remise_percent, l.fk_remise_except, l.subprice, l.ref_ext,';
2328 $sql .=
' l.situation_percent, l.fk_prev_id,';
2329 $sql .=
' l.rang, l.special_code,';
2330 $sql .=
' l.date_start as date_start, l.date_end as date_end,';
2331 $sql .=
' l.info_bits, l.total_ht, l.total_tva, l.total_localtax1, l.total_localtax2, l.total_ttc, l.fk_code_ventilation, l.fk_product_fournisseur_price as fk_fournprice, l.buy_price_ht as pa_ht,';
2332 $sql .=
' l.fk_unit,';
2333 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2334 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2335 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet as l';
2336 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
2337 $sql .=
' WHERE l.fk_facture = '.((int) $this->
id);
2338 $sql .=
' ORDER BY l.rang, l.rowid';
2340 dol_syslog(get_class($this).
'::fetch_lines', LOG_DEBUG);
2341 $result = $this->db->query(
$sql);
2343 $num = $this->db->num_rows($result);
2346 $objp = $this->db->fetch_object($result);
2349 $line->id = $objp->rowid;
2350 $line->rowid = $objp->rowid;
2351 $line->fk_facture = $objp->fk_facture;
2352 $line->label = $objp->custom_label;
2353 $line->desc = $objp->description;
2354 $line->description = $objp->description;
2355 $line->product_type = $objp->product_type;
2356 $line->ref = $objp->product_ref;
2357 $line->product_ref = $objp->product_ref;
2358 $line->libelle = $objp->product_label;
2359 $line->product_label = $objp->product_label;
2360 $line->product_desc = $objp->product_desc;
2361 $line->fk_product_type = $objp->fk_product_type;
2362 $line->qty = $objp->qty;
2363 $line->subprice = $objp->subprice;
2364 $line->ref_ext = $objp->ref_ext;
2366 $line->vat_src_code = $objp->vat_src_code;
2367 $line->tva_tx = $objp->tva_tx;
2368 $line->localtax1_tx = $objp->localtax1_tx;
2369 $line->localtax2_tx = $objp->localtax2_tx;
2370 $line->localtax1_type = $objp->localtax1_type;
2371 $line->localtax2_type = $objp->localtax2_type;
2372 $line->remise_percent = $objp->remise_percent;
2373 $line->fk_remise_except = $objp->fk_remise_except;
2374 $line->fk_product = $objp->fk_product;
2375 $line->date_start = $this->db->jdate($objp->date_start);
2376 $line->date_end = $this->db->jdate($objp->date_end);
2377 $line->date_start = $this->db->jdate($objp->date_start);
2378 $line->date_end = $this->db->jdate($objp->date_end);
2379 $line->info_bits = $objp->info_bits;
2380 $line->total_ht = $objp->total_ht;
2381 $line->total_tva = $objp->total_tva;
2382 $line->total_localtax1 = $objp->total_localtax1;
2383 $line->total_localtax2 = $objp->total_localtax2;
2384 $line->total_ttc = $objp->total_ttc;
2385 $line->code_ventilation = $objp->fk_code_ventilation;
2386 $line->fk_fournprice = $objp->fk_fournprice;
2387 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2388 $line->pa_ht = $marginInfos[0];
2389 $line->marge_tx = $marginInfos[1];
2390 $line->marque_tx = $marginInfos[2];
2391 $line->rang = $objp->rang;
2392 $line->special_code = $objp->special_code;
2393 $line->fk_parent_line = $objp->fk_parent_line;
2394 $line->situation_percent = $objp->situation_percent;
2395 $line->fk_prev_id = $objp->fk_prev_id;
2396 $line->fk_unit = $objp->fk_unit;
2399 $line->fk_accounting_account = $objp->fk_code_ventilation;
2402 $line->fk_multicurrency = $objp->fk_multicurrency;
2403 $line->multicurrency_code = $objp->multicurrency_code;
2404 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2405 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2406 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2407 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2409 $line->fetch_optionals();
2412 if (
getDolGlobalInt(
'MAIN_MULTILANGS') && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2413 $tmpproduct =
new Product($this->db);
2414 $tmpproduct->fetch($objp->fk_product);
2415 $tmpproduct->getMultiLangs();
2417 $line->multilangs = $tmpproduct->multilangs;
2420 $this->lines[$i] = $line;
2424 $this->db->free($result);
2427 $this->error = $this->db->error();
2442 $this->tab_previous_situation_invoice = array();
2443 $this->tab_next_situation_invoice = array();
2445 $sql =
'SELECT rowid, type, situation_cycle_ref, situation_counter FROM '.MAIN_DB_PREFIX.
'facture';
2446 $sql .=
" WHERE rowid <> ".((int) $this->
id);
2447 $sql .=
' AND entity = '.((int) $this->entity);
2448 $sql .=
' AND situation_cycle_ref = '.(int) $this->situation_cycle_ref;
2449 $sql .=
' ORDER BY situation_counter ASC';
2451 dol_syslog(get_class($this).
'::fetchPreviousNextSituationInvoice ', LOG_DEBUG);
2452 $result = $this->db->query(
$sql);
2453 if ($result && $this->db->num_rows($result) > 0) {
2454 while ($objp = $this->db->fetch_object($result)) {
2455 $invoice =
new Facture($this->db);
2456 if ($invoice->fetch($objp->rowid) > 0) {
2457 if ($objp->situation_counter < $this->situation_counter
2458 || ($objp->situation_counter == $this->situation_counter && $objp->rowid < $this->id)
2460 $this->tab_previous_situation_invoice[] = $invoice;
2462 $this->tab_next_situation_invoice[] = $invoice;
2481 if (empty($this->
type)) {
2484 if (isset($this->
ref)) {
2485 $this->
ref = trim($this->
ref);
2487 if (isset($this->ref_ext)) {
2488 $this->ref_ext = trim($this->ref_ext);
2490 if (isset($this->ref_client)) {
2491 $this->ref_client = trim($this->ref_client);
2493 if (isset($this->increment)) {
2494 $this->increment = trim($this->increment);
2496 if (isset($this->close_code)) {
2497 $this->close_code = trim($this->close_code);
2499 if (isset($this->close_note)) {
2500 $this->close_note = trim($this->close_note);
2502 if (isset($this->note) || isset($this->note_private)) {
2503 $this->note = (isset($this->note) ? trim($this->note) : trim($this->note_private));
2505 if (isset($this->note) || isset($this->note_private)) {
2506 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
2508 if (isset($this->note_public)) {
2509 $this->note_public = trim($this->note_public);
2511 if (isset($this->model_pdf)) {
2512 $this->model_pdf = trim($this->model_pdf);
2514 if (isset($this->import_key)) {
2515 $this->import_key = trim($this->import_key);
2517 if (isset($this->retained_warranty)) {
2518 $this->retained_warranty = floatval($this->retained_warranty);
2526 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture SET";
2527 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
2528 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
2529 $sql .=
" type=".(isset($this->
type) ? $this->db->escape($this->
type) :
"null").
",";
2530 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
2531 $sql .=
" increment=".(isset($this->increment) ?
"'".$this->db->escape($this->increment).
"'" :
"null").
",";
2532 $sql .=
" fk_soc=".(isset($this->socid) ? $this->db->escape($this->socid) :
"null").
",";
2533 $sql .=
" datec=".(strval($this->date_creation) !=
'' ?
"'".$this->db->idate($this->date_creation).
"'" :
'null').
",";
2534 $sql .=
" datef=".(strval($this->date) !=
'' ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
2535 $sql .=
" date_pointoftax=".(strval($this->date_pointoftax) !=
'' ?
"'".$this->db->idate($this->date_pointoftax).
"'" :
'null').
",";
2536 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
2537 $sql .=
" paye=".(isset($this->paye) ? $this->db->escape($this->paye) : 0).
",";
2538 $sql .=
" remise_percent=".(isset($this->remise_percent) ? $this->db->escape($this->remise_percent) :
"null").
",";
2539 $sql .=
" remise_absolue=".(isset($this->remise_absolue) ? $this->db->escape($this->remise_absolue) :
"null").
",";
2540 $sql .=
" close_code=".(isset($this->close_code) ?
"'".$this->db->escape($this->close_code).
"'" :
"null").
",";
2541 $sql .=
" close_note=".(isset($this->close_note) ?
"'".$this->db->escape($this->close_note).
"'" :
"null").
",";
2542 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
2543 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
2544 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
2545 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
2546 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
2547 $sql .=
" revenuestamp=".((isset($this->revenuestamp) && $this->revenuestamp !=
'') ? $this->db->escape($this->revenuestamp) :
"null").
",";
2548 $sql .=
" fk_statut=".(isset($this->statut) ? $this->db->escape($this->statut) :
"null").
",";
2549 $sql .=
" fk_user_author=".(isset($this->user_author) ? $this->db->escape($this->user_author) :
"null").
",";
2550 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? $this->db->escape($this->fk_user_valid) :
"null").
",";
2551 $sql .=
" fk_facture_source=".(isset($this->fk_facture_source) ? $this->db->escape($this->fk_facture_source) :
"null").
",";
2552 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->db->escape($this->fk_project) :
"null").
",";
2553 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->db->escape($this->cond_reglement_id) :
"null").
",";
2554 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->db->escape($this->mode_reglement_id) :
"null").
",";
2555 $sql .=
" date_lim_reglement=".(strval($this->date_lim_reglement) !=
'' ?
"'".$this->db->idate($this->date_lim_reglement).
"'" :
'null').
",";
2556 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
2557 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
2558 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
2559 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
",";
2560 $sql .=
" situation_cycle_ref=".(empty($this->situation_cycle_ref) ?
"null" : $this->db->escape($this->situation_cycle_ref)).
",";
2561 $sql .=
" situation_counter=".(empty($this->situation_counter) ?
"null" : $this->db->escape($this->situation_counter)).
",";
2562 $sql .=
" situation_final=".(empty($this->situation_final) ?
"0" : $this->db->escape($this->situation_final)).
",";
2563 $sql .=
" retained_warranty=".(empty($this->retained_warranty) ?
"0" : $this->db->escape($this->retained_warranty)).
",";
2564 $sql .=
" retained_warranty_date_limit=".(strval($this->retained_warranty_date_limit) !=
'' ?
"'".$this->db->idate($this->retained_warranty_date_limit).
"'" :
'null').
",";
2565 $sql .=
" retained_warranty_fk_cond_reglement=".(isset($this->retained_warranty_fk_cond_reglement) ?intval($this->retained_warranty_fk_cond_reglement) :
"null");
2566 $sql .=
" WHERE rowid=".((int) $this->
id);
2570 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
2571 $resql = $this->db->query(
$sql);
2574 $this->errors[] =
"Error ".$this->db->lasterror();
2584 if (!$error && !$notrigger) {
2595 foreach ($this->errors as $errmsg) {
2596 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
2597 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2599 $this->db->rollback();
2602 $this->db->commit();
2618 global $conf, $langs;
2620 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2621 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
2626 $result = $remise->fetch($idremise);
2629 if ($remise->fk_facture) {
2630 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2631 $this->db->rollback();
2636 $facligne->fk_facture = $this->id;
2637 $facligne->fk_remise_except = $remise->id;
2638 $facligne->desc = $remise->description;
2639 $facligne->vat_src_code = $remise->vat_src_code;
2640 $facligne->tva_tx = $remise->tva_tx;
2641 $facligne->subprice = -$remise->amount_ht;
2642 $facligne->fk_product = 0;
2644 $facligne->remise_percent = 0;
2645 $facligne->rang = -1;
2646 $facligne->info_bits = 2;
2648 if (!empty($conf->global->MAIN_ADD_LINE_AT_POSITION)) {
2649 $facligne->rang = 1;
2650 $linecount = count($this->lines);
2651 for ($ii = 1; $ii <= $linecount; $ii++) {
2657 if ($remise->fk_facture_source > 0) {
2658 $srcinvoice =
new Facture($this->db);
2659 $srcinvoice->fetch($remise->fk_facture_source);
2660 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmargin.class.php';
2662 $arraytmp = $formmargin->getMarginInfosArray($srcinvoice,
false);
2663 $facligne->pa_ht = $arraytmp[
'pa_total'];
2666 $facligne->total_ht = -$remise->amount_ht;
2667 $facligne->total_tva = -$remise->amount_tva;
2668 $facligne->total_ttc = -$remise->amount_ttc;
2670 $facligne->multicurrency_subprice = -$remise->multicurrency_subprice;
2671 $facligne->multicurrency_total_ht = -$remise->multicurrency_amount_ht;
2672 $facligne->multicurrency_total_tva = -$remise->multicurrency_amount_tva;
2673 $facligne->multicurrency_total_ttc = -$remise->multicurrency_amount_ttc;
2675 $lineid = $facligne->insert();
2680 $result = $remise->link_to_invoice($lineid, 0);
2682 $this->error = $remise->error;
2683 $this->db->rollback();
2687 $this->db->commit();
2690 $this->error = $facligne->error;
2691 $this->db->rollback();
2695 $this->error = $facligne->error;
2696 $this->db->rollback();
2700 $this->db->rollback();
2722 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
2723 if (empty($ref_client)) {
2724 $sql .=
' SET ref_client = NULL';
2726 $sql .=
' SET ref_client = \''.$this->db->escape($ref_client).
'\'';
2728 $sql .=
" WHERE rowid = ".((int) $this->
id);
2730 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2731 $resql = $this->db->query(
$sql);
2733 $this->errors[] = $this->db->error();
2738 $this->ref_client = $ref_client;
2741 if (!$notrigger && empty($error)) {
2751 $this->ref_client = $ref_client;
2753 $this->db->commit();
2756 foreach ($this->errors as $errmsg) {
2757 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2758 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2760 $this->db->rollback();
2773 public function delete($user, $notrigger = 0, $idwarehouse = -1)
2775 global $langs, $conf;
2776 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
2780 dol_syslog(get_class($this).
"::delete rowid=".$rowid.
", ref=".$this->ref.
", thirdparty=".(empty($this->thirdparty) ?
'' : $this->thirdparty->name), LOG_DEBUG);
2793 if (!$error && !$notrigger) {
2807 dol_syslog(get_class($this).
"::delete error deleteExtraFields ".$this->error, LOG_ERR);
2821 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'societe_remise_except';
2822 $sql .=
' WHERE fk_facture_source = '.((int) $rowid);
2823 $sql .=
' AND fk_facture_line IS NULL';
2824 $resql = $this->db->query(
$sql);
2828 $list_rowid_det = array();
2829 foreach ($this->lines as $key => $invoiceline) {
2830 $list_rowid_det[] = $invoiceline->id;
2834 if (count($list_rowid_det)) {
2835 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
2836 $sql .=
' SET fk_facture = NULL, fk_facture_line = NULL';
2837 $sql .=
' WHERE fk_facture_line IN ('.$this->db->sanitize(join(
',', $list_rowid_det)).
')';
2839 if (!$this->db->query(
$sql)) {
2840 $this->error = $this->db->error().
" sql=".
$sql;
2841 $this->errors[] = $this->error;
2842 $this->db->rollback();
2849 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'eventorganization_conferenceorboothattendee';
2850 $sql .=
' SET fk_invoice = NULL';
2851 $sql .=
' WHERE fk_invoice = '.((int) $rowid);
2853 if (!$this->db->query(
$sql)) {
2854 $this->error = $this->db->error().
" sql=".
$sql;
2855 $this->errors[] = $this->error;
2856 $this->db->rollback();
2860 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'element_time';
2861 $sql .=
' SET invoice_id = NULL, invoice_line_id = NULL';
2862 $sql .=
' WHERE invoice_id = '.((int) $rowid);
2864 if (!$this->db->query(
$sql)) {
2865 $this->error = $this->db->error().
" sql=".
$sql;
2866 $this->errors[] = $this->error;
2867 $this->db->rollback();
2872 if ($this->
type != self::TYPE_DEPOSIT && $result >= 0 &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_BILL) && $idwarehouse != -1) {
2873 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2874 $langs->load(
"agenda");
2876 $num = count($this->lines);
2877 for ($i = 0; $i < $num; $i++) {
2878 if ($this->lines[$i]->fk_product > 0) {
2880 $mouvP->origin = &$this;
2881 $mouvP->setOrigin($this->element, $this->
id);
2883 if ($this->
type == self::TYPE_CREDIT_NOTE) {
2884 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceDeleteDolibarr", $this->ref));
2886 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"InvoiceDeleteDolibarr", $this->ref));
2893 $main = MAIN_DB_PREFIX.
'facturedet';
2894 $ef = $main.
"_extrafields";
2895 $sqlef =
"DELETE FROM ".$ef.
" WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_facture = ".((int) $rowid).
")";
2897 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facturedet WHERE fk_facture = '.((int) $rowid);
2900 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture WHERE rowid = '.((int) $rowid);
2902 $resql = $this->db->query(
$sql);
2909 if ($conf->facture->dir_output && !empty($this->
ref)) {
2910 $dir = $conf->facture->dir_output.
"/".$ref;
2911 $file = $conf->facture->dir_output.
"/".$ref.
"/".$ref.
".pdf";
2912 if (file_exists($file)) {
2916 $langs->load(
"errors");
2917 $this->error = $langs->trans(
"ErrorFailToDeleteFile", $file);
2918 $this->errors[] = $this->error;
2919 $this->db->rollback();
2923 if (file_exists($dir)) {
2925 $langs->load(
"errors");
2926 $this->error = $langs->trans(
"ErrorFailToDeleteDir", $dir);
2927 $this->errors[] = $this->error;
2928 $this->db->rollback();
2934 $this->db->commit();
2937 $this->error = $this->db->lasterror().
" sql=".
$sql;
2938 $this->errors[] = $this->error;
2939 $this->db->rollback();
2943 $this->error = $this->db->lasterror().
" sql=".
$sql;
2944 $this->errors[] = $this->error;
2945 $this->db->rollback();
2949 $this->db->rollback();
2966 public function set_paid($user, $close_code =
'', $close_note =
'')
2969 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
2970 return $this->
setPaid($user, $close_code, $close_note);
2982 public function setPaid($user, $close_code =
'', $close_note =
'')
2986 if ($this->paye != 1) {
2991 dol_syslog(get_class($this).
"::setPaid rowid=".((
int) $this->
id), LOG_DEBUG);
2993 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture SET';
2994 $sql .=
' fk_statut='.self::STATUS_CLOSED;
2999 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
3002 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
3004 $sql .=
', fk_user_closing = '.((int) $user->id);
3005 $sql .=
", date_closing = '".$this->db->idate($now).
"'";
3006 $sql .=
" WHERE rowid = ".((int) $this->
id);
3008 $resql = $this->db->query(
$sql);
3018 $this->error = $this->db->lasterror();
3022 $this->db->commit();
3025 $this->db->rollback();
3048 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
3066 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
3067 $sql .=
' SET paye=0, fk_statut='.self::STATUS_VALIDATED.
', close_code=null, close_note=null,';
3068 $sql .=
' date_closing=null,';
3069 $sql .=
' fk_user_closing=null';
3070 $sql .=
" WHERE rowid = ".((int) $this->
id);
3072 dol_syslog(get_class($this).
"::setUnpaid", LOG_DEBUG);
3073 $resql = $this->db->query(
$sql);
3083 $this->error = $this->db->error();
3088 $this->db->commit();
3091 $this->db->rollback();
3113 dol_syslog(get_class($this).
"::set_canceled is deprecated, use setCanceled instead", LOG_NOTICE);
3114 return $this->
setCanceled($user, $close_code, $close_note);
3127 public function setCanceled($user, $close_code =
'', $close_note =
'')
3129 dol_syslog(get_class($this).
"::setCanceled rowid=".((
int) $this->
id), LOG_DEBUG);
3133 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture SET';
3134 $sql .=
' fk_statut='.self::STATUS_ABANDONED;
3136 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
3139 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
3141 $sql .=
" WHERE rowid = ".((int) $this->
id);
3143 $resql = $this->db->query(
$sql);
3147 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
3148 $sql .=
' SET fk_facture = NULL';
3149 $sql .=
' WHERE fk_facture = '.((int) $this->
id);
3151 $resql = $this->db->query(
$sql);
3156 $this->db->rollback();
3161 $this->db->commit();
3164 $this->error = $this->db->error().
" sql=".
$sql;
3165 $this->db->rollback();
3169 $this->error = $this->db->error().
" sql=".
$sql;
3170 $this->db->rollback();
3186 public function validate($user, $force_number =
'', $idwarehouse = 0, $notrigger = 0, $batch_rule = 0)
3188 global $conf, $langs, $mysoc;
3189 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3191 $productStatic =
null;
3192 $warehouseStatic =
null;
3193 if ($batch_rule > 0) {
3194 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
3195 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
3196 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
3197 $productStatic =
new Product($this->db);
3198 $warehouseStatic =
new Entrepot($this->db);
3205 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number.
', idwarehouse='.$idwarehouse);
3212 if ($this->statut != self::STATUS_DRAFT) {
3213 dol_syslog(get_class($this).
"::validate status is not draft. operation canceled.", LOG_WARNING);
3216 if (count($this->lines) <= 0) {
3217 $langs->load(
"errors");
3218 $this->error = $langs->trans(
"ErrorObjectMustHaveLinesToBeValidated", $this->
ref);
3221 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->facture->creer))
3222 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->facture->invoice_advance->validate))) {
3223 $this->error =
'Permission denied';
3224 dol_syslog(get_class($this).
"::validate ".$this->error.
' MAIN_USE_ADVANCED_PERMS='.$conf->global->MAIN_USE_ADVANCED_PERMS, LOG_ERR);
3227 if (!empty($conf->global-> INVOICE_CHECK_POSTERIOR_DATE)) {
3229 if (!$last_of_type[0]) {
3230 $this->error = $langs->transnoentities(
"ErrorInvoiceIsNotLastOfSameType", $this->
ref,
dol_print_date($this->date,
'day'),
dol_print_date($last_of_type[1],
'day'));
3236 if (!empty($this->thirdparty) && is_object($this->thirdparty)) {
3237 $array_to_check = array(
'IDPROF1',
'IDPROF2',
'IDPROF3',
'IDPROF4',
'IDPROF5',
'IDPROF6',
'EMAIL',
'ACCOUNTANCY_CODE_CUSTOMER');
3238 foreach ($array_to_check as $key) {
3239 $keymin = strtolower($key);
3240 if (!property_exists($this->thirdparty, $keymin)) {
3243 $vallabel = $this->thirdparty->$keymin;
3245 $i = (int) preg_replace(
'/[^0-9]/',
'', $key);
3247 if ($this->thirdparty->isACompany()) {
3249 if ($mysoc->country_id > 0 && $this->thirdparty->country_id == $mysoc->country_id) {
3250 $idprof_mandatory =
'SOCIETE_'.$key.
'_INVOICE_MANDATORY';
3251 if (!$vallabel && !empty($conf->global->$idprof_mandatory)) {
3252 $langs->load(
"errors");
3253 $this->error = $langs->trans(
'ErrorProdIdIsMandatory', $langs->transcountry(
'ProfId'.$i, $this->thirdparty->country_code)).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
3254 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3260 if ($key ==
'EMAIL') {
3262 if (!empty($conf->global->SOCIETE_EMAIL_INVOICE_MANDATORY) && !isValidEMail($this->thirdparty->email)) {
3263 $langs->load(
"errors");
3264 $this->error = $langs->trans(
"ErrorBadEMail", $this->thirdparty->email).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
3265 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3269 if ($key ==
'ACCOUNTANCY_CODE_CUSTOMER') {
3271 if (!empty($conf->global->SOCIETE_ACCOUNTANCY_CODE_CUSTOMER_INVOICE_MANDATORY) && empty($this->thirdparty->code_compta)) {
3272 $langs->load(
"errors");
3273 $this->error = $langs->trans(
"ErrorAccountancyCodeCustomerIsMandatory", $this->thirdparty->name).
' ('.$langs->trans(
"ForbiddenBySetupRules").
')';
3274 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3283 $array_to_check = array(
'REF_CLIENT'=>
'RefCustomer');
3284 foreach ($array_to_check as $key => $val) {
3285 $keymin = strtolower($key);
3286 $vallabel = $this->$keymin;
3289 $keymandatory =
'INVOICE_'.$key.
'_MANDATORY_FOR_VALIDATION';
3290 if (!$vallabel && !empty($conf->global->$keymandatory)) {
3291 $langs->load(
"errors");
3293 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv($val)),
null,
'errors');
3300 if ($this->
type == self::TYPE_REPLACEMENT) {
3302 if ($this->fk_facture_source <= 0) {
3303 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"InvoiceReplacement"));
3304 $this->db->rollback();
3309 $facreplaced =
new Facture($this->db);
3310 $result = $facreplaced->fetch($this->fk_facture_source);
3312 $this->error = $langs->trans(
"ErrorBadInvoice");
3313 $this->db->rollback();
3318 $idreplacement = $facreplaced->getIdReplacingInvoice(
'validated');
3319 if ($idreplacement && $idreplacement != $this->
id) {
3320 $facreplacement =
new Facture($this->db);
3321 $facreplacement->fetch($idreplacement);
3322 $this->error = $langs->trans(
"ErrorInvoiceAlreadyReplaced", $facreplaced->ref, $facreplacement->ref);
3323 $this->db->rollback();
3327 $result = $facreplaced->setCanceled($user, self::CLOSECODE_REPLACED,
'');
3329 $this->error = $facreplaced->error;
3330 $this->db->rollback();
3336 if ($force_number) {
3337 $num = $force_number;
3338 } elseif (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
3339 if (!empty($conf->global->FAC_FORCE_DATE_VALIDATION)) {
3354 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
3355 $sql .=
" SET ref = '".$this->db->escape($num).
"', fk_statut = ".self::STATUS_VALIDATED.
", fk_user_valid = ".($user->id > 0 ? $user->id :
"null").
", date_valid = '".$this->db->idate($now).
"'";
3356 if (!empty($conf->global->FAC_FORCE_DATE_VALIDATION)) {
3357 $sql .=
", datef='".$this->db->idate($this->date).
"'";
3358 $sql .=
", date_lim_reglement='".$this->db->idate($this->date_lim_reglement).
"'";
3360 $sql .=
" WHERE rowid = ".((int) $this->
id);
3362 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
3363 $resql = $this->db->query(
$sql);
3370 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref))) {
3376 $result = $this->thirdparty->set_as_client();
3379 if ($this->
type != self::TYPE_DEPOSIT && $result >= 0 &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_BILL) && $idwarehouse > 0) {
3380 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
3381 $langs->load(
"agenda");
3384 $cpt = count($this->lines);
3385 for ($i = 0; $i < $cpt; $i++) {
3386 if ($this->lines[$i]->fk_product > 0) {
3388 $mouvP->origin = &$this;
3389 $mouvP->setOrigin($this->element, $this->
id);
3391 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3392 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
3395 $this->error = $mouvP->error;
3398 $is_batch_line =
false;
3399 if ($batch_rule > 0) {
3400 $productStatic->fetch($this->lines[$i]->fk_product);
3401 if ($productStatic->hasbatch()) {
3402 $is_batch_line =
true;
3403 $product_qty_remain = $this->lines[$i]->qty;
3409 $sortfield =
'pl.sellby,pl.eatby,pb.qty,pl.rowid';
3410 $sortorder =
'ASC,ASC,ASC,ASC';
3413 $resBatchList = $productbatch->findAllForProduct($productStatic->id, $idwarehouse, (
getDolGlobalInt(
'STOCK_ALLOW_NEGATIVE_TRANSFER') ?
null : 0), $sortfield, $sortorder);
3414 if (!is_array($resBatchList)) {
3416 $this->error = $this->db->lasterror();
3420 $batchList = $resBatchList;
3421 if (empty($batchList)) {
3423 $langs->load(
'errors');
3424 $warehouseStatic->fetch($idwarehouse);
3425 $this->error = $langs->trans(
'ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref);
3426 dol_syslog(__METHOD__.
' Error: '.$langs->transnoentitiesnoconv(
'ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR);
3429 foreach ($batchList as $batch) {
3430 if ($batch->qty <= 0) {
3435 if ($batch->qty >= $product_qty_remain) {
3436 $product_batch_qty = $product_qty_remain;
3439 $product_batch_qty = $batch->qty;
3441 $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_batch_qty, $this->lines[$i]->subprice, $langs->trans(
'InvoiceValidatedInDolibarr', $num),
'',
'',
'', $batch->batch);
3444 $this->error = $mouvP->error;
3448 $product_qty_remain -= $product_batch_qty;
3450 if ($product_qty_remain <= 0) {
3455 if (!$error && $product_qty_remain > 0) {
3458 $batch = $batchList[0];
3459 $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_qty_remain, $this->lines[$i]->subprice, $langs->trans(
'InvoiceValidatedInDolibarr', $num),
'',
'',
'', $batch->batch);
3462 $this->error = $mouvP->error;
3466 $langs->load(
'errors');
3467 $warehouseStatic->fetch($idwarehouse);
3468 $this->error = $langs->trans(
'ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref);
3469 dol_syslog(__METHOD__.
' Error: '.$langs->transnoentitiesnoconv(
'ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR);
3476 if (!$is_batch_line) {
3477 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
3480 $this->error = $mouvP->error;
3493 if (!$error && $this->
type == self::TYPE_CREDIT_NOTE && $this->fk_facture_source > 0) {
3494 $invoice_situation =
new Facture($this->db);
3495 $result = $invoice_situation->fetch($this->fk_facture_source);
3496 if ($result > 0 && $invoice_situation->type == self::TYPE_SITUATION && $invoice_situation->situation_final == 1) {
3497 $invoice_situation->situation_final = 0;
3499 $result = $invoice_situation->setFinal($user, 1);
3502 $this->error = $invoice_situation->error;
3503 $this->errors = $invoice_situation->errors;
3509 if (!$error && !$notrigger) {
3519 $this->oldref = $this->ref;
3522 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
3524 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"ecm_files set filename = CONCAT('".$this->db->escape($this->newref).
"', SUBSTR(filename, ".(strlen($this->
ref) + 1).
")), filepath = 'facture/".$this->db->escape($this->newref).
"'";
3525 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'facture/".$this->db->escape($this->
ref).
"' and entity = ".$conf->entity;
3526 $resql = $this->db->query(
$sql);
3529 $this->error = $this->db->lasterror();
3535 $dirsource = $conf->facture->dir_output.
'/'.$oldref;
3536 $dirdest = $conf->facture->dir_output.
'/'.$newref;
3537 if (!$error && file_exists($dirsource)) {
3538 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
3540 if (@rename($dirsource, $dirdest)) {
3543 $listoffiles =
dol_dir_list($conf->facture->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
3544 foreach ($listoffiles as $fileentry) {
3545 $dirsource = $fileentry[
'name'];
3546 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
3547 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
3548 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
3549 @rename($dirsource, $dirdest);
3567 $this->brouillon = 0;
3568 $this->date_validation = $now;
3571 if (!empty($conf->global->INVOICE_USE_SITUATION)) {
3573 $nboflines = count($this->lines);
3574 while (($i < $nboflines) && $final) {
3575 $final = ($this->lines[$i]->situation_percent == 100);
3579 if (empty($final)) {
3580 $this->situation_final = 0;
3582 $this->situation_final = 1;
3593 $this->db->commit();
3596 $this->db->rollback();
3609 foreach ($this->tab_next_situation_invoice as $next_invoice) {
3610 $is_last = $next_invoice->is_last_in_cycle();
3612 if ($next_invoice->statut == self::STATUS_DRAFT && $is_last != 1) {
3613 $this->error = $langs->trans(
'updatePriceNextInvoiceErrorUpdateline', $next_invoice->ref);
3617 $next_invoice->brouillon = 1;
3619 foreach ($next_invoice->lines as $line) {
3620 $result = $next_invoice->updateline(
3625 $line->remise_percent,
3629 $line->localtax1_tx,
3630 $line->localtax2_tx,
3633 $line->product_type,
3634 $line->fk_parent_line,
3636 $line->fk_fournprice,
3639 $line->special_code,
3640 $line->array_options,
3641 $line->situation_percent,
3646 $this->error = $langs->trans(
'updatePriceNextInvoiceErrorUpdateline', $next_invoice->ref);
3667 global $conf, $langs;
3671 if ($this->statut == self::STATUS_DRAFT) {
3672 dol_syslog(__METHOD__.
" already draft status", LOG_WARNING);
3680 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture";
3681 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT;
3682 $sql .=
" WHERE rowid = ".((int) $this->
id);
3684 $result = $this->db->query(
$sql);
3687 $this->oldcopy = clone $this;
3691 if ($this->
type != self::TYPE_DEPOSIT && $result >= 0 &&
isModEnabled(
'stock') && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
3692 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
3693 $langs->load(
"agenda");
3695 $num = count($this->lines);
3696 for ($i = 0; $i < $num; $i++) {
3697 if ($this->lines[$i]->fk_product > 0) {
3699 $mouvP->origin = &$this;
3700 $mouvP->setOrigin($this->element, $this->
id);
3702 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3703 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
3705 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
3712 $old_statut = $this->statut;
3713 $this->brouillon = 1;
3718 $result = $this->
call_trigger(
'BILL_UNVALIDATE', $user);
3721 $this->statut = $old_statut;
3722 $this->status = $old_statut;
3723 $this->brouillon = 0;
3727 $this->db->rollback();
3732 $this->db->commit();
3735 $this->db->rollback();
3739 $this->error = $this->db->error();
3740 $this->db->rollback();
3800 $fk_remise_except =
'',
3801 $price_base_type =
'HT',
3808 $fk_parent_line = 0,
3809 $fk_fournprice =
null,
3813 $situation_percent = 100,
3818 $noupdateafterinsertline = 0
3822 dol_syslog(__METHOD__.
": using line label is deprecated", LOG_WARNING);
3826 global $mysoc, $conf, $langs;
3828 dol_syslog(get_class($this).
"::addline id=$this->id, pu_ht=$pu_ht, qty=$qty, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, fk_product=$fk_product, remise_percent=$remise_percent, date_start=$date_start, date_end=$date_end, ventil=$ventil, 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, desc=".
dol_trunc($desc, 25), LOG_DEBUG);
3830 if ($this->statut == self::STATUS_DRAFT) {
3831 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3840 if (empty($info_bits)) {
3846 if (empty($ventil)) {
3849 if (empty($txtva)) {
3852 if (empty($txlocaltax1)) {
3855 if (empty($txlocaltax2)) {
3858 if (empty($fk_parent_line) || $fk_parent_line < 0) {
3859 $fk_parent_line = 0;
3861 if (empty($fk_prev_id)) {
3862 $fk_prev_id =
'null';
3864 if (!isset($situation_percent) || $situation_percent > 100 || (
string) $situation_percent ==
'') {
3865 $situation_percent = 100;
3867 if (empty($ref_ext)) {
3874 $pu_ht_devise =
price2num($pu_ht_devise);
3877 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3883 if ($price_base_type ==
'HT') {
3894 if ($date_start && $date_end && $date_start > $date_end) {
3895 $langs->load(
"errors");
3896 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3902 $product_type = $type;
3903 if (!empty($fk_product) && $fk_product > 0) {
3904 $product =
new Product($this->db);
3905 $result = $product->fetch($fk_product);
3906 $product_type = $product->type;
3908 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_INVOICE) && $product_type == 0 && $product->stock_reel < $qty) {
3909 $langs->load(
"errors");
3910 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnInvoice', $product->ref);
3911 $this->db->rollback();
3921 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3922 $vat_src_code = $reg[1];
3923 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3931 $tabprice =
calcul_price_total($qty, $pu,
$remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $product_type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx, $pu_ht_devise);
3933 $total_ht = $tabprice[0];
3934 $total_tva = $tabprice[1];
3935 $total_ttc = $tabprice[2];
3936 $total_localtax1 = $tabprice[9];
3937 $total_localtax2 = $tabprice[10];
3938 $pu_ht = $tabprice[3];
3941 $multicurrency_total_ht = $tabprice[16];
3942 $multicurrency_total_tva = $tabprice[17];
3943 $multicurrency_total_ttc = $tabprice[18];
3944 $pu_ht_devise = $tabprice[19];
3948 if ($ranktouse == -1) {
3949 $rangmax = $this->
line_max($fk_parent_line);
3950 $ranktouse = $rangmax + 1;
3956 $this->line->context = $this->context;
3958 $this->line->fk_facture = $this->id;
3959 $this->line->label = $label;
3960 $this->line->desc = $desc;
3961 $this->line->ref_ext = $ref_ext;
3963 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs($qty) : $qty);
3964 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
3966 $this->line->vat_src_code = $vat_src_code;
3967 $this->line->tva_tx = $txtva;
3968 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
3969 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
3970 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3971 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3973 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ht) : $total_ht);
3974 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ttc) : $total_ttc);
3975 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_tva) : $total_tva);
3976 $this->line->total_localtax1 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax1) : $total_localtax1);
3977 $this->line->total_localtax2 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax2) : $total_localtax2);
3979 $this->line->fk_product = $fk_product;
3980 $this->line->product_type = $product_type;
3982 $this->line->date_start = $date_start;
3983 $this->line->date_end = $date_end;
3984 $this->line->ventil = $ventil;
3985 $this->line->rang = $ranktouse;
3986 $this->line->info_bits = $info_bits;
3987 $this->line->fk_remise_except = $fk_remise_except;
3989 $this->line->special_code = $special_code;
3990 $this->line->fk_parent_line = $fk_parent_line;
3991 $this->line->origin = $origin;
3992 $this->line->origin_id = $origin_id;
3993 $this->line->situation_percent = $situation_percent;
3994 $this->line->fk_prev_id = $fk_prev_id;
3995 $this->line->fk_unit = $fk_unit;
3998 $this->line->fk_fournprice = $fk_fournprice;
3999 $this->line->pa_ht = $pa_ht;
4002 $this->line->fk_multicurrency = $this->fk_multicurrency;
4003 $this->line->multicurrency_code = $this->multicurrency_code;
4004 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht_devise) : $pu_ht_devise);
4006 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ht) : $multicurrency_total_ht);
4007 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_tva) : $multicurrency_total_tva);
4008 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
4010 if (is_array($array_options) && count($array_options) > 0) {
4011 $this->line->array_options = $array_options;
4014 $result = $this->line->insert();
4017 if (!empty($fk_parent_line)) {
4019 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
4020 $linecount = count($this->lines);
4021 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
4027 if (empty($noupdateafterinsertline)) {
4032 $this->db->commit();
4033 return $this->line->id;
4035 $this->error = $this->db->lasterror();
4036 $this->db->rollback();
4040 $this->error = $this->line->error;
4041 $this->errors = $this->line->errors;
4042 $this->db->rollback();
4046 $this->errors[]=
'status of invoice must be Draft to allow use of ->addline()';
4047 dol_syslog(get_class($this).
"::addline status of invoice must be Draft to allow use of ->addline()", LOG_ERR);
4083 public function updateline($rowid, $desc, $pu, $qty,
$remise_percent, $date_start, $date_end, $txtva, $txlocaltax1 = 0, $txlocaltax2 = 0, $price_base_type =
'HT', $info_bits = 0, $type = self::TYPE_STANDARD, $fk_parent_line = 0, $skip_update_total = 0, $fk_fournprice =
null, $pa_ht = 0, $label =
'', $special_code = 0, $array_options = 0, $situation_percent = 100, $fk_unit =
null, $pu_ht_devise = 0, $notrigger = 0, $ref_ext =
'', $rang = 0)
4085 global $conf, $user;
4088 dol_syslog(__METHOD__.
": using line label is deprecated", LOG_WARNING);
4091 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
4093 global $mysoc, $langs;
4095 dol_syslog(get_class($this).
"::updateline rowid=$rowid, desc=$desc, pu=$pu, qty=$qty, remise_percent=$remise_percent, date_start=$date_start, date_end=$date_end, txtva=$txtva, txlocaltax1=$txlocaltax1, txlocaltax2=$txlocaltax2, price_base_type=$price_base_type, info_bits=$info_bits, type=$type, fk_parent_line=$fk_parent_line pa_ht=$pa_ht, special_code=$special_code, fk_unit=$fk_unit, pu_ht_devise=$pu_ht_devise", LOG_DEBUG);
4097 if ($this->statut == self::STATUS_DRAFT) {
4100 if (!$this->error) {
4101 $this->error = $langs->trans(
'invoiceLineProgressError');
4107 if ($date_start && $date_end && $date_start > $date_end) {
4108 $langs->load(
"errors");
4109 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
4119 if (empty($fk_parent_line) || $fk_parent_line < 0) {
4120 $fk_parent_line = 0;
4122 if (empty($special_code) || $special_code == 3) {
4125 if (!isset($situation_percent) || $situation_percent > 100 || (
string) $situation_percent ==
'') {
4126 $situation_percent = 100;
4128 if (empty($ref_ext)) {
4135 $pu_ht_devise =
price2num($pu_ht_devise);
4137 if (!preg_match(
'/\((.*)\)/', $txtva)) {
4157 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
4158 $vat_src_code = $reg[1];
4159 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
4162 $tabprice =
calcul_price_total($qty, $pu,
$remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $mysoc, $localtaxes_type, $situation_percent, $this->multicurrency_tx, $pu_ht_devise);
4164 $total_ht = $tabprice[0];
4165 $total_tva = $tabprice[1];
4166 $total_ttc = $tabprice[2];
4167 $total_localtax1 = $tabprice[9];
4168 $total_localtax2 = $tabprice[10];
4169 $pu_ht = $tabprice[3];
4170 $pu_tva = $tabprice[4];
4171 $pu_ttc = $tabprice[5];
4174 $multicurrency_total_ht = $tabprice[16];
4175 $multicurrency_total_tva = $tabprice[17];
4176 $multicurrency_total_ttc = $tabprice[18];
4177 $pu_ht_devise = $tabprice[19];
4184 $price = ($pu - $remise);
4190 $line->fetch($rowid);
4191 $line->fetch_optionals();
4193 if (!empty($line->fk_product)) {
4194 $product =
new Product($this->db);
4195 $result = $product->fetch($line->fk_product);
4196 $product_type = $product->type;
4198 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_INVOICE) && $product_type == 0 && $product->stock_reel < $qty) {
4199 $langs->load(
"errors");
4200 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnInvoice', $product->ref);
4201 $this->db->rollback();
4206 $staticline = clone $line;
4208 $line->oldline = $staticline;
4209 $this->line = $line;
4210 $this->line->context = $this->context;
4211 $this->line->rang = $rang;
4214 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
4215 $rangmax = $this->
line_max($fk_parent_line);
4216 $this->line->rang = $rangmax + 1;
4219 $this->line->id = $rowid;
4220 $this->line->rowid = $rowid;
4221 $this->line->label = $label;
4222 $this->line->desc = $desc;
4223 $this->line->ref_ext = $ref_ext;
4224 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ?abs($qty) : $qty);
4226 $this->line->vat_src_code = $vat_src_code;
4227 $this->line->tva_tx = $txtva;
4228 $this->line->localtax1_tx = $txlocaltax1;
4229 $this->line->localtax2_tx = $txlocaltax2;
4230 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
4231 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
4234 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ?-abs($pu_ht) : $pu_ht);
4235 $this->line->date_start = $date_start;
4236 $this->line->date_end = $date_end;
4237 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ht) : $total_ht);
4238 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_tva) : $total_tva);
4239 $this->line->total_localtax1 = $total_localtax1;
4240 $this->line->total_localtax2 = $total_localtax2;
4241 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ttc) : $total_ttc);
4242 $this->line->info_bits = $info_bits;
4243 $this->line->special_code = $special_code;
4244 $this->line->product_type = $type;
4245 $this->line->fk_parent_line = $fk_parent_line;
4246 $this->line->skip_update_total = $skip_update_total;
4247 $this->line->situation_percent = $situation_percent;
4248 $this->line->fk_unit = $fk_unit;
4250 $this->line->fk_fournprice = $fk_fournprice;
4251 $this->line->pa_ht = $pa_ht;
4254 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ?-abs($pu_ht_devise) : $pu_ht_devise);
4255 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_ht) : $multicurrency_total_ht);
4256 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_tva) : $multicurrency_total_tva);
4257 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
4259 if (is_array($array_options) && count($array_options) > 0) {
4261 foreach ($array_options as $key => $value) {
4262 $this->line->array_options[$key] = $array_options[$key];
4266 $result = $this->line->update($user, $notrigger);
4269 if (!empty($fk_parent_line)) {
4275 $this->db->commit();
4278 $this->error = $this->line->error;
4279 $this->db->rollback();
4283 $this->error =
"Invoice statut makes operation forbidden";
4297 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd
4298 INNER JOIN '.MAIN_DB_PREFIX.
'facture f ON (fd.fk_facture = f.rowid)
4299 WHERE fd.fk_prev_id = '.((int) $idline).
' AND f.fk_statut <> 0';
4301 $result = $this->db->query(
$sql);
4303 $this->error = $this->db->error();
4307 $obj = $this->db->fetch_object($result);
4309 if ($obj ===
null) {
4312 return ($situation_percent < $obj->situation_percent);
4328 global $mysoc, $user;
4331 if (($line->info_bits & 2) == 2) {
4335 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
4338 if ($percent > 100) {
4341 $line->situation_percent = $percent;
4342 $tabprice =
calcul_price_total($line->qty, $line->subprice, $line->remise_percent, $line->tva_tx, $line->localtax1_tx, $line->localtax2_tx, 0,
'HT', 0, $line->product_type, $mysoc,
'', $percent);
4343 $line->total_ht = $tabprice[0];
4344 $line->total_tva = $tabprice[1];
4345 $line->total_ttc = $tabprice[2];
4346 $line->total_localtax1 = $tabprice[9];
4347 $line->total_localtax2 = $tabprice[10];
4348 $line->multicurrency_total_ht = $tabprice[16];
4349 $line->multicurrency_total_tva = $tabprice[17];
4350 $line->multicurrency_total_ttc = $tabprice[18];
4351 $line->update($user);
4354 if ($update_price) {
4370 dol_syslog(get_class($this).
"::deleteline rowid=".((
int) $rowid), LOG_DEBUG);
4372 if ($this->statut != self::STATUS_DRAFT) {
4373 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
4379 $line->context = $this->context;
4382 $result = $line->fetch($rowid);
4383 if (!($result > 0)) {
4388 if ($id > 0 && $line->fk_facture != $id) {
4389 $this->error =
'ErrorLineIDDoesNotMatchWithObjectID';
4396 $staticline = clone $line;
4397 $line->oldline = $staticline;
4399 if ($line->delete($user) > 0) {
4403 $this->db->commit();
4406 $this->db->rollback();
4407 $this->error = $this->db->lasterror();
4411 $this->db->rollback();
4412 $this->error = $line->error;
4431 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
4432 return $this->
setDiscount($user, $remise, $notrigger);
4447 if (empty($remise)) {
4451 if ($user->hasRight(
'facture',
'creer')) {
4458 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
4459 $sql .=
' SET remise_percent = '.((float) $remise);
4460 $sql .=
" WHERE rowid = ".((int) $this->
id);
4461 $sql .=
' AND fk_statut = '.self::STATUS_DRAFT;
4464 $resql = $this->db->query(
$sql);
4466 $this->errors[] = $this->db->error();
4470 if (!$notrigger && empty($error)) {
4480 $this->remise_percent = $remise;
4483 $this->db->commit();
4486 foreach ($this->errors as $errmsg) {
4487 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
4488 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4490 $this->db->rollback();
4511 if (empty($remise)) {
4515 if ($user->hasRight(
'facture',
'creer')) {
4522 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
4523 $sql .=
' SET remise_absolue = '.((float) $remise);
4524 $sql .=
" WHERE rowid = ".((int) $this->
id);
4525 $sql .=
' AND fk_statut = '.self::STATUS_DRAFT;
4528 $resql = $this->db->query(
$sql);
4530 $this->errors[] = $this->db->error();
4535 $this->oldcopy = clone $this;
4536 $this->remise_absolue = $remise;
4540 if (!$notrigger && empty($error)) {
4550 $this->db->commit();
4553 foreach ($this->errors as $errmsg) {
4554 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
4555 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4557 $this->db->rollback();
4575 global $conf, $langs;
4577 if ($this->module_source ==
'takepos') {
4578 $langs->load(
'cashdesk');
4580 $moduleName =
'takepos';
4581 $moduleSourceName =
'Takepos';
4582 $addonConstName =
'TAKEPOS_REF_ADDON';
4585 if (empty($conf->global->TAKEPOS_REF_ADDON)) {
4586 $conf->global->TAKEPOS_REF_ADDON =
'mod_takepos_ref_simple';
4589 $addon = $conf->global->TAKEPOS_REF_ADDON;
4591 $langs->load(
'bills');
4593 $moduleName =
'facture';
4594 $moduleSourceName =
'Invoice';
4595 $addonConstName =
'FACTURE_ADDON';
4598 if (empty($conf->global->FACTURE_ADDON)) {
4599 $conf->global->FACTURE_ADDON =
'mod_facture_terre';
4600 } elseif ($conf->global->FACTURE_ADDON ==
'terre') {
4601 $conf->global->FACTURE_ADDON =
'mod_facture_terre';
4602 } elseif ($conf->global->FACTURE_ADDON ==
'mercure') {
4603 $conf->global->FACTURE_ADDON =
'mod_facture_mercure';
4606 $addon = $conf->global->FACTURE_ADDON;
4609 if (!empty($addon)) {
4610 dol_syslog(
"Call getNextNumRef with ".$addonConstName.
" = ".$conf->global->FACTURE_ADDON.
", thirdparty=".$soc->name.
", type=".$soc->typent_code.
", mode=".$mode, LOG_DEBUG);
4614 $file = $addon.
'.php';
4615 $classname = $addon;
4619 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
4620 foreach ($dirmodels as $reldir) {
4621 $dir =
dol_buildpath($reldir.
'core/modules/'.$moduleName.
'/');
4624 if (is_file($dir.$file) && is_readable($dir.$file)) {
4625 $mybool |= include_once $dir.$file;
4631 $file = $addon.
'/'.$addon.
'.modules.php';
4632 $classname =
'mod_'.$moduleName.
'_'.$addon;
4633 $classname = preg_replace(
'/\-.*$/',
'', $classname);
4635 foreach ($conf->file->dol_document_root as $dirroot) {
4636 $dir = $dirroot.
'/core/modules/'.$moduleName.
'/';
4639 if (is_file($dir.$file) && is_readable($dir.$file)) {
4640 $mybool |= include_once $dir.$file;
4650 $obj =
new $classname();
4652 $numref = $obj->getNextValue($soc, $this, $mode);
4659 if ($mode !=
'last' && !$numref) {
4660 $this->error = $obj->error;
4666 $langs->load(
'errors');
4667 print $langs->trans(
'Error').
' '.$langs->trans(
'ErrorModuleSetupNotComplete', $langs->transnoentitiesnoconv($moduleSourceName));
4680 $sql =
'SELECT c.rowid, datec, date_valid as datev, tms as datem,';
4681 $sql .=
' date_closing as dateclosing,';
4682 $sql .=
' fk_user_author, fk_user_valid, fk_user_closing';
4683 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as c';
4684 $sql .=
' WHERE c.rowid = '.((int) $id);
4686 $result = $this->db->query(
$sql);
4688 if ($this->db->num_rows($result)) {
4689 $obj = $this->db->fetch_object($result);
4690 $this->
id = $obj->rowid;
4691 if ($obj->fk_user_author) {
4692 $cuser =
new User($this->db);
4693 $cuser->fetch($obj->fk_user_author);
4694 $this->user_creation = $cuser;
4696 if ($obj->fk_user_valid) {
4697 $vuser =
new User($this->db);
4698 $vuser->fetch($obj->fk_user_valid);
4699 $this->user_validation = $vuser;
4701 if ($obj->fk_user_closing) {
4702 $cluser =
new User($this->db);
4703 $cluser->fetch($obj->fk_user_closing);
4704 $this->user_closing = $cluser;
4707 $this->date_creation = $this->db->jdate($obj->datec);
4708 $this->date_modification = $this->db->jdate($obj->datem);
4709 $this->date_validation = $this->db->jdate($obj->datev);
4710 $this->date_closing = $this->db->jdate($obj->dateclosing);
4712 $this->db->free($result);
4733 public function liste_array($shortlist = 0, $draft = 0, $excluser =
'', $socid = 0, $limit = 0, $offset = 0, $sortfield =
'f.datef,f.rowid', $sortorder =
'DESC')
4736 global $conf, $user;
4740 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
4741 $sql .=
" f.rowid as fid, f.ref as ref, f.datef as df";
4742 if (empty($user->rights->societe->client->voir) && !$socid) {
4743 $sql .=
", sc.fk_soc, sc.fk_user";
4745 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"facture as f";
4746 if (empty($user->rights->societe->client->voir) && !$socid) {
4747 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
4749 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
4750 $sql .=
" AND f.fk_soc = s.rowid";
4751 if (empty($user->rights->societe->client->voir) && !$socid) {
4752 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4755 $sql .=
" AND s.rowid = ".((int) $socid);
4758 $sql .=
" AND f.fk_statut = ".self::STATUS_DRAFT;
4760 if (is_object($excluser)) {
4761 $sql .=
" AND f.fk_user_author <> ".((int) $excluser->id);
4763 $sql .= $this->db->order($sortfield, $sortorder);
4764 $sql .= $this->db->plimit($limit, $offset);
4766 $result = $this->db->query(
$sql);
4768 $numc = $this->db->num_rows($result);
4771 while ($i < $numc) {
4772 $obj = $this->db->fetch_object($result);
4774 if ($shortlist == 1) {
4775 $ga[$obj->fid] = $obj->ref;
4776 } elseif ($shortlist == 2) {
4777 $ga[$obj->fid] = $obj->ref.
' ('.$obj->name.
')';
4779 $ga[$i][
'id'] = $obj->fid;
4780 $ga[$i][
'ref'] = $obj->ref;
4781 $ga[$i][
'name'] = $obj->name;
4810 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut as status, f.paye as paid,";
4811 $sql .=
" ff.rowid as rowidnext";
4813 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
4814 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON f.rowid = pf.fk_facture";
4815 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as ff ON f.rowid = ff.fk_facture_source";
4816 $sql .=
" WHERE (f.fk_statut = ".self::STATUS_VALIDATED.
" OR (f.fk_statut = ".self::STATUS_ABANDONED.
" AND f.close_code = '".self::CLOSECODE_ABANDONED.
"'))";
4817 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
4818 $sql .=
" AND f.paye = 0";
4819 $sql .=
" AND pf.fk_paiement IS NULL";
4820 $sql .=
" AND ff.fk_statut IS NULL";
4822 $sql .=
" AND f.fk_soc = ".((int) $socid);
4825 $sql .=
" ORDER BY f.ref";
4827 dol_syslog(get_class($this).
"::list_replacable_invoices", LOG_DEBUG);
4828 $resql = $this->db->query(
$sql);
4830 while ($obj = $this->db->fetch_object($resql)) {
4831 $return[$obj->rowid] = array(
4832 'id' => $obj->rowid,
4834 'status' => $obj->status,
4835 'paid' => $obj->paid,
4842 $this->error = $this->db->error();
4865 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut, f.type, f.paye, pf.fk_paiement";
4866 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
4867 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON f.rowid = pf.fk_facture";
4868 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as ff ON (f.rowid = ff.fk_facture_source AND ff.type=".self::TYPE_REPLACEMENT.
")";
4869 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
4870 $sql .=
" AND f.fk_statut in (".self::STATUS_VALIDATED.
",".self::STATUS_CLOSED.
")";
4874 $sql .=
" AND ff.type IS NULL";
4875 $sql .=
" AND f.type <> ".self::TYPE_CREDIT_NOTE;
4877 if (!empty($conf->global->INVOICE_USE_SITUATION_CREDIT_NOTE)) {
4879 $sql .=
" AND (f.type <> ".self::TYPE_SITUATION.
" OR f.rowid IN ";
4880 $sql .=
'(SELECT MAX(fs.rowid)';
4881 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as fs";
4882 $sql .=
" WHERE fs.entity IN (".getEntity(
'invoice').
")";
4883 $sql .=
" AND fs.type = ".self::TYPE_SITUATION;
4884 $sql .=
" AND fs.fk_statut IN (".self::STATUS_VALIDATED.
",".self::STATUS_CLOSED.
")";
4886 $sql .=
" AND fs.fk_soc = ".((int) $socid);
4888 $sql .=
" GROUP BY fs.situation_cycle_ref)";
4891 $sql .=
" AND f.type <> ".self::TYPE_SITUATION;
4895 $sql .=
" AND f.fk_soc = ".((int) $socid);
4897 $sql .=
" ORDER BY f.ref";
4899 dol_syslog(get_class($this).
"::list_qualified_avoir_invoices", LOG_DEBUG);
4900 $resql = $this->db->query(
$sql);
4902 while ($obj = $this->db->fetch_object($resql)) {
4904 if ($obj->fk_statut == self::STATUS_VALIDATED) {
4907 if ($obj->fk_statut == self::STATUS_CLOSED) {
4912 $paymentornot = ($obj->fk_paiement ? 1 : 0);
4913 $return[$obj->rowid] = array(
'ref'=>$obj->ref,
'status'=>$obj->fk_statut,
'type'=>$obj->type,
'paye'=>$obj->paye,
'paymentornot'=>$paymentornot);
4919 $this->error = $this->db->error();
4935 global $conf, $langs;
4939 $sql =
"SELECT f.rowid, f.date_lim_reglement as datefin, f.fk_statut, f.total_ht";
4940 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
4941 if (empty($user->rights->societe->client->voir) && !$user->socid) {
4942 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON f.fk_soc = sc.fk_soc";
4943 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4946 $sql .= $clause.
" f.paye=0";
4947 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
4948 $sql .=
" AND f.fk_statut = ".self::STATUS_VALIDATED;
4950 $sql .=
" AND f.fk_soc = ".((int) $user->socid);
4953 $resql = $this->db->query(
$sql);
4955 $langs->load(
"bills");
4959 $response->warning_delay = $conf->facture->client->warning_delay / 60 / 60 / 24;
4960 $response->label = $langs->trans(
"CustomerBillsUnpaid");
4961 $response->labelShort = $langs->trans(
"Unpaid");
4962 $response->url = DOL_URL_ROOT.
'/compta/facture/list.php?search_status=1&mainmenu=billing&leftmenu=customers_bills';
4965 $generic_facture =
new Facture($this->db);
4967 while ($obj = $this->db->fetch_object($resql)) {
4968 $generic_facture->date_lim_reglement = $this->db->jdate($obj->datefin);
4969 $generic_facture->statut = $obj->fk_statut;
4971 $response->nbtodo++;
4972 $response->total += $obj->total_ht;
4974 if ($generic_facture->hasDelay()) {
4975 $response->nbtodolate++;
4976 $response->url_late = DOL_URL_ROOT.
'/compta/facture/list.php?search_option=late&mainmenu=billing&leftmenu=customers_bills';
4980 $this->db->free($resql);
4984 $this->error = $this->db->error();
5023 global $conf, $langs, $user;
5027 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
5032 $sql =
"SELECT rowid";
5033 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
5034 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
5035 $sql .= $this->db->plimit(100);
5037 $resql = $this->db->query(
$sql);
5039 $num_prods = $this->db->num_rows($resql);
5041 while ($i < $num_prods) {
5043 $row = $this->db->fetch_row($resql);
5044 $prodids[$i] = $row[0];
5048 if (empty($num_prods)) {
5055 $this->
ref =
'SPECIMEN';
5056 $this->specimen = 1;
5058 $this->date = $nownotime;
5059 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
5060 $this->cond_reglement_id = 1;
5061 $this->cond_reglement_code =
'RECEP';
5063 $this->mode_reglement_id = 0;
5064 $this->mode_reglement_code =
'';
5066 $this->note_public =
'This is a comment (public)';
5067 $this->note_private =
'This is a comment (private)';
5068 $this->note =
'This is a comment (private)';
5070 $this->fk_user_author = $user->id;
5072 $this->multicurrency_tx = 1;
5073 $this->multicurrency_code = $conf->currency;
5075 $this->fk_incoterms = 0;
5076 $this->location_incoterms =
'';
5078 if (empty($option) || $option !=
'nolines') {
5082 while ($xnbp < $nbp) {
5084 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
5086 $line->subprice = 100;
5087 $line->tva_tx = 19.6;
5088 $line->localtax1_tx = 0;
5089 $line->localtax2_tx = 0;
5090 $line->remise_percent = 0;
5092 $prodid = mt_rand(1, $num_prods);
5093 $line->fk_product = $prodids[$prodid];
5095 $line->total_ht = -100;
5096 $line->total_ttc = -119.6;
5097 $line->total_tva = -19.6;
5098 $line->multicurrency_total_ht = -200;
5099 $line->multicurrency_total_ttc = -239.2;
5100 $line->multicurrency_total_tva = -39.2;
5101 } elseif ($xnbp == 2) {
5102 $line->subprice = -100;
5103 $line->total_ht = -100;
5104 $line->total_ttc = -119.6;
5105 $line->total_tva = -19.6;
5106 $line->remise_percent = 0;
5107 $line->multicurrency_total_ht = -200;
5108 $line->multicurrency_total_ttc = -239.2;
5109 $line->multicurrency_total_tva = -39.2;
5110 } elseif ($xnbp == 3) {
5111 $prodid = mt_rand(1, $num_prods);
5112 $line->fk_product = $prodids[$prodid];
5113 $line->total_ht = 50;
5114 $line->total_ttc = 59.8;
5115 $line->total_tva = 9.8;
5116 $line->multicurrency_total_ht = 100;
5117 $line->multicurrency_total_ttc = 119.6;
5118 $line->multicurrency_total_tva = 19.6;
5119 $line->remise_percent = 50;
5122 $prodid = mt_rand(1, $num_prods);
5123 $line->fk_product = $prodids[$prodid];
5124 $line->total_ht = 100;
5125 $line->total_ttc = 119.6;
5126 $line->total_tva = 19.6;
5127 $line->multicurrency_total_ht = 200;
5128 $line->multicurrency_total_ttc = 239.2;
5129 $line->multicurrency_total_tva = 39.2;
5130 $line->remise_percent = 0;
5133 $this->lines[$xnbp] = $line;
5136 $this->total_ht += $line->total_ht;
5137 $this->total_tva += $line->total_tva;
5138 $this->total_ttc += $line->total_ttc;
5140 $this->multicurrency_total_ht += $line->multicurrency_total_ht;
5141 $this->multicurrency_total_tva += $line->multicurrency_total_tva;
5142 $this->multicurrency_total_ttc += $line->multicurrency_total_ttc;
5146 $this->revenuestamp = 0;
5150 $line->desc = $langs->trans(
"Description").
" (offered line)";
5152 $line->subprice = 100;
5153 $line->tva_tx = 19.6;
5154 $line->localtax1_tx = 0;
5155 $line->localtax2_tx = 0;
5156 $line->remise_percent = 100;
5157 $line->total_ht = 0;
5158 $line->total_ttc = 0;
5159 $line->total_tva = 0;
5160 $line->multicurrency_total_ht = 0;
5161 $line->multicurrency_total_ttc = 0;
5162 $line->multicurrency_total_tva = 0;
5163 $prodid = mt_rand(1, $num_prods);
5164 $line->fk_product = $prodids[$prodid];
5166 $this->lines[$xnbp] = $line;
5180 global $conf, $user;
5182 $this->nb = array();
5186 $sql =
"SELECT count(f.rowid) as nb";
5187 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
5188 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON f.fk_soc = s.rowid";
5189 if (empty($user->rights->societe->client->voir) && !$user->socid) {
5190 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
5191 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
5194 $sql .=
" ".$clause.
" f.entity IN (".
getEntity(
'invoice').
")";
5196 $resql = $this->db->query(
$sql);
5198 while ($obj = $this->db->fetch_object($resql)) {
5199 $this->nb[
"invoices"] = $obj->nb;
5201 $this->db->free($resql);
5205 $this->error = $this->db->error();
5231 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
5233 global $conf, $langs;
5235 $outputlangs->loadLangs(array(
"bills",
"products"));
5239 $thisTypeConfName =
'FACTURE_ADDON_PDF_'.$this->type;
5241 if (!empty($this->model_pdf)) {
5242 $modele = $this->model_pdf;
5243 } elseif (!empty($this->modelpdf)) {
5244 $modele = $this->modelpdf;
5245 } elseif (!empty($conf->global->$thisTypeConfName)) {
5246 $modele = $conf->global->$thisTypeConfName;
5247 } elseif (!empty($conf->global->FACTURE_ADDON_PDF)) {
5248 $modele = $conf->global->FACTURE_ADDON_PDF;
5252 $modelpath =
"core/modules/facture/doc/";
5254 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
5264 $sql =
'SELECT max(situation_cycle_ref) FROM '.MAIN_DB_PREFIX.
'facture as f';
5265 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice', 0).
")";
5266 $resql = $this->db->query(
$sql);
5268 if ($this->db->num_rows($resql) > 0) {
5269 $res = $this->db->fetch_array($resql);
5270 $ref = $res[
'max(situation_cycle_ref)'];
5275 $this->db->free($resql);
5278 $this->error = $this->db->lasterror();
5293 return ($this->situation_counter == 1);
5307 $sql =
'SELECT rowid FROM '.MAIN_DB_PREFIX.
'facture';
5308 $sql .=
' WHERE situation_cycle_ref = '.((int) $this->situation_cycle_ref);
5309 $sql .=
' AND situation_counter < '.((int) $this->situation_counter);
5310 $sql .=
' AND entity = '.($this->entity > 0 ? $this->entity : $conf->entity);
5311 $resql = $this->db->query(
$sql);
5313 if ($resql && $this->db->num_rows($resql) > 0) {
5314 while ($row = $this->db->fetch_object($resql)) {
5316 $situation =
new Facture($this->db);
5317 $situation->fetch($id);
5318 $res[] = $situation;
5321 $this->error = $this->db->error();
5342 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture SET situation_final = '.((int) $this->situation_final).
' WHERE rowid = '.((int) $this->
id);
5345 $resql = $this->db->query(
$sql);
5347 $this->errors[] = $this->db->error();
5351 if (!$notrigger && empty($error)) {
5361 $this->db->commit();
5364 foreach ($this->errors as $errmsg) {
5365 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
5366 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
5368 $this->db->rollback();
5384 if (!empty($this->situation_cycle_ref)) {
5386 $sql =
'SELECT max(situation_counter) FROM '.MAIN_DB_PREFIX.
'facture';
5387 $sql .=
' WHERE situation_cycle_ref = '.((int) $this->situation_cycle_ref);
5388 $sql .=
' AND entity = '.($this->entity > 0 ? $this->entity : $conf->entity);
5389 $resql = $this->db->query(
$sql);
5391 if ($resql && $this->db->num_rows($resql) > 0) {
5392 $res = $this->db->fetch_array($resql);
5393 $last = $res[
'max(situation_counter)'];
5394 return ($last == $this->situation_counter);
5396 $this->error = $this->db->lasterror();
5397 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
5455 $hasDelay = $this->date_lim_reglement < ($now - $conf->facture->client->warning_delay);
5456 if ($hasDelay && !empty($this->retained_warranty) && !empty($this->retained_warranty_date_limit)) {
5458 $totalpaid = floatval($totalpaid);
5460 if ($totalpaid >= 0 && $RetainedWarrantyAmount >= 0) {
5461 if (($totalpaid < $this->total_ttc - $RetainedWarrantyAmount) && $this->date_lim_reglement < ($now - $conf->facture->client->warning_delay)) {
5463 } elseif ($totalpaid < $this->total_ttc && $this->retained_warranty_date_limit < ($now - $conf->facture->client->warning_delay)) {
5486 $displayWarranty =
false;
5487 if (!empty($this->retained_warranty)) {
5488 $displayWarranty =
true;
5492 $displayWarranty =
false;
5493 if (!empty($this->situation_final)) {
5494 $displayWarranty =
true;
5497 $displayWarranty =
true;
5499 foreach ($this->lines as $i => $line) {
5500 if ($line->product_type < 2 && $line->situation_percent < 100) {
5501 $displayWarranty =
false;
5509 return $displayWarranty;
5519 if (empty($this->retained_warranty)) {
5523 $retainedWarrantyAmount = 0;
5527 $displayWarranty =
true;
5529 if (!empty($this->lines)) {
5530 foreach ($this->lines as $i => $line) {
5531 if ($line->product_type < 2 && $line->situation_percent < 100) {
5532 $displayWarranty =
false;
5538 if ($displayWarranty && !empty($this->situation_final)) {
5540 $TPreviousIncoice = $this->tab_previous_situation_invoice;
5543 foreach ($TPreviousIncoice as &$fac) {
5544 $total2BillWT += $fac->total_ttc;
5546 $total2BillWT += $this->total_ttc;
5548 $retainedWarrantyAmount = $total2BillWT * $this->retained_warranty / 100;
5554 $retainedWarrantyAmount = $this->total_ttc * $this->retained_warranty / 100;
5557 if ($rounding < 0) {
5558 $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT, $conf->global->MAIN_MAX_DECIMALS_TOT);
5561 if ($rounding > 0) {
5562 return round($retainedWarrantyAmount, $rounding);
5565 return $retainedWarrantyAmount;
5576 dol_syslog(get_class($this).
'::setRetainedWarranty('.$value.
')');
5578 if ($this->statut >= 0) {
5579 $fieldname =
'retained_warranty';
5580 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
5581 $sql .=
" SET ".$fieldname.
" = ".((
float) $value);
5582 $sql .=
' WHERE rowid='.((int) $this->
id);
5584 if ($this->db->query(
$sql)) {
5585 $this->retained_warranty = floatval($value);
5588 dol_syslog(get_class($this).
'::setRetainedWarranty Erreur '.
$sql.
' - '.$this->db->error());
5589 $this->error = $this->db->error();
5593 dol_syslog(get_class($this).
'::setRetainedWarranty, status of the object is incompatible');
5594 $this->error =
'Status of the object is incompatible '.$this->statut;
5609 if (!$timestamp && $dateYmd) {
5610 $timestamp = $this->db->jdate($dateYmd);
5614 dol_syslog(get_class($this).
'::setRetainedWarrantyDateLimit('.$timestamp.
')');
5615 if ($this->statut >= 0) {
5616 $fieldname =
'retained_warranty_date_limit';
5617 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
5618 $sql .=
" SET ".$fieldname.
" = ".(strval($timestamp) !=
'' ?
"'".$this->db->idate($timestamp).
"'" :
'null');
5619 $sql .=
' WHERE rowid = '.((int) $this->
id);
5621 if ($this->db->query(
$sql)) {
5622 $this->retained_warranty_date_limit = $timestamp;
5625 dol_syslog(get_class($this).
'::setRetainedWarrantyDateLimit Erreur '.
$sql.
' - '.$this->db->error());
5626 $this->error = $this->db->error();
5630 dol_syslog(get_class($this).
'::setRetainedWarrantyDateLimit, status of the object is incompatible');
5631 $this->error =
'Status of the object is incompatible '.$this->statut;
5650 global $conf, $langs, $user;
5656 $errorsMsg = array();
5658 $langs->load(
"bills");
5661 $this->output .= $langs->trans(
'ModuleNotEnabled', $langs->transnoentitiesnoconv(
"Facture"));
5664 if (!in_array($datetouse, array(
'duedate',
'invoicedate'))) {
5665 $this->output .=
'Bad value for parameter datetouse. Must be "duedate" or "invoicedate"';
5675 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
5676 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
5677 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
5678 $formmail =
new FormMail($this->db);
5683 $tmpinvoice =
new Facture($this->db);
5688 $sql =
"SELECT rowid as id FROM ".MAIN_DB_PREFIX.
"facture as f";
5689 if (!empty($paymentmode) && $paymentmode !=
'all') {
5690 $sql .=
", ".MAIN_DB_PREFIX.
"c_paiement as cp";
5692 $sql .=
" WHERE f.paye = 0";
5693 $sql .=
" AND f.fk_statut = ".self::STATUS_VALIDATED;
5694 if ($datetouse ==
'invoicedate') {
5695 $sql .=
" AND f.datef = '".$this->db->idate($tmpidate,
'gmt').
"'";
5697 $sql .=
" AND f.date_lim_reglement = '".$this->db->idate($tmpidate,
'gmt').
"'";
5699 $sql .=
" AND f.entity IN (".getEntity(
'facture', 0).
")";
5700 if (!empty($paymentmode) && $paymentmode !=
'all') {
5701 $sql .=
" AND f.fk_mode_reglement = cp.id AND cp.code = '".$this->db->escape($paymentmode).
"'";
5704 if ($datetouse ==
'invoicedate') {
5705 $sql .= $this->db->order(
"datef",
"ASC");
5707 $sql .= $this->db->order(
"date_lim_reglement",
"ASC");
5710 $resql = $this->db->query(
$sql);
5713 if ($datetouse ==
'invoicedate') {
5714 $this->output .= $langs->transnoentitiesnoconv(
"SearchValidatedInvoicesWithDate", $stmpidate);
5716 $this->output .= $langs->transnoentitiesnoconv(
"SearchUnpaidInvoicesWithDueDate", $stmpidate);
5718 if (!empty($paymentmode) && $paymentmode !=
'all') {
5719 $this->output .=
' ('.$langs->transnoentitiesnoconv(
"PaymentMode").
' '.$paymentmode.
')';
5721 $this->output .=
'<br>';
5724 while ($obj = $this->db->fetch_object($resql)) {
5727 $res = $tmpinvoice->fetch($obj->id);
5729 $tmpinvoice->fetch_thirdparty();
5731 $outputlangs =
new Translate(
'', $conf);
5732 if ($tmpinvoice->thirdparty->default_lang) {
5733 $outputlangs->setDefaultLang($tmpinvoice->thirdparty->default_lang);
5734 $outputlangs->loadLangs(array(
"main",
"bills"));
5736 $outputlangs = $langs;
5740 $arraymessage = $formmail->getEMailTemplate($this->db,
'facture_send', $user, $outputlangs, (is_numeric($template) ? $template : 0), 1, (is_numeric($template) ?
'' : $template));
5741 if (is_numeric($arraymessage) && $arraymessage <= 0) {
5742 $langs->load(
"errors");
5743 $this->output .= $langs->trans(
'ErrorFailedToFindEmailTemplate', $template);
5756 $sendTopic =
make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv(
'InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
5759 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
5765 if ($forcerecipient) {
5766 $to = array($forcerecipient);
5768 $res = $tmpinvoice->fetch_thirdparty();
5769 $recipient = $tmpinvoice->thirdparty;
5771 $tmparraycontact = $tmpinvoice->liste_contact(-1,
'external', 0,
'BILLING');
5772 if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
5773 foreach ($tmparraycontact as $data_email) {
5774 if (!empty($data_email[
'email'])) {
5775 $to[] = $tmpinvoice->thirdparty->contact_get_property($data_email[
'id'],
'email');
5779 if (empty($to) && !empty($recipient->email)) {
5780 $to[] = $recipient->email;
5783 $errormesg =
"Failed to send remind to thirdparty id=".$tmpinvoice->socid.
". No email defined for invoice or customer.";
5787 $errormesg =
"Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
5794 if (!empty($arraymessage->email_from)) {
5795 $from = $arraymessage->email_from;
5798 $errormesg =
"Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
5802 if (!$error && !empty($to)) {
5805 $to = implode(
',', $to);
5806 if (!empty($arraymessage->email_to)) {
5807 $to = $to.
','.$arraymessage->email_to;
5811 $errors_to = $conf->global->MAIN_MAIL_ERRORS_TO;
5813 $trackid =
'inv'.$tmpinvoice->id;
5814 $sendcontext =
'standard';
5817 if (!empty($arraymessage->email_tocc)) {
5818 $email_tocc = $arraymessage->email_tocc;
5822 if (!empty($arraymessage->email_tobcc)) {
5823 $email_tobcc = $arraymessage->email_tobcc;
5827 $cMailFile =
new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(), $email_tocc, $email_tobcc, 0, 1, $errors_to,
'', $trackid,
'', $sendcontext,
'');
5830 if ($cMailFile->sendfile()) {
5834 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
5839 $actioncomm->type_code =
'AC_OTH_AUTO';
5840 $actioncomm->socid = $tmpinvoice->thirdparty->id;
5841 $actioncomm->contact_id = 0;
5843 $actioncomm->code =
'AC_EMAIL';
5844 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateOK (nbdays='.$nbdays.
' paymentmode='.$paymentmode.
' template='.$template.
' datetouse='.$datetouse.
' forcerecipient='.$forcerecipient.
')';
5845 $actioncomm->note_private = $sendContent;
5846 $actioncomm->fk_project = $tmpinvoice->fk_project;
5847 $actioncomm->datep =
dol_now();
5848 $actioncomm->datef = $actioncomm->datep;
5849 $actioncomm->percentage = -1;
5850 $actioncomm->authorid = $user->id;
5851 $actioncomm->userownerid = $user->id;
5853 $actioncomm->email_msgid = $cMailFile->msgid;
5854 $actioncomm->email_subject = $sendTopic;
5855 $actioncomm->email_from = $from;
5856 $actioncomm->email_sender =
'';
5857 $actioncomm->email_to = $to;
5861 $actioncomm->errors_to = $errors_to;
5863 $actioncomm->elementtype =
'invoice';
5864 $actioncomm->fk_element = $tmpinvoice->id;
5868 $actioncomm->create($user);
5870 $errormesg = $cMailFile->error.
' : '.$to;
5874 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
5879 $actioncomm->type_code =
'AC_OTH_AUTO';
5880 $actioncomm->socid = $tmpinvoice->thirdparty->id;
5881 $actioncomm->contact_id = 0;
5883 $actioncomm->code =
'AC_EMAIL';
5884 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateKO';
5885 $actioncomm->note_private = $errormesg;
5886 $actioncomm->fk_project = $tmpinvoice->fk_project;
5887 $actioncomm->datep =
dol_now();
5888 $actioncomm->datef = $actioncomm->datep;
5889 $actioncomm->percentage = -1;
5890 $actioncomm->authorid = $user->id;
5891 $actioncomm->userownerid = $user->id;
5893 $actioncomm->email_msgid = $cMailFile->msgid;
5894 $actioncomm->email_from = $from;
5895 $actioncomm->email_sender =
'';
5896 $actioncomm->email_to = $to;
5900 $actioncomm->errors_to = $errors_to;
5904 $actioncomm->create($user);
5907 $this->db->commit();
5911 $errorsMsg[] = $errormesg;
5914 $errorsMsg[] =
'Failed to fetch record invoice with ID = '.$obj->id;
5924 $this->output .=
'Nb of emails sent : '.$nbMailSend;
5927 $this->error =
'Nb of emails sent : '.$nbMailSend.
', '.(!empty($errorsMsg)) ? join(
', ', $errorsMsg) : $error;
5941 $sql =
"SELECT datef";
5942 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
5943 $sql .=
" WHERE type = " . (int) $this->
type ;
5944 $sql .=
" AND date_valid IS NOT NULL";
5945 $sql .=
" AND entity IN (".getEntity(
'invoice').
")";
5946 $sql .=
" ORDER BY datef DESC LIMIT 1";
5948 $result = $this->db->query(
$sql);
5951 if ($this->db->num_rows($result)) {
5952 $obj = $this->db->fetch_object($result);
5953 $last_date = $this->db->jdate($obj->datef);
5954 $invoice_date = $this->date;
5956 $is_last_of_same_type = $invoice_date >= $last_date;
5957 if ($allow_validated_drafts) {
5958 $is_last_of_same_type = $is_last_of_same_type || (!strpos($this->
ref,
'PROV') && $this->status ==
self::STATUS_DRAFT);
5961 return array($is_last_of_same_type, $last_date);
5982 $selected = (empty($arraydata[
'selected']) ? 0 : $arraydata[
'selected']);
5984 $return =
'<div class="box-flex-item box-flex-grow-zero">';
5985 $return .=
'<div class="info-box info-box-sm">';
5986 $return .=
'<span class="info-box-icon bg-infobox-action">';
5989 $return .=
'</span>';
5990 $return .=
'<div class="info-box-content">';
5991 $return .=
'<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this,
'getNomUrl') ? $this->
getNomUrl(1) : $this->ref).
'</span>';
5992 $return .=
'<input id="cb'.$this->id.
'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->
id.
'"'.($selected ?
' checked="checked"' :
'').
'>';
5993 if (property_exists($this,
'socid')) {
5994 $return .=
'<br><span class="info-box-label">'.$this->socid.
'</span>';
5996 if (property_exists($this,
'fk_user_author')) {
5997 $return .=
'<br><span class="info-box-label">'.$this->fk_user_author.
'</span>';
5999 if (method_exists($this,
'getLibStatut')) {
6000 $return .=
'<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).
'</div>';
6002 $return .=
'</div>';
6003 $return .=
'</div>';
6004 $return .=
'</div>';
6018 public $element =
'facturedet';
6023 public $table_element =
'facturedet';
6040 public $localtax1_type;
6041 public $localtax2_type;
6042 public $fk_remise_except;
6045 public $fk_fournprice;
6050 public $remise_percent;
6052 public $special_code;
6060 public $fk_code_ventilation = 0;
6065 public $skip_update_total;
6070 public $situation_percent;
6078 public $fk_multicurrency;
6079 public $multicurrency_code;
6080 public $multicurrency_subprice;
6081 public $multicurrency_total_ht;
6082 public $multicurrency_total_tva;
6083 public $multicurrency_total_ttc;
6103 $sql =
'SELECT fd.rowid, fd.fk_facture, fd.fk_parent_line, fd.fk_product, fd.product_type, fd.label as custom_label, fd.description, fd.price, fd.qty, fd.vat_src_code, fd.tva_tx,';
6104 $sql .=
' fd.localtax1_tx, fd. localtax2_tx, fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice, fd.ref_ext,';
6105 $sql .=
' fd.date_start as date_start, fd.date_end as date_end, fd.fk_product_fournisseur_price as fk_fournprice, fd.buy_price_ht as pa_ht,';
6106 $sql .=
' fd.info_bits, fd.special_code, fd.total_ht, fd.total_tva, fd.total_ttc, fd.total_localtax1, fd.total_localtax2, fd.rang,';
6107 $sql .=
' fd.fk_code_ventilation,';
6108 $sql .=
' fd.fk_unit, fd.fk_user_author, fd.fk_user_modif,';
6109 $sql .=
' fd.situation_percent, fd.fk_prev_id,';
6110 $sql .=
' fd.multicurrency_subprice,';
6111 $sql .=
' fd.multicurrency_total_ht,';
6112 $sql .=
' fd.multicurrency_total_tva,';
6113 $sql .=
' fd.multicurrency_total_ttc,';
6114 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc';
6115 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet as fd';
6116 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON fd.fk_product = p.rowid';
6117 $sql .=
' WHERE fd.rowid = '.((int) $rowid);
6119 $result = $this->db->query(
$sql);
6121 $objp = $this->db->fetch_object($result);
6124 $this->error =
'InvoiceLine with id '. $rowid .
' not found sql='.
$sql;
6128 $this->
rowid = $objp->rowid;
6129 $this->
id = $objp->rowid;
6130 $this->fk_facture = $objp->fk_facture;
6131 $this->fk_parent_line = $objp->fk_parent_line;
6132 $this->label = $objp->custom_label;
6133 $this->desc = $objp->description;
6134 $this->qty = $objp->qty;
6135 $this->subprice = $objp->subprice;
6136 $this->ref_ext = $objp->ref_ext;
6137 $this->vat_src_code = $objp->vat_src_code;
6138 $this->tva_tx = $objp->tva_tx;
6139 $this->localtax1_tx = $objp->localtax1_tx;
6140 $this->localtax2_tx = $objp->localtax2_tx;
6141 $this->remise_percent = $objp->remise_percent;
6142 $this->fk_remise_except = $objp->fk_remise_except;
6143 $this->fk_product = $objp->fk_product;
6144 $this->product_type = $objp->product_type;
6145 $this->date_start = $this->db->jdate($objp->date_start);
6146 $this->date_end = $this->db->jdate($objp->date_end);
6147 $this->info_bits = $objp->info_bits;
6148 $this->tva_npr = ($objp->info_bits & 1 == 1) ? 1 : 0;
6149 $this->special_code = $objp->special_code;
6150 $this->total_ht = $objp->total_ht;
6151 $this->total_tva = $objp->total_tva;
6152 $this->total_localtax1 = $objp->total_localtax1;
6153 $this->total_localtax2 = $objp->total_localtax2;
6154 $this->total_ttc = $objp->total_ttc;
6155 $this->fk_code_ventilation = $objp->fk_code_ventilation;
6156 $this->rang = $objp->rang;
6157 $this->fk_fournprice = $objp->fk_fournprice;
6158 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
6159 $this->pa_ht = $marginInfos[0];
6160 $this->marge_tx = $marginInfos[1];
6161 $this->marque_tx = $marginInfos[2];
6163 $this->
ref = $objp->product_ref;
6165 $this->product_ref = $objp->product_ref;
6166 $this->product_label = $objp->product_label;
6167 $this->product_desc = $objp->product_desc;
6169 $this->fk_unit = $objp->fk_unit;
6170 $this->fk_user_modif = $objp->fk_user_modif;
6171 $this->fk_user_author = $objp->fk_user_author;
6173 $this->situation_percent = $objp->situation_percent;
6174 $this->fk_prev_id = $objp->fk_prev_id;
6176 $this->multicurrency_subprice = $objp->multicurrency_subprice;
6177 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
6178 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
6179 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
6183 $this->db->free($result);
6187 $this->error = $this->db->lasterror();
6199 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
6201 global $langs, $user, $conf;
6205 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
6207 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
6210 $this->desc = trim($this->desc);
6211 if (empty($this->tva_tx)) {
6214 if (empty($this->localtax1_tx)) {
6215 $this->localtax1_tx = 0;
6217 if (empty($this->localtax2_tx)) {
6218 $this->localtax2_tx = 0;
6220 if (empty($this->localtax1_type)) {
6221 $this->localtax1_type = 0;
6223 if (empty($this->localtax2_type)) {
6224 $this->localtax2_type = 0;
6226 if (empty($this->total_localtax1)) {
6227 $this->total_localtax1 = 0;
6229 if (empty($this->total_localtax2)) {
6230 $this->total_localtax2 = 0;
6232 if (empty($this->rang)) {
6235 if (empty($this->remise_percent)) {
6236 $this->remise_percent = 0;
6238 if (empty($this->info_bits)) {
6239 $this->info_bits = 0;
6241 if (empty($this->subprice)) {
6242 $this->subprice = 0;
6244 if (empty($this->ref_ext)) {
6245 $this->ref_ext =
'';
6247 if (empty($this->special_code)) {
6248 $this->special_code = 0;
6250 if (empty($this->fk_parent_line)) {
6251 $this->fk_parent_line = 0;
6253 if (empty($this->fk_prev_id)) {
6254 $this->fk_prev_id = 0;
6256 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
6257 $this->situation_percent = 100;
6260 if (empty($this->pa_ht)) {
6263 if (empty($this->multicurrency_subprice)) {
6264 $this->multicurrency_subprice = 0;
6266 if (empty($this->multicurrency_total_ht)) {
6267 $this->multicurrency_total_ht = 0;
6269 if (empty($this->multicurrency_total_tva)) {
6270 $this->multicurrency_total_tva = 0;
6272 if (empty($this->multicurrency_total_ttc)) {
6273 $this->multicurrency_total_ttc = 0;
6277 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
6278 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
6281 $this->pa_ht = $result;
6286 if ($this->product_type < 0) {
6287 $this->error =
'ErrorProductTypeMustBe0orMore';
6290 if (!empty($this->fk_product) && $this->fk_product > 0) {
6294 $this->error =
'ErrorProductIdDoesNotExists';
6295 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6303 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facturedet';
6304 $sql .=
' (fk_facture, fk_parent_line, label, description, qty,';
6305 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
6306 $sql .=
' fk_product, product_type, remise_percent, subprice, ref_ext, fk_remise_except,';
6307 $sql .=
' date_start, date_end, fk_code_ventilation, ';
6308 $sql .=
' rang, special_code, fk_product_fournisseur_price, buy_price_ht,';
6309 $sql .=
' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2,';
6310 $sql .=
' situation_percent, fk_prev_id,';
6311 $sql .=
' fk_unit, fk_user_author, fk_user_modif,';
6312 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
6314 $sql .=
" VALUES (".$this->fk_facture.
",";
6315 $sql .=
" ".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null").
",";
6316 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
6317 $sql .=
" '".$this->db->escape($this->desc).
"',";
6318 $sql .=
" ".price2num($this->qty).
",";
6319 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
6320 $sql .=
" ".price2num($this->tva_tx).
",";
6321 $sql .=
" ".price2num($this->localtax1_tx).
",";
6322 $sql .=
" ".price2num($this->localtax2_tx).
",";
6323 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
6324 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
6325 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
6326 $sql .=
" ".((int) $this->product_type).
",";
6327 $sql .=
" ".price2num($this->remise_percent).
",";
6328 $sql .=
" ".price2num($this->subprice).
",";
6329 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
6330 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
6331 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
6332 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
6333 $sql .=
' '.((int) $this->fk_code_ventilation).
',';
6334 $sql .=
' '.((int) $this->rang).
',';
6335 $sql .=
' '.((int) $this->special_code).
',';
6336 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
6337 $sql .=
' '.price2num($this->pa_ht).
',';
6338 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
6339 $sql .=
" ".price2num($this->total_ht).
",";
6340 $sql .=
" ".price2num($this->total_tva).
",";
6341 $sql .=
" ".price2num($this->total_ttc).
",";
6342 $sql .=
" ".price2num($this->total_localtax1).
",";
6343 $sql .=
" ".price2num($this->total_localtax2);
6344 $sql .=
", ".((float) $this->situation_percent);
6345 $sql .=
", ".(!empty($this->fk_prev_id) ? $this->fk_prev_id :
"null");
6346 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
6347 $sql .=
", ".((int) $user->id);
6348 $sql .=
", ".((int) $user->id);
6349 $sql .=
", ".(int) $this->fk_multicurrency;
6350 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
6351 $sql .=
", ".price2num($this->multicurrency_subprice);
6352 $sql .=
", ".price2num($this->multicurrency_total_ht);
6353 $sql .=
", ".price2num($this->multicurrency_total_tva);
6354 $sql .=
", ".price2num($this->multicurrency_total_ttc);
6357 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
6358 $resql = $this->db->query(
$sql);
6360 $this->
id = $this->db->last_insert_id(MAIN_DB_PREFIX.
'facturedet');
6361 $this->
rowid = $this->id;
6372 if ($this->fk_remise_except) {
6374 $result = $discount->fetch($this->fk_remise_except);
6379 if ($discount->fk_facture_line > 0) {
6380 if (empty($noerrorifdiscountalreadylinked)) {
6381 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
6382 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6383 $this->db->rollback();
6387 $result = $discount->link_to_invoice($this->
rowid, 0);
6389 $this->error = $discount->error;
6390 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6391 $this->db->rollback();
6396 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
6397 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6398 $this->db->rollback();
6402 $this->error = $discount->error;
6403 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6404 $this->db->rollback();
6411 $result = $this->
call_trigger(
'LINEBILL_INSERT', $user);
6413 $this->db->rollback();
6419 $this->db->commit();
6422 $this->error = $this->db->lasterror();
6423 $this->db->rollback();
6435 public function update($user =
'', $notrigger = 0)
6437 global $user, $conf;
6441 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
6444 $this->desc = trim($this->desc);
6445 if (empty($this->ref_ext)) {
6446 $this->ref_ext =
'';
6448 if (empty($this->tva_tx)) {
6451 if (empty($this->localtax1_tx)) {
6452 $this->localtax1_tx = 0;
6454 if (empty($this->localtax2_tx)) {
6455 $this->localtax2_tx = 0;
6457 if (empty($this->localtax1_type)) {
6458 $this->localtax1_type = 0;
6460 if (empty($this->localtax2_type)) {
6461 $this->localtax2_type = 0;
6463 if (empty($this->total_localtax1)) {
6464 $this->total_localtax1 = 0;
6466 if (empty($this->total_localtax2)) {
6467 $this->total_localtax2 = 0;
6469 if (empty($this->remise_percent)) {
6470 $this->remise_percent = 0;
6472 if (empty($this->info_bits)) {
6473 $this->info_bits = 0;
6475 if (empty($this->special_code)) {
6476 $this->special_code = 0;
6478 if (empty($this->product_type)) {
6479 $this->product_type = 0;
6481 if (empty($this->fk_parent_line)) {
6482 $this->fk_parent_line = 0;
6484 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
6485 $this->situation_percent = 100;
6487 if (empty($this->pa_ht)) {
6491 if (empty($this->multicurrency_subprice)) {
6492 $this->multicurrency_subprice = 0;
6494 if (empty($this->multicurrency_total_ht)) {
6495 $this->multicurrency_total_ht = 0;
6497 if (empty($this->multicurrency_total_tva)) {
6498 $this->multicurrency_total_tva = 0;
6500 if (empty($this->multicurrency_total_ttc)) {
6501 $this->multicurrency_total_ttc = 0;
6505 if ($this->product_type < 0) {
6510 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
6512 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
6516 $this->pa_ht = $result;
6523 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
6524 $sql .=
" description='".$this->db->escape($this->desc).
"'";
6525 $sql .=
", ref_ext='".$this->db->escape($this->ref_ext).
"'";
6526 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
6527 $sql .=
", subprice=".price2num($this->subprice);
6528 $sql .=
", remise_percent=".price2num($this->remise_percent);
6529 if ($this->fk_remise_except) {
6530 $sql .=
", fk_remise_except=".$this->fk_remise_except;
6532 $sql .=
", fk_remise_except=null";
6534 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->db->escape($this->vat_src_code)).
"'";
6535 $sql .=
", tva_tx=".price2num($this->tva_tx);
6536 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
6537 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
6538 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
6539 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
6540 $sql .=
", qty=".price2num($this->qty);
6541 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
6542 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
6543 $sql .=
", product_type=".$this->product_type;
6544 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
6545 $sql .=
", special_code='".$this->db->escape($this->special_code).
"'";
6546 if (empty($this->skip_update_total)) {
6547 $sql .=
", total_ht=".price2num($this->total_ht);
6548 $sql .=
", total_tva=".price2num($this->total_tva);
6549 $sql .=
", total_ttc=".price2num($this->total_ttc);
6550 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
6551 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
6553 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape($this->fk_fournprice).
"'" :
"null");
6554 $sql .=
", buy_price_ht=".(($this->pa_ht || (string) $this->pa_ht ===
'0') ?
price2num($this->pa_ht) :
"null");
6555 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null");
6556 if (!empty($this->rang)) {
6557 $sql .=
", rang=".((int) $this->rang);
6559 $sql .=
", situation_percent = ".((float) $this->situation_percent);
6560 $sql .=
", fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
6561 $sql .=
", fk_user_modif = ".((int) $user->id);
6564 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
6565 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
6566 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
6567 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
6569 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
6571 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
6572 $resql = $this->db->query(
$sql);
6575 $this->
id = $this->rowid;
6582 if (!$error && !$notrigger) {
6584 $result = $this->
call_trigger(
'LINEBILL_MODIFY', $user);
6586 $this->db->rollback();
6591 $this->db->commit();
6594 $this->error = $this->db->error();
6595 $this->db->rollback();
6607 public function delete($tmpuser =
null, $notrigger =
false)
6614 if (empty($notrigger)) {
6615 $result = $this->
call_trigger(
'LINEBILL_DELETE', $user);
6617 $this->db->rollback();
6626 $this->db->rollback();
6631 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
6632 $sql .=
' SET fk_facture_line = NULL';
6633 $sql .=
' WHERE fk_facture_line = '.((int) $this->
id);
6635 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
6636 $result = $this->db->query(
$sql);
6638 $this->error = $this->db->error();
6639 $this->errors[] = $this->error;
6640 $this->db->rollback();
6644 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'element_time';
6645 $sql .=
' SET invoice_id = NULL, invoice_line_id = NULL';
6646 $sql .=
' WHERE invoice_line_id = '.((int) $this->
id);
6647 if (!$this->db->query(
$sql)) {
6648 $this->error = $this->db->error().
" sql=".
$sql;
6649 $this->errors[] = $this->error;
6650 $this->db->rollback();
6654 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $this->
id);
6656 if ($this->db->query(
$sql)) {
6657 $this->db->commit();
6660 $this->error = $this->db->error().
" sql=".
$sql;
6661 $this->errors[] = $this->error;
6662 $this->db->rollback();
6678 dol_syslog(get_class($this).
"::update_total", LOG_DEBUG);
6681 if (empty($this->total_localtax1)) {
6682 $this->total_localtax1 = 0;
6684 if (empty($this->total_localtax2)) {
6685 $this->total_localtax2 = 0;
6689 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
6690 $sql .=
" total_ht=".price2num($this->total_ht);
6691 $sql .=
",total_tva=".price2num($this->total_tva);
6692 $sql .=
",total_localtax1=".price2num($this->total_localtax1);
6693 $sql .=
",total_localtax2=".price2num($this->total_localtax2);
6694 $sql .=
",total_ttc=".price2num($this->total_ttc);
6695 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
6697 dol_syslog(get_class($this).
"::update_total", LOG_DEBUG);
6699 $resql = $this->db->query(
$sql);
6701 $this->db->commit();
6704 $this->error = $this->db->error();
6705 $this->db->rollback();
6722 global $invoicecache;
6724 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
6728 if (!isset($invoicecache[$invoiceid])) {
6729 $invoicecache[$invoiceid] =
new Facture($this->db);
6730 $invoicecache[$invoiceid]->fetch($invoiceid);
6736 $sql =
"SELECT situation_percent FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $this->fk_prev_id);
6737 $resql = $this->db->query(
$sql);
6738 if ($resql && $this->db->num_rows($resql) > 0) {
6739 $res = $this->db->fetch_array($resql);
6741 $returnPercent = floatval($res[
'situation_percent']);
6743 if ($include_credit_note) {
6744 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
6745 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
6746 $sql .=
" WHERE fd.fk_prev_id = ".((int) $this->fk_prev_id);
6747 $sql .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
6748 $sql .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
6750 $res = $this->db->query(
$sql);
6752 while ($obj = $this->db->fetch_object($res)) {
6753 $returnPercent = $returnPercent + floatval($obj->situation_percent);
6760 return $returnPercent;
6762 $this->error = $this->db->error();
6763 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
6764 $this->db->rollback();