diff --git a/app/controllers/admin/property_hires_controller.rb b/app/controllers/admin/property_hires_controller.rb
index b3cd106..380106e 100644
--- a/app/controllers/admin/property_hires_controller.rb
+++ b/app/controllers/admin/property_hires_controller.rb
@@ -132,6 +132,23 @@ class Admin::PropertyHiresController < OrbitAdminController
redirect_to params[:referer_url]
end
+ def print_hire
+ @phire = PHire.find(params[:id]) rescue nil
+ property = @phire.property
+ @html = print_format(property.print_format, @phire)
+ end
+
+ def print_pdf
+ @phire = PHire.find(params[:id]) rescue nil
+ property = @phire.property
+ pdf_content = generate_pdf(print_format(property.print_format, @phire))
+
+ send_data pdf_content,
+ filename: "reservation_#{property.title.strip.downcase.gsub(/\s+/, "_")}_#{@phire.hiring_person_name.strip.downcase.gsub(/\s+/, "_")}.pdf",
+ type: 'application/pdf',
+ disposition: 'attachment'
+ end
+
def create
tmp_property_params = property_params
if tmp_property_params["copy_id"] && tmp_property_params["clone_p_hires"].blank?
diff --git a/app/helpers/admin/property_hires_helper.rb b/app/helpers/admin/property_hires_helper.rb
index 2b2c365..90fbb2f 100644
--- a/app/helpers/admin/property_hires_helper.rb
+++ b/app/helpers/admin/property_hires_helper.rb
@@ -65,6 +65,7 @@ module Admin::PropertyHiresHelper
end
return data
end
+
def render_custom_text_field(f,field_name,type)
text = "
@@ -80,10 +81,11 @@ module Admin::PropertyHiresHelper
end
text += "
-
- "
- text.html_safe
+
+ "
+ text.html_safe
end
+
def check_if_user_is_manager?
ma = ModuleApp.find_by_key("property_hire")
if OrbitHelper.current_user.nil?
@@ -93,6 +95,90 @@ module Admin::PropertyHiresHelper
end
return is_user_manager
end
+
+ def print_format(print_format, phire)
+ # Extract field names that are wrapped in curly braces
+ field_names = print_format.scan(/\{([^}]+)\}/).flatten
+
+ # Loop through extracted field names
+ field_names.each do |field_name|
+ field = PHire.fields[field_name]
+ if field
+ case field.type.to_s.downcase
+ when "date"
+ print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).strftime("%Y/%m/%d")) rescue print_format
+ when "datetime"
+ print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).strftime("%Y/%m/%d %H:%M")) rescue print_format
+ else
+ print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).to_s)
+ end
+ else
+ field = PHireField.where(:key => field_name, :property_id => phire.property_id).first
+ if field
+ value = PHireFieldValue.where(:p_hire_field_id => field.id, :p_hire_id => phire.id).first
+ print_format = print_format.gsub("{#{field_name}}", value ? value.get_value_by_locale(I18n.locale.to_s) : "")
+ end
+ end
+ end
+ return print_format
+ end
+
+ def generate_pdf(formatted_text)
+ html_content = <<-HTML
+
+
+
+
+
+
+
+ #{formatted_text}
+
+
+ HTML
+
+ # Generate PDF using WickedPDF
+ WickedPdf.new.pdf_from_string(
+ html_content,
+ encoding: 'UTF-8',
+ page_size: 'A4',
+ margin: {
+ top: 20,
+ bottom: 20,
+ left: 20,
+ right: 20
+ },
+ print_media_type: true,
+ zoom: 1,
+ dpi: 300,
+ footer: {
+ font_size: 8,
+ right: '[page] of [topage]',
+ spacing: 5
+ }
+ )
+ end
+
module HireMethod
extend ActionView::Helpers::UrlHelper
extend ActionView::Helpers::TagHelper
diff --git a/app/models/property.rb b/app/models/property.rb
index 8aacd78..ff9c229 100644
--- a/app/models/property.rb
+++ b/app/models/property.rb
@@ -35,7 +35,7 @@ class Property
field :p_display_end_time, type: DateTime
field :recurring_enable, type: Boolean, :default => false
field :property_color, :default => "#000000"
- field :print_format
+ field :print_format, type: String, default: ""
mount_uploader :image, ImageUploader
diff --git a/app/views/admin/property_hires/print_hire.html.erb b/app/views/admin/property_hires/print_hire.html.erb
new file mode 100644
index 0000000..9246552
--- /dev/null
+++ b/app/views/admin/property_hires/print_hire.html.erb
@@ -0,0 +1,30 @@
+
+
+
+<%= @html.html_safe %>
+
\ No newline at end of file
diff --git a/app/views/admin/property_hires/show.html.erb b/app/views/admin/property_hires/show.html.erb
index 0984832..674f1f1 100644
--- a/app/views/admin/property_hires/show.html.erb
+++ b/app/views/admin/property_hires/show.html.erb
@@ -122,6 +122,7 @@
<% if can_edit_or_delete?(p_hire.property) %>
+ <%= t("property_hire.print") %>
<%= t("property_hire.edit") %>
<%= t("property_hire.view") %>
<% if p_hire.passed %>
diff --git a/app/views/admin/property_hires/show_booking_details.html.erb b/app/views/admin/property_hires/show_booking_details.html.erb
index 3798a24..df41304 100644
--- a/app/views/admin/property_hires/show_booking_details.html.erb
+++ b/app/views/admin/property_hires/show_booking_details.html.erb
@@ -76,6 +76,7 @@
<% if manager %>
<% if @booking.passed %>
" class="btn btn-warning"><%= t("property_hire.reject") %>
+ <%= t("property_hire.print") %>
<% else %>
" class="btn btn-success"><%= t("property_hire.accept") %>
<% end %>
diff --git a/config/locales/en.yml b/config/locales/en.yml
index 084b40d..4990c4e 100644
--- a/config/locales/en.yml
+++ b/config/locales/en.yml
@@ -217,3 +217,5 @@ en:
recommendation: Recommendation
print_format: Print Format
print_settings: Print Settings
+ print: Print
+ save_data: Print PDF
diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml
index e701361..32d458d 100644
--- a/config/locales/zh_tw.yml
+++ b/config/locales/zh_tw.yml
@@ -238,4 +238,6 @@ zh_tw:
for_display: Display
recommendation: 擬辦/註解
print_format: 列印格式
- print_settings: 列印格式設定
\ No newline at end of file
+ print_settings: 列印格式設定
+ print: 列印
+ save_data: Print PDF
\ No newline at end of file
diff --git a/config/routes.rb b/config/routes.rb
index dd5252a..3d501f8 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -16,6 +16,8 @@ Rails.application.routes.draw do
delete "destroy_location"
get "show_booking_details"
get "pass_booking"
+ get "print_hire"
+ get "print_pdf"
delete "delete_booking_details"
get "edit_hire"
get "add_hire"
diff --git a/property_hire.gemspec b/property_hire.gemspec
index 094b0ed..e1556fd 100644
--- a/property_hire.gemspec
+++ b/property_hire.gemspec
@@ -39,5 +39,6 @@ Gem::Specification.new do |s|
s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.rdoc"]
s.test_files = Dir["test/**/*"]
-
+ s.add_dependency 'wicked_pdf', '~> 2.6'
+ s.add_dependency 'wkhtmltopdf-binary', '~> 0.12.6'
end
|