Class: LucaTerm::Book
Instance Attribute Summary collapse
-
#modeline ⇒ Object
Returns the value of attribute modeline.
-
#window ⇒ Object
Returns the value of attribute window.
Class Method Summary collapse
Instance Method Summary collapse
- #alert_dialog(msg) ⇒ Object
-
#edit_amount(current = nil, diff = nil) ⇒ Object
returns amount after edit.
- #edit_dialog(message = '', submessage = '', title: '') ⇒ Object
-
#edit_note(current = nil) ⇒ Object
returns note after edit.
-
#initialize(window, year, month, data = nil) ⇒ Book
constructor
A new instance of Book.
-
#main_loop ⇒ Object
render monthly journal list.
-
#select_code ⇒ Object
returns Account code after selection from list dialog.
-
#show_detail(record) ⇒ Object
render each journal.
Constructor Details
#initialize(window, year, month, data = nil) ⇒ Book
13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/luca_term/book.rb', line 13 def initialize(window, year, month, data=nil) @modeline = Window.new(1, 0, 0, 0) @window = window @year = year @month = month @data = data @index = 0 @active = 0 # active line in window @visible = set_visible(@data) @dict = LucaRecord::Dict.load('base.tsv') main_loop end |
Instance Attribute Details
#modeline ⇒ Object
Returns the value of attribute modeline.
11 12 13 |
# File 'lib/luca_term/book.rb', line 11 def modeline @modeline end |
#window ⇒ Object
Returns the value of attribute window.
11 12 13 |
# File 'lib/luca_term/book.rb', line 11 def window @window end |
Class Method Details
.journals(window, *args) ⇒ Object
26 27 28 |
# File 'lib/luca_term/book.rb', line 26 def self.journals(window, *args) new(window, args[0], args[1], LucaSupport::Code.readable(LucaBook::List.term(*args).data)) end |
Instance Method Details
#alert_dialog(msg) ⇒ Object
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
# File 'lib/luca_term/book.rb', line 360 def alert_dialog(msg) top = window.maxy >= 25 ? 5 : 2 sub = window.subwin(6, 35, (window.maxy-6)/2, (window.maxx - 35)/2) sub.box(?|, ?-) sub.setpos(2, 2) sub << msg sub.clrtoeol sub.refresh loop do cmd = window.getch case cmd when ' ', 'q', KEY_CTRL_J sub.close return end end end |
#edit_amount(current = nil, diff = nil) ⇒ Object
returns amount after edit
234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/luca_term/book.rb', line 234 def edit_amount(current = nil, diff = nil) diff_msg = diff.nil? ? '' : "#{diff} meets balance." begin scmd = edit_dialog "Current: #{current&.to_s}", diff_msg, title: 'Edit Amount' return nil if scmd.length == 0 # TODO: guard from not number return scmd.to_i rescue return nil end end |
#edit_dialog(message = '', submessage = '', title: '') ⇒ Object
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 |
# File 'lib/luca_term/book.rb', line 259 def edit_dialog( = '', = '', title: '') sub = window.subwin(5, 30, (window.maxy-5)/2, (window.maxx - 30)/2) sub.setpos(1, 1) sub << " #{message}" sub.clrtoeol sub.setpos(2, 1) sub.clrtoeol sub.setpos(2, 3) sub.attron(A_REVERSE) { sub << " > #{' ' * (30 - 9)}" } sub.setpos(3, 1) sub << " #{submessage}" sub.clrtoeol sub.box(?|, ?-) sub.setpos(0, 2) sub << "[ #{title} ]" sub.setpos(2, 7) sub.refresh loop do echo scmd = sub.getstr noecho sub.close return scmd end end |
#edit_note(current = nil) ⇒ Object
returns note after edit
248 249 250 251 252 253 254 255 256 257 |
# File 'lib/luca_term/book.rb', line 248 def edit_note(current = nil) msg = '' begin scmd = edit_dialog "Current: #{current&.to_s}", msg, title: 'Edit Note' return nil if scmd.length == 0 return scmd rescue return nil end end |
#main_loop ⇒ Object
render monthly journal list
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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/luca_term/book.rb', line 32 def main_loop loop do modeline.setpos(0, 0) modeline << "#{Date::ABBR_MONTHNAMES[@month.to_i]} #{@year}" modeline.clrtoeol modeline.refresh window.setpos(0,0) @visible.each.with_index(0) do |dat, i| cursor = i == @active ? :full : nil draw_line(dat, cursor, true) window << "\n" end (window.maxy - window.cury).times { window << "\n" } window.refresh window.keypad(true) cmd = window.getch case cmd when KEY_DOWN, 'j', KEY_CTRL_N next if @index >= @data.length - 1 cursor_down @data when KEY_UP, 'k', KEY_CTRL_P next if @index <= 0 cursor_up @data when 'G' cursor_last @data when 'm' ym = edit_dialog('Enter: [yyyy] m', title: 'Change Month')&.split(/[\/\s]/) ym = [@year, ym[0]] if ym.length == 1 @data = LucaSupport::Code.readable(LucaBook::List.term(*ym).data) @year, @month = ym @index = 0 @active = 0 @visible = set_visible(@data) when '<', 'h' target = Date.parse("#{@year}-#{@month}-1").prev_month @data = LucaSupport::Code.readable(LucaBook::List.term(target.year, target.month).data) @year, @month = target.year, target.month @index = 0 @active = 0 @visible = set_visible(@data) when '>', 'l' target = Date.parse("#{@year}-#{@month}-1").next_month @data = LucaSupport::Code.readable(LucaBook::List.term(target.year, target.month).data) @year, @month = target.year, target.month @index = 0 @active = 0 @visible = set_visible(@data) when KEY_ENTER, KEY_CTRL_J show_detail(@data[@index]) @visible = set_visible(@data) when 'N' newdate = edit_dialog "Enter date of new record: YYYY-m-d", title: 'Create Journal' tmpl = { date: newdate, debit: [ { code: '10XX', amount: 0 } ], credit: [ { code: '50XX', amount: 0 } ], } show_detail(tmpl) when 'q' exit 0 end end end |
#select_code ⇒ Object
returns Account code after selection from list dialog
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 |
# File 'lib/luca_term/book.rb', line 287 def select_code top = window.maxy >= 25 ? 5 : 2 sub = window.subwin(window.maxy - top, window.maxx - 4, top, 2) padding = ' ' * account_index(sub.maxx)[0].length list = @dict.reject{ |code, _e| code.length < 3 || /^[15]0XX/.match(code) || /^[89]ZZ/.match(code) } .map{ |code, entry| { code: code, label: entry[:label], category: padding } } tabstop = ['1', '5', '9', 'A', 'C'].map { |cap| list.index { |ac| /^#{cap}/.match(ac[:code]) } }.compact account_index(sub.maxx).each.with_index do |cat, i| list[tabstop[i]][:category] = cat end visible_dup = @visible index_dup = @index active_dup = @active @index = 0 @active = 0 @visible = nil @visible = set_visible(list, sub.maxy - 2) loop do @visible.each.with_index(0) do |entry, i| sub.setpos(i+1, 1) head = entry[:code].length == 3 ? '' : ' ' line = format("%s %s %s %s", head, entry[:category], entry[:code], entry[:label]) if i == @active sub.attron(A_REVERSE) { sub << line } else sub << line end sub.clrtoeol #sub << "\n" end (window.maxy - window.cury).times { window << "\n" } sub.box(?|, ?-) sub.setpos(0, 2) sub << "[ Select Account ]" sub.refresh cmd = window.getch case cmd when KEY_DOWN, 'j', KEY_CTRL_N next if @index >= list.length - 1 cursor_down list, sub.maxy - 2 when KEY_NPAGE cursor_pagedown list, sub.maxy - 2 when KEY_UP, 'k', KEY_CTRL_P next if @index <= 0 cursor_up list when KEY_PPAGE cursor_pageup list, sub.maxy - 2 when KEY_LEFT, 'h' cursor_jump tabstop, list, rev: true when KEY_RIGHT, 'l' cursor_jump tabstop, list when 'G' cursor_last list, sub.maxy - 2 when KEY_CTRL_J selected = list[@index][:code] @visible = visible_dup @index = index_dup @active = active_dup sub.close return selected when 'q', 27 @visible = visible_dup @index = index_dup @active = active_dup sub.close return nil end end end |
#show_detail(record) ⇒ Object
render each journal
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 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/luca_term/book.rb', line 106 def show_detail(record) @d_v = 0 @d_h = 0 debit_length = Array(record[:debit]).length credit_length = Array(record[:credit]).length date, txid = LucaSupport::Code.decode_id(record[:id]) if record[:id] fileid = record[:id].split('/').last if record[:id] date ||= record[:date] modeline.setpos(0, 0) modeline << "#{date} #{fileid} " modeline.clrtoeol modeline.refresh loop do window.setpos(0, 0) if record.dig(:headers, 'x-customer') window << format(" [%s] ", record.dig(:headers, 'x-customer')) end window << record[:note] clrtoeol; window << "\n" [debit_length, credit_length].max.times do |i| { id: nil, debit: [], credit: [] }.tap do |dat| if i < debit_length dat[:debit] << Array(record[:debit])[i] end if i < credit_length dat[:credit] << Array(record[:credit])[i] end cursor = @d_v == i ? @d_h : nil draw_line(dat, cursor) end clrtoeol window << "\n" end (window.maxy - window.cury).times { window << "\n" } window.refresh window.keypad(true) cmd = window.getch case cmd when KEY_DOWN, 'j', KEY_CTRL_N case @d_h when 0, 1 @d_v = @d_v >= debit_length - 1 ? 0 : @d_v + 1 else #2,3 @d_v = @d_v >= credit_length - 1 ? 0 : @d_v + 1 end when KEY_UP, 'k', KEY_CTRL_P case @d_h when 0, 1 @d_v = @d_v == 0 ? debit_length - 1 : @d_v - 1 else #2,3 @d_v = @d_v == 0 ? credit_length - 1 : @d_v - 1 end when KEY_LEFT, 'h', KEY_CTRL_B case @d_h when 1, 3 @d_h -= 1 when 2 @d_v = debit_length - 1 if @d_v > debit_length - 1 @d_h -= 1 else # 0 @d_v = credit_length - 1 if @d_v > credit_length - 1 @d_h = 3 end when KEY_RIGHT, 'l', KEY_CTRL_F case @d_h when 0, 2 @d_h += 1 when 1 @d_v = credit_length - 1 if @d_v > credit_length - 1 @d_h += 1 else # 3 @d_v = debit_length - 1 if @d_v > debit_length - 1 @d_h = 0 end when 'n' position = [0, 1].include?(@d_h) ? :debit : :credit new_code = select_code next if new_code.nil? new_amount = edit_amount next if new_amount.nil? record[position] << { code: new_code, amount: new_amount } debit_length = Array(record[:debit]).length credit_length = Array(record[:credit]).length when 'c' new_note = edit_note(record[:note]) next if new_note.nil? record[:note] = new_note when KEY_CTRL_J position, counter = [0,1].include?(@d_h) ? [:debit, :credit] : [:credit, :debit] if [0, 2].include? @d_h new_code = select_code next if new_code.nil? record[position][@d_v][:code] = new_code else diff = record[counter].map { |c| c[:amount] }.sum - record[position].map { |p| p[:amount] }.sum + record[position][@d_v][:amount] new_amount = edit_amount(record[position][@d_v][:amount], diff) next if new_amount.nil? record[position][@d_v][:amount] = new_amount end when 's', KEY_CTRL_S begin if record[:id] LucaBook::Journal.save record else LucaBook::Journal.create record end rescue RuntimeError => e if e. == 'BalanceUnmatch' alert_dialog("Error\n Debit/Credit balance not meet.") next end end break when 'q', 27 break end end end |