Sha256: 6f261df197e00c68bdb5308906fa0aa552454540cc4681178d35c64fdb2b3cda

Contents?: true

Size: 1.43 KB

Versions: 34

Compression:

Stored size: 1.43 KB

Contents

module Workarea
  class CopyOrder
    attr_reader :order, :params

    def initialize(order, params = {})
      @order = order
      @params = params || {}
    end

    def payment
      @payment ||= Payment.find(order.id) rescue nil
    end

    def shippings
      @shippings ||= Shipping.where(order_id: order.id).to_a
    end

    def new_order
      @new_order ||= order.dup
    end

    def new_payment
      @new_payment ||= payment.try(:dup)
    end

    def new_shippings
      @new_shippings ||= shippings.map(&:dup)
    end

    def perform
      save_new_order
      save_new_payment if new_payment.present?
      save_new_shippings if new_shippings.present?
      cancel_original if cancel_original?
    end

    def save_new_order
      Workarea.config.copy_order_ignored_fields.each do |field|
        new_order.send("#{field}=", nil)
      end
      new_order.copied_from = order
      new_order.save!
    end

    def save_new_payment
      new_payment.id = new_order.id
      new_payment.save!
    end

    def save_new_shippings
      new_shippings
        .each { |s| s.order_id = new_order.id }
        .each(&:save!)
    end

    def cancel_original?
      params[:cancel_original].to_s =~ /true/
    end

    def cancel_original
      return if order.canceled? || !order.placed?

      cancel = CancelOrder.new(
        order,
        restock: true,
        refund: true,
        fulfillment: true
      )

      cancel.perform
    end
  end
end

Version data entries

34 entries across 34 versions & 1 rubygems

Version Path
workarea-core-3.4.45 app/services/workarea/copy_order.rb
workarea-core-3.4.44 app/services/workarea/copy_order.rb
workarea-core-3.4.43 app/services/workarea/copy_order.rb
workarea-core-3.4.42 app/services/workarea/copy_order.rb
workarea-core-3.4.41 app/services/workarea/copy_order.rb
workarea-core-3.4.40 app/services/workarea/copy_order.rb
workarea-core-3.4.39 app/services/workarea/copy_order.rb
workarea-core-3.4.38 app/services/workarea/copy_order.rb
workarea-core-3.4.37 app/services/workarea/copy_order.rb
workarea-core-3.4.36 app/services/workarea/copy_order.rb
workarea-core-3.4.35 app/services/workarea/copy_order.rb
workarea-core-3.4.34 app/services/workarea/copy_order.rb
workarea-core-3.4.33 app/services/workarea/copy_order.rb
workarea-core-3.4.32 app/services/workarea/copy_order.rb
workarea-core-3.4.31 app/services/workarea/copy_order.rb
workarea-core-3.4.30 app/services/workarea/copy_order.rb
workarea-core-3.4.29 app/services/workarea/copy_order.rb
workarea-core-3.4.28 app/services/workarea/copy_order.rb
workarea-core-3.4.27 app/services/workarea/copy_order.rb
workarea-core-3.4.26 app/services/workarea/copy_order.rb