Sha256: a9240c1e88cfebcd069c4a3a06addbd470ca4e0f2b9590bdbba65845c39189d4

Contents?: true

Size: 1.57 KB

Versions: 28

Compression:

Stored size: 1.57 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 anonymize!
      new_order.update!(user_id: nil, email: nil)
      new_payment.destroy!
      new_shippings.each(&:destroy!)
    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

28 entries across 28 versions & 1 rubygems

Version Path
workarea-core-3.5.27 app/services/workarea/copy_order.rb
workarea-core-3.5.26 app/services/workarea/copy_order.rb
workarea-core-3.5.25 app/services/workarea/copy_order.rb
workarea-core-3.5.23 app/services/workarea/copy_order.rb
workarea-core-3.5.22 app/services/workarea/copy_order.rb
workarea-core-3.5.21 app/services/workarea/copy_order.rb
workarea-core-3.5.20 app/services/workarea/copy_order.rb
workarea-core-3.5.19 app/services/workarea/copy_order.rb
workarea-core-3.5.18 app/services/workarea/copy_order.rb
workarea-core-3.5.17 app/services/workarea/copy_order.rb
workarea-core-3.5.16 app/services/workarea/copy_order.rb
workarea-core-3.5.15 app/services/workarea/copy_order.rb
workarea-core-3.5.14 app/services/workarea/copy_order.rb
workarea-core-3.5.13 app/services/workarea/copy_order.rb
workarea-core-3.5.12 app/services/workarea/copy_order.rb
workarea-core-3.5.11 app/services/workarea/copy_order.rb
workarea-core-3.5.10 app/services/workarea/copy_order.rb
workarea-core-3.5.9 app/services/workarea/copy_order.rb
workarea-core-3.5.8 app/services/workarea/copy_order.rb
workarea-core-3.5.7 app/services/workarea/copy_order.rb