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 apply_shipcode(shipment, pkg)
            if shipment.respond_to? :retailops_set_tracking
              shipment.retailops_set_tracking(pkg)
              return
            end
            shipcode = pkg["shipcode"].to_s
            shipment.tracking = pkg['tracking'].to_s
            shipment.created_at = Time.parse(pkg['date'].to_s)
            method, *fields = shipcode.split('|')
            method = URI.parser.unescape(method.encode(Encoding::UTF_8))
            assocs = nil
            fields.each do |part|
              name, value = part.split(':')
              value = URI.parser.unescape(value.encode(Encoding::UTF_8))
              assocs ||= shipment.class.reflect_on_all_associations.each_with_object({}) { |a,h| h[a.name.to_s] = a.klass }
              if shipment.respond_to? "retailops_extend_#{name}="
                shipment.public_send("retailops_extend_#{name}=", value)
              elsif assocs.key? name
                shipment.attributes = { name => assocs[name].find_or_create_by!(name: value) }
              elsif shipment.class.column_names.include? name
                shipment.attributes = { name => value }
              end
            end
            shipment.shipping_rates.delete_all
            shipment.cost = 0.to_d
            shipment.add_shipping_method(@order_helper.advisory_method(method), true)
          end
def apply_shipcode ( shipment , pkg ) if shipment . respond_to? :retailops_set_tracking shipment . retailops_set_tracking ( pkg ) return end shipcode = pkg [ "shipcode" ] . to_s shipment . tracking = pkg [ 'tracking' ] . to_s shipment . created_at = Time . parse ( pkg [ 'date' ] . to_s ) method , * fields = shipcode . split ( '|' ) method = URI . parser . unescape ( method . encode ( Encoding :: UTF_8 ) ) assocs = nil fields . each do | part | name , value = part . split ( ':' ) value = URI . parser . unescape ( value . encode ( Encoding :: UTF_8 ) ) assocs ||= shipment . class . reflect_on_all_associations . each_with_object ( { } ) { | a , h | h [ a . name . to_s ] = a . klass } if shipment . respond_to? "retailops_extend_#{name}=" shipment . public_send ( "retailops_extend_#{name}=" , value ) elsif assocs . key? name shipment . attributes = { name => assocs [ name ] . find_or_create_by! ( name : value ) } elsif shipment . class . column_names . include? name shipment . attributes = { name => value } end end shipment . shipping_rates . delete_all shipment . cost = 0 . to_d shipment . add_shipping_method ( @order_helper . advisory_method ( method ) , true ) 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 .
# ShippingMethod|extrafield:value|extrafield:value
shipment
<not_specific>
pkg
<not_specific>