Skip to content

Commit

Permalink
take created_at into account when running billing in the past
Browse files Browse the repository at this point in the history
  • Loading branch information
nudded committed Dec 16, 2024
1 parent e71465c commit 2a8db21
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 176 deletions.
6 changes: 3 additions & 3 deletions app/services/subscriptions/billing_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ def billable_subscriptions
-- Do not bill subscriptions that have started _after_ :today (excludes subscriptions starting today! and also importantly invoices that might have started after this service is run)
AND DATE(subscriptions.started_at#{at_time_zone}) < DATE(:today#{at_time_zone})
-- Do not bill subscriptions that were not created yet
and DATE(subscriptions.created_at) <= Date(:today)
AND (
subscriptions.ending_at IS NULL OR
DATE(subscriptions.ending_at#{at_time_zone}) != DATE(:today#{at_time_zone})
Expand All @@ -95,9 +97,7 @@ def base_subscription_scope(billing_time: nil, interval: nil, conditions: nil)
INNER JOIN plans ON plans.id = subscriptions.plan_id
INNER JOIN customers ON customers.id = subscriptions.customer_id
INNER JOIN organizations ON organizations.id = customers.organization_id
WHERE subscriptions.status IN (#{Subscription.statuses[:active]}, #{Subscription.statuses[:terminated]})
-- Because this job might be run for the past, we need to "revert" the past and if the subscription was not yet terminated, it should be billed.
AND (subscriptions.terminated_at is NULL OR subscriptions.terminated_at >= :today#{at_time_zone})
WHERE subscriptions.status = #{Subscription.statuses[:active]}
AND subscriptions.billing_time = #{Subscription.billing_times[billing_time]}
AND plans.interval = #{Plan.intervals[interval]}
AND #{conditions.join(" AND ")}
Expand Down
Loading

0 comments on commit 2a8db21

Please sign in to comment.