-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Working untested version for 1-1-stable
- Loading branch information
Showing
5 changed files
with
57 additions
and
46 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1,26 +1,36 @@ | ||
Spree::Order.class_eval do | ||
|
||
def core_charges | ||
adjustments.where(:source_type => 'Spree::LineItem') | ||
end | ||
|
||
# Called each time an orderis updated | ||
def create_core_charges | ||
applicable_charges, charges_to_destroy = self.core_charges.partition{|cc| cc.originator && cc.originator.eligible? } | ||
charges_to_destroy.each(&:delete) | ||
line_item_with_core_charge_ids = applicable_charges.map(&:source_id) | ||
|
||
self.line_items.reload.each do |li| | ||
next unless li.product.core_amount | ||
|
||
self.core_charges << Spree::Adjustment.create({ | ||
:label => "#{I18n.t(:core_charge)} [#{li.variant.sku}]", | ||
:source => li, | ||
:adjustable => self, | ||
:originator => li, | ||
:amount => li.calculate_core_charge, | ||
:eligible => true | ||
}) unless line_item_with_core_charge_ids.include?(li.id) | ||
to_keep, to_destroy = adjustments.core.partition{|x| x.originator and x.originator.eligible?} | ||
to_destroy.each(&:delete) | ||
core_variant_ids = to_keep.map{|x| x.originator.variant_id } | ||
|
||
Spree::Adjustment.skip_callback :save, :after, :update_adjustable | ||
|
||
line_items.each do |li| | ||
next unless li.product.core_amount # The product has no core charge associated with it | ||
next if core_variant_ids.include? li.variant_id # The charge for this line item already exists | ||
|
||
new_core_charge = Spree::Adjustment.new | ||
new_core_charge.label = "#{I18n.t(:core_charge)} [#{li.variant.sku}]" | ||
new_core_charge.source = li | ||
new_core_charge.adjustable = self | ||
new_core_charge.originator = li | ||
new_core_charge.amount = li.calculate_core_charge | ||
new_core_charge.eligible = true | ||
|
||
new_core_charge.save | ||
end | ||
|
||
Spree::Adjustment.set_callback :save, :after, :update_adjustable | ||
|
||
update_totals | ||
|
||
update_attributes_without_callbacks({ | ||
:adjustment_total => adjustment_total, | ||
:total => total | ||
}) | ||
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 |
---|---|---|
@@ -1,20 +1,2 @@ | ||
require 'spree_core' | ||
|
||
module Spree | ||
module CoreCharges | ||
class Engine < Rails::Engine | ||
isolate_namespace Spree | ||
engine_name 'spree_core_charges' | ||
|
||
def self.activate | ||
Dir.glob(File.join(File.dirname(__FILE__), "../app/**/*_decorator*.rb")) do |c| | ||
Rails.configuration.cache_classes ? require(c) : load(c) | ||
end | ||
Order.register_update_hook('create_core_charges') | ||
end | ||
|
||
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/overrides) | ||
config.to_prepare &method(:activate).to_proc | ||
end | ||
end | ||
end | ||
require 'spree_core_charges/engine' |
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,19 @@ | ||
module Spree | ||
module CoreCharges | ||
class Engine < Rails::Engine | ||
isolate_namespace Spree | ||
engine_name 'spree_core_charges' | ||
|
||
def self.activate | ||
Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c| | ||
Rails.configuration.cache_classes ? require(c) : load(c) | ||
end | ||
|
||
Spree::Order.register_update_hook(:create_core_charges) | ||
end | ||
|
||
config.autoload_paths += %W(#{config.root}/lib #{config.root}/app/overrides) | ||
config.to_prepare &method(:activate).to_proc | ||
end | ||
end | ||
end |
Empty file.