Class: Synel::InventoryExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/synel/inventory_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, exchanger_name, export, exporters, find, 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 Synel Inventories


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
82
# File 'app/exchangers/synel/inventory_exchanger.rb', line 4

def import
  male_adult_cow = ProductNatureVariant.import_from_nomenclature(:male_adult_cow)
  place = BuildingDivision.last # find_by_work_number("B07_D2")
  owner = Entity.where(of_company: false).reorder(:id).first

  rows = CSV.read(file, encoding: 'CP1252', col_sep: "\t", headers: true)
  w.count = rows.size

  # find animals credentials in preferences
  identifier = Identifier.find_by(nature: :cattling_root_number)
  cattling_root_number = identifier ? identifier.value : '??????????'
  parents = { mother: {}, father: {} }

  rows.each do |row|
    born_on = (row[4].blank? ? nil : Date.parse(row[4]))
    incoming_on = (row[6].blank? ? nil : Date.parse(row[6]))
    outgoing_on = (row[12].blank? ? nil : Date.parse(row[12]))

    r = OpenStruct.new(
      work_number: row[0],
      identification_number: (row[0] ? cattling_root_number + row[0].to_s : nil),
      name: (row[1].blank? ? FFaker::Name.first_name + ' (MN)' : row[1].capitalize),
      mother_variety_code: (row[13].blank? ? nil : row[13]),
      father_variety_code: (row[14].blank? ? nil : row[14]),
      sex: (row[3].blank? ? nil : (row[3] == 'F' ? :female : :male)),
      born_on: born_on,
      born_at: (born_on ? born_on.to_datetime + 10.hours : nil),
      incoming_cause: row[5],
      incoming_on: incoming_on,
      incoming_at: (incoming_on ? incoming_on.to_datetime + 10.hours : nil),
      mother_identification_number: row[7],
      mother_work_number: (row[7] ? row[7][-4..-1] : nil),
      mother_name: (row[8].blank? ? FFaker::Name.first_name : row[8].capitalize),
      father_identification_number: row[9],
      father_work_number: (row[9] ? row[9][-4..-1] : nil),
      father_name: (row[10].blank? ? FFaker::Name.first_name : row[10].capitalize),
      outgoing_cause: row[11],
      outgoing_on: outgoing_on,
      outgoing_at: (outgoing_on ? outgoing_on.to_datetime + 10.hours : nil)
    )
    # check if animal is present in DB
    next unless animal = Animal.find_by(identification_number: r.identification_number)

    # Find mother
    unless r.mother_identification_number.blank? &&
           Animal.find_by(identification_number: r.mother_identification_number)
      parents[:mother][r.mother_identification_number] ||=
        Animal.find_by(identification_number: r.mother_identification_number)
      link = animal.links.new(nature: :mother, started_at: animal.born_at)
      link.linked = parents[:mother][r.mother_identification_number]
      link.save
    end

    # find a the father variety from field in file
    father_items = Nomen::Variety.where(french_race_code: r.father_variety_code)
    father_bos_variety = father_items ? father_items.first.name : 'bos'

    # Find or create father
    unless r.father_identification_number.blank?
      parents[:father][r.father_identification_number] ||=
        Animal.find_by(identification_number: r.father_identification_number) ||
        Animal.create!(
          variant_id: male_adult_cow.id,
          name: r.father_name,
          variety: father_bos_variety,
          identification_number: r.father_identification_number,
          work_number: r.father_work_number,
          initial_owner: owner,
          initial_container: place,
          default_storage: place
        )
      link = animal.links.new(nature: :father, started_at: animal.born_at)
      link.linked = parents[:father][r.father_identification_number]
      link.save
    end
    w.check_point
  end
  parents = nil
end