Files
sx-fc/app/pdf/order_item_pdf.rb
2022-04-19 19:40:53 +06:30

176 lines
6.5 KiB
Ruby
Executable File

class OrderItemPdf < Prawn::Document
include NumberFormattable
attr_accessor :label_width,:price_column_width,:page_width, :page_height, :margin, :margin_top, :price_width, :item_width, :header_font_size, :item_font_size,:item_height,:qty_width,:total_width,:item_description_width
def initialize(print_settings,order_item, print_status, options, alt_name, before_updated_qty)
self.page_width = print_settings.page_width
self.page_height = print_settings.page_height
self.header_font_size = print_settings.header_font_size.to_i
self.item_font_size = print_settings.item_font_size.to_i
self.margin = 0
self.margin_top = 7
self.price_width = 40 # No Need for item
self.qty_width = 40
self.total_width = 40 # No Need for item
self.item_width = self.page_width - (self.qty_width - self.margin)
self.item_height = 15
self.item_description_width = self.page_width - (self.price_width + self.qty_width + self.total_width)
self.label_width=90
super(:margin => [self.margin_top, self.margin, self.margin, self.margin], :page_size => [self.page_width, self.page_height])
# super(:margin => [10, 5, 30, 5], :page_size => [200,400])
# db font setup
if print_settings.font != ""
font_families.update("#{print_settings.font}" => {
:normal => "public/fonts/#{print_settings.font}.ttf",
:italic => "public/fonts/#{print_settings.font}.ttf",
:bold => "public/fonts/#{print_settings.font}.ttf",
:bold_italic => "public/fonts/#{print_settings.font}.ttf"
})
font "#{print_settings.font}"
fallback_fonts ["Courier", "Helvetica", "Times-Roman"]
end
# font "public/fonts/Zawgyi-One.ttf"
# font "public/fonts/padauk.ttf"
#font "public/fonts/Chinese.ttf"
if order_item.source =='app'
text "Mobile Order", :size => self.header_font_size,:align => :center, :style=>:bold
move_down 1
y_position = cursor
bounding_box([0,y_position], :width =>self.item_description_width) do
text "#{order_item.contact_no}", :size => self.header_font_size,:align => :left
end
if !order_item.dining.nil?
bounding_box([self.item_description_width,y_position], :width =>self.label_width) do
text "#{ order_item.type + '-' + order_item.dining + print_status }" , :size => self.header_font_size,:align => :right
end
end
move_down 1
else
if !order_item.dining.nil?
text "#{ order_item.type + '-' + order_item.dining + print_status }", :size => self.header_font_size,:align => :center, :left_margin => -20
else
text "#{ print_status }", :size => self.header_font_size,:align => :center, :left_margin => -20
end
end
stroke_horizontal_rule
move_down 3
#order_info
order_info(order_item.order_id, order_item.order_by,order_item.order_at)
# order items
order_items(order_item, options, alt_name, precision, before_updated_qty)
end
# Write Order Information to PDF
def order_info(order_no, order_by, order_at)
#booking ID
booking_id = Order.find_by_order_id(order_no).booking.booking_id
y_position = cursor
bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do
text "Booking: #{booking_id}", :size => self.item_font_size,:align => :left
end
move_down 2
y_position = cursor
bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do
text "OrderNo: #{order_no}", :size => self.item_font_size,:align => :left
end
move_down 2
y_position = cursor
bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do
text "OrderBy: #{order_by} ", :size => self.item_font_size,:align => :left
end
move_down 2
y_position = cursor
bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do
text "Date: #{order_at.strftime("%Y-%m-%d %I:%M %p")}", :size => self.item_font_size,:align => :left
end
stroke_horizontal_rule
move_down 10
end
# Write Order items to PDF
def order_items(order_item, options, alt_name, precision, before_updated_qty)
y_position = cursor
#Add Order Item
add_order_items(order_item, options, alt_name, precision)
dash(1, :space => 1, :phase => 1)
stroke_horizontal_line 0, (self.page_width - self.margin)
add_updated_qty_text(before_updated_qty, order_item.qty, precision)
move_down 5
end
# Add order items under order info
def add_order_items(order_item, options, alt_name, precision)
y_position = cursor
move_down 5
bounding_box([0,y_position], :width => self.item_width) do
text "#{order_item.item_code} - #{order_item.item_name}", :size => self.item_font_size,:align => :left
end
bounding_box([self.item_width,y_position], :width => self.qty_width) do
text "[#{number_format(order_item.qty.to_i, :precision => precision.to_i)}]", :size => self.item_font_size,:align => :left
end
bounding_box([0,y_position], :width => self.item_width) do
text "#{order_item.item_code} - #{order_item.item_name}", :size => self.item_font_size,:align => :left
end
if alt_name
if order_item.alt_name
move_down 4
# font("public/fonts/NotoSansCJKtc-Regular.ttf") do
text "(#{order_item.alt_name})", :size => self.item_font_size,:align => :left, :inline_format => true
# end
end
end
if !options.empty?
move_down 5
# add option
y_position = cursor
bounding_box([0,y_position], :width => self.item_width) do
text "#{options}", :size => self.item_font_size,:align => :left
end
end
move_down 5
end
#add updated qty text
def add_updated_qty_text(before_updated_qty, updated_qty, precision)
if before_updated_qty.to_i > 0 && !before_updated_qty.nil?
move_down 5
# add option
y_position = cursor
bounding_box([0,y_position], :width => self.item_width, :height => self.item_height) do
text "* Change quantity [#{number_format(before_updated_qty.to_i, :precision => precision.to_i)}] to [#{number_format(updated_qty.to_i, :precision => precision.to_i)}]", :size => self.item_font_size,:align => :left
end
end
end
def get_booking_id(order_no)
booking = Order.joins(" JOIN booking_orders bo ON bo.order_id = orders.order_id")
.joins(" JOIN bookings b ON b.booking_id=bo.booking_id")
.where("orders.order_id='#{order_no}'")
.first()
return booking.booking_id
end
end