Class: InterventionInput

Inherits:
InterventionProductParameter show all
Defined in:
app/models/intervention_input.rb

Overview

An intervention input represents a product which is used and “consumed” by the intervention. The input is divided from a source product. Its tracking number follows the new product.

Instance Method Summary collapse

Methods inherited from InterventionProductParameter

#default_name, #human_quantity, #is_population?, #measurable?, #name, #quantity_handler_reference, #runnable?, #set_default_name!, #units_selector_list, #working_zone_svg

Methods inherited from InterventionParameter

calculate_cost_amount_computation, compare_with_planned, #cost, #earn, #earn_amount_computation, order_by_product_name, #reference, #role, role, #runnable?

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

#cost_amount_computation(nature: nil, natures: {}) ⇒ Object


206
207
208
209
210
211
212
213
214
215
216
217
218
# File 'app/models/intervention_input.rb', line 206

def cost_amount_computation(nature: nil, natures: {})
  return InterventionParameter::AmountComputation.failed unless product
  reception_item = product.incoming_parcel_item
  options = { quantity: quantity_population, unit_name: product.unit_name }
  if reception_item && reception_item.purchase_order_item
    options[:purchase_order_item] = reception_item.purchase_order_item
    return InterventionParameter::AmountComputation.quantity(:purchase, options)
  else
    options[:catalog_usage] = :purchase
    options[:catalog_item] = product.default_catalog_item(options[:catalog_usage])
    return InterventionParameter::AmountComputation.quantity(:catalog, options)
  end
end

#input_quantity_per_areaObject


88
89
90
91
92
93
94
95
96
97
# File 'app/models/intervention_input.rb', line 88

def input_quantity_per_area
  if intervention.working_zone_area.to_d > 0.0 && (quantity.dimension == :mass || quantity.dimension == :volume)
    unit = quantity.unit.to_s + '_per_hectare'
    q = (quantity.value.to_f / intervention.working_zone_area.to_f).round(2)
    q_per_hectare = Measure.new(q.to_f, unit.to_sym)
  elsif quantity.dimension == :volume_area_density || quantity.dimension == :mass_area_density
    q_per_hectare = quantity
  end
  q_per_hectare
end

return legal dose according to Lexicon phyto dataset and maaid number


116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/models/intervention_input.rb', line 116

def legal_pesticide_informations
  pesticide = RegisteredPhytosanitaryProduct.where(maaid: variant.france_maaid).first
  if pesticide
    specie = intervention.activity_productions.first.cultivation_variety
    usages = pesticide.usages.of_specie(specie)

    info = {}
    info[:name] = pesticide.proper_name
    info[:usage] = usages.first.target_name['fra'] if usages.first
    info[:dose] = Measure.new(usages.first.dose_quantity, usages.first.dose_unit) if usages.first
    info
  end
end

only case in mass_area_density && volume_area_density in legals


131
132
133
134
135
136
137
138
139
# File 'app/models/intervention_input.rb', line 131

def legal_treatment_ratio
  ratio = 1.0
  if legal_pesticide_informations[:dose].dimension == :mass_area_density && input_quantity_per_area.dimension == :mass_area_density
    ratio = input_quantity_per_area.convert(legal_pesticide_informations[:dose].unit) / legal_pesticide_informations[:dose].to_d
  elsif legal_pesticide_informations[:dose].dimension == :volume_area_density && input_quantity_per_area.dimension == :volume_area_density
    ratio = input_quantity_per_area.convert(legal_pesticide_informations[:dose].unit) / legal_pesticide_informations[:dose].to_d
  end
  ratio.to_d
end

#pfi_reference_doseObject

return pfi dose according to Lexicon pfi dataset and maaid number


100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/models/intervention_input.rb', line 100

def pfi_reference_dose
  dose = nil
  if variant.france_maaid
    act = intervention.activities
    first_production = intervention.activity_productions.first
    harvest_year = first_production.campaign.harvest_year if first_production && first_production.campaign
    crop_code = act.first.production_nature.pfi_crop_code if act.first.production_nature
    maaid = variant.france_maaid
    if crop_code && maaid && harvest_year
      dose = RegisteredPfiDose.where(maaid: maaid, crop_id: crop_code, harvest_year: harvest_year, target_id: nil).first
    end
  end
  dose
end

#pfi_treatment_ratioObject

only case in mass_area_density && volume_area_density in pfi reference


142
143
144
145
146
147
148
149
150
151
152
# File 'app/models/intervention_input.rb', line 142

def pfi_treatment_ratio
  ratio = 1.0
  if pfi_reference_dose && pfi_reference_dose.dose.to_d > 0.0
    if pfi_reference_dose.dose.dimension == :mass_area_density && input_quantity_per_area.dimension == :mass_area_density
      ratio = input_quantity_per_area.convert(pfi_reference_dose.dose.unit) / pfi_reference_dose.dose.to_d
    elsif pfi_reference_dose.dose.dimension == :volume_area_density && input_quantity_per_area.dimension == :volume_area_density
      ratio = input_quantity_per_area.convert(pfi_reference_dose.dose.unit) / pfi_reference_dose.dose.to_d
    end
  end
  ratio.to_d
end

#reglementary_status(target) ⇒ Object

from EPHY


155
156
157
158
159
160
161
162
163
164
165
166
167
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
197
198
199
200
# File 'app/models/intervention_input.rb', line 155

def reglementary_status(target)
  dose = quantity.convert(:liter_per_hectare)

  # if AMM number on product
  if variant.france_maaid

    # get agent if exist
    agent = Pesticide::Agent.find(variant.france_maaid)

    reglementary_doses = {}

    if agent.usages.any?
      # for each usages matching variety, get data in reglementary_doses hash
      agent.usages.each_with_index do |usage, index|
        next unless usage.subject_variety && usage.dose
        # get variables
        activity_variety = target.product.variety
        activity_variety ||= target.best_activity_production.cultivation_variety if target.best_activity_production
        uv = Nomen::Variety[usage.subject_variety.to_sym]

        next unless activity_variety && (uv >= Nomen::Variety[activity_variety.to_sym])
        reglementary_doses[index] = {}
        reglementary_doses[index][:name] = usage.name.to_s.downcase
        reglementary_doses[index][:variety] = uv.l
        reglementary_doses[index][:level] = :activity_variety
        reglementary_doses[index][:legal_dose] = usage.dose
        reglementary_doses[index][:max_inputs_count] = usage.max_inputs_count
        reglementary_doses[index][:untreated_zone_margin] = usage.untreated_zone_margin
        reglementary_doses[index][:pre_harvest_interval] = usage.pre_harvest_interval
        if usage.dose.is_a?(Measure) && quantity.is_a?(Measure) && usage.dose.dimension == :volume_area_density
          if usage.dose.convert(:liter_per_hectare) < quantity.convert(:liter_per_hectare)
            reglementary_doses[index][:status] = :stop
          elsif usage.dose.convert(:liter_per_hectare) == quantity.convert(:liter_per_hectare)
            reglementary_doses[index][:status] = :caution
          elsif usage.dose.convert(:liter_per_hectare) > quantity.convert(:liter_per_hectare)
            reglementary_doses[index][:status] = :go
          end
        end
      end
    end
    # puts reglementary_doses.inspect.green

    reglementary_doses

  end
end

#stock_amountObject


202
203
204
# File 'app/models/intervention_input.rb', line 202

def stock_amount
  quantity_population * unit_pretax_stock_amount
end