Class: TaxJp::SocialInsurance

Inherits:
Object
  • Object
show all
Defined in:
lib/tax_jp/social_insurance.rb

Overview

社会保険

Constant Summary collapse

DB_PATH =
File.join(TaxJp::Utils.data_dir, '社会保険料.db')

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(row) ⇒ SocialInsurance

Returns a new instance of SocialInsurance.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/tax_jp/social_insurance.rb', line 24

def initialize(row)
  @valid_from = row[0]
  @valid_until = row[1]
  @grade = row[2]
  @pension_grade = row[3]
  @monthly_standard = row[4]
  @daily_standard = row[5]
  @salary_from = row[6]
  @salary_to = row[7]

  @health_insurance = TaxJp::HealthInsurance.new(
    :valid_from => row[8], :valid_until => row[9],
    :monthly_standard => row[4],
    :prefecture => Prefecture.find_by_code(row[10]),
    :general => row[11], :particular => row[12],
    :basic => row[13])

  @welfare_pension = TaxJp::WelfarePension.new(
    :valid_from => row[14], :valid_until => row[15],
    :monthly_standard => row[4],
    :general => row[16], :particular => row[17],
    :child_support => row[18])
end

Instance Attribute Details

#daily_standardObject (readonly)

Returns the value of attribute daily_standard.



16
17
18
# File 'lib/tax_jp/social_insurance.rb', line 16

def daily_standard
  @daily_standard
end

#gradeObject (readonly)

Returns the value of attribute grade.



15
16
17
# File 'lib/tax_jp/social_insurance.rb', line 15

def grade
  @grade
end

#health_insuranceObject (readonly)

健康保険



20
21
22
# File 'lib/tax_jp/social_insurance.rb', line 20

def health_insurance
  @health_insurance
end

#monthly_standardObject (readonly)

Returns the value of attribute monthly_standard.



16
17
18
# File 'lib/tax_jp/social_insurance.rb', line 16

def monthly_standard
  @monthly_standard
end

#pension_gradeObject (readonly)

Returns the value of attribute pension_grade.



15
16
17
# File 'lib/tax_jp/social_insurance.rb', line 15

def pension_grade
  @pension_grade
end

#salary_fromObject (readonly)

Returns the value of attribute salary_from.



17
18
19
# File 'lib/tax_jp/social_insurance.rb', line 17

def salary_from
  @salary_from
end

#salary_toObject (readonly)

Returns the value of attribute salary_to.



17
18
19
# File 'lib/tax_jp/social_insurance.rb', line 17

def salary_to
  @salary_to
end

#valid_fromObject (readonly)

等級



14
15
16
# File 'lib/tax_jp/social_insurance.rb', line 14

def valid_from
  @valid_from
end

#valid_untilObject (readonly)

等級



14
15
16
# File 'lib/tax_jp/social_insurance.rb', line 14

def valid_until
  @valid_until
end

#welfare_pensionObject (readonly)

厚生年金



22
23
24
# File 'lib/tax_jp/social_insurance.rb', line 22

def welfare_pension
  @welfare_pension
end

Class Method Details

.find_by_date_and_prefecture_and_grade(date, prefecture, grade) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/tax_jp/social_insurance.rb', line 81

def self.find_by_date_and_prefecture_and_grade(date, prefecture, grade)
  if date.is_a?(Date)
    date = date.strftime('%Y-%m-%d')
  elsif date.is_a?(String)
  else
    raise TypeError.new(date.class)
  end

  if prefecture.is_a?(TaxJp::Prefecture)
    prefecture_code = prefecture.code
  elsif prefecture.to_s =~ /[0-9]{2}/
    prefecture_code = prefecture
  else
    p = Prefecture.find_by_name(prefecture.to_s)
    if p
      prefecture_code = p.code
    else
      raise TypeError.new(prefecture.class)
    end
  end

  if grade.is_a?(TaxJp::SocialInsurance)
    grade = grade.grade
  elsif grade.is_a?(Fixnum)
    grade = grade.to_i
  else
    raise TypeError.new("#{grade.class} は等級として不正です。")
  end

  with_database do |db|
    sql =  'select g.*, hi.*, wp.* from grades g '
    sql << 'inner join health_insurances hi on (hi.valid_from <= ? and hi.valid_until >= ? and hi.prefecture_code = ?) '
    sql << 'inner join welfare_pensions wp on (wp.valid_from <= ? and wp.valid_until >= ?) '
    sql << 'where g.valid_from <= ? and g.valid_until >= ? and g.grade = ? '

    ret = nil
    db.execute(sql, [date, date, prefecture_code, date, date, date, date, grade]) do |row|
      if ret
        raise "健康保険が重複して登録されています。date=#{date}, prefecture_code=#{prefecture_code}, grade=#{grade}"
      else
        ret = TaxJp::SocialInsurance.new(row)
      end
    end
    ret
  end
end

.find_grade_by_date_and_salary(date, salary) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/tax_jp/social_insurance.rb', line 48

def self.find_grade_by_date_and_salary(date, salary)
  date = date.strftime('%Y-%m-%d') if date.is_a?(Date)

  with_database do |db|
    sql = 'select * from grades where valid_from <= ? and valid_until >= ? and salary_from <= ? and salary_to > ?'

    ret = nil
    db.execute(sql, [date, date, salary, salary]) do |row|
      if ret
        raise "等級が重複して登録されています。date=#{date}, salary=#{salary}"
      else
        ret = TaxJp::SocialInsurance.new(row)
      end
    end
    ret
  end
end

.find_grades_by_date(date) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/tax_jp/social_insurance.rb', line 66

def self.find_grades_by_date(date)
  date = date.strftime('%Y-%m-%d') if date.is_a?(Date)

  with_database do |db|
    sql = 'select * from grades where valid_from <= ? and valid_until >= ?'
    
    ret = []
    db.execute(sql, [date, date]) do |row|
      ret << TaxJp::SocialInsurance.new(row)
    end
    
    ret.sort{|a, b| a.grade <=> b.grade }
  end
end

.with_databaseObject



128
129
130
131
132
133
134
135
# File 'lib/tax_jp/social_insurance.rb', line 128

def self.with_database
  db = SQLite3::Database.new(DB_PATH)
  begin
    yield db
  ensure
    db.close
  end
end