diff --git a/htdocs/core/class/commonobject.class.php b/htdocs/core/class/commonobject.class.php index 02691a2b2de..e0646e4f2c8 100644 --- a/htdocs/core/class/commonobject.class.php +++ b/htdocs/core/class/commonobject.class.php @@ -2253,24 +2253,43 @@ abstract class CommonObject } /** - * Function that returns the total amount of discounts applied. + * Function that returns the total amount HT of discounts applied for all lines. * - * @return false|float False is returned if the discount couldn't be retrieved + * @return in 0 if no discount */ function getTotalDiscount() { - $sql = 'SELECT (SUM(`subprice`) - SUM(`total_ht`)) as `discount` FROM '.MAIN_DB_PREFIX.$this->table_element.'det WHERE `'.$this->fk_element.'` = '.$this->id; + $total_discount=0; - $query = $this->db->query($sql); + $sql = "SELECT subprice as pu_ht, qty, remise_percent, total_ht"; + $sql.= " FROM ".MAIN_DB_PREFIX.$this->table_element."det"; + $sql.= " WHERE ".$this->fk_element." = ".$this->id; - if ($query) + dol_syslog(get_class($this).'::getTotalDiscount sql='.$sql); + $resql = $this->db->query($sql); + if ($resql) { - $result = $this->db->fetch_object($query); + $num=$this->db->num_rows($resql); + $i=0; + while ($i < $num) + { + $obj = $this->db->fetch_object($query); - return price2num($result->discount); + $pu_ht = $obj->pu_ht; + $qty= $obj->qty; + $discount_percent_line = $obj->remise_percent; + $total_ht = $obj->total_ht; + + $total_discount_line = price2num(($pu_ht * $qty) - $total_ht, 'MT'); + $total_discount += $total_discount_line; + + $i++; + } } - - return false; + else dol_syslog(get_class($this).'::getTotalDiscount '.$this->db->lasterror(), LOG_ERR); + + //print $total_discount; exit; + return price2num($total_discount); } /** diff --git a/htdocs/core/modules/commande/doc/doc_generic_order_odt.modules.php b/htdocs/core/modules/commande/doc/doc_generic_order_odt.modules.php index ad1a2eaa4d4..de7fde68667 100644 --- a/htdocs/core/modules/commande/doc/doc_generic_order_odt.modules.php +++ b/htdocs/core/modules/commande/doc/doc_generic_order_odt.modules.php @@ -115,7 +115,7 @@ class doc_generic_order_odt extends ModelePDFCommandes 'object_total_ht'=>price($object->total_ht,0,$outputlangs), 'object_total_vat'=>price($object->total_tva,0,$outputlangs), 'object_total_ttc'=>price($object->total_ttc,0,$outputlangs), - 'object_total_discount' => price($object->getTotalDiscount(), 0, $outputlangs), + 'object_total_discount_ht' => price($object->getTotalDiscount(), 0, $outputlangs), 'object_vatrate'=>vatrate($object->tva), 'object_note_private'=>$object->note, 'object_note'=>$object->note_public, diff --git a/htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php b/htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php index c85f5cd950a..81b2477f819 100644 --- a/htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php +++ b/htdocs/core/modules/facture/doc/doc_generic_invoice_odt.modules.php @@ -124,7 +124,7 @@ class doc_generic_invoice_odt extends ModelePDFFactures 'object_total_ht'=>price($object->total_ht,0,$outputlangs), 'object_total_vat'=>price($object->total_tva,0,$outputlangs), 'object_total_ttc'=>price($object->total_ttc,0,$outputlangs), - 'object_total_discount' => price($object->getTotalDiscount(), 0, $outputlangs), + 'object_total_discount_ht' => price($object->getTotalDiscount(), 0, $outputlangs), 'object_vatrate'=>(isset($object->tva)?vatrate($object->tva):''), 'object_note_private'=>$object->note, 'object_note'=>$object->note_public, diff --git a/htdocs/core/modules/propale/doc/doc_generic_proposal_odt.modules.php b/htdocs/core/modules/propale/doc/doc_generic_proposal_odt.modules.php index b9732b103db..2dc391dc08d 100644 --- a/htdocs/core/modules/propale/doc/doc_generic_proposal_odt.modules.php +++ b/htdocs/core/modules/propale/doc/doc_generic_proposal_odt.modules.php @@ -114,7 +114,7 @@ class doc_generic_proposal_odt extends ModelePDFPropales 'object_total_ht'=>price($object->total_ht,0,$outputlangs), 'object_total_vat'=>price($object->total_tva,0,$outputlangs), 'object_total_ttc'=>price($object->total_ttc,0,$outputlangs), - 'object_total_discount' => price($object->getTotalDiscount(), 0, $outputlangs), + 'object_total_discount_ht' => price($object->getTotalDiscount(), 0, $outputlangs), 'object_vatrate'=>vatrate($object->tva), 'object_note_private'=>$object->note, 'object_note'=>$object->note_public, diff --git a/htdocs/install/doctemplates/invoices/template_invoice.odt b/htdocs/install/doctemplates/invoices/template_invoice.odt index 2d50e435221..2c43d353bbb 100644 Binary files a/htdocs/install/doctemplates/invoices/template_invoice.odt and b/htdocs/install/doctemplates/invoices/template_invoice.odt differ