Sha256: f1eb2adbab9e05974fd0c5b54408ffafc03fb1bf1e5332ac917b584d4f812550

Contents?: true

Size: 1.59 KB

Versions: 4

Compression:

Stored size: 1.59 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

        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 attributes_for_gl_transaction
            { source: self, location: location,
             description: "IA #{self.visible_id}" }
        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

4 entries across 4 versions & 2 rubygems

Version Path
stockor-0.1.8 lib/skr/models/inventory_adjustment.rb
stockor-0.1.7 lib/skr/models/inventory_adjustment.rb
stockor-0.1.5 lib/skr/models/inventory_adjustment.rb
stockor-core-0.2 lib/skr/inventory_adjustment.rb