Class: Legrain::Epicea::JournalsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/legrain/epicea/journals_exchanger.rb

Constant Summary collapse

JOURNAL_NATURES =
{
  AC: :purchases,
  B: :bank,
  OD: :various,
  VT: :sales
}.with_indifferent_access

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


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
# File 'app/exchangers/legrain/epicea/journals_exchanger.rb', line 11

def import
  rows = CSV.read(file, headers: true, encoding: 'cp1252', col_sep: ';', skip_blanks: true, quote_char: '🐧')

  Preference.set!(:currency, :EUR) unless Preference.find_by(name: :currency)

  entries = {}
  w.reset!(rows.count, :yellow)
  rows.each_with_index do |row, _index|
    number = row[1].to_s.strip
    unless entries[number]
      journal = Journal.create_with(
        name: "Journal #{row[0]}",
        nature: JOURNAL_NATURES[row[0].sub(/[0-9]/, '')]
      ).find_or_create_by!(
        code: row[0],
        currency: 'EUR'
      )
      entries[number] = {
        printed_on: Date.parse(row[2]),
        journal: journal,
        number: number,
        currency: journal.currency,
        items_attributes: {}
      }
    end
     = row[3].to_s.upcase
     = Account.find_or_create_by_number(, name: )
    id = (entries[number][:items_attributes].keys.max || 0) + 1
    entries[number][:items_attributes][id] = {
      real_debit: row[6].to_f,
      real_credit: row[7].to_f,
      account: ,
      name: row[5]
    }
  end

  started_on = entries.values.map { |v| v[:printed_on] }.uniq.sort.first
  FinancialYear.create!(started_on: started_on.beginning_of_month) unless FinancialYear.on(started_on)

  w.reset!(entries.keys.size)
  entries.values.each do |entry|
    JournalEntry.create!(entry)
    w.check_point
  end
end