private def apply()

in lib/zuora-core/src/main/scala/com/gu/zuora/subscription/RatePlanChargeBillingSchedule.scala [86:155]


  private def apply(
      customerAcceptanceDate: LocalDate,
      contractEffectiveDate: LocalDate,
      billingDay: Option[String],
      triggerEvent: Option[String],
      triggerDate: Option[LocalDate],
      processedThroughDate: Option[LocalDate],
      chargedThroughDate: Option[LocalDate],
      billCycleDay: Int,
      upToPeriodType: Option[String],
      upToPeriods: Option[Int],
      optionalBillingPeriodName: Option[String],
      specificBillingPeriod: Option[Int],
      endDateCondition: Option[String],
      effectiveStartDate: LocalDate,
  ): Either[ZuoraApiFailure, RatePlanChargeBillingSchedule] = {
    for {
      endDateCondition <- endDateCondition.toRight(ZuoraApiFailure("RatePlanCharge.endDateCondition is required"))
      billingPeriodName <- optionalBillingPeriodName.toRight(
        ZuoraApiFailure("RatePlanCharge.billingPeriod is required"),
      )
      billingPeriod <- billingPeriodForName(billingPeriodName, specificBillingPeriod)

      calculatedRatePlanStartDate <- ratePlanStartDate(
        customerAcceptanceDate,
        contractEffectiveDate,
        billingDay,
        triggerEvent,
        triggerDate,
        processedThroughDate,
        billCycleDay,
      )

      calculatedRatePlanEndDate <- ratePlanEndDate(
        billingPeriod,
        calculatedRatePlanStartDate,
        endDateCondition,
        upToPeriodType,
        upToPeriods,
      )

      scheduleForCalculatedStartDate = RatePlanChargeBillingSchedule(
        calculatedRatePlanStartDate,
        calculatedRatePlanEndDate,
        billingPeriod,
      )

      endDateBasedOnEffectiveStartDate <- ratePlanEndDate(
        billingPeriod,
        effectiveStartDate,
        endDateCondition,
        upToPeriodType,
        upToPeriods,
      )

      scheduleForEffectiveStartDate = RatePlanChargeBillingSchedule(
        effectiveStartDate,
        endDateBasedOnEffectiveStartDate,
        billingPeriod,
      )

      billingSchedule <- selectScheduleThatPredictsProcessedThroughDate(
        NonEmptyList.of(
          scheduleForCalculatedStartDate,
          scheduleForEffectiveStartDate,
        ),
        processedThroughDate,
      )
    } yield billingSchedule
  }