app/controllers/spree/api/retailops/settlement_controller.rb
# Package adding: This reflects a fairly major impedence mismatch
# between ROP and Spree, insofar as Spree wants to create shipments at
# checkout time and charge exact shipping, while ROP standard practice
# is to charge an abstraction of shipping that's close enough on
# average, and then decide the details of how this order will be
# shipped when it's actually ready to go out the door.  Because of this
# we have to fudge stuff a bit on shipment and recreate the Spree
# shipments to reflect how ROP is actually shipping it, so that the
# customer has the most actionable information.  A complication is that
# Spree's shipping rates are tied to the shipment, and need to be
# converted into order adjustments before we start mangling the
# shipments.  Credit where due: this code is heavily inspired by some
# code written by @schwartzdev for @dotandbo to handle an Ordoro
# integration.
Package adding: This reflects a fairly major impedence mismatch
between ROP and Spree, insofar as Spree wants to create shipments at
checkout time and charge exact shipping, while ROP standard practice
is to charge an abstraction of shipping that's close enough on
average, and then decide the details of how this order will be
shipped when it's actually ready to go out the door.  Because of this
we have to fudge stuff a bit on shipment and recreate the Spree
shipments to reflect how ROP is actually shipping it, so that the
customer has the most actionable information.  A complication is that
Spree's shipping rates are tied to the shipment, and need to be
converted into order adjustments before we start mangling the
shipments.  Credit where due: this code is heavily inspired by some
code written by @schwartzdev for @dotandbo to handle an Ordoro
integration.
Package adding : This reflects a fairly major impedence mismatch between ROP and Spree insofar as Spree wants to create shipments at checkout time and charge exact shipping while ROP standard practice is to charge an abstraction of shipping that ' s close enough on average and then decide the details of how this order will be shipped when it ' s actually ready to go out the door . Because of this we have to fudge stuff a bit on shipment and recreate the Spree shipments to reflect how ROP is actually shipping it so that the customer has the most actionable information . A complication is that Spree ' s shipping rates are tied to the shipment and need to be converted into order adjustments before we start mangling the shipments . Credit where due : this code is heavily inspired by some code written by @schwartzdev for @dotandbo to handle an Ordoro integration .
def extract_items_into_package(pkg)
            return if @order.canceled?
            number = 'P' + pkg["id"].to_i.to_s
            tracking = pkg["tracking"].to_s
            line_items = pkg["contents"].to_a
            from_location_name = pkg["from"].to_s
            from_location = Spree::StockLocation.find_or_create_by!(name: from_location_name) { |l| l.admin_name = from_location_name }
            shipment = @order.shipments.where( "number = ? OR number LIKE ?", number, "#{number}-%" ).first 
            if shipment
              apply_shipcode shipment, pkg
              shipment.save!
              return 
            end
            shipment = @order.shipments.build
            shipment.number = number
            shipment.stock_location = from_location
            collect_inventory_units
            line_items.to_a.each do |item|
              line_item = @order.line_items.find(item["id"].to_i)
              quantity = item["quantity"].to_i
              reused = collected_units_for_line_item(line_item).slice!(0, quantity)
              shipment.inventory_units.concat(reused)
              quantity -= reused.count
            end
            shipment.save!
            apply_shipcode shipment, pkg
            shipment.state = 'ready'
            shipment.ship!
            shipment.save!
            @order.shipments.reload
            @order.shipments.each do |s|
                s.reload
                s.destroy! if s.manifest.empty?
            end
            @order.reload
            @order.update!
          end
def extract_items_into_package ( pkg ) return if @order . canceled? number = 'P' + pkg [ "id" ] . to_i . to_s tracking = pkg [ "tracking" ] . to_s line_items = pkg [ "contents" ] . to_a from_location_name = pkg [ "from" ] . to_s from_location = Spree :: StockLocation . find_or_create_by! ( name : from_location_name ) { | l | l . admin_name = from_location_name } shipment = @order . shipments . where ( "number = ? OR number LIKE ?" , number , "#{number}-%" ) . first if shipment apply_shipcode shipment , pkg shipment . save! return end shipment = @order . shipments . build shipment . number = number shipment . stock_location = from_location collect_inventory_units line_items . to_a . each do | item | line_item = @order . line_items . find ( item [ "id" ] . to_i ) quantity = item [ "quantity" ] . to_i reused = collected_units_for_line_item ( line_item ) . slice! ( 0 , quantity ) shipment . inventory_units . concat ( reused ) quantity -= reused . count end shipment . save! apply_shipcode shipment , pkg shipment . state = 'ready' shipment . ship! shipment . save! @order . shipments . reload @order . shipments . each do | s | s . reload s . destroy! if s . manifest . empty? end @order . reload @order . update! end
Package adding: This reflects a fairly major impedence mismatch
between ROP and Spree, insofar as Spree wants to create shipments at
checkout time and charge exact shipping, while ROP standard practice
is to charge an abstraction of shipping that's close enough on
average, and then decide the details of how this order will be
shipped when it's actually ready to go out the door.
Package adding : This reflects a fairly major impedence mismatch between ROP and Spree insofar as Spree wants to create shipments at checkout time and charge exact shipping while ROP standard practice is to charge an abstraction of shipping that ' s close enough on average and then decide the details of how this order will be shipped when it ' s actually ready to go out the door .
# tolerate Spree's disambiguative renaming # idempotence # TODO: not sure if we should allow adding stuff after the shipment ships # move existing inventory units # limit transfered units to ordered units
pkg
<not_specific>