Sha256: 7901d9c09886b4033ecd8ace13bc851e2c9fe5060a669bb5f0cdeccd9f2c7c94

Contents?: true

Size: 1.31 KB

Versions: 16

Compression:

Stored size: 1.31 KB

Contents

module Comee
  module Core
    class SalesOrderItem < ApplicationRecord
      before_validation :set_delivery_date, if: proc { delivery_date.nil? }
      before_validation :update_quantity_remaining, if: proc { quantity_remaining.nil? }

      belongs_to :sales_order
      belongs_to :product
      has_many :warehouse_shipment_items

      validates :delivery_date, presence: true
      validates :quantity, :price, presence: true, numericality: {greater_than: 0}
      validates :quantity_en_route, numericality: {greater_than_or_equal_to: 0}
      validates :quantity_remaining, numericality: {greater_than: 0}

      delegate(:name, to: :product, prefix: true)
      delegate(:code, to: :product, prefix: true)

      def set_delivery_date
        self.delivery_date = sales_order.expected_delivery_date
      end

      def update_quantity_remaining
        self.quantity_remaining = quantity
        return unless quantity_en_route_changed? && quantity_en_route_was.positive? && quantity_en_route.zero?

        self.quantity_remaining -= quantity_en_route_was
      end

      def update_quantity_en_route(quantity)
        self.quantity_en_route = quantity
        save
      end

      def confirm_delivery
        self.quantity_remaining -= quantity_en_route
        self.quantity_en_route = 0
        save
      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_item.rb
comee_core-0.1.38 app/models/comee/core/sales_order_item.rb
comee_core-0.1.37 app/models/comee/core/sales_order_item.rb
comee_core-0.1.36 app/models/comee/core/sales_order_item.rb
comee_core-0.1.35 app/models/comee/core/sales_order_item.rb
comee_core-0.1.34 app/models/comee/core/sales_order_item.rb
comee_core-0.1.33 app/models/comee/core/sales_order_item.rb
comee_core-0.1.32 app/models/comee/core/sales_order_item.rb
comee_core-0.1.31 app/models/comee/core/sales_order_item.rb
comee_core-0.1.30 app/models/comee/core/sales_order_item.rb
comee_core-0.1.29 app/models/comee/core/sales_order_item.rb
comee_core-0.1.28 app/models/comee/core/sales_order_item.rb
comee_core-0.1.27 app/models/comee/core/sales_order_item.rb
comee_core-0.1.26 app/models/comee/core/sales_order_item.rb
comee_core-0.1.25 app/models/comee/core/sales_order_item.rb
comee_core-0.1.24 app/models/comee/core/sales_order_item.rb