Skip to content

Commit

Permalink
update the service
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Sep 20, 2024
1 parent ce0a819 commit 3a55a2f
Showing 1 changed file with 70 additions and 10 deletions.
80 changes: 70 additions & 10 deletions app/models/services/pdf/service_order_pdf_generator.rb
Original file line number Diff line number Diff line change
@@ -45,20 +45,80 @@ def generate_client_details(pdf)
pdf.text "Data de conclusão: "
pdf.move_down 20
end

def generate_products_table(pdf)
pdf.text "Peças", size: 14, style: :bold
pdf.move_down 10


data = [["Produto", "Código", "Quantidade", "Preço un.", "Valor total"]]

@production.production_products.each do |pp|
pdf.text "Produto: #{pp.product.name}"
pdf.text "Código: #{pp.product.sku}"
pdf.text "Quantidade: #{pp.quantity}"
pdf.text "Preço un.: #{number_to_currency(pp.unit_price)}"
pdf.text "Valor total: #{number_to_currency(pp.total_price)}"
pdf.move_down 10
data << [
pp.product.name,
pp.product.sku,
pp.quantity,
number_to_currency(pp.unit_price),
number_to_currency(pp.total_price)
]
end


column_widths = [200, 100, 60, 60, 80]

# Calculate row heights
row_heights = data.map do |row|
row.map.with_index do |cell, i|
pdf.height_of(cell.to_s, width: column_widths[i], size: 10) + 10 # Add some padding
end.max
end

pdf.bounding_box([0, pdf.cursor], width: pdf.bounds.width, height: row_heights.sum + 1) do
y_position = pdf.bounds.top

data.each_with_index do |row, row_index|
row_height = row_heights[row_index]

# Fill header row
if row_index == 0
pdf.fill_color "DDDDDD"
pdf.fill_rectangle [0, y_position], pdf.bounds.width, row_height
pdf.fill_color "000000"
end

# Draw horizontal line
pdf.stroke_horizontal_line 0, pdf.bounds.width, at: y_position

# Draw cell contents
x_position = 0
row.each_with_index do |cell, col_index|
width = column_widths[col_index]
pdf.bounding_box([x_position, y_position], width: width, height: row_height) do
pdf.text_box cell.to_s,
size: 10,
align: :center,
valign: :center,
overflow: :shrink_to_fit,
style: (row_index == 0 ? :bold : :normal),
at: [0, pdf.cursor],
width: width,
height: row_height
end
x_position += width
end

y_position -= row_height
end

# Draw vertical lines
column_widths.reduce(0) do |x_position, width|
pdf.stroke_vertical_line pdf.bounds.top, pdf.bounds.bottom, at: x_position
x_position + width
end
pdf.stroke_vertical_line pdf.bounds.top, pdf.bounds.bottom, at: pdf.bounds.width

# Draw bottom line
pdf.stroke_horizontal_line 0, pdf.bounds.width, at: pdf.bounds.bottom
end

pdf.move_down 20
end

@@ -88,4 +148,4 @@ def generate_signature(pdf)
end
end
end
end
end

0 comments on commit 3a55a2f

Please sign in to comment.