Class: Shipping::YrcFreightLegacy
- Defined in:
- app/services/shipping/yrc_freight_legacy.rb
Overview
Constant Summary collapse
- COST_DISCREPANCY_THRESHOLD =
200.0- COST_DISCREPANCY_THRESHOLD_RATIO =
this is max ratio of discrepancy by total shipping cost
0.5- COST_DISCREPANCY_THRESHOLD_BY_TOTAL_VALUE_RATIO =
this is max ratio of discrepancy by total delivery value
0.05
Instance Attribute Summary
Attributes inherited from Base
#address, #address2, #address3, #address_residential, #attention_name, #billing_account, #billing_country, #billing_zip, #ci_comments, #city, #close_report_only, #cod_amount, #cod_collection_type, #company, #country, #currency_code, #data, #debug, #declared_value, #delivery_instructions, #delivery_total_value, #description, #discount_price, #dropoff_type, #email, #eta, #export_reason, #freight_class, #freightquote_authorization_url, #freightquote_client_id, #freightquote_client_secret, #freightquote_customer_code, #freightquote_events_url, #freightquote_rating_url, #freightquote_shipping_url, #freightquote_voiding_url, #handling_instructions, #has_loading_dock, #image_type, #include_first_class_mail_options, #insured_value, #is_construction_site, #is_trade_show, #label_type, #limited_access, #line_items, #master_tracking_number, #measure_height, #measure_length, #measure_units, #measure_width, #media_mail, #multiple_piece_shipping, #negotiated_rates, #package, #package_count, #package_sequence_number, #package_total, #packages, #packaging_type, #pay_type, #phone, #pickup_datetime, #pickup_instructions, #plain_response, #price, #rate_data, #reference_number_1, #reference_number_2, #reference_number_3, #reference_number_code_1, #reference_number_code_2, #required, #requires_appointment, #requires_inside_delivery, #requires_liftgate, #response, #return_to_address, #return_to_address2, #return_to_address3, #return_to_address_residential, #return_to_attention_name, #return_to_city, #return_to_company, #return_to_country, #return_to_email, #return_to_has_loading_dock, #return_to_is_construction_site, #return_to_is_trade_show, #return_to_limited_access, #return_to_name, #return_to_phone, #return_to_requires_appointment, #return_to_requires_inside_delivery, #return_to_requires_liftgate, #return_to_state, #return_to_zip, #rl_carriers_api_key, #rl_carriers_shipping_url, #saturday_delivery, #sender_address, #sender_address2, #sender_address3, #sender_address_residential, #sender_attention_name, #sender_city, #sender_company, #sender_country, #sender_email, #sender_has_loading_dock, #sender_is_construction_site, #sender_is_trade_show, #sender_limited_access, #sender_name, #sender_phone, #sender_requires_appointment, #sender_requires_inside_delivery, #sender_requires_liftgate, #sender_state, #sender_tax_identification_number, #sender_zip, #service_code, #service_type, #services, #ship_date, #shipengine_api_key, #shipengine_canadapost_account_id, #shipengine_canadapost_parent_account_number, #shipengine_canpar_account_id, #shipengine_dhl_express_account_id, #shipengine_fed_ex_account_id, #shipengine_fed_ex_ca_account_id, #shipengine_purolator_account_id, #shipengine_ups_account_id, #shipengine_ups_ca_account_id, #shipengine_usps_account_id, #shipper_address, #shipper_address2, #shipper_address3, #shipper_address_residential, #shipper_attention_name, #shipper_city, #shipper_company, #shipper_country, #shipper_email, #shipper_has_loading_dock, #shipper_is_construction_site, #shipper_is_trade_show, #shipper_limited_access, #shipper_name, #shipper_phone, #shipper_requires_appointment, #shipper_requires_inside_delivery, #shipper_requires_liftgate, #shipper_state, #shipper_zip, #signature_confirmation, #skip_png_download, #skip_rate_test, #special_instructions, #state, #tax_identification_number, #time_in_transit, #total_shipment_weight, #transaction_type, #weight, #weight_units, #zip
Instance Method Summary collapse
- #find_rates(logger = nil) ⇒ Object
-
#get_bol_pdf_file(bol_id) ⇒ Object
if successful enc_label_image = REXML::XPath.first(@response, "//soap:Envelope/soap:Body/GetBillOfLadingLabelPDFResponse/GetBillOfLadingLabelPDFResult/PdfDocument").text shipping_label_pdf_file = Tempfile.new("shipping_label") shipping_label_pdf_file.binmode shipping_label_pdf_file.write Base64.decode64(enc_label_image) shipping_label_pdf_file.rewind return shipping_label_pdf_file else err_msgs = [] REXML::XPath.first(@response, "//soap:Envelope/soap:Body/GetBillOfLadingLabelPDFResponse/GetBillOfLadingLabelPDFResult/Messages") do |err_msg| err_msgs << err_msg end msg = "GetBillOfLadingLabelPDF did not succeed: #')" if msg.blank? successful = false raise ShippingError, msg end end.
- #get_freight_class_from_package(package) ⇒ Object
- #label(return_label = false, logger = nil) ⇒ Object
Methods inherited from Base
#fedex, #initialize, #purolator, state_from_zip, #ups, #ups_freight
Constructor Details
This class inherits a constructor from Shipping::Base
Instance Method Details
#find_rates(logger = nil) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'app/services/shipping/yrc_freight_legacy.rb', line 14 def find_rates(logger=nil) logger ||= Rails.logger @required = [:zip, :country, :sender_state, :sender_zip, :sender_country, :packages] @required += [:yrc_freight_user_name, :yrc_freight_password, :yrc_freight_business_id, :insured_value] @insured_value = 50000.0 if @insured_value.to_f > 50000.0 # UPS limits this to 50000.0 for domestic @country ||= 'US' country_iso3 = "USA" if @country == "US" country_iso3 = "CAN" if @country == "CA" @sender_country ||= 'US' sender_country_iso3 = "USA" if @sender_country == "US" sender_country_iso3 = "CAN" if @sender_country == "CA" shipper_country_iso3 = "USA" if @shipper_country == "US" shipper_country_iso3 = "CAN" if @shipper_country == "CA" @yrc_freight_rating_url ||= "https://api.yrc.com/node/api/ratequote" state = STATES.has_value?(@state.downcase) ? STATES.index(@state.downcase).upcase : @state.upcase unless @state.blank? sender_state = STATES.has_value?(@sender_state.downcase) ? STATES.index(@sender_state.downcase).upcase : @sender_state.upcase unless @sender_state.blank? datetime = Time.current tz = @sender_country == 'CA' ? 'America/Chicago' : 'America/Toronto' Time.use_zone(tz) do datetime = Time.current if datetime > Time.zone.parse("3:00pm") datetime = 1.working.day.since(Time.zone.parse("10:00:00")) end end request_hash = { login: { username: @yrc_freight_user_name, password: @yrc_freight_password, busId: @yrc_freight_business_id, busRole: ( ( (@sender_address != @shipper_address) && (@address != @shipper_address) ) ? "Third Party" : "Shipper" ), # TBD rb_any_ship_from paymentTerms: "Prepaid" }, details: { serviceClass: (@service_type == 'Guaranteed' ? "GDEL" : "STD"), typeQuery: "QUOTE", pickupDate: datetime.strftime("%Y%m%d"), guarantee: (@service_type == 'Guaranteed' ? true : false) }, originLocation: { city: @sender_city, state: @sender_state, postalCode: @sender_zip, country: sender_country_iso3 }, destinationLocation: { city: @city, state: @state, postalCode: @zip, country: country_iso3 }, listOfCommodities: { commodity: [] }, serviceOpts: { accOptions: [] } } commodity_arr = [] @packages.each_with_index do |package, i| value = package.lbs package_weight = [value,1.0].max.round value = package.inches(:length) package_length = [value,0.1].max.round value = package.inches(:width) package_width = [value,0.1].max.round value = package.inches(:height) package_height = [value,0.1].max.round commodity_arr << { nmfcClass: get_freight_class_from_package(package), handlingUnits: 1, packageCode: (package.pallet? ? 'PLT' : (package.crate? ? 'CRT' : 'CTN')), packageLength: package_length, packageWidth: package_width, packageHeight: package_height, weight: package_weight } end request_hash[:listOfCommodities][:commodity] = commodity_arr = [] # <xsd:enumeration id="Protect From Freezing" value="FREZ"/> # <xsd:enumeration id="Hazardous Materials" value="HAZM"/> # <xsd:enumeration id="Inside Delivery" value="ID"/> # <xsd:enumeration id="Inside Pick-up" value="IP"/> # <xsd:enumeration id="Liftgate Service at Delivery" value="LFTD"/> # <xsd:enumeration id="Liftgate Service at Pickup" value="LFTO"/> # <xsd:enumeration id="Limited Access Delivery" value="LTDD"/> # <xsd:enumeration id="Limited Access Pick-Up" value="LTDO"/> # <xsd:enumeration id="Marking or Tagging" value="MRK"/> # <xsd:enumeration id="Notify Consignee" value="NTFY"/> # <xsd:enumeration id="Residential Delivery" value="HOMD"/> # <xsd:enumeration id="Residential Pick-up" value="HOMP"/> # <xsd:enumeration id="Single Shipment" value="SS"/> # <xsd:enumeration id="Delivery Appointment" value="APPT"/> { 'LFTO' => (@sender_requires_liftgate || false), 'LFTD' => (@requires_liftgate || false), 'IP' => (@sender_requires_inside_delivery || false), 'ID' => (@requires_inside_delivery || false), 'NTFY' => (@address_residential || false), 'HOMP' => (@sender_address_residential || false), 'HOMD' => (@address_residential || false), 'LTDO' => (@sender_limited_access || false), 'LTDD' => (@limited_access || false), "APPT" => (@address_residential || @requires_appointment || false) }.each do |acc, apply_acc| if apply_acc << acc end end request_hash[:serviceOpts][:accOptions] = @data = request_hash.to_json get_response(@yrc_freight_rating_url, {'Content-Type' => 'application/json'}) logger.info "Shipping YRC Freight find_rates Request:" logger.info "#{@yrc_freight_rating_url}" logger.info "#{@data}" logger.info "Shipping YRC Freight find_rates Response:" logger.info "#{@response}" rates = [] rate_estimates = [] successful, msg = nil successful = @response['isSuccess'] successful = false if ( (@response.dig('pageRoot', 'returnCode') == '9018') || (@response.dig('pageRoot', 'bodyHead', 'message').to_s.downcase.index('unsuccessful')) ) if successful service_code = ((@response.dig('pageRoot', 'bodyMain', 'rateQuote', 'delivery', 'requestedServiceType', 'level') == 'GDEL') ? "YRC_GDEL_GSDS" : "YRC_LTL") transportation_charges = (@response.dig('pageRoot', 'bodyMain', 'rateQuote', 'ratedCharges', 'freightCharges').to_f/100.0).to_f.round(2) = (@response.dig('pageRoot', 'bodyMain', 'rateQuote', 'ratedCharges', 'otherCharges').to_f/100.0).to_f.round(2) total_charges = (@response.dig('pageRoot', 'bodyMain', 'rateQuote', 'ratedCharges', 'totalCharges').to_f/100.0).to_f.round(2) days_in_transit = @response.dig('pageRoot', 'bodyMain', 'rateQuote', 'delivery', 'standardDays').to_i + 3 # per Christian and JJ quote_number = @response.dig('pageRoot', 'bodyMain', 'rateQuote', 'referenceId') if total_charges and service_code estimate = Hash.new estimate[:service_code] = service_code estimate[:price] = total_charges estimate[:total_charges] = total_charges estimate[:transportation_charges] = transportation_charges estimate[:service_options_charges] = estimate[:insured_value] = @insured_value.to_f.round(2) estimate[:currency] = @response.dig('pageRoot', 'bodyMain', 'rateQuote', 'currency') || "USD" rate_data = { quote_number: quote_number, total_price: total_charges, days_in_transit: days_in_transit } estimate[:rate_data] = rate_data # allows for things like estimate.service_code def estimate.method_missing(name, *args) has_key?(name) ? self[name] : super end rate_estimates << estimate end end msg = "" if rate_estimates.empty? err_msgs = @response["errors"] err_msgs << @response.dig('pageRoot', 'bodyHead', 'message') if @response.dig('pageRoot', 'bodyHead', 'message').present? msg = "No shipping rates could be found for the destination address: #{err_msgs.join(', ')}" if msg.blank? end response = Hash.new response[:success] = successful response[:message] = "YRC Freight: #{msg}" if msg.present? response[:request] = @data.to_s response[:xml] = @response.to_s response[:rates] = rate_estimates # allows for things like fedex.success? def response.method_missing(name, *args) has_key?(name) ? self[name] : super end # logger.info "Shipping UPS find_rates response:\n#{response.inspect}" return response end |
#get_bol_pdf_file(bol_id) ⇒ Object
if successful
enc_label_image = REXML::XPath.first(@response, "//soap:Envelope/soap:Body/GetBillOfLadingLabelPDFResponse/GetBillOfLadingLabelPDFResult/PdfDocument").text
shipping_label_pdf_file = Tempfile.new("shipping_label")
shipping_label_pdf_file.binmode
shipping_label_pdf_file.write Base64.decode64(enc_label_image)
shipping_label_pdf_file.rewind
return shipping_label_pdf_file
else
err_msgs = []
REXML::XPath.first(@response, "//soap:Envelope/soap:Body/GetBillOfLadingLabelPDFResponse/GetBillOfLadingLabelPDFResult/Messages") do |err_msg|
err_msgs << err_msg
end
msg = "GetBillOfLadingLabelPDF did not succeed: #')" if msg.blank?
successful = false
raise ShippingError, msg
end
end
607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 |
# File 'app/services/shipping/yrc_freight_legacy.rb', line 607 def get_bol_pdf_file(bol_id) logger ||= Rails.logger @data = +'' b = Builder::XmlMarkup.new :target => @data b.instruct! b.tag!("soap:Envelope", {'xmlns:soap' => "http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema"}) {|b| b.tag!("soap:Body") {|b| b.GetBillOfLadingPdf(xmlns: "http://www.rlcarriers.com/") {|b| b.APIKey @rl_carriers_api_key b.billOfLadingId bol_id } } } get_response(@rl_carriers_shipping_url, {'Content-Type' => 'text/xml'}) logger.info "Shipping RlCarriers GetBillOfLadingPdf Request:" logger.info "#{@rl_carriers_shipping_url}" logger.info "#{@data}" logger.info "Shipping RlCarriers GetBillOfLadingPdf Response:" logger.info "#{@response}" successful, msg = nil success = REXML::XPath.first(@response, "//soap:Envelope/soap:Body/GetBillOfLadingPdfResponse/GetBillOfLadingPdfResult/WasSuccess").text successful = true if success == 'true' if successful enc_label_image = REXML::XPath.first(@response, "//soap:Envelope/soap:Body/GetBillOfLadingPdfResponse/GetBillOfLadingPdfResult/PdfDocument").text bol_pdf_file = Tempfile.new("bol") bol_pdf_file.binmode bol_pdf_file.write Base64.decode64(enc_label_image) bol_pdf_file.rewind bol_pdf_file.flush bol_pdf_file.fsync return bol_pdf_file else err_msgs = [] REXML::XPath.first(@response, "//soap:Envelope/soap:Body/GetBillOfLadingPdfResponse/GetBillOfLadingPdfResult/Messages") do |err_msg| err_msgs << err_msg end msg = "GetBillOfLadingPdf did not succeed: #{err_msgs.join(', ')}" if msg.blank? successful = false raise ShippingError, msg end end |
#get_freight_class_from_package(package) ⇒ Object
547 548 549 550 551 552 553 554 555 556 557 558 559 |
# File 'app/services/shipping/yrc_freight_legacy.rb', line 547 def get_freight_class_from_package(package) # get total weight in lbs and cubic feet of package, and figure out density in lbs per cubic foot or PCF total_weight = package.lbs.to_f total_cubic_ft = package.inches(:length).to_f*package.inches(:width).to_f*package.inches(:height).to_f/1728.0 pcf = total_weight/total_cubic_ft UpsFreight::FREIGHT_CLASS_BY_PCF.each do |freight_class, pcf_limits| if pcf >= pcf_limits[:lower] and pcf < pcf_limits[:upper] return freight_class end end # worst case return the highest freight class return UpsFreight::FREIGHT_CLASS_BY_PCF.to_a.last.first.to_f end |
#label(return_label = false, logger = nil) ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 |
# File 'app/services/shipping/yrc_freight_legacy.rb', line 196 def label(return_label=false, logger=nil) logger ||= Rails.logger @required = [:zip, :country, :sender_state, :sender_zip, :sender_country, :packages, :service_code] @required += [:yrc_freight_user_name, :yrc_freight_password, :yrc_freight_business_id, :rate_data, :insured_value] quote_number = @rate_data['quote_number'] @service_type = 'Guaranteed' if (@service_code.index("GDEL")) unless @skip_rate_test # here we have an ST, and don't care about comparing rates # Here we need to re-rate quote, because we have no way to get a total price charged for a bill of lading from R+L Carriers so we re-quote just before total_price = @rate_data['total_price'].to_f # So let's re-find rates, but with the new criteria for the label estimate_res = find_rates # find the matching rate based on the service code estimate = nil estimate = estimate_res[:rates].detect{|e| e.dig(:service_code) == @service_code} if estimate.present? && (new_total_price = estimate.dig(:rate_data, :total_price)) && total_price diff = (new_total_price - total_price).abs if diff && ( (diff < COST_DISCREPANCY_THRESHOLD) && (diff/total_price < COST_DISCREPANCY_THRESHOLD_RATIO) || ( (@delivery_total_value.to_f > 0.0) && (@delivery_total_value.to_f > COST_DISCREPANCY_THRESHOLD * 10.0) && (diff/@delivery_total_value.to_f < COST_DISCREPANCY_THRESHOLD_BY_TOTAL_VALUE_RATIO) ) ) # we have a matching rate using the @service_code matching total_price = new_total_price quote_number = estimate.dig(:rate_data, :quote_number) || @rate_data['quote_number'] else # no match or pricing threshold exceeded raise ShippingError, "Rate has expired or now exceeds thresholds, please HOLD order and refresh shipping rates/methods" end else # no match or pricing threshold exceeded raise ShippingError, "Rate has expired or now exceeds thresholds, please HOLD order and refresh shipping rates/methods" end end @insured_value = 50000.0 if @insured_value.to_f > 50000.0 # UPS limits this to 50000.0 for domestic @country ||= 'US' country_iso3 = "USA" if @country == "US" country_iso3 = "CAN" if @country == "CA" @sender_country ||= 'US' sender_country_iso3 = "USA" if @sender_country == "US" sender_country_iso3 = "CAN" if @sender_country == "CA" shipper_country_iso3 = "USA" if @shipper_country == "US" shipper_country_iso3 = "CAN" if @shipper_country == "CA" @yrc_freight_shipping_url ||= "https://api.yrc.com/dynamic/national/webservice/YRCSecureBOL" state = STATES.has_value?(@state.downcase) ? STATES.index(@state.downcase).upcase : @state.upcase unless @state.blank? sender_state = STATES.has_value?(@sender_state.downcase) ? STATES.index(@sender_state.downcase).upcase : @sender_state.upcase unless @sender_state.blank? datetime = Time.current tz = 'America/Chicago' tz = 'Eastern Time (US & Canada)' if @sender_country == 'CA' Time.use_zone(tz) do datetime = Time.current if datetime > Time.zone.parse("3:00pm") datetime = 1.working.day.since(Time.zone.parse("10:00:00")) end end origin_same_as_shipper = true # Arrgh sometimes the addresses can be similar but not the same, for e.g: # @sender_address "300 GRANTON DR" # @shipper_address "300 Granton Drive" # so use this test origin_same_as_shipper = false if (@sender_address.to_s.downcase.index(@shipper_address.to_s.downcase).nil? && @shipper_address.to_s.downcase.index(@sender_address.to_s.downcase).nil?) @data = +'' b = Builder::XmlMarkup.new :target => @data b.instruct! b.tag!("soapenv:Envelope", {'xmlns:xsi' => "http://www.w3.org/2001/XMLSchema-instance", 'xmlns:xsd' => "http://www.w3.org/2001/XMLSchema", 'xmlns:soapenv' => "http://schemas.xmlsoap.org/soap/envelope/", 'xmlns:yrc' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCSecureBOL.wsdl", 'xmlns:soapenc' => "http://schemas.xmlsoap.org/soap/encoding/"}) {|b| b.tag!("soapenv:Header") b.tag!("soapenv:Body") {|b| b.tag!("yrc:submitBOL", {'soapenv:encodingStyle' => "http://schemas.xmlsoap.org/soap/encoding/"}) {|b| b.UsernameToken('xmlns:yrc' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCCommonTypes.xsd", 'xsi:type' => "yrc:Credentials") {|b| b.Username(@yrc_freight_user_name, 'xsi:type' => "xsd:string") b.Password(@yrc_freight_password, 'xsi:type' => "xsd:string") } b.submitBOLRequest('xmlns:yrc' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBolMessages.xsd", 'xsi:type' => "yrc:submitBOLRequest") {|b| b.bolDetail('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:BoLDetail") {|b| b.pickupDate(datetime.strftime("%m/%d/%Y"), 'xsi:type' => "xsd:string") role = ( ( (@sender_address != @shipper_address) && (@address != @shipper_address) ) ? "TP" : "SH" ) # TBD rb_any_ship_from b.role(role, 'xsi:type' => "yrc1:Roles") # SH for shipper or TP for third party b.autoSchedulePickup true b.autoEmailBOL false b.paymentTerms('P', 'xsi:type' => "yrc1:Terms") # prepaid b.originAddressSameAsShipper true # TBD rb_any_ship_from TBDYRC synonynmous with populating shipFrom, but we only populate shipFrom for "blind shipments" which are very expensive... } if (origin_same_as_shipper == true) # TBD rb_any_ship_from, regular WarmlyYours warehouse shipment b.shipper('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:Customer") {|b| b.companyName(@shipper_company, 'xsi:type' => "xsd:string") b.address("#{[(@shipper_address.present? ? @shipper_address : nil), (@shipper_address2.present? ? @shipper_address2 : nil)].compact.join(', ')}", 'xsi:type' => "xsd:string") # annoyingly, only one address field here b.city(@shipper_city, 'xsi:type' => "xsd:string") b.state(@shipper_state, 'xsi:type' => "xsd:string") b.zip(@shipper_zip, 'xsi:type' => "xsd:string") b.country(shipper_country_iso3, 'xsi:type' => "yrc1:countryCodeList") b.phoneNumber(@shipper_phone, 'xsi:type' => "xsd:string") b.businessID(@yrc_freight_business_id, 'xsi:type' => "xsd:string") } else # TBD rb_any_ship_from, this is a drop-ship (not "blind shipment") b.shipper('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:Customer") {|b| b.companyName(@sender_company, 'xsi:type' => "xsd:string") b.address("#{[(@sender_address.present? ? @sender_address : nil), (@sender_address2.present? ? @sender_address2 : nil)].compact.join(', ')}", 'xsi:type' => "xsd:string") # annoyingly, only one address field here b.city(@sender_city, 'xsi:type' => "xsd:string") b.state(@sender_state, 'xsi:type' => "xsd:string") b.zip(@sender_zip, 'xsi:type' => "xsd:string") b.country(sender_country_iso3, 'xsi:type' => "yrc1:countryCodeList") b.phoneNumber(@sender_phone, 'xsi:type' => "xsd:string") } end b.consignee('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:Customer") {|b| b.companyName(@company || @attention_name, 'xsi:type' => "xsd:string") b.address("#{[(@address.present? ? @address : nil), (@address2.present? ? @address2 : nil)].compact.join(', ')}" , 'xsi:type' => "xsd:string") # annoyingly, only one address field here b.city(@city, 'xsi:type' => "xsd:string") b.state(@state, 'xsi:type' => "xsd:string") b.zip(@zip, 'xsi:type' => "xsd:string") b.country(country_iso3, 'xsi:type' => "yrc1:countryCodeList") b.phoneNumber(@phone, 'xsi:type' => "xsd:string") } b.thirdParty('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:Customer") {|b| b.companyName(@shipper_company, 'xsi:type' => "xsd:string") b.address("#{[(@shipper_address.present? ? @shipper_address : nil), (@shipper_address2.present? ? @shipper_address2 : nil)].compact.join(', ')}", 'xsi:type' => "xsd:string") # annoyingly, only one address field here b.city(@shipper_city, 'xsi:type' => "xsd:string") b.state(@shipper_state, 'xsi:type' => "xsd:string") b.zip(@shipper_zip, 'xsi:type' => "xsd:string") b.country(shipper_country_iso3, 'xsi:type' => "yrc1:countryCodeList") b.phoneNumber(@shipper_phone, 'xsi:type' => "xsd:string") b.businessID(@yrc_freight_business_id, 'xsi:type' => "xsd:string") } if (origin_same_as_shipper != true) # TBD rb_any_ship_from, this is a drop-ship so we are the third party who will pay b.customsBroker('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:Customer") {|b| if @country.index('CA') # Here we are hardcoding Willson International Canada, Toronto location b.companyName('Willson International', 'xsi:type' => "xsd:string") b.address('2345 Argentia Road, Suite 201', 'xsi:type' => "xsd:string") b.city('Mississauga', 'xsi:type' => "xsd:string") b.state('ON', 'xsi:type' => "xsd:string") b.zip('L5N8K4', 'xsi:type' => "xsd:string") b.country('CAN', 'xsi:type' => "yrc1:countryCodeList") b.phoneNumber('905-643-9054', 'xsi:type' => "xsd:string") else # Here we are hardcoding Willson International US, NY location b.companyName('Willson International', 'xsi:type' => "xsd:string") b.address('160 Wales Avenue, Suite 100', 'xsi:type' => "xsd:string") b.city('Tonawanda', 'xsi:type' => "xsd:string") b.state('NY', 'xsi:type' => "xsd:string") b.zip('14150', 'xsi:type' => "xsd:string") b.country('USA', 'xsi:type' => "yrc1:countryCodeList") b.phoneNumber('800-315-1918', 'xsi:type' => "xsd:string") end } if (@sender_country != @country) b.shipFrom('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:Customer") {|b| b.companyName(@sender_company, 'xsi:type' => "xsd:string") b.address("#{[(@sender_address.present? ? @sender_address : nil), (@sender_address2.present? ? @sender_address2 : nil)].compact.join(', ')}", 'xsi:type' => "xsd:string") # annoyingly, only one address field here b.city(@sender_city, 'xsi:type' => "xsd:string") b.state(@sender_state, 'xsi:type' => "xsd:string") b.zip(@sender_zip, 'xsi:type' => "xsd:string") b.country(sender_country_iso3, 'xsi:type' => "yrc1:countryCodeList") b.phoneNumber(@sender_phone, 'xsi:type' => "xsd:string") } if (origin_same_as_shipper != true) b.commodityInformation('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:CommodityInformation") {|b| b.weightTypeIdentifier("LB", 'xsi:type' => "yrc1:WeightTypes") } b.commodityItem('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:CommodityArray", 'soapenc:arrayType' => "yrc1:CommodityItem[]") {|b| @packages.each_with_index do |package, i| b.item('xsi:type' => "yrc1:CommodityItem") {|b| value = package.lbs package_weight = [value,1.0].max.round value = package.inches(:length) package_length = [value,0.1].max.round value = package.inches(:width) package_width = [value,0.1].max.round value = package.inches(:height) package_height = [value,0.1].max.round b.totalWeight(package_weight, 'xsi:type' => "xsd:string") b.handlingUnitQuantity(1, 'xsi:type' => "xsd:string") container_type = (package.pallet? ? 'PLT' : (package.crate? ? 'CRT' : 'CTN')) b.handlingUnitType(container_type, 'xsi:type' => "yrc1:HandlingUnitTypes") b.productDesc('Radiant Heating Elements and Controls', 'xsi:type' => "xsd:string") freight_class = get_freight_class_from_package(package) b.nmfc package.nmfc_code if package.nmfc_code b.freightClass(freight_class, 'xsi:type' => "yrc1:FreightClassTypes") b.isHazardous false b.length(package_length, 'xsi:type' => "xsd:string") b.width(package_width, 'xsi:type' => "xsd:string") b.height(package_height, 'xsi:type' => "xsd:string") } end } b.referenceNumbers('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:ReferenceNumberArray", 'soapenc:arrayType' => "yrc1:ReferenceNumber[]") {|b| b.item('xsi:type' => "yrc1:ReferenceNumber") {|b| b.refNumber(@reference_number_1, 'xsi:type' => "xsd:string") b.refNumberType('CO', 'xsi:type' => "yrc1:ReferenceNumberTypes") } if @reference_number_1.present? b.item('xsi:type' => "yrc1:ReferenceNumber") {|b| b.refNumber(@reference_number_2, 'xsi:type' => "xsd:string") b.refNumberType('PO', 'xsi:type' => "yrc1:ReferenceNumberTypes") } if @reference_number_2.present? } if (@reference_number_1.present? || @reference_number_2.present?) b.specialInstuctions('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:SpecialInstructions") {|b| b.dockInstructions(@special_instructions, 'xsi:type' => "xsd:string") } if (@special_instructions.present?) if @insured_value&.positive? if @sender_country == "CAN" b.declaredValue(sprintf('%.2f', @insured_value), 'xsi:type' => "xsd:string") b.declaredValueInd('CAD', 'xsi:type' => "yrc1:CurrencyTypes") else b.excessLiability(sprintf('%.2f', @insured_value), 'xsi:type' => "xsd:string") end end b.serviceOptions('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:ServiceOptionsArray", 'soapenc:arrayType' => "yrc1:ServiceOption[]") {|b| # <xsd:enumeration id="Protect From Freezing" value="FREZ"/> # <xsd:enumeration id="Hazardous Materials" value="HAZM"/> # <xsd:enumeration id="Inside Delivery" value="ID"/> # <xsd:enumeration id="Inside Pick-up" value="IP"/> # <xsd:enumeration id="Liftgate Service at Delivery" value="LFTD"/> # <xsd:enumeration id="Liftgate Service at Pickup" value="LFTO"/> # <xsd:enumeration id="Limited Access Delivery" value="LTDD"/> # <xsd:enumeration id="Limited Access Pick-Up" value="LTDO"/> # <xsd:enumeration id="Marking or Tagging" value="MRK"/> # <xsd:enumeration id="Notify Consignee" value="NTFY"/> # <xsd:enumeration id="Residential Delivery" value="HOMD"/> # <xsd:enumeration id="Residential Pick-up" value="HOMP"/> # <xsd:enumeration id="Single Shipment" value="SS"/> # <xsd:enumeration id="Delivery Appointment" value="APPT"/> { 'LFTO' => (@sender_requires_liftgate || false), 'LFTD' => (@requires_liftgate || false), 'IP' => (@sender_requires_inside_delivery || false), 'ID' => (@requires_inside_delivery || false), 'NTFY' => (@address_residential || false), 'HOMP' => (@sender_address_residential || false), 'HOMD' => (@address_residential || false), 'LTDO' => (@sender_limited_access || false), 'LTDD' => (@limited_access || false), "APPT" => (@address_residential || @requires_appointment || false) }.each do |acc, apply_acc| if apply_acc b.item('xsi:type' => "yrc1:ServiceOption") {|b| b.serviceOptionType(acc, 'xsi:type' => "yrc1:ServiceOptionType") b.serviceOptionPaymentTerms('P', 'xsi:type' => "yrc1:Terms") } end end } b.deliveryServiceOptions('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:DeliveryServiceOptions") {|b| # LTL is the default, but implement our guaranteed scheme (if selected), we won't implement all the other complex ones here, leave it for manual override if @service_type == 'Guaranteed' b.deliveryServiceOption('GDEL', 'xsi:type' => "yrc1:DeliveryServiceTypes") b.GDELAdditionalInfo('xsi:type' => "yrc1:GDELType") {|b| # <xsd:enumeration id="by 5pm" value="FIVE_PM"/> b.deliveryByTime('FIVE_PM', 'xsi:type' => "yrc1:DueByTimeType") } else b.deliveryServiceOption('LTL', 'xsi:type' => "yrc1:DeliveryServiceTypes") end } b.pickupRequest('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:PickupRequest") {|b| b.pickupLocationContactName(@sender_attention_name, 'xsi:type' => "xsd:string") b.pickupLocationPhone(@sender_phone, 'xsi:type' => "xsd:string") # add any origin special services in ALL CAPS to the pickup notes if (@sender_requires_liftgate || @sender_requires_inside_delivery || @sender_limited_access) reqs = [] reqs << 'ORIGIN LIFTGATE' if @sender_requires_liftgate reqs << 'INSIDE PICKUP' if @sender_requires_inside_delivery reqs << 'LIMITED ACCESS PICKUP' if @sender_limited_access b.pickupNotes("#{reqs.join(', ')} REQUESTED", 'xsi:type' => "xsd:string") end b.shipmentReadyTime(datetime.strftime("%H:%M"), 'xsi:type' => "xsd:string") # format HH:MM, 24-Hour Local Time b.dockCloseTime("17:00", 'xsi:type' => "xsd:string") b.isCertifiedPickup false b.sendConfirmationEmail false } b.bolLabelPDF('xmlns:yrc1' => "http://my.yrc.com/national/WebServices/2009/01/31/YRCBoLTypes.xsd", 'xsi:type' => "yrc1:BolLabelPDF") {|b| b.generateBolPDF true b.bolDocumentType('STD', 'xsi:type' => "yrc1:BolDocumentType") b.generateShippingLabelsPDF true b.numberOfLabelsPerShipment(2, 'xsi:type' => "xsd:string") b.labelStartingPosition(1, 'xsi:type' => "xsd:string") b.labelFormat('CONT_4X6', 'xsi:type' => "yrc1:LabelFormat") b.generateProLabelsPDF false } } } } } logger.info "Shipping YRC Freight label Request:" logger.info "#{@yrc_freight_shipping_url}" logger.info "#{@data}" get_response(@yrc_freight_shipping_url, {'SOAPAction' => 'http://my.yrc.com/national/WebServices/2009/01/31/YRCSecureBOL.wsdl/YRCSecureBOL', 'Content-Type' => 'text/xml', 'Accept-Charset' => 'UTF-8'}) logger.info "Shipping YRC Freight label Response:" logger.info "#{@response}" successful, msg = nil success_resps = [] err_msgs = [] REXML::XPath.each(@response, "//env:Envelope/env:Body/ns3:StatusMessages") do |sr| if REXML::XPath.first(sr, "//item").text == 'SUCCESS' success_resps << true else success_resps << false err_msgs << REXML::XPath.first(sr, "//item").text end end successful = true if success_resps.all?{|sr| sr == true} response = Hash.new if successful bol_id = REXML::XPath.first(@response, "//env:Envelope/env:Body/ns2:submitBOLResponse/proNumber").text web_pro_number = bol_id pickup_req_number = REXML::XPath.first(@response, "//env:Envelope/env:Body/ns2:submitBOLResponse/pickupReferenceNumber").text response[:tracking_number] = web_pro_number encodedBolPdfId = REXML::XPath.first(@response, "//env:Envelope/env:Body/ns2:submitBOLResponse/encodedBolPdf")['href'].split("#").last enc_bol_image = REXML::XPath.match(@response, "//xsd:base64Binary").detect{|b| b['id'] == encodedBolPdfId}.text if enc_bol_image.present? response[:encoded_bol_image] = enc_bol_image response[:bol_image] = Tempfile.new("yrc_freight_bol") response[:bol_image].binmode response[:bol_image].write Base64.decode64(response[:encoded_bol_image]) response[:bol_image].rewind response[:bol_image].flush response[:bol_image].fsync end encodedShippingLabelsPdfId = REXML::XPath.first(@response, "//env:Envelope/env:Body/ns2:submitBOLResponse/encodedShippingLabelsPdf")['href'].split("#").last enc_label_image = REXML::XPath.match(@response, "//xsd:base64Binary").detect{|b| b['id'] == encodedShippingLabelsPdfId}.text if enc_bol_image.present? response[:encoded_image] = enc_label_image response[:image] = Tempfile.new("shipping_label") response[:image].binmode response[:image].write Base64.decode64(response[:encoded_image]) response[:image].rewind response[:image].flush response[:image].fsync enc_bol_image = REXML::XPath.match(@response, "//xsd:base64Binary").detect{|b| b['id'] == encodedBolPdfId}.text end else msg = "Ship Labeling did not succeed: #{err_msgs.join(', ')}" successful = false raise ShippingError, msg end # allows for things like fedex.success? def response.method_missing(name, *args) has_key?(name) ? self[name] : super end return {labels: [response], shipment_identification_number: web_pro_number, carrier_bol: bol_id, pickup_confirmation_number: pickup_req_number, total_charges: total_price, ship_request_xml: "#{@data}", ship_reply_xml: "#{@response}"} end |