Created
August 25, 2017 11:45
-
-
Save pjmartorell/ee233eae64460c3087fc95979bef769e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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