From 3a55a2f702ab742d590ebff325167ea2904ec4f4 Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Fri, 20 Sep 2024 17:15:48 -0300 Subject: [PATCH 1/2] update the service --- .../pdf/service_order_pdf_generator.rb | 80 ++++++++++++++++--- 1 file changed, 70 insertions(+), 10 deletions(-) diff --git a/app/models/services/pdf/service_order_pdf_generator.rb b/app/models/services/pdf/service_order_pdf_generator.rb index e0f6023e..e8619d7f 100644 --- a/app/models/services/pdf/service_order_pdf_generator.rb +++ b/app/models/services/pdf/service_order_pdf_generator.rb @@ -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 \ No newline at end of file From 19bade6d2f8c49d012d904871ed32a47ecebd631 Mon Sep 17 00:00:00 2001 From: puppe1990 Date: Fri, 20 Sep 2024 17:31:11 -0300 Subject: [PATCH 2/2] create the order payment pdf --- .../pdf/payment_order_pdf_generator.rb | 120 ++++++++++++++++++ app/views/productions/show.html.erb | 4 +- config/routes.rb | 1 + 3 files changed, 124 insertions(+), 1 deletion(-) create mode 100644 app/models/services/pdf/payment_order_pdf_generator.rb diff --git a/app/models/services/pdf/payment_order_pdf_generator.rb b/app/models/services/pdf/payment_order_pdf_generator.rb new file mode 100644 index 00000000..19dd2f07 --- /dev/null +++ b/app/models/services/pdf/payment_order_pdf_generator.rb @@ -0,0 +1,120 @@ +require 'prawn' + +module Services + module Pdf + class PaymentOrderPdfGenerator + include ActionView::Helpers::NumberHelper + + def initialize(production) + @production = production + end + + def generate + Prawn::Document.new do |pdf| + setup_font(pdf) + generate_header(pdf) + generate_tailor_details(pdf) + generate_products_table(pdf) + generate_totals(pdf) + generate_signature(pdf) + end + end + + private + + def setup_font(pdf) + pdf.font_families.update("DejaVu" => { + normal: "#{Rails.root}/app/assets/fonts/DejaVuSans.ttf", + bold: "#{Rails.root}/app/assets/fonts/DejaVuSans-Bold.ttf" + }) + pdf.font "DejaVu" + end + + def generate_header(pdf) + pdf.text "Ordem de Pagamento N° #{@production.service_order_number}", size: 16, style: :bold + pdf.move_down 20 + end + + def generate_tailor_details(pdf) + pdf.text "Costureiro: #{@production.tailor.name}", style: :bold + pdf.text "Número da OS: #{@production.service_order_number}" + pdf.text "Data de entrada: #{@production.cut_date&.strftime("%d/%m/%Y")}" + pdf.text "Data de conclusão: #{@production.production_products.maximum(:delivery_date)&.strftime("%d/%m/%Y")}" + pdf.move_down 20 + end + + 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"]] + + @production.production_products.each do |pp| + adjusted_quantity = pp.pieces_delivered - (pp.dirty + pp.error + pp.discard) + adjusted_price = pp.unit_price * adjusted_quantity + + data << [ + pp.product.name, + pp.pieces_delivered, + number_to_currency(pp.unit_price), + pp.dirty, + pp.error, + pp.discard, + 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 + end + end + + # Draw last vertical line + pdf.stroke_vertical_line(y_position, y_position - row_height, at: table_width) + + pdf.move_down row_height + end + + # Draw bottom line of the table + pdf.stroke_horizontal_line(0, table_width) + + pdf.move_down 20 + end + + def generate_totals(pdf) + total_price = @production.production_products.sum do |pp| + adjusted_quantity = pp.pieces_delivered - (pp.dirty + pp.error + pp.discard) + pp.unit_price * adjusted_quantity + end + + pdf.text "Total a pagar: #{number_to_currency(total_price)}", style: :bold, align: :right + pdf.move_down 30 + end + + def generate_signature(pdf) + pdf.text "Assinatura do Costureiro: ________________________________" + pdf.move_down 20 + pdf.text "Data: _____/_____/_____" + end + end + end +end \ No newline at end of file diff --git a/app/views/productions/show.html.erb b/app/views/productions/show.html.erb index 6167d728..da262074 100644 --- a/app/views/productions/show.html.erb +++ b/app/views/productions/show.html.erb @@ -8,10 +8,12 @@ +
<%= link_to 'Download Service Order PDF', service_order_pdf_production_path(@production), class: 'btn btn-primary', target: '_blank' %> + <%= link_to 'Download Payment Order PDF', payment_order_pdf_production_path(@production), class: 'btn btn-success', target: '_blank' %>
- +
diff --git a/config/routes.rb b/config/routes.rb index 77acbcef..66ec21d6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,7 @@ member do patch :verify get :service_order_pdf + get :payment_order_pdf # Add this line end end