Last active
June 8, 2020 12:16
-
-
Save iarie/5ef4c5db83725ebb85e3a1591a7bde1d to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Get Status | |
get_status = -> (po_number) do | |
r = Supplier::Alphabroder::OrderStatusV2::GetOrderStatus.new.call(po_number) | |
doc = Nokogiri::XML(r.body) | |
nodes = doc.xpath("//order[not(shipstatus)]") | |
pnode = Supplier::Alphabroder::OrderStatusV2::OrderXmlNode.new(nodes) | |
pnode.ship_method_nodes.map do |n| | |
{ | |
number: n.shipper_number, | |
wh: n.warehouse, | |
status: Supplier::Alphabroder::OrderStatusV2::OrderNodeConverter::SHIPMENT_STATUS_MAP[n.ship_status] | |
} | |
end | |
end | |
# Order ids with stuck :picking states | |
interval = Date.new(2020, 5, 1).all_month | |
stock_location_ids = Spree::Supplier.alphabroder.stock_location_ids | |
order_ids = Spree::Order.joins(:shipments).where( | |
completed_at: interval, | |
spree_shipments: { | |
state: 'picking', | |
stock_location_id: stock_location_ids | |
} | |
).where( | |
'spree_shipments.deleted_at IS NULL' | |
).ids.uniq; order_ids.size | |
# Loading Orders | |
orders = Spree::Order.includes(:supplier_orders, shipments: [:supplier_shipments, :stock_location]).find(order_ids); nil | |
shipments_and_states = orders.flat_map do |order| | |
pons = order.supplier_orders.where(supplier_name: 'alphabroder').pluck(:order_number) | |
supplier_shipment_statuses = pons.flat_map do |po_number| | |
get_status.call(po_number) | |
end | |
shipments = order.shipments.where('spree_shipments.deleted_at IS NULL') | |
shipments.map do |spree_s| | |
next if spree_s.supplier.name != 'alphabroder' | |
next if spree_s.supplier_shipments.empty? | |
next if spree_s.state != 'picking' | |
wh = spree_s.stock_location.admin_name | |
ss_states = spree_s.supplier_shipments.map do |ss| | |
api_status = supplier_shipment_statuses.find { |x| x[:wh] == wh }&.fetch(:status) | |
{ | |
id: ss.id, | |
number: ss.shipment_number, | |
current_status: ss.status, | |
api_status: api_status | |
} | |
end | |
change_status_to = ss_states.first[:api_status] if ss_states.first[:api_status] != spree_s.state | |
{ | |
id: spree_s.id, | |
current_state: spree_s.state, | |
supplier: spree_s.supplier.name, | |
wh: wh, | |
supplier_shipment_states: ss_states, | |
change_status_to: change_status_to | |
} | |
end.compact | |
print('.') | |
end; print('') | |
# [{:id=>15881589, | |
# :current_state=>"picking", | |
# :supplier=>"alphabroder", | |
# :wh=>"PH", | |
# :supplier_shipment_states=> | |
# [{:id=>8133204, :number=>"28747812", :current_status=>"picking", :api_status=>"shipped"}], | |
# :change_status_to=>"shipped"}, | |
# {:id=>15881587, | |
# :current_state=>"picking", | |
# :supplier=>"alphabroder", | |
# :wh=>"CC", | |
# :supplier_shipment_states=> | |
# [{:id=>8133203, :number=>"28747812", :current_status=>"picking", :api_status=>"shipped"}], | |
# :change_status_to=>"shipped"}] | |
# states variation: | |
# shipments_and_states.group_by { |x| x[:change_status_to] }.transform_values(&:size) | |
# {"shipped"=>384, nil=>41} | |
# supplier shipments: | |
# shipments_and_states.group_by { |x| x[:supplier_shipment_states].size }.transform_values(&:size) | |
# => {1=>270, 2=>155} | |
shipments_to_update = shipments_and_states.select { |x| x[:change_status_to].present? } | |
# UPDATE DB: | |
# | |
# shipments_to_update.map do |x| | |
# ship = Spree::Shipment.find(x[:id]) | |
# ship.transaction do | |
# ship.update_column(state: x[:change_status_to]) | |
# ship.supplier_shipments.update_all(status: x[:change_status_to]) | |
# end | |
# print('.') | |
# [ship.id] | |
# end; print(''); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment