Add #included and #additional scopes to Spree::Adjustment

We can now do things like:
```
included_tax = order.adjustments.tax.included.sum(:amount)
additional_tax = order.adjustments.tax.additional.sum(:amount)
```
This commit is contained in:
Matt-Yorkley
2020-12-27 12:32:09 +00:00
parent 66bf69b52c
commit 8be18cd05c
2 changed files with 31 additions and 0 deletions

View File

@@ -70,6 +70,8 @@ module Spree
scope :charge, -> { where('amount >= 0') }
scope :credit, -> { where('amount < 0') }
scope :return_authorization, -> { where(source_type: "Spree::ReturnAuthorization") }
scope :included, -> { where(included: true) }
scope :additional, -> { where(included: false) }
scope :enterprise_fee, -> { where(originator_type: 'EnterpriseFee') }
scope :admin, -> { where(source_type: nil, originator_type: nil) }

View File

@@ -460,5 +460,34 @@ module Spree
context "extends LocalizedNumber" do
it_behaves_like "a model using the LocalizedNumber module", [:amount]
end
describe "included and additional scopes" do
let!(:zone) { create(:zone_with_member) }
let(:order) { create(:order) }
let(:included_in_price) { true }
let(:tax_rate) {
create(:tax_rate, included_in_price: included_in_price,
calculator: ::Calculator::FlatRate.new(preferred_amount: 0.1))
}
let(:included) { true }
let(:adjustment) {
create(:adjustment, adjustable: order, source: order,
originator: tax_rate, included: included)
}
context "when tax is included in price" do
it "is returned by the #included scope" do
expect(Spree::Adjustment.included).to eq [adjustment]
end
end
context "when tax is additional to the price" do
let(:included) { false }
it "is returned by the #additional scope" do
expect(Spree::Adjustment.additional).to eq [adjustment]
end
end
end
end
end