Class: Milklic::IndividualProductionExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/milklic/individual_production_exchanger.rb

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, deprecated?, exchanger_name, export, exporters, find, find_and_import, find_by, human_name, import, import!, importers, importers_selection, inherited, #initialize, register_exchanger

Constructor Details

This class inherits a constructor from ActiveExchanger::Base

Instance Method Details

#importObject


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/exchangers/milklic/individual_production_exchanger.rb', line 3

def import
  analyser_attributes = YAML.load_file(File.join(File.dirname(__FILE__), 'entity.yml'))

  unless analyser = Entity.find_by(siret_number: analyser_attributes[:siret_number])
    analyser = Entity.create!(analyser_attributes)
  end

  cattling_number = Identifier.find_by(nature: :cattling_number).value if Identifier.find_by(nature: :cattling_number)

  begin
    rows = CSV.read(file, encoding: 'CP1252', col_sep: ';', headers: true)
  rescue
    raise NotWellFormedFileError
  end
  w.count = rows.size

  rows.each_with_index do |row, _index|
    r = OpenStruct.new(
      animal_name: row[0],
      animal_work_number: row[1],
      animal_lactation_number: row[2],
      animal_lactation_started_on: (row[3].present? ? Date.strptime(row[3], '%d/%m/%y') : nil)
    )

    # if an animal exist
    if animal = Animal.find_by(work_number: r.animal_work_number)
      for i in 4..25
        next unless row[i] && row.headers[i]
        milk_daily_production_measure = row[i].tr(',', '.').to_d.in_kilogram_per_day
        milk_daily_production_at = Date.strptime(row.headers[i], '%d/%m/%y').to_time
        reference_number = cattling_number + '-L' + r.animal_lactation_number.to_s + '-C' + milk_daily_production_at.month.to_s

        unless analysis = Analysis.where(reference_number: reference_number, analyser: analyser).first
          analysis = Analysis.create!(reference_number: reference_number, nature: 'unitary_cow_milk_analysis',
                                      analyser: analyser, sampled_at: milk_daily_production_at, analysed_at: milk_daily_production_at)
          analysis.read!(:milk_daily_production, milk_daily_production_measure)
          analysis.product = animal
          analysis.save!
        end
        animal.read!(:milk_daily_production, milk_daily_production_measure, at: milk_daily_production_at, force: true) if milk_daily_production_measure && milk_daily_production_at
      end
    end
    w.check_point
  end
end