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

add a new report to confirmed productions #349

Merged
merged 2 commits into from
Sep 24, 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
63 changes: 63 additions & 0 deletions app/controllers/productions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ def payment_order_pdf
send_data pdf.render, filename: "payment_order_#{@production.service_order_number}.pdf", type: 'application/pdf', disposition: 'inline'
end

def unpaid_confirmed
@unpaid_confirmed_productions = Production.includes(:tailor, production_products: :product)
.where(confirmed: true, paid: false)
.order(cut_date: :desc, service_order_number: :desc)

@paid_productions = Production.includes(:tailor, production_products: :product)
.where(confirmed: true, paid: true)
.order(payment_date: :desc, service_order_number: :desc)

if params[:tailor_id].present?
@unpaid_confirmed_productions = @unpaid_confirmed_productions.where(tailor_id: params[:tailor_id])
@paid_productions = @paid_productions.where(tailor_id: params[:tailor_id])
end

@tailors_summary = calculate_tailors_summary_unpaid(@unpaid_confirmed_productions)

respond_to do |format|
format.html
format.csv { send_data generate_unpaid_confirmed_csv, filename: "unpaid_confirmed_productions_#{Date.today}.csv" }
end
end

private

def set_production
Expand Down Expand Up @@ -172,6 +194,27 @@ def calculate_tailors_summary(productions)
summary
end

def calculate_tailors_summary_unpaid(productions)
summary = productions.each_with_object({}) do |production, summary|
tailor_id = production.tailor_id
summary[tailor_id] ||= { productions_count: 0, total_value: 0, products: {} }
summary[tailor_id][:productions_count] += 1

production.production_products.each do |pp|
summary[tailor_id][:total_value] += pp.total_price if pp.total_price
summary[tailor_id][:products][pp.product_id] ||= { count: 0, value: 0 }
summary[tailor_id][:products][pp.product_id][:count] += pp.quantity
summary[tailor_id][:products][pp.product_id][:value] += pp.total_price if pp.total_price
end
end

summary.each do |tailor_id, tailor_summary|
tailor_summary[:products] = tailor_summary[:products].sort_by { |_, data| -data[:value] }.to_h
end

summary
end

def generate_tailors_summary_csv
require 'csv'

Expand Down Expand Up @@ -207,4 +250,24 @@ def generate_products_in_production_csv
end
end
end

def generate_unpaid_confirmed_csv
require 'csv'

CSV.generate(headers: true) do |csv|
csv << ['Tailor Name', 'Total Productions', 'Total Value', 'Products']

@tailors_summary.each do |tailor_id, summary|
tailor = Tailor.find(tailor_id)
products_summary = summary[:products].map { |product_id, data| "#{Product.find(product_id).name}: #{data[:count]} (#{number_to_currency(data[:value])})" }.join(', ')

csv << [
tailor.name,
summary[:productions_count],
number_to_currency(summary[:total_value]),
products_summary
]
end
end
end
end
83 changes: 83 additions & 0 deletions app/views/productions/_production_list.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<div class="row mt-3">
<% productions.each do |production| %>
<div class="col-12 mb-4">
<div class="card">
<div class="card-header d-flex justify-content-between align-items-center">
<h5 class="card-title mb-0"><%= t('.service_order_number') %>: <%= production.service_order_number %></h5>
<div>
<%= link_to t('helpers.links.edit'), edit_production_path(production), class: 'btn btn-primary btn-sm mr-2' %>
<%= link_to t('helpers.links.show'), production_path(production), class: 'btn btn-info btn-sm' %>
</div>
</div>
<div class="card-body">
<div class="row mb-3">
<div class="col-md-4">
<p><strong><%= Production.human_attribute_name(:tailor) %>:</strong> <%= production.tailor.name %></p>
<p><strong><%= Production.human_attribute_name(:cut_date) %>:</strong> <%= l(production.cut_date, format: :long) if production.cut_date %></p>
<p><strong><%= Production.human_attribute_name(:expected_delivery_date) %>:</strong> <%= l(production.expected_delivery_date, format: :long) if production.expected_delivery_date %></p>
<p><strong><%= Production.human_attribute_name(:payment_date) %>:</strong> <%= l(production.payment_date, format: :long) if production.payment_date %></p>
</div>
</div>
<h6 class="mt-4"><%= t('.products') %>:</h6>
<div class="table-responsive">
<table class="table table-sm">
<thead>
<tr>
<th><%= t('.product') %></th>
<th><%= t('.quantity') %></th>
<th><%= t('.pieces_delivered') %></th>
<th><%= t('.unit_price') %></th>
<th><%= t('.dirty') %></th>
<th><%= t('.error') %></th>
<th><%= t('.discard') %></th>
<th><%= t('.returned') %></th>
<th><%= t('.discount') %></th>
<th><%= t('.total') %></th>
</tr>
</thead>
<tbody>
<% total_all_rows = 0 %>
<% total_discount = 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 = unit_price * (pp.dirty + pp.error + pp.discard) %>
<% returned_discount = pp.returned ? total_price : 0 %>
<% total_discount_row = discount + returned_discount %>
<% adjusted_price = unit_price * adjusted_quantity - total_discount_row %>

<% total_all_rows += total_price %>
<% total_discount += total_discount_row %>

<tr>
<td><%= pp.product.name %></td>
<td><%= pp.quantity %></td>
<td><%= pp.pieces_delivered %></td>
<td><%= number_to_currency(unit_price) %></td>
<td><%= pp.dirty %></td>
<td><%= pp.error %></td>
<td><%= pp.discard %></td>
<td><%= pp.returned ? t('yes') : t('no') %></td>
<td><%= number_to_currency(total_discount_row) %></td>
<td><%= number_to_currency(adjusted_price) %></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<th colspan="9" class="text-right"><%= t('.total_discount') %>:</th>
<th><%= number_to_currency(total_discount) %></th>
</tr>
<tr>
<th colspan="9" class="text-right"><%= t('.total_to_pay') %>:</th>
<th><%= number_to_currency(total_all_rows - total_discount) %></th>
</tr>
</tfoot>
</table>
</div>
</div>
</div>
</div>
<% end %>
</div>
75 changes: 75 additions & 0 deletions app/views/productions/unpaid_confirmed.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<br><br>
<div class="container-fluid">
<h1 class="mb-4"><%= t('productions.unpaid_confirmed.title') %></h1>

<%= form_tag unpaid_confirmed_productions_path, method: :get, class: 'mb-4' do %>
<div class="form-group">
<%= label_tag :tailor_id, t('.filter_by_tailor') %>
<%= select_tag :tailor_id,
options_from_collection_for_select(Tailor.all, :id, :name, params[:tailor_id]),
include_blank: t('.all_tailors'),
class: 'form-control' %>
</div>
<%= submit_tag t('productions.unpaid_confirmed.apply_filter'), class: 'btn btn-primary' %>
<% end %>

<%= link_to t('.download_csv'), unpaid_confirmed_productions_path(format: :csv, tailor_id: params[:tailor_id]), class: 'btn btn-secondary mb-3' %>

<ul class="nav nav-tabs" id="myTab" role="tablist">
<li class="nav-item" role="presentation">
<a class="nav-link active" id="unpaid-tab" data-toggle="tab" href="#unpaid" role="tab" aria-controls="unpaid" aria-selected="true">Unpaid Productions</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="paid-tab" data-toggle="tab" href="#paid" role="tab" aria-controls="paid" aria-selected="false">Paid Productions</a>
</li>
<li class="nav-item" role="presentation">
<a class="nav-link" id="tailors-tab" data-toggle="tab" href="#tailors" role="tab" aria-controls="tailors" aria-selected="false">Tailors Summary</a>
</li>
</ul>

<div class="tab-content" id="myTabContent">
<div class="tab-pane fade show active" id="unpaid" role="tabpanel" aria-labelledby="unpaid-tab">
<%= render 'production_list', productions: @unpaid_confirmed_productions %>
</div>

<div class="tab-pane fade" id="paid" role="tabpanel" aria-labelledby="paid-tab">
<%= render 'production_list', productions: @paid_productions %>
</div>

<div class="tab-pane fade" id="tailors" role="tabpanel" aria-labelledby="tailors-tab">
<div class="row mt-3">
<% @tailors_summary.each do |tailor_id, summary| %>
<div class="col-md-4 mb-4">
<div class="card">
<div class="card-header">
<h5 class="card-title"><%= Tailor.find(tailor_id).name %></h5>
</div>
<div class="card-body">
<p><strong><%= t('.total_productions') %>:</strong> <%= summary[:productions_count] %></p>
<p><strong><%= t('.total_value') %>:</strong> <%= number_to_currency(summary[:total_value]) %></p>
<h6 class="mt-3"><%= t('.products') %>:</h6>
<ul>
<% summary[:products].each do |product_id, data| %>
<li><%= Product.find(product_id).name %>: <%= data[:count] %> (<%= number_to_currency(data[:value]) %>)</li>
<% end %>
</ul>
</div>
</div>
</div>
<% end %>
</div>
</div>
</div>
</div>

<%= javascript_include_tag 'https://code.jquery.com/jquery-3.5.1.slim.min.js' %>
<%= javascript_include_tag 'https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.bundle.min.js' %>

<script>
$(document).ready(function() {
$('#myTab a').on('click', function (e) {
e.preventDefault()
$(this).tab('show')
})
});
</script>
3 changes: 3 additions & 0 deletions app/views/shared/_sidebar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@
<li class="<%= active_nav_item('productions', 'missing_pieces') %>">
<%= link_to t("productions.side_missing_pieces"), missing_pieces_productions_path, class: 'nav-link' %>
</li>
<li class="<%= active_nav_item('productions', 'unpaid_confirmed') %>">
<%= link_to t("productions.unpaid_confirmed.title"), unpaid_confirmed_productions_path, class: 'nav-link' %>
</li>
<li class="<%= active_nav_item('productions', 'products_in_production_report') %>">
<%= link_to t("productions.products_in_production_report"), products_in_production_report_productions_path, class: 'nav-link' %>
</li>
Expand Down
26 changes: 26 additions & 0 deletions config/locales/pt-BR.models.productions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,29 @@ pt-BR:
edit: "Editar %{model}"
actions:
remove: "Remover"
productions:
unpaid_confirmed:
title: "Cortes Concluídos"
filter_by_tailor: "Filtrar por Costureiro"
all_tailors: "Todos os Costureiros"
apply_filter: "Aplicar Filtro"
download_csv: "Baixar CSV"
total_productions: "Total de Produções"
total_value: "Valor Total"
products: "Produtos"
production_list:
service_order_number: "Número da Ordem de Serviço"
product: "Produto"
quantity: "Quantidade"
pieces_delivered: "Peças Entregues"
unit_price: "Preço Un."
dirty: "Sujo"
error: "Erro"
discard: "Descarte"
returned: "Devolvido"
discount: "Desconto"
total: "Total"
total_discount: "Total de Desconto"
total_to_pay: "Total a Pagar"
yes: "Sim"
no: "Não"
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
resources :productions do
collection do
get 'missing_pieces'
get :products_in_production_report
get 'products_in_production_report'
get 'unpaid_confirmed' # Add this line
end
member do
patch :verify
Expand Down
Loading