Rails 5 Prawn pdf显示总表值

时间:2016-10-27 14:15:12

标签: ruby-on-rails prawn

在我看来,我有

<h4><%= number_to_currency @grand_total, precision: 0, unit: "EUR ", separator: "," %></h4>

这显示了列的正确总计。 @grand_total在我的控制器中定义,它是模型中定义的total的总和。

我的模特

class Ticketline < ActiveRecord::Base
    belongs_to :ticket, :foreign_key => 'TICKET'
    belongs_to :product, :foreign_key => 'PRODUCT'

def discount
    (self.AMOUNT - self.TOTAL) 
end

def total_amount
 ( pricesell = self.try(:PRICESELL) || 0
   units = self.try(:UNITS) || 0
   pricesell * units)
end

def total
 ( 

   price = self.try(:PRICE) || 0
   units = self.UNITS || 0
   price * units)
end

def consignor_cost
  cost = product.location.try(:DISCOUNT_CONSIGNOR)  || 0
  cost ? (self.total * cost) : 0
end


def cost_of_goods_sold
 cost = product.PRICEBUY || 0

 cost ? (cost * self.TOTALUNITS) : 0
end

def gross_profit
(self.total - self.consignor_cost - self.cost_of_goods_sold) 
end 


class ProductSale < Ticketline

end

end

我的控制器

class ProductSalesController < TicketlinesController


  def index


    params.permit!
    @q = Ticketline.joins(:product, :product => :location).group(:PRODUCT, :TICKET, :DISCOUNT_CONSIGNOR).select("PRODUCT, DISCOUNT_CONSIGNOR, UNITS, TICKET, SUM(ticketlines.PRICESELL*UNITS) AS AMOUNT, SUM(PRICE*UNITS) AS TOTAL, PRICE, UNITS, ticketlines.PRICESELL, SUM(UNITS) AS TOTALUNITS").ransack(params[:q])
    @product_sales = @q.result.paginate(:page => params[:page], :per_page => 30)
    @product_salesnp = @q.result
    @amount_total = @q.result.map(&:total_amount).sum
    @discount_total = @q.result.map(&:discount).sum
    @grand_total = @q.result.map(&:total).sum
    @consignor_cost_total = @q.result.map(&:consignor_cost).sum
    @cost_of_goods_sold_total = @q.result.map(&:cost_of_goods_sold).sum
    @gross_profit_total = @q.result.map(&:gross_profit).sum
    respond_to do |format|
    format.html

    format.pdf do
        pdf = SalesByProductPdf.new(@product_salesnp)
        pdf.render_file "report.pdf"
        send_data pdf.render, filename: 'report.pdf', type: 'application/pdf', disposition: 'inline'
      end

  end
  end
end

在prawn生成的pdf上我想显示相同的内容,所以我试着输入相应的pdf.rb文件:

   class SalesByProductPdf < Prawn::Document
 include ActionView::Helpers::NumberHelper


  def initialize(product_sales)
    super()
    @product_sales = product_sales
    header
    text_content
    table_content
    footer
  end

def header 
#something 
end

def text_content
#something
 end

def table_content
#something 
end

def footer
text number_to_currency(grand_total, precision: 0, unit: "EUR ", separator: ",")
 end
end

给了我没有错误但没有显示任何价值。

正确的语法是什么?

1 个答案:

答案 0 :(得分:1)

您可以在您的prawn文件中明确包含ActionView::Helpers::NumberHelper模块或任何模块。

尝试传入pdf文件@grand_total方法中的initialize实例变量:

class SalesByProductPdf < Prawn::Document
  include ActionView::Helpers::NumberHelper

  def initialize(product_salesnp, grand_total)
    super()
    @product_salesnp = product_salesnp
    @grand_total = grand_total
    header
    text_content
    table_content
    footer
  end

   ...

  def footer
    text number_to_currency(@grand_total, precision: 0, unit: "EUR ", separator: ",")
  end
end

当您创建新的Pdf对象时,也会在控制器中传入@grand_total

format.pdf do
  pdf = SalesByProductPdf.new(@product_salesnp, @grand_total)
  pdf.render_file "report.pdf"
  send_data pdf.render, filename: 'report.pdf', type: 'application/pdf', disposition: 'inline'
end

希望这应该有效..