Class: Inspection

Inherits:
Ekylibre::Record::Base show all
Includes:
Attachable
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-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: inspections

activity_id                    :integer          not null
comment                        :text
created_at                     :datetime         not null
creator_id                     :integer
forecast_harvest_week          :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?, #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

#any_quantity?Boolean

Returns:

  • (Boolean)

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

def any_quantity?
  %i[net_mass items_count].any? { |dim| quantity_measured?(dim) }
end

#column_for(dimension) ⇒ Object

CODE HELPERS


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

def column_for(dimension)
  :"#{dimension}_value"
end

#default_area_unitObject


254
255
256
# File 'app/models/inspection.rb', line 254

def default_area_unit
  :square_meter
end

#default_per_area_unit(dimension) ⇒ Object


242
243
244
245
246
# File 'app/models/inspection.rb', line 242

def default_per_area_unit(dimension)
  return :unity_per_square_meter     if dimension.to_sym == :items_count
  return :kilogram_per_square_meter  if dimension.to_sym == :net_mass
  unknown_dimension(dimension)
end

#default_quantity_unit(dimension) ⇒ Object


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

def default_quantity_unit(dimension)
  return :unity     if dimension.to_sym == :items_count
  return :kilogram  if dimension.to_sym == :net_mass
  unknown_dimension(dimension)
end

#marketable_quantity(dimension, scale = nil) ⇒ Object


113
114
115
# File 'app/models/inspection.rb', line 113

def marketable_quantity(dimension, scale = nil)
  calibration_values(dimension, :marketable_quantity, scale, true)
end

#marketable_yield(dimension, scale = nil) ⇒ Object


117
118
119
# File 'app/models/inspection.rb', line 117

def marketable_yield(dimension, scale = nil)
  calibration_values(dimension, :marketable_yield, scale, true)
end

#measure_grading(dimension) ⇒ Object

MEASURE


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

def measure_grading(dimension)
  send :"measure_grading_#{dimension}" # Delegated to activity
end

#points_of_category(category = nil) ⇒ Object


139
140
141
142
# File 'app/models/inspection.rb', line 139

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

#points_percentage(dimension, category = nil) ⇒ Object


156
157
158
159
160
161
# File 'app/models/inspection.rb', line 156

def points_percentage(dimension, category = nil)
  sum_on_points :percentage,
                round: false,
                from: category,
                with: dimension
end

#points_sum(dimension, category = nil) ⇒ Object

POINTS


131
132
133
# File 'app/models/inspection.rb', line 131

def points_sum(dimension, category = nil)
  sum_column_on(points_of_category(category), dimension)
end

#points_total(dimension, category = nil) ⇒ Object


144
145
146
147
148
# File 'app/models/inspection.rb', line 144

def points_total(dimension, category = nil)
  sum_on_points :projected_total,
                from: category,
                with: dimension
end

#points_unmarketable_sum(dimension) ⇒ Object


135
136
137
# File 'app/models/inspection.rb', line 135

def points_unmarketable_sum(dimension)
  sum_column_on(points.unmarketable, dimension)
end

#points_yield(dimension, category = nil) ⇒ Object


150
151
152
153
154
# File 'app/models/inspection.rb', line 150

def points_yield(dimension, category = nil)
  sum_on_points :quantity_yield,
                from: category,
                with: dimension
end

#positionObject

return the order of the grading relative to product


99
100
101
# File 'app/models/inspection.rb', line 99

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

#product_net_surface_areaObject


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

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

#projected_total(dimension, scale = nil) ⇒ Object


121
122
123
# File 'app/models/inspection.rb', line 121

def projected_total(dimension, scale = nil)
  calibration_values(dimension, :projected_total, scale)
end

#quantity(dimension, scale = nil) ⇒ Object

CALIBRATIONS


105
106
107
# File 'app/models/inspection.rb', line 105

def quantity(dimension, scale = nil)
  calibration_values(dimension, :quantity_in_unit, scale)
end

#quantity_measured?(dimension) ⇒ Boolean

Returns:

  • (Boolean)

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

def quantity_measured?(dimension)
  measure_grading(dimension) && quantity(dimension).to_f.nonzero?
end

#quantity_per_area_unit(dimension) ⇒ Object


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

def quantity_per_area_unit(dimension)
  possible_unit = "#{quantity_unit(dimension).name}_per_#{product_net_surface_area.unit}"
  return possible_unit if Nomen::Unit.find(possible_unit)
  default_per_area_unit(dimension)
end

#quantity_statable?(dimension) ⇒ Boolean

PREDICATES

Returns:

  • (Boolean)

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

def quantity_statable?(dimension)
  product_net_surface_area && measure_grading(dimension)
end

#quantity_unit(dimension) ⇒ Object

UNITS


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

def quantity_unit(dimension)
  return Nomen::Unit.find(default_quantity_unit(dimension)) if dimension.to_sym == :items_count
  return grading_net_mass_unit                              if dimension.to_sym == :net_mass
end

#quantity_yield(dimension, scale = nil) ⇒ Object


109
110
111
# File 'app/models/inspection.rb', line 109

def quantity_yield(dimension, scale = nil)
  calibration_values(dimension, :quantity_yield, scale)
end

#sample_area(unit: nil) ⇒ Object


171
172
173
174
# File 'app/models/inspection.rb', line 171

def sample_area(unit: nil)
  unit ||= default_area_unit
  sampling_area.to_d(unit)
end

#sampling_areaObject


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

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

#siblingsObject

ORDERING


94
95
96
# File 'app/models/inspection.rb', line 94

def siblings
  product.inspections
end

#total_area(unit: nil) ⇒ Object

AREAS


166
167
168
169
# File 'app/models/inspection.rb', line 166

def total_area(unit: nil)
  unit ||= default_area_unit
  product_net_surface_area.to_d(unit)
end

#unknown_dimension(dimension) ⇒ Object


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

def unknown_dimension(dimension)
  raise "Unknown dimension #{dimension.inspect}"
end

#unmarketable_rate(dimension) ⇒ Object


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

def unmarketable_rate(dimension)
  total = quantity(dimension)
  total.to_d.nonzero? && (points_unmarketable_sum(dimension) / total)
end

#user_per_area_unit(dimension) ⇒ Object


236
237
238
239
240
# File 'app/models/inspection.rb', line 236

def user_per_area_unit(dimension)
  return :thousand_per_hectare      if dimension.to_sym == :items_count
  return :ton_per_hectare           if dimension.to_sym == :net_mass
  unknown_dimension(dimension)
end

#user_quantity_unit(dimension) ⇒ Object


230
231
232
233
234
# File 'app/models/inspection.rb', line 230

def user_quantity_unit(dimension)
  return :thousand  if dimension.to_sym == :items_count
  return :ton       if dimension.to_sym == :net_mass
  unknown_dimension(dimension)
end