Guest User

Untitled

a guest
Feb 19th, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.65 KB | None | 0 0
  1. require 'ruport/rails'
  2.  
  3. class Order < ActiveRecord::Base
  4.  
  5. has_many :line_items
  6.  
  7. validates_presence_of :name, :email, :phone, :pay_type
  8.  
  9. acts_as_reportable
  10.  
  11. def to_pdf
  12. titles = LineItem.to_ds :find => { :conditions => ["order_id = ?", id]}
  13. titles.remove_columns!('id','order_id')
  14. report = Ruport::Format.order_object :plugin => :pdf, :data => titles
  15.  
  16. report.ref = "WEB"+id.to_s
  17. report.date = created_at.strftime("%d/%m/%Y") unless created_at.nil?
  18. report.name = name unless name.nil?
  19. report.custcode = custcode unless custcode.nil?
  20. report.phone = phone unless phone.nil?
  21. report.email = email unless email.nil?
  22. report.custref = custref unless custref.nil?
  23. report.comments = comments unless comments.nil?
  24.  
  25. company_data = []
  26. company_data << "Rainbow Book Agencies"
  27. company_data << "Word of Life Dist (Aust.)"
  28. company_data << "303 Arthur St"
  29. company_data << "Fairfield VIC 3070"
  30. company_data << "Ph: +61 3 9481 6611"
  31. company_data << "Fax: +61 3 9481 2763"
  32. company_data << "rba@rainbowbooks.com.au"
  33. company_data << "ABN: 11 111 111 111"
  34.  
  35. report.company_data = company_data
  36.  
  37. report.render
  38. end
  39.  
  40. end
  41.  
  42. module Ruport
  43.  
  44. class Format::Engine::Order < Format::Engine
  45.  
  46. # company header
  47. attribute :company_data
  48.  
  49. # order meta data
  50. attribute :comments
  51. attribute :custcode
  52. attribute :custref
  53. attribute :date
  54. attribute :email
  55. attribute :name
  56. attribute :phone
  57. attribute :ref
  58.  
  59. renderer do
  60. active_plugin.build_company_header
  61. active_plugin.build_order_header
  62. active_plugin.render_order
  63. end
  64.  
  65. alias_engine Order, :order_engine
  66. Format.build_interface_for Order, :order
  67.  
  68. end
  69.  
  70. class Format::Plugin::PDFPlugin < Format::Plugin
  71.  
  72. renderer :order do
  73. require "pdf/writer"
  74. require "pdf/simpletable"
  75. return unless defined? PDF::Writer
  76. pdf = PDF::Writer.new( :paper => "A4")
  77.  
  78. pdf.start_page_numbering(500, 20, 8, :right)
  79. #Report Title
  80. pdf.text "Order", :font_size => 28, :justification => :right
  81.  
  82. # Company Information in top lefthand corner
  83. helper(:build_company_header) { |eng|
  84.  
  85. if eng.company_data.class == Array
  86.  
  87. PDF::SimpleTable.new do |table|
  88. @values = []
  89. eng.company_data.each do |line|
  90. @values << { "value" => line}
  91. end
  92. table.data = @values
  93. table.column_order = ["value"]
  94. table.show_headings = false
  95. table.font_size = 10
  96. table.show_lines = :outer
  97. table.shade_rows = :none
  98. table.width = 200
  99. table.position = :left
  100. table.orientation = :right
  101. table.render_on(pdf)
  102. end
  103. end
  104. }
  105.  
  106. # Order details
  107. helper(:build_order_header) { |eng|
  108.  
  109. pdf.add_text_wrap(310, 750, 80, "Date:", 12)
  110. pdf.add_text_wrap(310, 730, 80, "Ref:", 12)
  111. pdf.add_text_wrap(310, 710, 80, "Customer:", 12)
  112. pdf.add_text_wrap(310, 690, 80, "Cust Ref:", 12)
  113. pdf.add_text_wrap(310, 670, 80, "Phone:", 12)
  114. pdf.add_text_wrap(310, 650, 80, "Email:", 12)
  115.  
  116. pdf.add_text_wrap(390, 750, 200, eng.date.to_s, 12) unless eng.date.nil?
  117. pdf.add_text_wrap(390, 730, 200, eng.ref, 12)
  118. pdf.add_text_wrap(390, 710, 200, eng.name, 12)
  119. pdf.add_text_wrap(390, 690, 200, eng.custref, 12)
  120. pdf.add_text_wrap(390, 670, 200, eng.phone, 12)
  121. pdf.add_text_wrap(390, 650, 200, eng.email, 12)
  122. }
  123.  
  124. # order contents
  125. pdf.y = 620
  126.  
  127. PDF::SimpleTable.new do |table|
  128. table.width = 450
  129. table.orientation = :center
  130. table.data = data
  131. table.show_lines = :outer
  132. table.column_order = data.fields
  133. table.render_on(pdf)
  134. table.font_size = 12
  135. end
  136.  
  137.  
  138. # footer
  139. pdf.open_object do |footer|
  140. pdf.save_state
  141. pdf.stroke_color! Color::Black
  142. pdf.stroke_style! PDF::Writer::StrokeStyle::DEFAULT
  143.  
  144. pdf.add_text_wrap(50, 20, 200, "Printed at " + Time.now.strftime("%H:%M %d/%m/%Y"), 8)
  145.  
  146. pdf.restore_state
  147. pdf.close_object
  148. pdf.add_object(footer, :all_pages)
  149. end
  150.  
  151. pdf.stop_page_numbering(true, :current)
  152. pdf.render
  153. end
  154.  
  155. register_on :order_engine
  156. end
  157. end
Add Comment
Please, Sign In to add comment