app/controllers/spree/api/retailops/orders_controller.rb
# This probably calls update! far more times than it needs to as a result of line item hooks, etc
# Exercise for interested parties: fix that
#
# Here are example parameters
#
#
# {
#   "order_amts" => {
#     "shipping_amt" => 4.98,
#     "discount_amt" => 0,
#     "tax_amt" => 0,
#     "direct_tax_amt" => 0
#   },
#   "rmas" => nil,
#   "line_items" => [
#     {
#       "estimated_extended_cost" => "27.50",
#       "apportioned_ship_amt" => 4.98,
#       "sku" => "136270",
#       "quantity" => "1",
#       "estimated_ship_date" => 1458221964,
#       "direct_ship_amt" => 0,
#       "corr" => "575714",
#       "removed" => nil,
#       "estimated_cost" => 27.5,
#       "estimated_unit_cost" => 27.5,
#       "unit_price" => 49.98
#     }
#   ],
#   "options" => {},
#   "order_refnum" => "R280725117",
#   "order" => {}
# }
#
This probably calls update. far more times than it needs to as a result of line item hooks, etc
Exercise for interested parties: fix that

Here are example parameters


This probably calls update . far more times than it needs to as a result of line item hooks etc Exercise for interested parties : fix that Here are example parameters
def synchronize
          authorize! :manage, Order
          changed = false
          result = []
          order = Order.find_by!(number: params["order_refnum"].to_s)
          @helper = Spree::Retailops::RopOrderHelper.new
          @helper.order = order
          @helper.options = options
          ActiveRecord::Base.transaction do
            if defined?(RetailopsLineItemUpdateHandler)
              line_items_changed, line_items_result = RetailopsLineItemUpdateHandler.new(order, params["line_items"].to_a).call
            else
              line_items_changed, line_items_result = Spree::Retailops::RopLineItemUpdater.new(order, params["line_items"].to_a).call
            end
            changed = true if line_items_changed
            result = result + line_items_result
            items_changed = changed
            order.all_adjustments.tax.each { |a| a.close if a.open? } 
            params["rmas"].to_a.each do |rma|
              changed = true if sync_rma order, rma
            end
            ro_amts = params['order_amts'] || {}
            if options["ro_authoritative_ship"]
              if ro_amts["shipping_amt"]
                total = ro_amts["shipping_amt"].to_d
                item_level = 0.to_d + params['line_items'].to_a.collect{ |l| l['direct_ship_amt'].to_d }.sum
                changed = true if @helper.apply_shipment_price(total, total - item_level)
              end
            elsif items_changed
              calc_ship = @helper.calculate_ship_price
              @helper.apply_shipment_price(calc_ship) if calc_ship
            end
            if changed
              if items_changed
                order.all_adjustments.tax.each { |a| a.open if a.closed? }
                order.adjustments.promotion.each { |a| a.open if a.closed? }
              end
              order.update!
              order.all_adjustments.tax.each { |a| a.close if a.open? }
              order.adjustments.promotion.each { |a| a.close if a.open? }
            end
            if order.respond_to?(:retailops_after_writeback)
              order.retailops_after_writeback(params)
            end
            order.update! if changed
          end
          render text: {
            changed: changed,
            dump: Extractor.walk_order_obj(order),
            result: result,
          }.to_json
        end
def synchronize authorize! :manage , Order changed = false result = [ ] order = Order . find_by! ( number : params [ "order_refnum" ] . to_s ) @helper = Spree :: Retailops :: RopOrderHelper . new @helper . order = order @helper . options = options ActiveRecord :: Base . transaction do if defined? ( RetailopsLineItemUpdateHandler ) line_items_changed , line_items_result = RetailopsLineItemUpdateHandler . new ( order , params [ "line_items" ] . to_a ) . call else line_items_changed , line_items_result = Spree :: Retailops :: RopLineItemUpdater . new ( order , params [ "line_items" ] . to_a ) . call end changed = true if line_items_changed result = result + line_items_result items_changed = changed order . all_adjustments . tax . each { | a | a . close if a . open? } params [ "rmas" ] . to_a . each do | rma | changed = true if sync_rma order , rma end ro_amts = params [ 'order_amts' ] || { } if options [ "ro_authoritative_ship" ] if ro_amts [ "shipping_amt" ] total = ro_amts [ "shipping_amt" ] . to_d item_level = 0 . to_d + params [ 'line_items' ] . to_a . collect { | l | l [ 'direct_ship_amt' ] . to_d } . sum changed = true if @helper . apply_shipment_price ( total , total - item_level ) end elsif items_changed calc_ship = @helper . calculate_ship_price @helper . apply_shipment_price ( calc_ship ) if calc_ship end if changed if items_changed order . all_adjustments . tax . each { | a | a . open if a . closed? } order . adjustments . promotion . each { | a | a . open if a . closed? } end order . update! order . all_adjustments . tax . each { | a | a . close if a . open? } order . adjustments . promotion . each { | a | a . close if a . open? } end if order . respond_to? ( :retailops_after_writeback ) order . retailops_after_writeback ( params ) end order . update! if changed end render text : { changed : changed , dump : Extractor . walk_order_obj ( order ) , result : result , } . to_json end
This probably calls update!
This probably calls update!
# RetailOps will be sending in an authoritative (potentially updated) list of line items # We make our data match that as well as possible, and then send the list back annotated # with channel_refnums and quantities/costs/etc # Allow tax to organically recalculate # omitted RMAs are treated as 'no action' # recalculate and apply ship price if we still have enough information to do so # calc_ship may be nil otherwise # Allow tax to organically recalculate # *slightly* against the spirit of adjustments to automatically reopen them, but this is triggered on item changes which are (generally) human-initiated in RO