package order import "github.com/perfect-panel/server/internal/types" // getDiscount returns the discount factor for the given quantity. // // Design: when a quantity has both a new_user_only=true entry and a new_user_only=false entry, // the new_user_only=true entry is a "gating marker" (its discount value is irrelevant). // The actual discounted price is always carried by the new_user_only=false entry. // Whether the buyer is eligible to use this quantity is enforced by isNewUserOnlyForQuantity // before calling this function. Here we simply return the best applicable discount. // // Priority: new_user_only=false entry beats new_user_only=true entry for the same quantity. func getDiscount(discounts []types.SubscribeDiscount, inputMonths int64, isNewUser bool) float64 { var best float64 = 1 foundFallback := false for _, d := range discounts { if d.Quantity != inputMonths || d.Discount <= 0 || d.Discount >= 100 { continue } factor := d.Discount / float64(100) if !d.NewUserOnly { // Prefer the non-new-user-only tier as the actual price tier. best = factor foundFallback = true } else if !foundFallback && isNewUser { // Fall back to new-user-only tier only when no general tier exists // and the buyer qualifies. best = factor } } return best } // isNewUserOnlyForQuantity checks whether the matched discount tier has new_user_only enabled. // Returns true only when all matching tiers for the given quantity require new-user status // (i.e. no fallback tier with new_user_only=false exists). When both a new-user-only tier // and a general tier exist for the same quantity, non-new-users can still purchase via the // general tier, so this returns false. func isNewUserOnlyForQuantity(discounts []types.SubscribeDiscount, inputQuantity int64) bool { hasNewUserOnly := false hasFallback := false for _, d := range discounts { if d.Quantity != inputQuantity { continue } if d.NewUserOnly { hasNewUserOnly = true } else { hasFallback = true } } return hasNewUserOnly && !hasFallback }