Skip to content

Instantly share code, notes, and snippets.

@zhiyao
Created October 20, 2016 03:08
Show Gist options
  • Save zhiyao/797c9caaa9df8412059c2ef195eebc31 to your computer and use it in GitHub Desktop.
Save zhiyao/797c9caaa9df8412059c2ef195eebc31 to your computer and use it in GitHub Desktop.
require 'prawn/fast_png'
class InvoicePdf < Prawn::Document
def initialize(statement, view)
super(top_margin: 70)
@statement = statement
@line_items = @statement.line_items.order('created_at asc')
@student = @statement.student
@view = view
@width = margin_box.right
@height = margin_box.top
header_left
header_right
#statement_info
line_items
notes
# total_price
end
def header_left
@cursor = cursor()
move_up 20
logo = "public/images/logo.jpeg"
image logo, width: 100
move_cursor_to @cursor
indent 120 do
text "Company Pte. Ltd.", size: 15, style: :bold
text "(201112625E)\n\n", size: 12
text "Jurong West Street 52,
#00-000,
Singapore 640517
Tel: (65)9999 9999", size: 12
end
end
def header_right
move_cursor_to @cursor
indent(@width-200) do
data = [["Invoice No", "#{@statement.running}"], ["Date Issued", "#{@statement.for_month_of.strftime("%d %B %Y")}"], ["Period", "#{@statement.for_month_of.strftime("%B %Y")}"], ["Account no", "#{@student.client_nric}"]]
table(data) do |t|
t.columns(0..2).align = :left
end
end
end
def line_items
move_down 50
text "<u>Cash Invoice<u>", size: 15, style: :bold, align: :center, inline_format: TRUE
move_down 10
table line_item_rows do
row(0).font_style = :bold
columns(2..4).align = :right
column(1).width = 150
self.row_colors = ["DDDDDD", "FFFFFF"]
self.header = true
end
move_down 50
data = [["Student", "Amount Paid", "Amount Due"], ["#{@student.name}", "#{@statement.total_paid}", "#{price(@statement.total)}"]]
table(data, position: :center) do |t|
t.row(0).font_style = :bold
t.columns(2).align = :right
end
end
def line_item_rows
[["ITEM", "DESCRIPTION", "QUANTITY", "UNIT PRICE", "AMOUNT(SGD)"]] +
@line_items.map do |item|
[item.name, item.description, item.quantity, price(item.amount), price(item.amount * item.quantity)]
end
end
def notes
move_down 50
table [["•", "All payments must be made direct to Company Pte. Ltd. Payment made through intermediary will be the customer’s own peril."],
["•", "Fees made are not refundable or returnable"],
["•", "All cheque should be made crossed and made payable to Company Pte. Ltd."]], cell_style: {border_width: 0}
end
def price(num)
@view.number_to_currency(num)
end
def total_price
move_down 15
@total_price = 0
@line_items.map do |item|
@total_price += item.amount * item.quantity
end
text "Total Price: #{price(@total_price)}"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment