diff --git a/htdocs/compta/facture/card.php b/htdocs/compta/facture/card.php index 96b0049170f..07b7179ddee 100644 --- a/htdocs/compta/facture/card.php +++ b/htdocs/compta/facture/card.php @@ -4788,6 +4788,8 @@ if ($action == 'create') { $absolute_discount = price2num($absolute_discount, 'MT'); $absolute_creditnote = price2num($absolute_creditnote, 'MT'); + $nb_creditnote_notyetavailable = $soc->getOpenCreditNotesNotYetConvertedIntoDiscount(0); + $author = new User($db); if ($object->user_creation_id) { $author->fetch($object->user_creation_id); @@ -5245,7 +5247,7 @@ if ($action == 'create') { print ' '.$langs->transnoentities("CorrectInvoice", $facusing->getNomUrl(1, '', 32)).''; } - // Retrieve credit note ids + // Retrieve credit note ids (credit notes generated from this invoice) $object->getListIdAvoirFromInvoice(); if (!empty($object->creditnote_ids)) { diff --git a/htdocs/core/class/commoninvoice.class.php b/htdocs/core/class/commoninvoice.class.php index 7804cca4c7b..5bd78517f79 100644 --- a/htdocs/core/class/commoninvoice.class.php +++ b/htdocs/core/class/commoninvoice.class.php @@ -453,7 +453,8 @@ abstract class CommonInvoice extends CommonObject $sql = "SELECT rowid"; $sql .= " FROM ".$this->db->prefix().$this->table_element; $sql .= " WHERE fk_facture_source = ".((int) $this->id); - $sql .= " AND type = 2"; + $sql .= " AND type = ".self::TYPE_CREDIT_NOTE; + $resql = $this->db->query($sql); if ($resql) { $num = $this->db->num_rows($resql); diff --git a/htdocs/core/class/discount.class.php b/htdocs/core/class/discount.class.php index 2c37973aace..11b1caf0230 100644 --- a/htdocs/core/class/discount.class.php +++ b/htdocs/core/class/discount.class.php @@ -378,8 +378,6 @@ class DiscountAbsolute extends CommonObject */ public function delete($user) { - global $conf, $langs; - // Check if we can remove the discount if ($this->fk_facture_source) { $sql = "SELECT COUNT(rowid) as nb"; @@ -605,7 +603,7 @@ class DiscountAbsolute extends CommonObject * Return amount (with tax) of discounts currently available for a company, user or other criteria * * @param ?Societe $company Object third party for filter - * @param ?User $user Filtre sur un user auteur des remises + * @param ?User $user Filtre on an author of the discount * @param string $filter Filter other. Warning: Do not use a user input value here. * @param int|float $maxvalue Filter on max value for discount * @param int<0,1> $discount_type 0 => customer discount, 1 => supplier discount @@ -671,7 +669,6 @@ class DiscountAbsolute extends CommonObject return -1; } - /** * Return amount (with tax) of all deposits invoices used by invoice as a payment. * Should always be empty, except if option FACTURE_DEPOSITS_ARE_JUST_PAYMENTS or FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS is on (not recommended). diff --git a/htdocs/core/tpl/object_discounts.tpl.php b/htdocs/core/tpl/object_discounts.tpl.php index 2a704394009..16409b53a60 100644 --- a/htdocs/core/tpl/object_discounts.tpl.php +++ b/htdocs/core/tpl/object_discounts.tpl.php @@ -24,14 +24,22 @@ * $backtopage URL to come back to from discount modification pages */ - /** - * @var Object $object +/** + * @var Conf $conf * @var Form $form + * @var Object $object * @var Translate $langs * @var Societe $thirdparty - * @var float $absolute_discount Amount of fixed discounts available - * @var float $absolute_creditnote Amount of credit notes available + * @var string $backtopage + * + * @var float $absolute_discount Amount of fixed discounts available + * @var float $absolute_creditnote Amount of credit notes available + * @var int $nb_creditnote_notyetavailable Nb of cedit notes not yet available (not yet converted into discount) + * @var string $filterabsolutediscount + * @var string $filtercreditnote + * @var string $resteapayer * @var int $cannotApplyDiscount + * @var int $discount_type */ print ''."\n"; @@ -140,6 +148,12 @@ if ($absolute_creditnote > 0) { } } +if ($nb_creditnote_notyetavailable > 0) { + print ''; + $translationKey = 'SomeNotConvertedCreditNoteExists'; + print '
'.$langs->trans($translationKey, $nb_creditnote_notyetavailable).''; +} + if ($absolute_discount <= 0 && $absolute_creditnote <= 0) { if ($conf->dol_optimize_smallscreen) { $translationKey = 'AbsoluteDiscount'; diff --git a/htdocs/langs/en_US/compta.lang b/htdocs/langs/en_US/compta.lang index acaae9d05dd..4747e794743 100644 --- a/htdocs/langs/en_US/compta.lang +++ b/htdocs/langs/en_US/compta.lang @@ -329,3 +329,4 @@ RuleForLinesDates=Rule for lines dates Prepaid=Prepaid Postpaid=Postpaid SetRuleForLinesDates=Set rule for lines dates +SomeNotConvertedCreditNoteExists=There is %s credit notes not yet converted for consumption diff --git a/htdocs/societe/class/societe.class.php b/htdocs/societe/class/societe.class.php index 31a7f7bf76d..b8db8866b1d 100644 --- a/htdocs/societe/class/societe.class.php +++ b/htdocs/societe/class/societe.class.php @@ -2810,6 +2810,48 @@ class Societe extends CommonObject } } + + /** + * Return amount (with tax) of discounts currently available for a company, user or other criteria + * + * @param int<0,1> $discount_type 0 => customer discount, 1 => supplier discount + * @return int Return integer <0 if KO, nb otherwise + */ + public function getOpenCreditNotesNotYetConvertedIntoDiscount($discount_type = 0) + { + global $conf; + + $sql = "SELECT COUNT(f.rowid) as nb, SUM(f.total_ttc) as amount, SUM(f.multicurrency_total_ttc) as multicurrency_amount"; + if (!empty($discount_type)) { + $sql .= " FROM ".$this->db->prefix()."facture_fourn as f"; + } else { + $sql .= " FROM ".$this->db->prefix()."facture as f"; + } + $sql .= " WHERE f.entity = ".$conf->entity; + $sql .= " AND paye = 0 AND fk_statut = 1"; + $sql .= " AND f.type = ".CommonInvoice::TYPE_CREDIT_NOTE; + $sql .= " AND NOT EXISTS (SELECT rowid FROM ".$this->db->prefix()."societe_remise_except as rc"; + $sql .= " WHERE rc.discount_type = ".((int) $discount_type); + if (!empty($discount_type)) { + $sql .= " AND rc.fk_invoice_supplier = f.rowid"; + } else { + $sql .= " AND rc.fk_facture = f.rowid"; + } + $sql .= " AND rc.fk_soc = ".((int) $this->socid); + $sql .= ")"; + $sql .= " AND f.fk_soc = ".((int) $this->socid); + + $resql = $this->db->query($sql); + if ($resql) { + $obj = $this->db->fetch_object($resql); + + return $obj->nb; + } + + return -1; + } + + /** * Return array of sales representatives *