Class: PackagesListReportPdf

Inherits:
Prawn::Document
  • Object
show all
Includes:
MoneyRails::ActionViewExtension
Defined in:
app/my_lib/packages_list_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(packages = nil, args = {}) ⇒ PackagesListReportPdf

Returns a new instance of PackagesListReportPdf.



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

def initialize(packages=nil, args={})
  @packages = packages
  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.



15
16
17
# File 'app/my_lib/packages_list_report_pdf.rb', line 15

def emails
  @emails
end

#package_typeObject

Returns the value of attribute package_type.



15
16
17
# File 'app/my_lib/packages_list_report_pdf.rb', line 15

def package_type
  @package_type
end

#packagesObject

Returns the value of attribute packages.



15
16
17
# File 'app/my_lib/packages_list_report_pdf.rb', line 15

def packages
  @packages
end

#pdf_urlObject

Returns the value of attribute pdf_url.



15
16
17
# File 'app/my_lib/packages_list_report_pdf.rb', line 15

def pdf_url
  @pdf_url
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



15
16
17
# File 'app/my_lib/packages_list_report_pdf.rb', line 15

def restaurant_id
  @restaurant_id
end

#typeObject

Returns the value of attribute type.



15
16
17
# File 'app/my_lib/packages_list_report_pdf.rb', line 15

def type
  @type
end

Instance Method Details



146
147
148
149
150
151
152
153
154
# File 'app/my_lib/packages_list_report_pdf.rb', line 146

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



142
143
144
# File 'app/my_lib/packages_list_report_pdf.rb', line 142

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

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



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/my_lib/packages_list_report_pdf.rb', line 113

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



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
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
101
102
103
104
105
106
107
108
109
110
111
# File 'app/my_lib/packages_list_report_pdf.rb', line 37

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
                 Restaurant-Package-ID
                 Package-Name-English
                 Package-Name-Thai
                 Package-Type
                 Start-Date
                 End-Date
                 Active
                ])
  elsif file_number == 2
    data.push(%w[Res-ID Res-Name
                 Restaurant-Package-ID
                 Lowest-Price
                 Highest-Price
                 Original-Price
                 Rank-in-restaurant-scope
                 Auto-Extend
                 Booking-Duration(mins)
                 ])
  end

  packages.find_each do |v|
    row_data = if file_number == 1
                [
                  restaurant.id,
                  restaurant.name,
                  v.id,
                  v.package.name_en,
                  v.package.name_th,
                  v.package_type,
                  v.start_date,
                  v.end_date,
                  v.active? ? 'true' : 'false'
                ]
               elsif file_number == 2
                [
                  restaurant.id,
                  restaurant.name,
                  v.id,
                  v.package&.lowest_pricing&.price&.amount,
                  v.package&.highest_pricing&.price&.amount,
                  v.package&.package_attr&.original_price&.amount,
                  v.rank_in_restaurant_scope,
                  v.auto_extend? ? 'true' : 'false',
                  v.package&.reservation_duration
                ]
               end
    data.push(row_data)
  end
  table(data, :column_widths => TABLE_COLUMN_WIDTHS, :header => true)
end

#save_attachmentObject



132
133
134
# File 'app/my_lib/packages_list_report_pdf.rb', line 132

def save_attachment
  self.attachment = store_to_attachment
end

#store_to_attachmentObject



136
137
138
139
140
# File 'app/my_lib/packages_list_report_pdf.rb', line 136

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