Class: Synel::AnimalsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/synel/animals_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

Create or updates animals


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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/exchangers/synel/animals_exchanger.rb', line 4

def import
  demo_mode = Preference.value(:demo, false, :boolean)
  variants = {}
  owner = Entity.of_company
  now = Time.zone.now

  rows = CSV.read(file, encoding: 'CP1252', col_sep: ';', headers: true).delete_if { |r| r[4].blank? }
  w.count = rows.size

  rows.each do |row|
    born_on = (row[4].blank? ? nil : Date.parse(row[4]))
    dead_on = (row[10].blank? ? nil : Date.parse(row[10]))
    r = OpenStruct.new(
      country: row[0],
      identification_number: row[1],
      work_number: row[2],
      name: (row[3].blank? ? FFaker::Name.first_name + ' (MN)' : row[3].capitalize),
      born_on: born_on,
      born_at: (born_on ? born_on.to_datetime + 10.hours : nil),
      age: (born_on ? (Time.zone.today - born_on) : 0).to_f,
      corabo: row[5],
      sex: (row[6] == 'F' ? :female : :male),
      # :arrival_cause => (arrival_causes[row[7]] || row[7]),
      # :initial_arrival_cause => (initial_arrival_causes[row[7]] || row[7]),
      arrived_on: (row[8].blank? ? nil : Date.parse(row[8])),
      # :departure_cause => (departure_causes[row[9]] ||row[9]),
      departed_on: dead_on,
      dead_at: (dead_on ? dead_on.to_datetime + 20.hours : nil)
    )
    unless animal = Animal.find_by(identification_number: r.identification_number)
      group = nil

      # find a bos variety from corabo field in file
      item = Nomen::Variety.find_by(french_race_code: r.corabo)
      variety = (item ? item.name : :bos_taurus)
      variant = ProductNatureVariant.import_from_nomenclature(r.sex == :male ? :male_adult_cow : :female_adult_cow)

      animal = Animal.create!(
        variant: variant,
        name: r.name,
        variety: variety,
        identification_number: r.identification_number,
        work_number: r.work_number,
        initial_born_at: r.born_at,
        initial_dead_at: r.dead_at,
        initial_owner: owner,
        initial_population: 1.0,
        # initial_container: group.record.default_storage,
        default_storage: group ? group.record.default_storage : nil
      )
    end
    # Sex is already known but not if the group has no sex
    # animal.read!(:sex, r.sex, at: r.born_at) if animal.sex.blank?
    animal.read!(:healthy, true, at: r.born_at)

    # load demo data weight and state
    if demo_mode
      weighted_at = r.born_at
      if weighted_at && weighted_at < Time.zone.now
        variation = 0.02
        while (r.dead_at.nil? || weighted_at < r.dead_at) && weighted_at < Time.zone.now
          age = (weighted_at - r.born_at).to_f
          weight = (age < 990 ? 700 * Math.sin(age / (100 * 2 * Math::PI)) + 50.0 : 750)
          weight += rand(weight * variation * 2) - (weight * variation)
          animal.read!(:net_mass, weight.in_kilogram.round(1), at: weighted_at)
          weighted_at += (70 + rand(40)).days + 30.minutes - rand(60).minutes
        end
      end
    end

    if group
      animal.memberships.create!(group: group.record, started_at: arrived_on, nature: :interior)
      animal.memberships.create!(started_at: departed_on, nature: :exterior) if r.departed_on
    end

    w.check_point
  end
end