Sha256: b9f7eae4ece93bd9a8761b38f3a63747dd82fd2ee7b9eff2ee8396c82902c5c6

Contents?: true

Size: 1.52 KB

Versions: 2

Compression:

Stored size: 1.52 KB

Contents

module Skr

    class InventoryAdjustment < Skr::Model

        has_visible_id
        has_gl_transaction if: :should_apply_gl?

        has_one :gl_transaction, :as=>:source, :inverse_of=>:source

        belongs_to :location, export: true
        belongs_to :reason, :class_name=>'Skr::IaReason', export: true

        has_many :lines, :class_name=>'Skr::IaLine', :inverse_of=>:inventory_adjustment, export: { writable:true }

        validates :reason, :location, :set=>true
        validate :ensure_state_is_savable
        validates_associated :lines

        delegate_and_export :location_code
        delegate_and_export :reason_code

        enum state: {
            pending:  0,
            applied:  5
        }
        state_machine do
            state :pending , :initial=>true
            state :applied
            event :mark_applied do
                transitions from: :pending, to: :applied
                before :apply_adjustment
            end
        end

        private

        def should_apply_gl?
            state_event == :mark_applied
        end

        def ensure_state_is_savable
            if applied? && state_was == 'applied'
                errors.add('base' , "Cannot update record once it's approved and applied")
                return false
            end
        end

        def apply_adjustment
            self.lines.each do | line |
                next if line.is_applied?
                line.unlock_adjusting{ line.adjust_qty! }
            end
            true
        end

    end


end # Skr module

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stockor-0.5.0 lib/skr/models/inventory_adjustment.rb
stockor-0.4.0 lib/skr/models/inventory_adjustment.rb