Skip to content

Commit

Permalink
add service order in the productions
Browse files Browse the repository at this point in the history
  • Loading branch information
puppe1990 committed Sep 18, 2024
1 parent 5acb317 commit e7f8241
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,6 @@ gem 'roo-xls'
gem 'activerecord-import'
gem 'appsignal', '>= 3.7.6'

gem 'rubyzip'
gem 'rubyzip'

gem 'prawn'
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -339,10 +339,15 @@ GEM
ast (~> 2.4.1)
racc
path_expander (1.1.1)
pdf-core (0.10.0)
pg (1.5.6)
platform_agent (1.0.1)
activesupport (>= 5.2.0)
useragent (~> 0.16.3)
prawn (2.5.0)
matrix (~> 0.4)
pdf-core (~> 0.10.0)
ttfunk (~> 1.8)
pry (0.14.2)
coderay (~> 1.1)
method_source (~> 1.0)
Expand Down Expand Up @@ -567,6 +572,8 @@ GEM
thread_safe (0.3.6)
tilt (2.3.0)
timeout (0.4.1)
ttfunk (1.8.0)
bigdecimal (~> 3.1)
tty-which (0.4.2)
turbo-rails (2.0.6)
actionpack (>= 6.0.0)
Expand Down Expand Up @@ -649,6 +656,7 @@ DEPENDENCIES
pagy (~> 6.0.0)
pg (~> 1.1)
platform_agent
prawn
pry-byebug
puma (~> 6.4, >= 6.4.2)
pundit
Expand Down
Binary file added app/assets/fonts/DejaVuSans-Bold.ttf
Binary file not shown.
Binary file added app/assets/fonts/DejaVuSans.ttf
Binary file not shown.
70 changes: 70 additions & 0 deletions app/controllers/productions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# app/controllers/productions_controller.rb

require 'prawn'
include ActionView::Helpers::NumberHelper

class ProductionsController < ApplicationController
before_action :set_production, only: [:show, :edit, :update, :destroy, :verify]
before_action :set_tailors, only: [:new, :edit, :create, :update]
Expand Down Expand Up @@ -98,6 +101,12 @@ def products_in_production_report
end
end

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

private

def set_production
Expand Down Expand Up @@ -176,4 +185,65 @@ 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
1 change: 1 addition & 0 deletions app/views/productions/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
<%= link_to t('.destroy', :default => t("helpers.links.destroy")), production_path(@production), :method => 'delete',
:data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) },
:class => 'btn btn-danger' %>
<%= link_to 'Download Service Order PDF', service_order_pdf_production_path(@production), class: 'btn btn-primary', target: '_blank' %>
</div>

<% content_for(:page_title, 'Show Production') %>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
end
member do
patch :verify
get :service_order_pdf
end
end

Expand Down

0 comments on commit e7f8241

Please sign in to comment.