Sha256: d7538a43b2c2d2342629fa0f44e9fd806be6304dd47663072ff32be68197ac01

Contents?: true

Size: 1.32 KB

Versions: 16

Compression:

Stored size: 1.32 KB

Contents

module Comee
  module Core
    class SalesOrder < ApplicationRecord
      before_save :total_price

      belongs_to :customer_order
      belongs_to :fulfillment_center, optional: true
      has_many :sales_order_items
      has_many_attached :files

      enum :status, {draft: 0, submitted: 1, confirmed: 2, canceled: 3}

      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 self.ransackable_attributes(_auth_object = nil)
        %w[
          order_number
          status
          customer_order_id
        ]
      end

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

      def files_url
        files.attached? ? files.map { |file| Rails.application.routes.url_helpers.rails_blob_url(file, only_path: false) } : []
      end
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
comee_core-0.1.69 app/models/comee/core/sales_order.rb
comee_core-0.1.68 app/models/comee/core/sales_order.rb
comee_core-0.1.67 app/models/comee/core/sales_order.rb
comee_core-0.1.66 app/models/comee/core/sales_order.rb
comee_core-0.1.65 app/models/comee/core/sales_order.rb
comee_core-0.1.64 app/models/comee/core/sales_order.rb
comee_core-0.1.63 app/models/comee/core/sales_order.rb
comee_core-0.1.62 app/models/comee/core/sales_order.rb
comee_core-0.1.61 app/models/comee/core/sales_order.rb
comee_core-0.1.60 app/models/comee/core/sales_order.rb
comee_core-0.1.59 app/models/comee/core/sales_order.rb
comee_core-0.1.58 app/models/comee/core/sales_order.rb
comee_core-0.1.57 app/models/comee/core/sales_order.rb
comee_core-0.1.56 app/models/comee/core/sales_order.rb
comee_core-0.1.55 app/models/comee/core/sales_order.rb
comee_core-0.1.54 app/models/comee/core/sales_order.rb