Class: MenuSectionsReportPdf

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(menu_sections = nil, args = {}) ⇒ MenuSectionsReportPdf

Returns a new instance of MenuSectionsReportPdf.



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

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



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def date
  @date
end

#date_filter_typeObject

Returns the value of attribute date_filter_type.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def date_filter_type
  @date_filter_type
end

#date_fromObject

Returns the value of attribute date_from.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def date_from
  @date_from
end

#date_toObject

Returns the value of attribute date_to.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def date_to
  @date_to
end

#emailsObject

Returns the value of attribute emails.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def emails
  @emails
end

Returns the value of attribute menu_items_ids.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def menu_items_ids
  @menu_items_ids
end

Returns the value of attribute menu_items_type.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def menu_items_type
  @menu_items_type
end

Returns the value of attribute menu_sections.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def menu_sections
  @menu_sections
end

#pdf_urlObject

Returns the value of attribute pdf_url.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def pdf_url
  @pdf_url
end

#restaurant_idObject

Returns the value of attribute restaurant_id.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def restaurant_id
  @restaurant_id
end

#typeObject

Returns the value of attribute type.



19
20
21
# File 'app/my_lib/menu_sections_report_pdf.rb', line 19

def type
  @type
end

Instance Method Details



115
116
117
118
119
120
121
122
123
# File 'app/my_lib/menu_sections_report_pdf.rb', line 115

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



111
112
113
# File 'app/my_lib/menu_sections_report_pdf.rb', line 111

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

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



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'app/my_lib/menu_sections_report_pdf.rb', line 85

def generate_pdf(menu_sections, args={})
  args[:file_name] = "Hungry-Hub-report-menu-items#{Time.zone.today}-#{restaurant_id}#{Time.current_time.to_i}.pdf"
  file_pdf = self.class.new(menu_sections, 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_pdf.download_link
end

#generate_pdf_tableObject



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

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
  data.push(%w[Restaurant-ID Restaurant-Name Name-en Name-th Package-In-Use Menu-Items-count
  Created Updated Menu-Items ])

  menu_sections.find_each do |v|
    row_data = [
                  restaurant.id,
                  restaurant.name,
                  v.name_en,
                  v.name_th,
                  v.package ? 1 : 0,
                  v.menus ? v.menus.size : 0,
                  v&.created_at&.strftime('%d %b %Y'),
                  v&.updated_at&.strftime('%d %b %Y'),
                  v.menus.joins(:translations).group(:id).pluck(:name).map(&:inspect).join(', ')
              ]
    data.push(row_data)
  end
  table(data, :column_widths => TABLE_COLUMN_WIDTHS, :header => true)
end

#save_attachmentObject



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

def save_attachment
  self.attachment = store_to_attachment
end

#store_to_attachmentObject



105
106
107
108
109
# File 'app/my_lib/menu_sections_report_pdf.rb', line 105

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