Browse Source

WIP: Add shipcloud/ups

master
Harald Welte 7 months ago
parent
commit
a6e25cda35
  1. 121
      app/models/spree/calculator/sysmocom_ups_calculator.rb
  2. 5
      lib/sysmocom_dhl/engine.rb
  3. 3
      sysmocom_dhl.gemspec

121
app/models/spree/calculator/sysmocom_ups_calculator.rb

@ -0,0 +1,121 @@
# AGPLv3 sysmocom s.f.m.c. GmbH
module Spree
class Calculator::SysmocomScBaseCalculator < Calculator
def total_value_no_tax(o)
item_total = o.line_items.map(&:amount).sum
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
end
return o.ship_address.country.iso
end
def extract_dst_dadr(o)
if o.kind_of?(Spree::Shipment)
return o.address
end
return o.ship_address
end
def cbrt(x)
return x ** (1.0/3)
end
def estimate_dimensions(weight_kg, density_kg_per_dm3)
volume_dm3 = weight_kg.to_f / density_kg_per_dm3.to_f
volume_cm3 = 1000.0 * volume_dm3
# assuming l=3x, w=2x, h1x -> x=6
x = cbrt(volume_cm3 / 6)
return 3.0 * x, 2.0 * x, 1.0 * x
end
def sc_compute(carrier, service, object)
# This could be more than an order but not right now
weight = total_weight(object)
dst = extract_dst_addr(object)
length, width, height = estimate_dimensions(weight, 0.5)
quote = Shipcloud::ShipmentQuote.create(
carrier: carrier,
service: service,
to: {
street: dst.address1,
zip_code: dst.zipcode,
city: dst.city,
country: dst.country.iso,
},
from: {
street: "Alt-Moabit",
street_no: "93",
zip_code: "10559",
city: "Berlin",
country: "DE",
},
package: {
weight: weight,
width: width,
length: length,
height: height,
},
)
return quote.price
end
def available?(object)
# use 'compute' to determine availability
begin
price = sc_compute(object)
rescue =>
return false
else
return true
end
end
end
class Calculator::SysmocomScUpsStdCalculator < SysmocomScBaseCalculator
def self.description
I18n.t(:sysmocom_ups_standard)
end
def compute(object)
return sc_compute('ups', 'standard')
end
end
class Calculator::SysmocomScUpsExpressCalculator < SysmocomScBaseCalculator
def self.description
I18n.t(:sysmocom_ups_express)
end
def compute(object)
return sc_compute('ups', 'one_day')
end
end
class Calculator::SysmocomScUpsExpeditedCalculator < SysmocomScBaseCalculator
def self.description
I18n.t(:sysmocom_ups_expedited)
end
def compute(object)
return sc_compute('ups', 'ups_expedited')
end
end
end

5
lib/sysmocom_dhl/engine.rb

@ -21,7 +21,10 @@ module SysmocomDhl
app.config.spree.calculators.shipping_methods += [
# ActiveSupport magic to search a model...
Spree::Calculator::SysmocomValueCalculator,
Spree::Calculator::SysmocomMailValueCalculator
Spree::Calculator::SysmocomMailValueCalculator,
Spree::Calculator::SysmocomScUpsStdCalculator,
Spree::Calculator::SysmocomScUpsExpressCalculator,
Spree::Calculator::SysmocomScUpsExpeditedCalculator,
]
end

3
sysmocom_dhl.gemspec

@ -1,7 +1,7 @@
Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
s.name = 'sysmocom_dhl'
s.version = '1.2.0'
s.version = '2.0.0'
s.summary = 'sysmocom_dhl shipping helper'
s.description = 'Deal with shipping cost, zones, insurance'
s.required_ruby_version = '>= 1.9.1'
@ -18,6 +18,7 @@ Gem::Specification.new do |s|
s.requirements << 'none'
s.add_dependency('spree_core', '>= 1.2.0')
s.add_dependency('shipcloud')
s.add_development_dependency 'capybara', '1.0.1'
s.add_development_dependency 'factory_girl', '~> 2.6.4'

Loading…
Cancel
Save