Class: Ekylibre::CashTransfertsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/cash_transferts_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 cash_transfert between internal accounts


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
# File 'app/exchangers/ekylibre/cash_transferts_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 = {
      started_on: row[0].blank? ? nil : Date.parse(row[0].to_s),
      sender_account_name: row[1].blank? ? nil : row[1].to_s,
      receiver_account_name: row[2].blank? ? nil : row[2].to_s,
      amount: row[3].blank? ? nil : row[3].tr(',', '.').to_d
    }.to_struct

    unless r.started_on
      w.error "Need a valid date for #{r.started_on}"
      next
    end

    # find emission cash by name or bank name or iban
    emission_cash = Cash.where('name ILIKE ?', r.).first
    emission_cash ||= Cash.where('bank_name ILIKE ?', r.).first
    emission_cash ||= Cash.find_by(iban: r.)

    unless emission_cash
      w.error "Need a valid emission_cash for #{r.}"
      next
    end

    # find reception cash by name or bank name or iban
    reception_cash = Cash.where('name ILIKE ?', r.).first
    reception_cash ||= Cash.where('bank_name ILIKE ?', r.).first
    reception_cash ||= Cash.find_by(iban: r.)

    unless reception_cash
      w.error "Need a valid reception_cash for #{r.}"
      next
    end

    # check if cash_transfer already exist
    unless cash_transfer = CashTransfer.find_by(emission_amount: r.amount,
                                                emission_cash_id: emission_cash.id,
                                                reception_cash_id: reception_cash.id,
                                                transfered_at: r.started_on.to_time)
      CashTransfer.create!(
                    emission_amount: r.amount,
                    emission_cash_id: emission_cash.id,
                    reception_cash_id: reception_cash.id,
                    transfered_at: r.started_on.to_time)
    end

    w.check_point
  end
end