Class: VouchersHistoryReportPdf

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vouchers = nil, args = {}) ⇒ VouchersHistoryReportPdf

Returns a new instance of VouchersHistoryReportPdf.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 21

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

  super(top_margin: 10, page_size: "A4", page_layout: :landscape)
  self.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

#dateObject

Returns the value of attribute date.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def date
  @date
end

#date_filter_typeObject

Returns the value of attribute date_filter_type.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def date_filter_type
  @date_filter_type
end

#date_fromObject

Returns the value of attribute date_from.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def date_from
  @date_from
end

#date_toObject

Returns the value of attribute date_to.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def date_to
  @date_to
end

#emailsObject

Returns the value of attribute emails.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def emails
  @emails
end

#pdf_urlObject

Returns the value of attribute pdf_url.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def pdf_url
  @pdf_url
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def restaurant_id
  @restaurant_id
end

#typeObject

Returns the value of attribute type.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def type
  @type
end

#vouchersObject

Returns the value of attribute vouchers.



17
18
19
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 17

def vouchers
  @vouchers
end

Instance Method Details



127
128
129
130
131
132
133
134
135
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 127

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



123
124
125
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 123

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

#generate_pdf(vouchers, args = {}) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 94

def generate_pdf(vouchers, args={})
  [1, 2].each do |number|
    args[:file_number] = number
    args[:file_name] = "Hungry-Hub-report-#{Time.zone.today}-#{restaurant_id}#{Time.current_time.to_i}-#{number}.pdf"
    file_pdf = self.class.new(vouchers, 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



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

def generate_pdf_table
  I18n.locale = :en
  data = []

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

  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

  move_down 20
  if file_number == 1
    data.push(%w[Restaurant-ID Restaurant-Name Voucher-Number Voucher-Details Phone
                 Name Guest-Details])
  elsif file_number == 2
    data.push(%w[Voucher-Number Voucher-Details Redeemed-Date Redeemed-Time Package-Type Price])
  end

  vouchers.find_each do |v|
    row_data = if file_number == 1
                [
                  restaurant.id,
                  restaurant.name,
                  v.ticket_code,
                  v.ticket_name,
                  v.phone,
                  v.name,
                  v.guest_detail
                ]
               elsif file_number == 2
                [
                  v.ticket_code,
                  v.ticket_name,
                  v&.redeemed_at&.strftime('%d %b %Y'),
                  v&.redeemed_at&.strftime('%H:%M'),
                  v&.package_type&.title,
                  Money.new(v.amount_cents, v.amount.currency.to_s).format,
                ]
               end
    data.push(row_data)
  end
  table(data, :column_widths => TABLE_COLUMN_WIDTHS, :header => true)
end

#save_attachmentObject



113
114
115
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 113

def save_attachment
  self.attachment = store_to_attachment
end

#store_to_attachmentObject



117
118
119
120
121
# File 'app/my_lib/vouchers_history_report_pdf.rb', line 117

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