Skip to content

Commit

Permalink
fix(timezone): Correct calculation for true-up fee with timezone
Browse files Browse the repository at this point in the history
  • Loading branch information
rsempe committed Aug 16, 2023
1 parent a48a827 commit bd9f3ea
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
7 changes: 3 additions & 4 deletions app/services/fees/create_true_up_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,10 @@ def call
delegate :charge, :subscription, to: :fee

def prorated_min_amount_cents
from_date = boundaries.charges_from_datetime.to_date
to_date = boundaries.charges_to_datetime.to_date

# NOTE: number of days between beginning of the period and the termination date
number_of_day_to_bill = (to_date + 1.day - from_date).to_i
from_datetime = boundaries.charges_from_datetime.to_time
to_datetime = boundaries.charges_to_datetime.to_time
number_of_day_to_bill = (to_datetime - from_datetime).fdiv(1.day).ceil

date_service.charge_single_day_price(charge:) * number_of_day_to_bill
end
Expand Down
6 changes: 3 additions & 3 deletions spec/scenarios/spending_minimum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

it 'creates expected credit note and invoice' do
### 8 Jan: Create subscription
travel_to(DateTime.new(2023, 1, 8)) do
travel_to(DateTime.new(2023, 1, 8, 8)) do
expect {
create_subscription(
{
Expand All @@ -51,7 +51,7 @@
expect(sub_invoice.total_amount_cents).to eq(4645) # 60 / 31 * 24

### 25 Feb: Create event and Terminate subscription
travel_to(DateTime.new(2023, 2, 25)) do
travel_to(DateTime.new(2023, 2, 25, 6)) do
create_event(
{
code: metric.code,
Expand Down Expand Up @@ -183,7 +183,7 @@
expect(sub_invoice.total_amount_cents).to eq(4645) # 60 / 31 * 24

### 25 Feb: Create event and Terminate subscription
travel_to(DateTime.new(2023, 2, 25)) do
travel_to(DateTime.new(2023, 2, 25, 8)) do
create_event(
{
code: metric.code,
Expand Down
52 changes: 47 additions & 5 deletions spec/services/fees/create_true_up_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,20 @@

let(:charge) { create(:standard_charge, plan:, min_amount_cents: 1000) }
let(:invoice) { create(:invoice, customer:, organization:) }
let(:fee) { create(:charge_fee, amount_cents:, customer:, charge:, invoice:) }
let(:fee) do
create(
:charge_fee,
amount_cents:,
customer:,
charge:,
properties: {
'from_datetime' => DateTime.parse('2023-08-01 00:00:00'),
'to_datetime' => DateTime.parse('2023-08-31 23:59:59'),
'charges_from_datetime' => DateTime.parse('2023-08-01 00:00:00'),
'charges_to_datetime' => DateTime.parse('2023-08-31 23:59:59'),
},
)
end
let(:amount_cents) { 700 }

before { tax }
Expand Down Expand Up @@ -69,10 +82,10 @@
amount_cents:,
charge:,
properties: {
'from_datetime' => Date.parse('2022-08-01 00:00:00'),
'to_datetime' => Date.parse('2022-08-15 23:59:59'),
'charges_from_datetime' => Date.parse('2022-08-01 00:00:00'),
'charges_to_datetime' => Date.parse('2022-08-15 23:59:59'),
'from_datetime' => DateTime.parse('2022-08-01 00:00:00'),
'to_datetime' => DateTime.parse('2022-08-15 23:59:59'),
'charges_from_datetime' => DateTime.parse('2022-08-01 00:00:00'),
'charges_to_datetime' => DateTime.parse('2022-08-15 23:59:59'),
},
)
end
Expand All @@ -91,5 +104,34 @@
end
end
end

context 'with customer timezone' do
let(:customer) { create(:customer, organization:, timezone: 'Pacific/Fiji') }

it 'instantiates a true-up fee' do
travel_to(DateTime.new(2023, 9, 1)) do
result = create_service.call

aggregate_failures do
expect(result).to be_success

expect(result.true_up_fee).to have_attributes(
subscription: fee.subscription,
charge: fee.charge,
amount_currency: fee.currency,
fee_type: 'charge',
invoiceable: fee.charge,
properties: fee.properties,
payment_status: 'pending',
units: 1,
events_count: 0,
group: nil,
amount_cents: 300,
true_up_parent_fee_id: fee.id,
)
end
end
end
end
end
end

0 comments on commit bd9f3ea

Please sign in to comment.