class PrawnReport::Report

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

report parts.

Attributes

data[R]
force_today_as[RW]
group_totals[R]
header_class[RW]
header_other_pages_class[RW]
max_height[R]
max_width[R]
pdf[R]
report_params[RW]
running_totals[RW]
totals[R]
x[RW]

Public Class Methods

new(report_params) click to toggle source
# 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

Public Instance Methods

box(width, height, options = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/report_info.rb, line 10
def fits?(h)
  (y - footer_size - h - @report_params[:margin][2]) >= 0
end
format(value, formatter, options = {}) click to toggle source
# 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_size() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
params() click to toggle source
# File lib/report.rb, line 67
def params
  @report_params
end
space(width) click to toggle source
# File lib/report_helpers.rb, line 44
def space(width)
  @x += width
end
text(text, width, options = {}) click to toggle source
# 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 = {}) click to toggle source
# 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
today() click to toggle source
# File lib/report_info.rb, line 22
def today
  @force_today_as ? @force_today_as : Date.today
end
y() click to toggle source
# File lib/report_helpers.rb, line 36
def y
  @pdf.y
end
y=(y) click to toggle source
# File lib/report_helpers.rb, line 40
def y=(y)
  @pdf.y = y
end

Protected Instance Methods

draw_group_header() click to toggle source
# File lib/report.rb, line 143
def draw_group_header
  if @report_params[:group][:header_class]
    header = @report_params[:group][:header_class].new(self)
    header.draw
  end
end
draw_group_summary() click to toggle source
# File lib/report.rb, line 136
def draw_group_summary
  if @report_params[:group] && @report_params[:group][:summary_class]
    summary = @report_params[:group][:summary_class].new(self)
    summary.draw
  end
end
draw_header(klass) click to toggle source
# File lib/report.rb, line 112
def draw_header(klass)
  if klass
    header = klass.new(self)
    header.draw
    @pdf.y = @max_height - header.height
    @x = 0
  end
end
draw_header_first_page() click to toggle source
# File lib/report.rb, line 104
def draw_header_first_page
  draw_header(@header_class)
end
draw_header_other_pages() click to toggle source
# File lib/report.rb, line 108
def draw_header_other_pages
  draw_header(@header_other_pages_class || @header_class)
end
draw_summary() click to toggle source
# File lib/report.rb, line 129
def draw_summary
  if @summary_class
    summary = @summary_class.new(self)
    summary.draw
  end
end
get_raw_field_value(row, column_name) click to toggle source
# File lib/report.rb, line 182
def get_raw_field_value(row, column_name)
  c = row
  column_name.split('.').each {|n| c = c[n] if c}
  c.nil? ? '' : c
end
initialize_running_totals() click to toggle source
# File lib/report.rb, line 163
def initialize_running_totals
  @running_totals.each do |rt|
    @totals[rt] = 0
    @group_totals[rt] = 0
  end
end
reset_group_totals() click to toggle source
# File lib/report.rb, line 170
def reset_group_totals
  @running_totals.each do |rt|
    @group_totals[rt] = 0
  end
end
reset_totals() click to toggle source
# File lib/report.rb, line 176
def reset_totals
  @running_totals.each do |rt|
    @totals[rt] = 0
  end
end
run_totals(data_row) click to toggle source
# File lib/report.rb, line 154
def run_totals(data_row)
  @running_totals.each do |rt|
    vl= get_raw_field_value(data_row,rt)
    vl= (vl.is_a?(String) ? vl.to_f : vl)
    @totals[rt] = (@totals[rt] || 0) + (vl == '' ? 0 : vl)
    @group_totals[rt] = (@group_totals[rt] || 0) + (vl == '' ? 0 : vl)
  end
end
second_pass() click to toggle source
# File lib/report.rb, line 150
def second_pass

end