Class: PrawnReport::Report

Inherits:
Object
  • Object
show all
Defined in:
lib/report.rb,
lib/report_info.rb,
lib/report_helpers.rb

Overview

Report is the base class for all reports, it encapsulates all logic for rendering

report parts.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(report_params) ⇒ Report

Returns a new instance of Report.



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
# File 'lib/report.rb', line 34

def initialize(report_params)
  @report_params = DEFAULT_REPORT_PARAMS.merge(report_params || {})
  @running_totals = @report_params.delete(:running_totals) || []
  @num_pages = 1

  @pdf = Prawn::Document.new(@report_params)

  @pdf.font(DEFAULT_FONT)
  @pdf.line_width = LINE_WIDTH

  if @report_params[:page_size].is_a?(String)
    if @report_params[:page_layout] == :portrait
      w, h = *Prawn::Document::PageGeometry::SIZES[@report_params[:page_size]]
    else
      h, w = *Prawn::Document::PageGeometry::SIZES[@report_params[:page_size]]
    end
  else
    w, h = @report_params[:page_size]
  end
  @x = 0
  @y = @max_height = h - (@report_params[:margin][0] + @report_params[:margin][2])
  @max_width = w - (@report_params[:margin][1] + @report_params[:margin][3])

  @footer_size = 0
  @pdf.move_cursor_to(max_height - @report_params[:margin][2])

  @header_class = @header_other_pages_class = @summary_band_class =  @footer_class = nil
  @totals = {}
  @group_totals = {}

  initialize_running_totals
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



30
31
32
# File 'lib/report.rb', line 30

def data
  @data
end

#force_today_asObject

Returns the value of attribute force_today_as.



8
9
10
# File 'lib/report_info.rb', line 8

def force_today_as
  @force_today_as
end

#group_totalsObject (readonly)

Returns the value of attribute group_totals.



30
31
32
# File 'lib/report.rb', line 30

def group_totals
  @group_totals
end

#header_classObject

Returns the value of attribute header_class.



31
32
33
# File 'lib/report.rb', line 31

def header_class
  @header_class
end

#header_other_pages_classObject

Returns the value of attribute header_other_pages_class.



31
32
33
# File 'lib/report.rb', line 31

def header_other_pages_class
  @header_other_pages_class
end

#max_heightObject (readonly)

Returns the value of attribute max_height.



30
31
32
# File 'lib/report.rb', line 30

def max_height
  @max_height
end

#max_widthObject (readonly)

Returns the value of attribute max_width.



30
31
32
# File 'lib/report.rb', line 30

def max_width
  @max_width
end

#pdfObject (readonly)

Returns the value of attribute pdf.



30
31
32
# File 'lib/report.rb', line 30

def pdf
  @pdf
end

#report_paramsObject

Returns the value of attribute report_params.



31
32
33
# File 'lib/report.rb', line 31

def report_params
  @report_params
end

#running_totalsObject

Returns the value of attribute running_totals.



31
32
33
# File 'lib/report.rb', line 31

def running_totals
  @running_totals
end

#totalsObject (readonly)

Returns the value of attribute totals.



30
31
32
# File 'lib/report.rb', line 30

def totals
  @totals
end

#xObject

Returns the value of attribute x.



31
32
33
# File 'lib/report.rb', line 31

def x
  @x
end

Instance Method Details

#box(width, height, options = {}) ⇒ Object



6
7
8
# File 'lib/report_helpers.rb', line 6

def box(width, height, options = {})
  @pdf.rounded_rectangle([0, y], width, height, TEXT_BOX_RADIUS)
end

#draw(data) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/report.rb', line 71

def draw(data)
  @data = data

  draw_header_first_page
  draw_internal
  draw_summary
  draw_footer

  second_pass

  @pdf.close_and_stroke
  @pdf.render
end

#draw_graph(g, params) ⇒ Object



80
81
82
83
# File 'lib/report_helpers.rb', line 80

def draw_graph(g, params)
  data = StringIO.new(g.to_blob)
  @pdf.image(data, params)
end

#fill_color(color) ⇒ Object



96
97
98
99
100
# File 'lib/report.rb', line 96

def fill_color(color)
  @pdf.fill_color color
    @pdf.fill_rectangle [x,y], max_width, 15
    @pdf.fill_color '000000'
end

#fits?(h) ⇒ Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/report_info.rb', line 10

def fits?(h)
  (y - footer_size - h - @report_params[:margin][2]) >= 0
end


18
19
20
# File 'lib/report_info.rb', line 18

def footer_size
  @footer_class ? @footer_class.height : 0
end

#format(value, formatter, options = {}) ⇒ Object



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
# File 'lib/report_helpers.rb', line 53

def format(value, formatter, options = {})
  if !value.nil? && value != ''
    if (formatter == :currency)
      if value < 0
        '-'+((value.to_i*-1).to_s.reverse.gsub(/...(?=.)/,'\&.').reverse) + ',' + ('%02d' % ((value.abs * 100).round % 100))
      else
        (value.to_i.to_s.reverse.gsub(/...(?=.)/,'\&.').reverse) + ',' + ('%02d' % ((value * 100).round % 100))
      end
    elsif (formatter == :date)
      begin
        value.to_time.strftime('%d/%m/%Y') || value.to_time.strftime('%Y/%m/%d')
      rescue
        value.to_s
      end
    elsif (formatter == :timezone_date)
      tz = Time.zone.parse(value)
      tz.nil? ? '' : tz.strftime('%d/%m/%Y')
    elsif (formatter == :function)
      send(options[:formatter_function].to_s, value)
    else
      value.to_s
    end
  else
    ''
  end
end

#header_sizeObject



14
15
16
# File 'lib/report_info.rb', line 14

def header_size
  @header ? @header_class.height : 0
end

#horizontal_line(x_ini = 0, x_end = @max_width) ⇒ Object



30
31
32
33
34
# File 'lib/report_helpers.rb', line 30

def horizontal_line(x_ini = 0, x_end = @max_width)
  @pdf.stroke do
    @pdf.horizontal_line(x_ini, x_end, :at => y)
  end
end

#line_break(size = TEXT_SIZE) ⇒ Object



48
49
50
51
# File 'lib/report_helpers.rb', line 48

def line_break(size = TEXT_SIZE)
  @x = 0
  @pdf.move_down(@pdf.height_of('A', :size => size) + 2)
end

#new_page(print_titles = true) ⇒ Object



85
86
87
88
89
90
91
92
93
94
# File 'lib/report.rb', line 85

def new_page(print_titles = true)
  draw_footer

  @num_pages += 1
  @pdf.start_new_page
  @x = 0
  @pdf.move_down(@report_params[:margin][0])

  draw_header_other_pages
end

#paramsObject



67
68
69
# File 'lib/report.rb', line 67

def params
  @report_params
end

#space(width) ⇒ Object



44
45
46
# File 'lib/report_helpers.rb', line 44

def space(width)
  @x += width
end

#text(text, width, options = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/report_helpers.rb', line 20

def text(text, width, options = {})
  font_size = options[:font_size] || TEXT_SIZE
  @pdf.text_box(text.to_s, :size => font_size, :style => options[:style], :at => [@x, y - 4],
        :width => width, :height => font_size,
        :valign => (options[:valign] || :top),
        :align => (options[:align] || :left)
        )
  @x = @x + width
end

#text_box_with_box(label, text, width, height = nil, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/report_helpers.rb', line 10

def text_box_with_box(label, text, width, height = nil, options = {})
  @pdf.rounded_rectangle([@x, y], width, height || TEXT_BOX_HEIGTH, TEXT_BOX_RADIUS)
  @pdf.text_box(label, :size => LABEL_SIZE, :at => [@x + 2, y - 2], :width => width - 2,
      :height => LABEL_SIZE, :valign => :top)
  h_text = height.nil? ? TEXT_SIZE : height - LABEL_SIZE - 4
  @pdf.text_box(text.to_s || '', { :size => TEXT_SIZE, :at => [@x + 2, y - LABEL_SIZE - 4],
      :width => width - 2, :height => h_text, :valign => :top }.merge(options))
  @x += width
end

#todayObject



22
23
24
# File 'lib/report_info.rb', line 22

def today
  @force_today_as ? @force_today_as : Date.today
end

#yObject



36
37
38
# File 'lib/report_helpers.rb', line 36

def y
  @pdf.y
end

#y=(y) ⇒ Object



40
41
42
# File 'lib/report_helpers.rb', line 40

def y=(y)
  @pdf.y = y
end