Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'ruport/rails'
- class Order < ActiveRecord::Base
- has_many :line_items
- validates_presence_of :name, :email, :phone, :pay_type
- acts_as_reportable
- def to_pdf
- titles = LineItem.to_ds :find => { :conditions => ["order_id = ?", id]}
- titles.remove_columns!('id','order_id')
- report = Ruport::Format.order_object :plugin => :pdf, :data => titles
- report.ref = "WEB"+id.to_s
- report.date = created_at.strftime("%d/%m/%Y") unless created_at.nil?
- report.name = name unless name.nil?
- report.custcode = custcode unless custcode.nil?
- report.phone = phone unless phone.nil?
- report.email = email unless email.nil?
- report.custref = custref unless custref.nil?
- report.comments = comments unless comments.nil?
- company_data = []
- company_data << "Rainbow Book Agencies"
- company_data << "Word of Life Dist (Aust.)"
- company_data << "303 Arthur St"
- company_data << "Fairfield VIC 3070"
- company_data << "Ph: +61 3 9481 6611"
- company_data << "Fax: +61 3 9481 2763"
- company_data << "rba@rainbowbooks.com.au"
- company_data << "ABN: 11 111 111 111"
- report.company_data = company_data
- report.render
- end
- end
- module Ruport
- class Format::Engine::Order < Format::Engine
- # company header
- attribute :company_data
- # order meta data
- attribute :comments
- attribute :custcode
- attribute :custref
- attribute :date
- attribute :email
- attribute :name
- attribute :phone
- attribute :ref
- renderer do
- active_plugin.build_company_header
- active_plugin.build_order_header
- active_plugin.render_order
- end
- alias_engine Order, :order_engine
- Format.build_interface_for Order, :order
- end
- class Format::Plugin::PDFPlugin < Format::Plugin
- renderer :order do
- require "pdf/writer"
- require "pdf/simpletable"
- return unless defined? PDF::Writer
- pdf = PDF::Writer.new( :paper => "A4")
- pdf.start_page_numbering(500, 20, 8, :right)
- #Report Title
- pdf.text "Order", :font_size => 28, :justification => :right
- # Company Information in top lefthand corner
- helper(:build_company_header) { |eng|
- if eng.company_data.class == Array
- PDF::SimpleTable.new do |table|
- @values = []
- eng.company_data.each do |line|
- @values << { "value" => line}
- end
- table.data = @values
- table.column_order = ["value"]
- table.show_headings = false
- table.font_size = 10
- table.show_lines = :outer
- table.shade_rows = :none
- table.width = 200
- table.position = :left
- table.orientation = :right
- table.render_on(pdf)
- end
- end
- }
- # Order details
- helper(:build_order_header) { |eng|
- pdf.add_text_wrap(310, 750, 80, "Date:", 12)
- pdf.add_text_wrap(310, 730, 80, "Ref:", 12)
- pdf.add_text_wrap(310, 710, 80, "Customer:", 12)
- pdf.add_text_wrap(310, 690, 80, "Cust Ref:", 12)
- pdf.add_text_wrap(310, 670, 80, "Phone:", 12)
- pdf.add_text_wrap(310, 650, 80, "Email:", 12)
- pdf.add_text_wrap(390, 750, 200, eng.date.to_s, 12) unless eng.date.nil?
- pdf.add_text_wrap(390, 730, 200, eng.ref, 12)
- pdf.add_text_wrap(390, 710, 200, eng.name, 12)
- pdf.add_text_wrap(390, 690, 200, eng.custref, 12)
- pdf.add_text_wrap(390, 670, 200, eng.phone, 12)
- pdf.add_text_wrap(390, 650, 200, eng.email, 12)
- }
- # order contents
- pdf.y = 620
- PDF::SimpleTable.new do |table|
- table.width = 450
- table.orientation = :center
- table.data = data
- table.show_lines = :outer
- table.column_order = data.fields
- table.render_on(pdf)
- table.font_size = 12
- end
- # footer
- pdf.open_object do |footer|
- pdf.save_state
- pdf.stroke_color! Color::Black
- pdf.stroke_style! PDF::Writer::StrokeStyle::DEFAULT
- pdf.add_text_wrap(50, 20, 200, "Printed at " + Time.now.strftime("%H:%M %d/%m/%Y"), 8)
- pdf.restore_state
- pdf.close_object
- pdf.add_object(footer, :all_pages)
- end
- pdf.stop_page_numbering(true, :current)
- pdf.render
- end
- register_on :order_engine
- end
- end
Add Comment
Please, Sign In to add comment