Sha256: 5f1d823f32ddf94c043fccd1377423b4d38f70402ec02780a790afb7d11cd235

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

module Comee
  module Core
    class SalesOrder < ApplicationRecord
      before_save :total_price

      belongs_to :customer_order
      has_many :sales_order_items

      enum :status, {draft: 0, validated: 1, authorized: 2, approved: 3, cancelled: 4, confirmed: 5, finalized: 6}

      validates :order_number, presence: true, uniqueness: true
      validates :status, presence: true
      validates :payment_penalty,
                presence: true,
                numericality: {greater_than_or_equal_to: 0, less_than_or_equal_to: 100}

      validates :total_price,
                :amount_paid,
                presence: true,
                numericality: {greater_than_or_equal_to: 0}

      def total_price
        sales_order_items = SalesOrderItem.where(sales_order_id: id)
        total_price = sales_order_items.sum(&:price)
        self.total_price = total_price
      end

      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[
          order_number
          status
          customer_order_id
        ]
      end

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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
comee_core-0.1.41 app/models/comee/core/sales_order.rb