Class: TaxJp::SocialInsurance

Inherits:
Object
  • Object
show all
Extended by:
TaxJp::SocialInsurances::Utils
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

Methods included from TaxJp::SocialInsurances::Utils

convert_to_date, convert_to_prefecture_code

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
# File 'lib/tax_jp/social_insurance.rb', line 24

def initialize(row)
  @grade = TaxJp::SocialInsurances::Grade.new(
    :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::SocialInsurances::HealthInsurance.new(
    :grade => @grade,
    :valid_from => row[8], :valid_until => row[9],
    :prefecture => Prefecture.find_by_code(row[10]),
    :general => row[11], :care => row[12],
    :particular => row[13], :basic => row[14])

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

Instance Attribute Details

#gradeObject (readonly)

等級



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

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

#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_all_by_date_and_prefecture(date, prefecture) ⇒ Object



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

def self.find_all_by_date_and_prefecture(date, prefecture)
  date = convert_to_date(date)
  prefecture_code = convert_to_prefecture_code(prefecture)

  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 = ? or hi.prefecture_code is null)) '
    sql << 'inner join welfare_pensions wp on (wp.valid_from <= ? and wp.valid_until >= ?) '
    sql << 'where g.valid_from <= ? and g.valid_until >= ? '

    ret = []
    db.execute(sql, [date, date, prefecture_code, date, date, date, date]) do |row|
      ret << TaxJp::SocialInsurance.new(row)
    end
    ret
  end
end

.find_by_date_and_prefecture_and_salary(date, prefecture, salary) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/tax_jp/social_insurance.rb', line 85

def self.find_by_date_and_prefecture_and_salary(date, prefecture, salary)
  date = convert_to_date(date)
  prefecture_code = convert_to_prefecture_code(prefecture)
  salary = salary.to_i

  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 = ? or hi.prefecture_code is null)) '
    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.salary_from <= ? and g.salary_to > ? '

    ret = nil
    db.execute(sql, [date, date, prefecture_code, date, date, date, date, salary, salary]) do |row|
      ret = TaxJp::SocialInsurance.new(row)
    end
    ret
  end
end

.with_databaseObject



104
105
106
107
108
109
110
111
# File 'lib/tax_jp/social_insurance.rb', line 104

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

Instance Method Details

#valid_fromObject



45
46
47
48
49
50
51
52
53
54
# File 'lib/tax_jp/social_insurance.rb', line 45

def valid_from
  ret = grade.valid_from
  if health_insurance and health_insurance.valid_from > ret
    ret = health_insurance.valid_from
  end
  if welfare_pension and welfare_pension.valid_from > ret
    ret = welfare_pension.valid_from
  end
  ret
end

#valid_untilObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/tax_jp/social_insurance.rb', line 56

def valid_until
  ret = grade.valid_until
  if health_insurance and health_insurance.valid_until < ret
    ret = health_insurance.valid_until
  end
  if welfare_pension and welfare_pension.valid_until < ret
    ret = welfare_pension.valid_until
  end
  ret
end