class Luca::Jp::Urikake

Public Instance Methods

list() click to toggle source
# File lib/luca/jp/urikake.rb, line 31
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) click to toggle source
# File lib/luca/jp/urikake.rb, line 15
def report(total = nil)
  listed_amount = 0
  str = CSV.generate(String.new, headers: false, col_sep: ',', encoding: 'UTF-8') 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 ]
    end
  end
  File.open('HOI030_3.0.csv', 'w') { |f| f.write(str) }
end