-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
390 additions
and
13 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
app/models/spree_cm_commissioner/calculator/flat_percent_item_total_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module SpreeCmCommissioner | ||
module Calculator | ||
module FlatPercentItemTotalDecorator | ||
def compute(object) | ||
computed_amount = (object.amount * preferred_flat_percent / 100).round(2) | ||
computed_amount = cap if cap && computed_amount > cap | ||
|
||
[computed_amount, object.amount].min | ||
end | ||
end | ||
end | ||
end | ||
|
||
unless Spree::Calculator::FlatPercentItemTotal.included_modules.include?(SpreeCmCommissioner::Calculator::FlatPercentItemTotalDecorator) | ||
Spree::Calculator::FlatPercentItemTotal.prepend(SpreeCmCommissioner::Calculator::FlatPercentItemTotalDecorator) | ||
end |
16 changes: 16 additions & 0 deletions
16
app/models/spree_cm_commissioner/calculator/percent_on_line_item_decorator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
module SpreeCmCommissioner | ||
module Calculator | ||
module PercentOnLineItemDecorator | ||
def compute(object) | ||
computed_amount = (object.amount * preferred_percent / 100).round(2) | ||
computed_amount = cap if cap && computed_amount > cap | ||
|
||
[computed_amount, object.amount].min | ||
end | ||
end | ||
end | ||
end | ||
|
||
unless Spree::Calculator::PercentOnLineItem.included_modules.include?(SpreeCmCommissioner::Calculator::PercentOnLineItemDecorator) | ||
Spree::Calculator::PercentOnLineItem.prepend(SpreeCmCommissioner::Calculator::PercentOnLineItemDecorator) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
app/overrides/spree/admin/orders/_adjustments/adjustments.html.erb.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<!-- replace "[data-hook='order_details_adjustments']" --> | ||
<% eligible_adjustments = adjustments.eligible%> | ||
<tbody id="order-charges"> | ||
<% eligible_adjustments.group_by(&:label).each do |label, adjustments| %> | ||
<tr class="total"> | ||
<td> | ||
<strong><%= label %>:</strong> | ||
</td> | ||
<td class="total"> | ||
<span> | ||
<%= Spree::Money.new( | ||
adjustments.sum(&:amount), | ||
currency: adjustments.first.order.try(:currency) | ||
) %> | ||
|
||
<% if eligible_adjustments.promotion.present? %> | ||
~ (Cap | ||
<%= Spree::Money.new( | ||
eligible_adjustments.promotion.first.source.calculator&.cap, | ||
currency: adjustments.first.order.try(:currency) | ||
) %>) | ||
<%end%> | ||
</span> | ||
</td> | ||
</tr> | ||
<% end %> | ||
</tbody> |
13 changes: 13 additions & 0 deletions
13
app/overrides/spree/admin/promotions/_form/cap_field.html.erb.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
11 changes: 11 additions & 0 deletions
11
app/overrides/spree/admin/promotions/actions/_create_adjustment/cap.html.erb.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- insert_after ".calculator-fields > .row > .settings" --> | ||
|
||
<div class="form-group col-12 col-md-6 mt-3 mb-0" id="cap_field"> | ||
<%= label_tag "#{param_prefix}[calculator_attributes][cap]", "Cap" %> | ||
<div class="input-group"> | ||
<div class="input-group-prepend"> | ||
<span class="input-group-text"><%= currency_symbol(current_currency) %></span> | ||
</div> | ||
<%= text_field_tag "#{param_prefix}[calculator_attributes][cap]", number_to_currency(promotion_action.calculator.cap, unit: ''), class: 'form-control' %> | ||
</div> | ||
</div> |
11 changes: 11 additions & 0 deletions
11
app/overrides/spree/admin/promotions/actions/_create_item_adjustments/cap.html.erb.deface
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!-- insert_after ".calculator-fields > .row > .settings" --> | ||
|
||
<div class="form-group col-12 col-md-6 mt-3 mb-0" id="cap_field"> | ||
<%= label_tag "#{param_prefix}[calculator_attributes][cap]", "Cap" %> | ||
<div class="input-group"> | ||
<div class="input-group-prepend"> | ||
<span class="input-group-text"><%= currency_symbol(current_currency) %></span> | ||
</div> | ||
<%= text_field_tag "#{param_prefix}[calculator_attributes][cap]", number_to_currency(promotion_action.calculator.cap, unit: ''), class: 'form-control' %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AddCapToSpreeCalculator < ActiveRecord::Migration[7.0] | ||
def change | ||
add_column :spree_calculators, :cap, :float, if_not_exists: true | ||
end | ||
end |
47 changes: 47 additions & 0 deletions
47
spec/models/spree/calculator/flat_percent_item_total_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Spree::Calculator::FlatPercentItemTotal, type: :model do | ||
let(:calculator) { Spree::Calculator::FlatPercentItemTotal.new } | ||
let(:order) { create(:order) } | ||
|
||
before { allow(calculator).to receive_messages preferred_flat_percent: 10 } | ||
|
||
describe '#compute' do | ||
context 'without cap' do | ||
it 'rounds result correctly' do | ||
allow(order).to receive_messages amount: 31.08 | ||
expect(calculator.compute(order)).to eq 3.11 | ||
|
||
allow(order).to receive_messages amount: 31.00 | ||
expect(calculator.compute(order)).to eq 3.10 | ||
end | ||
|
||
it 'returns object.amount if computed amount is greater' do | ||
allow(order).to receive_messages amount: 30.00 | ||
allow(calculator).to receive_messages preferred_flat_percent: 110 | ||
|
||
expect(calculator.compute(order)).to eq 30.0 | ||
end | ||
end | ||
|
||
context 'with cap' do | ||
before do | ||
calculator.update(cap: 10) | ||
end | ||
|
||
it 'returns cap if computed amount is greater' do | ||
allow(order).to receive_messages amount: 50.00 | ||
allow(calculator).to receive_messages preferred_flat_percent: 50 | ||
|
||
expect(calculator.compute(order)).to eq 10.00 | ||
end | ||
|
||
it 'returns computed amount if cap is greater' do | ||
allow(order).to receive_messages amount: 15.00 | ||
allow(calculator).to receive_messages preferred_flat_percent: 50 | ||
|
||
expect(calculator.compute(order)).to eq 7.50 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
require 'spec_helper' | ||
|
||
RSpec.describe Spree::Calculator::PercentOnLineItem, type: :model do | ||
let(:calculator) { Spree::Calculator::PercentOnLineItem.new } | ||
let(:line_item) { create(:line_item) } | ||
|
||
before { allow(calculator).to receive_messages preferred_percent: 10 } | ||
|
||
describe '#compute' do | ||
context 'without cap' do | ||
it 'rounds result correctly' do | ||
allow(line_item).to receive_messages amount: 31.08 | ||
expect(calculator.compute(line_item)).to eq 3.11 | ||
|
||
allow(line_item).to receive_messages amount: 31.00 | ||
expect(calculator.compute(line_item)).to eq 3.10 | ||
end | ||
|
||
it 'returns object.amount if computed amount is greater' do | ||
allow(line_item).to receive_messages amount: 30.00 | ||
allow(calculator).to receive_messages preferred_percent: 110 | ||
|
||
expect(calculator.compute(line_item)).to eq 30.0 | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.