Class: Inventory

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable, Customizable
Defined in:
app/models/inventory.rb

Overview

Informations

License

Ekylibre - Simple agricultural ERP Copyright (C) 2008-2009 Brice Texier, Thibaud Merigon Copyright (C) 2010-2012 Brice Texier Copyright (C) 2012-2019 Brice Texier, David Joulin

This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License along with this program. If not, see www.gnu.org/licenses.

Table: inventories

accounted_at               :datetime
achieved_at                :datetime
created_at                 :datetime         not null
creator_id                 :integer
currency                   :string
custom_fields              :jsonb
financial_year_id          :integer
id                         :integer          not null, primary key
journal_entry_id           :integer
lock_version               :integer          default(0), not null
name                       :string           not null
number                     :string           not null
product_nature_category_id :integer
reflected                  :boolean          default(FALSE), not null
reflected_at               :datetime
responsible_id             :integer
updated_at                 :datetime         not null
updater_id                 :integer

Instance Method Summary collapse

Methods included from Customizable

#custom_value, #set_custom_value, #validate_custom_fields

Methods inherited from Ekylibre::Record::Base

#already_updated?, #check_if_destroyable?, #check_if_updateable?, columns_definition, #customizable?, customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, nomenclature_reflections, #old_record, #others, refers_to, #unsuppress, #updateable?

Methods included from Userstamp::Stampable

included

Methods included from Userstamp::Stamper

included

Instance Method Details

#build_missing_itemsObject


125
126
127
128
129
130
131
132
133
# File 'app/models/inventory.rb', line 125

def build_missing_items
  self.achieved_at ||= Time.zone.now
  Matter.at(achieved_at).mine_or_undefined(achieved_at).of_category(product_nature_category).includes(:populations).find_each do |product|
    next if items.detect { |i| i.product_id == product.id }
    population = product.population(at: self.achieved_at)
    # shape = product.shape(at: self.achieved_at)
    items.build(product_id: product.id, actual_population: population, expected_population: population)
  end
end

#printed_atObject


89
90
91
92
# File 'app/models/inventory.rb', line 89

def printed_at
  ActiveSupport::Deprecation.warn('Directly use achieved_at')
  achieved_at
end

#reflectObject

Apply deltas on products


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'app/models/inventory.rb', line 108

def reflect
  unless reflectable?
    errors.add(:reflected, :invalid)
    return false
  end
  self.reflected_at = Time.zone.now
  self.reflected = true
  return false unless valid? && items.all?(&:valid?)

  Ekylibre::Record::Base.transaction do
    save!
    items.find_each(&:save!)
  end

  true
end

#reflect!Object

Apply deltas on products and raises an error if any problem

Raises:

  • (StandardError)

103
104
105
# File 'app/models/inventory.rb', line 103

def reflect!
  raise StandardError, 'Cannot reflect inventory on stocks' unless reflect
end

#reflectable?Boolean

Returns:

  • (Boolean)

98
99
100
# File 'app/models/inventory.rb', line 98

def reflectable?
  !reflected? # && self.class.unreflecteds.before(self.achieved_at).empty?
end

#refresh!Object

Raises:

  • (StandardError)

135
136
137
138
139
140
# File 'app/models/inventory.rb', line 135

def refresh!
  raise StandardError, 'Cannot refresh uneditable inventory' unless editable?
  items.clear
  build_missing_items
  save!
end