43 include_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;
120 public $fk_user_author;
125 public $fk_user_valid;
130 public $fk_user_modif;
141 public $date_livraison;
146 public $delivery_date;
158 public $ref_customer;
171 public $remise_absolue;
172 public $remise_percent;
175 public $total_localtax1;
176 public $total_localtax2;
178 public $revenuestamp;
206 public $linked_objects = array();
208 public $date_lim_reglement;
209 public $cond_reglement_code;
210 public $mode_reglement_code;
220 public $lines = array();
223 public $extraparams = array();
227 public $date_pointoftax;
233 public $fk_multicurrency;
235 public $multicurrency_code;
236 public $multicurrency_tx;
237 public $multicurrency_total_ht;
238 public $multicurrency_total_tva;
239 public $multicurrency_total_ttc;
244 public $situation_cycle_ref;
249 public $situation_counter;
254 public $situation_final;
259 public $tab_previous_situation_invoice = array();
264 public $tab_next_situation_invoice = array();
271 public $retained_warranty;
276 public $retained_warranty_date_limit;
281 public $retained_warranty_fk_cond_reglement;
312 public $fields = array(
313 'rowid' =>array(
'type'=>
'integer',
'label'=>
'TechnicalID',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>1),
314 'ref' =>array(
'type'=>
'varchar(30)',
'label'=>
'Ref',
'enabled'=>1,
'visible'=>1,
'notnull'=>1,
'showoncombobox'=>1,
'position'=>5),
315 'entity' =>array(
'type'=>
'integer',
'label'=>
'Entity',
'default'=>1,
'enabled'=>1,
'visible'=>-2,
'notnull'=>1,
'position'=>20,
'index'=>1),
316 'ref_client' =>array(
'type'=>
'varchar(255)',
'label'=>
'RefCustomer',
'enabled'=>1,
'visible'=>-1,
'position'=>10),
317 'ref_ext' =>array(
'type'=>
'varchar(255)',
'label'=>
'Ref ext',
'enabled'=>1,
'visible'=>0,
'position'=>12),
319 'type' =>array(
'type'=>
'smallint(6)',
'label'=>
'Type',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>15),
321 'fk_soc' =>array(
'type'=>
'integer:Societe:societe/class/societe.class.php',
'label'=>
'ThirdParty',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>50),
322 'datef' =>array(
'type'=>
'date',
'label'=>
'DateInvoice',
'enabled'=>1,
'visible'=>1,
'position'=>20),
323 'date_valid' =>array(
'type'=>
'date',
'label'=>
'DateValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>22),
324 'date_lim_reglement' =>array(
'type'=>
'date',
'label'=>
'DateDue',
'enabled'=>1,
'visible'=>1,
'position'=>25),
325 'date_closing' =>array(
'type'=>
'datetime',
'label'=>
'Date closing',
'enabled'=>1,
'visible'=>-1,
'position'=>30),
326 'paye' =>array(
'type'=>
'smallint(6)',
'label'=>
'InvoicePaidCompletely',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>80),
328 'remise_percent' =>array(
'type'=>
'double',
'label'=>
'RelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>90),
329 'remise_absolue' =>array(
'type'=>
'double',
'label'=>
'CustomerRelativeDiscount',
'enabled'=>1,
'visible'=>-1,
'position'=>91),
331 'close_code' =>array(
'type'=>
'varchar(16)',
'label'=>
'EarlyClosingReason',
'enabled'=>1,
'visible'=>-1,
'position'=>92),
332 'close_note' =>array(
'type'=>
'varchar(128)',
'label'=>
'EarlyClosingComment',
'enabled'=>1,
'visible'=>-1,
'position'=>93),
333 'total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'AmountHT',
'enabled'=>1,
'visible'=>1,
'position'=>95,
'isameasure'=>1),
334 'total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'AmountVAT',
'enabled'=>1,
'visible'=>-1,
'position'=>100,
'isameasure'=>1),
335 'localtax1' =>array(
'type'=>
'double(24,8)',
'label'=>
'LT1',
'enabled'=>1,
'visible'=>-1,
'position'=>110,
'isameasure'=>1),
336 'localtax2' =>array(
'type'=>
'double(24,8)',
'label'=>
'LT2',
'enabled'=>1,
'visible'=>-1,
'position'=>120,
'isameasure'=>1),
337 'revenuestamp' =>array(
'type'=>
'double(24,8)',
'label'=>
'RevenueStamp',
'enabled'=>1,
'visible'=>-1,
'position'=>115,
'isameasure'=>1),
338 'total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'AmountTTC',
'enabled'=>1,
'visible'=>1,
'position'=>130,
'isameasure'=>1),
339 'fk_user_author' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserAuthor',
'enabled'=>1,
'visible'=>-1,
'position'=>165),
340 'fk_user_modif' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserModif',
'enabled'=>1,
'visible'=>-2,
'notnull'=>-1,
'position'=>166),
341 'fk_user_valid' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserValidation',
'enabled'=>1,
'visible'=>-1,
'position'=>167),
342 'fk_user_closing' =>array(
'type'=>
'integer:User:user/class/user.class.php',
'label'=>
'UserClosing',
'enabled'=>1,
'visible'=>-1,
'position'=>168),
343 'fk_facture_source' =>array(
'type'=>
'integer',
'label'=>
'SourceInvoice',
'enabled'=>1,
'visible'=>-1,
'position'=>170),
344 'fk_projet' =>array(
'type'=>
'integer:Project:projet/class/project.class.php:1:fk_statut=1',
'label'=>
'Project',
'enabled'=>1,
'visible'=>-1,
'position'=>175),
345 'fk_account' =>array(
'type'=>
'integer',
'label'=>
'Fk account',
'enabled'=>1,
'visible'=>-1,
'position'=>180),
346 'fk_currency' =>array(
'type'=>
'varchar(3)',
'label'=>
'CurrencyCode',
'enabled'=>1,
'visible'=>-1,
'position'=>185),
347 'fk_cond_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentTerm',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>190),
348 'fk_mode_reglement' =>array(
'type'=>
'integer',
'label'=>
'PaymentMode',
'enabled'=>1,
'visible'=>-1,
'position'=>195),
349 'note_private' =>array(
'type'=>
'text',
'label'=>
'NotePrivate',
'enabled'=>1,
'visible'=>0,
'position'=>205),
350 'note_public' =>array(
'type'=>
'text',
'label'=>
'NotePublic',
'enabled'=>1,
'visible'=>0,
'position'=>210),
351 'model_pdf' =>array(
'type'=>
'varchar(255)',
'label'=>
'Model pdf',
'enabled'=>1,
'visible'=>0,
'position'=>215),
352 'extraparams' =>array(
'type'=>
'varchar(255)',
'label'=>
'Extraparams',
'enabled'=>1,
'visible'=>-1,
'position'=>225),
353 'situation_cycle_ref' =>array(
'type'=>
'smallint(6)',
'label'=>
'Situation cycle ref',
'enabled'=>
'$conf->global->INVOICE_USE_SITUATION',
'visible'=>-1,
'position'=>230),
354 'situation_counter' =>array(
'type'=>
'smallint(6)',
'label'=>
'Situation counter',
'enabled'=>
'$conf->global->INVOICE_USE_SITUATION',
'visible'=>-1,
'position'=>235),
355 'situation_final' =>array(
'type'=>
'smallint(6)',
'label'=>
'Situation final',
'enabled'=>
'empty($conf->global->INVOICE_USE_SITUATION) ? 0 : 1',
'visible'=>-1,
'position'=>240),
356 'retained_warranty' =>array(
'type'=>
'double',
'label'=>
'Retained warranty',
'enabled'=>
'$conf->global->INVOICE_USE_RETAINED_WARRANTY',
'visible'=>-1,
'position'=>245),
357 'retained_warranty_date_limit' =>array(
'type'=>
'date',
'label'=>
'Retained warranty date limit',
'enabled'=>
'$conf->global->INVOICE_USE_RETAINED_WARRANTY',
'visible'=>-1,
'position'=>250),
358 '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),
359 'fk_incoterms' =>array(
'type'=>
'integer',
'label'=>
'IncotermCode',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>260),
360 'location_incoterms' =>array(
'type'=>
'varchar(255)',
'label'=>
'IncotermLabel',
'enabled'=>
'$conf->incoterm->enabled',
'visible'=>-1,
'position'=>265),
361 'date_pointoftax' =>array(
'type'=>
'date',
'label'=>
'DatePointOfTax',
'enabled'=>
'$conf->global->INVOICE_POINTOFTAX_DATE',
'visible'=>-1,
'position'=>270),
362 'fk_multicurrency' =>array(
'type'=>
'integer',
'label'=>
'MulticurrencyID',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>275),
363 'multicurrency_code' =>array(
'type'=>
'varchar(255)',
'label'=>
'Currency',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>280),
364 'multicurrency_tx' =>array(
'type'=>
'double(24,8)',
'label'=>
'CurrencyRate',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>285,
'isameasure'=>1),
365 'multicurrency_total_ht' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountHT',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>290,
'isameasure'=>1),
366 'multicurrency_total_tva' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountVAT',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>291,
'isameasure'=>1),
367 'multicurrency_total_ttc' =>array(
'type'=>
'double(24,8)',
'label'=>
'MulticurrencyAmountTTC',
'enabled'=>
'$conf->multicurrency->enabled',
'visible'=>-1,
'position'=>292,
'isameasure'=>1),
368 'fk_fac_rec_source' =>array(
'type'=>
'integer',
'label'=>
'RecurringInvoiceSource',
'enabled'=>1,
'visible'=>-1,
'position'=>305),
369 'last_main_doc' =>array(
'type'=>
'varchar(255)',
'label'=>
'LastMainDoc',
'enabled'=>1,
'visible'=>-1,
'position'=>310),
370 'module_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSModule',
'enabled'=>1,
'visible'=>-1,
'position'=>315),
371 'pos_source' =>array(
'type'=>
'varchar(32)',
'label'=>
'POSTerminal',
'enabled'=>1,
'visible'=>-1,
'position'=>320),
372 'datec' =>array(
'type'=>
'datetime',
'label'=>
'DateCreation',
'enabled'=>1,
'visible'=>-1,
'position'=>500),
373 'tms' =>array(
'type'=>
'timestamp',
'label'=>
'DateModificationShort',
'enabled'=>1,
'visible'=>-1,
'notnull'=>1,
'position'=>500),
374 'import_key' =>array(
'type'=>
'varchar(14)',
'label'=>
'ImportId',
'enabled'=>1,
'visible'=>-2,
'position'=>900),
375 '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')),
437 const CLOSECODE_DISCOUNTVAT =
'discount_vat';
438 const CLOSECODE_BADDEBT =
'badcustomer';
439 const CLOSECODE_BANKCHARGE =
'bankcharge';
440 const CLOSECODE_OTHER =
'other';
442 const CLOSECODE_ABANDONED =
'abandon';
443 const CLOSECODE_REPLACED =
'replaced';
466 public function create(
User $user, $notrigger = 0, $forceduedate = 0)
468 global $langs, $conf, $mysoc, $hookmanager;
472 if (empty($this->
type)) {
475 $this->ref_client = trim($this->ref_client);
476 $this->note = (isset($this->note) ? trim($this->note) : trim($this->note_private));
477 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note_private));
478 $this->note_public = trim($this->note_public);
479 if (!$this->cond_reglement_id) {
480 $this->cond_reglement_id = 0;
482 if (!$this->mode_reglement_id) {
483 $this->mode_reglement_id = 0;
485 $this->brouillon = 1;
489 if (!empty($this->multicurrency_code)) {
491 if (empty($this->multicurrency_tx)) {
499 $this->fk_multicurrency = 0;
501 if (empty($this->fk_multicurrency)) {
502 $this->multicurrency_code = $conf->currency;
503 $this->fk_multicurrency = 0;
504 $this->multicurrency_tx = 1;
507 dol_syslog(get_class($this).
"::create user=".$user->id.
" date=".$this->date);
510 if (empty($this->date)) {
511 $this->error =
"Try to create an invoice with an empty parameter (date)";
512 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
516 $result = $soc->fetch($this->socid);
518 $this->error =
"Failed to fetch company: ".$soc->error;
519 dol_syslog(get_class($this).
"::create ".$this->error, LOG_ERR);
527 $originaldatewhen =
null;
528 $nextdatewhen =
null;
529 $previousdaynextdatewhen =
null;
532 if ($this->fac_rec > 0) {
533 $this->fk_fac_rec_source = $this->fac_rec;
535 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture-rec.class.php';
537 $result = $_facrec->fetch($this->fac_rec);
538 $result = $_facrec->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
541 $originaldatewhen = $_facrec->date_when;
542 $nextdatewhen =
null; $previousdaynextdatewhen =
null;
543 if ($originaldatewhen) {
544 $nextdatewhen =
dol_time_plus_duree($originaldatewhen, $_facrec->frequency, $_facrec->unit_frequency);
548 if (!empty($_facrec->frequency)) {
549 $this->socid = $_facrec->socid;
551 $this->entity = $_facrec->entity;
554 $this->fk_project =
GETPOST(
'projectid',
'int') > 0 ? ((int)
GETPOST(
'projectid',
'int')) : $_facrec->fk_project;
555 $this->note_public =
GETPOSTISSET(
'note_public') ?
GETPOST(
'note_public',
'restricthtml') : $_facrec->note_public;
556 $this->note_private =
GETPOSTISSET(
'note_private') ?
GETPOST(
'note_private',
'restricthtml') : $_facrec->note_private;
557 $this->model_pdf =
GETPOSTISSET(
'model') ?
GETPOST(
'model',
'alpha') : $_facrec->model_pdf;
558 $this->cond_reglement_id =
GETPOST(
'cond_reglement_id',
'int') > 0 ? ((int)
GETPOST(
'cond_reglement_id',
'int')) : $_facrec->cond_reglement_id;
559 $this->mode_reglement_id =
GETPOST(
'mode_reglement_id',
'int') > 0 ? ((int)
GETPOST(
'mode_reglement_id',
'int')) : $_facrec->mode_reglement_id;
560 $this->fk_account =
GETPOST(
'fk_account') > 0 ? ((int)
GETPOST(
'fk_account')) : $_facrec->fk_account;
563 $this->total_ht = $_facrec->total_ht;
564 $this->total_ttc = $_facrec->total_ttc;
567 $this->remise_absolue = $_facrec->remise_absolue;
568 $this->remise_percent = $_facrec->remise_percent;
569 $this->fk_incoterms = $_facrec->fk_incoterms;
570 $this->location_incoterms = $_facrec->location_incoterms;
576 $this->ref_client = trim($this->ref_client);
577 $this->note_public = trim($this->note_public);
578 $this->note_private = trim($this->note_private);
579 $this->note_private =
dol_concatdesc($this->note_private, $langs->trans(
"GeneratedFromRecurringInvoice", $_facrec->ref));
581 $this->array_options = $_facrec->array_options;
583 if (!$this->mode_reglement_id) {
584 $this->mode_reglement_id = 0;
586 $this->brouillon = 1;
590 $this->linked_objects = $_facrec->linkedObjectsIds;
597 if ($_facrec->frequency > 0) {
598 dol_syslog(
"This is a recurring invoice so we set date_last_gen and next date_when");
599 if (empty($_facrec->date_when)) {
600 $_facrec->date_when = $now;
602 $next_date = $_facrec->getNextDate();
603 $result = $_facrec->setValueFrom(
'date_last_gen', $now,
'',
null,
'date',
'', $user,
'');
605 $result = $_facrec->setNextDate($next_date, 1);
609 $outputlangs = $langs;
612 if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($this->thirdparty->default_lang)) {
613 $newlang = $this->thirdparty->default_lang;
615 if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($this->default_lang)) {
616 $newlang = $this->default_lang;
618 if (!empty($newlang)) {
620 $outputlangs->setDefaultLang($newlang);
626 $substitutionarray[
'__INVOICE_MONTH__'] =
dol_print_date($this->date,
'%m');
629 $substitutionarray[
'__INVOICE_MONTH_TEXT__'] =
dol_print_date($this->date,
'%B');
632 $substitutionarray[
'__INVOICE_YEAR__'] =
dol_print_date($this->date,
'%Y');
635 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_BEFORE_GEN__'] = (isset($originaldatewhen) ?
dol_print_date($originaldatewhen,
'dayhour') :
'');
636 $substitutionarray[
'__INVOICE_DATE_NEXT_INVOICE_AFTER_GEN__'] = (isset($nextdatewhen) ?
dol_print_date($nextdatewhen,
'dayhour') :
'');
637 $substitutionarray[
'__INVOICE_PREVIOUS_DATE_NEXT_INVOICE_AFTER_GEN__'] = (isset($previousdaynextdatewhen) ?
dol_print_date($previousdaynextdatewhen,
'dayhour') :
'');
638 $substitutionarray[
'__INVOICE_COUNTER_CURRENT__'] = $_facrec->nb_gen_done;
639 $substitutionarray[
'__INVOICE_COUNTER_MAX__'] = $_facrec->nb_gen_max;
650 if (empty($forceduedate)) {
656 $this->date_lim_reglement = $duedate;
658 $this->date_lim_reglement = $forceduedate;
662 $socid = $this->socid;
664 $sql =
"INSERT INTO ".MAIN_DB_PREFIX.
"facture (";
671 $sql .=
", remise_absolue";
672 $sql .=
", remise_percent";
674 $sql .=
", date_pointoftax";
675 $sql .=
", note_private";
676 $sql .=
", note_public";
677 $sql .=
", ref_client, ref_int";
678 $sql .=
", fk_account";
679 $sql .=
", module_source, pos_source, fk_fac_rec_source, fk_facture_source, fk_user_author, fk_projet";
680 $sql .=
", fk_cond_reglement, fk_mode_reglement, date_lim_reglement, model_pdf";
681 $sql .=
", situation_cycle_ref, situation_counter, situation_final";
682 $sql .=
", fk_incoterms, location_incoterms";
683 $sql .=
", fk_multicurrency";
684 $sql .=
", multicurrency_code";
685 $sql .=
", multicurrency_tx";
686 $sql .=
", retained_warranty";
687 $sql .=
", retained_warranty_date_limit";
688 $sql .=
", retained_warranty_fk_cond_reglement";
692 $sql .=
", ".setEntity($this);
693 $sql .=
", ".($this->ref_ext ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null");
694 $sql .=
", '".$this->db->escape($this->
type).
"'";
695 $sql .=
", ".((int) $socid);
696 $sql .=
", '".$this->db->idate($now).
"'";
697 $sql .=
", ".($this->remise_absolue > 0 ? $this->remise_absolue :
'NULL');
698 $sql .=
", ".($this->remise_percent > 0 ? $this->remise_percent :
'NULL');
699 $sql .=
", '".$this->db->idate($this->date).
"'";
700 $sql .=
", ".(empty($this->date_pointoftax) ?
"null" :
"'".$this->db->idate($this->date_pointoftax).
"'");
701 $sql .=
", ".($this->note_private ?
"'".$this->db->escape($this->note_private).
"'" :
"null");
702 $sql .=
", ".($this->note_public ?
"'".$this->db->escape($this->note_public).
"'" :
"null");
703 $sql .=
", ".($this->ref_client ?
"'".$this->db->escape($this->ref_client).
"'" :
"null");
704 $sql .=
", ".($this->ref_int ?
"'".$this->db->escape($this->ref_int).
"'" :
"null");
705 $sql .=
", ".($this->fk_account > 0 ? $this->fk_account :
'NULL');
706 $sql .=
", ".($this->module_source ?
"'".$this->db->escape($this->module_source).
"'" :
"null");
707 $sql .=
", ".($this->pos_source !=
'' ?
"'".$this->db->escape($this->pos_source).
"'" :
"null");
708 $sql .=
", ".($this->fk_fac_rec_source ?
"'".$this->db->escape($this->fk_fac_rec_source).
"'" :
"null");
709 $sql .=
", ".($this->fk_facture_source ?
"'".$this->db->escape($this->fk_facture_source).
"'" :
"null");
710 $sql .=
", ".($user->id > 0 ? (int) $user->id :
"null");
711 $sql .=
", ".($this->fk_project ? $this->fk_project :
"null");
712 $sql .=
", ".((int) $this->cond_reglement_id);
713 $sql .=
", ".((int) $this->mode_reglement_id);
714 $sql .=
", '".$this->db->idate($this->date_lim_reglement).
"'";
715 $sql .=
", ".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null");
716 $sql .=
", ".($this->situation_cycle_ref ?
"'".$this->db->escape($this->situation_cycle_ref).
"'" :
"null");
717 $sql .=
", ".($this->situation_counter ?
"'".$this->db->escape($this->situation_counter).
"'" :
"null");
718 $sql .=
", ".($this->situation_final ? $this->situation_final : 0);
719 $sql .=
", ".(int) $this->fk_incoterms;
720 $sql .=
", '".$this->db->escape($this->location_incoterms).
"'";
721 $sql .=
", ".(int) $this->fk_multicurrency;
722 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
723 $sql .=
", ".(double) $this->multicurrency_tx;
724 $sql .=
", ".(empty($this->retained_warranty) ?
"0" : $this->
db->escape($this->retained_warranty));
725 $sql .=
", ".(!empty($this->retained_warranty_date_limit) ?
"'".$this->db->idate($this->retained_warranty_date_limit).
"'" :
'NULL');
726 $sql .=
", ".(int) $this->retained_warranty_fk_cond_reglement;
731 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'facture');
734 $this->
ref =
'(PROV'.$this->id.
')';
735 $sql =
'UPDATE '.MAIN_DB_PREFIX.
"facture SET ref='".$this->
db->escape($this->
ref).
"' WHERE rowid=".((int) $this->
id);
742 if (!empty($this->linkedObjectsIds) && empty($this->linked_objects)) {
743 $this->linked_objects = $this->linkedObjectsIds;
747 if (!$error && $this->
id && !empty($this->linked_objects) && is_array($this->linked_objects)) {
748 foreach ($this->linked_objects as $origin => $tmp_origin_id) {
749 if (is_array($tmp_origin_id)) {
750 foreach ($tmp_origin_id as $origin_id) {
753 $this->error = $this->
db->lasterror();
759 $origin_id = $tmp_origin_id;
762 $this->error = $this->
db->lasterror();
770 if (!$error && $this->
id && !empty($conf->global->MAIN_PROPAGATE_CONTACTS_FROM_ORIGIN) && !empty($this->origin) && !empty($this->origin_id)) {
771 $originforcontact = $this->origin;
772 $originidforcontact = $this->origin_id;
773 if ($originforcontact ==
'shipping') {
774 require_once DOL_DOCUMENT_ROOT.
'/expedition/class/expedition.class.php';
776 $exp->fetch($this->origin_id);
777 $exp->fetchObjectLinked(
null,
'',
null,
'',
'OR', 1,
'sourcetype', 0);
778 if (count($exp->linkedObjectsIds[
'commande']) > 0) {
779 foreach ($exp->linkedObjectsIds[
'commande'] as $key => $value) {
780 $originforcontact =
'commande';
781 if (is_object($value)) {
782 $originidforcontact = $value->id;
784 $originidforcontact = $value;
791 $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";
792 $sqlcontact .=
" WHERE element_id = ".((int) $originidforcontact).
" AND ec.fk_c_type_contact = ctc.rowid AND ctc.element = '".$this->
db->escape($originforcontact).
"'";
794 $resqlcontact = $this->
db->query($sqlcontact);
796 while ($objcontact = $this->
db->fetch_object($resqlcontact)) {
798 $this->
add_contact($objcontact->fk_socpeople, $objcontact->code, $objcontact->source);
808 if (!$error && empty($this->fac_rec) && count($this->lines) && is_object($this->lines[0])) {
811 dol_syslog(
"There is ".count($this->lines).
" lines that are invoice lines objects");
812 foreach ($this->lines as $i => $val) {
813 $newinvoiceline = $this->lines[$i];
815 $newinvoiceline->context = $this->context;
817 $newinvoiceline->fk_facture = $this->id;
819 $newinvoiceline->origin = $this->lines[$i]->element;
820 $newinvoiceline->origin_id = $this->lines[$i]->id;
823 if ($this->lines[$i]->date_start_fill == 1 && $originaldatewhen) {
824 $newinvoiceline->date_start = $originaldatewhen;
826 if ($this->lines[$i]->date_end_fill == 1 && $previousdaynextdatewhen) {
827 $newinvoiceline->date_end = $previousdaynextdatewhen;
832 if (($newinvoiceline->product_type != 9 && empty($newinvoiceline->fk_parent_line)) || $newinvoiceline->product_type == 9) {
837 $vatrate = $newinvoiceline->tva_tx;
838 if ($newinvoiceline->vat_src_code && ! preg_match(
'/\(.*\)/', $vatrate)) $vatrate.=
' ('.$newinvoiceline->vat_src_code.
')';
840 $newinvoiceline->fk_parent_line = $fk_parent_line;
844 $discount->fetch($newinvoiceline->fk_remise_except);
846 $discountId = $soc->set_remise_except($discount->amount_ht, $user, $discount->description, $discount->tva_tx);
847 $newinvoiceline->fk_remise_except = $discountId;
851 $newinvoiceline->desc,
852 $newinvoiceline->subprice,
853 $newinvoiceline->qty,
855 $newinvoiceline->localtax1_tx,
856 $newinvoiceline->localtax2_tx,
857 $newinvoiceline->fk_product,
858 $newinvoiceline->remise_percent,
859 $newinvoiceline->date_start,
860 $newinvoiceline->date_end,
861 $newinvoiceline->fk_code_ventilation,
862 $newinvoiceline->info_bits,
863 $newinvoiceline->fk_remise_except,
866 $newinvoiceline->product_type,
867 $newinvoiceline->rang,
868 $newinvoiceline->special_code,
869 $newinvoiceline->element,
872 $newinvoiceline->fk_fournprice,
873 $newinvoiceline->pa_ht,
874 $newinvoiceline->label,
875 $newinvoiceline->array_options,
876 $newinvoiceline->situation_percent,
877 $newinvoiceline->fk_prev_id,
878 $newinvoiceline->fk_unit,
879 $newinvoiceline->multicurrency_subprice,
880 $newinvoiceline->ref_ext,
885 if ($result > 0 && $newinvoiceline->product_type == 9) {
886 $fk_parent_line = $result;
890 $this->error = $newinvoiceline->error;
891 $this->errors = $newinvoiceline->errors;
896 } elseif (!$error && empty($this->fac_rec)) {
899 dol_syslog(
"There is ".count($this->lines).
" lines that are array lines");
901 foreach ($this->lines as $i => $val) {
902 $line = $this->lines[$i];
906 if (!is_object($line)) {
907 $line = (object) $line;
912 if (($line->product_type != 9 && empty($line->fk_parent_line)) || $line->product_type == 9) {
917 $vatrate = $line->tva_tx;
918 if ($line->vat_src_code && !preg_match(
'/\(.*\)/', $vatrate)) {
919 $vatrate .=
' ('.$line->vat_src_code.
')';
922 if (!empty($conf->global->MAIN_CREATEFROM_KEEP_LINE_ORIGIN_INFORMATION)) {
923 $originid = $line->origin_id;
924 $origintype = $line->origin;
926 $originid = $line->id;
927 $origintype = $this->element;
931 if (empty($line->ref_ext)) {
943 $line->remise_percent,
946 $line->fk_code_ventilation,
948 $line->fk_remise_except,
957 $line->fk_fournprice,
960 $line->array_options,
961 $line->situation_percent,
964 $line->multicurrency_subprice,
969 $this->error = $this->
db->lasterror();
971 $this->
db->rollback();
976 if ($result > 0 && $line->product_type == 9) {
977 $fk_parent_line = $result;
986 if (!$error && $this->fac_rec > 0) {
987 foreach ($_facrec->lines as $i => $val) {
988 if ($_facrec->lines[$i]->fk_product) {
990 $res = $prod->fetch($_facrec->lines[$i]->fk_product);
1001 $tva_tx = $_facrec->lines[$i]->tva_tx.($_facrec->lines[$i]->vat_src_code ?
'('.$_facrec->lines[$i]->vat_src_code.
')' :
'');
1002 $tva_npr = $_facrec->lines[$i]->info_bits;
1003 if (empty($tva_tx)) {
1006 $localtax1_tx = $_facrec->lines[$i]->localtax1_tx;
1007 $localtax2_tx = $_facrec->lines[$i]->localtax2_tx;
1009 $fk_product_fournisseur_price = empty($_facrec->lines[$i]->fk_product_fournisseur_price) ? null : $_facrec->lines[$i]->fk_product_fournisseur_price;
1010 $buyprice = empty($_facrec->lines[$i]->buyprice) ? 0 : $_facrec->lines[$i]->buyprice;
1013 if (!$buyprice && $_facrec->lines[$i]->fk_product > 0) {
1014 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.product.class.php';
1016 $producttmp->fetch($_facrec->lines[$i]->fk_product);
1021 if ($conf->global->MARGIN_TYPE ==
'costprice' && !empty($producttmp->cost_price)) {
1022 $buyprice = $producttmp->cost_price;
1023 } elseif (!empty($conf->stock->enabled) && ($conf->global->MARGIN_TYPE ==
'costprice' || $conf->global->MARGIN_TYPE ==
'pmp') && !empty($producttmp->pmp)) {
1024 $buyprice = $producttmp->pmp;
1026 if ($producttmp->find_min_price_product_fournisseur($_facrec->lines[$i]->fk_product) > 0) {
1027 if ($producttmp->product_fourn_price_id > 0) {
1028 $buyprice =
price2num($producttmp->fourn_unitprice * (1 - $producttmp->fourn_remise_percent / 100) + $producttmp->fourn_remise,
'MU');
1034 $result_insert = $this->
addline(
1035 $_facrec->lines[$i]->desc,
1036 $_facrec->lines[$i]->subprice,
1037 $_facrec->lines[$i]->qty,
1041 $_facrec->lines[$i]->fk_product,
1042 $_facrec->lines[$i]->remise_percent,
1043 ($_facrec->lines[$i]->date_start_fill == 1 && $originaldatewhen) ? $originaldatewhen :
'',
1044 ($_facrec->lines[$i]->date_end_fill == 1 && $previousdaynextdatewhen) ? $previousdaynextdatewhen :
'',
1050 $_facrec->lines[$i]->product_type,
1051 $_facrec->lines[$i]->rang,
1052 $_facrec->lines[$i]->special_code,
1056 $fk_product_fournisseur_price,
1058 $_facrec->lines[$i]->label,
1059 empty($_facrec->lines[$i]->array_options) ?
null:$_facrec->lines[$i]->array_options,
1060 $_facrec->lines[$i]->situation_percent,
1062 $_facrec->lines[$i]->fk_unit,
1063 $_facrec->lines[$i]->multicurrency_subprice,
1064 $_facrec->lines[$i]->ref_ext,
1068 if ($result_insert < 0) {
1070 $this->error = $this->
db->error();
1089 if (!$error && !$notrigger) {
1099 $this->
db->commit();
1102 $this->
db->rollback();
1106 $this->error = $langs->trans(
'FailedToUpdatePrice');
1107 $this->
db->rollback();
1111 dol_syslog(get_class($this).
"::create error ".$this->error, LOG_ERR);
1112 $this->
db->rollback();
1116 $this->error = $this->
db->error();
1117 $this->
db->rollback();
1141 if (!empty($this->array_options)) {
1142 $facture->array_options = $this->array_options;
1145 foreach ($this->lines as &$line) {
1146 $line->fetch_optionals();
1150 $facture->type = $this->type;
1151 $facture->socid = $this->socid;
1152 $facture->date = $this->date;
1153 $facture->date_pointoftax = $this->date_pointoftax;
1154 $facture->note_public = $this->note_public;
1155 $facture->note_private = $this->note_private;
1156 $facture->ref_client = $this->ref_client;
1157 $facture->modelpdf = $this->model_pdf;
1158 $facture->model_pdf = $this->model_pdf;
1159 $facture->fk_project = $this->fk_project;
1160 $facture->cond_reglement_id = $this->cond_reglement_id;
1161 $facture->mode_reglement_id = $this->mode_reglement_id;
1162 $facture->remise_absolue = $this->remise_absolue;
1163 $facture->remise_percent = $this->remise_percent;
1165 $facture->origin = $this->origin;
1166 $facture->origin_id = $this->origin_id;
1167 $facture->fk_account = $this->fk_account;
1169 $facture->lines = $this->lines;
1170 $facture->situation_counter = $this->situation_counter;
1171 $facture->situation_cycle_ref = $this->situation_cycle_ref;
1172 $facture->situation_final = $this->situation_final;
1174 $facture->retained_warranty = $this->retained_warranty;
1175 $facture->retained_warranty_fk_cond_reglement = $this->retained_warranty_fk_cond_reglement;
1176 $facture->retained_warranty_date_limit = $this->retained_warranty_date_limit;
1178 $facture->fk_user_author = $user->id;
1182 foreach ($facture->lines as $i => $tmpline) {
1183 $facture->lines[$i]->fk_prev_id = $this->lines[$i]->rowid;
1184 if ($invertdetail) {
1185 $facture->lines[$i]->subprice = -$facture->lines[$i]->subprice;
1186 $facture->lines[$i]->total_ht = -$facture->lines[$i]->total_ht;
1187 $facture->lines[$i]->total_tva = -$facture->lines[$i]->total_tva;
1188 $facture->lines[$i]->total_localtax1 = -$facture->lines[$i]->total_localtax1;
1189 $facture->lines[$i]->total_localtax2 = -$facture->lines[$i]->total_localtax2;
1190 $facture->lines[$i]->total_ttc = -$facture->lines[$i]->total_ttc;
1191 $facture->lines[$i]->ref_ext =
'';
1195 dol_syslog(get_class($this).
"::createFromCurrent invertdetail=".$invertdetail.
" socid=".$this->socid.
" nboflines=".count($facture->lines));
1197 $facid = $facture->create($user);
1199 $this->error = $facture->error;
1200 $this->errors = $facture->errors;
1201 } elseif ($this->
type == self::TYPE_SITUATION && !empty($conf->global->INVOICE_USE_SITUATION)) {
1204 foreach ($this->linkedObjectsIds as $typeObject => $Tfk_object) {
1205 foreach ($Tfk_object as $fk_object) {
1206 $facture->add_object_linked($typeObject, $fk_object);
1210 $facture->add_object_linked(
'facture', $this->fk_facture_source);
1226 global $conf, $hookmanager;
1234 $object->fetch($fromid);
1237 $objFrom = clone $object;
1240 if (!empty($this->socid) && $this->socid != $object->socid) {
1243 if ($objsoc->fetch($this->socid) > 0) {
1244 $object->socid = $objsoc->id;
1245 $object->cond_reglement_id = (!empty($objsoc->cond_reglement_id) ? $objsoc->cond_reglement_id : 0);
1246 $object->mode_reglement_id = (!empty($objsoc->mode_reglement_id) ? $objsoc->mode_reglement_id : 0);
1247 $object->fk_project =
'';
1248 $object->fk_delivery_address =
'';
1259 $object->date = (empty($this->date) ?
dol_now() : $this->date);
1260 $object->user_author = $user->id;
1261 $object->user_valid =
null;
1262 $object->fk_user_author = $user->id;
1263 $object->fk_user_valid =
null;
1264 $object->fk_facture_source = 0;
1265 $object->date_creation =
'';
1266 $object->date_modification =
'';
1267 $object->date_validation =
'';
1268 $object->ref_client =
'';
1269 $object->close_code =
'';
1270 $object->close_note =
'';
1271 if ($conf->global->MAIN_DONT_KEEP_NOTE_ON_CLONING == 1) {
1272 $object->note_private =
'';
1273 $object->note_public =
'';
1277 foreach ($object->lines as $i => $line) {
1278 if (($object->lines[$i]->info_bits & 0x02) == 0x02) {
1279 unset($object->lines[$i]);
1285 if (!empty($conf->global->INVOICE_AUTO_NEXT_MONTH_ON_LINES) && !empty($line->date_start) && !empty($line->date_end)) {
1297 if (
dol_mktime(0, 0, 0, $start[
'mon'], $start[
'mday'], $start[
'year'],
'gmt') == $first
1298 &&
dol_mktime(23, 59, 59, $end[
'mon'], $end[
'mday'], $end[
'year'],
'gmt') == $last) {
1302 $object->lines[$i]->date_start = $newFirst;
1303 $object->lines[$i]->date_end = $newLast;
1307 $object->lines[$i]->ref_ext =
'';
1311 $object->context[
'createfromclone'] =
'createfromclone';
1312 $result = $object->create($user);
1315 $this->error = $object->error;
1316 $this->errors = $object->errors;
1319 if ($object->copy_linked_contact($objFrom,
'internal') < 0) {
1321 $this->error = $object->error;
1322 $this->errors = $object->errors;
1323 } elseif ($object->socid == $objFrom->socid) {
1325 if ($object->copy_linked_contact($objFrom,
'external') < 0) {
1327 $this->error = $object->error;
1328 $this->errors = $object->errors;
1335 if (is_object($hookmanager)) {
1336 $parameters = array(
'objFrom'=>$objFrom);
1338 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $object, $action);
1340 $this->errors += $hookmanager->errors;
1341 $this->error = $hookmanager->error;
1347 unset($object->context[
'createfromclone']);
1351 $this->
db->commit();
1354 $this->
db->rollback();
1368 global $conf, $hookmanager;
1376 $num = count($object->lines);
1377 for ($i = 0; $i < $num; $i++) {
1380 $line->libelle = $object->lines[$i]->libelle;
1381 $line->label = $object->lines[$i]->label;
1382 $line->desc = $object->lines[$i]->desc;
1383 $line->subprice = $object->lines[$i]->subprice;
1384 $line->total_ht = $object->lines[$i]->total_ht;
1385 $line->total_tva = $object->lines[$i]->total_tva;
1386 $line->total_localtax1 = $object->lines[$i]->total_localtax1;
1387 $line->total_localtax2 = $object->lines[$i]->total_localtax2;
1388 $line->total_ttc = $object->lines[$i]->total_ttc;
1389 $line->vat_src_code = $object->lines[$i]->vat_src_code;
1390 $line->tva_tx = $object->lines[$i]->tva_tx;
1391 $line->localtax1_tx = $object->lines[$i]->localtax1_tx;
1392 $line->localtax2_tx = $object->lines[$i]->localtax2_tx;
1393 $line->qty = $object->lines[$i]->qty;
1394 $line->fk_remise_except = $object->lines[$i]->fk_remise_except;
1395 $line->remise_percent = $object->lines[$i]->remise_percent;
1396 $line->fk_product = $object->lines[$i]->fk_product;
1397 $line->info_bits = $object->lines[$i]->info_bits;
1398 $line->product_type = $object->lines[$i]->product_type;
1399 $line->rang = $object->lines[$i]->rang;
1400 $line->special_code = $object->lines[$i]->special_code;
1401 $line->fk_parent_line = $object->lines[$i]->fk_parent_line;
1402 $line->fk_unit = $object->lines[$i]->fk_unit;
1403 $line->date_start = $object->lines[$i]->date_start;
1404 $line->date_end = $object->lines[$i]->date_end;
1407 $line->fk_multicurrency = $object->lines[$i]->fk_multicurrency;
1408 $line->multicurrency_code = $object->lines[$i]->multicurrency_code;
1409 $line->multicurrency_subprice = $object->lines[$i]->multicurrency_subprice;
1410 $line->multicurrency_total_ht = $object->lines[$i]->multicurrency_total_ht;
1411 $line->multicurrency_total_tva = $object->lines[$i]->multicurrency_total_tva;
1412 $line->multicurrency_total_ttc = $object->lines[$i]->multicurrency_total_ttc;
1414 $line->fk_fournprice = $object->lines[$i]->fk_fournprice;
1415 $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);
1416 $line->pa_ht = $marginInfos[0];
1419 $object->lines[$i]->fetch_optionals();
1420 foreach ($object->lines[$i]->array_options as $options_key => $value) {
1421 $line->array_options[$options_key] = $value;
1424 $this->lines[$i] = $line;
1427 $this->socid = $object->socid;
1428 $this->fk_project = $object->fk_project;
1429 $this->fk_account = $object->fk_account;
1430 $this->cond_reglement_id = $object->cond_reglement_id;
1431 $this->mode_reglement_id = $object->mode_reglement_id;
1432 $this->availability_id = $object->availability_id;
1433 $this->demand_reason_id = $object->demand_reason_id;
1434 $this->delivery_date = (empty($object->delivery_date) ? $object->date_livraison : $object->delivery_date);
1435 $this->date_livraison = $object->delivery_date;
1436 $this->fk_delivery_address = $object->fk_delivery_address;
1437 $this->contact_id = $object->contact_id;
1438 $this->ref_client = $object->ref_client;
1440 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1441 $this->note_private = $object->note_private;
1442 $this->note_public = $object->note_public;
1445 $this->module_source = $object->module_source;
1446 $this->pos_source = $object->pos_source;
1448 $this->origin = $object->element;
1449 $this->origin_id = $object->id;
1451 $this->fk_user_author = $user->id;
1454 $object->fetch_optionals();
1455 foreach ($object->array_options as $options_key => $value) {
1456 $this->array_options[$options_key] = $value;
1460 $this->linked_objects[$this->origin] = $this->origin_id;
1461 if (!empty($object->other_linked_objects) && is_array($object->other_linked_objects)) {
1462 $this->linked_objects = array_merge($this->linked_objects, $object->other_linked_objects);
1465 $ret = $this->
create($user);
1469 $hookmanager->initHooks(array(
'invoicedao'));
1471 $parameters = array(
'objFrom'=>$object);
1473 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $this, $action);
1475 $this->errors += $hookmanager->errors;
1476 $this->error = $hookmanager->error;
1504 global $conf, $langs, $hookmanager, $action;
1506 if (! in_array($origin->element, array(
'propal',
'commande'))) {
1507 $origin->error =
'ErrorCanOnlyAutomaticallyGenerateADepositFromProposalOrOrder';
1512 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'DateInvoice'));
1516 require_once DOL_DOCUMENT_ROOT .
'/core/lib/date.lib.php';
1518 if ($date > (
dol_get_last_hour(
dol_now(
'tzuserrel')) + (empty($conf->global->INVOICE_MAX_FUTURE_DELAY) ? 0 : $conf->global->INVOICE_MAX_FUTURE_DELAY))) {
1519 $origin->error =
'ErrorDateIsInFuture';
1523 if ($payment_terms_id <= 0) {
1524 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'PaymentConditionsShort'));
1528 $payment_conditions_deposit_percent =
getDictionaryValue(
'c_payment_term',
'deposit_percent', $origin->cond_reglement_id);
1530 if (empty($payment_conditions_deposit_percent)) {
1531 $origin->error =
'ErrorPaymentConditionsNotEligibleToDepositCreation';
1535 if (empty($origin->deposit_percent)) {
1536 $origin->error = $langs->trans(
'ErrorFieldRequired', $langs->transnoentities(
'DepositPercent'));
1540 $deposit =
new self($origin->db);
1541 $deposit->socid = $origin->socid;
1543 $deposit->fk_project = $origin->fk_project;
1544 $deposit->ref_client = $origin->ref_client;
1545 $deposit->date = $date;
1546 $deposit->mode_reglement_id = $origin->mode_reglement_id;
1547 $deposit->cond_reglement_id = $payment_terms_id;
1548 $deposit->availability_id = $origin->availability_id;
1549 $deposit->demand_reason_id = $origin->demand_reason_id;
1550 $deposit->fk_account = $origin->fk_account;
1551 $deposit->fk_incoterms = $origin->fk_incoterms;
1552 $deposit->location_incoterms = $origin->location_incoterms;
1553 $deposit->fk_multicurrency = $origin->fk_multicurrency;
1554 $deposit->multicurrency_code = $origin->multicurrency_code;
1555 $deposit->multicurrency_tx = $origin->multicurrency_tx;
1556 $deposit->module_source = $origin->module_source;
1557 $deposit->pos_source = $origin->pos_source;
1558 $deposit->model_pdf =
'crabe';
1560 $modelByTypeConfName =
'FACTURE_ADDON_PDF_' . $deposit->type;
1562 if (!empty($conf->global->$modelByTypeConfName)) {
1563 $deposit->model_pdf = $conf->global->$modelByTypeConfName;
1564 } elseif (!empty($conf->global->FACTURE_ADDON_PDF)) {
1565 $deposit->model_pdf = $conf->global->FACTURE_ADDON_PDF;
1568 if (empty($conf->global->MAIN_DISABLE_PROPAGATE_NOTES_FROM_ORIGIN)) {
1569 $deposit->note_private = $origin->note_private;
1570 $deposit->note_public = $origin->note_public;
1573 $deposit->origin = $origin->element;
1574 $deposit->origin_id = $origin->id;
1576 $origin->fetch_optionals();
1578 foreach ($origin->array_options as $extrakey => $value) {
1579 $deposit->array_options[$extrakey] = $value;
1582 $deposit->linked_objects[$deposit->origin] = $deposit->origin_id;
1584 foreach ($overrideFields as $key => $value) {
1585 $deposit->$key = $value;
1588 $deposit->context[
'createdepositfromorigin'] =
'createdepositfromorigin';
1590 $origin->db->begin();
1593 $createReturn = $deposit->create($user, $notrigger);
1595 if ($createReturn <= 0) {
1596 $origin->db->rollback();
1597 $origin->error = $deposit->error;
1598 $origin->errors = $deposit->errors;
1602 $amount_ttc_diff = 0;
1603 $amountdeposit = array();
1604 $descriptions = array();
1606 if (! empty($conf->global->MAIN_DEPOSIT_MULTI_TVA)) {
1607 $amount = $origin->total_ttc * ($origin->deposit_percent / 100);
1609 $TTotalByTva = array();
1610 foreach ($origin->lines as &$line) {
1611 if (!empty($line->special_code)) {
1614 $TTotalByTva[$line->tva_tx] += $line->total_ttc;
1615 $descriptions[$line->tva_tx] .=
'<li>' . (! empty($line->product_ref) ? $line->product_ref .
' - ' :
'');
1616 $descriptions[$line->tva_tx] .= (! empty($line->product_label) ? $line->product_label .
' - ' :
'');
1617 $descriptions[$line->tva_tx] .= $langs->trans(
'Qty') .
' : ' . $line->qty;
1618 $descriptions[$line->tva_tx] .=
' - ' . $langs->trans(
'TotalHT') .
' : ' .
price($line->total_ht) .
'</li>';
1621 foreach ($TTotalByTva as $tva => &$total) {
1622 $coef = $total / $origin->total_ttc;
1623 $am = $amount * $coef;
1624 $amount_ttc_diff += $am;
1625 $amountdeposit[$tva] += $am / (1 + $tva / 100);
1629 $lines = $origin->lines;
1630 $numlines = count($lines);
1631 for ($i = 0; $i < $numlines; $i++) {
1632 if (empty($lines[$i]->qty)) {
1635 if (!empty($lines[$i]->special_code)) {
1639 $totalamount += $lines[$i]->total_ht;
1640 $tva_tx = $lines[$i]->tva_tx;
1641 $amountdeposit[$tva_tx] += ($lines[$i]->total_ht * $origin->deposit_percent) / 100;
1642 $descriptions[$tva_tx] .=
'<li>' . (! empty($lines[$i]->product_ref) ? $lines[$i]->product_ref .
' - ' :
'');
1643 $descriptions[$tva_tx] .= (! empty($lines[$i]->product_label) ? $lines[$i]->product_label .
' - ' :
'');
1644 $descriptions[$tva_tx] .= $langs->trans(
'Qty') .
' : ' . $lines[$i]->qty;
1645 $descriptions[$tva_tx] .=
' - ' . $langs->trans(
'TotalHT') .
' : ' .
price($lines[$i]->total_ht) .
'</li>';
1648 if ($totalamount == 0) {
1649 $amountdeposit[0] = 0;
1652 $amount_ttc_diff = $amountdeposit[0];
1655 foreach ($amountdeposit as $tva => $amount) {
1656 if (empty($amount)) {
1660 $descline =
'(DEPOSIT) ('. $origin->deposit_percent .
'%) - '.$origin->ref;
1663 if (! empty($conf->global->INVOICE_DEPOSIT_VARIABLE_MODE_DETAIL_LINES_IN_DESCRIPTION) && ! empty($descriptions[$tva])) {
1664 $descline .=
'<ul>' . $descriptions[$tva] .
'</ul>';
1667 $addlineResult = $deposit->addline(
1674 (empty($conf->global->INVOICE_PRODUCTID_DEPOSIT) ? 0 : $conf->global->INVOICE_PRODUCTID_DEPOSIT),
1694 if ($addlineResult < 0) {
1695 $origin->db->rollback();
1696 $origin->error = $deposit->error;
1697 $origin->errors = $deposit->errors;
1702 $diff = $deposit->total_ttc - $amount_ttc_diff;
1704 if (!empty($conf->global->MAIN_DEPOSIT_MULTI_TVA) && $diff != 0) {
1705 $deposit->fetch_lines();
1706 $subprice_diff = $deposit->lines[0]->subprice - $diff / (1 + $deposit->lines[0]->tva_tx / 100);
1708 $updatelineResult = $deposit->updateline(
1709 $deposit->lines[0]->id,
1710 $deposit->lines[0]->desc,
1712 $deposit->lines[0]->qty,
1713 $deposit->lines[0]->remise_percent,
1714 $deposit->lines[0]->date_start,
1715 $deposit->lines[0]->date_end,
1716 $deposit->lines[0]->tva_tx,
1720 $deposit->lines[0]->info_bits,
1721 $deposit->lines[0]->product_type,
1725 $deposit->lines[0]->pa_ht,
1726 $deposit->lines[0]->label,
1732 if ($updatelineResult < 0) {
1733 $origin->db->rollback();
1734 $origin->error = $deposit->error;
1735 $origin->errors = $deposit->errors;
1741 if (! is_object($hookmanager)) {
1742 require_once DOL_DOCUMENT_ROOT .
'/core/class/hookmanager.class.php';
1746 $hookmanager->initHooks(array(
'invoicedao'));
1748 $parameters = array(
'objFrom' => $origin);
1749 $reshook = $hookmanager->executeHooks(
'createFrom', $parameters, $deposit, $action);
1752 $origin->db->rollback();
1753 $origin->error = $hookmanager->error;
1754 $origin->errors = $hookmanager->errors;
1758 if (! empty($autoValidateDeposit)) {
1759 $validateReturn = $deposit->validate($user,
'', 0, $notrigger);
1761 if ($validateReturn < 0) {
1762 $origin->db->rollback();
1763 $origin->error = $deposit->error;
1764 $origin->errors = $deposit->errors;
1769 unset($deposit->context[
'createdepositfromorigin']);
1771 $origin->db->commit();
1790 public function getNomUrl($withpicto = 0, $option =
'', $max = 0, $short = 0, $moretitle =
'', $notooltip = 0, $addlinktonotes = 0, $save_lastsearch_value = -1, $target =
'')
1792 global $langs, $conf, $user, $mysoc;
1794 if (!empty($conf->dol_no_mouse_hover)) {
1800 if ($option ==
'withdraw') {
1801 $url = DOL_URL_ROOT.
'/compta/facture/prelevement.php?facid='.$this->id;
1803 $url = DOL_URL_ROOT.
'/compta/facture/card.php?facid='.$this->id;
1806 if (!$user->rights->facture->lire) {
1810 if ($option !==
'nolink') {
1812 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
1813 if ($save_lastsearch_value == -1 && preg_match(
'/list\.php/', $_SERVER[
"PHP_SELF"])) {
1814 $add_save_lastsearch_values = 1;
1816 if ($add_save_lastsearch_values) {
1817 $url .=
'&save_lastsearch_values=1';
1825 $picto = $this->picto;
1826 if ($this->
type == self::TYPE_REPLACEMENT) {
1829 if ($this->
type == self::TYPE_CREDIT_NOTE) {
1832 if ($this->
type == self::TYPE_DEPOSIT) {
1837 if ($user->rights->facture->lire) {
1838 $label =
img_picto(
'', $picto).
' <u class="paddingrightonly">'.$langs->trans(
"Invoice").
'</u>';
1839 if ($this->
type == self::TYPE_REPLACEMENT) {
1840 $label =
img_picto(
'', $picto).
' <u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"ReplacementInvoice").
'</u>';
1842 if ($this->
type == self::TYPE_CREDIT_NOTE) {
1843 $label =
img_picto(
'', $picto).
' <u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"CreditNote").
'</u>';
1845 if ($this->
type == self::TYPE_DEPOSIT) {
1846 $label =
img_picto(
'', $picto).
' <u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"Deposit").
'</u>';
1848 if ($this->
type == self::TYPE_SITUATION) {
1849 $label =
img_picto(
'', $picto).
' <u class="paddingrightonly">'.$langs->transnoentitiesnoconv(
"InvoiceSituation").
'</u>';
1851 if (isset($this->statut) && isset($this->alreadypaid)) {
1852 $label .=
' '.$this->getLibStatut(5, $this->alreadypaid);
1854 if (!empty($this->
ref)) {
1855 $label .=
'<br><b>'.$langs->trans(
'Ref').
':</b> '.$this->ref;
1857 if (!empty($this->ref_client)) {
1858 $label .=
'<br><b>'.$langs->trans(
'RefCustomer').
':</b> '.$this->ref_client;
1860 if (!empty($this->date)) {
1861 $label .=
'<br><b>'.$langs->trans(
'Date').
':</b> '.
dol_print_date($this->date,
'day');
1863 if (!empty($this->total_ht)) {
1864 $label .=
'<br><b>'.$langs->trans(
'AmountHT').
':</b> '.
price($this->total_ht, 0, $langs, 0, -1, -1, $conf->currency);
1866 if (!empty($this->total_tva)) {
1867 $label .=
'<br><b>'.$langs->trans(
'AmountVAT').
':</b> '.
price($this->total_tva, 0, $langs, 0, -1, -1, $conf->currency);
1869 if (!empty($this->total_localtax1) && $this->total_localtax1 != 0) {
1870 $label .=
'<br><b>'.$langs->transcountry(
'AmountLT1', $mysoc->country_code).
':</b> '.
price($this->total_localtax1, 0, $langs, 0, -1, -1, $conf->currency);
1872 if (!empty($this->total_localtax2) && $this->total_localtax2 != 0) {
1873 $label .=
'<br><b>'.$langs->transcountry(
'AmountLT2', $mysoc->country_code).
':</b> '.
price($this->total_localtax2, 0, $langs, 0, -1, -1, $conf->currency);
1875 if (!empty($this->total_ttc)) {
1876 $label .=
'<br><b>'.$langs->trans(
'AmountTTC').
':</b> '.
price($this->total_ttc, 0, $langs, 0, -1, -1, $conf->currency);
1879 $label .=
' - '.$moretitle;
1883 $linkclose = ($target ?
' target="'.$target.
'"' :
'');
1884 if (empty($notooltip) && $user->rights->facture->lire) {
1885 if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
1886 $label = $langs->trans(
"Invoice");
1887 $linkclose .=
' alt="'.dol_escape_htmltag($label, 1).
'"';
1889 $linkclose .=
' title="'.dol_escape_htmltag($label, 1).
'"';
1890 $linkclose .=
' class="classfortooltip"';
1893 $linkstart =
'<a href="'.$url.
'"';
1894 $linkstart .= $linkclose.
'>';
1897 if ($option ==
'nolink') {
1902 $result .= $linkstart;
1904 $result .=
img_object(($notooltip ?
'' : $label), $picto, ($notooltip ? (($withpicto != 2) ?
'class="paddingright"' :
'') :
'class="'.(($withpicto != 2) ?
'paddingright ' :
'').
'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
1906 if ($withpicto != 2) {
1907 $result .= ($max ?
dol_trunc($this->
ref, $max) : $this->ref);
1909 $result .= $linkend;
1911 if ($addlinktonotes) {
1912 $txttoshow = ($user->socid > 0 ? $this->note_public : $this->note_private);
1915 $notetoshow = $langs->trans(
"ViewPrivateNote").
':<br>'.$txttoshow;
1916 $result .=
' <span class="note inline-block">';
1917 $result .=
'<a href="'.DOL_URL_ROOT.
'/compta/facture/note.php?id='.$this->
id.
'" class="classfortooltip" title="'.
dol_escape_htmltag($notetoshow, 1, 1).
'">';
1922 $result .=
'</span>';
1926 global $action, $hookmanager;
1927 $hookmanager->initHooks(array(
'invoicedao'));
1928 $parameters = array(
'id'=>$this->
id,
'getnomurl' => &$result,
'notooltip' => $notooltip,
'addlinktonotes' => $addlinktonotes,
'save_lastsearch_value'=> $save_lastsearch_value,
'target' => $target);
1929 $reshook = $hookmanager->executeHooks(
'getNomUrl', $parameters, $this, $action);
1931 $result = $hookmanager->resPrint;
1933 $result .= $hookmanager->resPrint;
1949 public function fetch($rowid, $ref =
'', $ref_ext =
'', $notused =
'', $fetch_situation =
false)
1951 if (empty($rowid) && empty($ref) && empty($ref_ext)) {
1955 $sql =
'SELECT f.rowid,f.entity,f.ref,f.ref_client,f.ref_ext,f.ref_int,f.type,f.fk_soc';
1956 $sql .=
', f.total_tva, f.localtax1, f.localtax2, f.total_ht, f.total_ttc, f.revenuestamp';
1957 $sql .=
', f.remise_percent, f.remise_absolue, f.remise';
1958 $sql .=
', f.datef as df, f.date_pointoftax';
1959 $sql .=
', f.date_lim_reglement as dlr';
1960 $sql .=
', f.datec as datec';
1961 $sql .=
', f.date_valid as datev';
1962 $sql .=
', f.tms as datem';
1963 $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';
1964 $sql .=
', f.fk_facture_source, f.fk_fac_rec_source';
1965 $sql .=
', f.fk_mode_reglement, f.fk_cond_reglement, f.fk_projet as fk_project, f.extraparams';
1966 $sql .=
', f.situation_cycle_ref, f.situation_counter, f.situation_final';
1967 $sql .=
', f.fk_account';
1968 $sql .=
", f.fk_multicurrency, f.multicurrency_code, f.multicurrency_tx, f.multicurrency_total_ht, f.multicurrency_total_tva, f.multicurrency_total_ttc";
1969 $sql .=
', p.code as mode_reglement_code, p.libelle as mode_reglement_libelle';
1970 $sql .=
', c.code as cond_reglement_code, c.libelle as cond_reglement_libelle, c.libelle_facture as cond_reglement_libelle_doc';
1971 $sql .=
', f.fk_incoterms, f.location_incoterms';
1972 $sql .=
', f.module_source, f.pos_source';
1973 $sql .=
", i.libelle as label_incoterms";
1974 $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";
1975 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as f';
1976 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_payment_term as c ON f.fk_cond_reglement = c.rowid';
1977 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_paiement as p ON f.fk_mode_reglement = p.id';
1978 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'c_incoterms as i ON f.fk_incoterms = i.rowid';
1981 $sql .=
" WHERE f.rowid=".((int) $rowid);
1983 $sql .=
' WHERE f.entity IN ('.getEntity(
'invoice').
')';
1985 $sql .=
" AND f.ref='".$this->db->escape($ref).
"'";
1988 $sql .=
" AND f.ref_ext='".$this->db->escape($ref_ext).
"'";
1991 $sql .=
" AND f.ref_int='".$this->db->escape($notused).
"'";
1995 dol_syslog(get_class($this).
"::fetch", LOG_DEBUG);
1999 $obj = $this->
db->fetch_object(
$resql);
2001 $this->
id = $obj->rowid;
2002 $this->entity = $obj->entity;
2004 $this->
ref = $obj->ref;
2005 $this->ref_client = $obj->ref_client;
2006 $this->ref_customer = $obj->ref_client;
2007 $this->ref_ext = $obj->ref_ext;
2008 $this->
type = $obj->type;
2009 $this->date = $this->
db->jdate($obj->df);
2010 $this->date_pointoftax = $this->
db->jdate($obj->date_pointoftax);
2011 $this->date_creation = $this->
db->jdate($obj->datec);
2012 $this->date_validation = $this->
db->jdate($obj->datev);
2013 $this->date_modification = $this->
db->jdate($obj->datem);
2014 $this->datem = $this->
db->jdate($obj->datem);
2015 $this->remise_percent = $obj->remise_percent;
2016 $this->remise_absolue = $obj->remise_absolue;
2017 $this->total_ht = $obj->total_ht;
2018 $this->total_tva = $obj->total_tva;
2019 $this->total_localtax1 = $obj->localtax1;
2020 $this->total_localtax2 = $obj->localtax2;
2021 $this->total_ttc = $obj->total_ttc;
2022 $this->revenuestamp = $obj->revenuestamp;
2023 $this->paye = $obj->paye;
2024 $this->close_code = $obj->close_code;
2025 $this->close_note = $obj->close_note;
2027 $this->socid = $obj->fk_soc;
2028 $this->thirdparty =
null;
2030 $this->fk_project = $obj->fk_project;
2031 $this->project =
null;
2033 $this->statut = $obj->fk_statut;
2034 $this->status = $obj->fk_statut;
2036 $this->date_lim_reglement = $this->
db->jdate($obj->dlr);
2037 $this->mode_reglement_id = $obj->fk_mode_reglement;
2038 $this->mode_reglement_code = $obj->mode_reglement_code;
2039 $this->mode_reglement = $obj->mode_reglement_libelle;
2040 $this->cond_reglement_id = $obj->fk_cond_reglement;
2041 $this->cond_reglement_code = $obj->cond_reglement_code;
2042 $this->cond_reglement = $obj->cond_reglement_libelle;
2043 $this->cond_reglement_doc = $obj->cond_reglement_libelle_doc;
2044 $this->fk_account = ($obj->fk_account > 0) ? $obj->fk_account :
null;
2045 $this->fk_facture_source = $obj->fk_facture_source;
2046 $this->fk_fac_rec_source = $obj->fk_fac_rec_source;
2047 $this->note = $obj->note_private;
2048 $this->note_private = $obj->note_private;
2049 $this->note_public = $obj->note_public;
2050 $this->user_author = $obj->fk_user_author;
2051 $this->user_valid = $obj->fk_user_valid;
2052 $this->user_modification = $obj->fk_user_modif;
2053 $this->fk_user_author = $obj->fk_user_author;
2054 $this->fk_user_valid = $obj->fk_user_valid;
2055 $this->fk_user_modif = $obj->fk_user_modif;
2056 $this->model_pdf = $obj->model_pdf;
2057 $this->modelpdf = $obj->model_pdf;
2058 $this->last_main_doc = $obj->last_main_doc;
2059 $this->situation_cycle_ref = $obj->situation_cycle_ref;
2060 $this->situation_counter = $obj->situation_counter;
2061 $this->situation_final = $obj->situation_final;
2062 $this->retained_warranty = $obj->retained_warranty;
2063 $this->retained_warranty_date_limit = $this->db->jdate($obj->retained_warranty_date_limit);
2064 $this->retained_warranty_fk_cond_reglement = $obj->retained_warranty_fk_cond_reglement;
2066 $this->extraparams = (array) json_decode($obj->extraparams,
true);
2069 $this->fk_incoterms = $obj->fk_incoterms;
2070 $this->location_incoterms = $obj->location_incoterms;
2071 $this->label_incoterms = $obj->label_incoterms;
2073 $this->module_source = $obj->module_source;
2074 $this->pos_source = $obj->pos_source;
2077 $this->fk_multicurrency = $obj->fk_multicurrency;
2078 $this->multicurrency_code = $obj->multicurrency_code;
2079 $this->multicurrency_tx = $obj->multicurrency_tx;
2080 $this->multicurrency_total_ht = $obj->multicurrency_total_ht;
2081 $this->multicurrency_total_tva = $obj->multicurrency_total_tva;
2082 $this->multicurrency_total_ttc = $obj->multicurrency_total_ttc;
2084 if (($this->
type == self::TYPE_SITUATION || ($this->
type == self::TYPE_CREDIT_NOTE && $this->situation_cycle_ref > 0)) && $fetch_situation) {
2088 if ($this->status == self::STATUS_DRAFT) {
2089 $this->brouillon = 1;
2097 $this->lines = array();
2101 $this->error = $this->
db->error();
2109 $this->error =
'Invoice with id='.$rowid.
' or ref='.$ref.
' or ref_ext='.$ref_ext.
' not found';
2111 dol_syslog(__METHOD__.$this->error, LOG_WARNING);
2115 $this->error = $this->
db->lasterror();
2130 public function fetch_lines($only_product = 0, $loadalsotranslation = 0)
2133 global $langs, $conf;
2135 $this->lines = array();
2137 $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,';
2138 $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,';
2139 $sql .=
' l.situation_percent, l.fk_prev_id,';
2140 $sql .=
' l.rang, l.special_code,';
2141 $sql .=
' l.date_start as date_start, l.date_end as date_end,';
2142 $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,';
2143 $sql .=
' l.fk_unit,';
2144 $sql .=
' l.fk_multicurrency, l.multicurrency_code, l.multicurrency_subprice, l.multicurrency_total_ht, l.multicurrency_total_tva, l.multicurrency_total_ttc,';
2145 $sql .=
' p.ref as product_ref, p.fk_product_type as fk_product_type, p.label as product_label, p.description as product_desc';
2146 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet as l';
2147 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON l.fk_product = p.rowid';
2148 $sql .=
' WHERE l.fk_facture = '.((int) $this->
id);
2149 $sql .=
' ORDER BY l.rang, l.rowid';
2151 dol_syslog(get_class($this).
'::fetch_lines', LOG_DEBUG);
2152 $result = $this->
db->query($sql);
2154 $num = $this->
db->num_rows($result);
2157 $objp = $this->
db->fetch_object($result);
2160 $line->id = $objp->rowid;
2161 $line->rowid = $objp->rowid;
2162 $line->fk_facture = $objp->fk_facture;
2163 $line->label = $objp->custom_label;
2164 $line->desc = $objp->description;
2165 $line->description = $objp->description;
2166 $line->product_type = $objp->product_type;
2167 $line->ref = $objp->product_ref;
2168 $line->product_ref = $objp->product_ref;
2169 $line->libelle = $objp->product_label;
2170 $line->product_label = $objp->product_label;
2171 $line->product_desc = $objp->product_desc;
2172 $line->fk_product_type = $objp->fk_product_type;
2173 $line->qty = $objp->qty;
2174 $line->subprice = $objp->subprice;
2175 $line->ref_ext = $objp->ref_ext;
2177 $line->vat_src_code = $objp->vat_src_code;
2178 $line->tva_tx = $objp->tva_tx;
2179 $line->localtax1_tx = $objp->localtax1_tx;
2180 $line->localtax2_tx = $objp->localtax2_tx;
2181 $line->localtax1_type = $objp->localtax1_type;
2182 $line->localtax2_type = $objp->localtax2_type;
2183 $line->remise_percent = $objp->remise_percent;
2184 $line->fk_remise_except = $objp->fk_remise_except;
2185 $line->fk_product = $objp->fk_product;
2186 $line->date_start = $this->
db->jdate($objp->date_start);
2187 $line->date_end = $this->
db->jdate($objp->date_end);
2188 $line->date_start = $this->
db->jdate($objp->date_start);
2189 $line->date_end = $this->
db->jdate($objp->date_end);
2190 $line->info_bits = $objp->info_bits;
2191 $line->total_ht = $objp->total_ht;
2192 $line->total_tva = $objp->total_tva;
2193 $line->total_localtax1 = $objp->total_localtax1;
2194 $line->total_localtax2 = $objp->total_localtax2;
2195 $line->total_ttc = $objp->total_ttc;
2196 $line->code_ventilation = $objp->fk_code_ventilation;
2197 $line->fk_fournprice = $objp->fk_fournprice;
2198 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
2199 $line->pa_ht = $marginInfos[0];
2200 $line->marge_tx = $marginInfos[1];
2201 $line->marque_tx = $marginInfos[2];
2202 $line->rang = $objp->rang;
2203 $line->special_code = $objp->special_code;
2204 $line->fk_parent_line = $objp->fk_parent_line;
2205 $line->situation_percent = $objp->situation_percent;
2206 $line->fk_prev_id = $objp->fk_prev_id;
2207 $line->fk_unit = $objp->fk_unit;
2210 $line->fk_accounting_account = $objp->fk_code_ventilation;
2213 $line->fk_multicurrency = $objp->fk_multicurrency;
2214 $line->multicurrency_code = $objp->multicurrency_code;
2215 $line->multicurrency_subprice = $objp->multicurrency_subprice;
2216 $line->multicurrency_total_ht = $objp->multicurrency_total_ht;
2217 $line->multicurrency_total_tva = $objp->multicurrency_total_tva;
2218 $line->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
2220 $line->fetch_optionals();
2223 if (!empty($conf->global->MAIN_MULTILANGS) && !empty($objp->fk_product) && !empty($loadalsotranslation)) {
2225 $tmpproduct->fetch($objp->fk_product);
2226 $tmpproduct->getMultiLangs();
2228 $line->multilangs = $tmpproduct->multilangs;
2231 $this->lines[$i] = $line;
2235 $this->
db->free($result);
2238 $this->error = $this->
db->error();
2253 $this->tab_previous_situation_invoice = array();
2254 $this->tab_next_situation_invoice = array();
2256 $sql =
'SELECT rowid, type, situation_cycle_ref, situation_counter FROM '.MAIN_DB_PREFIX.
'facture';
2257 $sql .=
" WHERE rowid <> ".((int) $this->
id);
2258 $sql .=
' AND entity = '.((int) $this->entity);
2259 $sql .=
' AND situation_cycle_ref = '.(int) $this->situation_cycle_ref;
2260 $sql .=
' ORDER BY situation_counter ASC';
2262 dol_syslog(get_class($this).
'::fetchPreviousNextSituationInvoice ', LOG_DEBUG);
2263 $result = $this->
db->query($sql);
2264 if ($result && $this->
db->num_rows($result) > 0) {
2265 while ($objp = $this->
db->fetch_object($result)) {
2267 if ($invoice->fetch($objp->rowid) > 0) {
2268 if ($objp->situation_counter < $this->situation_counter
2269 || ($objp->situation_counter == $this->situation_counter && $objp->rowid < $this->id)
2271 $this->tab_previous_situation_invoice[] = $invoice;
2273 $this->tab_next_situation_invoice[] = $invoice;
2294 if (empty($this->
type)) {
2297 if (isset($this->
ref)) {
2298 $this->
ref = trim($this->
ref);
2300 if (isset($this->ref_ext)) {
2301 $this->ref_ext = trim($this->ref_ext);
2303 if (isset($this->ref_client)) {
2304 $this->ref_client = trim($this->ref_client);
2306 if (isset($this->increment)) {
2307 $this->increment = trim($this->increment);
2309 if (isset($this->close_code)) {
2310 $this->close_code = trim($this->close_code);
2312 if (isset($this->close_note)) {
2313 $this->close_note = trim($this->close_note);
2315 if (isset($this->note) || isset($this->note_private)) {
2316 $this->note = (isset($this->note) ? trim($this->note) : trim($this->note_private));
2318 if (isset($this->note) || isset($this->note_private)) {
2319 $this->note_private = (isset($this->note_private) ? trim($this->note_private) : trim($this->note));
2321 if (isset($this->note_public)) {
2322 $this->note_public = trim($this->note_public);
2324 if (isset($this->model_pdf)) {
2325 $this->model_pdf = trim($this->model_pdf);
2327 if (isset($this->import_key)) {
2328 $this->import_key = trim($this->import_key);
2330 if (isset($this->retained_warranty)) {
2331 $this->retained_warranty = floatval($this->retained_warranty);
2339 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture SET";
2340 $sql .=
" ref=".(isset($this->
ref) ?
"'".$this->db->escape($this->
ref).
"'" :
"null").
",";
2341 $sql .=
" ref_ext=".(isset($this->ref_ext) ?
"'".$this->db->escape($this->ref_ext).
"'" :
"null").
",";
2342 $sql .=
" type=".(isset($this->
type) ? $this->
db->escape($this->
type) :
"null").
",";
2343 $sql .=
" ref_client=".(isset($this->ref_client) ?
"'".$this->db->escape($this->ref_client).
"'" :
"null").
",";
2344 $sql .=
" increment=".(isset($this->increment) ?
"'".$this->db->escape($this->increment).
"'" :
"null").
",";
2345 $sql .=
" fk_soc=".(isset($this->socid) ? $this->
db->escape($this->socid) :
"null").
",";
2346 $sql .=
" datec=".(strval($this->date_creation) !=
'' ?
"'".$this->db->idate($this->date_creation).
"'" :
'null').
",";
2347 $sql .=
" datef=".(strval($this->date) !=
'' ?
"'".$this->db->idate($this->date).
"'" :
'null').
",";
2348 $sql .=
" date_pointoftax=".(strval($this->date_pointoftax) !=
'' ?
"'".$this->db->idate($this->date_pointoftax).
"'" :
'null').
",";
2349 $sql .=
" date_valid=".(strval($this->date_validation) !=
'' ?
"'".$this->db->idate($this->date_validation).
"'" :
'null').
",";
2350 $sql .=
" paye=".(isset($this->paye) ? $this->
db->escape($this->paye) : 0).
",";
2351 $sql .=
" remise_percent=".(isset($this->remise_percent) ? $this->
db->escape($this->remise_percent) :
"null").
",";
2352 $sql .=
" remise_absolue=".(isset($this->remise_absolue) ? $this->
db->escape($this->remise_absolue) :
"null").
",";
2353 $sql .=
" close_code=".(isset($this->close_code) ?
"'".$this->db->escape($this->close_code).
"'" :
"null").
",";
2354 $sql .=
" close_note=".(isset($this->close_note) ?
"'".$this->db->escape($this->close_note).
"'" :
"null").
",";
2355 $sql .=
" total_tva=".(isset($this->total_tva) ? $this->total_tva :
"null").
",";
2356 $sql .=
" localtax1=".(isset($this->total_localtax1) ? $this->total_localtax1 :
"null").
",";
2357 $sql .=
" localtax2=".(isset($this->total_localtax2) ? $this->total_localtax2 :
"null").
",";
2358 $sql .=
" total_ht=".(isset($this->total_ht) ? $this->total_ht :
"null").
",";
2359 $sql .=
" total_ttc=".(isset($this->total_ttc) ? $this->total_ttc :
"null").
",";
2360 $sql .=
" revenuestamp=".((isset($this->revenuestamp) && $this->revenuestamp !=
'') ? $this->
db->escape($this->revenuestamp) :
"null").
",";
2361 $sql .=
" fk_statut=".(isset($this->statut) ? $this->
db->escape($this->statut) :
"null").
",";
2362 $sql .=
" fk_user_author=".(isset($this->user_author) ? $this->
db->escape($this->user_author) :
"null").
",";
2363 $sql .=
" fk_user_valid=".(isset($this->fk_user_valid) ? $this->
db->escape($this->fk_user_valid) :
"null").
",";
2364 $sql .=
" fk_facture_source=".(isset($this->fk_facture_source) ? $this->
db->escape($this->fk_facture_source) :
"null").
",";
2365 $sql .=
" fk_projet=".(isset($this->fk_project) ? $this->
db->escape($this->fk_project) :
"null").
",";
2366 $sql .=
" fk_cond_reglement=".(isset($this->cond_reglement_id) ? $this->
db->escape($this->cond_reglement_id) :
"null").
",";
2367 $sql .=
" fk_mode_reglement=".(isset($this->mode_reglement_id) ? $this->
db->escape($this->mode_reglement_id) :
"null").
",";
2368 $sql .=
" date_lim_reglement=".(strval($this->date_lim_reglement) !=
'' ?
"'".$this->db->idate($this->date_lim_reglement).
"'" :
'null').
",";
2369 $sql .=
" note_private=".(isset($this->note_private) ?
"'".$this->db->escape($this->note_private).
"'" :
"null").
",";
2370 $sql .=
" note_public=".(isset($this->note_public) ?
"'".$this->db->escape($this->note_public).
"'" :
"null").
",";
2371 $sql .=
" model_pdf=".(isset($this->model_pdf) ?
"'".$this->db->escape($this->model_pdf).
"'" :
"null").
",";
2372 $sql .=
" import_key=".(isset($this->import_key) ?
"'".$this->db->escape($this->import_key).
"'" :
"null").
",";
2373 $sql .=
" situation_cycle_ref=".(empty($this->situation_cycle_ref) ?
"null" : $this->
db->escape($this->situation_cycle_ref)).
",";
2374 $sql .=
" situation_counter=".(empty($this->situation_counter) ?
"null" : $this->
db->escape($this->situation_counter)).
",";
2375 $sql .=
" situation_final=".(empty($this->situation_final) ?
"0" : $this->
db->escape($this->situation_final)).
",";
2376 $sql .=
" retained_warranty=".(empty($this->retained_warranty) ?
"0" : $this->
db->escape($this->retained_warranty)).
",";
2377 $sql .=
" retained_warranty_date_limit=".(strval($this->retained_warranty_date_limit) !=
'' ?
"'".$this->db->idate($this->retained_warranty_date_limit).
"'" :
'null').
",";
2378 $sql .=
" retained_warranty_fk_cond_reglement=".(isset($this->retained_warranty_fk_cond_reglement) ?intval($this->retained_warranty_fk_cond_reglement) :
"null");
2379 $sql .=
" WHERE rowid=".((int) $this->
id);
2383 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
2387 $this->errors[] =
"Error ".$this->db->lasterror();
2397 if (!$error && !$notrigger) {
2408 foreach ($this->errors as $errmsg) {
2409 dol_syslog(get_class($this).
"::update ".$errmsg, LOG_ERR);
2410 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2412 $this->
db->rollback();
2415 $this->
db->commit();
2431 global $conf, $langs;
2433 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
2434 include_once DOL_DOCUMENT_ROOT.
'/core/class/discount.class.php';
2439 $result = $remise->fetch($idremise);
2442 if ($remise->fk_facture) {
2443 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed");
2444 $this->
db->rollback();
2449 $facligne->fk_facture = $this->id;
2450 $facligne->fk_remise_except = $remise->id;
2451 $facligne->desc = $remise->description;
2452 $facligne->vat_src_code = $remise->vat_src_code;
2453 $facligne->tva_tx = $remise->tva_tx;
2454 $facligne->subprice = -$remise->amount_ht;
2455 $facligne->fk_product = 0;
2457 $facligne->remise_percent = 0;
2458 $facligne->rang = -1;
2459 $facligne->info_bits = 2;
2461 if (!empty($conf->global->MAIN_ADD_LINE_AT_POSITION)) {
2462 $facligne->rang = 1;
2463 $linecount = count($this->lines);
2464 for ($ii = 1; $ii <= $linecount; $ii++) {
2470 if ($remise->fk_facture_source > 0) {
2472 $srcinvoice->fetch($remise->fk_facture_source);
2473 include_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmargin.class.php';
2475 $arraytmp = $formmargin->getMarginInfosArray($srcinvoice,
false);
2476 $facligne->pa_ht = $arraytmp[
'pa_total'];
2479 $facligne->total_ht = -$remise->amount_ht;
2480 $facligne->total_tva = -$remise->amount_tva;
2481 $facligne->total_ttc = -$remise->amount_ttc;
2483 $facligne->multicurrency_subprice = -$remise->multicurrency_subprice;
2484 $facligne->multicurrency_total_ht = -$remise->multicurrency_amount_ht;
2485 $facligne->multicurrency_total_tva = -$remise->multicurrency_amount_tva;
2486 $facligne->multicurrency_total_ttc = -$remise->multicurrency_amount_ttc;
2488 $lineid = $facligne->insert();
2493 $result = $remise->link_to_invoice($lineid, 0);
2495 $this->error = $remise->error;
2496 $this->
db->rollback();
2500 $this->
db->commit();
2503 $this->error = $facligne->error;
2504 $this->
db->rollback();
2508 $this->error = $facligne->error;
2509 $this->
db->rollback();
2513 $this->
db->rollback();
2535 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
2536 if (empty($ref_client)) {
2537 $sql .=
' SET ref_client = NULL';
2539 $sql .=
' SET ref_client = \''.$this->db->escape($ref_client).
'\'';
2541 $sql .=
" WHERE rowid = ".((int) $this->
id);
2543 dol_syslog(__METHOD__.
' this->id='.$this->id.
', ref_client='.$ref_client, LOG_DEBUG);
2546 $this->errors[] = $this->
db->error();
2551 $this->ref_client = $ref_client;
2554 if (!$notrigger && empty($error)) {
2564 $this->ref_client = $ref_client;
2566 $this->
db->commit();
2569 foreach ($this->errors as $errmsg) {
2570 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
2571 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
2573 $this->
db->rollback();
2586 public function delete($user, $notrigger = 0, $idwarehouse = -1)
2588 global $langs, $conf;
2589 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
2593 dol_syslog(get_class($this).
"::delete rowid=".$rowid.
", ref=".$this->ref.
", thirdparty=".(empty($this->thirdparty) ?
'' : $this->thirdparty->name), LOG_DEBUG);
2606 if (!$error && !$notrigger) {
2620 dol_syslog(get_class($this).
"::delete error deleteExtraFields ".$this->error, LOG_ERR);
2634 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'societe_remise_except';
2635 $sql .=
' WHERE fk_facture_source = '.((int) $rowid);
2636 $sql .=
' AND fk_facture_line IS NULL';
2641 $list_rowid_det = array();
2642 foreach ($this->lines as $key => $invoiceline) {
2643 $list_rowid_det[] = $invoiceline->id;
2647 if (count($list_rowid_det)) {
2648 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
2649 $sql .=
' SET fk_facture = NULL, fk_facture_line = NULL';
2650 $sql .=
' WHERE fk_facture_line IN ('.$this->db->sanitize(join(
',', $list_rowid_det)).
')';
2652 if (!$this->
db->query($sql)) {
2653 $this->error = $this->
db->error().
" sql=".$sql;
2654 $this->errors[] = $this->error;
2655 $this->
db->rollback();
2662 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'eventorganization_conferenceorboothattendee';
2663 $sql .=
' SET fk_invoice = NULL';
2664 $sql .=
' WHERE fk_invoice = '.((int) $rowid);
2666 if (!$this->
db->query($sql)) {
2667 $this->error = $this->
db->error().
" sql=".$sql;
2668 $this->errors[] = $this->error;
2669 $this->
db->rollback();
2673 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'projet_task_time';
2674 $sql .=
' SET invoice_id = NULL, invoice_line_id = NULL';
2675 $sql .=
' WHERE invoice_id = '.((int) $rowid);
2677 if (!$this->
db->query($sql)) {
2678 $this->error = $this->
db->error().
" sql=".$sql;
2679 $this->errors[] = $this->error;
2680 $this->
db->rollback();
2685 if ($this->
type != self::TYPE_DEPOSIT && $result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_BILL) && $idwarehouse != -1) {
2686 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
2687 $langs->load(
"agenda");
2689 $num = count($this->lines);
2690 for ($i = 0; $i < $num; $i++) {
2691 if ($this->lines[$i]->fk_product > 0) {
2693 $mouvP->origin = &$this;
2694 $mouvP->setOrigin($this->element, $this->
id);
2696 if ($this->
type == self::TYPE_CREDIT_NOTE) {
2697 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceDeleteDolibarr", $this->ref));
2699 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"InvoiceDeleteDolibarr", $this->ref));
2706 $main = MAIN_DB_PREFIX.
'facturedet';
2707 $ef = $main.
"_extrafields";
2708 $sqlef =
"DELETE FROM ".$ef.
" WHERE fk_object IN (SELECT rowid FROM ".$main.
" WHERE fk_facture = ".((int) $rowid).
")";
2710 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facturedet WHERE fk_facture = '.((int) $rowid);
2713 $sql =
'DELETE FROM '.MAIN_DB_PREFIX.
'facture WHERE rowid = '.((int) $rowid);
2722 if ($conf->facture->dir_output && !empty($this->
ref)) {
2723 $dir = $conf->facture->dir_output.
"/".$ref;
2724 $file = $conf->facture->dir_output.
"/".$ref.
"/".$ref.
".pdf";
2725 if (file_exists($file)) {
2729 $langs->load(
"errors");
2730 $this->error = $langs->trans(
"ErrorFailToDeleteFile", $file);
2731 $this->errors[] = $this->error;
2732 $this->
db->rollback();
2736 if (file_exists($dir)) {
2738 $langs->load(
"errors");
2739 $this->error = $langs->trans(
"ErrorFailToDeleteDir", $dir);
2740 $this->errors[] = $this->error;
2741 $this->
db->rollback();
2747 $this->
db->commit();
2750 $this->error = $this->
db->lasterror().
" sql=".$sql;
2751 $this->errors[] = $this->error;
2752 $this->
db->rollback();
2756 $this->error = $this->
db->lasterror().
" sql=".$sql;
2757 $this->errors[] = $this->error;
2758 $this->
db->rollback();
2762 $this->
db->rollback();
2779 public function set_paid($user, $close_code =
'', $close_note =
'')
2782 dol_syslog(get_class($this).
"::set_paid is deprecated, use setPaid instead", LOG_NOTICE);
2783 return $this->
setPaid($user, $close_code, $close_note);
2795 public function setPaid($user, $close_code =
'', $close_note =
'')
2799 if ($this->paye != 1) {
2804 dol_syslog(get_class($this).
"::setPaid rowid=".((
int) $this->
id), LOG_DEBUG);
2806 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture SET';
2807 $sql .=
' fk_statut='.self::STATUS_CLOSED;
2812 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
2815 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
2817 $sql .=
', fk_user_closing = '.((int) $user->id);
2818 $sql .=
", date_closing = '".$this->db->idate($now).
"'";
2819 $sql .=
" WHERE rowid = ".((int) $this->
id);
2831 $this->error = $this->
db->lasterror();
2835 $this->
db->commit();
2838 $this->
db->rollback();
2861 dol_syslog(get_class($this).
"::set_unpaid is deprecated, use setUnpaid instead", LOG_NOTICE);
2879 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
2880 $sql .=
' SET paye=0, fk_statut='.self::STATUS_VALIDATED.
', close_code=null, close_note=null,';
2881 $sql .=
' date_closing=null,';
2882 $sql .=
' fk_user_closing=null';
2883 $sql .=
" WHERE rowid = ".((int) $this->
id);
2885 dol_syslog(get_class($this).
"::setUnpaid", LOG_DEBUG);
2896 $this->error = $this->
db->error();
2901 $this->
db->commit();
2904 $this->
db->rollback();
2926 dol_syslog(get_class($this).
"::set_canceled is deprecated, use setCanceled instead", LOG_NOTICE);
2927 return $this->
setCanceled($user, $close_code, $close_note);
2940 public function setCanceled($user, $close_code =
'', $close_note =
'')
2942 dol_syslog(get_class($this).
"::setCanceled rowid=".((
int) $this->
id), LOG_DEBUG);
2946 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture SET';
2947 $sql .=
' fk_statut='.self::STATUS_ABANDONED;
2949 $sql .=
", close_code='".$this->db->escape($close_code).
"'";
2952 $sql .=
", close_note='".$this->db->escape($close_note).
"'";
2954 $sql .=
" WHERE rowid = ".((int) $this->
id);
2960 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
2961 $sql .=
' SET fk_facture = NULL';
2962 $sql .=
' WHERE fk_facture = '.((int) $this->
id);
2969 $this->
db->rollback();
2974 $this->
db->commit();
2977 $this->error = $this->
db->error().
" sql=".$sql;
2978 $this->
db->rollback();
2982 $this->error = $this->
db->error().
" sql=".$sql;
2983 $this->
db->rollback();
2999 public function validate($user, $force_number =
'', $idwarehouse = 0, $notrigger = 0, $batch_rule = 0)
3001 global $conf, $langs, $mysoc;
3002 require_once DOL_DOCUMENT_ROOT.
'/core/lib/files.lib.php';
3004 $productStatic =
null;
3005 $warehouseStatic =
null;
3006 if ($batch_rule > 0) {
3007 require_once DOL_DOCUMENT_ROOT.
'/product/class/product.class.php';
3008 require_once DOL_DOCUMENT_ROOT.
'/product/class/productbatch.class.php';
3009 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/entrepot.class.php';
3010 $productStatic =
new Product($this->
db);
3012 $productbatch =
new ProductBatch($this->
db);
3018 dol_syslog(get_class($this).
'::validate user='.$user->id.
', force_number='.$force_number.
', idwarehouse='.$idwarehouse);
3025 if ($this->statut != self::STATUS_DRAFT) {
3026 dol_syslog(get_class($this).
"::validate status is not draft. operation canceled.", LOG_WARNING);
3029 if (count($this->lines) <= 0) {
3030 $langs->load(
"errors");
3031 $this->error = $langs->trans(
"ErrorObjectMustHaveLinesToBeValidated", $this->
ref);
3034 if ((empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->facture->creer))
3035 || (!empty($conf->global->MAIN_USE_ADVANCED_PERMS) && empty($user->rights->facture->invoice_advance->validate))) {
3036 $this->error =
'Permission denied';
3037 dol_syslog(get_class($this).
"::validate ".$this->error.
' MAIN_USE_ADVANCED_PERMS='.$conf->global->MAIN_USE_ADVANCED_PERMS, LOG_ERR);
3040 if (!empty($conf->global-> INVOICE_CHECK_POSTERIOR_DATE)) {
3042 if (!$last_of_type[0]) {
3043 $this->error = $langs->transnoentities(
"ErrorInvoiceIsNotLastOfSameType", $this->
ref,
dol_print_date($this->date,
'day'),
dol_print_date($last_of_type[1],
'day'));
3049 if (!empty($this->thirdparty) && is_object($this->thirdparty)) {
3050 $array_to_check = array(
'IDPROF1',
'IDPROF2',
'IDPROF3',
'IDPROF4',
'IDPROF5',
'IDPROF6',
'EMAIL',
'ACCOUNTANCY_CODE_CUSTOMER');
3051 foreach ($array_to_check as $key) {
3052 $keymin = strtolower($key);
3053 if (!property_exists($this->thirdparty, $keymin)) {
3056 $vallabel = $this->thirdparty->$keymin;
3058 $i = (int) preg_replace(
'/[^0-9]/',
'', $key);
3060 if ($this->thirdparty->isACompany()) {
3062 if ($mysoc->country_id > 0 && $this->thirdparty->country_id == $mysoc->country_id) {
3063 $idprof_mandatory =
'SOCIETE_'.$key.
'_INVOICE_MANDATORY';
3064 if (!$vallabel && !empty($conf->global->$idprof_mandatory)) {
3065 $langs->load(
"errors");
3066 $this->error = $langs->trans(
'ErrorProdIdIsMandatory', $langs->transcountry(
'ProfId'.$i, $this->thirdparty->country_code)).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
3067 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3073 if ($key ==
'EMAIL') {
3075 if (!empty($conf->global->SOCIETE_EMAIL_INVOICE_MANDATORY) && !isValidEMail($this->thirdparty->email)) {
3076 $langs->load(
"errors");
3077 $this->error = $langs->trans(
"ErrorBadEMail", $this->thirdparty->email).
' ('.$langs->trans(
"ForbiddenBySetupRules").
') ['.$langs->trans(
'Company').
' : '.$this->thirdparty->name.
']';
3078 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3082 if ($key ==
'ACCOUNTANCY_CODE_CUSTOMER') {
3084 if (!empty($conf->global->SOCIETE_ACCOUNTANCY_CODE_CUSTOMER_INVOICE_MANDATORY) && empty($this->thirdparty->code_compta)) {
3085 $langs->load(
"errors");
3086 $this->error = $langs->trans(
"ErrorAccountancyCodeCustomerIsMandatory", $this->thirdparty->name).
' ('.$langs->trans(
"ForbiddenBySetupRules").
')';
3087 dol_syslog(__METHOD__.
' '.$this->error, LOG_ERR);
3096 $array_to_check = array(
'REF_CLIENT'=>
'RefCustomer');
3097 foreach ($array_to_check as $key => $val) {
3098 $keymin = strtolower($key);
3099 $vallabel = $this->$keymin;
3102 $keymandatory =
'INVOICE_'.$key.
'_MANDATORY_FOR_VALIDATION';
3103 if (!$vallabel && !empty($conf->global->$keymandatory)) {
3104 $langs->load(
"errors");
3106 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv($val)),
null,
'errors');
3113 if ($this->
type == self::TYPE_REPLACEMENT) {
3115 if ($this->fk_facture_source <= 0) {
3116 $this->error = $langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"InvoiceReplacement"));
3117 $this->
db->rollback();
3123 $result = $facreplaced->fetch($this->fk_facture_source);
3125 $this->error = $langs->trans(
"ErrorBadInvoice");
3126 $this->
db->rollback();
3131 $idreplacement = $facreplaced->getIdReplacingInvoice(
'validated');
3132 if ($idreplacement && $idreplacement != $this->
id) {
3133 $facreplacement =
new Facture($this->
db);
3134 $facreplacement->fetch($idreplacement);
3135 $this->error = $langs->trans(
"ErrorInvoiceAlreadyReplaced", $facreplaced->ref, $facreplacement->ref);
3136 $this->
db->rollback();
3140 $result = $facreplaced->setCanceled($user, self::CLOSECODE_REPLACED,
'');
3142 $this->error = $facreplaced->error;
3143 $this->
db->rollback();
3149 if ($force_number) {
3150 $num = $force_number;
3151 } elseif (preg_match(
'/^[\(]?PROV/i', $this->
ref) || empty($this->
ref)) {
3152 if (!empty($conf->global->FAC_FORCE_DATE_VALIDATION)) {
3167 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
3168 $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).
"'";
3169 if (!empty($conf->global->FAC_FORCE_DATE_VALIDATION)) {
3170 $sql .=
", datef='".$this->db->idate($this->date).
"'";
3171 $sql .=
", date_lim_reglement='".$this->db->idate($this->date_lim_reglement).
"'";
3173 $sql .=
" WHERE rowid = ".((int) $this->
id);
3175 dol_syslog(get_class($this).
"::validate", LOG_DEBUG);
3183 if (!$error && (preg_match(
'/^[\(]?PROV/i', $this->
ref))) {
3189 $result = $this->thirdparty->set_as_client();
3192 if ($this->
type != self::TYPE_DEPOSIT && $result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_BILL) && $idwarehouse > 0) {
3193 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
3194 $langs->load(
"agenda");
3197 $cpt = count($this->lines);
3198 for ($i = 0; $i < $cpt; $i++) {
3199 if ($this->lines[$i]->fk_product > 0) {
3201 $mouvP->origin = &$this;
3202 $mouvP->setOrigin($this->element, $this->
id);
3204 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3205 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
3208 $this->error = $mouvP->error;
3211 $is_batch_line =
false;
3212 if ($batch_rule > 0) {
3213 $productStatic->fetch($this->lines[$i]->fk_product);
3214 if ($productStatic->hasbatch()) {
3215 $is_batch_line =
true;
3216 $product_qty_remain = $this->lines[$i]->qty;
3222 $sortfield =
'pl.sellby,pl.eatby,pb.qty,pl.rowid';
3223 $sortorder =
'ASC,ASC,ASC,ASC';
3226 $resBatchList = $productbatch->findAllForProduct($productStatic->id, $idwarehouse, (!empty($conf->global->STOCK_ALLOW_NEGATIVE_TRANSFER) ?
null : 0), $sortfield, $sortorder);
3227 if (!is_array($resBatchList)) {
3229 $this->error = $this->
db->lasterror();
3233 $batchList = $resBatchList;
3234 if (empty($batchList)) {
3236 $langs->load(
'errors');
3237 $warehouseStatic->fetch($idwarehouse);
3238 $this->error = $langs->trans(
'ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref);
3239 dol_syslog(__METHOD__.
' Error: '.$langs->transnoentitiesnoconv(
'ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR);
3242 foreach ($batchList as $batch) {
3243 if ($batch->qty <= 0) {
3248 if ($batch->qty >= $product_qty_remain) {
3249 $product_batch_qty = $product_qty_remain;
3252 $product_batch_qty = $batch->qty;
3254 $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_batch_qty, $this->lines[$i]->subprice, $langs->trans(
'InvoiceValidatedInDolibarr', $num),
'',
'',
'', $batch->batch);
3257 $this->error = $mouvP->error;
3261 $product_qty_remain -= $product_batch_qty;
3263 if ($product_qty_remain <= 0) {
3268 if (!$error && $product_qty_remain > 0) {
3269 if ($conf->global->STOCK_ALLOW_NEGATIVE_TRANSFER) {
3271 $batch = $batchList[0];
3272 $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_qty_remain, $this->lines[$i]->subprice, $langs->trans(
'InvoiceValidatedInDolibarr', $num),
'',
'',
'', $batch->batch);
3275 $this->error = $mouvP->error;
3279 $langs->load(
'errors');
3280 $warehouseStatic->fetch($idwarehouse);
3281 $this->error = $langs->trans(
'ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref);
3282 dol_syslog(__METHOD__.
' Error: '.$langs->transnoentitiesnoconv(
'ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR);
3289 if (!$is_batch_line) {
3290 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceValidatedInDolibarr", $num));
3293 $this->error = $mouvP->error;
3306 if (!$error && $this->
type == self::TYPE_CREDIT_NOTE && $this->fk_facture_source > 0) {
3307 $invoice_situation =
new Facture($this->
db);
3308 $result = $invoice_situation->fetch($this->fk_facture_source);
3309 if ($result > 0 && $invoice_situation->type == self::TYPE_SITUATION && $invoice_situation->situation_final == 1) {
3310 $invoice_situation->situation_final = 0;
3312 $result = $invoice_situation->setFinal($user, 1);
3315 $this->error = $invoice_situation->error;
3316 $this->errors = $invoice_situation->errors;
3322 if (!$error && !$notrigger) {
3332 $this->oldref = $this->ref;
3335 if (preg_match(
'/^[\(]?PROV/i', $this->
ref)) {
3337 $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).
"'";
3338 $sql .=
" WHERE filename LIKE '".$this->db->escape($this->
ref).
"%' AND filepath = 'facture/".$this->
db->escape($this->
ref).
"' and entity = ".$conf->entity;
3342 $this->error = $this->
db->lasterror();
3348 $dirsource = $conf->facture->dir_output.
'/'.$oldref;
3349 $dirdest = $conf->facture->dir_output.
'/'.$newref;
3350 if (!$error && file_exists($dirsource)) {
3351 dol_syslog(get_class($this).
"::validate rename dir ".$dirsource.
" into ".$dirdest);
3353 if (@rename($dirsource, $dirdest)) {
3356 $listoffiles =
dol_dir_list($conf->facture->dir_output.
'/'.$newref,
'files', 1,
'^'.preg_quote($oldref,
'/'));
3357 foreach ($listoffiles as $fileentry) {
3358 $dirsource = $fileentry[
'name'];
3359 $dirdest = preg_replace(
'/^'.preg_quote($oldref,
'/').
'/', $newref, $dirsource);
3360 $dirsource = $fileentry[
'path'].
'/'.$dirsource;
3361 $dirdest = $fileentry[
'path'].
'/'.$dirdest;
3362 @rename($dirsource, $dirdest);
3381 $this->brouillon = 0;
3382 $this->date_validation = $now;
3385 if (!empty($conf->global->INVOICE_USE_SITUATION)) {
3387 $nboflines = count($this->lines);
3388 while (($i < $nboflines) && $final) {
3389 $final = ($this->lines[$i]->situation_percent == 100);
3393 if (empty($final)) {
3394 $this->situation_final = 0;
3396 $this->situation_final = 1;
3407 $this->
db->commit();
3410 $this->
db->rollback();
3423 foreach ($this->tab_next_situation_invoice as $next_invoice) {
3424 $is_last = $next_invoice->is_last_in_cycle();
3426 if ($next_invoice->statut == self::STATUS_DRAFT && $is_last != 1) {
3427 $this->error = $langs->trans(
'updatePriceNextInvoiceErrorUpdateline', $next_invoice->ref);
3431 $next_invoice->brouillon = 1;
3433 foreach ($next_invoice->lines as $line) {
3434 $result = $next_invoice->updateline(
3439 $line->remise_percent,
3443 $line->localtax1_tx,
3444 $line->localtax2_tx,
3447 $line->product_type,
3448 $line->fk_parent_line,
3450 $line->fk_fournprice,
3453 $line->special_code,
3454 $line->array_options,
3455 $line->situation_percent,
3460 $this->error = $langs->trans(
'updatePriceNextInvoiceErrorUpdateline', $next_invoice->ref);
3481 global $conf, $langs;
3485 if ($this->statut == self::STATUS_DRAFT) {
3486 dol_syslog(__METHOD__.
" already draft status", LOG_WARNING);
3494 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facture";
3495 $sql .=
" SET fk_statut = ".self::STATUS_DRAFT;
3496 $sql .=
" WHERE rowid = ".((int) $this->
id);
3498 $result = $this->
db->query($sql);
3501 $this->oldcopy = clone $this;
3505 if ($this->
type != self::TYPE_DEPOSIT && $result >= 0 && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
3506 require_once DOL_DOCUMENT_ROOT.
'/product/stock/class/mouvementstock.class.php';
3507 $langs->load(
"agenda");
3509 $num = count($this->lines);
3510 for ($i = 0; $i < $num; $i++) {
3511 if ($this->lines[$i]->fk_product > 0) {
3513 $mouvP->origin = &$this;
3514 $mouvP->setOrigin($this->element, $this->
id);
3516 if ($this->
type == self::TYPE_CREDIT_NOTE) {
3517 $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
3519 $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans(
"InvoiceBackToDraftInDolibarr", $this->ref));
3526 $old_statut = $this->statut;
3527 $this->brouillon = 1;
3532 $result = $this->
call_trigger(
'BILL_UNVALIDATE', $user);
3535 $this->statut = $old_statut;
3536 $this->status = $old_statut;
3537 $this->brouillon = 0;
3541 $this->
db->rollback();
3546 $this->
db->commit();
3549 $this->
db->rollback();
3553 $this->error = $this->
db->error();
3554 $this->
db->rollback();
3609 $remise_percent = 0,
3614 $fk_remise_except =
'',
3615 $price_base_type =
'HT',
3622 $fk_parent_line = 0,
3623 $fk_fournprice =
null,
3627 $situation_percent = 100,
3632 $noupdateafterinsertline = 0
3636 dol_syslog(__METHOD__.
": using line label is deprecated", LOG_WARNING);
3640 global $mysoc, $conf, $langs;
3642 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);
3644 if ($this->statut == self::STATUS_DRAFT) {
3645 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3648 if (empty($remise_percent)) {
3649 $remise_percent = 0;
3654 if (empty($info_bits)) {
3660 if (empty($ventil)) {
3663 if (empty($txtva)) {
3666 if (empty($txlocaltax1)) {
3669 if (empty($txlocaltax2)) {
3672 if (empty($fk_parent_line) || $fk_parent_line < 0) {
3673 $fk_parent_line = 0;
3675 if (empty($fk_prev_id)) {
3676 $fk_prev_id =
'null';
3678 if (!isset($situation_percent) || $situation_percent > 100 || (
string) $situation_percent ==
'') {
3679 $situation_percent = 100;
3681 if (empty($ref_ext)) {
3685 $remise_percent =
price2num($remise_percent);
3688 $pu_ht_devise =
price2num($pu_ht_devise);
3691 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3697 if ($price_base_type ==
'HT') {
3708 if ($date_start && $date_end && $date_start > $date_end) {
3709 $langs->load(
"errors");
3710 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3716 $product_type = $type;
3717 if (!empty($fk_product) && $fk_product > 0) {
3719 $result = $product->fetch($fk_product);
3720 $product_type = $product->type;
3722 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_INVOICE) && $product_type == 0 && $product->stock_reel < $qty) {
3723 $langs->load(
"errors");
3724 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnInvoice', $product->ref);
3725 $this->
db->rollback();
3735 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3736 $vat_src_code = $reg[1];
3737 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3745 $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);
3747 $total_ht = $tabprice[0];
3748 $total_tva = $tabprice[1];
3749 $total_ttc = $tabprice[2];
3750 $total_localtax1 = $tabprice[9];
3751 $total_localtax2 = $tabprice[10];
3752 $pu_ht = $tabprice[3];
3755 $multicurrency_total_ht = $tabprice[16];
3756 $multicurrency_total_tva = $tabprice[17];
3757 $multicurrency_total_ttc = $tabprice[18];
3758 $pu_ht_devise = $tabprice[19];
3762 if ($ranktouse == -1) {
3763 $rangmax = $this->
line_max($fk_parent_line);
3764 $ranktouse = $rangmax + 1;
3770 $this->line->context = $this->context;
3772 $this->line->fk_facture = $this->id;
3773 $this->line->label = $label;
3774 $this->line->desc = $desc;
3775 $this->line->ref_ext = $ref_ext;
3777 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ? abs($qty) : $qty);
3778 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht) : $pu_ht);
3780 $this->line->vat_src_code = $vat_src_code;
3781 $this->line->tva_tx = $txtva;
3782 $this->line->localtax1_tx = ($total_localtax1 ? $localtaxes_type[1] : 0);
3783 $this->line->localtax2_tx = ($total_localtax2 ? $localtaxes_type[3] : 0);
3784 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
3785 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
3787 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ht) : $total_ht);
3788 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_ttc) : $total_ttc);
3789 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_tva) : $total_tva);
3790 $this->line->total_localtax1 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax1) : $total_localtax1);
3791 $this->line->total_localtax2 = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($total_localtax2) : $total_localtax2);
3793 $this->line->fk_product = $fk_product;
3794 $this->line->product_type = $product_type;
3795 $this->line->remise_percent = $remise_percent;
3796 $this->line->date_start = $date_start;
3797 $this->line->date_end = $date_end;
3798 $this->line->ventil = $ventil;
3799 $this->line->rang = $ranktouse;
3800 $this->line->info_bits = $info_bits;
3801 $this->line->fk_remise_except = $fk_remise_except;
3803 $this->line->special_code = $special_code;
3804 $this->line->fk_parent_line = $fk_parent_line;
3805 $this->line->origin = $origin;
3806 $this->line->origin_id = $origin_id;
3807 $this->line->situation_percent = $situation_percent;
3808 $this->line->fk_prev_id = $fk_prev_id;
3809 $this->line->fk_unit = $fk_unit;
3812 $this->line->fk_fournprice = $fk_fournprice;
3813 $this->line->pa_ht = $pa_ht;
3816 $this->line->fk_multicurrency = $this->fk_multicurrency;
3817 $this->line->multicurrency_code = $this->multicurrency_code;
3818 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ? -abs($pu_ht_devise) : $pu_ht_devise);
3820 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ht) : $multicurrency_total_ht);
3821 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_tva) : $multicurrency_total_tva);
3822 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ? -abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
3824 if (is_array($array_options) && count($array_options) > 0) {
3825 $this->line->array_options = $array_options;
3828 $result = $this->line->insert();
3831 if (!empty($fk_parent_line)) {
3833 } elseif ($ranktouse > 0 && $ranktouse <= count($this->lines)) {
3834 $linecount = count($this->lines);
3835 for ($ii = $ranktouse; $ii <= $linecount; $ii++) {
3841 if (empty($noupdateafterinsertline)) {
3846 $this->
db->commit();
3847 return $this->line->id;
3849 $this->error = $this->
db->lasterror();
3850 $this->
db->rollback();
3854 $this->error = $this->line->error;
3855 $this->errors = $this->line->errors;
3856 $this->
db->rollback();
3860 dol_syslog(get_class($this).
"::addline status of invoice must be Draft to allow use of ->addline()", LOG_ERR);
3896 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)
3898 global $conf, $user;
3901 dol_syslog(__METHOD__.
": using line label is deprecated", LOG_WARNING);
3904 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
3906 global $mysoc, $langs;
3908 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);
3910 if ($this->statut == self::STATUS_DRAFT) {
3913 if (!$this->error) {
3914 $this->error = $langs->trans(
'invoiceLineProgressError');
3920 if ($date_start && $date_end && $date_start > $date_end) {
3921 $langs->load(
"errors");
3922 $this->error = $langs->trans(
'ErrorStartDateGreaterEnd');
3932 if (empty($fk_parent_line) || $fk_parent_line < 0) {
3933 $fk_parent_line = 0;
3935 if (empty($special_code) || $special_code == 3) {
3938 if (!isset($situation_percent) || $situation_percent > 100 || (
string) $situation_percent ==
'') {
3939 $situation_percent = 100;
3941 if (empty($ref_ext)) {
3945 $remise_percent =
price2num($remise_percent);
3948 $pu_ht_devise =
price2num($pu_ht_devise);
3950 if (!preg_match(
'/\((.*)\)/', $txtva)) {
3970 if (preg_match(
'/\((.*)\)/', $txtva, $reg)) {
3971 $vat_src_code = $reg[1];
3972 $txtva = preg_replace(
'/\s*\(.*\)/',
'', $txtva);
3975 $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);
3977 $total_ht = $tabprice[0];
3978 $total_tva = $tabprice[1];
3979 $total_ttc = $tabprice[2];
3980 $total_localtax1 = $tabprice[9];
3981 $total_localtax2 = $tabprice[10];
3982 $pu_ht = $tabprice[3];
3983 $pu_tva = $tabprice[4];
3984 $pu_ttc = $tabprice[5];
3987 $multicurrency_total_ht = $tabprice[16];
3988 $multicurrency_total_tva = $tabprice[17];
3989 $multicurrency_total_ttc = $tabprice[18];
3990 $pu_ht_devise = $tabprice[19];
3995 if ($remise_percent > 0) {
3996 $remise = round(($pu * $remise_percent / 100), 2);
3997 $price = ($pu - $remise);
4003 $line->fetch($rowid);
4004 $line->fetch_optionals();
4006 if (!empty($line->fk_product)) {
4008 $result = $product->fetch($line->fk_product);
4009 $product_type = $product->type;
4011 if (!empty($conf->global->STOCK_MUST_BE_ENOUGH_FOR_INVOICE) && $product_type == 0 && $product->stock_reel < $qty) {
4012 $langs->load(
"errors");
4013 $this->error = $langs->trans(
'ErrorStockIsNotEnoughToAddProductOnInvoice', $product->ref);
4014 $this->
db->rollback();
4019 $staticline = clone $line;
4021 $line->oldline = $staticline;
4022 $this->line = $line;
4023 $this->line->context = $this->context;
4024 $this->line->rang = $rang;
4027 if (!empty($fk_parent_line) && !empty($staticline->fk_parent_line) && $fk_parent_line != $staticline->fk_parent_line) {
4028 $rangmax = $this->
line_max($fk_parent_line);
4029 $this->line->rang = $rangmax + 1;
4032 $this->line->id = $rowid;
4033 $this->line->rowid = $rowid;
4034 $this->line->label = $label;
4035 $this->line->desc = $desc;
4036 $this->line->ref_ext = $ref_ext;
4037 $this->line->qty = ($this->
type == self::TYPE_CREDIT_NOTE ?abs($qty) : $qty);
4039 $this->line->vat_src_code = $vat_src_code;
4040 $this->line->tva_tx = $txtva;
4041 $this->line->localtax1_tx = $txlocaltax1;
4042 $this->line->localtax2_tx = $txlocaltax2;
4043 $this->line->localtax1_type = empty($localtaxes_type[0]) ?
'' : $localtaxes_type[0];
4044 $this->line->localtax2_type = empty($localtaxes_type[2]) ?
'' : $localtaxes_type[2];
4046 $this->line->remise_percent = $remise_percent;
4047 $this->line->subprice = ($this->
type == self::TYPE_CREDIT_NOTE ?-abs($pu_ht) : $pu_ht);
4048 $this->line->date_start = $date_start;
4049 $this->line->date_end = $date_end;
4050 $this->line->total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ht) : $total_ht);
4051 $this->line->total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_tva) : $total_tva);
4052 $this->line->total_localtax1 = $total_localtax1;
4053 $this->line->total_localtax2 = $total_localtax2;
4054 $this->line->total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($total_ttc) : $total_ttc);
4055 $this->line->info_bits = $info_bits;
4056 $this->line->special_code = $special_code;
4057 $this->line->product_type = $type;
4058 $this->line->fk_parent_line = $fk_parent_line;
4059 $this->line->skip_update_total = $skip_update_total;
4060 $this->line->situation_percent = $situation_percent;
4061 $this->line->fk_unit = $fk_unit;
4063 $this->line->fk_fournprice = $fk_fournprice;
4064 $this->line->pa_ht = $pa_ht;
4067 $this->line->multicurrency_subprice = ($this->
type == self::TYPE_CREDIT_NOTE ?-abs($pu_ht_devise) : $pu_ht_devise);
4068 $this->line->multicurrency_total_ht = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_ht) : $multicurrency_total_ht);
4069 $this->line->multicurrency_total_tva = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_tva) : $multicurrency_total_tva);
4070 $this->line->multicurrency_total_ttc = (($this->
type == self::TYPE_CREDIT_NOTE || $qty < 0) ?-abs($multicurrency_total_ttc) : $multicurrency_total_ttc);
4072 if (is_array($array_options) && count($array_options) > 0) {
4074 foreach ($array_options as $key => $value) {
4075 $this->line->array_options[$key] = $array_options[$key];
4079 $result = $this->line->update($user, $notrigger);
4082 if (!empty($fk_parent_line)) {
4088 $this->
db->commit();
4091 $this->error = $this->line->error;
4092 $this->
db->rollback();
4096 $this->error =
"Invoice statut makes operation forbidden";
4110 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd
4111 INNER JOIN '.MAIN_DB_PREFIX.
'facture f ON (fd.fk_facture = f.rowid)
4112 WHERE fd.fk_prev_id = '.((int) $idline).
' AND f.fk_statut <> 0';
4114 $result = $this->
db->query($sql);
4116 $this->error = $this->
db->error();
4120 $obj = $this->
db->fetch_object($result);
4122 if ($obj ===
null) {
4125 return $situation_percent < $obj->situation_percent;
4141 global $mysoc, $user;
4144 if (($line->info_bits & 2) == 2) {
4148 include_once DOL_DOCUMENT_ROOT.
'/core/lib/price.lib.php';
4151 if ($percent > 100) {
4154 $line->situation_percent = $percent;
4155 $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);
4156 $line->total_ht = $tabprice[0];
4157 $line->total_tva = $tabprice[1];
4158 $line->total_ttc = $tabprice[2];
4159 $line->total_localtax1 = $tabprice[9];
4160 $line->total_localtax2 = $tabprice[10];
4161 $line->multicurrency_total_ht = $tabprice[16];
4162 $line->multicurrency_total_tva = $tabprice[17];
4163 $line->multicurrency_total_ttc = $tabprice[18];
4164 $line->update($user);
4167 if ($update_price) {
4182 dol_syslog(get_class($this).
"::deleteline rowid=".((
int) $rowid), LOG_DEBUG);
4184 if ($this->statut != self::STATUS_DRAFT) {
4185 $this->error =
'ErrorDeleteLineNotAllowedByObjectStatus';
4192 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'societe_remise_except';
4193 $sql .=
' SET fk_facture_line = NULL';
4194 $sql .=
' WHERE fk_facture_line = '.((int) $rowid);
4196 dol_syslog(get_class($this).
"::deleteline", LOG_DEBUG);
4197 $result = $this->
db->query($sql);
4199 $this->error = $this->
db->error();
4200 $this->
db->rollback();
4206 $line->context = $this->context;
4209 $result = $line->fetch($rowid);
4210 if (!($result > 0)) {
4214 if ($line->delete($user) > 0) {
4218 $this->
db->commit();
4221 $this->
db->rollback();
4222 $this->error = $this->
db->lasterror();
4226 $this->
db->rollback();
4227 $this->error = $line->error;
4246 dol_syslog(get_class($this).
"::set_remise is deprecated, use setDiscount instead", LOG_NOTICE);
4247 return $this->
setDiscount($user, $remise, $notrigger);
4261 if (empty($remise)) {
4265 if ($user->rights->facture->creer) {
4272 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
4273 $sql .=
' SET remise_percent = '.((float) $remise);
4274 $sql .=
" WHERE rowid = ".((int) $this->
id);
4275 $sql .=
' AND fk_statut = '.self::STATUS_DRAFT;
4280 $this->errors[] = $this->
db->error();
4284 if (!$notrigger && empty($error)) {
4294 $this->remise_percent = $remise;
4297 $this->
db->commit();
4300 foreach ($this->errors as $errmsg) {
4301 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
4302 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4304 $this->
db->rollback();
4323 if (empty($remise)) {
4327 if ($user->rights->facture->creer) {
4334 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture';
4335 $sql .=
' SET remise_absolue = '.((float) $remise);
4336 $sql .=
" WHERE rowid = ".((int) $this->
id);
4337 $sql .=
' AND fk_statut = '.self::STATUS_DRAFT;
4342 $this->errors[] = $this->
db->error();
4347 $this->oldcopy = clone $this;
4348 $this->remise_absolue = $remise;
4352 if (!$notrigger && empty($error)) {
4362 $this->
db->commit();
4365 foreach ($this->errors as $errmsg) {
4366 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
4367 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
4369 $this->
db->rollback();
4385 global $conf, $langs;
4387 if ($this->module_source ==
'takepos') {
4388 $langs->load(
'cashdesk');
4390 $moduleName =
'takepos';
4391 $moduleSourceName =
'Takepos';
4392 $addonConstName =
'TAKEPOS_REF_ADDON';
4395 if (empty($conf->global->TAKEPOS_REF_ADDON)) {
4396 $conf->global->TAKEPOS_REF_ADDON =
'mod_takepos_ref_simple';
4399 $addon = $conf->global->TAKEPOS_REF_ADDON;
4401 $langs->load(
'bills');
4403 $moduleName =
'facture';
4404 $moduleSourceName =
'Invoice';
4405 $addonConstName =
'FACTURE_ADDON';
4408 if (empty($conf->global->FACTURE_ADDON)) {
4409 $conf->global->FACTURE_ADDON =
'mod_facture_terre';
4410 } elseif ($conf->global->FACTURE_ADDON ==
'terre') {
4411 $conf->global->FACTURE_ADDON =
'mod_facture_terre';
4412 } elseif ($conf->global->FACTURE_ADDON ==
'mercure') {
4413 $conf->global->FACTURE_ADDON =
'mod_facture_mercure';
4416 $addon = $conf->global->FACTURE_ADDON;
4419 if (!empty($addon)) {
4420 dol_syslog(
"Call getNextNumRef with ".$addonConstName.
" = ".$conf->global->FACTURE_ADDON.
", thirdparty=".$soc->name.
", type=".$soc->typent_code.
", mode=".$mode, LOG_DEBUG);
4424 $file = $addon.
'.php';
4425 $classname = $addon;
4429 $dirmodels = array_merge(array(
'/'), (array) $conf->modules_parts[
'models']);
4430 foreach ($dirmodels as $reldir) {
4431 $dir =
dol_buildpath($reldir.
'core/modules/'.$moduleName.
'/');
4434 if (is_file($dir.$file) && is_readable($dir.$file)) {
4435 $mybool |= include_once $dir.$file;
4441 $file = $addon.
'/'.$addon.
'.modules.php';
4442 $classname =
'mod_'.$moduleName.
'_'.$addon;
4443 $classname = preg_replace(
'/\-.*$/',
'', $classname);
4445 foreach ($conf->file->dol_document_root as $dirroot) {
4446 $dir = $dirroot.
'/core/modules/'.$moduleName.
'/';
4449 if (is_file($dir.$file) && is_readable($dir.$file)) {
4450 $mybool |= include_once $dir.$file;
4460 $obj =
new $classname();
4462 $numref = $obj->getNextValue($soc, $this, $mode);
4469 if ($mode !=
'last' && !$numref) {
4470 $this->error = $obj->error;
4476 $langs->load(
'errors');
4477 print $langs->trans(
'Error').
' '.$langs->trans(
'ErrorModuleSetupNotComplete', $langs->transnoentitiesnoconv($moduleSourceName));
4490 $sql =
'SELECT c.rowid, datec, date_valid as datev, tms as datem,';
4491 $sql .=
' date_closing as dateclosing,';
4492 $sql .=
' fk_user_author, fk_user_valid, fk_user_closing';
4493 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facture as c';
4494 $sql .=
' WHERE c.rowid = '.((int) $id);
4496 $result = $this->
db->query($sql);
4498 if ($this->
db->num_rows($result)) {
4499 $obj = $this->
db->fetch_object($result);
4500 $this->
id = $obj->rowid;
4501 if ($obj->fk_user_author) {
4502 $cuser =
new User($this->
db);
4503 $cuser->fetch($obj->fk_user_author);
4504 $this->user_creation = $cuser;
4506 if ($obj->fk_user_valid) {
4507 $vuser =
new User($this->
db);
4508 $vuser->fetch($obj->fk_user_valid);
4509 $this->user_validation = $vuser;
4511 if ($obj->fk_user_closing) {
4512 $cluser =
new User($this->
db);
4513 $cluser->fetch($obj->fk_user_closing);
4514 $this->user_closing = $cluser;
4517 $this->date_creation = $this->
db->jdate($obj->datec);
4518 $this->date_modification = $this->
db->jdate($obj->datem);
4519 $this->date_validation = $this->
db->jdate($obj->datev);
4520 $this->date_closing = $this->
db->jdate($obj->dateclosing);
4522 $this->
db->free($result);
4543 public function liste_array($shortlist = 0, $draft = 0, $excluser =
'', $socid = 0, $limit = 0, $offset = 0, $sortfield =
'f.datef,f.rowid', $sortorder =
'DESC')
4546 global $conf, $user;
4550 $sql =
"SELECT s.rowid, s.nom as name, s.client,";
4551 $sql .=
" f.rowid as fid, f.ref as ref, f.datef as df";
4552 if (empty($user->rights->societe->client->voir) && !$socid) {
4553 $sql .=
", sc.fk_soc, sc.fk_user";
4555 $sql .=
" FROM ".MAIN_DB_PREFIX.
"societe as s, ".MAIN_DB_PREFIX.
"facture as f";
4556 if (empty($user->rights->societe->client->voir) && !$socid) {
4557 $sql .=
", ".MAIN_DB_PREFIX.
"societe_commerciaux as sc";
4559 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
4560 $sql .=
" AND f.fk_soc = s.rowid";
4561 if (empty($user->rights->societe->client->voir) && !$socid) {
4562 $sql .=
" AND s.rowid = sc.fk_soc AND sc.fk_user = ".((int) $user->id);
4565 $sql .=
" AND s.rowid = ".((int) $socid);
4568 $sql .=
" AND f.fk_statut = ".self::STATUS_DRAFT;
4570 if (is_object($excluser)) {
4571 $sql .=
" AND f.fk_user_author <> ".((int) $excluser->id);
4573 $sql .= $this->
db->order($sortfield, $sortorder);
4574 $sql .= $this->
db->plimit($limit, $offset);
4576 $result = $this->
db->query($sql);
4578 $numc = $this->
db->num_rows($result);
4581 while ($i < $numc) {
4582 $obj = $this->
db->fetch_object($result);
4584 if ($shortlist == 1) {
4585 $ga[$obj->fid] = $obj->ref;
4586 } elseif ($shortlist == 2) {
4587 $ga[$obj->fid] = $obj->ref.
' ('.$obj->name.
')';
4589 $ga[$i][
'id'] = $obj->fid;
4590 $ga[$i][
'ref'] = $obj->ref;
4591 $ga[$i][
'name'] = $obj->name;
4620 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut as status, f.paye as paid,";
4621 $sql .=
" ff.rowid as rowidnext";
4623 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
4624 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON f.rowid = pf.fk_facture";
4625 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as ff ON f.rowid = ff.fk_facture_source";
4626 $sql .=
" WHERE (f.fk_statut = ".self::STATUS_VALIDATED.
" OR (f.fk_statut = ".self::STATUS_ABANDONED.
" AND f.close_code = '".self::CLOSECODE_ABANDONED.
"'))";
4627 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
4628 $sql .=
" AND f.paye = 0";
4629 $sql .=
" AND pf.fk_paiement IS NULL";
4630 $sql .=
" AND ff.fk_statut IS NULL";
4632 $sql .=
" AND f.fk_soc = ".((int) $socid);
4635 $sql .=
" ORDER BY f.ref";
4637 dol_syslog(get_class($this).
"::list_replacable_invoices", LOG_DEBUG);
4640 while ($obj = $this->
db->fetch_object(
$resql)) {
4641 $return[$obj->rowid] = array(
4642 'id' => $obj->rowid,
4644 'status' => $obj->status,
4645 'paid' => $obj->paid,
4652 $this->error = $this->
db->error();
4675 $sql =
"SELECT f.rowid as rowid, f.ref, f.fk_statut, f.type, f.paye, pf.fk_paiement";
4676 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
4677 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"paiement_facture as pf ON f.rowid = pf.fk_facture";
4678 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"facture as ff ON (f.rowid = ff.fk_facture_source AND ff.type=".self::TYPE_REPLACEMENT.
")";
4679 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice').
")";
4680 $sql .=
" AND f.fk_statut in (".self::STATUS_VALIDATED.
",".self::STATUS_CLOSED.
")";
4684 $sql .=
" AND ff.type IS NULL";
4685 $sql .=
" AND f.type <> ".self::TYPE_CREDIT_NOTE;
4687 if (!empty($conf->global->INVOICE_USE_SITUATION_CREDIT_NOTE)) {
4689 $sql .=
" AND (f.type <> ".self::TYPE_SITUATION.
" OR f.rowid IN ";
4690 $sql .=
'(SELECT MAX(fs.rowid)';
4691 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as fs";
4692 $sql .=
" WHERE fs.entity IN (".getEntity(
'invoice').
")";
4693 $sql .=
" AND fs.type = ".self::TYPE_SITUATION;
4694 $sql .=
" AND fs.fk_statut IN (".self::STATUS_VALIDATED.
",".self::STATUS_CLOSED.
")";
4696 $sql .=
" AND fs.fk_soc = ".((int) $socid);
4698 $sql .=
" GROUP BY fs.situation_cycle_ref)";
4701 $sql .=
" AND f.type <> ".self::TYPE_SITUATION;
4705 $sql .=
" AND f.fk_soc = ".((int) $socid);
4707 $sql .=
" ORDER BY f.ref";
4709 dol_syslog(get_class($this).
"::list_qualified_avoir_invoices", LOG_DEBUG);
4712 while ($obj = $this->
db->fetch_object(
$resql)) {
4714 if ($obj->fk_statut == self::STATUS_VALIDATED) {
4717 if ($obj->fk_statut == self::STATUS_CLOSED) {
4722 $paymentornot = ($obj->fk_paiement ? 1 : 0);
4723 $return[$obj->rowid] = array(
'ref'=>$obj->ref,
'status'=>$obj->fk_statut,
'type'=>$obj->type,
'paye'=>$obj->paye,
'paymentornot'=>$paymentornot);
4729 $this->error = $this->
db->error();
4745 global $conf, $langs;
4749 $sql =
"SELECT f.rowid, f.date_lim_reglement as datefin, f.fk_statut, f.total_ht";
4750 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
4751 if (empty($user->rights->societe->client->voir) && !$user->socid) {
4752 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON f.fk_soc = sc.fk_soc";
4753 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
4756 $sql .= $clause.
" f.paye=0";
4757 $sql .=
" AND f.entity IN (".getEntity(
'invoice').
")";
4758 $sql .=
" AND f.fk_statut = ".self::STATUS_VALIDATED;
4760 $sql .=
" AND f.fk_soc = ".((int) $user->socid);
4765 $langs->load(
"bills");
4769 $response->warning_delay = $conf->facture->client->warning_delay / 60 / 60 / 24;
4770 $response->label = $langs->trans(
"CustomerBillsUnpaid");
4771 $response->labelShort = $langs->trans(
"Unpaid");
4772 $response->url = DOL_URL_ROOT.
'/compta/facture/list.php?search_status=1&mainmenu=billing&leftmenu=customers_bills';
4775 $generic_facture =
new Facture($this->
db);
4777 while ($obj = $this->
db->fetch_object(
$resql)) {
4778 $generic_facture->date_lim_reglement = $this->
db->jdate($obj->datefin);
4779 $generic_facture->statut = $obj->fk_statut;
4781 $response->nbtodo++;
4782 $response->total += $obj->total_ht;
4784 if ($generic_facture->hasDelay()) {
4785 $response->nbtodolate++;
4786 $response->url_late = DOL_URL_ROOT.
'/compta/facture/list.php?search_option=late&mainmenu=billing&leftmenu=customers_bills';
4794 $this->error = $this->
db->error();
4833 global $conf, $langs, $user;
4837 $nownotime =
dol_mktime(0, 0, 0, $arraynow[
'mon'], $arraynow[
'mday'], $arraynow[
'year']);
4842 $sql =
"SELECT rowid";
4843 $sql .=
" FROM ".MAIN_DB_PREFIX.
"product";
4844 $sql .=
" WHERE entity IN (".getEntity(
'product').
")";
4845 $sql .= $this->
db->plimit(100);
4849 $num_prods = $this->
db->num_rows(
$resql);
4851 while ($i < $num_prods) {
4853 $row = $this->
db->fetch_row(
$resql);
4854 $prodids[$i] = $row[0];
4858 if (empty($num_prods)) {
4865 $this->
ref =
'SPECIMEN';
4866 $this->specimen = 1;
4868 $this->date = $nownotime;
4869 $this->date_lim_reglement = $nownotime + 3600 * 24 * 30;
4870 $this->cond_reglement_id = 1;
4871 $this->cond_reglement_code =
'RECEP';
4873 $this->mode_reglement_id = 0;
4874 $this->mode_reglement_code =
'';
4876 $this->note_public =
'This is a comment (public)';
4877 $this->note_private =
'This is a comment (private)';
4878 $this->note =
'This is a comment (private)';
4880 $this->fk_user_author = $user->id;
4882 $this->multicurrency_tx = 1;
4883 $this->multicurrency_code = $conf->currency;
4885 $this->fk_incoterms = 0;
4886 $this->location_incoterms =
'';
4888 if (empty($option) || $option !=
'nolines') {
4892 while ($xnbp < $nbp) {
4894 $line->desc = $langs->trans(
"Description").
" ".$xnbp;
4896 $line->subprice = 100;
4897 $line->tva_tx = 19.6;
4898 $line->localtax1_tx = 0;
4899 $line->localtax2_tx = 0;
4900 $line->remise_percent = 0;
4902 $prodid = mt_rand(1, $num_prods);
4903 $line->fk_product = $prodids[$prodid];
4905 $line->total_ht = -100;
4906 $line->total_ttc = -119.6;
4907 $line->total_tva = -19.6;
4908 $line->multicurrency_total_ht = -200;
4909 $line->multicurrency_total_ttc = -239.2;
4910 $line->multicurrency_total_tva = -39.2;
4911 } elseif ($xnbp == 2) {
4912 $line->subprice = -100;
4913 $line->total_ht = -100;
4914 $line->total_ttc = -119.6;
4915 $line->total_tva = -19.6;
4916 $line->remise_percent = 0;
4917 $line->multicurrency_total_ht = -200;
4918 $line->multicurrency_total_ttc = -239.2;
4919 $line->multicurrency_total_tva = -39.2;
4920 } elseif ($xnbp == 3) {
4921 $prodid = mt_rand(1, $num_prods);
4922 $line->fk_product = $prodids[$prodid];
4923 $line->total_ht = 50;
4924 $line->total_ttc = 59.8;
4925 $line->total_tva = 9.8;
4926 $line->multicurrency_total_ht = 100;
4927 $line->multicurrency_total_ttc = 119.6;
4928 $line->multicurrency_total_tva = 19.6;
4929 $line->remise_percent = 50;
4932 $prodid = mt_rand(1, $num_prods);
4933 $line->fk_product = $prodids[$prodid];
4934 $line->total_ht = 100;
4935 $line->total_ttc = 119.6;
4936 $line->total_tva = 19.6;
4937 $line->multicurrency_total_ht = 200;
4938 $line->multicurrency_total_ttc = 239.2;
4939 $line->multicurrency_total_tva = 39.2;
4940 $line->remise_percent = 0;
4943 $this->lines[$xnbp] = $line;
4946 $this->total_ht += $line->total_ht;
4947 $this->total_tva += $line->total_tva;
4948 $this->total_ttc += $line->total_ttc;
4950 $this->multicurrency_total_ht += $line->multicurrency_total_ht;
4951 $this->multicurrency_total_tva += $line->multicurrency_total_tva;
4952 $this->multicurrency_total_ttc += $line->multicurrency_total_ttc;
4956 $this->revenuestamp = 0;
4960 $line->desc = $langs->trans(
"Description").
" (offered line)";
4962 $line->subprice = 100;
4963 $line->tva_tx = 19.6;
4964 $line->localtax1_tx = 0;
4965 $line->localtax2_tx = 0;
4966 $line->remise_percent = 100;
4967 $line->total_ht = 0;
4968 $line->total_ttc = 0;
4969 $line->total_tva = 0;
4970 $line->multicurrency_total_ht = 0;
4971 $line->multicurrency_total_ttc = 0;
4972 $line->multicurrency_total_tva = 0;
4973 $prodid = mt_rand(1, $num_prods);
4974 $line->fk_product = $prodids[$prodid];
4976 $this->lines[$xnbp] = $line;
4990 global $conf, $user;
4992 $this->nb = array();
4996 $sql =
"SELECT count(f.rowid) as nb";
4997 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture as f";
4998 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe as s ON f.fk_soc = s.rowid";
4999 if (empty($user->rights->societe->client->voir) && !$user->socid) {
5000 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"societe_commerciaux as sc ON s.rowid = sc.fk_soc";
5001 $sql .=
" WHERE sc.fk_user = ".((int) $user->id);
5004 $sql .=
" ".$clause.
" f.entity IN (".
getEntity(
'invoice').
")";
5008 while ($obj = $this->
db->fetch_object(
$resql)) {
5009 $this->nb[
"invoices"] = $obj->nb;
5015 $this->error = $this->
db->error();
5041 public function generateDocument($modele, $outputlangs, $hidedetails = 0, $hidedesc = 0, $hideref = 0, $moreparams =
null)
5043 global $conf, $langs;
5045 $outputlangs->loadLangs(array(
"bills",
"products"));
5049 $thisTypeConfName =
'FACTURE_ADDON_PDF_'.$this->type;
5051 if (!empty($this->model_pdf)) {
5052 $modele = $this->model_pdf;
5053 } elseif (!empty($this->modelpdf)) {
5054 $modele = $this->modelpdf;
5055 } elseif (!empty($conf->global->$thisTypeConfName)) {
5056 $modele = $conf->global->$thisTypeConfName;
5057 } elseif (!empty($conf->global->FACTURE_ADDON_PDF)) {
5058 $modele = $conf->global->FACTURE_ADDON_PDF;
5062 $modelpath =
"core/modules/facture/doc/";
5064 return $this->
commonGenerateDocument($modelpath, $modele, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
5074 $sql =
'SELECT max(situation_cycle_ref) FROM '.MAIN_DB_PREFIX.
'facture as f';
5075 $sql .=
" WHERE f.entity IN (".getEntity(
'invoice', 0).
")";
5078 if ($this->
db->num_rows(
$resql) > 0) {
5079 $res = $this->
db->fetch_array(
$resql);
5080 $ref = $res[
'max(situation_cycle_ref)'];
5088 $this->error = $this->
db->lasterror();
5089 dol_syslog(
"Error sql=".$sql.
", error=".$this->error, LOG_ERR);
5103 return ($this->situation_counter == 1);
5117 $sql =
'SELECT rowid FROM '.MAIN_DB_PREFIX.
'facture';
5118 $sql .=
' WHERE situation_cycle_ref = '.((int) $this->situation_cycle_ref);
5119 $sql .=
' AND situation_counter < '.((int) $this->situation_counter);
5120 $sql .=
' AND entity = '.($this->entity > 0 ? $this->entity : $conf->entity);
5124 while ($row = $this->
db->fetch_object(
$resql)) {
5127 $situation->fetch($id);
5128 $res[] = $situation;
5131 $this->error = $this->
db->error();
5132 dol_syslog(
"Error sql=".$sql.
", error=".$this->error, LOG_ERR);
5152 $sql =
'UPDATE '.MAIN_DB_PREFIX.
'facture SET situation_final = '.((int) $this->situation_final).
' WHERE rowid = '.((int) $this->
id);
5157 $this->errors[] = $this->
db->error();
5161 if (!$notrigger && empty($error)) {
5171 $this->
db->commit();
5174 foreach ($this->errors as $errmsg) {
5175 dol_syslog(__METHOD__.
' Error: '.$errmsg, LOG_ERR);
5176 $this->error .= ($this->error ?
', '.$errmsg : $errmsg);
5178 $this->
db->rollback();
5194 if (!empty($this->situation_cycle_ref)) {
5196 $sql =
'SELECT max(situation_counter) FROM '.MAIN_DB_PREFIX.
'facture';
5197 $sql .=
' WHERE situation_cycle_ref = '.((int) $this->situation_cycle_ref);
5198 $sql .=
' AND entity = '.($this->entity > 0 ? $this->entity : $conf->entity);
5202 $res = $this->
db->fetch_array(
$resql);
5203 $last = $res[
'max(situation_counter)'];
5204 return ($last == $this->situation_counter);
5206 $this->error = $this->
db->lasterror();
5207 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
5265 $hasDelay = $this->date_lim_reglement < ($now - $conf->facture->client->warning_delay);
5266 if ($hasDelay && !empty($this->retained_warranty) && !empty($this->retained_warranty_date_limit)) {
5268 $totalpaid = floatval($totalpaid);
5270 if ($totalpaid >= 0 && $RetainedWarrantyAmount >= 0) {
5271 if (($totalpaid < $this->total_ttc - $RetainedWarrantyAmount) && $this->date_lim_reglement < ($now - $conf->facture->client->warning_delay)) {
5273 } elseif ($totalpaid < $this->total_ttc && $this->retained_warranty_date_limit < ($now - $conf->facture->client->warning_delay)) {
5296 $displayWarranty =
false;
5297 if (!empty($this->retained_warranty)) {
5298 $displayWarranty =
true;
5302 $displayWarranty =
false;
5303 if (!empty($this->situation_final)) {
5304 $displayWarranty =
true;
5307 $displayWarranty =
true;
5309 foreach ($this->lines as $i => $line) {
5310 if ($line->product_type < 2 && $line->situation_percent < 100) {
5311 $displayWarranty =
false;
5319 return $displayWarranty;
5329 if (empty($this->retained_warranty)) {
5333 $retainedWarrantyAmount = 0;
5337 $displayWarranty =
true;
5339 if (!empty($this->lines)) {
5340 foreach ($this->lines as $i => $line) {
5341 if ($line->product_type < 2 && $line->situation_percent < 100) {
5342 $displayWarranty =
false;
5348 if ($displayWarranty && !empty($this->situation_final)) {
5350 $TPreviousIncoice = $this->tab_previous_situation_invoice;
5353 foreach ($TPreviousIncoice as &$fac) {
5354 $total2BillWT += $fac->total_ttc;
5356 $total2BillWT += $this->total_ttc;
5358 $retainedWarrantyAmount = $total2BillWT * $this->retained_warranty / 100;
5364 $retainedWarrantyAmount = $this->total_ttc * $this->retained_warranty / 100;
5367 if ($rounding < 0) {
5368 $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT, $conf->global->MAIN_MAX_DECIMALS_TOT);
5371 if ($rounding > 0) {
5372 return round($retainedWarrantyAmount, $rounding);
5375 return $retainedWarrantyAmount;
5386 dol_syslog(get_class($this).
'::setRetainedWarranty('.$value.
')');
5388 if ($this->statut >= 0) {
5389 $fieldname =
'retained_warranty';
5390 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
5391 $sql .=
" SET ".$fieldname.
" = ".((
float) $value);
5392 $sql .=
' WHERE rowid='.((int) $this->
id);
5394 if ($this->
db->query($sql)) {
5395 $this->retained_warranty = floatval($value);
5398 dol_syslog(get_class($this).
'::setRetainedWarranty Erreur '.$sql.
' - '.$this->db->error());
5399 $this->error = $this->
db->error();
5403 dol_syslog(get_class($this).
'::setRetainedWarranty, status of the object is incompatible');
5404 $this->error =
'Status of the object is incompatible '.$this->statut;
5419 if (!$timestamp && $dateYmd) {
5420 $timestamp = $this->
db->jdate($dateYmd);
5424 dol_syslog(get_class($this).
'::setRetainedWarrantyDateLimit('.$timestamp.
')');
5425 if ($this->statut >= 0) {
5426 $fieldname =
'retained_warranty_date_limit';
5427 $sql =
'UPDATE '.MAIN_DB_PREFIX.$this->table_element;
5428 $sql .=
" SET ".$fieldname.
" = ".(strval($timestamp) !=
'' ?
"'".$this->db->idate($timestamp).
"'" :
'null');
5429 $sql .=
' WHERE rowid = '.((int) $this->
id);
5431 if ($this->
db->query($sql)) {
5432 $this->retained_warranty_date_limit = $timestamp;
5435 dol_syslog(get_class($this).
'::setRetainedWarrantyDateLimit Erreur '.$sql.
' - '.$this->db->error());
5436 $this->error = $this->
db->error();
5440 dol_syslog(get_class($this).
'::setRetainedWarrantyDateLimit, status of the object is incompatible');
5441 $this->error =
'Status of the object is incompatible '.$this->statut;
5458 global $conf, $langs, $user;
5464 $errorsMsg = array();
5466 $langs->load(
"bills");
5469 $this->output .= $langs->trans(
'ModuleNotEnabled', $langs->transnoentitiesnoconv(
"Facture"));
5479 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
5480 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formmail.class.php';
5481 require_once DOL_DOCUMENT_ROOT.
'/core/class/CMailFile.class.php';
5492 $sql =
"SELECT rowid as id FROM ".MAIN_DB_PREFIX.
"facture as f";
5493 if (!empty($paymentmode) && $paymentmode !=
'all') {
5494 $sql .=
", ".MAIN_DB_PREFIX.
"c_paiement as cp";
5496 $sql .=
" WHERE f.paye = 0";
5497 $sql .=
" AND f.fk_statut = ".self::STATUS_VALIDATED;
5498 $sql .=
" AND f.date_lim_reglement = '".$this->db->idate($tmpidate,
'gmt').
"'";
5499 $sql .=
" AND f.entity IN (".getEntity(
'facture').
")";
5500 if (!empty($paymentmode) && $paymentmode !=
'all') {
5501 $sql .=
" AND f.fk_mode_reglement = cp.id AND cp.code = '".$this->db->escape($paymentmode).
"'";
5504 $sql .= $this->
db->order(
"date_lim_reglement",
"ASC");
5509 $this->output .= $langs->transnoentitiesnoconv(
"SearchUnpaidInvoicesWithDueDate", $stmpidate);
5510 if (!empty($paymentmode) && $paymentmode !=
'all') {
5511 $this->output .=
' ('.$langs->transnoentitiesnoconv(
"PaymentMode").
' '.$paymentmode.
')';
5513 $this->output .=
'<br>';
5516 while ($obj = $this->
db->fetch_object(
$resql)) {
5519 $res = $tmpinvoice->fetch($obj->id);
5521 $tmpinvoice->fetch_thirdparty();
5523 $outputlangs =
new Translate(
'', $conf);
5524 if ($tmpinvoice->thirdparty->default_lang) {
5525 $outputlangs->setDefaultLang($tmpinvoice->thirdparty->default_lang);
5526 $outputlangs->loadLangs(array(
"main",
"bills"));
5528 $outputlangs = $langs;
5532 $arraymessage = $formmail->getEMailTemplate($this->
db,
'facture_send', $user, $outputlangs, (is_numeric($template) ? $template : 0), 1, (is_numeric($template) ?
'' : $template));
5533 if (is_numeric($arraymessage) && $arraymessage <= 0) {
5534 $langs->load(
"errors");
5535 $this->output .= $langs->trans(
'ErrorFailedToFindEmailTemplate', $template);
5548 $sendTopic =
make_substitutions(empty($arraymessage->topic) ? $outputlangs->transnoentitiesnoconv(
'InformationMessage') : $arraymessage->topic, $substitutionarray, $outputlangs, 1);
5551 $content = $outputlangs->transnoentitiesnoconv($arraymessage->content);
5557 $res = $tmpinvoice->fetch_thirdparty();
5558 $recipient = $tmpinvoice->thirdparty;
5560 if (!empty($recipient->email)) {
5561 $to = $recipient->email;
5563 $errormesg =
"Failed to send remind to thirdparty id=".$tmpinvoice->socid.
". No email defined for user.";
5567 $errormesg =
"Failed to load recipient with thirdparty id=".$tmpinvoice->socid;
5572 $from = $conf->global->MAIN_MAIL_EMAIL_FROM;
5574 $errormesg =
"Failed to get sender into global setup MAIN_MAIL_EMAIL_FROM";
5578 if (!$error && $to) {
5582 $errors_to = $conf->global->MAIN_MAIL_ERRORS_TO;
5584 $trackid =
'inv'.$tmpinvoice->id;
5585 $sendcontext =
'standard';
5588 $cMailFile =
new CMailFile($sendTopic, $to, $from, $sendContent, array(), array(), array(),
'',
"", 0, 1, $errors_to,
'', $trackid,
'', $sendcontext,
'');
5591 if ($cMailFile->sendfile()) {
5595 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
5600 $actioncomm->type_code =
'AC_OTH_AUTO';
5601 $actioncomm->socid = $tmpinvoice->thirdparty->id;
5602 $actioncomm->contact_id = 0;
5604 $actioncomm->code =
'AC_EMAIL';
5605 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateOK';
5606 $actioncomm->note_private = $sendContent;
5607 $actioncomm->fk_project = $tmpinvoice->fk_project;
5608 $actioncomm->datep =
dol_now();
5609 $actioncomm->datef = $actioncomm->datep;
5610 $actioncomm->percentage = -1;
5611 $actioncomm->authorid = $user->id;
5612 $actioncomm->userownerid = $user->id;
5614 $actioncomm->email_msgid = $cMailFile->msgid;
5615 $actioncomm->email_from = $from;
5616 $actioncomm->email_sender =
'';
5617 $actioncomm->email_to = $to;
5621 $actioncomm->errors_to = $errors_to;
5625 $actioncomm->create($user);
5627 $errormesg = $cMailFile->error.
' : '.$to;
5631 require_once DOL_DOCUMENT_ROOT.
'/comm/action/class/actioncomm.class.php';
5636 $actioncomm->type_code =
'AC_OTH_AUTO';
5637 $actioncomm->socid = $tmpinvoice->thirdparty->id;
5638 $actioncomm->contact_id = 0;
5640 $actioncomm->code =
'AC_EMAIL';
5641 $actioncomm->label =
'sendEmailsRemindersOnInvoiceDueDateKO';
5642 $actioncomm->note_private = $errormesg;
5643 $actioncomm->fk_project = $tmpinvoice->fk_project;
5644 $actioncomm->datep =
dol_now();
5645 $actioncomm->datef = $actioncomm->datep;
5646 $actioncomm->percentage = -1;
5647 $actioncomm->authorid = $user->id;
5648 $actioncomm->userownerid = $user->id;
5650 $actioncomm->email_msgid = $cMailFile->msgid;
5651 $actioncomm->email_from = $from;
5652 $actioncomm->email_sender =
'';
5653 $actioncomm->email_to = $to;
5657 $actioncomm->errors_to = $errors_to;
5661 $actioncomm->create($user);
5664 $this->
db->commit();
5668 $errorsMsg[] = $errormesg;
5671 $errorsMsg[] =
'Failed to fetch record invoice with ID = '.$obj->id;
5681 $this->output .=
'Nb of emails sent : '.$nbMailSend;
5684 $this->error =
'Nb of emails sent : '.$nbMailSend.
', '.(!empty($errorsMsg)) ? join(
', ', $errorsMsg) : $error;
5698 $sql =
"SELECT datef";
5699 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture";
5700 $sql .=
" WHERE type = " . (int) $this->
type ;
5701 $sql .=
" AND date_valid IS NOT NULL";
5702 $sql .=
" ORDER BY datef DESC LIMIT 1";
5704 $result = $this->
db->query($sql);
5707 if ($this->
db->num_rows($result)) {
5708 $obj = $this->
db->fetch_object($result);
5709 $last_date = $this->
db->jdate($obj->datef);
5710 $invoice_date = $this->date;
5712 $is_last_of_same_type = $invoice_date >= $last_date;
5713 if ($allow_validated_drafts) {
5714 $is_last_of_same_type = $is_last_of_same_type || (!strpos($this->
ref,
'PROV') && $this->status ==
self::STATUS_DRAFT);
5717 return array($is_last_of_same_type, $last_date);
5737 public $element =
'facturedet';
5742 public $table_element =
'facturedet';
5756 public $localtax1_type;
5757 public $localtax2_type;
5758 public $fk_remise_except;
5761 public $fk_fournprice;
5766 public $remise_percent;
5768 public $special_code;
5776 public $fk_code_ventilation = 0;
5781 public $skip_update_total;
5786 public $situation_percent;
5794 public $fk_multicurrency;
5795 public $multicurrency_code;
5796 public $multicurrency_subprice;
5797 public $multicurrency_total_ht;
5798 public $multicurrency_total_tva;
5799 public $multicurrency_total_ttc;
5809 $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,';
5810 $sql .=
' fd.localtax1_tx, fd. localtax2_tx, fd.remise, fd.remise_percent, fd.fk_remise_except, fd.subprice, fd.ref_ext,';
5811 $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,';
5812 $sql .=
' fd.info_bits, fd.special_code, fd.total_ht, fd.total_tva, fd.total_ttc, fd.total_localtax1, fd.total_localtax2, fd.rang,';
5813 $sql .=
' fd.fk_code_ventilation,';
5814 $sql .=
' fd.fk_unit, fd.fk_user_author, fd.fk_user_modif,';
5815 $sql .=
' fd.situation_percent, fd.fk_prev_id,';
5816 $sql .=
' fd.multicurrency_subprice,';
5817 $sql .=
' fd.multicurrency_total_ht,';
5818 $sql .=
' fd.multicurrency_total_tva,';
5819 $sql .=
' fd.multicurrency_total_ttc,';
5820 $sql .=
' p.ref as product_ref, p.label as product_label, p.description as product_desc';
5821 $sql .=
' FROM '.MAIN_DB_PREFIX.
'facturedet as fd';
5822 $sql .=
' LEFT JOIN '.MAIN_DB_PREFIX.
'product as p ON fd.fk_product = p.rowid';
5823 $sql .=
' WHERE fd.rowid = '.((int) $rowid);
5825 $result = $this->
db->query($sql);
5827 $objp = $this->
db->fetch_object($result);
5829 $this->
rowid = $objp->rowid;
5830 $this->
id = $objp->rowid;
5831 $this->fk_facture = $objp->fk_facture;
5832 $this->fk_parent_line = $objp->fk_parent_line;
5833 $this->label = $objp->custom_label;
5834 $this->desc = $objp->description;
5835 $this->qty = $objp->qty;
5836 $this->subprice = $objp->subprice;
5837 $this->ref_ext = $objp->ref_ext;
5838 $this->vat_src_code = $objp->vat_src_code;
5839 $this->tva_tx = $objp->tva_tx;
5840 $this->localtax1_tx = $objp->localtax1_tx;
5841 $this->localtax2_tx = $objp->localtax2_tx;
5842 $this->remise_percent = $objp->remise_percent;
5843 $this->fk_remise_except = $objp->fk_remise_except;
5844 $this->fk_product = $objp->fk_product;
5845 $this->product_type = $objp->product_type;
5846 $this->date_start = $this->
db->jdate($objp->date_start);
5847 $this->date_end = $this->
db->jdate($objp->date_end);
5848 $this->info_bits = $objp->info_bits;
5849 $this->tva_npr = ($objp->info_bits & 1 == 1) ? 1 : 0;
5850 $this->special_code = $objp->special_code;
5851 $this->total_ht = $objp->total_ht;
5852 $this->total_tva = $objp->total_tva;
5853 $this->total_localtax1 = $objp->total_localtax1;
5854 $this->total_localtax2 = $objp->total_localtax2;
5855 $this->total_ttc = $objp->total_ttc;
5856 $this->fk_code_ventilation = $objp->fk_code_ventilation;
5857 $this->rang = $objp->rang;
5858 $this->fk_fournprice = $objp->fk_fournprice;
5859 $marginInfos =
getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $this->fk_fournprice, $objp->pa_ht);
5860 $this->pa_ht = $marginInfos[0];
5861 $this->marge_tx = $marginInfos[1];
5862 $this->marque_tx = $marginInfos[2];
5864 $this->
ref = $objp->product_ref;
5866 $this->product_ref = $objp->product_ref;
5867 $this->product_label = $objp->product_label;
5868 $this->product_desc = $objp->product_desc;
5870 $this->fk_unit = $objp->fk_unit;
5871 $this->fk_user_modif = $objp->fk_user_modif;
5872 $this->fk_user_author = $objp->fk_user_author;
5874 $this->situation_percent = $objp->situation_percent;
5875 $this->fk_prev_id = $objp->fk_prev_id;
5877 $this->multicurrency_subprice = $objp->multicurrency_subprice;
5878 $this->multicurrency_total_ht = $objp->multicurrency_total_ht;
5879 $this->multicurrency_total_tva = $objp->multicurrency_total_tva;
5880 $this->multicurrency_total_ttc = $objp->multicurrency_total_ttc;
5882 $this->
db->free($result);
5886 $this->error = $this->
db->lasterror();
5898 public function insert($notrigger = 0, $noerrorifdiscountalreadylinked = 0)
5900 global $langs, $user, $conf;
5904 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
5906 dol_syslog(get_class($this).
"::insert rang=".$this->rang, LOG_DEBUG);
5909 $this->desc = trim($this->desc);
5910 if (empty($this->tva_tx)) {
5913 if (empty($this->localtax1_tx)) {
5914 $this->localtax1_tx = 0;
5916 if (empty($this->localtax2_tx)) {
5917 $this->localtax2_tx = 0;
5919 if (empty($this->localtax1_type)) {
5920 $this->localtax1_type = 0;
5922 if (empty($this->localtax2_type)) {
5923 $this->localtax2_type = 0;
5925 if (empty($this->total_localtax1)) {
5926 $this->total_localtax1 = 0;
5928 if (empty($this->total_localtax2)) {
5929 $this->total_localtax2 = 0;
5931 if (empty($this->rang)) {
5934 if (empty($this->remise_percent)) {
5935 $this->remise_percent = 0;
5937 if (empty($this->info_bits)) {
5938 $this->info_bits = 0;
5940 if (empty($this->subprice)) {
5941 $this->subprice = 0;
5943 if (empty($this->ref_ext)) {
5944 $this->ref_ext =
'';
5946 if (empty($this->special_code)) {
5947 $this->special_code = 0;
5949 if (empty($this->fk_parent_line)) {
5950 $this->fk_parent_line = 0;
5952 if (empty($this->fk_prev_id)) {
5953 $this->fk_prev_id = 0;
5955 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
5956 $this->situation_percent = 100;
5959 if (empty($this->pa_ht)) {
5962 if (empty($this->multicurrency_subprice)) {
5963 $this->multicurrency_subprice = 0;
5965 if (empty($this->multicurrency_total_ht)) {
5966 $this->multicurrency_total_ht = 0;
5968 if (empty($this->multicurrency_total_tva)) {
5969 $this->multicurrency_total_tva = 0;
5971 if (empty($this->multicurrency_total_ttc)) {
5972 $this->multicurrency_total_ttc = 0;
5976 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
5977 if (($result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product)) < 0) {
5980 $this->pa_ht = $result;
5985 if ($this->product_type < 0) {
5986 $this->error =
'ErrorProductTypeMustBe0orMore';
5989 if (!empty($this->fk_product) && $this->fk_product > 0) {
5993 $this->error =
'ErrorProductIdDoesNotExists';
5994 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6002 $sql =
'INSERT INTO '.MAIN_DB_PREFIX.
'facturedet';
6003 $sql .=
' (fk_facture, fk_parent_line, label, description, qty,';
6004 $sql .=
' vat_src_code, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
6005 $sql .=
' fk_product, product_type, remise_percent, subprice, ref_ext, fk_remise_except,';
6006 $sql .=
' date_start, date_end, fk_code_ventilation, ';
6007 $sql .=
' rang, special_code, fk_product_fournisseur_price, buy_price_ht,';
6008 $sql .=
' info_bits, total_ht, total_tva, total_ttc, total_localtax1, total_localtax2,';
6009 $sql .=
' situation_percent, fk_prev_id,';
6010 $sql .=
' fk_unit, fk_user_author, fk_user_modif,';
6011 $sql .=
' fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
6013 $sql .=
" VALUES (".$this->fk_facture.
",";
6014 $sql .=
" ".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null").
",";
6015 $sql .=
" ".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null").
",";
6016 $sql .=
" '".$this->db->escape($this->desc).
"',";
6017 $sql .=
" ".price2num($this->qty).
",";
6018 $sql .=
" ".(empty($this->vat_src_code) ?
"''" :
"'".$this->db->escape($this->vat_src_code).
"'").
",";
6019 $sql .=
" ".price2num($this->tva_tx).
",";
6020 $sql .=
" ".price2num($this->localtax1_tx).
",";
6021 $sql .=
" ".price2num($this->localtax2_tx).
",";
6022 $sql .=
" '".$this->db->escape($this->localtax1_type).
"',";
6023 $sql .=
" '".$this->db->escape($this->localtax2_type).
"',";
6024 $sql .=
' '.((!empty($this->fk_product) && $this->fk_product > 0) ? $this->fk_product :
"null").
',';
6025 $sql .=
" ".((int) $this->product_type).
",";
6026 $sql .=
" ".price2num($this->remise_percent).
",";
6027 $sql .=
" ".price2num($this->subprice).
",";
6028 $sql .=
" '".$this->db->escape($this->ref_ext).
"',";
6029 $sql .=
' '.(!empty($this->fk_remise_except) ? $this->fk_remise_except :
"null").
',';
6030 $sql .=
" ".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null").
",";
6031 $sql .=
" ".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null").
",";
6032 $sql .=
' '.((int) $this->fk_code_ventilation).
',';
6033 $sql .=
' '.((int) $this->rang).
',';
6034 $sql .=
' '.((int) $this->special_code).
',';
6035 $sql .=
' '.(!empty($this->fk_fournprice) ? $this->fk_fournprice :
"null").
',';
6036 $sql .=
' '.price2num($this->pa_ht).
',';
6037 $sql .=
" '".$this->db->escape($this->info_bits).
"',";
6038 $sql .=
" ".price2num($this->total_ht).
",";
6039 $sql .=
" ".price2num($this->total_tva).
",";
6040 $sql .=
" ".price2num($this->total_ttc).
",";
6041 $sql .=
" ".price2num($this->total_localtax1).
",";
6042 $sql .=
" ".price2num($this->total_localtax2);
6043 $sql .=
", ".((float) $this->situation_percent);
6044 $sql .=
", ".(!empty($this->fk_prev_id) ? $this->fk_prev_id :
"null");
6045 $sql .=
", ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
6046 $sql .=
", ".((int) $user->id);
6047 $sql .=
", ".((int) $user->id);
6048 $sql .=
", ".(int) $this->fk_multicurrency;
6049 $sql .=
", '".$this->db->escape($this->multicurrency_code).
"'";
6050 $sql .=
", ".price2num($this->multicurrency_subprice);
6051 $sql .=
", ".price2num($this->multicurrency_total_ht);
6052 $sql .=
", ".price2num($this->multicurrency_total_tva);
6053 $sql .=
", ".price2num($this->multicurrency_total_ttc);
6056 dol_syslog(get_class($this).
"::insert", LOG_DEBUG);
6059 $this->
id = $this->
db->last_insert_id(MAIN_DB_PREFIX.
'facturedet');
6060 $this->
rowid = $this->id;
6071 if ($this->fk_remise_except) {
6073 $result = $discount->fetch($this->fk_remise_except);
6078 if ($discount->fk_facture_line > 0) {
6079 if (empty($noerrorifdiscountalreadylinked)) {
6080 $this->error = $langs->trans(
"ErrorDiscountAlreadyUsed", $discount->id);
6081 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6082 $this->
db->rollback();
6086 $result = $discount->link_to_invoice($this->
rowid, 0);
6088 $this->error = $discount->error;
6089 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6090 $this->
db->rollback();
6095 $this->error = $langs->trans(
"ErrorADiscountThatHasBeenRemovedIsIncluded");
6096 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6097 $this->
db->rollback();
6101 $this->error = $discount->error;
6102 dol_syslog(get_class($this).
"::insert Error ".$this->error, LOG_ERR);
6103 $this->
db->rollback();
6110 $result = $this->
call_trigger(
'LINEBILL_INSERT', $user);
6112 $this->
db->rollback();
6118 $this->
db->commit();
6121 $this->error = $this->
db->lasterror();
6122 $this->
db->rollback();
6134 public function update($user =
'', $notrigger = 0)
6136 global $user, $conf;
6140 $pa_ht_isemptystring = (empty($this->pa_ht) && $this->pa_ht ==
'');
6143 $this->desc = trim($this->desc);
6144 if (empty($this->ref_ext)) {
6145 $this->ref_ext =
'';
6147 if (empty($this->tva_tx)) {
6150 if (empty($this->localtax1_tx)) {
6151 $this->localtax1_tx = 0;
6153 if (empty($this->localtax2_tx)) {
6154 $this->localtax2_tx = 0;
6156 if (empty($this->localtax1_type)) {
6157 $this->localtax1_type = 0;
6159 if (empty($this->localtax2_type)) {
6160 $this->localtax2_type = 0;
6162 if (empty($this->total_localtax1)) {
6163 $this->total_localtax1 = 0;
6165 if (empty($this->total_localtax2)) {
6166 $this->total_localtax2 = 0;
6168 if (empty($this->remise_percent)) {
6169 $this->remise_percent = 0;
6171 if (empty($this->info_bits)) {
6172 $this->info_bits = 0;
6174 if (empty($this->special_code)) {
6175 $this->special_code = 0;
6177 if (empty($this->product_type)) {
6178 $this->product_type = 0;
6180 if (empty($this->fk_parent_line)) {
6181 $this->fk_parent_line = 0;
6183 if (!isset($this->situation_percent) || $this->situation_percent > 100 || (
string) $this->situation_percent ==
'') {
6184 $this->situation_percent = 100;
6186 if (empty($this->pa_ht)) {
6190 if (empty($this->multicurrency_subprice)) {
6191 $this->multicurrency_subprice = 0;
6193 if (empty($this->multicurrency_total_ht)) {
6194 $this->multicurrency_total_ht = 0;
6196 if (empty($this->multicurrency_total_tva)) {
6197 $this->multicurrency_total_tva = 0;
6199 if (empty($this->multicurrency_total_ttc)) {
6200 $this->multicurrency_total_ttc = 0;
6204 if ($this->product_type < 0) {
6209 if ($this->pa_ht == 0 && $pa_ht_isemptystring) {
6211 $result = $this->
defineBuyPrice($this->subprice, $this->remise_percent, $this->fk_product);
6215 $this->pa_ht = $result;
6222 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
6223 $sql .=
" description='".$this->db->escape($this->desc).
"'";
6224 $sql .=
", ref_ext='".$this->db->escape($this->ref_ext).
"'";
6225 $sql .=
", label=".(!empty($this->label) ?
"'".$this->db->escape($this->label).
"'" :
"null");
6226 $sql .=
", subprice=".price2num($this->subprice);
6227 $sql .=
", remise_percent=".price2num($this->remise_percent);
6228 if ($this->fk_remise_except) {
6229 $sql .=
", fk_remise_except=".$this->fk_remise_except;
6231 $sql .=
", fk_remise_except=null";
6233 $sql .=
", vat_src_code = '".(empty($this->vat_src_code) ?
'' : $this->
db->escape($this->vat_src_code)).
"'";
6234 $sql .=
", tva_tx=".price2num($this->tva_tx);
6235 $sql .=
", localtax1_tx=".price2num($this->localtax1_tx);
6236 $sql .=
", localtax2_tx=".price2num($this->localtax2_tx);
6237 $sql .=
", localtax1_type='".$this->db->escape($this->localtax1_type).
"'";
6238 $sql .=
", localtax2_type='".$this->db->escape($this->localtax2_type).
"'";
6239 $sql .=
", qty=".price2num($this->qty);
6240 $sql .=
", date_start=".(!empty($this->date_start) ?
"'".$this->db->idate($this->date_start).
"'" :
"null");
6241 $sql .=
", date_end=".(!empty($this->date_end) ?
"'".$this->db->idate($this->date_end).
"'" :
"null");
6242 $sql .=
", product_type=".$this->product_type;
6243 $sql .=
", info_bits='".$this->db->escape($this->info_bits).
"'";
6244 $sql .=
", special_code='".$this->db->escape($this->special_code).
"'";
6245 if (empty($this->skip_update_total)) {
6246 $sql .=
", total_ht=".price2num($this->total_ht);
6247 $sql .=
", total_tva=".price2num($this->total_tva);
6248 $sql .=
", total_ttc=".price2num($this->total_ttc);
6249 $sql .=
", total_localtax1=".price2num($this->total_localtax1);
6250 $sql .=
", total_localtax2=".price2num($this->total_localtax2);
6252 $sql .=
", fk_product_fournisseur_price=".(!empty($this->fk_fournprice) ?
"'".$this->db->escape($this->fk_fournprice).
"'" :
"null");
6253 $sql .=
", buy_price_ht=".(($this->pa_ht || $this->pa_ht === 0 || $this->pa_ht ===
'0') ?
price2num($this->pa_ht) :
"null");
6254 $sql .=
", fk_parent_line=".($this->fk_parent_line > 0 ? $this->fk_parent_line :
"null");
6255 if (!empty($this->rang)) {
6256 $sql .=
", rang=".((int) $this->rang);
6258 $sql .=
", situation_percent = ".((float) $this->situation_percent);
6259 $sql .=
", fk_unit = ".(!$this->fk_unit ?
'NULL' : $this->fk_unit);
6260 $sql .=
", fk_user_modif = ".((int) $user->id);
6263 $sql .=
", multicurrency_subprice=".price2num($this->multicurrency_subprice);
6264 $sql .=
", multicurrency_total_ht=".price2num($this->multicurrency_total_ht);
6265 $sql .=
", multicurrency_total_tva=".price2num($this->multicurrency_total_tva);
6266 $sql .=
", multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc);
6268 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
6270 dol_syslog(get_class($this).
"::update", LOG_DEBUG);
6274 $this->
id = $this->rowid;
6281 if (!$error && !$notrigger) {
6283 $result = $this->
call_trigger(
'LINEBILL_MODIFY', $user);
6285 $this->
db->rollback();
6290 $this->
db->commit();
6293 $this->error = $this->
db->error();
6294 $this->
db->rollback();
6306 public function delete($tmpuser =
null, $notrigger =
false)
6313 if (empty($notrigger)) {
6314 $result = $this->
call_trigger(
'LINEBILL_DELETE', $user);
6316 $this->
db->rollback();
6325 $this->
db->rollback();
6329 $sql =
"DELETE FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $this->
rowid);
6331 if ($this->
db->query($sql)) {
6332 $this->
db->commit();
6335 $this->error = $this->
db->error().
" sql=".$sql;
6336 $this->
db->rollback();
6352 dol_syslog(get_class($this).
"::update_total", LOG_DEBUG);
6355 if (empty($this->total_localtax1)) {
6356 $this->total_localtax1 = 0;
6358 if (empty($this->total_localtax2)) {
6359 $this->total_localtax2 = 0;
6363 $sql =
"UPDATE ".MAIN_DB_PREFIX.
"facturedet SET";
6364 $sql .=
" total_ht=".price2num($this->total_ht).
"";
6365 $sql .=
",total_tva=".price2num($this->total_tva).
"";
6366 $sql .=
",total_localtax1=".price2num($this->total_localtax1).
"";
6367 $sql .=
",total_localtax2=".price2num($this->total_localtax2).
"";
6368 $sql .=
",total_ttc=".price2num($this->total_ttc).
"";
6369 $sql .=
" WHERE rowid = ".((int) $this->
rowid);
6371 dol_syslog(get_class($this).
"::update_total", LOG_DEBUG);
6375 $this->
db->commit();
6378 $this->error = $this->
db->error();
6379 $this->
db->rollback();
6396 global $invoicecache;
6397 if (is_null($this->fk_prev_id) || empty($this->fk_prev_id) || $this->fk_prev_id ==
"") {
6401 if (!isset($invoicecache[$invoiceid])) {
6402 $invoicecache[$invoiceid] =
new Facture($this->
db);
6403 $invoicecache[$invoiceid]->fetch($invoiceid);
6409 $sql =
"SELECT situation_percent FROM ".MAIN_DB_PREFIX.
"facturedet WHERE rowid = ".((int) $this->fk_prev_id);
6412 $res = $this->
db->fetch_array(
$resql);
6414 $returnPercent = floatval($res[
'situation_percent']);
6416 if ($include_credit_note) {
6417 $sql =
'SELECT fd.situation_percent FROM '.MAIN_DB_PREFIX.
'facturedet fd';
6418 $sql .=
' JOIN '.MAIN_DB_PREFIX.
'facture f ON (f.rowid = fd.fk_facture) ';
6419 $sql .=
" WHERE fd.fk_prev_id = ".((int) $this->fk_prev_id);
6420 $sql .=
" AND f.situation_cycle_ref = ".((int) $invoicecache[$invoiceid]->situation_cycle_ref);
6421 $sql .=
" AND f.type = ".Facture::TYPE_CREDIT_NOTE;
6423 $res = $this->
db->query($sql);
6425 while ($obj = $this->
db->fetch_object($res)) {
6426 $returnPercent = $returnPercent + floatval($obj->situation_percent);
6433 return $returnPercent;
6435 $this->error = $this->
db->error();
6436 dol_syslog(get_class($this).
"::select Error ".$this->error, LOG_ERR);
6437 $this->
db->rollback();