private def applyDiscountAndThenSum()

in lambda/src/main/scala/pricemigrationengine/model/AmendmentData.scala [202:236]


  private def applyDiscountAndThenSum(discountPercentage: Option[Double], beforeDiscount: Seq[BigDecimal]): BigDecimal =
    beforeDiscount.map(applyDiscount(discountPercentage)).sum

  private def applyDiscount(discountPercentage: Option[Double])(beforeDiscount: BigDecimal) =
    roundDown(discountPercentage.fold(beforeDiscount)(percentage => (100 - percentage) / 100 * beforeDiscount))

  def roundDown(d: BigDecimal): BigDecimal = d.setScale(2, RoundingMode.DOWN)

  /** In some cases, a product rate plan charge has a monthly billing period but a subscription has overridden it with a
    * rate plan charge with a different billing period. In these cases, the price has to be multiplied by the number of
    * months in the subscription billing period.
    */
  private[model] def adjustedForBillingPeriod(
      price: BigDecimal,
      subscriptionBillingPeriod: Option[String],
      productBillingPeriod: Option[String]
  ): Either[DataExtractionFailure, BigDecimal] = {

    val multiple = (subscriptionBillingPeriod, productBillingPeriod) match {
      case (Some(billingPeriod), Some(prodBillingPeriod)) if billingPeriod == prodBillingPeriod => Right(1)
      case (Some(billingPeriod), Some("Month")) =>
        monthMultiples
          .get(billingPeriod)
          .map(Right(_))
          .getOrElse(Left(DataExtractionFailure(s"Unknown billing period: $billingPeriod")))
      case (billingPeriod, productBillingPeriod) =>
        Left(
          DataExtractionFailure(
            s"Invalid billing period combinations: subscription = $billingPeriod, product = $productBillingPeriod"
          )
        )
    }

    multiple map (_ * price)
  }