Sha256: 276a47c7c6a3b11c0275a310225a168f02002e287dedae2baf4dea2380a39ca2

Contents?: true

Size: 1.42 KB

Versions: 16

Compression:

Stored size: 1.42 KB

Contents

module Comee
  module Core
    class SalesOrder < ApplicationRecord
      belongs_to :client
      has_many :sales_order_items

      enum :status, {draft: 0, confirmed: 1, cancelled: 2}

      validates :order_number, presence: true, uniqueness: true
      validates :order_date, :expected_delivery_date, :delivery_address, :invoice_address, :status, presence: true
      validates :payment_no_of_days,
                :payment_discount,
                :payment_penalty,
                presence: true,
                numericality: {greater_than_or_equal_to: 0, less_than_or_equal_to: 100}

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

      def confirm
        raise(StandardError, "Sales order is already confirmed.") if SalesOrder.statuses[status] == SalesOrder.statuses[:confirmed]

        self.status = SalesOrder.statuses[:confirmed]
        save
      end

      def cancel
        raise(StandardError, "Sales order is already cancelled.") if SalesOrder.statuses[status] == SalesOrder.statuses[:cancelled]

        self.status = SalesOrder.statuses[:cancelled]
        save
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[
          delivery_address
          expected_delivery_date
          invoice_address
          order_date
          order_number
          status
          client_id
        ]
      end

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

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
comee_core-0.1.39 app/models/comee/core/sales_order.rb
comee_core-0.1.38 app/models/comee/core/sales_order.rb
comee_core-0.1.37 app/models/comee/core/sales_order.rb
comee_core-0.1.36 app/models/comee/core/sales_order.rb
comee_core-0.1.35 app/models/comee/core/sales_order.rb
comee_core-0.1.34 app/models/comee/core/sales_order.rb
comee_core-0.1.33 app/models/comee/core/sales_order.rb
comee_core-0.1.32 app/models/comee/core/sales_order.rb
comee_core-0.1.31 app/models/comee/core/sales_order.rb
comee_core-0.1.30 app/models/comee/core/sales_order.rb
comee_core-0.1.29 app/models/comee/core/sales_order.rb
comee_core-0.1.28 app/models/comee/core/sales_order.rb
comee_core-0.1.27 app/models/comee/core/sales_order.rb
comee_core-0.1.26 app/models/comee/core/sales_order.rb
comee_core-0.1.25 app/models/comee/core/sales_order.rb
comee_core-0.1.24 app/models/comee/core/sales_order.rb