Class: Inspection

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

activity_id                    :integer          not null
comment                        :text
created_at                     :datetime         not null
creator_id                     :integer
id                             :integer          not null, primary key
implanter_application_width    :decimal(19, 4)
implanter_rows_number          :integer
implanter_working_width        :decimal(19, 4)
lock_version                   :integer          default(0), not null
number                         :string           not null
product_id                     :integer          not null
product_net_surface_area_unit  :string
product_net_surface_area_value :decimal(19, 4)
sampled_at                     :datetime         not null
sampling_distance              :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

#items_count(scale) ⇒ Object


219
220
221
# File 'app/models/inspection.rb', line 219

def items_count(scale)
  calibrations.of_scale(scale).sum(:items_count)
end

#marketable_net_mass(scale = nil) ⇒ Object


247
248
249
250
251
252
253
# File 'app/models/inspection.rb', line 247

def marketable_net_mass(scale = nil)
  if scale.nil?
    (scales.map { |s| marketable_net_mass(s) }.sum / scales.count).round(0)
  else
    calibrations.of_scale(scale).marketable.map(&:marketable_net_mass).sum.round(0)
  end
end

#marketable_yield(scale = nil) ⇒ Object


255
256
257
258
259
260
261
# File 'app/models/inspection.rb', line 255

def marketable_yield(scale = nil)
  if scale.nil?
    (scales.map { |s| marketable_yield(s) }.sum / scales.count).round(0)
  else
    calibrations.of_scale(scale).marketable.map(&:marketable_yield).sum.round(0)
  end
end

#mass_statable?Boolean

# return the current stock in ground # unit could be :ton or :thousand # n: number of product or net mass of product # m: sampling distance value in meter (see abacus) # c: coefficient (see abacus) # total = n * ( plant_surface_area_in_hectare / m ) * c def product_stock_in_ground(unit = :ton)

# area unit
area_unit = unit.to_s + '_per_hectare'
# n
n = if unit == :ton || unit == :kilogram
      net_mass.convert(unit)
    elsif unit == :unity
      item_count
    else
      net_mass.to_d(:ton)
    end
# m
m = sampling_distance if sampling_distance
# c
c = 10_000 / implanter_working_width if implanter_working_width
# total
if n && c
  current_stock = n * (net_surface_area_in_hectare / m) * c
  return current_stock.to_d.in(unit.to_sym)
else
  return nil
end

end

Returns:

  • (Boolean)

176
177
178
# File 'app/models/inspection.rb', line 176

def mass_statable?
  product_net_surface_area && measure_grading_net_mass
end

#net_mass(scale = nil) ⇒ Object


223
224
225
226
227
228
229
# File 'app/models/inspection.rb', line 223

def net_mass(scale = nil)
  if scale.nil?
    (scales.map { |s| net_mass(s) }.sum / scales.count).round(0)
  else
    calibrations.of_scale(scale).sum(:net_mass_value).in(activity.grading_net_mass_unit)
  end
end

#net_mass_yield(scale = nil) ⇒ Object


239
240
241
242
243
244
245
# File 'app/models/inspection.rb', line 239

def net_mass_yield(scale = nil)
  if scale.nil?
    (scales.map { |s| net_mass_yield(s) }.sum / scales.count).round(0)
  else
    calibrations.of_scale(scale).map(&:net_mass_yield).sum.round(0)
  end
end

#points_items_count(category = nil) ⇒ Object


199
200
201
# File 'app/models/inspection.rb', line 199

def points_items_count(category = nil)
  points_of_category(category).sum(:items_count)
end

#points_net_mass(category = nil) ⇒ Object


203
204
205
# File 'app/models/inspection.rb', line 203

def points_net_mass(category = nil)
  points_of_category(category).sum(:net_mass_value).in(activity.grading_net_mass_unit)
end

#points_net_mass_percentage(category = nil) ⇒ Object


215
216
217
# File 'app/models/inspection.rb', line 215

def points_net_mass_percentage(category = nil)
  points_of_category(category).map(&:net_mass_percentage).sum
end

#points_net_mass_yield(category = nil) ⇒ Object


211
212
213
# File 'app/models/inspection.rb', line 211

def points_net_mass_yield(category = nil)
  points_of_category(category).map(&:net_mass_yield).sum.round(0)
end

#points_of_category(category = nil) ⇒ Object


194
195
196
197
# File 'app/models/inspection.rb', line 194

def points_of_category(category = nil)
  return points if category.blank?
  points.of_category(category)
end

#positionObject

return the order of the grading relative to product


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

def position
  siblings.reorder(:sampled_at).pluck(:id).index(id) + 1
end

#product_net_surface_areaObject


180
181
182
183
184
# File 'app/models/inspection.rb', line 180

def product_net_surface_area
  return nil if product_net_surface_area_value.blank? ||
                product_net_surface_area_unit.blank?
  product_net_surface_area_value.in(product_net_surface_area_unit)
end

#sampling_areaObject


190
191
192
# File 'app/models/inspection.rb', line 190

def sampling_area
  (sampling_length.to_d(:meter) * implanter_working_width).in(:square_meter)
end

#sampling_lengthObject


186
187
188
# File 'app/models/inspection.rb', line 186

def sampling_length
  (sampling_distance || 0).in(:meter)
end

#set_implanter_valuesObject


96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/inspection.rb', line 96

def set_implanter_values
  return unless product

  # get sowing intervention of current plant
  interventions = Intervention.with_outputs(product)

  equipment = nil

  if interventions.any?
    # get abilities of each tool to grab sower or implanter
    interventions.first.tools.each do |tool|
      if tool.product.able_to?('sow') || tool.product.able_to?('implant')
        equipment = tool.product
      end
    end

    if equipment
      # get rows_count and application_width of sower or implanter
      rows_count = equipment.rows_count(sampled_at) # if equipment.has_indicator?(rows_count)
      # rows_count = equipment.rows_count(self.sampled_at)
      application_width = equipment.application_width(sampled_at).convert(:meter) # if equipment.has_indicator?(application_width)
      # set rows_count to implanter_application_width
      self.implanter_rows_number ||= rows_count if rows_count
      self.implanter_application_width ||= application_width.to_d if application_width
    end
  end
end

#set_net_surface_areaObject


85
86
87
88
89
90
91
92
93
94
# File 'app/models/inspection.rb', line 85

def set_net_surface_area
  return unless product
  if product.net_surface_area
    self.product_net_surface_area_value ||= product.net_surface_area.to_d(:hectare)
    self.product_net_surface_area_unit ||= 'hectare'
  elsif product.shape
    self.product_net_surface_area_value ||= product.shape.area.to_d(:hectare)
    self.product_net_surface_area_unit ||= 'hectare'
  end
end

#siblingsObject


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

def siblings
  product.inspections
end

#total_net_mass(scale = nil) ⇒ Object


231
232
233
234
235
236
237
# File 'app/models/inspection.rb', line 231

def total_net_mass(scale = nil)
  if scale.nil?
    (scales.map { |s| total_net_mass(s) }.sum / scales.count).round(0)
  else
    calibrations.of_scale(scale).map(&:total_net_mass).sum
  end
end

#total_points_net_mass(category = nil) ⇒ Object


207
208
209
# File 'app/models/inspection.rb', line 207

def total_points_net_mass(category = nil)
  points_of_category(category).map(&:total_net_mass).sum.round(0)
end

#unmarketable_net_massObject


268
269
270
# File 'app/models/inspection.rb', line 268

def unmarketable_net_mass
  points.unmarketable.sum(:net_mass_value).in(activity.grading_net_mass_unit)
end

#unmarketable_rateObject


263
264
265
266
# File 'app/models/inspection.rb', line 263

def unmarketable_rate
  # raise [unmarketable_net_mass.to_s, total_net_mass.to_s].to_sentence
  net_mass.value != 0 ? unmarketable_net_mass / net_mass : nil
end