Class: PerformanceReviewReportPdf

Inherits:
Prawn::Document
  • Object
show all
Includes:
MoneyRails::ActionViewExtension
Defined in:
app/my_lib/performance_review_report_pdf.rb

Constant Summary collapse

TABLE_COLUMN_WIDTHS =
[
  80,
  80,
  80,
  80,
  80,
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ PerformanceReviewReportPdf

Returns a new instance of PerformanceReviewReportPdf.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/my_lib/performance_review_report_pdf.rb', line 20

def initialize(args = {})
  if args.present?
    args.to_h.with_indifferent_access.each do |k, v|
      send("#{k}=".to_sym, v)
    end
  end

  @dates = if date_filter_type == 'single'
             @start_date = @end_date = date
             [date]
           else
             (start_date.to_date..end_date.to_date).to_a
           end

  super(top_margin: 10, page_size: 'A4', page_layout: :landscape)
  font_families.update('Mitr' => {
                         normal: Rails.root.join('vendor/assets/fonts/mitr/Mitr-Regular.ttf'),
                         bold: Rails.root.join('vendor/assets/fonts/mitr/Mitr-Bold.ttf'),
                       })

  font 'Mitr'
  @pdf_url = {}
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def data
  @data
end

#dateObject

Returns the value of attribute date.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def date
  @date
end

#date_filter_typeObject

Returns the value of attribute date_filter_type.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def date_filter_type
  @date_filter_type
end

#datesObject

Returns the value of attribute dates.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def dates
  @dates
end

#emailsObject

Returns the value of attribute emails.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def emails
  @emails
end

#end_dateObject

Returns the value of attribute end_date.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def end_date
  @end_date
end

#pdf_urlObject

Returns the value of attribute pdf_url.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def pdf_url
  @pdf_url
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def restaurant_id
  @restaurant_id
end

#resultObject

Returns the value of attribute result.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def result
  @result
end

#start_dateObject

Returns the value of attribute start_date.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def start_date
  @start_date
end

#typeObject

Returns the value of attribute type.



16
17
18
# File 'app/my_lib/performance_review_report_pdf.rb', line 16

def type
  @type
end

Instance Method Details



105
106
107
108
109
110
111
112
113
# File 'app/my_lib/performance_review_report_pdf.rb', line 105

def download_link
  link = attachment.pdf_url
  link = "#{Figaro.env.HH_HOST_URL!}#{link}" unless link.include? 'http'

  return link unless Figaro.bool_env! :ENABLE_CDN

  link.gsub(Figaro.env.CDN_URL, Figaro.env.HH_HOST_URL)
  link
end

#filepathObject



101
102
103
# File 'app/my_lib/performance_review_report_pdf.rb', line 101

def filepath
  Rails.root.join('tmp', 'reports', file_name).to_s
end

#generate_pdf(args = {}) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'app/my_lib/performance_review_report_pdf.rb', line 72

def generate_pdf(args = {})
  [1, 2, 3, 4, 5, 6].each do |number|
    args[:file_number] = number
    args[:file_name] = name(number)
    file_pdf = self.class.new(args)
    file_pdf.generate_pdf_table
    file_pdf.render_file(file_pdf.filepath)
    path = Rails.root.join(file_pdf.filepath)
    loop do
      break if path.exist?

      sleep 1
    end

    file_pdf.save_attachment
    @pdf_url["file_#{number}".to_sym] = file_pdf.download_link
  end
end

#generate_pdf_tableObject



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
# File 'app/my_lib/performance_review_report_pdf.rb', line 44

def generate_pdf_table
  I18n.locale = :en
  @result = []

  image_url = AdminSetting.report_pdf_image_url
  if image_url.present?
    banner = [[{ image: open(image_url), image_height: 110, image_width: 530,
                 position: :center }]]
  end

  label = []
  label += [['Restaurant Name', restaurant.name]]

  bounding_box([0, 510], width: 230) do
    table(label, cell_style: { border_width: 0 })
    # transparent(0.5) { stroke_bounds }
  end
  if banner.present?
    bounding_box([370, 530], width: 250) do
      table(banner, cell_style: { border_width: 0 })
      # transparent(0.5) { stroke_bounds }
    end
  end

  gen_content_file(file_number)
  table(result, column_widths: TABLE_COLUMN_WIDTHS, header: true)
end

#save_attachmentObject



91
92
93
# File 'app/my_lib/performance_review_report_pdf.rb', line 91

def save_attachment
  self.attachment = store_to_attachment
end

#store_to_attachmentObject



95
96
97
98
99
# File 'app/my_lib/performance_review_report_pdf.rb', line 95

def store_to_attachment
  new_attachment = Attachment.new pdf: open(filepath)
  new_attachment.save!
  new_attachment
end