Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions app/models/fixed_charge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class FixedCharge < ApplicationRecord
has_many :taxes, through: :applied_taxes

scope :pay_in_advance, -> { where(pay_in_advance: true) }
scope :pay_in_arrears, -> { where(pay_in_advance: false) }

CHARGE_MODELS = {
standard: "standard",
Expand All @@ -33,13 +34,19 @@ class FixedCharge < ApplicationRecord

validates :units, numericality: {greater_than_or_equal_to: 0}
validates :charge_model, presence: true
validates :pay_in_advance, inclusion: {in: [true, false]}
validates :prorated, inclusion: {in: [true, false]}
validates :pay_in_advance, exclusion: [nil]
validates :prorated, exclusion: [nil]
validates :properties, presence: true

def equal_properties?(fixed_charge)
charge_model == fixed_charge.charge_model && properties == fixed_charge.properties
end

def included_in_next_subscription?(subscription)
return false if subscription.next_subscription.nil?

subscription.next_subscription.plan.fixed_charges.exists?(add_on_id:)
end
end

# == Schema Information
Expand Down
5 changes: 4 additions & 1 deletion app/models/subscription.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,11 @@ def adjusted_boundaries(datetime, boundaries)
to_datetime: dates_service.to_datetime,
charges_from_datetime: dates_service.charges_from_datetime,
charges_to_datetime: dates_service.charges_to_datetime,
fixed_charges_from_datetime: dates_service.fixed_charges_from_datetime,
fixed_charges_to_datetime: dates_service.fixed_charges_to_datetime,
timestamp: datetime,
charges_duration: dates_service.charges_duration_in_days
charges_duration: dates_service.charges_duration_in_days,
fixed_charges_duration: dates_service.fixed_charges_duration_in_days
)

InvoiceSubscription.matching?(self, previous_period_boundaries) ? boundaries : previous_period_boundaries
Expand Down
26 changes: 26 additions & 0 deletions app/services/subscriptions/dates/semiannual_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,32 @@ def compute_charges_to_date
compute_to_date(compute_charges_from_date)
end

def compute_fixed_charges_from_date
return monthly_service.compute_fixed_charges_from_date if plan.bill_fixed_charges_monthly

if terminated?
return subscription.anniversary? ? previous_anniversary_day(billing_date) : billing_date.beginning_of_half_year
end

return compute_from_date if plan.pay_in_arrears?
return base_date.beginning_of_half_year if calendar?

previous_anniversary_day(base_date)
end

def compute_fixed_charges_to_date
return monthly_service.compute_fixed_charges_to_date if plan.bill_fixed_charges_monthly
return compute_fixed_charges_from_date.end_of_half_year if calendar?

compute_to_date(compute_fixed_charges_from_date)
end

def compute_fixed_charges_duration(from_date:)
return monthly_service.compute_fixed_charges_duration(from_date:) if plan.bill_fixed_charges_monthly

compute_duration(from_date:)
end

def compute_duration(from_date:)
next_to_date = compute_to_date(from_date)

Expand Down
18 changes: 15 additions & 3 deletions app/services/subscriptions/dates_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,21 @@ def self.charge_pay_in_advance_interval(timestamp, subscription)

{
charges_from_date: date_service.charges_from_datetime&.to_date,
charges_to_date: date_service.charges_to_datetime&.to_date,
fixed_charges_from_date: date_service.fixed_charges_from_datetime&.to_date,
fixed_charges_to_date: date_service.fixed_charges_to_datetime&.to_date
charges_to_date: date_service.charges_to_datetime&.to_date
}
end

def self.fixed_charge_pay_in_advance_interval(timestamp, subscription)
date_service = new_instance(
subscription,
Time.zone.at(timestamp),
current_usage: true
)

{
fixed_charges_from_datetime: date_service.fixed_charges_from_datetime,
fixed_charges_to_datetime: date_service.fixed_charges_to_datetime,
fixed_charges_duration: date_service.fixed_charges_duration_in_days
}
end

Expand Down
67 changes: 65 additions & 2 deletions spec/models/fixed_charge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,42 @@

it { is_expected.to validate_numericality_of(:units).is_greater_than_or_equal_to(0) }
it { is_expected.to validate_presence_of(:charge_model) }
it { is_expected.to validate_inclusion_of(:pay_in_advance).in_array([true, false]) }
it { is_expected.to validate_inclusion_of(:prorated).in_array([true, false]) }
it { is_expected.to validate_exclusion_of(:pay_in_advance).in_array([nil]) }
it { is_expected.to validate_exclusion_of(:prorated).in_array([nil]) }
it { is_expected.to validate_presence_of(:properties) }

describe "scopes" do
let(:scoped) { create(:fixed_charge) }
let(:deleted) { create(:fixed_charge, :deleted) }
let(:pay_in_advance) { create(:fixed_charge, pay_in_advance: true) }
let(:pay_in_arrears) { create(:fixed_charge, pay_in_advance: false) }

before do
scoped
deleted
pay_in_advance
pay_in_arrears
end

describe ".all" do
it "returns all not deleted fixed charges" do
expect(described_class.all).to match_array([scoped, pay_in_advance, pay_in_arrears])
end
end

describe ".pay_in_advance" do
it "returns only pay_in_advance fixed charges" do
expect(described_class.pay_in_advance).to match_array([pay_in_advance])
end
end

describe ".pay_in_arrears" do
it "returns only pay_in_arrears fixed charges" do
expect(described_class.pay_in_arrears).to match_array([pay_in_arrears, scoped])
end
end
end

describe "#equal_properties?" do
let(:fixed_charge1) { build(:fixed_charge, properties: {amount: 100}) }

Expand Down Expand Up @@ -51,4 +83,35 @@
end
end
end

describe "#included_in_next_subscription?" do
let(:add_on) { build(:add_on) }
let(:fixed_charge) { build(:fixed_charge, add_on:) }
let(:subscription) { create(:subscription, plan: fixed_charge.plan) }
let(:next_subscription) { create(:subscription, :with_previous_subscription, previous_subscription: subscription) }

context "when the fixed charge is included in the next subscription" do
before { next_subscription.plan.fixed_charges = [fixed_charge] }

it "returns true" do
expect(fixed_charge.included_in_next_subscription?(subscription)).to be true
end
end

context "when the fixed charge is not included in the next subscription" do
before { next_subscription.plan.fixed_charges = [] }

it "returns false" do
expect(fixed_charge.included_in_next_subscription?(subscription)).to be false
end
end

context "when there is no next subscription" do
let(:next_subscription) { nil }

it "returns false" do
expect(fixed_charge.included_in_next_subscription?(subscription)).to be false
end
end
end
end
4 changes: 4 additions & 0 deletions spec/models/subscription_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -662,6 +662,8 @@
to_datetime: date_service.to_datetime,
charges_from_datetime: date_service.charges_from_datetime,
charges_to_datetime: date_service.charges_to_datetime,
fixed_charges_from_datetime: date_service.fixed_charges_from_datetime,
fixed_charges_to_datetime: date_service.fixed_charges_to_datetime,
timestamp: billing_date
}
end
Expand Down Expand Up @@ -696,6 +698,8 @@
expect(new_boundaries.to_datetime.iso8601).to eq("2024-05-31T23:59:59Z")
expect(new_boundaries.charges_from_datetime.iso8601).to eq("2024-05-01T00:00:00Z")
expect(new_boundaries.charges_to_datetime.iso8601).to eq("2024-05-31T23:59:59Z")
expect(new_boundaries.fixed_charges_from_datetime.iso8601).to eq("2024-05-01T00:00:00Z")
expect(new_boundaries.fixed_charges_to_datetime.iso8601).to eq("2024-05-31T23:59:59Z")
end
end
end
Expand Down
Loading
Loading