Class: Ekylibre::AccountsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/accounts_exchanger.rb

Overview

Permits to import accounts from a CSV file with 3 columns:

- Name
- Family (from nomenclature ActivityFamily)
- Cultivation variety (from nomenclature Variety)

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

#checkObject


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'app/exchangers/ekylibre/accounts_exchanger.rb', line 7

def check
  valid = true
  rows = CSV.read(file, headers: true).delete_if { |r| r[0].blank? }
  w.count = rows.size
  rows.each do |row|
    r = {
      number: row[0].to_s.strip.gsub(/0+\z/, ''),
      name: (row[1].blank? ? nil : row[1].to_s.strip),
      nature: (row[2].blank? ? nil : row[2].to_sym)
    }.to_struct

    w.check_point
  end
  valid
end

#importObject

Create or updates accounts


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
# File 'app/exchangers/ekylibre/accounts_exchanger.rb', line 24

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

  rows.each_with_index do |row, index|
    line_number = index + 2
    r = {
      number: row[0].to_s.strip.gsub(/0+\z/, ''),
      name: (row[1].blank? ? nil : row[1].to_s),
      nature: (row[2].blank? ? nil : row[2].to_sym)
    }.to_struct

    # puts "line : #{line_number} - number : #{r.number}".inspect.red

    # get usage from parent account or import account from nomenclature
    usages = Account.find_parent_usage(r.number)

    attributes = {
      name: r.name,
      number: r.number,
      usages: usages
    }

     = Account.find_or_initialize_by(number: r.number)
    .attributes = attributes
    .save!

    # puts "line : #{line_number} - account created/updated : #{account.name}".inspect.green

    w.check_point
  end
end