Class: Luca::Jp::Urikake

Inherits:
LucaDeal::Invoice
  • Object
show all
Defined in:
lib/luca/jp/urikake.rb

Instance Method Summary collapse

Instance Method Details

#listObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/luca/jp/urikake.rb', line 34

def list
  invoices = self.class.asof(@date.year, @date.month)
               .map { |dat, _path| dat }
               .sort_by { |invoice| invoice.dig('subtotal', 0, 'items') }
               .reverse

  reports = invoices.filter { |invoice| 500_000 <= invoice['subtotal'].inject(0) { |sum, i| sum + i['items'] + i['tax'] } }
  return reports if reports.length >= 5

  additional = invoices
                 .filter { |invoice| 500_000 > invoice['subtotal'].inject(0) { |sum, i| sum + i['items'] + i['tax'] } }
                 .take(5 - reports.length)
  reports.concat(additional)
end

#report(total = nil, encoding = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/luca/jp/urikake.rb', line 15

def report(total = nil, encoding = nil)
  listed_amount = 0
  encoding ||= 'SJIS'
  str = CSV.generate(String.new, headers: false, col_sep: ',', encoding: encoding) do |f|
    list.map do |invoice|
      amount = readable(invoice.dig('subtotal', 0, 'items') + invoice.dig('subtotal', 0, 'tax'))
      listed_amount += amount
      f << ['3', '0', '売掛金', invoice.dig('customer', 'name'), invoice.dig('customer', 'address'), amount, nil ]
    end
    if total
      f << ['3', '0', '売掛金', 'その他', nil, total - listed_amount, nil ]
      f << ['3', '1', nil, nil, nil, total, nil ]
    else
      f << ['3', '1', nil, nil, nil, listed_amount, nil ]
    end
  end
  File.open('HOI030_3.0.csv', 'w') { |f| f.write(str) }
end