Class: VouchersListReportPdf

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of VouchersListReportPdf.



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

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

#emailsObject

Returns the value of attribute emails.



21
22
23
# File 'app/my_lib/vouchers_list_report_pdf.rb', line 21

def emails
  @emails
end

#package_type_codeObject

Returns the value of attribute package_type_code.



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

def package_type_code
  @package_type_code
end

#pdf_urlObject

Returns the value of attribute pdf_url.



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

def pdf_url
  @pdf_url
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



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

def restaurant_id
  @restaurant_id
end

#selling_dateObject

Returns the value of attribute selling_date.



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

def selling_date
  @selling_date
end

#selling_end_dateObject

Returns the value of attribute selling_end_date.



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

def selling_end_date
  @selling_end_date
end

#selling_filter_typeObject

Returns the value of attribute selling_filter_type.



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

def selling_filter_type
  @selling_filter_type
end

#selling_start_dateObject

Returns the value of attribute selling_start_date.



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

def selling_start_date
  @selling_start_date
end

#typeObject

Returns the value of attribute type.



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

def type
  @type
end

#valid_dateObject

Returns the value of attribute valid_date.



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

def valid_date
  @valid_date
end

#valid_date_fromObject

Returns the value of attribute valid_date_from.



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

def valid_date_from
  @valid_date_from
end

#valid_date_toObject

Returns the value of attribute valid_date_to.



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

def valid_date_to
  @valid_date_to
end

#valid_filter_typeObject

Returns the value of attribute valid_filter_type.



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

def valid_filter_type
  @valid_filter_type
end

#vouchersObject

Returns the value of attribute vouchers.



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

def vouchers
  @vouchers
end

Instance Method Details



135
136
137
138
139
140
141
142
143
# File 'app/my_lib/vouchers_list_report_pdf.rb', line 135

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



131
132
133
# File 'app/my_lib/vouchers_list_report_pdf.rb', line 131

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

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



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/my_lib/vouchers_list_report_pdf.rb', line 102

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



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
93
94
95
96
97
98
99
100
# File 'app/my_lib/vouchers_list_report_pdf.rb', line 41

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[Res-ID Res-Name VC-ID VC-Name Price
                Quantity Sold Redeemed Description])
  elsif file_number == 2
    data.push(%w[VC-ID VC-Name Package-Type Selling-Start-Date
                 Selling-End-Date Valid-Start-Date Valid-End-Date Payment Discount])
  end

  vouchers.find_each do |v|
    row_data = if file_number == 1
                [
                  restaurant.id,
                  restaurant.name,
                  v.id,
                  v.name,
                  Money.new(v.price_cents, v.price_currency).format,
                  v.quantity - v.total_orders,
                  v.total_orders,
                  Ticket.where(ticket_group_id: v.id).where.not(redeemed_at: nil).size,
                  v.description
                ]
               elsif file_number == 2
                [
                  v.id,
                  v.name,
                  v&.package_type&.title,
                  v.selling_start_date,
                  v.selling_end_date,
                  v.valid_start_date,
                  v.valid_end_date,
                  v.payment_types.pluck(:name).uniq.to_s,
                  Money.new(v.discount_price_cents, v.discount_price_currency).format
                ]
               end
    data.push(row_data)
  end
  table(data, :column_widths => TABLE_COLUMN_WIDTHS, :header => true)
end

#save_attachmentObject



121
122
123
# File 'app/my_lib/vouchers_list_report_pdf.rb', line 121

def save_attachment
  self.attachment = store_to_attachment
end

#store_to_attachmentObject



125
126
127
128
129
# File 'app/my_lib/vouchers_list_report_pdf.rb', line 125

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