Skip to content

Instantly share code, notes, and snippets.

@pjmartorell
Created August 25, 2017 11:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjmartorell/ee233eae64460c3087fc95979bef769e to your computer and use it in GitHub Desktop.
Save pjmartorell/ee233eae64460c3087fc95979bef769e to your computer and use it in GitHub Desktop.
module Delivery
class PackageFittingChecker
def self.fit?(shipping_method_id, quantities_with_max_units_and_shipping_package_categories)
case shipping_method_id
when "correos", "d_post"
max_units_per_package = :max_units_per_correos_package
when "ups_envelope"
max_units_per_package = :max_units_per_ups_package
when "dhl", "ups_express_saver", "ups_standard"
return true
else
raise "Unknown shipping method!"
end
return false unless quantities_with_max_units_and_shipping_package_categories.map{|elem| elem[:shipping_package_category]}.compact.uniq.size == 1
sum = 0
quantities_with_max_units_and_shipping_package_categories.each do |elem|
return false unless elem[max_units_per_package] > 0
sum += filling_ratio(elem[:quantity], elem[max_units_per_package])
return false if sum > 100
end
true
end
private
def self.filling_ratio(quantity, max_units)
(quantity / max_units.to_f) * 100
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment