From 4a8dbde59d1707b972da2683fa965b686a50ac4f Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Mon, 23 Sep 2024 12:12:17 -0300 Subject: [PATCH] update the pdf and fix the show view --- .../pdf/payment_order_pdf_generator.rb | 107 ++++++++++++------ app/views/productions/show.html.erb | 9 +- 2 files changed, 76 insertions(+), 40 deletions(-) diff --git a/app/models/services/pdf/payment_order_pdf_generator.rb b/app/models/services/pdf/payment_order_pdf_generator.rb index 19dd2f07..e544d247 100644 --- a/app/models/services/pdf/payment_order_pdf_generator.rb +++ b/app/models/services/pdf/payment_order_pdf_generator.rb @@ -46,13 +46,14 @@ 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", "Total"]] - + + data = [["Produto", "Quantidade", "Preço Un.", "Sujo", "Erro", "Descarte", "Desconto", "Total"]] + @production.production_products.each do |pp| adjusted_quantity = pp.pieces_delivered - (pp.dirty + pp.error + pp.discard) - adjusted_price = pp.unit_price * adjusted_quantity - + discount = pp.unit_price * (pp.dirty + pp.error + pp.discard) + adjusted_price = pp.unit_price * adjusted_quantity - discount + data << [ pp.product.name, pp.pieces_delivered, @@ -60,52 +61,84 @@ def generate_products_table(pdf) pp.dirty, pp.error, pp.discard, + number_to_currency(discount), number_to_currency(adjusted_price) ] end - - table_width = pdf.bounds.width - columns = data.first.length - column_widths = [0.4, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1].map { |w| w * table_width } - row_height = 30 - - data.each_with_index do |row, row_index| - y_position = pdf.cursor - - # Draw horizontal line - pdf.stroke_horizontal_line(0, table_width, at: y_position) - - row.each_with_index do |cell, col_index| - x_position = column_widths.take(col_index).sum - cell_width = column_widths[col_index] - - # Draw vertical line - pdf.stroke_vertical_line(y_position, y_position - row_height, at: x_position) - - # Add cell content - pdf.bounding_box([x_position + 2, y_position - 2], width: cell_width - 4, height: row_height - 4) do - pdf.text cell.to_s, size: 10, align: (col_index == 0 ? :left : :center), valign: :center + + column_widths = [120, 60, 60, 40, 40, 40, 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 - - # Draw last vertical line - pdf.stroke_vertical_line(y_position, y_position - row_height, at: table_width) - - pdf.move_down row_height + 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 - - # Draw bottom line of the table - pdf.stroke_horizontal_line(0, table_width) - + pdf.move_down 20 end def generate_totals(pdf) + total_discount = @production.production_products.sum do |pp| + pp.unit_price * (pp.dirty + pp.error + pp.discard) + end + total_price = @production.production_products.sum do |pp| adjusted_quantity = pp.pieces_delivered - (pp.dirty + pp.error + pp.discard) - pp.unit_price * adjusted_quantity + discount = pp.unit_price * (pp.dirty + pp.error + pp.discard) + pp.unit_price * adjusted_quantity - discount end + 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_price)}", style: :bold, align: :right pdf.move_down 30 end diff --git a/app/views/productions/show.html.erb b/app/views/productions/show.html.erb index da262074..3233c942 100644 --- a/app/views/productions/show.html.erb +++ b/app/views/productions/show.html.erb @@ -48,12 +48,12 @@
<%= model_class.human_attribute_name(:observation) %>:
<%= @production.observation %>
<%= model_class.human_attribute_name(:notions_cost) %>:
-
<%= number_to_currency(@production.notions_cost) %>
+
<%= number_to_currency(@production.notions_cost || 0) %>
<%= model_class.human_attribute_name(:fabric_cost) %>:
-
<%= number_to_currency(@production.fabric_cost) %>
+
<%= number_to_currency(@production.fabric_cost || 0) %>
<%= t('productions.show.price_per_piece') %>:
- <% total_cost = @production.notions_cost + @production.fabric_cost + @production.total_price %> + <% total_cost = (@production.notions_cost || 0) + (@production.fabric_cost || 0) + (@production.total_price || 0) %> <% total_quantity = @production.production_products.sum(:quantity) %> <%= number_to_currency(total_cost / total_quantity) if total_quantity > 0 %>
@@ -72,6 +72,7 @@ Produto Quantidade Preço Unitário + Desconto Preço Total Peças Entregues Sujo @@ -87,6 +88,7 @@ <%= pp.product&.name || 'Nenhum produto atribuído' %> <%= pp.quantity %> <%= number_to_currency(pp.unit_price) %> + <%= number_to_currency(pp.unit_price * (pp.dirty + pp.error + pp.discard)) %> <%= number_to_currency(pp.total_price) %> <%= pp.pieces_delivered || 0 %> <%= pp.dirty || 0 %> @@ -104,6 +106,7 @@ Total da Produção <%= @production.production_products.sum(:quantity) %> + <%= number_to_currency(@production.production_products.sum { |pp| pp.unit_price * (pp.dirty + pp.error + pp.discard) }) %> <%= number_to_currency(@production.total_price) %> <%= @production.production_products.sum(:pieces_delivered) %> <%= @production.production_products.sum(:dirty) %>