Skip to content

Commit

Permalink
improve the calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Oct 17, 2024
1 parent 6fd0252 commit 028975c
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions app/models/services/pdf/payment_order_pdf_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ def generate_products_table(pdf)
pdf.text "Peças Entregues", size: 14, style: :bold
pdf.move_down 10

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

total_quantity = 0
total_pieces_delivered = 0
total_dirty = 0
total_error = 0
total_discard = 0
total_lost = 0
total_returned = 0
total_discount = 0
total_price = 0
Expand All @@ -70,15 +71,17 @@ def generate_products_table(pdf)
dirty = pp.dirty || 0
error = pp.error || 0
discard = pp.discard || 0
lost = pp.lost_pieces || 0

discount = pp.returned ? 0 : unit_price * (dirty + error + discard)
discount = pp.returned ? 0 : unit_price * (dirty + error + discard + lost)
row_total = pp.returned ? 0 : (unit_price * pieces_delivered - discount)

total_quantity += quantity
total_pieces_delivered += pieces_delivered
total_dirty += dirty
total_error += error
total_discard += discard
total_lost += lost
total_returned += pp.returned ? 1 : 0
total_discount += discount
total_price += row_total
Expand All @@ -91,6 +94,7 @@ def generate_products_table(pdf)
dirty,
error,
discard,
lost,
pp.returned ? 'Sim' : 'Não',
pp.returned ? '-' : number_to_currency(discount),
pp.returned ? '-' : number_to_currency(row_total)
Expand All @@ -106,17 +110,18 @@ def generate_products_table(pdf)
total_dirty,
total_error,
total_discard,
total_lost,
total_returned,
number_to_currency(total_discount),
number_to_currency(total_price)
]

column_widths = [100, 50, 50, 50, 40, 40, 40, 50, 60, 70]
column_widths = [80, 30, 40, 45, 30, 30, 40, 35, 45, 50, 55]

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

Expand All @@ -142,7 +147,7 @@ def generate_products_table(pdf)
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: 8,
size: 7,
align: :center,
valign: :center,
overflow: :shrink_to_fit,
Expand Down Expand Up @@ -173,7 +178,7 @@ def generate_products_table(pdf)

def generate_totals(pdf)
total_discount = @production.production_products.sum do |pp|
(pp.unit_price || 0) * ((pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))
(pp.unit_price || 0) * ((pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0) + (pp.lost_pieces || 0))
end

total_returned = @production.production_products.sum do |pp|
Expand All @@ -191,6 +196,8 @@ def generate_totals(pdf)

total_to_pay = total_pieces_delivered_price - total_discount

total_lost_pieces = @production.production_products.sum { |pp| pp.lost_pieces || 0 }

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
Expand All @@ -199,6 +206,8 @@ def generate_totals(pdf)
pdf.move_down 10
pdf.text "Total devolvido: #{number_to_currency(total_returned)}", style: :bold, align: :right
pdf.move_down 10
pdf.text "Total peças perdidas: #{total_lost_pieces}", style: :bold, align: :right
pdf.move_down 10
pdf.text "Total a pagar: #{number_to_currency(total_to_pay)}", style: :bold, align: :right
pdf.move_down 30
end
Expand Down

0 comments on commit 028975c

Please sign in to comment.