Class: Edi::MftGateway::ConfirmMessageProcessor
- Inherits:
-
BaseEdiService
- Object
- BaseService
- BaseEdiService
- Edi::MftGateway::ConfirmMessageProcessor
- Defined in:
- app/services/edi/mft_gateway/confirm_message_processor.rb
Constant Summary
Constants included from AddressAbbreviator
AddressAbbreviator::MAX_LENGTH
Instance Attribute Summary
Attributes inherited from BaseEdiService
Instance Method Summary collapse
- #acknowledge_order(order, options = {}) ⇒ Object
- #auto_cancel_order(order, order_hash, reason_code, bad_vendor_skus = nil, bad_merchant_skus = nil) ⇒ Object
- #auto_reject_order(order, order_hash, reason_code, bad_vendor_skus = nil, bad_merchant_skus = nil) ⇒ Object
- #back_order(order, options = {}) ⇒ Object
-
#cancel_order(order, options = {}) ⇒ Object
not sure what else to do here, SPS Commerce has some gaping holes.
- #confirm_invoice(invoice, options = {}) ⇒ Object
- #generate_and_upload_packing_slip(order, order_hash) ⇒ Object
- #generate_packing_slip_pdf(order, order_hash, _packing_slip_file_name) ⇒ Object
- #get_sps_code_or_scac_from_invoice(invoice) ⇒ Object
- #map_container_type(container_type) ⇒ Object
-
#process(options) ⇒ Object
Picks up the edi communication log in the queue ready to process, generate confirms.
-
#process_acknowledgement(options) ⇒ Object
{ "Header": { "OrderHeader": { "TradingPartnerId": "TPIDXXXXWARMLYYOU", "PurchaseOrderNumber": "896628588", "TsetPurposeCode": "00", "PurchaseOrderDate": "2023-01-27", "AcknowledgementType": "AD", "AcknowledgementDate": "2023-01-27", "Vendor": "Warmly_Yours", "CustomerOrderNumber": "SO659403" }, "Address": [ { "AddressTypeCode": "ST", "LocationCodeQualifier": "12", "AddressLocationNumber": "1234567890", "AddressName": "Customer Name", "AddressAlternateName": "Attn Attn Name", "Address1": "1234 Street Name Rd", "City": "City", "State": "CA", "PostalCode": "12345" }, { "AddressTypeCode": "BT", "LocationCodeQualifier": "9", "AddressLocationNumber": "847174989", "AddressName": "Build.com", "Address1": "402 Otterson Dr Ste 100", "City": "Chico", "State": "CA", "PostalCode": "95928" } ] }, "Structure": { "LineItem": [ { "OrderLine": { "LineSequenceNumber": "1", "BuyerPartNumber": "TW-F10KS-HP", "VendorPartNumber": "TW-F10KS-HP", "ConsumerPackageCode": "881308069032", "OrderQty": "1", "OrderQtyUOM": "EA", "PurchasePrice": "440.3", "PurchasePriceBasis": "PE" }, "LineItemAcknowledgement": { "ItemStatusCode": "IA", "ItemScheduleQty": "1", "ItemScheduleUOM": "EA", "ItemScheduleQualifier": "068", "ItemScheduleDate": "2023-01-27" } } ] }, "Summary": { "TotalLineItemNumber": "1" } }.
Methods inherited from BaseEdiService
#duplicate_po_already_notified?, #initialize, #mark_duplicate_po_as_notified, #report_order_creation_issues, #safe_process_edi_communication_log
Methods included from AddressAbbreviator
#abbreviate_street, #collect_street_originals, #record_address_abbreviation_notes
Methods inherited from BaseService
#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #options, #tagged_logger
Constructor Details
This class inherits a constructor from Edi::BaseEdiService
Instance Method Details
#acknowledge_order(order, options = {}) ⇒ Object
5 6 7 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 5 def acknowledge_order(order, = {}) process_acknowledgement(.merge({ order:, action: 'ack' })) end |
#auto_cancel_order(order, order_hash, reason_code, bad_vendor_skus = nil, bad_merchant_skus = nil) ⇒ Object
17 18 19 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 17 def auto_cancel_order(order, order_hash, reason_code, bad_vendor_skus = nil, bad_merchant_skus = nil) process_acknowledgement(.merge({ order:, order_hash:, action: 'reject', reason_code:, bad_vendor_skus:, bad_merchant_skus: })) end |
#auto_reject_order(order, order_hash, reason_code, bad_vendor_skus = nil, bad_merchant_skus = nil) ⇒ Object
9 10 11 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 9 def auto_reject_order(order, order_hash, reason_code, bad_vendor_skus = nil, bad_merchant_skus = nil) process_acknowledgement(.merge({ order:, order_hash:, action: 'reject', reason_code:, bad_vendor_skus:, bad_merchant_skus: })) end |
#back_order(order, options = {}) ⇒ Object
214 215 216 217 218 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 214 def back_order(order, = {}) return if orchestrator.ignore_back_orders process_acknowledgement(.merge({ order:, order_hash:, action: 'reject', reason_code: 'item(s) not in stock', bad_vendor_skus:, bad_merchant_skus: })) end |
#cancel_order(order, options = {}) ⇒ Object
not sure what else to do here, SPS Commerce has some gaping holes
13 14 15 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 13 def cancel_order(order, = {}) # not sure what else to do here, SPS Commerce has some gaping holes process_acknowledgement(.merge({ order:, action: 'reject', reason_code: order.edi_cancellation_reason_description })) end |
#confirm_invoice(invoice, options = {}) ⇒ Object
220 221 222 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 220 def confirm_invoice(invoice, = {}) process(.merge({ order: invoice.order, invoice: })) end |
#generate_and_upload_packing_slip(order, order_hash) ⇒ Object
602 603 604 605 606 607 608 609 610 611 612 613 614 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 602 def generate_and_upload_packing_slip(order, order_hash) packing_slip_file_name = "PO_#{order.edi_po_number}_packing_slip.pdf" pdf = generate_packing_slip_pdf(order, order_hash, packing_slip_file_name) path = File.join(Rails.application.config.x.temp_storage_path.to_s, packing_slip_file_name) File.open(path, 'wb') do |file| file << pdf file.flush file.fsync end upload = Upload.uploadify(path, 'custom_packing_slip_pdf', order, packing_slip_file_name) order.uploads << upload upload end |
#generate_packing_slip_pdf(order, order_hash, _packing_slip_file_name) ⇒ Object
616 617 618 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 616 def generate_packing_slip_pdf(order, order_hash, _packing_slip_file_name) Pdf::Document::EdiPackingSlip.call(order, order_hash, orchestrator) end |
#get_sps_code_or_scac_from_invoice(invoice) ⇒ Object
597 598 599 600 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 597 def get_sps_code_or_scac_from_invoice(invoice) shipping_method_name = invoice.delivery.shipping_option.name orchestrator.ship_code_mapper.hw_to_sps_code_or_scac(shipping_method_name) end |
#map_container_type(container_type) ⇒ Object
584 585 586 587 588 589 590 591 592 593 594 595 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 584 def map_container_type(container_type) case container_type when 'carton' 'CTN' when 'crate' 'CRT' when 'pallet', 'palleted_crate' 'PLT' else 'CTN' end end |
#process(options) ⇒ Object
Picks up the edi communication log in the queue ready to process, generate confirms
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 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 369 def process() order = [:order] invoice = [:invoice] delivery = invoice.delivery transmit_after = [:transmit_after] shp_date, shp_time = Time.current.iso8601.split('T') shipped_date = order.shipped_date&.iso8601&.split('T')&.first || shp_date expected_delivery_date = order.get_expected_ship_date_time&.iso8601&.split('T')&.first tot_lines = order.line_items.goods.where.not(edi_reference: nil).size order_hash = JSON.parse(order.).with_indifferent_access order_line_hash_arr = order_hash.dig(:Structure, :LineItem) ship_to_address_hash = order_hash.dig(:Header, :Address)&.detect { |add_hash| add_hash[:AddressTypeCode] == 'ST' } || order_hash.dig(:Header, :Address)&.detect { |add_hash| add_hash[:AddressTypeCode] == 'MA' } # MA for Canadian Tire store_number = ship_to_address_hash.dig(:AddressLocationNumber) || ship_to_address_hash.dig(:Address1).to_s.split('#').last.delete(' ') ma_address_hash = nil if orchestrator.partner == :mft_gateway_canadian_tire && store_number.present? && (ship_to_address_hash.dig(:AddressTypeCode) == 'ST') && !ship_to_address_hash.dig(:LocationCodeQualifier).present? # put in the ^&%^& store number for Canadian Tire DSCO in an MA segment, even though they don't send it to us! ma_address_hash = {} ma_address_hash[:AddressTypeCode] = 'MA' # ^&%^&% Canadian Tire requires this ma_address_hash[:LocationCodeQualifier] ||= '92' ma_address_hash[:AddressLocationNumber] ||= store_number ma_address_hash[:Address1] = order&.shipping_address&.company_name_override end # Comment out this old SPS Commerce weirdness with Canadian Tire # if (ship_to_address_hash[:AddressTypeCode] == 'MA' && ship_to_address_hash[:Address1].blank?) # # must populate Canadian Tire actual address fields # ship_to_address_hash[:AddressTypeCode] = 'ST' # ^&%^&% Canadian Tire requires this # ship_to_address_hash[:AddressName] = order&.shipping_address&.company_name_override # ship_to_address_hash[:Address1] = order&.shipping_address&.street1 # ship_to_address_hash[:City] = order&.shipping_address&.city # ship_to_address_hash[:State] = order&.shipping_address&.state_code # ship_to_address_hash[:PostalCode] = order&.shipping_address&.zip # end ship_to_address_hash[:State] = order&.shipping_address&.state_code if ship_to_address_hash[:State].to_s.length != 2 ship_from_address = delivery.origin_address scac_code = order_hash.dig(:Header, :CarrierInformation, :CarrierAlphaCode).presence || get_sps_code_or_scac_from_invoice(invoice) # reflect back to them what they sent us (unless unspecified) or find something in the map scac_code = get_sps_code_or_scac_from_invoice(invoice) if scac_code.to_s.downcase.index('unspecified') shipping_code = order_hash.dig(:Header, :CarrierInformation, :ServiceLevelCodes, :ServiceLevelCode).presence || order_hash.dig(:Header, :CarrierInformation, :ShipServiceLevelCode).presence # This can be all kinds of different for SPS ServiceLevelCode Parcel for new Canadian Tire DSCO ShipServiceLevelCode 'PRLG' shipping_code = get_sps_code_or_scac_from_invoice(invoice) if orchestrator.partner == :mft_gateway_canadian_tire && shipping_code.to_s.downcase.index('unsf') # take it from invoice if UNSF routing = delivery.friendly_shipping_method_for_edi routing = order_hash.dig(:Header, :CarrierInformation, :CarrierRouting).presence || get_sps_code_or_scac_from_invoice(invoice) if orchestrator.partner == :mft_gateway_build_com con = order_hash.dig(:Header, :OrderHeader, :CustomerOrderNumber).presence || order&.reference_number # we need something here for Zoro # documentation from SPS Commerce son = order_hash.dig(:Header, :OrderHeader, :SalesOrderNumber).presence || order&.reference_number # we need something here for Build.com # documentation from SPS Commerce order_header = { TradingPartnerId: orchestrator.partner_id, InvoiceNumber: invoice.reference_number, # per Build.com we don't have to limit this to the last 6 digits PurchaseOrderNumber: order.edi_po_number, TsetPurposeCode: '00', # 00: Original PurchaseOrderDate: order.edi_order_date, # e.g. 2022-12-24 Vendor: orchestrator.vendor_id, CustomerOrderNumber: con, SalesOrderNumber: son } delivery_pack_level_hash_arr = [] n = [delivery.line_items.goods.parents_only.sum(:quantity), delivery.shipments.completed.top_level.count].min delivery.shipments.completed.top_level.sort_by { |s| s.to_package.billable_weight }.first(n).map do |shipment| shipment_item_level_hash_arr = [] # we need to deal with kits so shipment.shipment_content_grouped_content_key.each do |key_hash, qty| # deal with *&%&* DSCO padding LineSequenceNumber with leading zeroes order_line_hash = order_line_hash_arr.detect do |olh| (olh.dig(:OrderLine, :LineSequenceNumber).to_s == key_hash[:edi_line_number].to_s) || (olh.dig(:OrderLine, :LineSequenceNumber).to_i.to_s == key_hash[:edi_line_number].to_s) end order_line_hash ||= {} line_seq_number = order_line_hash.dig(:OrderLine, :LineSequenceNumber) || key_hash[:edi_line_number] buyer_part_number = order_line_hash.dig(:OrderLine, :BuyerPartNumber) || key_hash[:third_party_part_number] || order_line_hash.dig(:OrderLine, :SKU) || key_hash[:sku] # documentation from SPS Commerce requires this sku = buyer_part_number sku = order_line_hash.dig(:OrderLine, :SKU) || key_hash[:sku] || buyer_part_number if orchestrator.partner == :mft_gateway_canadian_tire consumer_package_code = key_hash[:upc] consumer_package_code ||= order_line_hash.dig(:OrderLine, :ConsumerPackageCode) # documentation from SPS Commerce requires this shipment_item_level_hash_arr << { ShipmentLine: { LineSequenceNumber: line_seq_number, BuyerPartNumber: buyer_part_number, SKU: sku, VendorPartNumber: key_hash[:sku], ConsumerPackageCode: consumer_package_code, ShipQty: qty, ShipQtyUOM: 'EA' }, ProductOrItemDescription: { ProductCharacteristicCode: '08', # from: https://developercenter.spscommerce.com/#/rsx/docs/fields-qualifiers/7.7.6/ItemRegistries/ProductCharacteristicCode: ... 08 Product Description ... ProductDescription: (Item.find_by_sku(key_hash[:sku])&.name || "WarmlyYours Product #{key_hash[:sku]}").to_s.tr('′', "'").gsub(/[^0-9a-z.,\s]/i, '')[0..79] } } end pack_level_type = 'P' pack_level_type = 'T' if (map_container_type(shipment.container_type) == 'PLT' && orchestrator.partner != :mft_gateway_build_com) # Build.com only accepts P here delivery_pack_level_hash_arr << { Pack: { PackLevelType: pack_level_type, ShippingSerialID: shipment.container_code, CarrierPackageID: shipment.tracking_number || delivery.reported_master_tracking_number, PackageWareHouseCode: orchestrator.warehouse_code }, QuantityAndWeight: { PackingMedium: map_container_type(shipment.container_type), LadingQuantity: '1', Weight: shipment.weight, WeightUOM: 'LB' }, ItemLevel: shipment_item_level_hash_arr } end se_address_hash = nil if orchestrator.partner == :mft_gateway_build_com se_address_hash = { AddressTypeCode: 'SE', # per Bille at SPS Commerce SE Selling Party ?? Just repeat ship from I guess LocationCodeQualifier: '91', AddressLocationNumber: "#{order.store.id}#{order.store.warehouse_address_id}", # documentation from SPS Commerce requires this so put some&^*&^ thing here AddressName: order.store.warehouse_address.company_name || 'WarmlyYours', Address1: ship_from_address.street1, Address2: ship_from_address.street2, Address3: ship_from_address.street3, City: ship_from_address.city, State: ship_from_address.state_code, PostalCode: ship_from_address.zip_compact, Country: ship_from_address.country.iso } end address_hashes = [] address_hashes << ship_to_address_hash.merge({ PostalCode: order&.shipping_address&.zip_compact }) address_hashes << ma_address_hash address_hashes << { AddressTypeCode: 'SF', # from: https://developercenter.spscommerce.com/#/rsx/docs/fields-qualifiers/7.7.6/Shipments/AddressTypeCode: ... SF Ship From ... LocationCodeQualifier: '6', AddressLocationNumber: "0000#{order.store.warehouse_address_id}".last(2), # documentation from SPS Commerce requires this and per Bille at SPS needs to be one of 1 DUNS, 9 DUNS plus 4, 6 Plant or 8 UCC/EAN (??!!) so put some&^*&^ thing here # Canadian Tire require it is two characters AddressName: order.store.warehouse_address.company_name || 'WarmlyYours', Address1: ship_from_address.street1, Address2: ship_from_address.street2, Address3: ship_from_address.street3, City: ship_from_address.city, State: ship_from_address.state_code, PostalCode: ship_from_address.zip_compact, Country: ship_from_address.country.iso, Contacts: [{ ContactTypeCode: 'IC', ContactName: 'WarmlyYours', PrimaryPhone: '800-875-5285', PrimaryEmail: 'orders@warmlyyours.com' }] } address_hashes << se_address_hash shp_hash_data = { Header: { ShipmentHeader: { TradingPartnerId: orchestrator.partner_id, ShipmentIdentification: delivery.reported_master_tracking_number, ShippingSerialID: delivery.shipments.first&.container_code, ShipDate: shipped_date, # e.g. 2022-12-24 TsetPurposeCode: '00', # 00: Original ShipNoticeDate: shp_date, # e.g. 2022-12-24 ShipNoticeTime: shp_time, # e.g. 21:05:49-06:00 ASNStructureCode: '0003', # e.g. 21:05:49-06:00 BillOfLadingNumber: delivery.carrier_bol || delivery.reported_master_tracking_number, # if we have bol, populate it here, but fall back to master tracking number because documentation from SPS Commerce requires it CarrierProNumber: delivery.ltl_pro_number, # if we have PRO, populate it here, but skip otherwise CurrentScheduledDeliveryDate: expected_delivery_date, # e.g. 2022-12-24 VendorId: orchestrator.vendor_id }, Dates: [ { DateTimeQualifier: '011', # per Bille at SPS Commerce 011 Actual Ship Date: shipped_date # e.g. 2022-12-24 }, { DateTimeQualifier: '017', # per Bille at SPS Commerce 017 Estimated Delivery Date: expected_delivery_date # e.g. 2022-12-24 } ], Contacts: { ContactTypeCode: 'IC', ContactName: 'WarmlyYours', PrimaryPhone: '800-875-5285', PrimaryEmail: 'orders@warmlyyours.com' }, Address: address_hashes.compact, CarrierInformation: { CarrierTransMethodCode: 'T', # T Best Way (Shipper's option) CarrierAlphaCode: scac_code, CarrierRouting: routing, ShipServiceLevelCode: shipping_code }, QuantityAndWeight: { PackingMedium: map_container_type(delivery.shipments.completed.top_level.sort_by { |s| s.to_package.billable_weight }.last.container_type), LadingQuantity: delivery.shipments.completed.top_level.count, Weight: delivery.shipments.completed.top_level.to_a.sum { |s| s.weight }, WeightUOM: 'LB' } }, Structure: { OrderLevel: [ { OrderHeader: order_header, PackLevel: delivery_pack_level_hash_arr } ] }, Summary: { TotalLineItemNumber: tot_lines # not sure why they want this, but here it is } }.compact.to_json EdiCommunicationLog.create_outbound_file_from_data(data: shp_hash_data, file_extension: 'shipment', partner: orchestrator.partner, category: 'order_shipment', data_type: 'json', resources: order, transmit_after:, file_info: { lines_confirmed: tot_lines, packages: delivery.shipments.top_level.completed.size }) end |
#process_acknowledgement(options) ⇒ Object
{
"Header": {
"OrderHeader": {
"TradingPartnerId": "TPIDXXXXWARMLYYOU",
"PurchaseOrderNumber": "896628588",
"TsetPurposeCode": "00",
"PurchaseOrderDate": "2023-01-27",
"AcknowledgementType": "AD",
"AcknowledgementDate": "2023-01-27",
"Vendor": "Warmly_Yours",
"CustomerOrderNumber": "SO659403"
},
"Address": [
{
"AddressTypeCode": "ST",
"LocationCodeQualifier": "12",
"AddressLocationNumber": "1234567890",
"AddressName": "Customer Name",
"AddressAlternateName": "Attn Attn Name",
"Address1": "1234 Street Name Rd",
"City": "City",
"State": "CA",
"PostalCode": "12345"
},
{
"AddressTypeCode": "BT",
"LocationCodeQualifier": "9",
"AddressLocationNumber": "847174989",
"AddressName": "Build.com",
"Address1": "402 Otterson Dr Ste 100",
"City": "Chico",
"State": "CA",
"PostalCode": "95928"
}
]
},
"Structure": {
"LineItem": [
{
"OrderLine": {
"LineSequenceNumber": "1",
"BuyerPartNumber": "TW-F10KS-HP",
"VendorPartNumber": "TW-F10KS-HP",
"ConsumerPackageCode": "881308069032",
"OrderQty": "1",
"OrderQtyUOM": "EA",
"PurchasePrice": "440.3",
"PurchasePriceBasis": "PE"
},
"LineItemAcknowledgement": {
"ItemStatusCode": "IA",
"ItemScheduleQty": "1",
"ItemScheduleUOM": "EA",
"ItemScheduleQualifier": "068",
"ItemScheduleDate": "2023-01-27"
}
}
]
},
"Summary": {
"TotalLineItemNumber": "1"
}
}
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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'app/services/edi/mft_gateway/confirm_message_processor.rb', line 85 def process_acknowledgement() order = [:order] transmit_after = [:transmit_after] ack_date, = Time.current.iso8601.split('T') order_hash = JSON.parse(order.).with_indifferent_access ship_to_address_hash = order_hash.dig(:Header, :Address)&.detect { |add_hash| add_hash[:AddressTypeCode] == 'ST' } || order_hash.dig(:Header, :Address)&.detect { |add_hash| add_hash[:AddressTypeCode] == 'MA' } # MA for Canadian Tire if ship_to_address_hash[:AddressTypeCode] == 'MA' && ship_to_address_hash[:Address1].blank? # must populate Canadian Tire actual address fields ship_to_address_hash[:AddressTypeCode] = 'ST' # ^&%^&% Canadian Tire requires this ship_to_address_hash[:AddressName] = order&.shipping_address&.company_name_override ship_to_address_hash[:Address1] = order&.shipping_address&.street1 ship_to_address_hash[:City] = order&.shipping_address&.city ship_to_address_hash[:State] = order&.shipping_address&.state_code ship_to_address_hash[:PostalCode] = order&.shipping_address&.zip end ship_to_address_hash[:State] = order&.shipping_address&.state_code if ship_to_address_hash[:State].to_s.length != 2 bill_to_address_hash = order_hash.dig(:Header, :Address)&.detect { |add_hash| add_hash[:AddressTypeCode] == 'BT' } order_line_hash_arr = order_hash.dig(:Structure, :LineItem) tot_lines = order_line_hash_arr.size tset_purpose_code = '00' # 00: Original if [:action] == 'ack' code = 'AD' # we do this here because we need packing details like packed shipments, etc generate_and_upload_packing_slip(order, order_hash) if orchestrator.try(:generate_packing_slip) else code = (orchestrator.partner == :mft_gateway_build_com ? 'RJ' : 'RD') # Use RJ for Build.com because ^&%&^% SPS errors out *despite* what they documented! Otherwsie use RD tset_purpose_code = '2' if orchestrator.partner == :mft_gateway_canadian_tire # 2: Prearranged Schedule for Canadian Tire cancellation 870 end con = order_hash.dig(:Header, :OrderHeader, :CustomerOrderNumber).presence || order&.reference_number # we need something here for Zoro # documentation from SPS Commerce son = order_hash.dig(:Header, :OrderHeader, :SalesOrderNumber).presence || order&.reference_number # we need something here for Build.com # documentation from SPS Commerce # we fill or kill, no partial, no splits, no delays; for any rejection, populate the notes order_header = { TradingPartnerId: orchestrator.partner_id, PurchaseOrderNumber: order.edi_po_number, TsetPurposeCode: tset_purpose_code, PurchaseOrderDate: order.edi_order_date, # e.g. 2022-12-24 AcknowledgementType: code, AcknowledgementDate: ack_date, # e.g. 2022-12-24 Vendor: orchestrator.vendor_id, CustomerOrderNumber: con, SalesOrderNumber: son } unless [:action] == 'ack' if orchestrator.partner == :mft_gateway_canadian_tire # per 'Dsco Dropship EDI Specifications_v4.2.pdf' note = [:reason_code] || 'A80' # per SPS Commerce support@spscommerce.com email dated November 13, 2023 elsif orchestrator.partner == :mft_gateway_zoro_tools note = "GEN - #{[:reason_code] || 'unknown item SKU'}" else note = "We fill or kill the entire order, rejecting order because of reason: #{[:reason_code] || 'unknown item SKU'}" note += ", bad vendor SKUs: #{[:bad_vendor_skus].join(', ')}" if [:bad_vendor_skus].present? note += ", bad merchant part numbers: #{[:bad_merchant_skus].join(', ')}" if [:bad_merchant_skus].present? end end line_items_hash_arr = [] order_line_hash_arr.each do |order_line_hash| line_seq_number = order_line_hash.dig(:OrderLine, :LineSequenceNumber) li = order.line_items.goods.where.not(edi_reference: nil).detect { |l| l.edi_line_number.to_s == line_seq_number || l.edi_line_number == line_seq_number.to_s.to_i } # deal with *&%&* DSCO padding line_seq_number with leading zeroes vendor_sku = order_line_hash.dig(:OrderLine, :VendorPartNumber).presence || order_line_hash.dig(:OrderLine, :SKU) || li&.item&.sku buyer_part_number = order_line_hash.dig(:OrderLine, :BuyerPartNumber).presence || li&.catalog_item&.third_party_part_number.presence || vendor_sku sku = buyer_part_number sku = order_line_hash.dig(:OrderLine, :SKU) || vendor_sku || buyer_part_number if orchestrator.partner == :mft_gateway_canadian_tire consumer_package_code = li&.item&.upc.presence || order_line_hash.dig(:OrderLine, :ConsumerPackageCode).presence order_line_qty = order_line_hash.dig(:OrderLine, :OrderQty) order_line_qty_uom = order_line_hash.dig(:OrderLine, :OrderQtyUOM).presence || 'EA' order_line_price = order_line_hash.dig(:OrderLine, :PurchasePrice) item_status_code = ([:action] == 'ack' ? 'IA' : 'IR') # IA: Accept, IR: Reject line_items_hash = { OrderLine: { LineSequenceNumber: line_seq_number, BuyerPartNumber: buyer_part_number, SKU: sku, VendorPartNumber: vendor_sku, ConsumerPackageCode: consumer_package_code, OrderQty: order_line_qty, OrderQtyUOM: order_line_qty_uom, PurchasePrice: order_line_price, PurchasePriceBasis: (orchestrator.partner == :mft_gateway_canadian_tire ? 'NT' : 'PE') # just make it work, everyone has their own codes }, ProductOrItemDescription: { ProductCharacteristicCode: '08', # from: https://developercenter.spscommerce.com/#/rsx/docs/fields-qualifiers/7.7.6/ItemRegistries/ProductCharacteristicCode: ... 08 Product Description ... ProductDescription: (Item.find_by_sku(vendor_sku)&.name || "WarmlyYours Product #{vendor_sku}").to_s.tr('′', "'").gsub(/[^0-9a-z.,\s]/i, '')[0..79] }, LineItemAcknowledgement: { ItemStatusCode: item_status_code, ItemScheduleQty: order_line_qty, ItemScheduleUOM: order_line_qty_uom }, Notes: note } if item_status_code == 'IA' line_items_hash[:LineItemAcknowledgement][:ItemScheduleQualifier] = '068' # 068 Scheduled Ship, line_items_hash[:LineItemAcknowledgement][:ItemScheduleDate] = order.get_scheduled_ship_date_time.iso8601.split('T').first elsif item_status_code == 'IR' && orchestrator.partner != :mft_gateway_canadian_tire # Canadian Tire does not want these when cancelling # 001 Cancel Date # 036 Discontinued, Date when something is no longer available/valid qualifier_code = ([:reason_code].to_s.index('discontinued') ? '036' : '001') line_items_hash[:LineItemAcknowledgement][:ItemScheduleQualifier] = qualifier_code line_items_hash[:LineItemAcknowledgement][:ItemScheduleDate] = Time.current.iso8601.split('T').first end line_items_hash_arr << line_items_hash end ack_hash_data = { Header: { OrderHeader: order_header, Address: [ship_to_address_hash.merge({ PostalCode: order&.shipping_address&.zip_compact }), bill_to_address_hash].compact, # make sure shipping address uses compact postal code Notes: note || 'OK' # per Udith, no structure here }, Structure: { LineItem: line_items_hash_arr }, Summary: { TotalLineItemNumber: tot_lines } }.compact.to_json EdiCommunicationLog.create_outbound_file_from_data(data: ack_hash_data, file_extension: 'acknowledge', partner: orchestrator.partner, category: 'order_acknowledge', data_type: 'json', resources: order, transmit_after:, file_info: { lines_confirmed: tot_lines }) end |