Class: ManureManagementPlanZone

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/manure_management_plan_zone.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: manure_management_plan_zones

absorbed_nitrogen_at_opening                    :decimal(19, 4)
activity_production_id                          :integer          not null
administrative_area                             :string
computation_method                              :string           not null
created_at                                      :datetime         not null
creator_id                                      :integer
cultivation_variety                             :string
expected_yield                                  :decimal(19, 4)
humus_mineralization                            :decimal(19, 4)
id                                              :integer          not null, primary key
intermediate_cultivation_residue_mineralization :decimal(19, 4)
irrigation_water_nitrogen                       :decimal(19, 4)
lock_version                                    :integer          default(0), not null
maximum_nitrogen_input                          :decimal(19, 4)
meadow_humus_mineralization                     :decimal(19, 4)
mineral_nitrogen_at_opening                     :decimal(19, 4)
nitrogen_at_closing                             :decimal(19, 4)
nitrogen_input                                  :decimal(19, 4)
nitrogen_need                                   :decimal(19, 4)
organic_fertilizer_mineral_fraction             :decimal(19, 4)
plan_id                                         :integer          not null
previous_cultivation_residue_mineralization     :decimal(19, 4)
soil_nature                                     :string
soil_production                                 :decimal(19, 4)
updated_at                                      :datetime         not null
updater_id                                      :integer

Instance Method Summary collapse

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

#available_water_capacityObject

TODO: Compute available from parcels or CZ ?


118
119
120
# File 'app/models/manure_management_plan_zone.rb', line 118

def available_water_capacity
  0.0.in_liter_per_hectare
end

#computeObject


82
83
84
85
86
87
88
89
# File 'app/models/manure_management_plan_zone.rb', line 82

def compute
  for name, value in Calculus::ManureManagementPlan.compute(parameters)
    if %w(absorbed_nitrogen_at_opening expected_yield humus_mineralization intermediate_cultivation_residue_mineralization irrigation_water_nitrogen maximum_nitrogen_input meadow_humus_mineralization mineral_nitrogen_at_opening nitrogen_at_closing nitrogen_input nitrogen_need organic_fertilizer_mineral_fraction previous_cultivation_residue_mineralization soil_production).include?(name.to_s)
      send("#{name}=", value.to_f(:kilogram_per_hectare))
    end
  end
  save!
end

#cultivation_variety_nameObject


130
131
132
133
134
135
# File 'app/models/manure_management_plan_zone.rb', line 130

def cultivation_variety_name
  unless cultivation_variety && item = Nomen::Variety[cultivation_variety].human_name
    return nil
  end
  item
end

#estimate_expected_yieldObject


76
77
78
79
80
# File 'app/models/manure_management_plan_zone.rb', line 76

def estimate_expected_yield
  if computation_method
    self.expected_yield = Calculus::ManureManagementPlan.estimate_expected_yield(parameters).to_f(plan.mass_density_unit)
  end
end

#parametersObject


91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/models/manure_management_plan_zone.rb', line 91

def parameters
  hash = {
    available_water_capacity: available_water_capacity,
    opened_at: opened_at,
    support: activity_production
  }
  if activity_production.usage
    hash[:production_usage] = Nomen::ProductionUsage[activity_production.usage]
  end
  if computation_method && Calculus::ManureManagementPlan.method_exist?(computation_method.to_sym)
    hash[:method] = computation_method.to_sym
  else
    Rails.logger.warn "Method #{computation_method} doesn't exist. Use default method instead."
    hash[:method] = :external
  end
  if administrative_area
    hash[:administrative_area] = Nomen::AdministrativeArea[administrative_area]
  end
  hash[:variety] = Nomen::Variety[cultivation_variety] if cultivation_variety
  hash[:soil_nature] = Nomen::SoilNature[soil_nature] if soil_nature
  if expected_yield
    hash[:expected_yield] = expected_yield.in(plan.mass_density_unit)
  end
  hash
end

#soil_nature_nameObject

To have human_name in report


123
124
125
126
127
128
# File 'app/models/manure_management_plan_zone.rb', line 123

def soil_nature_name
  unless soil_nature && item = Nomen::SoilNature[soil_nature].human_name
    return nil
  end
  item
end