Skip to content

Instantly share code, notes, and snippets.

@kivanio
Last active August 29, 2015 14:17
Show Gist options
  • Save kivanio/46e31c78ab43d4d6e7f2 to your computer and use it in GitHub Desktop.
Save kivanio/46e31c78ab43d4d6e7f2 to your computer and use it in GitHub Desktop.
ActiveAdmin.register Shipment do
def generate_shipment(shipment)
# Generate Shipment
Prawn::Document.generate @shipment.shipment_location do |pdf|
# Title
pdf.text "Shipment ##{shipment.code}", :size => 25
# Sender info
pdf.text shipment.sender.first_name
pdf.text shipment.sender.address
pdf.text shipment.sender.phone
#pdf.draw_text "#{t('.created_at')}: #{l(shipment.created_at, :format => :short)}", :at => [pdf.bounds.width / 2, pdf.bounds.height - 30]
# Our company info
# pdf.float do
# pdf.bounding_box [0, pdf.bounds.top - 5], :width => pdf.bounds.width do
# pdf.text shipment.Sender.company.name, :size => 20, :align => :right
# end
# end
pdf.move_down 20
# Items
header = ['Description', 'Weight', 'Total']
items = shipment.items.collect do |item|
[item.description, item.weight.to_s, number_to_currency(item.total)]
end
items = items + [["", "", "Discount:", "#{number_with_delimiter(shipment.discount)}%"]] \
+ [["", "", "Sub-total:", "#{number_to_currency(shipment.subtotal)}"]] \
+ [["", "", "Taxes:", "#{number_to_currency(shipment.taxes)} (#{number_with_delimiter(shipment.tax)}%)"]] \
+ [["", "", "Total:", "#{number_to_currency(shipment.total)}"]]
pdf.table [header] + items, :header => true, :width => pdf.bounds.width do
row(-4..-1).borders = []
row(-4..-1).column(2).align = :right
row(0).style :font_style => :bold
row(-1).style :font_style => :bold
end
# :border_style => :grid,
# :headers => header,
# :width => pdf.bounds.width,
# :row_colors => %w{cccccc eeeeee},
# :align => { 0 => :right, 1 => :left, 2 => :right, 3 => :right, 4 => :right }
# Terms
if shipment.terms != ''
pdf.move_down 20
pdf.text 'Terms', :size => 18
pdf.text shipment.terms
end
# Notes
if shipment.notes != ''
pdf.move_down 20
pdf.text 'Notes', :size => 18
pdf.text shipment.notes
end
# Footer
pdf.draw_text "Generated at #{l(Time.now, :format => :short)}", :at => [0, 0]
end
end
end
#-------------------------------------------------------------------------#
ActiveAdmin.register Shipment do
scope :all, :default => true
scope :draft do |shipments|
shipments.where(:status => Shipment::STATUS_DRAFT)
end
scope :sent do |shipments|
shipments.where(:status => Shipment::STATUS_SENT)
end
scope :paid do |shipments|
shipments.where(:status => Shipment::STATUS_PAID)
end
index do
column :status do |shipment|
status_tag shipment.status, shipment.status_tag
end
column :code do |shipment|
link_to "##{shipment.code}", admin_shipment_path(shipment)
end
column :sender
column "Issued" do |shipment|
due = if shipment.pickup_date
" (due in #{distance_of_time_in_words Time.now, shipment.pickup_date})"
else
""
end
"#{l shipment.created_at, :format => :short}" + due
end
column :total do |shipment|
number_to_currency shipment.total
end
column do |shipment|
link_to("Details", admin_shipment_path(shipment)) + " | " + \
link_to("Edit", edit_admin_shipment_path(shipment)) + " | " + \
link_to("Delete", admin_shipment_path(shipment), :method => :delete, :confirm => "Are you sure?")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment