Class: Luca::Jp::Sonshitsu

Inherits:
Object
  • Object
show all
Defined in:
lib/luca/jp/sonshitsu.rb

Overview

青色申告に基く繰越損失の計算

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(records, date) ⇒ Sonshitsu

Returns a new instance of Sonshitsu.



21
22
23
24
25
26
27
# File 'lib/luca/jp/sonshitsu.rb', line 21

def initialize(records, date)
  @records = records
  @report_date = date
  net_amount
  @deduction = 0
  @profit = 0
end

Instance Attribute Details

#deductionObject (readonly)

当期控除額



15
16
17
# File 'lib/luca/jp/sonshitsu.rb', line 15

def deduction
  @deduction
end

#profitObject (readonly)

控除後所得



17
18
19
# File 'lib/luca/jp/sonshitsu.rb', line 17

def profit
  @profit
end

#recordsObject (readonly)

各期繰越損失



19
20
21
# File 'lib/luca/jp/sonshitsu.rb', line 19

def records
  @records
end

Class Method Details

.load(this_year) ⇒ Object

sonshitsu format is as follows:

- start_date: 2020-01-01
  end_date: 2020-12-31
  increase: 1000000
- start_date: 2021-01-01
  end_date: 2021-12-31
  decrease: 800000


38
39
40
41
42
43
44
45
46
47
# File 'lib/luca/jp/sonshitsu.rb', line 38

def self.load(this_year)
  records = if File.exist?(record_file)
              YAML.safe_load(File.read(record_file), permitted_classes: [Date], aliases: true)
                .filter { |record| record['start_date'] > this_year.prev_year(11) && record['end_date'] < this_year }
                .sort { |a, b| a['start_date'] <=> b['start_date'] }
           else
             []
           end
  new(records, this_year)
end

.record_fileObject



97
98
99
# File 'lib/luca/jp/sonshitsu.rb', line 97

def self.record_file
  Pathname(LucaSupport::CONST.pjdir) / 'data' / 'balance' / 'sonshitsu.yml'
end

Instance Method Details

#net_amountObject



83
84
85
86
87
88
# File 'lib/luca/jp/sonshitsu.rb', line 83

def net_amount
  @records.each do |record|
    record['amount'] = record['increase'] - past_decreased(record['decrease'])
    record['decrease'] = record['decrease']&.reject { |decrease_record| decrease_record['date'] > @report_date.prev_year }
  end
end

#past_decreased(decrease_records) ⇒ Object



90
91
92
93
94
95
# File 'lib/luca/jp/sonshitsu.rb', line 90

def past_decreased(decrease_records)
  return 0 if decrease_records.nil?

  decrease_records.filter { |record| record['date'] <= @report_date.prev_year }
    .map { |record| record['val'] }.sum || 0
end

#saveObject



49
50
51
52
# File 'lib/luca/jp/sonshitsu.rb', line 49

def save
  File.open(self.class.record_file, 'w') { |f| f.puts YAML.dump(@records)}
  self
end

#update(profit_or_loss) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/luca/jp/sonshitsu.rb', line 54

def update(profit_or_loss)
  return self if profit_or_loss == 0

  if profit_or_loss < 0
    start_date, end_date = LucaBook::Util.current_fy(@report_date)
    @records << {
      'start_date' => start_date,
      'end_date' => end_date,
      'increase' => profit_or_loss.abs
    }
    @profit = profit_or_loss
    return self
  end

  @records.each do |record|
    next if profit_or_loss <= 0
    next if record['amount'] <= 0

    decrease_amount = [record['amount'], profit_or_loss].min
    record['decrease'] ||= []
    record['decrease'] << { 'date' => @report_date, 'val' => decrease_amount }
    record['amount'] -= decrease_amount
    profit_or_loss -= decrease_amount
    @deduction += decrease_amount
  end
  @profit = profit_or_loss
  self
end