Skip to content

Commit

Permalink
No longer use total periods in subscription alignment.
Browse files Browse the repository at this point in the history
  • Loading branch information
rvdsteege committed Jun 5, 2024
1 parent 28d7ed5 commit 799d690
Showing 1 changed file with 10 additions and 11 deletions.
21 changes: 10 additions & 11 deletions src/Subscriptions/SubscriptionPhase.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,24 +680,23 @@ public static function align( self $phase, \DateTimeInterface $align_date ) {

$alignment_phase = new self( $phase->get_subscription(), $start_date, $alignment_interval, $phase->get_amount() );

$alignment_phase->set_total_periods( 1 );
$alignment_phase->set_alignment_rate( $alignment_difference->days / $regular_difference->days );

// Remove one period from regular phase.
$total_periods = $phase->get_total_periods();

if ( null !== $total_periods ) {
$phase->set_total_periods( $total_periods - 1 );
}
$alignment_end_date = $start_date->add( $alignment_interval );

$alignment_end_date = $alignment_phase->get_end_date();
$alignment_phase->set_end_date( $alignment_end_date );
$alignment_phase->set_alignment_rate( $alignment_difference->days / $regular_difference->days );

if ( null === $alignment_end_date ) {
if ( null === $alignment_phase->get_end_date() ) {
throw new \Exception( 'The align phase should always end because this phase exists for one period.' );
}

$phase->set_start_date( $alignment_end_date );

if ( null !== $phase->end_date ) {
$end_date = $phase->end_date->add( $alignment_interval );

$phase->set_end_date( $end_date );
}

return $alignment_phase;
}
}

0 comments on commit 799d690

Please sign in to comment.