diff --git a/app/models/spree/calculator/sysmocom_value_calculator.rb b/app/models/spree/calculator/sysmocom_value_calculator.rb new file mode 100644 index 0000000..3876762 --- /dev/null +++ b/app/models/spree/calculator/sysmocom_value_calculator.rb @@ -0,0 +1,189 @@ +# AGPLv3 sysmocom s.f.m.c. GmbH + +puts "Fooo" + +module Spree + class Calculator::SysmocomValueCalculator < Calculator + + def self.description + I18n.t(:sysmocom_value_based) + end + + ZONE1 = { + "BE" => 1, + "BG" => 1, + "DK" => 1, + "EE" => 1, + "FI" => 1, + "FR" => 1, + "GR" => 1, + "GB" => 1, + "IE" => 1, + "IT" => 1, + "LV" => 1, + "LU" => 1, + "MT" => 1, + "MC" => 1, + "NL" => 1, + "AT" => 1, + "PL" => 1, + "PT" => 1, + "RO" => 1, + "SE" => 1, + "SK" => 1, + "SI" => 1, + "ES" => 1, + "CZ" => 1, + "HU" => 1, + "CY" => 1 + }.freeze() + +ZONE2 = { +#"Älandinseln (Finnland)" + "AD" => 1, + "AL" => 1, + "BY" => 1, +#Berg Athos + "BA" => 1, +#Campione d'Italia Italien +#Ceuta..Spanien + "FO" => 1, + "GE" => 1, + "GI" => 1, + "IS" => 1, +#Kanalinseln (Großbritannien) +#Kanarische Inseln (Spanien) +#Kosovo + "HR" => 1, + "LI" => 1, +#Livigno + "MK" => 1, + "MD" => 1, +#Melila (Spanien) +#Montenegro + "NO" => 1, + "CH" => 1, + "RU" => 1, + "SM" => 1, + "RS" => 1, + "TR" => 1, + "UA" => 1, + "VA" => 1, + }.freeze + +ZONE3 = { + "EG" => 1, + "DZ" => 1, + "AM" => 1, + "AZ" => 1, + "IL" => 1, + "JO" => 1, + "CA" => 1, + "KZ" => 1, + "LB" => 1, + "LY" => 1, + "MA" => 1, +#Palästinensische Gebiete + "PM" => 1, + "SY" => 1, + "TN" => 1, + "US" => 1, +}.freeze + + def packages_price(value, limit, price_each) + price = 0 + rest = value + + # It is intended that a package reaching the limit is counted twice + # Also a zero value should still be one package. + while rest >= 0 do + price += price_each + rest -= limit + end + + price + end + + def price_germany(value) + # 6 Euro and up to 500 Euro in Value for a 2kg package + # http://www.dhl.de/dhl-paket + return packages_price(value, 500, 6.0) + end + + def price_package_zone1(value) + # 17 Euro and up to 500 Euro in value for a 5kg package or + # 2kg registered parcel with 35 Euro of value... + # http://www.dhl.de/de/paket/pakete-versenden/weltweit-versenden/paket.html + if value < 35 + return packages_price(value, 35, 12) + else + return packages_price(value, 500, 17.0) + end + end + + def price_package_zone2(value) + # 5kg package + # TODO: for the last item we could check for 74.11 again + if value < 74.11 + return packages_price(value, 74.11, 32) + else + return packages_price(value, 500, 38) + end + end + + def price_package_zone3(value) + # 5kg package + # TODO see zone2 + if value < 74.11 + return packages_price(value, 74.11, 38) + else + return packages_price(value, 500, 54) + end + end + + def price_package_zone4(value) + if value < 74.11 + return packages_price(value, 74.11, 44) + else + return packages_price(value, 500, 66) + end + end + + + def total_value_no_tax(o) + value = o.line_items.inject(0) { |sum, item | + sum + (item.price * item.quantity)} + reductions = o.adjustments.eligible.inject(0) { |sum, adj| + if adj.originator_type == "Spree::ShippingMethod" + add = 0 + elsif adj.originator_type == "Spree::TaxRate" + add = 0 + else + add = adjustment.amount + end + sum + add} + value + reductions + end + + def compute(order) + # This could be more than an order but not right now + shipping_value = total_value_no_tax(order) + + # Go through the zones by name... + if order.ship_address.country.iso == 'DE' + return price_germany(shipping_value) + elsif ZONE1.has_key?(order.ship_address.country.iso) + return price_package_zone1(shipping_value) + elsif ZONE2.has_key?(order.ship_address.country.iso) + return price_package_zone2(shipping_value) + elsif ZONE3.has_key?(order.ship_address.country.iso) + return price_package_zone3(shipping_value) + else + return price_package_zone4(shipping_value) + end + end + + end +end + + diff --git a/config/locales/en.yml b/config/locales/en.yml index 38206c1..5dd637e 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -3,3 +3,4 @@ en: dhl: DHL dhl_label: "DHL Label (CSV)" dhls: DHL + sysmocom_value_based: sysmocom Value Based diff --git a/lib/sysmocom_dhl/engine.rb b/lib/sysmocom_dhl/engine.rb index 928572d..a130ebb 100644 --- a/lib/sysmocom_dhl/engine.rb +++ b/lib/sysmocom_dhl/engine.rb @@ -1,4 +1,4 @@ -module SpreeTest +module SysmocomDhl class Engine < Rails::Engine require 'spree/core' isolate_namespace Spree @@ -17,6 +17,13 @@ module SpreeTest end end + initializer "sysmocom_shipping_calculators" do |app| + app.config.spree.calculators.shipping_methods += [ + # ActiveSupport magic to search a model... + Spree::Calculator::SysmocomValueCalculator + ] + end + config.to_prepare &method(:activate).to_proc end end