lib/spree/retailops/rop_order_helper.rb
# 2015-02-27: Transforming shipments to an advisory shipping method turns out to be a bad idea because that loses the information about what the original
# shipping method was, and we need that information in order to recalculate the shipping price for new/removed items in the new 'delegated' mode.  So
# instead, keep the existing shipment method, but set the adjustment to 0 and close it.
#
# When doing writebacks in RO-authoritative mode, price is the total RO shipping price while order_level is the part not attached to any line.
2015-02-27: Transforming shipments to an advisory shipping method turns out to be a bad idea because that loses the information about what the original
shipping method was, and we need that information in order to recalculate the shipping price for new/removed items in the new 'delegated' mode.  So
instead, keep the existing shipment method, but set the adjustment to 0 and close it.

When doing writebacks in RO-authoritative mode, price is the total RO shipping price while order_level is the part not attached to any line.
2015 - 02 - 27 : Transforming shipments to an advisory shipping method turns out to be a bad idea because that loses the information about what the original shipping method was and we need that information in order to recalculate the shipping price for new / removed items in the new ' delegated ' mode . So instead keep the existing shipment method but set the adjustment to 0 and close it . When doing writebacks in RO - authoritative mode price is the total RO shipping price while order_level is the part not attached to any line .
def apply_shipment_price(price, order_level = nil)
        changed = false
        return false if @order.canceled?
        if @order.respond_to?(:retailops_set_shipping_amt)
          return @order.retailops_set_shipping_amt( total_shipping_amt: price, order_shipping_amt: order_level )
        end
        target_ship = nil
        if Spree::Config[:retailops_express_shipping_price] != "adjustment"
          target_ship = @order.shipments.to_a.reject(&:shipped?).first || @order.shipments.first
        end
        @order.shipments.each do |shipment|
          this_ship_price = 0
          if shipment == target_ship
            this_ship_price = price
            price = 0 
          end
          rate = shipment.selected_shipping_rate
          unless shipment.selected_shipping_rate
            shipment.add_shipping_method(rop_tbd_method, true)
            rate = shipment.selected_shipping_rate
            shipment.save! 
            changed = true
          end
          if shipment.respond_to?(:adjustment_total) && shipment.adjustment_total > 0
            shipment.adjustments.delete_all
          end
          if shipment.cost != this_ship_price
            changed = true
            rate.cost = this_ship_price
            rate.save!
            if shipment.respond_to?(:adjustment)
              adj = shipment.adjustment
              adj.amount = this_ship_price
              adj.close if adj.open?
              adj.save!
            end
            shipment.cost = this_ship_price
            shipment.save!
          end
        end
        order_ship_adj = @order.adjustments.where(label: standard_shipping_label).first
        if price > 0 && !order_ship_adj
          @order.adjustments.create!(amount: price, label: standard_shipping_label, mandatory: false)
          @order.save!
          changed = true
        elsif order_ship_adj && order_ship_adj.amount != price
          order_ship_adj.amount = price
          order_ship_adj.save!
          @order.save!
          changed = true
        end
      end
def apply_shipment_price ( price , order_level = nil ) changed = false return false if @order . canceled? if @order . respond_to? ( :retailops_set_shipping_amt ) return @order . retailops_set_shipping_amt ( total_shipping_amt : price , order_shipping_amt : order_level ) end target_ship = nil if Spree :: Config [ :retailops_express_shipping_price ] != "adjustment" target_ship = @order . shipments . to_a . reject ( & :shipped? ) . first || @order . shipments . first end @order . shipments . each do | shipment | this_ship_price = 0 if shipment == target_ship this_ship_price = price price = 0 end rate = shipment . selected_shipping_rate unless shipment . selected_shipping_rate shipment . add_shipping_method ( rop_tbd_method , true ) rate = shipment . selected_shipping_rate shipment . save! changed = true end if shipment . respond_to? ( :adjustment_total ) && shipment . adjustment_total > 0 shipment . adjustments . delete_all end if shipment . cost != this_ship_price changed = true rate . cost = this_ship_price rate . save! if shipment . respond_to? ( :adjustment ) adj = shipment . adjustment adj . amount = this_ship_price adj . close if adj . open? adj . save! end shipment . cost = this_ship_price shipment . save! end end order_ship_adj = @order . adjustments . where ( label : standard_shipping_label ) . first if price > 0 && ! order_ship_adj @order . adjustments . create! ( amount : price , label : standard_shipping_label , mandatory : false ) @order . save! changed = true elsif order_ship_adj && order_ship_adj . amount != price order_ship_adj . amount = price order_ship_adj . save! @order . save! changed = true end end
2015-02-27: Transforming shipments to an advisory shipping method turns out to be a bad idea because that loses the information about what the original
shipping method was, and we need that information in order to recalculate the shipping price for new/removed items in the new 'delegated' mode.
2015 - 02 - 27 : Transforming shipments to an advisory shipping method turns out to be a bad idea because that loses the information about what the original shipping method was and we need that information in order to recalculate the shipping price for new / removed items in the new ' delegated ' mode .
# do not need adjustment # probably shouldn't happen # ensure that the adjustment is created #shipment.ensure_correct_adjustment # adjustment ought to exist by now... # Override and lock the shipment adjustment so that normal Spree rules won't apply to change it # otherwise setting the shipping rate was enough.  Can't actually close a shipping rate but hopefully those won't be recalculated too often
price
<not_specific>
order_level
<not_specific>