def totalChargeAmount()

in lambda/src/main/scala/pricemigrationengine/model/AmendmentData.scala [120:145]


  def totalChargeAmount(
      subscription: ZuoraSubscription,
      invoiceList: ZuoraInvoiceList,
      serviceStartDate: LocalDate
  ): Either[DataExtractionFailure, BigDecimal] = {
    /*
     * As charge amounts on Zuora invoice previews don't include tax,
     * we have to find the price of the rate plan charges on the subscription
     * that correspond with items in the invoice list.
     */
    val amounts = for {
      invoiceItem <- ZuoraInvoiceItem.items(invoiceList, subscription, serviceStartDate)
      ratePlanCharge <- ZuoraRatePlanCharge.matchingRatePlanCharge(subscription, invoiceItem).toSeq
    } yield individualChargeAmount(ratePlanCharge)

    val discounts = amounts.collect { case Left(percentageDiscount) => percentageDiscount }

    if (discounts.length > 1) Left(DataExtractionFailure(s"Multiple discounts applied: ${discounts.mkString(", ")}"))
    else {
      val newPrice = applyDiscountAndThenSum(
        discountPercentage = discounts.headOption,
        beforeDiscount = amounts.collect { case Right(amount) => amount }
      )
      Right(newPrice)
    }
  }