Sha256: 3c3eae596efb8b4d9cc06b4dd94de59fc96f70b6cc872a2f9b361768f48abb3c

Contents?: true

Size: 1.6 KB

Versions: 3

Compression:

Stored size: 1.6 KB

Contents

module Comee
  module Core
    class SalesOrder < ApplicationRecord
      before_save :total_price
      before_save :set_parent_client_name

      belongs_to :customer_order
      belongs_to :fulfillment_center, optional: true
      belongs_to :client
      has_many :sales_order_items
      has_many_attached :files
      has_one :customs_detail

      enum :status, {draft: 0, submitted: 1, confirmed: 2, accepted: 3, canceled: 4}
      enum :purchase_status, {unprocessed: 0, processed: 1}

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

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

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

      def set_parent_client_name
        self.parent_client_name = client.parent&.name
      end

      def self.ransackable_attributes(_auth_object = nil)
        %w[
          id
          order_number
          status
          purchase_status
          customer_order_id
        ]
      end

      def self.ransackable_associations(_auth_object = nil)
        ["client"]
      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

3 entries across 3 versions & 1 rubygems

Version Path
comee_core-0.2.77 app/models/comee/core/sales_order.rb
comee_core-0.2.76 app/models/comee/core/sales_order.rb
comee_core-0.2.75 app/models/comee/core/sales_order.rb