Rails 5 位大虾 pdf 显示总 table 值

Rails 5 Prawn pdf show total table values

在我看来我有

<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

大虾生成的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

这没有给我错误,但没有显示任何值。

正确的语法是什么?

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

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

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

希望能奏效..