Skip to content

Commit

Permalink
Merge pull request #339 from Purple-Stock/staging
Browse files Browse the repository at this point in the history
Staging
  • Loading branch information
puppe1990 authored Sep 21, 2024
2 parents deab3b2 + 19bade6 commit 18b63b1
Show file tree
Hide file tree
Showing 4 changed files with 194 additions and 11 deletions.
120 changes: 120 additions & 0 deletions app/models/services/pdf/payment_order_pdf_generator.rb
Original file line number Diff line number Diff line change
@@ -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
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
Expand Up @@ -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

Expand Down Expand Up @@ -88,4 +148,4 @@ def generate_signature(pdf)
end
end
end
end
end
4 changes: 3 additions & 1 deletion app/views/productions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@
<div class="breadcrumb-item">Show Production</div>
</div>
</div>

<div class="actions">
<%= 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' %>
</div>

<div class="section-body">
<div class="card">
<div class="card-header">
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
member do
patch :verify
get :service_order_pdf
get :payment_order_pdf # Add this line
end
end

Expand Down

0 comments on commit 18b63b1

Please sign in to comment.