Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update the show and fix the total_quantity #380

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 43 additions & 24 deletions app/models/services/pdf/payment_order_pdf_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,45 +54,61 @@ def generate_products_table(pdf)

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

total_all_rows = 0
total_quantity = 0
total_pieces_delivered = 0
total_dirty = 0
total_error = 0
total_discard = 0
total_returned = 0
total_discount = 0
total_price = 0

@production.production_products.each do |pp|
unit_price = pp.unit_price || 0
total_price = pp.total_price || 0
adjusted_quantity = pp.pieces_delivered - (pp.dirty + pp.error + pp.discard)
discount = pp.returned ? 0 : unit_price * (pp.dirty + pp.error + pp.discard)
adjusted_price = pp.returned ? 0 : (unit_price * adjusted_quantity - discount)

total_all_rows += total_price
quantity = pp.quantity || 0
pieces_delivered = pp.pieces_delivered || 0
dirty = pp.dirty || 0
error = pp.error || 0
discard = pp.discard || 0

discount = pp.returned ? 0 : unit_price * (dirty + error + discard)
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_returned += pp.returned ? 1 : 0
total_discount += discount
total_price += row_total

data << [
pp.product.name,
pp.quantity,
pp.pieces_delivered,
quantity,
pieces_delivered,
number_to_currency(unit_price),
pp.dirty,
pp.error,
pp.discard,
dirty,
error,
discard,
pp.returned ? 'Sim' : 'Não',
pp.returned ? '-' : number_to_currency(discount),
pp.returned ? '-' : number_to_currency(adjusted_price)
pp.returned ? '-' : number_to_currency(row_total)
]
end

# Add a row for totals
data << [
"Total",
@production.production_products.sum(:quantity),
@production.production_products.sum(:pieces_delivered),
total_quantity,
total_pieces_delivered,
"",
@production.production_products.sum(:dirty),
@production.production_products.sum(:error),
@production.production_products.sum(:discard),
@production.production_products.where(returned: true).count,
total_dirty,
total_error,
total_discard,
total_returned,
number_to_currency(total_discount),
number_to_currency(total_all_rows - total_discount)
number_to_currency(total_price)
]

column_widths = [100, 50, 50, 50, 40, 40, 40, 50, 60, 70]
Expand Down Expand Up @@ -157,16 +173,19 @@ def generate_products_table(pdf)

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

total_returned = @production.production_products.sum do |pp|
pp.returned ? (pp.total_price || 0) : 0
pp.returned ? (pp.quantity * (pp.unit_price || 0)) : 0
end

total_price = @production.production_products.sum { |pp| pp.total_price || 0 }
total_price = @production.production_products.sum do |pp|
pp.quantity * (pp.unit_price || 0)
end

total_pieces_delivered_price = @production.production_products.sum do |pp|
next 0 if pp.returned
(pp.pieces_delivered || 0) * (pp.unit_price || 0)
end

Expand All @@ -191,4 +210,4 @@ def generate_signature(pdf)
end
end
end
end
end
113 changes: 61 additions & 52 deletions app/views/productions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -71,72 +71,72 @@
<tr>
<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>Preço Unitário</th>
<th>Sujo</th>
<th>Erro</th>
<th>Descarte</th>
<th>Quantidade Devolvida</th>
<th>Peças Faltantes</th>
<th>Data de Entrega</th>
<th>Devolvido</th>
<th>Desconto</th>
<th>Total</th>
</tr>
</thead>
<tbody>
<% total_quantity = 0 %>
<% total_pieces_delivered = 0 %>
<% total_dirty = 0 %>
<% total_error = 0 %>
<% total_discard = 0 %>
<% total_returned = 0 %>
<% total_discount = 0 %>
<% total_price = 0 %>

<% @production.production_products.each do |pp| %>
<% unit_price = pp.unit_price || 0 %>
<% quantity = pp.quantity || 0 %>
<% pieces_delivered = pp.pieces_delivered || 0 %>
<% dirty = pp.dirty || 0 %>
<% error = pp.error || 0 %>
<% discard = pp.discard || 0 %>

<% discount = pp.returned ? 0 : unit_price * (dirty + error + discard) %>
<% 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_returned += pp.returned ? 1 : 0 %>
<% total_discount += discount %>
<% total_price += row_total %>

<tr>
<td><%= pp.product&.name || 'Nenhum produto atribuído' %></td>
<td><%= pp.quantity %></td>
<td><%= number_to_currency(pp.unit_price) %></td>
<td>
<% if pp.unit_price %>
<%= number_to_currency(pp.unit_price * ((pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))) %>
<% else %>
N/A
<% end %>
<% if pp.returned %>
<br>
<%= number_to_currency(pp.total_price) %>
<% end %>
</td>
<td><%= number_to_currency(pp.total_price) %></td>
<td><%= pp.pieces_delivered || 0 %></td>
<td><%= pp.dirty || 0 %></td>
<td><%= pp.error || 0 %></td>
<td><%= pp.discard || 0 %></td>
<td><%= pp.returned ? pp.quantity : 0 %></td>
<td>
<%= pp.quantity - ((pp.pieces_delivered || 0) + (pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0)) %>
</td>
<td><%= pt_only_date_format(pp.delivery_date) || 'Não informado' %></td>
<td><%= pp.returned ? 'Yes' : 'No' %></td>
<td><%= quantity %></td>
<td><%= pieces_delivered %></td>
<td><%= number_to_currency(unit_price) %></td>
<td><%= dirty %></td>
<td><%= error %></td>
<td><%= discard %></td>
<td><%= pp.returned ? 'Sim' : 'Não' %></td>
<td><%= pp.returned ? '-' : number_to_currency(discount) %></td>
<td><%= pp.returned ? '-' : number_to_currency(row_total) %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<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 || 0) * (pp.dirty + pp.error + pp.discard) }) %>
<br>
<%= number_to_currency(@production.production_products.where(returned: true).sum(:total_price)) %>
</td>
<td><%= number_to_currency(@production.total_price) %></td>
<td><%= @production.production_products.sum(:pieces_delivered) %></td>
<td><%= @production.production_products.sum(:dirty) %></td>
<td><%= @production.production_products.sum(:error) %></td>
<td><%= @production.production_products.sum(:discard) %></td>
<td><%= @production.production_products.where(returned: true).sum(:quantity) %></td>
<td>
<%= @production.production_products.sum { |pp|
pp.quantity - ((pp.pieces_delivered || 0) + (pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))
} %>
</td>
<td><%= total_quantity %></td>
<td><%= total_pieces_delivered %></td>
<td></td>
<td><%= total_dirty %></td>
<td><%= total_error %></td>
<td><%= total_discard %></td>
<td><%= total_returned %></td>
<td><%= number_to_currency(total_discount) %></td>
<td><%= number_to_currency(total_price) %></td>
</tr>
</tfoot>
</table>
Expand All @@ -163,16 +163,24 @@
</tr>
<% end %>
</tbody>
<tfoot>
</table>

<table class="table table-striped mt-4">
<tbody>
<tr>
<th><%= t('.total_paid') %></th>
<td><%= number_to_currency(@production.total_paid) %></td>
<td><%= number_to_currency(@production.payments.sum(:amount)) %></td>
</tr>
<tr>
<th><%= t('.remaining_balance') %></th>
<td><%= number_to_currency(@production.remaining_balance) %></td>
<td>
<% total_price = @production.production_products.sum { |pp| pp.returned ? 0 : (pp.pieces_delivered || 0) * (pp.unit_price || 0) } %>
<% total_discount = @production.production_products.sum { |pp| (pp.unit_price || 0) * ((pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0)) } %>
<% remaining_balance = (total_price - total_discount) - @production.payments.sum(:amount) %>
<%= number_to_currency(remaining_balance) %>
</td>
</tr>
</tfoot>
</tbody>
</table>
</div>
</div>
Expand All @@ -190,3 +198,4 @@
<% content_for(:page_title, 'Show Production') %>



1 change: 1 addition & 0 deletions config/locales/pt-BR.models.productions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ pt-BR:
discard: "Descarte"
unit_price: "Preço Unitário"
total_price: "Preço Total"
returned: "Devolvido"
payment:
amount: "Valor"
payment_date: "Data do Pagamento"
Expand Down
Loading