Skip to content

Instantly share code, notes, and snippets.

@ksugiarto
Created October 23, 2016 16:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ksugiarto/307ca5161e35c66f65e623a07ccc6fdf to your computer and use it in GitHub Desktop.
Save ksugiarto/307ca5161e35c66f65e623a07ccc6fdf to your computer and use it in GitHub Desktop.
Generate Prawn using custom routes
class ReportController < ApplicationController
def get_data
@customers = Customer.order(:name)
end
def sale
@sales = Sale
.where("EXTRACT(MONTH FROM sales.transaction_date)=EXTRACT(MONTH FROM CURRENT_DATE) AND EXTRACT(YEAR FROM sales.transaction_date)=EXTRACT(YEAR FROM CURRENT_DATE) AND sales.status IN (1)")
.order(:created_at).reverse_order
.pagination(params[:page])
get_data
end
def sale_filter
@sales = Sale.filter_transaction_period(params[:transaction_start_date], params[:transaction_end_date])
.filter_customer(params[:customer_id].to_i)
.filter_status(params[:status])
.sort_report(params[:sort])
.pagination(params[:page])
@sales_pdf = Sale.filter_transaction_period(params[:transaction_start_date], params[:transaction_end_date])
.filter_customer(params[:customer_id].to_i)
.filter_status(params[:status])
.sort_report(params[:sort])
@grand_total = @sales_pdf.sum(&:total_amount)
if params[:transaction_start_date].present? && params[:transaction_end_date].blank?
@period = ">= #{params[:transaction_start_date].to_date.strftime('%d-%m-%Y')}"
elsif params[:transaction_start_date].blank? && params[:transaction_end_date].present?
@period = "<= #{params[:transaction_end_date].to_date.strftime('%d-%m-%Y')}"
elsif params[:transaction_start_date].present? && params[:transaction_end_date].present?
@period = "#{params[:transaction_start_date].to_date.strftime('%d-%m-%Y')} s.d. #{params[:transaction_end_date].to_date.strftime('%d-%m-%Y')}"
else
@period = "#{Date.today.strftime("%B %Y")}"
end
respond_to do |format|
format.js
format.pdf do
pdf = SalePdf.new(@sales_pdf, view_context, ApplicationController.helpers.get_date_print, current_user.username, @period, ApplicationController.helpers.company_name, @grand_total)
send_data pdf.render, filename: "#{I18n.t 'report.sale'}-#{Time.now.strftime("%Y%m%dT%H%M%S")}.pdf",
type: "application/pdf", disposition: "inline"
end
end
end
end
class SalePdf < Prawn::Document
def initialize(models, view, date_print, employee, period, company_name, grand_total)
super()
@models = models
@view = view
@date_print = date_print
@employee = employee
@period = period
@company_name = company_name
@grand_total = grand_total
header
content
footer
end
def logo
# logopath = "#{Rails.root}/public/assets/company_logo/logo.png"
# image logopath, :width => 110, :height => 60, :position => :center, :vposition => :top
end
def check_mark
# logopath = "#{Rails.root}/app/assets/images/heavy_check_mark.png"
# image logopath, :width => 10, :height => 10, :position => :center, :vposition => :top
end
def header
define_grid(:columns => 5, :rows => 8, :gutter => 10)
grid(0,0).bounding_box do
logo
end
grid([0,1], [0,3]).bounding_box do
font("Times-Roman") do
text "#{@company_name}", :align => :center, :size => 15, :style => :bold
text "#{I18n.t 'report.sale'}", :align => :center, :size => 13, :style => :bold
text "#{I18n.t 'report.period'}: #{@period}", :align => :center, :size => 12
end
end
grid(0,4).bounding_box do
end
stroke do
self.line_width = 2
horizontal_line 0, 540, :at => 650
end
end
def model_item_rows
@number=0
[["", "#{I18n.t 'report.grand_total'}", "", "", "", "Rp #{ApplicationController.helpers.precision(@grand_total)}"],
["#{I18n.t 'row_number'}", "#{I18n.t 'sale.si_id'}",
"#{I18n.t 'sale.transaction_date'}", "#{I18n.t 'sale.customer'}",
"#{I18n.t 'sale.customer_group'}", "#{I18n.t 'sale.total_amount'}" ]] +
@models.map do |model|
[ "#{@number+=1}.", "#{model.si_id}",
"#{ApplicationController.helpers.date(model.try(:transaction_date))}",
"#{model.customer_name}", "#{model.customer_group_name}",
"#{ApplicationController.helpers.precision(model.total_amount)}" ]
end +
[["", "#{I18n.t 'report.grand_total'}", "", "", "", "Rp #{ApplicationController.helpers.precision(@grand_total)}"]]
end
def content
models = @models
table model_item_rows, :cell_style => { :font => "Times-Roman", :size => 9 }, :width => 540 do
self.header = true
self.row_colors = ["FFFFFF", "F5F5F5"]
self.column_widths = {0=>30, 1=>75, 2=>75, 3=>165, 4=>60, 5=>135}
# cells.borders = []
# rows(0).borders = [:bottom]
row(0).font_style = :bold
row(models.count+2).font_style = :bold
row(0..1).background_color = "708DC6"
row(models.count+2).background_color = "708DC6"
column(0).align=:right
columns(5..6).align=:right
end
end
def footer
move_down 10
repeat(:all) do
stroke do
horizontal_line 0, 540, :at => 2
end
number_pages "[#{I18n.t 'report.sale'} - #{I18n.t 'report.date_print'}: #{@date_print} - #{I18n.t 'report.print_by'}: #{@employee}]", :size => 9, :at => [0, 0]
end
number_pages "(<page>/<total>)", :size => 9, :at => [500, 0]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment