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-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: intervention_parameters

created_at              :datetime         not null
creator_id              :integer
event_participation_id  :integer
group_id                :integer
id                      :integer          not null, primary key
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
updated_at              :datetime         not null
updater_id              :integer
variant_id              :integer
working_zone            :geometry({:srid=>4326, :type=>"multi_polygon"})

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from InterventionParameter

#cost, #cost_amount_computation, #earn, #earn_amount_computation, #reference

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?

Class Method Details

.roleObject


126
127
128
# File 'app/models/intervention_product_parameter.rb', line 126

def self.role
  @name ||= name.gsub(/^Intervention/, '').underscore.to_sym
end

Instance Method Details

#default_nameObject

Compute a default with given environment


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'app/models/intervention_product_parameter.rb', line 168

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


134
135
136
# File 'app/models/intervention_product_parameter.rb', line 134

def human_quantity
  quantity.l if quantity
end

#measurable?Boolean

Returns:

  • (Boolean)

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

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

#nameObject


122
123
124
# File 'app/models/intervention_product_parameter.rb', line 122

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

#quantity_handler_referenceObject


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

def quantity_handler_reference
  reference.handler(quantity_handler)
end

#roleObject


130
131
132
# File 'app/models/intervention_product_parameter.rb', line 130

def role
  self.class.role
end

#runnable?Boolean

Define if the cast is valid for run

Returns:

  • (Boolean)

199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/intervention_product_parameter.rb', line 199

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.


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

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

#working_zone_svgObject


138
139
140
# File 'app/models/intervention_product_parameter.rb', line 138

def working_zone_svg
  working_zone.to_svg if working_zone
end