tax_jp 0.2.9 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/HISTORY.md +7 -0
- data/data//344/275/217/346/211/200/addresses.csv +123929 -0
- data/data//344/275/217/346/211/200/schema_addresses.sql +9 -0
- data/data//344/275/217/346/211/200.db +0 -0
- data/lib/build_tasks/build.rake +9 -0
- data/lib/build_tasks/download_address.sh +16 -0
- data/lib/tax_jp/address.rb +37 -0
- data/lib/tax_jp/addresses/db_builder.rb +64 -0
- data/lib/tax_jp/utils.rb +21 -0
- data/lib/tax_jp/version.rb +1 -1
- data/lib/tax_jp.rb +3 -0
- metadata +12 -5
@@ -0,0 +1,9 @@
|
|
1
|
+
create table addresses (
|
2
|
+
zip_code char(7) not null,
|
3
|
+
prefecture_code char(2) not null,
|
4
|
+
prefecture_name varchar(4) not null,
|
5
|
+
city varchar(255) not null,
|
6
|
+
section varchar(255) not null
|
7
|
+
);
|
8
|
+
|
9
|
+
create unique index index_zip_code_on_addresses on addresses(zip_code);
|
Binary file
|
data/lib/build_tasks/build.rake
CHANGED
@@ -1,11 +1,13 @@
|
|
1
1
|
require 'rake'
|
2
2
|
require 'tax_jp'
|
3
|
+
require 'tax_jp/addresses/db_builder'
|
3
4
|
require 'tax_jp/depreciation_rates/db_builder'
|
4
5
|
require 'tax_jp/social_insurances/db_builder'
|
5
6
|
require 'tax_jp/withheld_taxes/db_builder'
|
6
7
|
|
7
8
|
namespace :taxjp do
|
8
9
|
task :build do
|
10
|
+
Rake::Task["taxjp:build:address"].invoke
|
9
11
|
Rake::Task["taxjp:build:consumption_tax"].invoke
|
10
12
|
Rake::Task["taxjp:build:depreciation_rate"].invoke
|
11
13
|
Rake::Task["taxjp:build:social_insurance"].invoke
|
@@ -14,6 +16,13 @@ namespace :taxjp do
|
|
14
16
|
|
15
17
|
namespace :build do
|
16
18
|
|
19
|
+
desc '住所DBを構築します。'
|
20
|
+
task :address do
|
21
|
+
puts '住所'
|
22
|
+
fail unless system("bash -ex #{File.dirname(__FILE__)}/download_address.sh")
|
23
|
+
TaxJp::Addresses::DbBuilder.new.run
|
24
|
+
end
|
25
|
+
|
17
26
|
desc '消費税DBを構築します。'
|
18
27
|
task :consumption_tax do
|
19
28
|
puts '消費税'
|
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/bin/sh
|
2
|
+
|
3
|
+
ZIP_FILE=ken_all.zip
|
4
|
+
|
5
|
+
if [ -e tmp/${ZIP_FILE} ]; then
|
6
|
+
echo "tmp/${ZIP_FILE} が存在するので、再ダウンロードは行いません。"
|
7
|
+
else
|
8
|
+
mkdir tmp
|
9
|
+
curl -# -o tmp/${ZIP_FILE} -LO http://www.post.japanpost.jp/zipcode/dl/kogaki/zip/ken_all.zip
|
10
|
+
fi
|
11
|
+
|
12
|
+
pushd tmp
|
13
|
+
rm -f KEN_ALL.CSV
|
14
|
+
unzip ${ZIP_FILE}
|
15
|
+
iconv -f SJIS -t UTF8 KEN_ALL.CSV > ../data/住所/addresses.csv
|
16
|
+
popd
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module TaxJp
|
2
|
+
module Addresses
|
3
|
+
end
|
4
|
+
|
5
|
+
# 減価償却率
|
6
|
+
class Address
|
7
|
+
DB_PATH = File.join(TaxJp::Utils.data_dir, '住所.db')
|
8
|
+
|
9
|
+
attr_reader :zip_code
|
10
|
+
attr_reader :prefecture_code, :prefecture_name
|
11
|
+
attr_reader :city, :section
|
12
|
+
|
13
|
+
def initialize(row)
|
14
|
+
@zip_code = row[0]
|
15
|
+
@prefecture_code = row[1]
|
16
|
+
@prefecture_name = row[2]
|
17
|
+
@city = row[3]
|
18
|
+
@section = row[4]
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.find_by_zip_code(zip_code)
|
22
|
+
zip_code = TaxJp::Utils.convert_to_zip_code(zip_code)
|
23
|
+
|
24
|
+
TaxJp::Utils.with_database(DB_PATH) do |db|
|
25
|
+
sql = 'select * from addresses '
|
26
|
+
sql << 'where zip_code = ? '
|
27
|
+
|
28
|
+
ret = nil
|
29
|
+
db.execute(sql, [zip_code]) do |row|
|
30
|
+
ret = TaxJp::Address.new(row)
|
31
|
+
end
|
32
|
+
ret
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
require 'csv'
|
2
|
+
|
3
|
+
class TaxJp::Addresses::DbBuilder
|
4
|
+
|
5
|
+
def initialize(db_path = nil)
|
6
|
+
@db_path = db_path || TaxJp::Address::DB_PATH
|
7
|
+
end
|
8
|
+
|
9
|
+
def run(options = {})
|
10
|
+
with_database(options) do |db|
|
11
|
+
puts
|
12
|
+
prefecture_code = nil
|
13
|
+
CSV.foreach(File.join('data', '住所', 'addresses.csv')) do |line|
|
14
|
+
if prefecture_code != line[0][0..1]
|
15
|
+
prefecture_code = line[0][0..1]
|
16
|
+
puts prefecture_code
|
17
|
+
end
|
18
|
+
|
19
|
+
zip_code = line[2]
|
20
|
+
prefecture_name = line[6]
|
21
|
+
city = line[7]
|
22
|
+
section = line[8] == '以下に掲載がない場合' ? '' : line[8]
|
23
|
+
|
24
|
+
section = section.gsub(/(.*/, '')
|
25
|
+
if section.end_with?('地割)') or section.end_with?('地割')
|
26
|
+
next
|
27
|
+
end
|
28
|
+
|
29
|
+
row = [zip_code, prefecture_code, prefecture_name, city, section]
|
30
|
+
db.execute(insert_sql, row)
|
31
|
+
end
|
32
|
+
puts
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
def with_database(options = {})
|
39
|
+
if options.fetch(:recreate, true)
|
40
|
+
FileUtils.rm_f(@db_path)
|
41
|
+
db = SQLite3::Database.new(@db_path)
|
42
|
+
db.execute(TaxJp::Utils.load_file(File.join('住所', 'schema_addresses.sql')))
|
43
|
+
else
|
44
|
+
db = SQLite3::Database.new(@db_path)
|
45
|
+
end
|
46
|
+
|
47
|
+
begin
|
48
|
+
yield db
|
49
|
+
ensure
|
50
|
+
db.close
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def insert_sql
|
55
|
+
columns = %w{zip_code prefecture_code prefecture_name city section}
|
56
|
+
|
57
|
+
ret = 'insert into addresses ( '
|
58
|
+
ret << columns.join(',')
|
59
|
+
ret << ') values ('
|
60
|
+
ret << columns.map{|c| '?' }.join(',')
|
61
|
+
ret << ')'
|
62
|
+
ret
|
63
|
+
end
|
64
|
+
end
|
data/lib/tax_jp/utils.rb
CHANGED
@@ -27,6 +27,15 @@ module TaxJp
|
|
27
27
|
File.write(dest, ERB.new(File.read(src), 0, '-').result)
|
28
28
|
end
|
29
29
|
|
30
|
+
def with_database(db_path)
|
31
|
+
db = SQLite3::Database.new(db_path)
|
32
|
+
begin
|
33
|
+
yield db
|
34
|
+
ensure
|
35
|
+
db.close
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
30
39
|
def convert_to_date(value)
|
31
40
|
ret = nil
|
32
41
|
|
@@ -37,7 +46,19 @@ module TaxJp
|
|
37
46
|
else
|
38
47
|
raise TypeError.new(value.class)
|
39
48
|
end
|
49
|
+
|
50
|
+
ret
|
51
|
+
end
|
52
|
+
|
53
|
+
def convert_to_zip_code(value)
|
54
|
+
ret = nil
|
40
55
|
|
56
|
+
if value.is_a?(String)
|
57
|
+
ret = value.sub('-', '')
|
58
|
+
else
|
59
|
+
raise TypeError.new(value.class)
|
60
|
+
end
|
61
|
+
|
41
62
|
ret
|
42
63
|
end
|
43
64
|
|
data/lib/tax_jp/version.rb
CHANGED
data/lib/tax_jp.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tax_jp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ichylinux
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-05-11 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: sqlite3
|
@@ -87,7 +87,7 @@ dependencies:
|
|
87
87
|
- - "<"
|
88
88
|
- !ruby/object:Gem::Version
|
89
89
|
version: 5.0.0
|
90
|
-
description:
|
90
|
+
description: 税金計算ライブラリ
|
91
91
|
email:
|
92
92
|
- ichylinux@gmail.com
|
93
93
|
- hiroyuki@hybitz.co.jp
|
@@ -98,11 +98,15 @@ files:
|
|
98
98
|
- ".gitignore"
|
99
99
|
- ".travis.yml"
|
100
100
|
- Gemfile
|
101
|
+
- HISTORY.md
|
101
102
|
- LICENSE
|
102
103
|
- README.md
|
103
104
|
- Rakefile
|
104
105
|
- app/assets/javascripts/tax.js
|
105
106
|
- app/assets/javascripts/tax_jp.js
|
107
|
+
- data/住所.db
|
108
|
+
- data/住所/addresses.csv
|
109
|
+
- data/住所/schema_addresses.sql
|
106
110
|
- data/消費税.yml
|
107
111
|
- data/減価償却率.db
|
108
112
|
- data/減価償却率/schema_depreciation_rates.sql
|
@@ -138,7 +142,10 @@ files:
|
|
138
142
|
- features/step_definitions/prefectures.rb
|
139
143
|
- features/support/env.rb
|
140
144
|
- lib/build_tasks/build.rake
|
145
|
+
- lib/build_tasks/download_address.sh
|
141
146
|
- lib/tax_jp.rb
|
147
|
+
- lib/tax_jp/address.rb
|
148
|
+
- lib/tax_jp/addresses/db_builder.rb
|
142
149
|
- lib/tax_jp/const.rb
|
143
150
|
- lib/tax_jp/consumption_tax.rb
|
144
151
|
- lib/tax_jp/depreciation_rate.rb
|
@@ -178,10 +185,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
178
185
|
version: '0'
|
179
186
|
requirements: []
|
180
187
|
rubyforge_project:
|
181
|
-
rubygems_version: 2.
|
188
|
+
rubygems_version: 2.6.4
|
182
189
|
signing_key:
|
183
190
|
specification_version: 4
|
184
|
-
summary:
|
191
|
+
summary: 税金計算ライブラリ
|
185
192
|
test_files:
|
186
193
|
- features/.gitignore
|
187
194
|
- features/01.都道府県.feature
|