Skip to content

Instantly share code, notes, and snippets.

@serg2801
Created August 18, 2017 07:26
Show Gist options
  • Save serg2801/2c17f9b2319604fc4a171d679bb908ab to your computer and use it in GitHub Desktop.
Save serg2801/2c17f9b2319604fc4a171d679bb908ab to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require 'prawn'
class OrderPdf < Prawn::Document
def initialize(order, pdf_name)
super()
@order = order
file_name = Rails.root.join("public/pdf/orders/#{pdf_name}.pdf")
create_dirs
header
table_content if order.both? || order.room_details?
text_content if order.both? || order.summary?
text_content_total if order.both? || order.summary?
render_file(file_name)
end
WIDTHS = [50, 110, 50, 130, 40, 40, 40, 40, 40]
HEADERS = ['Item#', 'Description', 'Color', 'Dimensions', 'MSRP', 'Price', 'Select', 'Order Total', 'Status']
def header
item = Item.find_by(item_number: @order.order_lists.first[:item]) unless @order.order_lists.blank?
image "#{Rails.root}/app/assets/images/default_for_pdf.png", width: 540, height: 252
unless item.blank?
room = item.room
move_down(10)
text "GROUP #{room.item_group_code} - #{room.group_name}", size: 15
move_down(10)
end
end
def text_content
move_down(10)
text 'Summary', size: 15
horizontal_line_order
y_position = cursor - 10
bounding_box([0, y_position], width: 200, height: 80) do
fill_color 'bcbaba'
text 'Sub Total:', size: 10
move_down(10)
text 'Sales Tax:', size: 10
move_down(10)
text 'Coupon:', size: 10
move_down(10)
text 'Count Payment:', size: 10, width: 80
end
bounding_box([500, y_position], width: 200, height: 80) do
fill_color 'bcbaba'
text "$#{@order.sub_total}", size: 10
move_down(10)
text "$#{@order.tax}", size: 10
move_down(10)
text "$#{@order.coupon}", size: 10
move_down(10)
text "$#{@order.count_payment}", size: 10, width: 80
end
end
def text_content_total
horizontal_line_order
y_position = cursor - 10
bounding_box([0, y_position], width: 270, height: 10) do
text 'Total:', size: 10, style: :bold, color: '000000'
end
bounding_box([500, y_position], width: 270, height: 10) do
text "$#{@order.total}", size: 10, style: :bold, color: '000000'
end
horizontal_line_order
end
def table_content
table order_rows do
row(0).style borders: [], size: 9, text_color: 'e8e1e1'
self.header = true
self.row_colors = ['e8e1e1', 'ffffff']
self.column_widths = WIDTHS
cells.style borders: [], size: 9, text_color: '000000', height: 20
end
end
def horizontal_line_order
move_down(10)
stroke_color 'e8e1e1'
stroke_horizontal_rule
end
def order_rows
[HEADERS] + @order.order_lists.map do |item|
[item.item, item.description, item.color, item.dimensions, item.msrp,
item.price, item.select, item.order_total, item.inventory_status]
end
end
def create_dirs
dir_pdf = "#{Rails.root}/public/pdf"
dir_orders = "#{Rails.root}/public/pdf/orders"
FileUtils.mkdir(dir_pdf) unless Dir.exist?(dir_pdf)
FileUtils.mkdir(dir_orders) unless Dir.exist?(dir_orders)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment