Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
294 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
lib/holiday-stops/src/main/scala/com/gu/holiday_stops/SixdayVoucherHolidayStopConfig.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.gu.holiday_stops | ||
|
||
case class SixdayVoucherHolidayStopConfig( | ||
productRatePlanId: String | ||
) | ||
|
||
object SixdayVoucherHolidayStopConfig { | ||
val Prod = SixdayVoucherHolidayStopConfig( | ||
productRatePlanId = "2c92a0fd56fe270b0157040e42e536ef" | ||
) | ||
val Code = SixdayVoucherHolidayStopConfig( | ||
productRatePlanId = "2c92c0f955ca02910155da254a641fb3" | ||
) | ||
val Dev = SixdayVoucherHolidayStopConfig( | ||
productRatePlanId = "2c92c0f8555ce5cf01556e7f01771b8a" | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...s/src/main/scala/com/gu/holiday_stops/subscription/CurrentSixdayVoucherSubscription.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.gu.holiday_stops.subscription | ||
|
||
import com.gu.holiday_stops.{Config, ZuoraHolidayError} | ||
import com.gu.salesforce.holiday_stops.SalesforceHolidayStopRequestsDetail.StoppedPublicationDate | ||
import enumeratum._ | ||
|
||
object CurrentSixdayVoucherSubscriptionPredicates { | ||
def ratePlanIsSixdayVoucher(ratePlan: RatePlan, sixdayVoucherProductRatePlanId: String): Boolean = | ||
ratePlan.productRatePlanId == sixdayVoucherProductRatePlanId | ||
|
||
def stoppedPublicationIsNotOnSunday(stoppedPublicationDate: StoppedPublicationDate): Boolean = | ||
List("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday").contains(stoppedPublicationDate.getDayOfWeek) | ||
|
||
def ratePlanHasBeenInvoicedForAllCharges(ratePlan: RatePlan): Boolean = { | ||
ratePlan.ratePlanCharges.forall { ratePlanCharge => | ||
(for { | ||
fromInclusive <- ratePlanCharge.processedThroughDate | ||
toExclusive <- ratePlanCharge.chargedThroughDate | ||
} yield { | ||
toExclusive isAfter fromInclusive | ||
}).getOrElse(false) | ||
} | ||
} | ||
|
||
def billingPeriodIsAnnualOrMonthOrQuarterOrSemiAnnual(ratePlan: RatePlan): Boolean = { | ||
val billingPeriods = ratePlan.ratePlanCharges.map(_.billingPeriod) | ||
val allPeriodsAreTheSame = billingPeriods.headOption.exists(bp => billingPeriods.forall(_ == bp)) | ||
val expectedBillingPeriod = billingPeriods.forall(List(Some("Annual"), Some("Month"), Some("Quarter"), Some("Semi-Annual")).contains) | ||
allPeriodsAreTheSame && expectedBillingPeriod | ||
} | ||
} | ||
|
||
case class CurrentSixdayVoucherSubscription( | ||
subscriptionNumber: String, | ||
billingPeriod: String, | ||
price: Double, | ||
invoicedPeriod: CurrentInvoicedPeriod, | ||
ratePlanId: String, | ||
productRatePlanId: String, | ||
dayOfWeek: VoucherDayOfWeek | ||
) extends CurrentVoucherSubscription | ||
|
||
|
||
object CurrentSixdayVoucherSubscription { | ||
private def findSixdayVoucherRatePlan( | ||
subscription: Subscription, | ||
sixdayVoucherProductRatePlanId: String, | ||
stoppedPublicationDate: StoppedPublicationDate): Option[RatePlan] = { | ||
|
||
subscription | ||
.ratePlans | ||
.find { ratePlan => | ||
import CurrentSixdayVoucherSubscriptionPredicates._ | ||
List( | ||
stoppedPublicationIsNotOnSunday(stoppedPublicationDate), | ||
ratePlanIsSixdayVoucher(ratePlan, sixdayVoucherProductRatePlanId), | ||
ratePlanHasBeenInvoicedForAllCharges(ratePlan), // FIXME: Why is Saturday not billed? | ||
billingPeriodIsAnnualOrMonthOrQuarterOrSemiAnnual(ratePlan), | ||
).forall(_ == true) | ||
} | ||
} | ||
|
||
def apply( | ||
subscription: Subscription, | ||
config: Config, | ||
stoppedPublicationDate: StoppedPublicationDate | ||
): Either[ZuoraHolidayError, CurrentSixdayVoucherSubscription] = { | ||
findSixdayVoucherRatePlan(subscription, config.sixdayVoucherConfig.productRatePlanId, stoppedPublicationDate).flatMap { currentSixdayVoucherRatePlan => | ||
for { | ||
rpc <- currentSixdayVoucherRatePlan.ratePlanCharges.find(_.name == stoppedPublicationDate.getDayOfWeek) // find particular RPC, Saturday or Sunday | ||
billingPeriod <- rpc.billingPeriod | ||
startDateIncluding <- rpc.processedThroughDate | ||
endDateExcluding <- rpc.chargedThroughDate | ||
} yield new CurrentSixdayVoucherSubscription( | ||
subscriptionNumber = subscription.subscriptionNumber, | ||
billingPeriod = billingPeriod, | ||
price = rpc.price, | ||
invoicedPeriod = CurrentInvoicedPeriod( | ||
startDateIncluding = startDateIncluding, | ||
endDateExcluding = endDateExcluding | ||
), | ||
ratePlanId = currentSixdayVoucherRatePlan.id, | ||
productRatePlanId = currentSixdayVoucherRatePlan.productRatePlanId, | ||
dayOfWeek = VoucherDayOfWeek.withName(stoppedPublicationDate.getDayOfWeek) | ||
) | ||
}.toRight(ZuoraHolidayError(s"Failed to determine Sixday Voucher Newspaper Guardian rate plan: $subscription")) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
...y-stops/src/main/scala/com/gu/holiday_stops/subscription/CurrentVoucherSubscription.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.gu.holiday_stops.subscription | ||
|
||
trait CurrentVoucherSubscription { | ||
def price: Double | ||
def billingPeriod: String | ||
def subscriptionNumber: String | ||
} |
Oops, something went wrong.