Class: InterventionProductParameter

Inherits:
InterventionParameter show all
Defined in:
app/models/intervention_product_parameter.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: intervention_parameters

assembly_id              :integer
batch_number             :string
component_id             :integer
created_at               :datetime         not null
creator_id               :integer
currency                 :string
dead                     :boolean          default(FALSE), not null
event_participation_id   :integer
group_id                 :integer
id                       :integer          not null, primary key
identification_number    :string
intervention_id          :integer          not null
lock_version             :integer          default(0), not null
new_container_id         :integer
new_group_id             :integer
new_name                 :string
new_variant_id           :integer
outcoming_product_id     :integer
position                 :integer          not null
product_id               :integer
quantity_handler         :string
quantity_indicator_name  :string
quantity_population      :decimal(19, 4)
quantity_unit_name       :string
quantity_value           :decimal(19, 4)
reference_name           :string           not null
type                     :string
unit_pretax_stock_amount :decimal(19, 4)   default(0.0), not null
updated_at               :datetime         not null
updater_id               :integer
variant_id               :integer
variety                  :string
working_zone             :geometry({:srid=>4326, :type=>"multi_polygon"})

Instance Method Summary collapse

Methods inherited from InterventionParameter

calculate_cost_amount_computation, compare_with_planned, #cost, #cost_amount_computation, #earn, #earn_amount_computation, order_by_product_name, #reference, #role, role

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

#default_nameObject

Compute a default with given environment


185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/models/intervention_product_parameter.rb', line 185

def default_name
  text = nil
  if reference.default_name?
    words = {
      campaign: campaign.name,
      activity: activity.name
    }.with_indifferent_access
    if produced = product
      words[:variant]     = produced.variant_name
      words[:variety]     = Nomen::Variety[produced.variety].human_name
      words[:derivative_of] = (produced.derivative_of ? Nomen::Variety[produced.variety].human_name : nil)
      words[:container] = (produced.container ? produced.container.name : nil)
      words[:default_storage] = (produced.default_storage ? produced.default_storage.name : nil)
      words[:born_at]     = produced.born_at.l
      words[:born_on]     = produced.born_at.to_date.l
      words[:birth_year]  = produced.born_at.year.to_s.rjust(4, '0')
      words[:birth_month] = produced.born_at.month.to_s.rjust(2, '0')
      words[:birth_day]   = produced.born_at.day.to_s.rjust(2, '0')
      words[:birth_month_name] = 'date.month_names'.t[produced.born_at.month]
      words[:birth_day_name]   = 'date.day_names'.t[produced.born_at.wday]
      words[:birth_month_abbr] = 'date.abbr_month_names'.t[produced.born_at.month]
      words[:birth_day_abbr]   = 'date.abbr_day_names'.t[produced.born_at.wday]
    end
    text = reference.default_name.dup.gsub(/\{\{\w+\}\}/) do |key|
      words[key[2..-3]]
    end
  end
  text
end

#human_quantityObject


147
148
149
# File 'app/models/intervention_product_parameter.rb', line 147

def human_quantity
  quantity.l if quantity
end

#is_population?Boolean

Returns:

  • (Boolean)

163
164
165
# File 'app/models/intervention_product_parameter.rb', line 163

def is_population?
  quantity_indicator_name == 'population'
end

#measurable?Boolean

Returns:

  • (Boolean)

159
160
161
# File 'app/models/intervention_product_parameter.rb', line 159

def measurable?
  quantity_handler? && quantity_handler_reference && quantity_handler_reference.measure?
end

#nameObject


143
144
145
# File 'app/models/intervention_product_parameter.rb', line 143

def name
  reference ? reference.human_name : reference_name.humanize
end

#quantity_handler_referenceObject


155
156
157
# File 'app/models/intervention_product_parameter.rb', line 155

def quantity_handler_reference
  reference.handler(quantity_handler)
end

#runnable?Boolean

Define if the cast is valid for run

Returns:

  • (Boolean)

216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'app/models/intervention_product_parameter.rb', line 216

def runnable?
  return true
  if reference.parted?
    if reference.known_variant?
      return quantity.present?
    else
      return (self.variant && quantity.present?)
    end
  elsif reference.produced?
    return self.variant
  elsif reference.type_variant?
    return self.variant
  else
    return product
  end
  false
end

#set_default_name!Object

Change name with default name like described in procedure if default-name attribute is given too. It uses interpolation to compose the wanted name. Not very i18nized for now, but permits to do the job.


178
179
180
181
182
# File 'app/models/intervention_product_parameter.rb', line 178

def set_default_name!
  if reference.default_name? && produced = product
    produced.update_column(:name, default_name)
  end
end

#units_selector_listObject


234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/models/intervention_product_parameter.rb', line 234

def units_selector_list
  options = self.reference.handlers.map do |handler|
    if handler.population?
      [Nomen::Unit.find(:unity).human_name, handler.name]
    elsif handler.widget == :number
      unit = handler.unit? ? handler.unit : Nomen::Unit.find(:unity)
      ["#{unit.symbol} (#{handler.human_name})", handler.name]
    else
      fail "Cannot handler #{handler.widget} handlers"
    end
  end
end

#working_zone_svgObject


151
152
153
# File 'app/models/intervention_product_parameter.rb', line 151

def working_zone_svg
  working_zone.to_svg if working_zone
end