Class: LucaBook::State
- Inherits:
-
LucaRecord::Base
- Object
- LucaRecord::Base
- LucaBook::State
- Includes:
- Accumulator
- Defined in:
- lib/luca_book/state.rb
Constant Summary collapse
- @@dict =
LucaRecord::Dict.new('base.tsv')
Instance Attribute Summary collapse
-
#bs_data ⇒ Object
readonly
Returns the value of attribute bs_data.
-
#pl_data ⇒ Object
readonly
Returns the value of attribute pl_data.
-
#start_balance ⇒ Object
readonly
Returns the value of attribute start_balance.
-
#statement ⇒ Object
readonly
Returns the value of attribute statement.
Class Method Summary collapse
- .accumulate_term(start_year, start_month, end_year, end_month) ⇒ Object
- .by_code(code, from_year, from_month, to_year = from_year, to_month = from_month, recursive: false) ⇒ Object
- .range(from_year, from_month, to_year = from_year, to_month = from_month) ⇒ Object
- .search_code(code) ⇒ Object
- .start_balance(year, month, recursive: true) ⇒ Object
Instance Method Summary collapse
- #accumulate_balance(data) ⇒ Object
- #bs(level = 3, legal: false) ⇒ Object
- #code2label ⇒ Object
- #code_sum(report) ⇒ Object
-
#initialize(data, count = nil, start_d: nil, end_d: nil) ⇒ State
constructor
A new instance of State.
- #net_income ⇒ Object
- #pl(level = 2) ⇒ Object
- #render_xbrl(filename = nil) ⇒ Object
-
#report_mail(level = 3) ⇒ Object
TODO: pl/bs may not be immutable.
- #set_balance ⇒ Object
- #stats(level = nil) ⇒ Object
- #write_xbrl(filename = nil) ⇒ Object
-
#xbrl_line(code, amount, prior_amount = nil) ⇒ Object
TODO: proper decimals attr for each currency.
Methods included from Accumulator
#credit_amount, #credit_count, #debit_amount, #debit_count, #each_month, #gross_amount, #gross_count, included, #net_amount
Constructor Details
#initialize(data, count = nil, start_d: nil, end_d: nil) ⇒ State
Returns a new instance of State.
24 25 26 27 28 29 30 |
# File 'lib/luca_book/state.rb', line 24 def initialize(data, count = nil, start_d: nil, end_d: nil) @monthly = data @count = count @start_date = start_d @end_date = end_d set_balance end |
Instance Attribute Details
#bs_data ⇒ Object (readonly)
Returns the value of attribute bs_data.
22 23 24 |
# File 'lib/luca_book/state.rb', line 22 def bs_data @bs_data end |
#pl_data ⇒ Object (readonly)
Returns the value of attribute pl_data.
22 23 24 |
# File 'lib/luca_book/state.rb', line 22 def pl_data @pl_data end |
#start_balance ⇒ Object (readonly)
Returns the value of attribute start_balance.
22 23 24 |
# File 'lib/luca_book/state.rb', line 22 def start_balance @start_balance end |
#statement ⇒ Object (readonly)
Returns the value of attribute statement.
22 23 24 |
# File 'lib/luca_book/state.rb', line 22 def statement @statement end |
Class Method Details
.accumulate_term(start_year, start_month, end_year, end_month) ⇒ Object
189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/luca_book/state.rb', line 189 def self.accumulate_term(start_year, start_month, end_year, end_month) date = Date.new(start_year, start_month, 1) last_date = Date.new(end_year, end_month, -1) return nil if date > last_date {}.tap do |res| diff, _count = net(date.year, date.month, last_date.year, last_date.month) diff.each do |k, v| next if /^[_]/.match(k) res[k] = res[k].nil? ? v : res[k] + v end end end |
.by_code(code, from_year, from_month, to_year = from_year, to_month = from_month, recursive: false) ⇒ Object
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 |
# File 'lib/luca_book/state.rb', line 52 def self.by_code(code, from_year, from_month, to_year = from_year, to_month = from_month, recursive: false) code = search_code(code) if code date = Date.new(from_year.to_i, from_month.to_i, -1) last_date = Date.new(to_year.to_i, to_month.to_i, -1) raise 'invalid term specified' if date > last_date balance = start_balance(date.year, date.month, recursive: recursive)[code] || 0 first_line = { 'code' => nil, 'label' => nil, 'debit_amount' => nil, 'debit_count' => nil, 'credit_amount' => nil, 'credit_count' => nil, 'net' => nil, 'balance' => balance, '_d' => nil } reports = [first_line].tap do |r| while date <= last_date do diff = {}.tap do |h| g = gross(date.year, date.month, code: code, recursive: recursive) sum = g.dig(:debit, code).nil? ? BigDecimal('0') : Util.calc_diff(g[:debit][code], code) sum -= g.dig(:credit, code).nil? ? BigDecimal('0') : Util.calc_diff(g[:credit][code], code) balance += sum h['code'] = code h['label'] = @@dict.dig(code, :label) h['debit_amount'] = g.dig(:debit, code) h['debit_count'] = g.dig(:debit_count, code) h['credit_amount'] = g.dig(:credit, code) h['credit_count'] = g.dig(:credit_count, code) h['net'] = sum h['balance'] = balance h['_d'] = date.to_s end r << diff date = Date.new(date.next_month.year, date.next_month.month, -1) end end LucaSupport::Code.readable(reports) end |
.range(from_year, from_month, to_year = from_year, to_month = from_month) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/luca_book/state.rb', line 32 def self.range(from_year, from_month, to_year = from_year, to_month = from_month) date = Date.new(from_year.to_i, from_month.to_i, -1) last_date = Date.new(to_year.to_i, to_month.to_i, -1) raise 'invalid term specified' if date > last_date counts = [] reports = [].tap do |r| while date <= last_date do diff, count = accumulate_month(date.year, date.month) r << diff.tap { |c| c['_d'] = date.to_s } counts << count.tap { |c| c['_d'] = date.to_s } date = Date.new(date.next_month.year, date.next_month.month, -1) end end new(reports, counts, start_d: Date.new(from_year.to_i, from_month.to_i, 1), end_d: Date.new(to_year.to_i, to_month.to_i, -1) ) end |
.search_code(code) ⇒ Object
283 284 285 286 287 288 289 290 291 292 |
# File 'lib/luca_book/state.rb', line 283 def self.search_code(code) return code if @@dict.dig(code) @@dict.search(code).tap do |new_code| if new_code.nil? puts "Search word is not matched with labels" exit 1 end end end |
.start_balance(year, month, recursive: true) ⇒ Object
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/luca_book/state.rb', line 214 def self.start_balance(year, month, recursive: true) start_date = Date.new(year, month, 1) balance = Dict.latest_balance(start_date) base = balance.each_with_object({}) do |(k, v), h| h[k] = BigDecimal(v[:balance].to_s) if v[:balance] h[k] ||= BigDecimal('0') if k.length == 2 end pre_first = Date.parse(balance.dig("_date", :label)).next_month if start_date <= pre_first return recursive ? total_subaccount(base) : base end pre_last = start_date.prev_month pre = accumulate_term(pre_first.year, pre_first.month, pre_last.year, pre_last.month) total = {}.tap do |h| (pre.keys + base.keys).uniq.each do |k| h[k] = (base[k] || BigDecimal('0')) + (pre[k] || BigDecimal('0')) end end recursive ? total_subaccount(total) : total end |
Instance Method Details
#accumulate_balance(data) ⇒ Object
159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/luca_book/state.rb', line 159 def accumulate_balance(data) { debit: [], credit: [] }.tap do |res| data.sort.to_h.each do |k, v| case k when /^[0-4].*/ res[:debit] << { k => v } when /^[5-9].*/ res[:credit] << { k => v } end end end end |
#bs(level = 3, legal: false) ⇒ Object
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/luca_book/state.rb', line 139 def bs(level = 3, legal: false) set_bs(level, legal: legal) base = accumulate_balance(@bs_data) rows = [base[:debit].length, base[:credit].length].max @statement = [].tap do |a| rows.times do |i| {}.tap do |res| res['debit_label'] = base[:debit][i] ? @@dict.dig(base[:debit][i].keys[0], :label) : '' #res['debit_balance'] = base[:debit][i] ? (@start_balance.dig(base[:debit][i].keys[0]) || 0) + base[:debit][i].values[0] : '' res['debit_balance'] = base[:debit][i] ? base[:debit][i].values[0] : '' res['credit_label'] = base[:credit][i] ? @@dict.dig(base[:credit][i].keys[0], :label) : '' #res['credit_balance'] = base[:credit][i] ? (@start_balance.dig(base[:credit][i].keys[0]) || 0) + base[:credit][i].values[0] : '' res['credit_balance'] = base[:credit][i] ? base[:credit][i].values[0] : '' a << res end end end readable(@statement) end |
#code2label ⇒ Object
84 85 86 87 88 89 90 91 |
# File 'lib/luca_book/state.rb', line 84 def code2label @statement ||= @monthly @statement.map do |report| {}.tap do |h| report.each { |k, v| h[@@dict.dig(k, :label) || k] = v } end end end |
#code_sum(report) ⇒ Object
204 205 206 207 208 |
# File 'lib/luca_book/state.rb', line 204 def code_sum(report) legal_items.each.with_object({}) do |k, h| h[k] = self.class.sum_matched(report, /^#{k}.*/) end end |
#net_income ⇒ Object
184 185 186 187 |
# File 'lib/luca_book/state.rb', line 184 def net_income set_pl(2) @pl_data['HA'] end |
#pl(level = 2) ⇒ Object
172 173 174 175 176 177 178 179 180 181 |
# File 'lib/luca_book/state.rb', line 172 def pl(level = 2) set_pl(level) @statement = @monthly.map do |data| {}.tap do |h| @pl_data.keys.each { |k| h[k] = data[k] || BigDecimal('0') } end end @statement << @pl_data readable(code2label) end |
#render_xbrl(filename = nil) ⇒ Object
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
# File 'lib/luca_book/state.rb', line 244 def render_xbrl(filename = nil) set_bs(3, legal: true) set_pl(3) country_suffix = LucaSupport::CONST.config['country'] || 'en' @company = CGI.escapeHTML(LucaSupport::CONST.config.dig('company', 'name')) @balance_sheet_selected = 'true' @pl_selected = 'true' @capital_change_selected = 'true' @issue_date = Date.today prior_bs = @start_balance.filter { |k, _v| /^[9]/.match(k) } @xbrl_entries = @bs_data.map{ |k, v| xbrl_line(k, v, prior_bs[k]) }.compact.join("\n") @xbrl_entries += @pl_data.map{ |k, v| xbrl_line(k, v) }.compact.join("\n") @xbrl_entries += equity_change.join("\n") @filename = filename || "statement-#{@issue_date}" [render_erb(search_template("base-#{country_suffix}.xbrl.erb")), render_erb(search_template("base-#{country_suffix}.xsd.erb"))] end |
#report_mail(level = 3) ⇒ Object
TODO: pl/bs may not be immutable
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/luca_book/state.rb', line 118 def report_mail(level = 3) @company = LucaSupport::CONST.config.dig('company', 'name') {}.tap do |res| pl(level).reverse.each do |month| month.each do |k, v| res[k] ||= [] res[k] << v end end @months = res['_d'] @pl = res.select{ |k,v| k != '_d' } end @bs = bs mail = Mail.new mail.to = LucaSupport::CONST.config.dig('mail', 'preview') || LucaSupport.CONST.config.dig('mail', 'from') mail.subject = 'Financial Report available' mail.html_part = Mail::Part.new(body: render_erb(search_template('monthly-report.html.erb')), content_type: 'text/html; charset=UTF-8') LucaSupport::Mail.new(mail, LucaSupport.CONST.pjdir).deliver end |
#set_balance ⇒ Object
210 211 212 |
# File 'lib/luca_book/state.rb', line 210 def set_balance @start_balance = self.class.start_balance(@start_date.year, @start_date.month) end |
#stats(level = nil) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/luca_book/state.rb', line 93 def stats(level = nil) keys = @count.map(&:keys).flatten.push('_t').uniq.sort @count.map! do |data| sum = 0 keys.each do |k| data[k] ||= 0 sum += data[k] if /^[^_]/.match(k) next if level.nil? || k.length <= level if data[k[0, level]] data[k[0, level]] += data[k] else data[k[0, level]] = data[k] end end data.select! { |k, _v| k.length <= level } if level data['_t'] = sum data.sort.to_h end keys.map! { |k| k[0, level] }.uniq.select! { |k| k.length <= level } if level @count.prepend({}.tap { |header| keys.each { |k| header[k] = @@dict.dig(k, :label) }}) @count end |
#write_xbrl(filename = nil) ⇒ Object
236 237 238 239 240 241 242 |
# File 'lib/luca_book/state.rb', line 236 def write_xbrl(filename = nil) xbrl, xsd = render_xbrl(filename) doctype = %Q(<?xml version="1.0" encoding="UTF-8"?>) File.open("#{@filename}.xbrl", 'w') { |f| f.write [doctype, xbrl].join("\n") } File.open("#{@filename}.xsd", 'w') { |f| f.write [doctype, xsd].join("\n") } end |
#xbrl_line(code, amount, prior_amount = nil) ⇒ Object
TODO: proper decimals attr for each currency
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/luca_book/state.rb', line 264 def xbrl_line(code, amount, prior_amount = nil) return nil if /^_/.match(code) context = /^[0-9]/.match(code) ? 'CurrentYearNonConsolidatedInstant' : 'CurrentYearNonConsolidatedDuration' tag = @@dict.dig(code, :xbrl_id) #raise "xbrl_id not found: #{code}" if tag.nil? return nil if tag.nil? return nil if readable(amount).zero? && prior_amount.nil? prior = if prior_amount.nil? /^[9]/.match(code) ? "<#{tag} decimals=\"0\" unitRef=\"#{Code.currency_code(LucaSupport::CONST.config['country'])}\" contextRef=\"Prior1YearNonConsolidatedInstant\">0</#{tag}>\n" : '' else "<#{tag} decimals=\"0\" unitRef=\"#{Code.currency_code(LucaSupport::CONST.config['country'])}\" contextRef=\"Prior1YearNonConsolidatedInstant\">#{readable(prior_amount)}</#{tag}>\n" end current = "<#{tag} decimals=\"0\" unitRef=\"#{Code.currency_code(LucaSupport::CONST.config['country'])}\" contextRef=\"#{context}\">#{readable(amount)}</#{tag}>" prior + current end |