sysmocom_dhl/app/models/spree/calculator/sysmocom_mail_value_calcula...

48 lines
1.2 KiB
Ruby

# AGPLv3 sysmocom s.f.m.c. GmbH
module Spree
class Calculator::SysmocomMailValueCalculator < Calculator
def self.description
I18n.t(:sysmocom_mail_value_based)
end
def total_value_no_tax(o)
item_total = o.line_items.map(&:amount).sum
item_total
end
def extract_iso_code(o)
if o.kind_of?(Spree::Shipment)
return o.address.country.iso
end
return o.ship_address.country.iso
end
def compute(object)
# This could be more than an order but not right now
shipping_value = total_value_no_tax(object)
iso_code = extract_iso_code(object)
# Warenpost International Unterschrift S: 6.20 / M: 9.50 / L: 19.50
base = 9.5
if iso_code == 'DE'
# Grossbrief Einschreiben: 3.95; Maxibrief Einschreiben: 5.10 / 7.30
base = 5.1
end
# We need to add insurance..
if shipping_value < 20
# Plain registered letter
return base
end
# Wert Intenational: 2.50 + 2 EUR je 100 EUR Haftungssumme
return base + 2.5 + (((shipping_value.round / 100) + 1) * 2)
end
end
end