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

Staging #338

Merged
merged 3 commits into from
Sep 20, 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: 1 addition & 62 deletions app/controllers/productions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def products_in_production_report

def service_order_pdf
@production = Production.find(params[:id])
pdf = generate_service_order_pdf(@production)
pdf = Services::Pdf::ServiceOrderPdfGenerator.new(@production).generate
send_data pdf.render, filename: "service_order_#{@production.service_order_number}.pdf", type: 'application/pdf', disposition: 'inline'
end

Expand Down Expand Up @@ -195,65 +195,4 @@ def generate_products_in_production_csv
end
end
end

def generate_service_order_pdf(production)
Prawn::Document.new do |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"

# Header
pdf.text "Ordem de serviço N° #{production.service_order_number}", size: 16, style: :bold
pdf.move_down 20

# Client and Order Details
pdf.text "Cliente: #{production.tailor.name}", style: :bold
pdf.text "Endereço: Endereço do cliente" # Replace with actual address when available
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 prevista: #{production.expected_delivery_date&.strftime("%d/%m/%Y")}"
pdf.text "Data de conclusão: "
pdf.move_down 20

# Products
pdf.text "Peças", size: 14, style: :bold
pdf.move_down 10

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
end

pdf.move_down 20

# Totals
pdf.text "Total serviços: #{number_to_currency(0)}", align: :right
pdf.text "Total peças: #{number_to_currency(production.total_price)}", align: :right
pdf.text "Total da ordem de serviço: #{number_to_currency(production.total_price)}", style: :bold, align: :right

pdf.move_down 30

# Observations
if production.observation.present?
pdf.text "Observações do Serviço", style: :bold
pdf.text production.observation
pdf.move_down 20
end

# Signature
pdf.text "Concordo com os termos descritos acima."
pdf.move_down 10
pdf.text "Data _____/_____/_____"
pdf.move_down 20
pdf.stroke_horizontal_rule
pdf.move_down 5
pdf.text "Assinatura do responsável"
end
end
end
91 changes: 91 additions & 0 deletions app/models/services/pdf/service_order_pdf_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
require 'prawn'

module Services
module Pdf
class ServiceOrderPdfGenerator
include ActionView::Helpers::NumberHelper

def initialize(production)
@production = production
end

def generate
Prawn::Document.new do |pdf|
setup_font(pdf)
generate_header(pdf)
generate_client_details(pdf)
generate_products_table(pdf)
generate_totals(pdf)
generate_observations(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 serviço N° #{@production.service_order_number}", size: 16, style: :bold
pdf.move_down 20
end

def generate_client_details(pdf)
pdf.text "Cliente: #{@production.tailor.name}", style: :bold
pdf.text "Endereço: Endereço do cliente" # Replace with actual address when available
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 prevista: #{@production.expected_delivery_date&.strftime("%d/%m/%Y")}"
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

@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
end

pdf.move_down 20
end

def generate_totals(pdf)
pdf.text "Total serviços: #{number_to_currency(0)}", align: :right
pdf.text "Total peças: #{number_to_currency(@production.total_price)}", align: :right
pdf.text "Total da ordem de serviço: #{number_to_currency(@production.total_price)}", style: :bold, align: :right
pdf.move_down 30
end

def generate_observations(pdf)
if @production.observation.present?
pdf.text "Observações do Serviço", style: :bold
pdf.text @production.observation
pdf.move_down 20
end
end

def generate_signature(pdf)
pdf.text "Concordo com os termos descritos acima."
pdf.move_down 10
pdf.text "Data _____/_____/_____"
pdf.move_down 20
pdf.stroke_horizontal_rule
pdf.move_down 5
pdf.text "Assinatura do responsável"
end
end
end
end
8 changes: 4 additions & 4 deletions app/views/productions/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@
</div>

<div class="row mb-3">
<!-- <div class="col-md-4">
<%#= form.label :confirmed, model_class.human_attribute_name(:confirmed), class: 'form-check-label' %>
<%#= form.check_box :confirmed, class: 'form-check-input' %>
</div> -->
<div class="col-md-4">
<%= form.label :confirmed, model_class.human_attribute_name(:confirmed), class: 'form-check-label' %>
<%= form.check_box :confirmed, class: 'form-check-input' %>
</div>
<div class="col-md-4">
<%= form.label :paid, model_class.human_attribute_name(:paid), class: 'form-check-label' %>
<%= form.check_box :paid, class: 'form-check-input' %>
Expand Down
26 changes: 21 additions & 5 deletions app/views/productions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<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' %>
</div>

<div class="section-body">
<div class="card">
Expand Down Expand Up @@ -36,8 +39,6 @@
<dd class="col-sm-9"><%= pt_only_date_format(@production.expected_delivery_date) %></dd>
<dt class="col-sm-3"><%= model_class.human_attribute_name(:payment_date) %>:</dt>
<dd class="col-sm-9"><%= pt_only_date_format(@production.payment_date) if @production.payment_date %></dd>
<dt class="col-sm-3"><%= model_class.human_attribute_name(:consider)%>:</dt>
<dd class="col-sm-9"><%= @production.consider ? 'Sim' : 'Não' %></dd>
<dt class="col-sm-3"><%= model_class.human_attribute_name(:confirmed) %>:</dt>
<dd class="col-sm-9"><%= @production.confirmed ? 'Sim' : 'Não' %></dd>
<dt class="col-sm-3"><%= model_class.human_attribute_name(:paid) %>:</dt>
Expand All @@ -49,7 +50,11 @@
<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>
<dt class="col-sm-3"><%= t('productions.show.price_per_piece') %>:</dt>
<dd class="col-sm-9"><%= number_to_currency(@production.price_per_piece) %></dd>
<dd class="col-sm-9">
<% total_cost = @production.notions_cost + @production.fabric_cost + @production.total_price %>
<% total_quantity = @production.production_products.sum(:quantity) %>
<%= number_to_currency(total_cost / total_quantity) if total_quantity > 0 %>
</dd>
</dl>
</div>
</div>
Expand Down Expand Up @@ -94,9 +99,20 @@
</tbody>
<tfoot>
<tr>
<th colspan="3">Total da Produção</th>
<th>Total da Produção</th>
<td><%= @production.production_products.sum(:quantity) %></td>
<td></td>
<td><%= number_to_currency(@production.total_price) %></td>
<td colspan="6"></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.sum { |pp|
pp.quantity - ((pp.pieces_delivered || 0) + (pp.dirty || 0) + (pp.error || 0) + (pp.discard || 0))
} %>
</td>
<td></td>
</tr>
</tfoot>
</table>
Expand Down
2 changes: 1 addition & 1 deletion config/locales/pt-BR.models.productions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pt-BR:
delivery_date: "Data de Entrega"
expected_delivery_date: "Data Prevista de Entrega"
consider: "Considerar"
confirmed: "Confirmado"
confirmed: "Recebimento Confirmado"
paid: "Pago"
observation: "Observação"
pieces_delivered: "Peças Entregues"
Expand Down
Loading