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-2016 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
custom_fields    :jsonb
id               :integer          not null, primary key
journal_entry_id :integer
lock_version     :integer          default(0), not null
name             :string           not null
number           :string
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?, attr_readonly_with_conditions, #check_if_destroyable?, #check_if_updateable?, columns_definition, complex_scopes, customizable?, #customizable?, #customized?, #destroyable?, #editable?, has_picture, #human_attribute_name, human_attribute_name_with_id, nomenclature_reflections, #old_record, #others, refers_to, scope_with_registration, simple_scopes, #updateable?

Instance Method Details

#build_missing_itemsObject


86
87
88
89
90
91
92
93
94
# File 'app/models/inventory.rb', line 86

def build_missing_items
  self.achieved_at ||= Time.zone.now
  Matter.at(achieved_at).of_owner(Entity.of_company).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

#reflectObject

Apply deltas on products

Raises:

  • (StandardError)

76
77
78
79
80
81
82
83
84
# File 'app/models/inventory.rb', line 76

def reflect
  raise StandardError, 'Not reflectable inventory' unless reflectable?
  self.class.transaction do
    self.reflected_at = Time.zone.now
    self.reflected = true
    save!
    items.find_each(&:save)
  end
end

#reflectable?Boolean

Returns:

  • (Boolean)

71
72
73
# File 'app/models/inventory.rb', line 71

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

#refresh!Object

Raises:

  • (StandardError)

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

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