Skip to content

Commit

Permalink
Merge pull request #183 from pronamic/182-subscription-phase-alignmen…
Browse files Browse the repository at this point in the history
…t-issue-for-subscriptions-with-end-date

Subscription phase alignment issue for subscriptions with end date
  • Loading branch information
rvdsteege authored Jun 5, 2024
2 parents b0a6a19 + f9d41f1 commit dc45e1a
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 31 deletions.
36 changes: 18 additions & 18 deletions src/Admin/AdminModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -533,28 +533,32 @@ public function maybe_test_payment() {
$subscription->set_description( $description );
$subscription->set_lines( $payment->get_lines() );

// Phase.
$phase = new SubscriptionPhase(
$subscription,
new DateTimeImmutable(),
new SubscriptionInterval( 'P' . $interval . Util::to_period( $interval_period ) ),
$price
);

// Ends on.
$total_periods = null;

if ( \array_key_exists( 'pronamic_pay_ends_on', $_POST ) ) {
$total_periods = null;

switch ( $_POST['pronamic_pay_ends_on'] ) {
case 'count':
$count = \filter_input( \INPUT_POST, 'pronamic_pay_ends_on_count', \FILTER_VALIDATE_INT );

if ( ! empty( $count ) ) {
$total_periods = $count;
}
$total_periods = (int) \filter_input( \INPUT_POST, 'pronamic_pay_ends_on_count', \FILTER_VALIDATE_INT );

break;
case 'date':
$end_date = \array_key_exists( 'pronamic_pay_ends_on_date', $_POST ) ? \sanitize_text_field( \wp_unslash( $_POST['pronamic_pay_ends_on_date'] ) ) : '';

if ( ! empty( $end_date ) ) {
$interval_spec = 'P' . $interval . Util::to_period( $interval_period );

$period = new \DatePeriod(
new \DateTime(),
new \DateInterval( $interval_spec ),
$phase->get_start_date(),
$phase->get_interval(),
new \DateTime( $end_date )
);

Expand All @@ -563,17 +567,13 @@ public function maybe_test_payment() {

break;
}
}

// Phase.
$phase = new SubscriptionPhase(
$subscription,
new DateTimeImmutable(),
new SubscriptionInterval( 'P' . $interval . Util::to_period( $interval_period ) ),
$price
);
if ( null !== $total_periods ) {
$end_date = $phase->get_start_date()->add( $phase->get_interval()->multiply( $total_periods ) );

$phase->set_total_periods( $total_periods );
$phase->set_end_date( $end_date );
}
}

$subscription->add_phase( $phase );

Expand Down
26 changes: 13 additions & 13 deletions src/Subscriptions/SubscriptionPhase.php
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,7 @@ private function add_interval( $date, $times = 1 ) {
*
* @param DateTimeImmutable $start_date Start date.
* @return SubscriptionPeriod|null
* @throws \Exception Throws exception on invalid date period.
*/
public function get_period( DateTimeImmutable $start_date = null ) {
if ( null === $start_date ) {
Expand All @@ -487,7 +488,11 @@ public function get_period( DateTimeImmutable $start_date = null ) {
$end_date = $this->add_interval( $start_date );

if ( null !== $this->end_date && $end_date > $this->end_date ) {
return null;
$end_date = $this->end_date;

if ( $start_date > $end_date ) {
throw new \Exception( 'The start date of a subscription period cannot be later than the end date.' );
}
}

$period = new SubscriptionPeriod( $this, $start_date, $end_date, $this->get_amount() );
Expand Down Expand Up @@ -675,24 +680,19 @@ 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 );
$alignment_end_date = $start_date->add( $alignment_interval );

// Remove one period from regular phase.
$total_periods = $phase->get_total_periods();
$alignment_phase->set_end_date( $alignment_end_date );
$alignment_phase->set_alignment_rate( $alignment_difference->days / $regular_difference->days );

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

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

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

$phase->set_start_date( $alignment_end_date );

return $alignment_phase;
}
}

0 comments on commit dc45e1a

Please sign in to comment.