Module: LucaBook::Accumulator::ClassMethods
- Defined in:
- lib/luca_book/accumulator.rb
Instance Method Summary collapse
- #accumulate_month(year, month) ⇒ Object
-
#gross(start_year, start_month, end_year = nil, end_month = nil, code: nil, date_range: nil, rows: 4, recursive: false, header: nil) ⇒ Object
for assert purpose.
-
#load_data(io, path = nil) ⇒ Object
Override LucaRecord::IO.load_data.
-
#net(start_year, start_month, end_year = nil, end_month = nil, code: nil, date_range: nil, recursive: false, header: nil) ⇒ Object
netting vouchers in specified term.
- #sum_matched(report, reg) ⇒ Object
-
#total_subaccount(report) ⇒ Object
Accumulate Level 2, 3 account.
Instance Method Details
#accumulate_month(year, month) ⇒ Object
14 15 16 17 |
# File 'lib/luca_book/accumulator.rb', line 14 def accumulate_month(year, month) monthly_record, count = net(year, month) [total_subaccount(monthly_record), count] end |
#gross(start_year, start_month, end_year = nil, end_month = nil, code: nil, date_range: nil, rows: 4, recursive: false, header: nil) ⇒ Object
for assert purpose
header: { customer: ‘some-customer’ }
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/luca_book/accumulator.rb', line 80 def gross(start_year, start_month, end_year = nil, end_month = nil, code: nil, date_range: nil, rows: 4, recursive: false, header: nil) if ! date_range.nil? raise if date_range.class != Range start_year = date_range.first.year start_month = date_range.first.month end_year = date_range.last.year end_month = date_range.last.month date_bound = { start: { month: "#{start_year}#{LucaSupport::Code.encode_month(start_month)}", day: LucaSupport::Code.encode_date(date_range.first) }, end: { month: "#{end_year}#{LucaSupport::Code.encode_month(end_month)}", day: LucaSupport::Code.encode_date(date_range.last) } } end scan_headers = header&.map { |k, v| [:customer, :editor].include?(k) ? [ "x-#{k.to_s}", v ] : nil }&.compact&.to_h end_year ||= start_year end_month ||= start_month sum = { debit: {}, credit: {}, debit_count: {}, credit_count: {} } idx_memo = [] enm = if scan_headers.nil? || scan_headers.empty? term(start_year, start_month, end_year, end_month, code, 'journals') else Enumerator.new do |y| term(start_year, start_month, end_year, end_month, code, 'journals') do |f, path| 4.times { f.gets } # skip to headers CSV.new(f, headers: false, col_sep: "\t", encoding: 'UTF-8') .each do |line| break if line.empty? if scan_headers.keys.include? line[0] f.rewind y << [f, path] end end end end end enm.each do |f, path| if date_bound if path[0] == date_bound[:start][:month] next if path[1][0] < date_bound[:start][:day] elsif path[0] == date_bound[:end][:month] next if path[1][0] > date_bound[:end][:day] end end CSV.new(f, headers: false, col_sep: "\t", encoding: 'UTF-8') .each_with_index do |row, i| break if i >= rows case i when 0 idx_memo = row.map(&:to_s) next if code && idx_memo.select { |idx| /^#{code}/.match(idx) }.empty? idx_memo.each do |r| sum[:debit][r] ||= BigDecimal('0') sum[:debit_count][r] ||= 0 end when 1 next if code && idx_memo.select { |idx| /^#{code}/.match(idx) }.empty? row.each_with_index do |r, j| sum[:debit][idx_memo[j]] += BigDecimal(r.to_s) sum[:debit_count][idx_memo[j]] += 1 end when 2 idx_memo = row.map(&:to_s) break if code && idx_memo.select { |idx| /^#{code}/.match(idx) }.empty? idx_memo.each do |r| sum[:credit][r] ||= BigDecimal('0') sum[:credit_count][r] ||= 0 end when 3 row.each_with_index do |r, j| sum[:credit][idx_memo[j]] += BigDecimal(r.to_s) sum[:credit_count][idx_memo[j]] += 1 end else puts row # for debug end end end return sum if code.nil? codes = if recursive sum[:debit].keys.concat(sum[:credit].keys).uniq.select { |k| /^#{code}/.match(k) } else Array(code) end res = { debit: { code => 0 }, credit: { code => 0 }, debit_count: { code => 0 }, credit_count: { code => 0 } } codes.each do |cd| res[:debit][code] += sum[:debit][cd] || BigDecimal('0') res[:credit][code] += sum[:credit][cd] || BigDecimal('0') res[:debit_count][code] += sum[:debit_count][cd] || 0 res[:credit_count][code] += sum[:credit_count][cd] || 0 end res end |
#load_data(io, path = nil) ⇒ Object
Override LucaRecord::IO.load_data
208 209 210 |
# File 'lib/luca_book/accumulator.rb', line 208 def load_data(io, path = nil) [io, path] end |
#net(start_year, start_month, end_year = nil, end_month = nil, code: nil, date_range: nil, recursive: false, header: nil) ⇒ Object
netting vouchers in specified term
192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/luca_book/accumulator.rb', line 192 def net(start_year, start_month, end_year = nil, end_month = nil, code: nil, date_range: nil, recursive: false, header: nil) g = gross(start_year, start_month, end_year, end_month, code: code, date_range: date_range, recursive: recursive, header: header) idx = (g[:debit].keys + g[:credit].keys).uniq.sort count = {} diff = {}.tap do |sum| idx.each do |code| sum[code] = g.dig(:debit, code).nil? ? BigDecimal('0') : LucaBook::Util.calc_diff(g[:debit][code], code) sum[code] -= g.dig(:credit, code).nil? ? BigDecimal('0') : LucaBook::Util.calc_diff(g[:credit][code], code) count[code] = (g.dig(:debit_count, code) || 0) + (g.dig(:credit_count, code) || 0) end end [diff, count] end |
#sum_matched(report, reg) ⇒ Object
72 73 74 |
# File 'lib/luca_book/accumulator.rb', line 72 def sum_matched(report, reg) report.select { |k, v| reg.match(k)}.values.sum end |
#total_subaccount(report) ⇒ Object
Accumulate Level 2, 3 account.
21 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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/luca_book/accumulator.rb', line 21 def total_subaccount(report) {}.tap do |res| res['A0'] = sum_matched(report, /^[A][0-9A-Z]{2,}/) res['B0'] = sum_matched(report, /^[B][0-9A-Z]{2,}/) res['BA'] = res['A0'] - res['B0'] res['C0'] = sum_matched(report, /^[C][0-9A-Z]{2,}/) res['CA'] = res['BA'] - res['C0'] res['D0'] = sum_matched(report, /^[D][0-9A-Z]{2,}/) res['E0'] = sum_matched(report, /^[E][0-9A-Z]{2,}/) res['EA'] = res['CA'] + res['D0'] - res['E0'] res['F0'] = sum_matched(report, /^[F][0-9A-Z]{2,}/) res['G0'] = sum_matched(report, /^[G][0-9][0-9A-Z]{1,}/) res['GA'] = res['EA'] + res['F0'] - res['G0'] res['H0'] = sum_matched(report, /^[H][0-9][0-9A-Z]{1,}/) res['HA'] = res['GA'] - res['H0'] report['9142'] = (report['9142'] || BigDecimal('0')) + res['HA'] res['9142'] = report['9142'] res['10'] = sum_matched(report, /^[12][0-9A-Z]{2,}/) jp_4v = sum_matched(report, /^[4][V]{2,}/) # deferred assets for JP GAAP res['30'] = sum_matched(report, /^[34][0-9A-Z]{2,}/) - jp_4v res['4V'] = jp_4v if LucaSupport::CONST.config['country'] == 'jp' res['50'] = sum_matched(report, /^[56][0-9A-Z]{2,}/) res['70'] = sum_matched(report, /^[78][0-9A-Z]{2,}/) res['91'] = sum_matched(report, /^91[0-9A-Z]{1,}/) res['8ZZ'] = res['50'] + res['70'] res['9ZZ'] = sum_matched(report, /^[9][0-9A-Z]{2,}/) res['1'] = res['10'] + res['30'] res['5'] = res['8ZZ'] + res['9ZZ'] res['_d'] = report['_d'] report.each do |k, v| res[k] ||= sum_matched(report, /^#{k}[0-9A-Z]{1,}/) if k.length == 2 res[k] = v if k.length == 3 end report.each do |k, v| if k.length >= 4 if res[k[0, 3]] res[k[0, 3]] += v else res[k[0, 3]] = v end res[k] = v end end res.sort.to_h end end |