mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-01 02:03:22 +00:00
Adjustments now have state instead of locked/unlocked
This commit is contained in:
committed by
Rob Harrington
parent
b5d33fc4b5
commit
38da4c8e12
@@ -71,7 +71,7 @@ class BillablePeriod < ActiveRecord::Base
|
||||
source: self,
|
||||
originator: nil, # enterprise.package
|
||||
mandatory: true,
|
||||
locked: false
|
||||
state: 'closed'
|
||||
}
|
||||
end
|
||||
end
|
||||
|
||||
@@ -54,19 +54,6 @@ class EnterpriseFee < ActiveRecord::Base
|
||||
order.adjustments.where(originator_type: 'EnterpriseFee').destroy_all
|
||||
end
|
||||
|
||||
# Create an adjustment that starts as locked. Preferable to making an adjustment and locking it since
|
||||
# the unlocked adjustment tends to get hit by callbacks before we have a chance to lock it.
|
||||
def create_locked_adjustment(label, target, calculable, mandatory=false)
|
||||
amount = compute_amount(calculable)
|
||||
return if amount == 0 && !mandatory
|
||||
target.adjustments.create({ :amount => amount,
|
||||
:source => calculable,
|
||||
:originator => self,
|
||||
:label => label,
|
||||
:mandatory => mandatory,
|
||||
:locked => true}, :without_protection => true)
|
||||
end
|
||||
|
||||
|
||||
private
|
||||
|
||||
|
||||
@@ -17,7 +17,7 @@ class ProductDistribution < ActiveRecord::Base
|
||||
end
|
||||
|
||||
def create_adjustment_for(line_item)
|
||||
a = enterprise_fee.create_locked_adjustment(adjustment_label_for(line_item), line_item.order, line_item, true)
|
||||
a = enterprise_fee.create_adjustment(adjustment_label_for(line_item), line_item.order, line_item, true)
|
||||
AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: 'distributor'
|
||||
end
|
||||
|
||||
|
||||
@@ -60,7 +60,7 @@ Spree::LineItem.class_eval do
|
||||
end
|
||||
|
||||
def price_with_adjustments
|
||||
# EnterpriseFee#create_locked_adjustment applies adjustments on line items to their parent order,
|
||||
# EnterpriseFee#create_adjustment applies adjustments on line items to their parent order,
|
||||
# so line_item.adjustments returns an empty array
|
||||
return 0 if quantity == 0
|
||||
(price + order.adjustments.where(source_id: id).sum(&:amount) / quantity).round(2)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
module OpenFoodNetwork
|
||||
class EnterpriseFeeApplicator < Struct.new(:enterprise_fee, :variant, :role)
|
||||
def create_line_item_adjustment(line_item)
|
||||
a = enterprise_fee.create_locked_adjustment(line_item_adjustment_label, line_item.order, line_item, true)
|
||||
a = enterprise_fee.create_adjustment(line_item_adjustment_label, line_item.order, line_item, true)
|
||||
|
||||
AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: role
|
||||
|
||||
@@ -9,7 +9,7 @@ module OpenFoodNetwork
|
||||
end
|
||||
|
||||
def create_order_adjustment(order)
|
||||
a = enterprise_fee.create_locked_adjustment(order_adjustment_label, order, order, true)
|
||||
a = enterprise_fee.create_adjustment(order_adjustment_label, order, order, true)
|
||||
|
||||
AdjustmentMetadata.create! adjustment: a, enterprise: enterprise_fee.enterprise, fee_name: enterprise_fee.name, fee_type: enterprise_fee.fee_type, enterprise_role: role
|
||||
|
||||
|
||||
@@ -170,7 +170,7 @@ describe EnterpriseFee do
|
||||
order.adjustments.create({:amount => 12.34,
|
||||
:source => order,
|
||||
:originator => tax_rate,
|
||||
:locked => true,
|
||||
:state => 'closed',
|
||||
:label => 'hello' }, :without_protection => true)
|
||||
|
||||
expect do
|
||||
|
||||
@@ -78,7 +78,7 @@ describe ProductDistribution do
|
||||
it "returns the adjustment when present" do
|
||||
pd = create(:product_distribution)
|
||||
line_item = create(:line_item)
|
||||
adjustment = pd.enterprise_fee.create_locked_adjustment('foo', line_item.order, line_item, true)
|
||||
adjustment = pd.enterprise_fee.create_adjustment('foo', line_item.order, line_item, true)
|
||||
|
||||
pd.send(:adjustment_for, line_item).should == adjustment
|
||||
end
|
||||
@@ -86,8 +86,8 @@ describe ProductDistribution do
|
||||
it "raises an error when there are multiple adjustments for this enterprise fee" do
|
||||
pd = create(:product_distribution)
|
||||
line_item = create(:line_item)
|
||||
pd.enterprise_fee.create_locked_adjustment('one', line_item.order, line_item, true)
|
||||
pd.enterprise_fee.create_locked_adjustment('two', line_item.order, line_item, true)
|
||||
pd.enterprise_fee.create_adjustment('one', line_item.order, line_item, true)
|
||||
pd.enterprise_fee.create_adjustment('two', line_item.order, line_item, true)
|
||||
|
||||
expect do
|
||||
pd.send(:adjustment_for, line_item)
|
||||
|
||||
@@ -140,7 +140,7 @@ describe Spree::Order do
|
||||
it "returns the sum of eligible enterprise fee adjustments" do
|
||||
ef = create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new )
|
||||
ef.calculator.set_preference :amount, 123.45
|
||||
a = ef.create_locked_adjustment("adjustment", o, o, true)
|
||||
a = ef.create_adjustment("adjustment", o, o, true)
|
||||
|
||||
o.admin_and_handling_total.should == 123.45
|
||||
end
|
||||
@@ -148,7 +148,7 @@ describe Spree::Order do
|
||||
it "does not include ineligible adjustments" do
|
||||
ef = create(:enterprise_fee, calculator: Spree::Calculator::FlatRate.new )
|
||||
ef.calculator.set_preference :amount, 123.45
|
||||
a = ef.create_locked_adjustment("adjustment", o, o, true)
|
||||
a = ef.create_adjustment("adjustment", o, o, true)
|
||||
|
||||
a.update_column :eligible, false
|
||||
|
||||
|
||||
Reference in New Issue
Block a user