Class: LucaBook::Import

Inherits:
Object
  • Object
show all
Defined in:
lib/luca_book/import.rb,
lib/luca_book/import_jp.rb

Constant Summary collapse

DEBIT_DEFAULT =
'10XX'
CREDIT_DEFAULT =
'50XX'

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, dict) ⇒ Import

Returns a new instance of Import.



20
21
22
23
24
25
26
27
28
29
# File 'lib/luca_book/import.rb', line 20

def initialize(path, dict)
  raise 'no such file' unless FileTest.file?(path)

  @target_file = path
  # TODO: yaml need to be configurable
  @dict_name = dict
  @dict = LucaBook::Dict.new("import-#{dict}.yaml")
  @code_map = LucaRecord::Dict.reverse(LucaRecord::Dict.load('base.tsv'))
  @config = @dict.csv_config if dict
end

Class Method Details

.import_json(io) ⇒ Object

JSON Format:

[
  {
    "date": "2020-05-04",
    "debit" : [
      {
        "label": "savings accounts",
        "amount": 20000
      }
    ],
    "credit" : [
      {
        "label": "trade notes receivable",
        "amount": 20000
      }
    ],
    "note": "settlement for the last month trade"
  }
]


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/luca_book/import.rb', line 51

def self.import_json(io)
  JSON.parse(io).each do |d|
    code_map = LucaRecord::Dict.reverse(LucaRecord::Dict.load('base.tsv'))
    d['debit'].each { |h| h['code'] = code_map.dig(h['label']) || DEBIT_DEFAULT }
    d['credit'].each { |h| h['code'] = code_map.dig(h['label']) || CREDIT_DEFAULT }

    LucaBook::Journal::ACCEPTED_HEADERS.each do |header|
      next if d[header].nil?
      d['headers'] ||= {}
      d['headers'][header] = d[header]
      d.delete header
    end
    LucaBook::Journal.create(d)
  end
end

Instance Method Details

#import_csvObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/luca_book/import.rb', line 67

def import_csv
  @dict.load_csv(@target_file) do |row|
    if @config[:type] == 'single'
      LucaBook::Journal.create(parse_single(row))
    elsif @config[:type] == 'double'
      p parse_double(row) # TODO: Not implemented yet
    else
      p row
    end
  end
end

#tax_extension(code1, code2, amount, options) ⇒ Object

TODO: need to be separated into pluggable l10n module. TODO: gensen rate >1m yen. TODO: gensen & consumption ‘round()` rules need to be confirmed. Profit or Loss account should be specified as code1.



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
# File 'lib/luca_book/import_jp.rb', line 14

def tax_extension(code1, code2, amount, options)
  return nil if options.nil? || options[:tax_options].nil?
  return nil if !options[:tax_options].include?('jp-gensen') && !options[:tax_options].include?('jp-consumption')

  gensen_rate = BigDecimal('0.1021')
  consumption_rate = BigDecimal('0.1')
  gensen_code = @code_map.dig(options[:gensen_label]) || @code_map.dig('預り金')
  gensen_idx = /^[5-8B-G]/.match(code1) ? 1 : 0
  consumption_idx = /^[A-G]/.match(code1) ? 0 : 1
  consumption_code = @code_map.dig(options[:consumption_label])
  consumption_code ||= /^[A]/.match(code1) ? @code_map.dig('仮受消費税等') : @code_map.dig('仮払消費税等')
  if options[:tax_options].include?('jp-gensen') && options[:tax_options].include?('jp-consumption')
    paid_rate = BigDecimal('1') + consumption_rate - gensen_rate
    gensen_amount = (amount / paid_rate * gensen_rate).round
    consumption_amount = (amount / paid_rate * consumption_rate).round
    [].tap do |res|
      res << [].tap do |res1|
        amount1 = amount
        amount1 -= consumption_amount if consumption_idx == 0
        amount1 += gensen_amount if gensen_idx == 1
        res1 << { 'code' => code1, 'amount' => amount1 }
        res1 << { 'code' => consumption_code, 'amount' => consumption_amount } if consumption_idx == 0
        res1 << { 'code' => gensen_code, 'amount' => gensen_amount } if gensen_idx == 0
      end
      res << [].tap do |res2|
        amount2 = amount
        amount2 -= consumption_amount if consumption_idx == 1
        amount2 += gensen_amount if gensen_idx == 0
        res2 << { 'code' => code2, 'amount' => amount2 }
        res2 << { 'code' => consumption_code, 'amount' => consumption_amount } if consumption_idx == 1
        res2 << { 'code' => gensen_code, 'amount' => gensen_amount } if gensen_idx == 1
      end
    end
  elsif options[:tax_options].include?('jp-gensen')
    paid_rate = BigDecimal('1') - gensen_rate
    gensen_amount = (amount / paid_rate * gensen_rate).round
    [].tap do |res|
      res << [].tap do |res1|
        amount1 = amount
        amount1 += gensen_amount if gensen_idx == 1
        res1 << { 'code' => code, 'amount' => amount1 }
        res1 << { 'code' => gensen_code, 'amount' => gensen_amount } if gensen_idx == 0
      end
      res << [].tap do |res2|
        amount2 = amount
        amount2 += gensen_amount if gensen_idx == 0
        mount2 ||= amount
        res2 << { 'code' => code2, 'amount' => amount2 }
        res2 << { 'code' => gensen_code, 'amount' => gensen_amount } if gensen_idx == 1
      end
    end
  elsif options[:tax_options].include?('jp-consumption')
    paid_rate = BigDecimal('1') + consumption_rate - gensen_rate
    consumption_amount = (amount / paid_rate * consumption_rate).round
    res << [].tap do |res1|
      amount1 = amount
      amount1 -= consumption_amount if consumption_idx == 0
      res1 << { 'code' => code1, 'amount' => amount1 }
      res1 << { 'code' => consumption_code, 'amount' => consumption_amount } if consumption_idx == 0
    end
    res << [].tap do |res2|
      amount2 = amount
      amount2 -= consumption_amount if consumption_idx == 1
      res2 << { 'code' => code2, 'amount' => amount2 }
      res2 << { 'code' => consumption_code, 'amount' => consumption_amount } if consumption_idx == 1
    end
  end
end