Class: LucaSalary::Total

Inherits:
LucaRecord::Base
  • Object
show all
Includes:
Accumulator
Defined in:
lib/luca_salary/total.rb

Overview

Total

Yearly summary records are stored in ‘payments/total’ by each profile.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Accumulator

included

Constructor Details

#initialize(year) ⇒ Total

Returns a new instance of Total.



16
17
18
19
20
21
22
23
# File 'lib/luca_salary/total.rb', line 16

def initialize(year)
  @date = Date.new(year.to_i, 12, -1)
  @slips = Total.search(year, 12).map do |slip|
    slip['profile'] = parse_current(Profile.find_secure(slip['id']))
    slip
  end
  @dict = LucaRecord::Dict.load_tsv_dict(Pathname(LucaRecord::CONST.pjdir) / 'dict' / 'code.tsv')
end

Instance Attribute Details

#slipsObject (readonly)

Returns the value of attribute slips.



14
15
16
# File 'lib/luca_salary/total.rb', line 14

def slips
  @slips
end

Class Method Details

.accumulator(year) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/luca_salary/total.rb', line 25

def self.accumulator(year)
  Profile.all do |profile|
    id = profile.dig('id')
    slips = term(year, 1, year, 12, id, 'payments')
    payment, _count = accumulate(slips)
    payment['id'] = id
    date = Date.new(year, 12, 31)
    payment = local_convert(Profile.find_secure(id), payment, date)
    upsert(payment, basedir: "payments/total/#{year}#{LucaSupport::Code.encode_month(12)}")
  end
end

.local_convert(profile, payment, date) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/luca_salary/total.rb', line 37

def self.local_convert(profile, payment, date)
  return payment if LucaRecord::CONST.config['country'].nil?

  require "luca_salary/#{LucaRecord::CONST.config['country'].downcase}"
  klass = Kernel.const_get("LucaSalary::#{LucaRecord::CONST.config['country'].capitalize}")
  klass.year_total(profile, payment, date)
rescue NameError
  return payment
end

.year_adjust(year, month) ⇒ Object

Apply adjustment for yearly refund. Search Year Total records 6 months before specified payslip month.



50
51
52
53
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
82
83
# File 'lib/luca_salary/total.rb', line 50

def self.year_adjust(year, month)
  total_dirs = Array(0..6)
                 .map{ |i| LucaSupport::Code.encode_dirname(Date.new(year, month, 1).prev_month(i)) }
                 .map do |subdir|
    search_path = "#{@dirname}/#{subdir}"
    Dir.exist?(abs_path(search_path)) ? search_path : nil
  end
  total_path = total_dirs.compact.first
  if total_path.nil?
    puts "No Year total directory exists. exit"
    exit 1
  end

  search(year, month, nil, nil, 'payments').each do |slip, path|
    begin
      find(slip['profile_id'], total_path) do |total|
        ['3A1', '4A1'].each { |cd| slip[cd] = total[cd] }
      end
    rescue
      # skip no adjust profile
    end
    # Recalculate sum
    ['3', '4'].each do |key|
      slip[key] = 0
      slip[key] = LucaSalary::Base.sum_code(slip, key)
    end
    slip['5'] = slip['1'] - slip['2'] - slip['3'] + slip['4']
    update_record(
      'payments',
      path,
      YAML.dump(LucaSupport::Code.readable(slip.sort.to_h))
    )
  end
end