Skip to content
Merged
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
32 changes: 27 additions & 5 deletions app/models/membership_pricing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,25 @@ def baskets_price_extras

comp_prices = [ 0, 0 ]
complements_prices.each { |p|
comp_prices = comp_prices.zip(p.map(&:round_to_five_cents)).map(&:sum)
comp_prices = comp_prices.zip(rounded_prices(p)).map(&:sum)
}

[
deliveries_counts.min * calculate_price_extra(extra, basket_size, comp_prices.min / deliveries_counts.min, deliveries_counts.min),
deliveries_counts.max * calculate_price_extra(extra, basket_size, comp_prices.max / deliveries_counts.max, deliveries_counts.max)
extra_total(deliveries_counts.min, extra, comp_prices.min),
extra_total(deliveries_counts.max, extra, comp_prices.max)
]
end

def extra_total(deliveries_count, extra, complements_price)
return 0 unless deliveries_count&.positive?

deliveries_count * calculate_price_extra(
extra,
basket_size,
complements_price / deliveries_count,
deliveries_count)
end

def calculate_price_extra(extra, basket_size, complements_price, deliveries_count)
return 0 if extra.to_f.zero?
return 0 unless basket_size
Expand All @@ -96,10 +106,13 @@ def complement_prices(complement_id, quantity)
complement = BasketComplement.find_by(id: complement_id)
return [ 0, 0 ] unless complement
return [ 0, 0 ] if quantity.zero?
return [ 0, 0 ] unless delivery_cycles.any?

deliveries_counts = delivery_cycles.map { |dc|
dc.billable_deliveries_count_for_basket_complement(complement)
}.uniq
return [ 0, 0 ] if deliveries_counts.empty? || deliveries_counts.any?(&:nil?)

[
deliveries_counts.min * complement.price * quantity,
deliveries_counts.max * complement.price * quantity
Expand All @@ -109,8 +122,11 @@ def complement_prices(complement_id, quantity)
def activity_participations_prices
return [ 0, 0 ] unless @params[:activity_participations_demanded_annually]
return [ 0, 0 ] unless basket_size
return [ 0, 0 ] unless delivery_cycles.any?

fy = Delivery.last&.fiscal_year
return [ 0, 0 ] unless fy

fy = Delivery.last.fiscal_year
counts = delivery_cycles.map { |dc|
m = Membership.new(
started_on: fy.beginning_of_year,
Expand All @@ -126,6 +142,8 @@ def activity_participations_prices
demanded = ActivityParticipationDemanded.new(m).count
-1 * (demanded - default) * Current.org.activity_price
}
return [ 0, 0 ] if counts.empty? || counts.any?(&:nil?)

[ counts.min, counts.max ]
end

Expand Down Expand Up @@ -185,6 +203,10 @@ def depot

def add(prices)
@min, @max =
[ @min, @max ].zip(prices.map(&:round_to_five_cents)).map(&:sum)
[ @min, @max ].zip(rounded_prices(prices)).map(&:sum)
end

def rounded_prices(prices)
Array(prices).map { |price| (price || 0).round_to_five_cents }
end
end
35 changes: 35 additions & 0 deletions test/models/membership_pricing_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,41 @@ def pricing(params = {})
assert_equal [ 140 + (2 + 1) * 50 ], pricing.prices
end

test "does not raise when activity participations are set but no delivery cycles apply" do
org(
activity_participations_form_min: 0,
activity_participations_form_max: 10,
activity_price: 50)

depot = depots(:farm)
depot.delivery_cycles.clear

pricing = pricing(
waiting_basket_size_id: small_id,
waiting_depot_id: depot.id,
waiting_delivery_cycle_id: "",
waiting_basket_price_extra: "1.0",
waiting_activity_participations_demanded_annually: "10")

assert_equal [ 0 ], pricing.prices
assert_not pricing.present?
end

test "does not raise when activity participations are set without depot or cycle" do
org(
activity_participations_form_min: 0,
activity_participations_form_max: 10,
activity_price: 50)
DeliveryCycle.visible.each { |cycle| cycle.update!(depots: []) }

pricing = pricing(
waiting_basket_size_id: small_id,
waiting_delivery_cycle_id: "",
waiting_activity_participations_demanded_annually: "10")

assert_equal [ 0 ], pricing.prices
end

test "basket size with availability restrictions reduces deliveries count" do
# Small basket is 10 price, mondays has 10 deliveries = 100
pricing = pricing(
Expand Down