Class: LucaSalary::Payment

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

Instance Method Summary collapse

Methods included from Accumulator

included

Constructor Details

#initialize(date = nil) ⇒ Payment

Returns a new instance of Payment.



16
17
18
19
20
# File 'lib/luca_salary/payment.rb', line 16

def initialize(date = nil)
  @date = Date.parse(date)
  @pjdir = Pathname(LucaRecord::CONST.pjdir)
  @dict = LucaRecord::Dict.load_tsv_dict(@pjdir / 'dict' / 'code.tsv')
end

Instance Method Details

#export_jsonObject

Export json for LucaBook. Accrual_date can be change with ‘payment_term` on config.yml. Default value is 0. `payment_term: 1` means payment on the next month of calculation target.



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
# File 'lib/luca_salary/payment.rb', line 53

def export_json
  accrual_date = if LucaRecord::CONST.config['payment_term']
                   pt = LucaRecord::CONST.config['payment_term']
                   Date.new(@date.prev_month(pt).year, @date.prev_month(pt).month, -1)
                 else
                   Date.new(@date.year, @date.month, -1)
                 end
  h = { debit: {}, credit: {} }
  accumulate.each do |k, v|
    next if @dict.dig(k, :acct_label).nil?

    pos = acct_balance(k)
    acct_label = @dict[k][:acct_label]
    h[pos][acct_label] = h[pos].key?(acct_label) ? h[pos][acct_label] + v : v
  end
  [].tap do |res|
    {}.tap do |item|
      item['date'] = "#{accrual_date.year}-#{accrual_date.month}-#{accrual_date.day}"
      item['debit'] = h[:debit].reject { |_k, v| v.nil? }.map { |k, v| { 'label' => k, 'amount' => v } }
      item['credit'] = h[:credit].reject { |_k, v| v.nil? }.map { |k, v| { 'label' => k, 'amount' => v } }
      item['x-editor'] = 'LucaSalary'
      res << item
    end
    puts JSON.dump(readable(res))
  end
end

#payslipObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/luca_salary/payment.rb', line 22

def payslip
  {}.tap do |report|
    report['asof'] = "#{@date.year}/#{@date.month}"
    report['payments'] = []
    report['records'] = []

    self.class.asof(@date.year, @date.month) do |payment|
      profile = LucaSalary::Profile.find(payment['profile_id'])
      summary = {
        'name' => profile['name'],
        "#{@dict.dig('5', :label) || '5'}" => payment['5']
      }

      slip = {}.tap do |line|
        line['name'] = profile['name']
        payment.each do |k, v|
          next if k == 'id'

          line["#{@dict.dig(k, :label) || k}"] = v
        end
      end
      report['payments'] << summary
      report['records'] << slip
    end
  end
end