Class: Ekylibre::EquipmentsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/equipments_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 equipments


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
83
# File 'app/exchangers/ekylibre/equipments_exchanger.rb', line 4

def import
  rows = CSV.read(file, headers: true).delete_if { |r| r[0].blank? }
  w.count = rows.size

  rows.each do |row|
    r = {
      name: row[0].blank? ? nil : row[0].to_s,
      variant_reference_name: row[1].blank? ? nil : row[1].to_sym,
      work_number: row[2].blank? ? nil : row[2].to_s,
      place_code: row[3].blank? ? nil : row[3].to_s,
      born_at: (row[4].blank? ? Date.civil(2000, 2, 2) : row[4]).to_datetime,
      brand: row[5].blank? ? nil : row[5].to_s,
      model: row[6].blank? ? nil : row[6].to_s,
      external: row[7].present?,
      owner_name: row[7].blank? ? nil : row[7].to_s,
      indicators: row[8].blank? ? {} : row[8].to_s.strip.split(/[[:space:]]*\;[[:space:]]*/).collect { |i| i.split(/[[:space:]]*\:[[:space:]]*/) }.each_with_object({}) do |i, h|
        h[i.first.strip.downcase.to_sym] = i.second
        h
      end,
      notes: row[9].blank? ? nil : row[9].to_s,
      unit_pretax_amount: row[10].blank? ? nil : row[10].tr(',', '.').to_d,
      price_indicator: row[11].blank? ? nil : row[11].to_sym
    }.to_struct

    unless r.variant_reference_name
      w.warn "Need a Variant for #{r.name}"
      next
    end

    # find or import from variant reference_nameclature the correct ProductNatureVariant
    unless (variant = ProductNatureVariant.find_by(work_number: r.variant_reference_name))
      if Nomen::ProductNatureVariant.find(r.variant_reference_name.downcase.to_sym)
        variant = ProductNatureVariant.import_from_nomenclature(r.variant_reference_name.downcase.to_sym)
      else
        raise "No variant exist in NOMENCLATURE for #{r.variant_reference_name.inspect}"
      end
    end
    pmodel = variant.matching_model

    # create a price
    catalog = Catalog.find_by(usage: :cost)
    if r.unit_pretax_amount && catalog && catalog.items.where(variant: variant).empty?
      variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: r.unit_pretax_amount, currency: 'EUR') # , indicator_name: r.price_indicator.to_s
    end

    # create the owner if not exist
    if r.external == true
      owner = Entity.where(last_name: r.owner_name.to_s).first
      owner ||= Entity.create!(born_at: Time.zone.today, last_name: r.owner_name.to_s, currency: Preference[:currency], language: Preference[:language], nature: :organization)
    else
      owner = Entity.of_company
    end

    container = r.place_code.present? ? Product.find_by(work_number: r.place_code) : nil

    # create the equipment
    equipment = pmodel.create!(variant_id: variant.id, name: r.name, initial_born_at: r.born_at, initial_owner: owner, initial_container: container, default_storage: container, work_number: r.work_number)

    # create indicators linked to equipment
    r.indicators.each do |indicator, value|
      if indicator.to_sym == :population
        equipment.move!(value, at: r.born_at)
      else
        equipment.read!(indicator, value, at: r.born_at, force: true)
      end
    end

    # attach georeading if exist for equipment
    if equipment
      if georeading = Georeading.find_by(number: r.work_number, nature: :polygon)
        equipment.read!(:shape, georeading.content, at: r.born_at, force: true)
      end
      if georeading = Georeading.find_by(number: r.work_number, nature: :point)
        equipment.read!(:geolocation, georeading.content, at: r.born_at, force: true)
      end
    end

    w.check_point
  end
end