Compare commits

...

3 Commits

Author SHA1 Message Date
rulingcom fce3571a84 small fix for nil values 2025-11-01 00:28:41 +08:00
rulingcom c5fe53d745 added priting and pdf 2025-10-31 23:10:37 +08:00
rulingcom 8b3049b1c7 WIP: print settings 2025-10-27 23:32:08 +08:00
13 changed files with 247 additions and 101 deletions

View File

@ -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?
@ -176,6 +193,10 @@ class Admin::PropertyHiresController < OrbitAdminController
end
end
def print_setting
@property = Property.where(:uid => params[:id].split("-").last).first rescue nil
end
def delete_booking_details
booking = PHire.find(params[:id])
property = booking.property

View File

@ -65,6 +65,7 @@ module Admin::PropertyHiresHelper
end
return data
end
def render_custom_text_field(f,field_name,type)
text = "<div>
<div class=\"input-append\">
@ -80,10 +81,11 @@ module Admin::PropertyHiresHelper
end
text += "</div>
</div>
</div>
</div>"
text.html_safe
</div>
</div>"
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.gsub("{#{field_name}}", "")
when "datetime"
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).strftime("%Y/%m/%d %H:%M")) rescue print_format.gsub("{#{field_name}}", "")
else
print_format = print_format.gsub("{#{field_name}}", phire.send(field_name).to_s) rescue print_format.gsub("{#{field_name}}", "")
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
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
body {
font-family: Arial, sans-serif;
font-size: 12px;
line-height: 1.4;
}
table {
width: 100%;
border-collapse: collapse;
margin-bottom: 20px;
}
th, td {
border: 1px solid #ddd;
padding: 8px;
text-align: left;
}
img {
max-width: 100%;
height: auto;
}
</style>
</head>
<body>
#{formatted_text}
</body>
</html>
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

View File

@ -27,6 +27,7 @@ class PHire
field :mobile_phone_of_contact_person
field :contact_person_Email
field :contact_person_department
field :recommendation
belongs_to :property
has_many :p_hire_field_values, :autosave => true, :dependent => :destroy
accepts_nested_attributes_for :p_hire_field_values, allow_destroy: true

View File

@ -35,6 +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, type: String, default: ""
mount_uploader :image, ImageUploader

View File

@ -816,6 +816,12 @@ ul.list-unstyled li {
</div>
</div>
<% end %>
<div class="form-group">
<%= f.label :recommendation, t("property_hire.recommendation"), :class => "col-sm-#{label_col} control-label" %>
<div class="col-sm-1">
<%= f.text_area :recommendation %>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-<%=label_col%> col-sm-<%=input_col%>">
<% if f.object.id.present? %>

View File

@ -100,6 +100,7 @@
<li><a href="#available_time" data-toggle="tab"><%= t('property_hire.available_time')%></a></li>
<li><a href="#auto_send_email_set" data-toggle="tab"><%= t('property_hire.auto_send_email_set') %></a></li>
<li><a href="#settings" data-toggle="tab"><%= t('property_hire.reservation_fields') %></a></li>
<li><a href="#printsettings" data-toggle="tab"><%= t('property_hire.print_settings') %></a></li>
</ul>
<!-- Module -->
<div class="tab-content module-area">
@ -136,16 +137,6 @@
<%= f.text_field :other_location %>
</div>
</div>
<%
=begin%>
<div class="control-group">
<%= f.label :property_number, t("property_hire.property_number"), :class => "control-label muted" %>
<div class="controls">
<%= f.text_field :property_number %>
</div>
</div>
<%
=end%>
<div class="control-group">
<%= f.label :p_display_start_time, t("property_hire.p_display_start_time"), :class => "control-label muted" %>
<div class="controls">
@ -193,16 +184,6 @@
</div>
</div>
</div>
<%
=begin%>
<div class="control-group">
<%= f.label :purchase_date, t("property_hire.purchase_date"), :class => "control-label muted" %>
<div class="controls">
<%= f.datetime_picker :purchase_date, :no_label => true, :new_record => @property.new_record?, :picker_type => "date", :format => "yyyy/MM/dd" %>
</div>
</div>
<%
=end%>
<div class="control-group">
<%= f.label :owners, t("property_hire.owners"), :class => "control-label muted" %>
<div class="controls">
@ -233,16 +214,6 @@
<%= f.text_field :owner_phone %>
</div>
</div>
<%
=begin%>
<div class="control-group">
<%= f.label :price, t("property_hire.price"), :class => "control-label muted" %>
<div class="controls">
<%= f.text_field :price %>
</div>
</div>
<%
=end%>
</div>
<!-- page_setting -->
<div class="tab-pane fade" id="page_setting">
@ -597,66 +568,66 @@
</ul>
</div>
</div>
<!-- reservation fields -->
<div class="tab-pane fade" id="settings">
<% fields_name = f.object.class::FIELDSNAME %>
<div style="padding: 1.2em;">
<table style="margin: 0;">
<thead>
<th style="padding:0 5px;"><%= t('property_hire.field_name') %></th>
<th style="padding:0 5px;"><%= t('property_hire.name') %></th>
<th style="padding:0 5px;"><%= t('property_hire.placeholder') %></th>
<th style="padding:0 5px;"><%= t('property_hire.disable') %></th>
<th style="padding:0 5px;"><%= t('property_hire.required') %></th>
<th style="padding:0 5px;"><%= t('property_hire.for_label') %></th>
<th style="padding:0 5px;"><%= t('property_hire.for_display') %></th>
</thead>
<tbody>
<% fields_name.each do |field_name| %>
<tr>
<td style="padding:0 5px;">
<%= t("property_hire.#{field_name}") %>
</td>
<td style="padding:0 5px;">
<%= render_custom_text_field(f,field_name,"name") %>
</td>
<td style="padding:0 5px;">
<%= render_custom_text_field(f,field_name,"placeholder") %>
</td>
<td style="padding:0 5px;">
<input type="hidden" name='<%= "#{f.object_name}[#{field_name}][enable]" %>' value="1">
<%= check_box_tag("#{f.object_name}[#{field_name}][enable]", "0" , (f.object.send(field_name)["enable"] == "0" rescue false)) %>
</td>
<td style="padding:0 5px;">
<input type="hidden" name='<%= "#{f.object_name}[#{field_name}][required]" %>' value="false">
<%= check_box_tag("#{f.object_name}[#{field_name}][required]", "true" , (f.object.send(field_name)["required"] == "true" rescue false), class: "for_required") %>
<% for_label = f.object.send(field_name)["required"] == "true" ? true : false %>
</td>
<td style="padding:0 5px;">
<%= f.radio_button :calendar_label_field_name, field_name, {:class => "for_label_selection #{(for_label == true ? '' : 'hide')}" } %>
</td>
<td style="padding:0 5px;">
<input type="hidden" name='<%= "#{f.object_name}[#{field_name}][for_display]" %>' value="false">
<%= check_box_tag("#{f.object_name}[#{field_name}][for_display]", "true" , (f.object.send(field_name)["for_display"] == "true" rescue false), class: "for_display") %>
<% for_label = f.object.send(field_name)["for_display"] == "true" ? true : false %>
</td>
</tr>
<% end %>
</tbody>
</table>
<table style="margin: 0;">
<thead>
<th style="padding:0 5px;"><%= t('property_hire.field_name') %></th>
<th style="padding:0 5px;"><%= t('property_hire.name') %></th>
<th style="padding:0 5px;"><%= t('property_hire.placeholder') %></th>
<th style="padding:0 5px;"><%= t('property_hire.disable') %></th>
<th style="padding:0 5px;"><%= t('property_hire.required') %></th>
<th style="padding:0 5px;"><%= t('property_hire.for_label') %></th>
<th style="padding:0 5px;"><%= t('property_hire.for_display') %></th>
</thead>
<tbody>
<% fields_name.each do |field_name| %>
<tr>
<td style="padding:0 5px;">
<%= t("property_hire.#{field_name}") %>
</td>
<td style="padding:0 5px;">
<%= render_custom_text_field(f,field_name,"name") %>
</td>
<td style="padding:0 5px;">
<%= render_custom_text_field(f,field_name,"placeholder") %>
</td>
<td style="padding:0 5px;">
<input type="hidden" name='<%= "#{f.object_name}[#{field_name}][enable]" %>' value="1">
<%= check_box_tag("#{f.object_name}[#{field_name}][enable]", "0" , (f.object.send(field_name)["enable"] == "0" rescue false)) %>
</td>
<td style="padding:0 5px;">
<input type="hidden" name='<%= "#{f.object_name}[#{field_name}][required]" %>' value="false">
<%= check_box_tag("#{f.object_name}[#{field_name}][required]", "true" , (f.object.send(field_name)["required"] == "true" rescue false), class: "for_required") %>
<% for_label = f.object.send(field_name)["required"] == "true" ? true : false %>
</td>
<td style="padding:0 5px;">
<%= f.radio_button :calendar_label_field_name, field_name, {:class => "for_label_selection #{(for_label == true ? '' : 'hide')}" } %>
</td>
<td style="padding:0 5px;">
<input type="hidden" name='<%= "#{f.object_name}[#{field_name}][for_display]" %>' value="false">
<%= check_box_tag("#{f.object_name}[#{field_name}][for_display]", "true" , (f.object.send(field_name)["for_display"] == "true" rescue false), class: "for_display") %>
<% for_label = f.object.send(field_name)["for_display"] == "true" ? true : false %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<hr style="border-color: black; ">
<div class="control-group">
<% field_name = "enable_notes_selector" %>
<label class="control-label muted"><%= t("property_hire.#{field_name}") %></label>
<div class="controls">
<label for="act_enabled_name" class="checkbox inline">
<%= hidden_field_tag "property[#{field_name}]", "0" %>
<%= f.check_box_tag "property[#{field_name}]", "1" , (@property[field_name] rescue false) %>
Enable
</label>
</div>
</div>
<div class="control-group" id="selector_block">
<% field_name = "enable_notes_selector" %>
<label class="control-label muted"><%= t("property_hire.#{field_name}") %></label>
<div class="controls">
<label for="act_enabled_name" class="checkbox inline">
<%= hidden_field_tag "property[#{field_name}]", "0" %>
<%= f.check_box_tag "property[#{field_name}]", "1" , (@property[field_name] rescue false) %>
Enable
</label>
</div>
<div class="control-group" id="selector_block">
<% field_name = "notes_selector" %>
<label class="control-label muted"><%= t("property_hire.#{field_name}") %></label>
<div class="controls">
@ -718,8 +689,23 @@
</div>
<a id="add_note_select_field" class="btn btn-primary"><%=t(:add)%></a>
</div>
</div>
</div>
<!-- print settings -->
<div class="tab-pane fade" id="printsettings">
<table>
<tbody>
<tr>
<td>
<%= label_tag t('property_hire.print_format') %>
</td>
<td>
<%= f.text_area :print_format,class: 'ckeditor' %>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<!-- Language Tabs -->
<div class="nav-name"><strong><%= t(:language) %></strong></div>
@ -762,17 +748,17 @@
</div>
<% end %>
</div>
<!-- Form Actions -->
<div class="form-actions">
<% referer = request.referer rescue nil %>
<% referer = get_referer_url if referer.blank? || request.host != URI.parse(URI.encode(referer)).host %>
<input type="hidden" name="referer_url" value="<%= referer %>">
<% unless @property.new_record? %>
<%= hidden_field_tag("property[id]", @property.id) %>
<% end %>
<%= f.submit t('submit'), class: 'btn btn-primary' %>
<%= link_to t('cancel'), referer, :class=>"btn" %>
</div>
<!-- Form Actions -->
<div class="form-actions">
<% referer = request.referer rescue nil %>
<% referer = get_referer_url if referer.blank? || request.host != URI.parse(URI.encode(referer)).host %>
<input type="hidden" name="referer_url" value="<%= referer %>">
<% unless @property.new_record? %>
<%= hidden_field_tag("property[id]", @property.id) %>
<% end %>
<%= f.submit t('submit'), class: 'btn btn-primary' %>
<%= link_to t('cancel'), referer, :class=>"btn" %>
</div>
<script type="text/javascript">
$("#bulletin_display_img").click(function(){$("#image_display_setting").toggleClass("hide")})
$(document).ready(function(){

View File

@ -0,0 +1,30 @@
<style>
input[type="button"],button,input[type="submit"] {
outline: 0;
border-radius: 1.3em;
background: #d9e4f7;
font-weight: bold;
outline: 0;
}
input[type="button"]:hover,button:hover,input[type="submit"]:hover {
background: #be8a8a;
}
.print_text{
padding: 2em;
width: 650px;
margin: auto;
}
@media print
{
.no-print, .no-print *
{
display: none !important;
}
}
</style>
<div class="no-print" style="text-align: center;">
<a href="<%= print_pdf_admin_property_hire_path(@phire) %>" target="_blank"><%= t('property_hire.save_data') %></a>
</div>
<div class="print_text">
<%= @html.html_safe %>
</div>

View File

@ -122,6 +122,7 @@
</td>
<td>
<% if can_edit_or_delete?(p_hire.property) %>
<a href="<%= print_hire_admin_property_hire_path(p_hire) %>" class="btn btn-info"><%= t("property_hire.print") %></a>
<a href="<%= edit_hire_admin_property_hire_path(p_hire) %>" class="btn btn-info"><%= t("property_hire.edit") %></a>
<a href="<%= show_booking_details_admin_property_hire_path(p_hire, :page => params[:page]) %>" class="btn btn-info"><%= t("property_hire.view") %></a>
<% if p_hire.passed %>

View File

@ -76,6 +76,7 @@
<% if manager %>
<% if @booking.passed %>
<a href="<%= pass_booking_admin_property_hire_path(@booking, :status => "reject") %>" class="btn btn-warning"><%= t("property_hire.reject") %></a>
<a href="<%= print_hire_admin_property_hire_path(@booking) %>" class="btn btn-info"><%= t("property_hire.print") %></a>
<% else %>
<a href="<%= pass_booking_admin_property_hire_path(@booking, :status => "accept") %>" class="btn btn-success"><%= t("property_hire.accept") %></a>
<% end %>

View File

@ -214,3 +214,8 @@ en:
property_color: Property color
admin_reserve: Reserve
for_display: Display
recommendation: Recommendation
print_format: Print Format
print_settings: Print Settings
print: Print
save_data: Print PDF

View File

@ -235,4 +235,9 @@ zh_tw:
export_reservation_data: Export data
for_label: For Label
property_color: Property color
for_display: Display
for_display: Display
recommendation: 擬辦/註解
print_format: 列印格式
print_settings: 列印格式設定
print: 列印
save_data: Print PDF

View File

@ -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"

View File

@ -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