Skip to content

Commit

Permalink
Merge pull request #342 from Purple-Stock/staging
Browse files Browse the repository at this point in the history
update the pdf and fix the show view
  • Loading branch information
puppe1990 authored Sep 23, 2024
2 parents 8575a55 + 4a8dbde commit 8025ce2
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 40 deletions.
107 changes: 70 additions & 37 deletions app/models/services/pdf/payment_order_pdf_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,66 +46,99 @@ 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,
number_to_currency(pp.unit_price),
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
Expand Down
9 changes: 6 additions & 3 deletions app/views/productions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,12 @@
<dt class="col-sm-3"><%= model_class.human_attribute_name(:observation) %>:</dt>
<dd class="col-sm-9"><%= @production.observation %></dd>
<dt class="col-sm-3"><%= model_class.human_attribute_name(:notions_cost) %>:</dt>
<dd class="col-sm-9"><%= number_to_currency(@production.notions_cost) %></dd>
<dd class="col-sm-9"><%= number_to_currency(@production.notions_cost || 0) %></dd>
<dt class="col-sm-3"><%= model_class.human_attribute_name(:fabric_cost) %>:</dt>
<dd class="col-sm-9"><%= number_to_currency(@production.fabric_cost) %></dd>
<dd class="col-sm-9"><%= number_to_currency(@production.fabric_cost || 0) %></dd>
<dt class="col-sm-3"><%= t('productions.show.price_per_piece') %>:</dt>
<dd class="col-sm-9">
<% 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 %>
</dd>
Expand All @@ -72,6 +72,7 @@
<th>Produto</th>
<th>Quantidade</th>
<th>Preço Unitário</th>
<th>Desconto</th>
<th>Preço Total</th>
<th>Peças Entregues</th>
<th>Sujo</th>
Expand All @@ -87,6 +88,7 @@
<td><%= pp.product&.name || 'Nenhum produto atribuído' %></td>
<td><%= pp.quantity %></td>
<td><%= number_to_currency(pp.unit_price) %></td>
<td><%= number_to_currency(pp.unit_price * (pp.dirty + pp.error + pp.discard)) %></td>
<td><%= number_to_currency(pp.total_price) %></td>
<td><%= pp.pieces_delivered || 0 %></td>
<td><%= pp.dirty || 0 %></td>
Expand All @@ -104,6 +106,7 @@
<th>Total da Produção</th>
<td><%= @production.production_products.sum(:quantity) %></td>
<td></td>
<td><%= number_to_currency(@production.production_products.sum { |pp| pp.unit_price * (pp.dirty + pp.error + pp.discard) }) %></td>
<td><%= number_to_currency(@production.total_price) %></td>
<td><%= @production.production_products.sum(:pieces_delivered) %></td>
<td><%= @production.production_products.sum(:dirty) %></td>
Expand Down

0 comments on commit 8025ce2

Please sign in to comment.