Class: InterventionParameter

Inherits:
Ekylibre::Record::Base show all
Defined in:
app/models/intervention_parameter.rb,
app/models/intervention_parameter/amount_computation.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: intervention_parameters

assembly_id              :integer
batch_number             :string
component_id             :integer
created_at               :datetime         not null
creator_id               :integer
currency                 :string
dead                     :boolean          default(FALSE), not null
event_participation_id   :integer
group_id                 :integer
id                       :integer          not null, primary key
identification_number    :string
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
unit_pretax_stock_amount :decimal(19, 4)   default(0.0), not null
updated_at               :datetime         not null
updater_id               :integer
variant_id               :integer
variety                  :string
working_zone             :geometry({:srid=>4326, :type=>"multi_polygon"})

Defined Under Namespace

Classes: AmountComputation

Class Method Summary collapse

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

Class Method Details

.calculate_cost_amount_computation(product_parameter) ⇒ Object


218
219
220
221
222
223
224
225
226
227
# File 'app/models/intervention_parameter.rb', line 218

def self.calculate_cost_amount_computation(product_parameter)
  if product_parameter.product.is_a?(Worker)
    computation = product_parameter.cost_amount_computation
  elsif product_parameter.product.try(:tractor?) && product_parameter.participation.present?
    computation = product_parameter.cost_amount_computation(natures: %i[travel intervention])
  else
    computation = product_parameter.cost_amount_computation(natures: %i[intervention])
  end
  computation
end

.compare_with_plannedObject


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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/models/intervention_parameter.rb', line 164

def self.compare_with_planned
  result = true
  associations = { InterventionDoer: :doers, InterventionTool: :tools, InterventionInput: :inputs }

  return true if self.all.empty?

  param = self.first
  if param.intervention.record?
    intervention = param.intervention.reload
    request_intervention = intervention.request_intervention.reload
  else
    request_intervention = param.intervention.reload
    intervention = request_intervention.record_interventions.first.reload
  end

  association = associations[param.type.to_sym]
  self_parameters = intervention.send(association)
  request_parameters = request_intervention.send(association)

  return false if (self_parameters.empty? && request_parameters.any?) || (self_parameters.any? && request_parameters.empty?)

  unless self_parameters.empty? && request_parameters.empty?
    return false if self_parameters.group_by(&:product_id).count != request_parameters.group_by(&:product_id).count

    request_parameters.group_by(&:product_id).each do |product_id, request_param|
      self_param = intervention.product_parameters.where(product_id: product_id)

      return false if self_param.empty?

      if self_param.first.input?
        self_quantity = self_param.map(&:quantity_population).compact.sum
        request_quantity = request_param.map(&:quantity_population).compact.sum

        percent = Intervention::PLANNED_REALISED_ACCEPTED_GAP[request_param.first.type.underscore.to_sym] || 1.2
        intervals = (request_quantity / percent..request_quantity * percent)

        return false unless intervals.include?(self_quantity)
      else
        rq_duration = 0
        request_param.each { |param| rq_duration += calculate_cost_amount_computation(param).quantity }

        self_duration = 0
        self_param.each { |param| self_duration += calculate_cost_amount_computation(param).quantity }

        percent = Intervention::PLANNED_REALISED_ACCEPTED_GAP[request_param.first.type.underscore.to_sym] || 1.2
        intervals = (rq_duration / percent..rq_duration * percent)

        return false unless intervals.include?(self_duration)
      end
    end
  end
  result
end

.order_by_product_nameObject


160
161
162
# File 'app/models/intervention_parameter.rb', line 160

def self.order_by_product_name
  joins(:product).order('products.name')
end

.roleObject


123
124
125
# File 'app/models/intervention_parameter.rb', line 123

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

Instance Method Details

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


148
149
150
# File 'app/models/intervention_parameter.rb', line 148

def cost(nature: nil, natures: {})
  cost_amount_computation(nature: nature, natures: natures).amount
end

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


144
145
146
# File 'app/models/intervention_parameter.rb', line 144

def cost_amount_computation(nature: nil, natures: {})
  AmountComputation.none
end

#earnObject


156
157
158
# File 'app/models/intervention_parameter.rb', line 156

def earn
  earn_amount_computation.amount
end

#earn_amount_computationObject


152
153
154
# File 'app/models/intervention_parameter.rb', line 152

def earn_amount_computation
  AmountComputation.none
end

#referenceObject

Returns a Procedo::Parameter corresponding to its reference_name in the current procedure


133
134
135
136
137
138
# File 'app/models/intervention_parameter.rb', line 133

def reference
  if @reference.blank? && intervention
    @reference = procedure.find(reference_name)
  end
  @reference
end

#roleObject


127
128
129
# File 'app/models/intervention_parameter.rb', line 127

def role
  self.class.role
end

#runnable?Boolean

Returns:

  • (Boolean)

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

def runnable?
  true
end