Skip to content

Commit

Permalink
Close #769 discount maximum cap
Browse files Browse the repository at this point in the history
  • Loading branch information
kimsrung committed Nov 16, 2023
1 parent 418d1c1 commit 729aaa7
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
15 changes: 15 additions & 0 deletions app/models/spree_cm_commissioner/order_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def self.prepended(base)
base.delegate :customer, to: :subscription, allow_nil: true

base.whitelisted_ransackable_associations |= %w[customer taxon payments]
base.register_update_hook :adjust_discount_by_cap

def base.search_by_qr_data!(data)
token = data.match(/^R\d{9,}-([A-Za-z0-9_\-]+)$/)&.captures
Expand All @@ -39,6 +40,20 @@ def base.search_by_qr_data!(data)
end
end

def adjust_discount_by_cap
promotion_adjustment = adjustments.promotion.eligible.first

return if promotion_adjustment.blank? # Check if the order has a promotion adjustment

promotion = promotion_adjustment.source.promotion

return unless promotion&.cap # Check if cap exist

negative_cap = (promotion.cap * -1).to_d # Convert cap to negative number to compare

updater.update_adjustment_total_by_cap(negative_cap) if adjustment_total < negative_cap
end

def state_changed_to_complete?
saved_change_to_state? && state == 'complete'
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/spree_cm_commissioner/order_updater_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ def update_item_total
order.item_total = line_items.sum(&:amount)
update_order_total
end

def update_adjustment_total_by_cap(cap)
update_adjustment_total
order.adjustment_total = order.promo_total = cap

update_order_total
end
end
end

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!-- insert_before "erb[loud]:contains('field_container :usage_limit')" -->

<div id="cap_field" class="form-group">
<%= f.field_container :cap do %>
<%= f.label :cap %>
<div class="input-group">
<div class="input-group-prepend">
<span class="input-group-text"><%= currency_symbol(current_currency) %></span>
</div>
<%= f.text_field :cap, value: number_to_currency(@promotion.cap, unit: ''), class: 'form-control' %>
</div>
<% end %>
</div>
5 changes: 5 additions & 0 deletions db/migrate/20231114015730_add_cap_to_spree_promotion.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCapToSpreePromotion < ActiveRecord::Migration[7.0]
def change
add_column :spree_promotions, :cap, :float, if_not_exists: true
end
end
56 changes: 56 additions & 0 deletions spec/models/spree/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,4 +299,60 @@
end
end
end

describe '#adjust_discount_by_cap' do
let(:calculator) { Spree::Calculator::FlatPercentItemTotal.create(preferred_flat_percent: 50) }
let!(:order) { create(:order_with_totals, line_items_price: 30, total: 30, item_total: 30) }

context 'when order has a promotion with a cap' do
let!(:promotion) { create(:promotion_with_order_adjustment, cap: 20, starts_at: Date.today - 1.day, expires_at: Date.today + 5.day, match_policy: "any")}
let!(:promotion_action) { Spree::Promotion::Actions::CreateAdjustment.create!(promotion: promotion, calculator: calculator) }
let!(:order_promotion) { create(:order_promotion, order: order, promotion: promotion)}
let!(:order_adjustment) { create(:adjustment, order: order, source: promotion_action, adjustable: order)}

it 'does not adjust promotion adjustment if within the cap' do
order.reload.update_with_updater!

expect(order.item_total.to_f).to eq(30.0)
expect(order.total.to_f).to eq(15.0)
expect(order.adjustment_total.to_f).to eq(-15.0)
end

it 'adjusts promotion adjustment to cap if exceeded' do
create(:line_item, order: order, price: 100)

order.reload.update_with_updater!

expect(order.item_total.to_f).to eq(130.0)
expect(order.total.to_f).to eq(110.0)
expect(order.adjustment_total.to_f).to eq(-20.0)
end
end

context 'when order has a promotion without a cap' do
let!(:promotion) { create(:promotion_with_order_adjustment, starts_at: Date.today - 1.day, expires_at: Date.today + 5.day, match_policy: "any")}
let!(:promotion_action) { Spree::Promotion::Actions::CreateAdjustment.create!(promotion: promotion, calculator: calculator) }
let!(:order_promotion) { create(:order_promotion, order: order, promotion: promotion)}
let!(:order_adjustment) { create(:adjustment, order: order, source: promotion_action, adjustable: order)}

it 'adjust promotion adjustment' do
order.reload.update_with_updater!

expect(order.item_total.to_f).to eq(30.0)
expect(order.total.to_f).to eq(15.0)
expect(order.adjustment_total.to_f).to eq(-15.0)
end

it 'adjusts promotion adjustment 50' do
create(:line_item, order: order, price: 100)

order.reload.update_with_updater!
order.update_totals

expect(order.item_total.to_f).to eq(130.0)
expect(order.total.to_f).to eq(65.0)
expect(order.adjustment_total.to_f).to eq(-65.0)
end
end
end
end

0 comments on commit 729aaa7

Please sign in to comment.