2016-10-27 4 views
2

は、私はこれは、列の正しい合計を示しRailsの5エビPDFショー合計テーブル値私の見解で

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

を持っています。 @grand_totalは私のコントローラで定義されており、それはモデルで定義されたtotalの合計です。

:私は同じことを見せたいので、私は、対応するpdf.rbファイルに入力しようとした海老によって生成されたPDFで

マイモデル

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 

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

明示的に、ActionView::Helpers::NumberHelperモジュールまたはあなたが必要とするモジュールをあなたのエンクロージャファイルに含めることができます。ことを願っ

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 

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に渡す:

はあなたのpdfファイルのinitialize方法で@grand_totalインスタンス変数に渡して試してみてください

+0

あなたの答えをありがとう。しかし、あなたのコードをどこに置くべきかはわかりません。 Iすでに私は 'デフ(GRAND_TOTAL)を初期化DEF をフッターに配置した場合、私のpdf.rbに'デフ(product_sales) スーパー() @product_sales =のproduct_sales ヘッダ TEXT_CONTENT table_content フッター end'を初期化してい super テキストnumber_to_currency(grand_total、精度:0、単位: "EUR"、区切り文字: "、") end end'まだpdfには何も表示されません。 – Catmal

+0

また、読みやすくするために質問を更新しました。 – Catmal

+0

あなたの '@ grand_total'インスタンス変数はどこから来ますか?それは '@ product_sales'モデルに関連するのですか、それとも別のモデルですか? – Ren

関連する問題