Weight based mail_value_calculator

Compute shipping/franking cost based on sum of weight in package
This commit is contained in:
Harald Welte 2019-01-15 18:41:41 +01:00
parent 60bb99fb80
commit 0917fbf514
1 changed files with 55 additions and 13 deletions

View File

@ -12,6 +12,14 @@ module Spree
item_total
end
def total_weight(o)
weight = 0
o.line_items.each do |li|
weight += li.quantity * li.variant.weight
end
return weight
end
def extract_iso_code(o)
if o.kind_of?(Spree::Shipment)
return o.address.country.iso
@ -19,26 +27,60 @@ module Spree
return o.ship_address.country.iso
end
def postage_for_weight_DE(weight)
# Grossbrief Einschreiben: 3.95; Maxibrief Einschreiben: 5.10 / 7.30
if weight < 500
return 3.95
elsif weight < 1000
return 5.10
elsif weight < 2000
return 7.30
else
return 99999
end
end
def postage_for_weight_INTL(weight)
# Warenpost International Unterschrift S: 6.20 / M: 9.50 / L: 19.50
if weight < 500
return 6.20
elsif weight < 1000
return 9.50
elsif weight < 2000
return 19.50
else
return 99999
end
end
def self.available?(object)
# Letters have a maximum weight of 2kg
weight = total_weight(object)
if weight < 2000
true
else
false
end
end
def compute(object)
# This could be more than an order but not right now
shipping_value = total_value_no_tax(object)
weight = total_weight(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
base = postage_for_weight_DE(weight)
else
base = postage_for_weight_INTL(weight)
# We need to add insurance..
if shipping_value > 20
# Wert Intenational: 2.50 + 2 EUR je 100 EUR Haftungssumme
base += 2.5 + (((shipping_value.round / 100) + 1) * 2)
end
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)
return base
end
end