Class: LucaSalary::JpAdjustment
- Inherits:
-
Profile
- Object
- Profile
- LucaSalary::JpAdjustment
- Defined in:
- lib/luca_salary/jp_adjustment.rb
Constant Summary collapse
- TAGS =
{ 'NTAAPP001' => { year: 'xml001_B00020', id: 'xml001_B00170', # マイナンバー kana: 'xml001_B00150', # フリガナ name: 'xml001_B00160', # 氏名 birth_date: { root: 'xml001_B00230', # 生年月日 year: 'xml001_B00240', # 西暦 month: 'xml001_B00270', # 月 day: 'xml001_B00280' # 日 }, family: { root: 'xml001_D00000', # 扶養親族情報繰り返し id: 'xml001_D00040', # マイナンバー kana: 'xml001_D00020', # フリガナ name: 'xml001_D00030', # 氏名 birth_date: { root: 'xml001_D00080', # 生年月日 year: 'xml001_D00090', # 西暦 month: 'xml001_D00120', # 月 day: 'xml001_D00130' # 日 }, income: 'xml001_D00160', # 本年中の所得の見積額 elderly: 'xml001_D00140', # 老人扶養親族 tokutei: 'xml001_D00150', # 特定扶養親族/特定親族 nonresident: 'xml001_D00170', # 非居住者である親族/控除対象外国外扶養親族 handicapped: { root: 'xml001_D00290', # 障害者である事実 type: 'xml001_D00300' # 障害者区分 } } }, 'NTAAPP004' => { year: 'xml004_B00020', spouse: { root: 'xml004_D00000', id: 'xml004_D00030', # マイナンバー kana: 'xml004_D00010', # フリガナ name: 'xml004_D00020', # 氏名 income: 'xml004_D00260', # 配偶者の本年中の合計所得金額の見積額 elderly: 'xml004_D00160', # 老人控除対象配偶者 (1: 該当) nonresident: 'xml004_D00170' # 非居住者である配偶者 } } }
Class Method Summary collapse
- .bulk_load(path) ⇒ Object
- .create_index ⇒ Object
- .find_profile(id_fragment) ⇒ Object
- .import(path, id = nil, params = nil) ⇒ Object
-
.load_xml_export(path) ⇒ Object
年末調整アプリのエクスポートデータ読み込み(非暗号zipまたはzipを解凍したディレクトリをサポート).
- .merge_family(family_list) ⇒ Object
-
.parse_family(xml, year) ⇒ Object
NTAAPP001: 扶養控除等申告書.
-
.parse_spouse(xml, year) ⇒ Object
NTAAPP004: 配偶者控除等申告書.
- .parse_xml(xml_set) ⇒ Object
- .same_person?(p1, p2) ⇒ Boolean
- .search_id(query, tax_ids, names) ⇒ Object
- .update_profile(previous, imported) ⇒ Object
Class Method Details
.bulk_load(path) ⇒ Object
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 |
# File 'lib/luca_salary/jp_adjustment.rb', line 311 def self.bulk_load(path) return enum_for(:bulk_load, path) unless block_given? has_many = if File.directory?(path) children = Dir.children(path).map { |c| File.join(path, c) } has_subdir = children.any? { |c| File.directory?(c) } has_zip = children.any? { |c| File.file?(c) && File.extname(c).downcase == '.zip' } has_subdir || has_zip elsif File.file?(path) if File.extname(path).downcase == '.zip' false else raise "Unsupported file type: #{path}" end else raise "Path not found: #{path}" end if has_many Dir.children(path).sort.map do |child| full_path = File.join(path, child) if File.directory?(full_path) || (File.file?(full_path) && File.extname(full_path).downcase == '.zip') data = load_xml_export(full_path) yield(parse_xml(data), Pathname(full_path)) unless data.empty? else nil end end.compact else data = load_xml_export(path) yield(parse_xml(data), Pathname(path)) unless data.empty? end end |
.create_index ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/luca_salary/jp_adjustment.rb', line 38 def self.create_index tax_ids = {} names = {} profiles = {} all('profiles').each do |p| profile = find_secure(p['id'], 'profiles') if profile['tax_id'] tax_ids[profile['tax_id'].to_s] = p['id'] end key = [profile['name'].gsub(" ", "").strip, profile['birth_date'].to_s] names[key] = p['id'] s_profile = find(p['id'], 's_profiles') profiles[profile['id']] = s_profile end [tax_ids, names, profiles] end |
.find_profile(id_fragment) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/luca_salary/jp_adjustment.rb', line 55 def self.find_profile(id_fragment) list = id_completion(id_fragment, basedir: 's_profiles') if list.length > 1 STDERR.puts "#{list.length} entries found for ID: #{id_fragment}. abort..." return nil end id = list.first find(id, 's_profiles') end |
.import(path, id = nil, params = nil) ⇒ Object
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 |
# File 'lib/luca_salary/jp_adjustment.rb', line 12 def self.import(path, id = nil, params = nil) tax_ids, names, s_profiles = create_index bulk_load(path).each do |o, path| s_profile = if id find_profile(id).tap do |p| if p.nil? FileUtils.mkdir_p(path.parent / 'rejected') FileUtils.move(path, path.parent / 'rejected') raise "No entries found for ID: #{id}. abort..." end end else id = search_id(o, tax_ids, names) if id.nil? STDERR.puts "#{o['name']} record not found. skip..." FileUtils.mkdir_p(path.parent / 'rejected') FileUtils.move(path, path.parent / 'rejected') next end s_profiles[id] end s_profile = update_profile(s_profile, o) save(s_profile, 's_profiles') end end |
.load_xml_export(path) ⇒ Object
年末調整アプリのエクスポートデータ読み込み(非暗号zipまたはzipを解凍したディレクトリをサポート)
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 |
# File 'lib/luca_salary/jp_adjustment.rb', line 347 def self.load_xml_export(path) docs = [] if File.directory?(path) Dir.children(path).sort.each do |child| full_path = File.join(path, child) next if File.directory?(full_path) ext = File.extname(child).downcase if ext == '.xml' begin docs << REXML::Document.new(File.read(full_path)) rescue StandardError => e STDERR.puts "#{full_path}: #{e.}" end elsif ext == '.zip' # skip without warning else STDERR.puts full_path end end return docs end if File.file?(path) && File.extname(path).downcase == '.zip' Zip::File.open(path) do |zip| zip.each do |entry| next if entry.directory? ext = File.extname(entry.name).downcase if ext == '.xml' begin docs << REXML::Document.new(entry.get_input_stream.read) rescue StandardError => e STDERR.puts "#{entry.name}: #{e.}" end elsif ext == '.zip' # skip without warning else STDERR.puts entry.name end end end end docs end |
.merge_family(family_list) ⇒ Object
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 |
# File 'lib/luca_salary/jp_adjustment.rb', line 287 def self.merge_family(family_list) merged = {} family_list.flatten.each do |f| key = if f['tax_id'] f['tax_id'] else [f['name'].gsub(" ", "").strip, f['birth_date'].to_s] end if merged.key?(key) target = merged[key] ['income', 'elderly', 'tokutei', 'nonresident', 'handicapped'].each do |field| if f[field] target[field] ||= {} target[field].merge!(f[field]) end end else merged[key] = f end end merged.values end |
.parse_family(xml, year) ⇒ Object
NTAAPP001: 扶養控除等申告書
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/luca_salary/jp_adjustment.rb', line 234 def self.parse_family(xml, year) = TAGS['NTAAPP001'] h = [] xml.elements.each("//#{[:family][:root]}") do |dep| f = {} f['tax_id'] = dep.elements[[:family][:id]]&.text f['name'] = dep.elements[[:family][:name]]&.text f['kana'] = dep.elements[[:family][:kana]]&.text = [:family][:birth_date] bd_node = dep.elements[[:root]] if bd_node y = bd_node.elements[[:year]]&.text m = bd_node.elements[[:month]]&.text d = bd_node.elements[[:day]]&.text if y && m && d f['birth_date'] = Date.new(y.to_i, m.to_i, d.to_i) end end if year dep_income = dep.elements[[:family][:income]]&.text if dep_income && !dep_income.empty? f['income'] = { year => dep_income.to_i } end elderly = dep.elements[[:family][:elderly]]&.text if elderly && (elderly == '1' || elderly == '2') f['elderly'] = { year => elderly.to_i } end tokutei = dep.elements[[:family][:tokutei]]&.text if tokutei && (tokutei == '1' || tokutei == '2') f['tokutei'] = { year => tokutei.to_i } end nonresident = dep.elements[[:family][:nonresident]]&.text f['nonresident'] = { year => nonresident.to_i } if nonresident == '1' = [:family][:handicapped] hc_node = dep.elements[[:root]] if hc_node hc_type = hc_node.elements[[:type]]&.text if hc_type && !hc_type.empty? && hc_type != '0' f['handicapped'] = { year => hc_type.to_i } end end end h << f end h end |
.parse_spouse(xml, year) ⇒ Object
NTAAPP004: 配偶者控除等申告書
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/luca_salary/jp_adjustment.rb', line 209 def self.parse_spouse(xml, year) = TAGS['NTAAPP004'] spouse = xml.elements["//#{[:spouse][:root]}"] return {} if ! spouse { 'income' => {} }.tap do |h| h['tax_id'] = spouse.elements[[:spouse][:id]]&.text h['name'] = spouse.elements[[:spouse][:name]]&.text h['kana'] = spouse.elements[[:spouse][:kana]]&.text nonresident = spouse.elements[[:spouse][:nonresident]]&.text if nonresident == '1' h['nonresident'] = {} unless h['nonresident'] h['nonresident'][year] = nonresident.to_i end income = spouse.elements[[:spouse][:income]]&.text h['income'][year] = income.to_i if income && !income.empty? elderly = spouse.elements[[:spouse][:elderly]]&.text h['elderly'] = '1' if elderly == '1' end end |
.parse_xml(xml_set) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/luca_salary/jp_adjustment.rb', line 147 def self.parse_xml(xml_set) h = { 'spouse' => {}, 'family' => [] } xml_set.each do |xml| # ルート要素の属性から様式IDを取得 form_id = xml.root.name = TAGS[form_id] next unless year_node = xml.elements["//#{[:year]}"] year = year_node&.text&.to_i if form_id == 'NTAAPP001' = TAGS[form_id] h['tax_id'] ||= xml.elements["//#{[:id]}"]&.text h['name'] ||= xml.elements["//#{[:name]}"]&.text h['kana'] ||= xml.elements["//#{[:kana]}"]&.text = [:birth_date] bd_node = xml.elements["//#{[:root]}"] if bd_node y = bd_node.elements[[:year]]&.text m = bd_node.elements[[:month]]&.text d = bd_node.elements[[:day]]&.text if y && m && d h['birth_date'] = Date.new(y.to_i, m.to_i, d.to_i) end end h['family'] << parse_family(xml, year) end if form_id == 'NTAAPP004' && year h['spouse'] = parse_spouse(xml, year) end end h['family'] = merge_family(h['family']) # 翌年の扶養控除等申告書との重複 h end |
.same_person?(p1, p2) ⇒ Boolean
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/luca_salary/jp_adjustment.rb', line 193 def self.same_person?(p1, p2) return false if p1.nil? || p2.nil? || p1.empty? || p2.empty? if p1['tax_id'] && p2['tax_id'] return p1['tax_id'].to_s == p2['tax_id'].to_s end n1 = p1['name'].to_s.gsub(' ', '').strip n2 = p2['name'].to_s.gsub(' ', '').strip bd1 = p1['birth_date'].to_s bd2 = p2['birth_date'].to_s n1 == n2 && bd1 == bd2 end |
.search_id(query, tax_ids, names) ⇒ Object
185 186 187 188 189 190 191 |
# File 'lib/luca_salary/jp_adjustment.rb', line 185 def self.search_id(query, tax_ids, names) id = tax_ids[query['tax_id']] if query['tax_id'] return id if id key = [query['name'].gsub(" ", "").strip, query['birth_date'].to_s] names[key] end |
.update_profile(previous, imported) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/luca_salary/jp_adjustment.rb', line 66 def self.update_profile(previous, imported) previous['tax_id'] ||= imported['tax_id'] previous['name'] ||= imported['name'] previous['katakana'] ||= imported['kana'] previous['birth_date'] ||= imported['birth_date'] if imported['spouse'] && !imported['spouse'].empty? if same_person?(previous['spouse'], imported['spouse']) # NOTE imported['spouse']をベースにし、incomeだけマージ income = (previous['spouse']['income'] || {}).merge(imported['spouse']['income']) previous['spouse'] = imported['spouse'] previous['spouse']['income'] = income else previous['spouse'] = imported['spouse'] end end if !imported['family'].empty? previous['family'] = imported['family'].map do |latest| registered = previous['family'].find { |m| same_person?(m, latest) } if registered ['income', 'elderly', 'tokutei', 'nonresident', 'handicapped'].each do |k| latest[k] = registered[k].merge(new_member[k]) if registered[k] && new_member[k] latest[k] ||= registered[k] end end latest end end previous end |