diff --git a/htdocs/core/class/discount.class.php b/htdocs/core/class/discount.class.php index 8e66b29a3f8..479a7a48367 100644 --- a/htdocs/core/class/discount.class.php +++ b/htdocs/core/class/discount.class.php @@ -227,6 +227,14 @@ class DiscountAbsolute return -1; } + $userid = $user->id; + if (! ($userid > 0)) // For example when record is saved into an anonymous context with a not loaded object $user. + { + $tmpinvoice = new Facture($this->db); + $tmpinvoice->fetch($this->fk_facture_source); + $userid = $tmpinvoice->fk_user_author; // We use the author of invoice + } + // Insert request $sql = "INSERT INTO ".MAIN_DB_PREFIX."societe_remise_except"; $sql.= " (entity, datec, fk_soc, discount_type, fk_user, description,"; @@ -234,7 +242,7 @@ class DiscountAbsolute $sql.= " multicurrency_amount_ht, multicurrency_amount_tva, multicurrency_amount_ttc,"; $sql.= " fk_facture_source, fk_invoice_supplier_source"; $sql.= ")"; - $sql.= " VALUES (".$conf->entity.", '".$this->db->idate($this->datec!=''?$this->datec:dol_now())."', ".$this->fk_soc.", ".(empty($this->discount_type)?0:intval($this->discount_type)).", ".$user->id.", '".$this->db->escape($this->description)."',"; + $sql.= " VALUES (".$conf->entity.", '".$this->db->idate($this->datec!=''?$this->datec:dol_now())."', ".$this->fk_soc.", ".(empty($this->discount_type)?0:intval($this->discount_type)).", ".$userid.", '".$this->db->escape($this->description)."',"; $sql.= " ".$this->amount_ht.", ".$this->amount_tva.", ".$this->amount_ttc.", ".$this->tva_tx.","; $sql.= " ".$this->multicurrency_amount_ht.", ".$this->multicurrency_amount_tva.", ".$this->multicurrency_amount_ttc.", "; $sql.= " ".($this->fk_facture_source ? "'".$this->db->escape($this->fk_facture_source)."'":"null").",";