Class: ProductNatureCategory

Inherits:
Ekylibre::Record::Base show all
Includes:
Customizable
Defined in:
app/models/product_nature_category.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: product_nature_categories

active                              :boolean          default(FALSE), not null
charge_account_id                   :integer
created_at                          :datetime         not null
creator_id                          :integer
custom_fields                       :jsonb
depreciable                         :boolean          default(FALSE), not null
description                         :text
fixed_asset_account_id              :integer
fixed_asset_allocation_account_id   :integer
fixed_asset_depreciation_method     :string
fixed_asset_depreciation_percentage :decimal(19, 4)   default(0.0)
fixed_asset_expenses_account_id     :integer
id                                  :integer          not null, primary key
lock_version                        :integer          default(0), not null
name                                :string           not null
number                              :string           not null
pictogram                           :string
product_account_id                  :integer
purchasable                         :boolean          default(FALSE), not null
reductible                          :boolean          default(FALSE), not null
reference_name                      :string
saleable                            :boolean          default(FALSE), not null
stock_account_id                    :integer
stock_movement_account_id           :integer
storable                            :boolean          default(FALSE), not null
subscribing                         :boolean          default(FALSE), not null
updated_at                          :datetime         not null
updater_id                          :integer

Class Method Summary collapse

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

Class Method Details

.any_reference_available?Boolean

Returns some nomenclature items are available to be imported, e.g. not already imported

Returns:

  • (Boolean)

140
141
142
# File 'app/models/product_nature_category.rb', line 140

def any_reference_available?
  Nomen::ProductNatureCategory.without(ProductNatureCategory.pluck(:reference_name).uniq).any?
end

.import_all_from_nomenclatureObject

Load.all product nature from product nature nomenclature


179
180
181
182
183
# File 'app/models/product_nature_category.rb', line 179

def import_all_from_nomenclature
  Nomen::ProductNatureCategory.find_each do |product_nature_category|
    import_from_nomenclature(product_nature_category)
  end
end

.import_from_nomenclature(reference_name, force = false) ⇒ Object

Load a product nature category from product nature category nomenclature


145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/models/product_nature_category.rb', line 145

def import_from_nomenclature(reference_name, force = false)
  unless (item = Nomen::ProductNatureCategory.find(reference_name))
    raise ArgumentError, "The product_nature_category #{reference_name.inspect} is unknown"
  end
  unless force
    category = ProductNatureCategory.find_by(reference_name: reference_name)
    return category if category
  end
  attributes = {
    active: true,
    name: item.human_name,
    reference_name: item.name,
    pictogram: item.pictogram,
    depreciable: item.depreciable,
    purchasable: item.purchasable,
    reductible: item.reductible,
    saleable: item.saleable,
    storable: item.storable,
    fixed_asset_depreciation_percentage: (item.depreciation_percentage.present? ? item.depreciation_percentage : 20),
    fixed_asset_depreciation_method: :simplified_linear
  }.with_indifferent_access
  %i[fixed_asset fixed_asset_allocation fixed_asset_expenses
     charge product stock stock_movement].each do ||
     = item.send("#{}_account")
    if .present?
      attributes["#{}_account"] = Account.find_or_import_from_nomenclature()
    end
  end
  # TODO: add in rake clean method a way to detect same translation in nomenclatures by locale (to avoid conflict with validation on uniq name for example)
  # puts "#{item.human_name} - #{item.name}".red
  create!(attributes)
end

Instance Method Details

#deliverable?Boolean

Returns:

  • (Boolean)

125
126
127
# File 'app/models/product_nature_category.rb', line 125

def deliverable?
  storable?
end

#labelObject


129
130
131
# File 'app/models/product_nature_category.rb', line 129

def label
  name # tc('label', :product_nature_category => self["name"])
end

#toObject


117
118
119
120
121
122
123
# File 'app/models/product_nature_category.rb', line 117

def to
  to = []
  to << :sales if saleable?
  to << :purchases if purchasable?
  # to << :produce if self.producible?
  to.collect { |x| tc('to.' + x.to_s) }.to_sentence
end