Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the payment pdf #346

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 32 additions & 23 deletions app/models/services/pdf/payment_order_pdf_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def generate_tailor_details(pdf)
def generate_products_table(pdf)
pdf.text "Peças Entregues", size: 14, style: :bold
pdf.move_down 10
data = [["Produto", "Quantidade", "Preço Un.", "Sujo", "Erro", "Descarte", "Devolvido", "Desconto", "Total"]]

data = [["Produto", "Quantidade", "Peças Entregues", "Preço Un.", "Sujo", "Erro", "Descarte", "Devolvido", "Desconto", "Total"]]

total_all_rows = 0
total_discount = 0

@production.production_products.each do |pp|
unit_price = pp.unit_price || 0
total_price = pp.total_price || 0
Expand All @@ -60,12 +60,13 @@ def generate_products_table(pdf)
returned_discount = pp.returned ? total_price : 0
total_discount_row = discount + returned_discount
adjusted_price = unit_price * adjusted_quantity - total_discount_row

total_all_rows += total_price
total_discount += total_discount_row

data << [
pp.product.name,
pp.quantity,
pp.pieces_delivered,
number_to_currency(unit_price),
pp.dirty,
Expand All @@ -76,10 +77,11 @@ def generate_products_table(pdf)
number_to_currency(adjusted_price)
]
end

# Add a row for totals
data << [
"Total",
@production.production_products.sum(:quantity),
@production.production_products.sum(:pieces_delivered),
"",
@production.production_products.sum(:dirty),
Expand All @@ -89,19 +91,19 @@ def generate_products_table(pdf)
number_to_currency(total_discount),
number_to_currency(total_all_rows - total_discount)
]
column_widths = [120, 60, 60, 40, 40, 40, 60, 60, 80]

column_widths = [100, 50, 50, 50, 40, 40, 40, 50, 60, 70]

# 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
pdf.height_of(cell.to_s, width: column_widths[i], size: 8) + 5 # 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]

Expand All @@ -111,17 +113,17 @@ def generate_products_table(pdf)
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,
size: 8,
align: :center,
valign: :center,
overflow: :shrink_to_fit,
Expand All @@ -132,21 +134,21 @@ def generate_products_table(pdf)
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

Expand All @@ -156,12 +158,19 @@ def generate_totals(pdf)
discount += (pp.total_price || 0) if pp.returned
discount
end

total_price = @production.production_products.sum { |pp| pp.total_price || 0 }
total_to_pay = total_price - total_discount


total_pieces_delivered_price = @production.production_products.sum do |pp|
(pp.pieces_delivered || 0) * (pp.unit_price || 0)
end

total_to_pay = total_pieces_delivered_price - total_discount

pdf.text "Total do corte: #{number_to_currency(total_price)}", style: :bold, align: :right
pdf.move_down 10
pdf.text "Total peças entregues: #{number_to_currency(total_pieces_delivered_price)}", style: :bold, align: :right
pdf.move_down 10
pdf.text "Total desconto: #{number_to_currency(total_discount)}", style: :bold, align: :right
pdf.move_down 10
pdf.text "Total a pagar: #{number_to_currency(total_to_pay)}", style: :bold, align: :right
Expand Down
Loading