Class: InterventionAgent

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

Overview

This class is used for all intervenants that make the interventions. It gathers tools and doers.

Direct Known Subclasses

InterventionDoer, InterventionTool

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

#catalog_usageObject


130
131
132
# File 'app/models/intervention_agent.rb', line 130

def catalog_usage
  :cost
end

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


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/intervention_agent.rb', line 75

def cost_amount_computation(nature: nil, natures: {})
  return InterventionParameter::AmountComputation.failed unless product

  quantity = if natures.empty?
               nature_quantity(nature)
             else
               natures_quantity(natures)
             end

  unit_name = Nomen::Unit.find(:hour).human_name
  unit_name = unit_name.pluralize if quantity > 1

  catalog_item =
    begin
      if nature.present? && nature != :intervention
        product.variant.catalog_items.joins(:catalog).where('catalogs.usage': "#{nature}_cost").first.catalog.usage
      else
        product.variant.catalog_items.joins(:catalog).where('catalogs.usage': 'cost').first.catalog.usage
      end
    rescue
      catalog_usage
    end

  options = {
    catalog_usage: catalog_item,
    quantity: quantity.to_d,
    unit_name: unit_name
  }

  options[:catalog_item] = product.default_catalog_item(options[:catalog_usage])
  InterventionParameter::AmountComputation.quantity(:catalog, options)
end

#nature_quantity(nature) ⇒ Object


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

def nature_quantity(nature)
  InterventionWorkingTimeDurationCalculationService
    .new(**working_duration_params)
    .perform(nature: nature)
end

#natures_quantity(natures) ⇒ Object


114
115
116
117
118
119
120
121
122
# File 'app/models/intervention_agent.rb', line 114

def natures_quantity(natures)
  quantity = 0

  natures.each do |nature|
    quantity += nature_quantity(nature)
  end

  quantity
end

#participationObject

return participation if exist


69
70
71
72
73
# File 'app/models/intervention_agent.rb', line 69

def participation
  if product
    participation = InterventionParticipation.find_by(product: product, intervention: intervention)
  end
end

#working_duration_paramsObject


108
109
110
111
112
# File 'app/models/intervention_agent.rb', line 108

def working_duration_params
  { intervention: intervention,
    participations: intervention.participations,
    product: product }
end