catmal
catmal

Reputation: 1758

Rails 5 Prawn pdf show total table values

In my view I have

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

This shows a correct total for a column. @grand_total is defined in my controller and it's the sum of total defined in model.

My model

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

My controller

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

On the pdf generated by prawn I want to show the same so I tried to enter on the corresponding pdf.rb file:

   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

which gives me no error but shows no value.

What is the correct syntax?

Upvotes: 2

Views: 393

Answers (1)

Ren
Ren

Reputation: 1374

You can explicitly include the ActionView::Helpers::NumberHelper module or whatever module you want in your prawn file.

Try passing in the @grand_total instance variable in your pdf file's initialize method:

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

And pass in @grand_total in your controller too when you create a new Pdf object:

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

Hopefully that should work..

Upvotes: 1

Related Questions