Class: TaxJp::SocialInsurances::DbBuilder

Inherits:
DbBuilder
  • Object
show all
Defined in:
lib/tax_jp/social_insurances/db_builder.rb

Instance Attribute Summary

Attributes inherited from DbBuilder

#db_path

Instance Method Summary collapse

Constructor Details

#initialize(db_path = nil) ⇒ DbBuilder

Returns a new instance of DbBuilder.



6
7
8
# File 'lib/tax_jp/social_insurances/db_builder.rb', line 6

def initialize(db_path = nil)
  super(db_path || TaxJp::SocialInsurance::DB_PATH)
end

Instance Method Details

#run(options = {}) ⇒ Object



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
# File 'lib/tax_jp/social_insurances/db_builder.rb', line 10

def run(options = {})
  with_database(options) do |db|
    Dir.glob(File.join(TaxJp::Utils.data_dir, '社会保険料', '等級-*.tsv')).each do |filename|
      valid_from, valid_until = filename_to_date(filename)

      CSV.foreach(filename, :col_sep => "\t") do |row|
        next if row[0].to_i == 0 and row[1].to_i == 0
        db.execute(insert_sql_grade, [valid_from, valid_until] + row.map{|col| TaxJp::Utils.normalize_amount(col)})
      end
    end

    Dir.glob(File.join(TaxJp::Utils.data_dir, '社会保険料', '旧健康保険.tsv')).each do |filename|
      CSV.foreach(filename, :col_sep => "\t") do |row|
        next unless row[2].to_f > 0
        values = []
        values << row[0]
        values << row[1]
        values << nil
        values << (row[2].to_f * 0.01).round(4)
        values << (row[3].to_f * 0.01).round(4)
        values << 0
        values << 0
        db.execute(insert_sql_health_insurance, values)
      end
    end

    Dir.glob(File.join(TaxJp::Utils.data_dir, '社会保険料', '健康保険-*.tsv')).each do |filename|
      valid_from, valid_until = filename_to_date(filename)

      CSV.foreach(filename, :col_sep => "\t") do |row|
        next unless row[1].to_f > 0
        values = []
        values << valid_from
        values << valid_until
        values << TaxJp::Prefecture.find_by_name(row[0]).code
        values << (row[1].to_f * 0.01).round(4)
        values << (row[2].to_f * 0.01).round(4)
        values << (row[3].to_f * 0.01).round(4)
        values << (row[3].to_f * 0.01).round(4)
        db.execute(insert_sql_health_insurance, values)
      end
    end

    Dir.glob(File.join(TaxJp::Utils.data_dir, '社会保険料', '厚生年金.tsv')).each do |filename|
      CSV.foreach(filename, :col_sep => "\t") do |row|
        next unless row[2].to_f > 0
        values = []
        values << row.shift
        values << row.shift
        values += row.map{|col| (col.to_f * 0.01).round(5) }
        db.execute(insert_sql_welfare_pensions, values)
      end
    end

  end
end