Module: JapaneseBookkeepingSVG

Defined in:
lib/japanese-bookkeeping-svg.rb,
lib/japanese_bookkeeping_svg/gem_version.rb,
lib/japanese_bookkeeping_svg/svg_generator.rb

Defined Under Namespace

Classes: SVGGenerator

Constant Summary collapse

WIDTH =
500
CENTER_X =
WIDTH / 2
LEFT_MARGIN =
15
RIGHT_MARGIN =
10
ACCOUNT_WIDTH =
110
AMOUNT_X =
LEFT_MARGIN + ACCOUNT_WIDTH + RIGHT_MARGIN
AMOUNT_WIDTH =
105
VERSION =
'0.0.2'.freeze
@@line_height =

rubocop:disable Style/ClassVars

SVGGenerator.convert_unit('1em')

Class Method Summary collapse

Class Method Details

.account_value(svg, x, y, journals, with_brackets) ⇒ Object

rubocop:disable Metrics/MethodLength,Metrics/AbcSize



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/japanese-bookkeeping-svg.rb', line 26

def self.(svg, x, y, journals, with_brackets) # rubocop:disable Metrics/MethodLength,Metrics/AbcSize
  key_style = {
    'textLength' => ACCOUNT_WIDTH
  }
  sum_value = 0
  journals.map do |key, value|
    c_x = x
    y += line_height
    key_style['lengthAdjust'] = key.size > 8 ? 'spacingAndGlyphs' : 'spacing'
    svg.text(c_x, y, '(') if with_brackets
    c_x += LEFT_MARGIN
    svg.text(c_x, y, key.to_s, key_style)
    c_x += ACCOUNT_WIDTH + RIGHT_MARGIN
    svg.text(c_x, y, ')') if with_brackets
    c_x += AMOUNT_WIDTH
    svg.text(c_x, y, delimited_number(value).to_s, value_style)
    sum_value += value
  end

  [y, sum_value]
end

.delimited_number(number) ⇒ Object



110
111
112
# File 'lib/japanese-bookkeeping-svg.rb', line 110

def self.delimited_number(number)
  number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, '\\1,')
end

.journalization(debits, credits) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/japanese-bookkeeping-svg.rb', line 48

def self.journalization(debits, credits)
  # TODO: check amount
  SVGGenerator.new do
    JapaneseBookkeepingSVG.(self, 0, 0, debits, true)
    JapaneseBookkeepingSVG.(self, CENTER_X + RIGHT_MARGIN, 0, credits, true)
  end
end

.line_heightObject



16
17
18
# File 'lib/japanese-bookkeeping-svg.rb', line 16

def self.line_height
  @@line_height
end

.t_accounts(account, debits, credits) ⇒ Object

rubocop:disable Metrics/LineLength,Metrics/MethodLength,Metrics/AbcSize



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
103
104
105
106
107
108
# File 'lib/japanese-bookkeeping-svg.rb', line 56

def self.t_accounts(, debits, credits) # rubocop:disable Metrics/LineLength,Metrics/MethodLength,Metrics/AbcSize
  # TODO: calc amount
  line_height = SVGGenerator.convert_unit('1em')
  that = self
  SVGGenerator.new do
     = {
      'textLength' => ACCOUNT_WIDTH,
      'text-anchor' => 'middle'
    }
    line_style = {
      'stroke' => 'black',
      'stroke-width' => 1
    }

    ['lengthAdjust'] = .size > 8 ? 'spacingAndGlyphs' : 'spacing'
    text(CENTER_X, line_height, , )
    t_start_y = line_height + 8
    line(0, t_start_y, WIDTH, t_start_y, line_style)

    y = t_start_y + 5
    debits_y, sum_debits = that.(self, 0, y, debits, false)
    credits_y, sum_credits = that.(self, CENTER_X, y, credits, false)

    if debits_y < credits_y
      y = credits_y + 8
      line(LEFT_MARGIN, y, AMOUNT_X, debits_y + 8, line_style)
      line(LEFT_MARGIN, y, CENTER_X, y, line_style)
      line(CENTER_X + AMOUNT_X, y, WIDTH, y, line_style)
    elsif debits_y > credits_y
      y = debits_y + 8
      line(AMOUNT_X, y, CENTER_X, y, line_style)
      line(CENTER_X + LEFT_MARGIN, y, CENTER_X + AMOUNT_X, credits_y + 8, line_style)
      line(CENTER_X + LEFT_MARGIN, y, WIDTH, y, line_style)
    else # eq
      y = debits_y + 8
      line(AMOUNT_X, y, CENTER_X, y, line_style)
      line(CENTER_X + AMOUNT_X, y, WIDTH, y, line_style)
    end

    y += line_height + 3
    text(CENTER_X - RIGHT_MARGIN, y, that.delimited_number(sum_debits).to_s, that.value_style)
    text(WIDTH - RIGHT_MARGIN, y, that.delimited_number(sum_credits).to_s, that.value_style)

    y += 8
    line(AMOUNT_X, y, CENTER_X, y, line_style)
    line(CENTER_X + AMOUNT_X, y, WIDTH, y, line_style)
    y += 3
    line(AMOUNT_X, y, CENTER_X, y, line_style)
    line(CENTER_X + AMOUNT_X, y, WIDTH, y, line_style)

    line(CENTER_X, t_start_y, CENTER_X, y, line_style)
  end
end

.value_styleObject



20
21
22
23
24
# File 'lib/japanese-bookkeeping-svg.rb', line 20

def self.value_style
  {
    'text-anchor' => 'end'
  }
end