Sha256: 66d37a10958039ed77e80fc8fe02f70a0719e4f5b6f5f9645cab7bb0e1c3fad1

Contents?: true

Size: 1.54 KB

Versions: 166

Compression:

Stored size: 1.54 KB

Contents

module Comee
  module Core
    class BackOrder < ApplicationRecord
      enum :status, {draft: 0, submitted: 1, confirmed: 2, accepted: 3}

      belongs_to :supplier
      has_many :back_order_items

      has_noticed_notifications model_name: "Comee::Core::Notification"

      validates :order_number, presence: true, uniqueness: true
      validates :order_date, :delivery_date, :terms, :delivery_address, :invoice_address, :status, presence: true

      delegate(:name, to: :supplier, prefix: true)

      def self.ransackable_attributes(_auth_object = nil)
        %w[id delivery_address delivery_date invoice_address order_date order_number status supplier_id]
      end

      def self.ransackable_associations(_auth_object = nil)
        []
      end

      def confirm
        raise "Order already confirmed." if BackOrder.statuses[status] == BackOrder.statuses[:confirmed]

        raise "Order should be in submitted state first." if BackOrder.statuses[status] != BackOrder.statuses[:submitted]

        self.status = BackOrder.statuses[:confirmed]
        back_order_items.each do |item|
          if item.supplier_unit_price == item.requested_unit_price
            self.price_changed = true
            break
          end
        end
        save!
      end

      def submit
        unless BackOrder.statuses[status] == BackOrder.statuses[:draft]
          raise(StandardError,
                "Back order can't be submitted if it is not in draft state.")
        end

        self.status = BackOrder.statuses[:submitted]
        save
      end
    end
  end
end

Version data entries

166 entries across 166 versions & 1 rubygems

Version Path
comee_core-0.3.62 app/models/comee/core/back_order.rb
comee_core-0.3.61 app/models/comee/core/back_order.rb
comee_core-0.3.60 app/models/comee/core/back_order.rb
comee_core-0.3.59 app/models/comee/core/back_order.rb
comee_core-0.3.58 app/models/comee/core/back_order.rb
comee_core-0.3.57 app/models/comee/core/back_order.rb
comee_core-0.3.56 app/models/comee/core/back_order.rb
comee_core-0.3.55 app/models/comee/core/back_order.rb
comee_core-0.3.54 app/models/comee/core/back_order.rb
comee_core-0.3.53 app/models/comee/core/back_order.rb
comee_core-0.3.51 app/models/comee/core/back_order.rb
comee_core-0.3.50 app/models/comee/core/back_order.rb
comee_core-0.3.49 app/models/comee/core/back_order.rb
comee_core-0.3.48 app/models/comee/core/back_order.rb
comee_core-0.3.47 app/models/comee/core/back_order.rb
comee_core-0.3.46 app/models/comee/core/back_order.rb
comee_core-0.3.45 app/models/comee/core/back_order.rb
comee_core-0.3.44 app/models/comee/core/back_order.rb
comee_core-0.3.43 app/models/comee/core/back_order.rb
comee_core-0.3.42 app/models/comee/core/back_order.rb