negos_app/app/controllers/public/documents_controller.rb
Nicolas Bally c978bbfc37 pdfs
2016-09-20 22:33:03 +02:00

142 lines
4.2 KiB
Ruby

class Public::DocumentsController < ApplicationController
layout "public"
before_filter :auth_customer
before_filter :check_enabled
before_filter :check_owner
def index
@documents = @accepted_offer.documents
end
def download
if !params[:admin]
@document = @accepted_offer.documents.find(params[:id])
else
@document = Document.find(params[:id])
end
if !params[:admin]
if @document.state == 'document_available'
@document.state = :document_downloaded
@document.save
admins = Admin.where.not(email: nil)
admins.each do |admin|
AdminMailer.customer_download_document(admin, @document, current_customer).deliver
end
end
end
if @document.particulars
@temp_file = "#{Rails.root}/pdf/documents/#{@document.id}_temp.pdf"
@final_file = "#{Rails.root}/pdf/documents/#{@document.id}.pdf"
@final_file2 = "#{Rails.root}/pdf/documents/#{@document.id}-2.pdf"
view = ActionView::Base.new(Rails.root.join('app/views'))
view.class.include ApplicationHelper
view.class.include Rails.application.routes.url_helpers
pdf = view.render(
:pdf => "#{@document.id}",
:template => "public/documents/particulars.html.haml",
:locals => {:@document => @document})
# then save to a file
pdf = WickedPdf.new.pdf_from_string(pdf, :margin => { top: 0, # default 10 (mm)
bottom: 0,
left: 0,
right: 0 })
save_path = @temp_file
File.open(save_path, 'wb') do |file|
file << pdf
end
require 'posix/spawn'
::POSIX::Spawn::Child.new 'pdftk', @document.document.file.path, 'background',@temp_file , 'output', @final_file
::POSIX::Spawn::Child.new 'pdftk', "A="+@document.document.file.path, 'B='+@final_file ,"cat", "B1", "A2-end", 'output', @final_file2
#pdftk A=/Users/nico/Dev/negos_app/pdf_stamp/contrat.pdf B=/Users/nico/Dev/negos_app/pdf/documents/3.pdf cat B1 A2-end output fichier-final.pdf
@data_to_send = File.open( @final_file2).read
send_data @data_to_send, :filename =>"negos-document-#{@document.id}.pdf" , :type => 'application/pdf',:disposition => (params[:inline] ? 'inline' : "attachment")
#render :inline => "j"
else
send_file @document.document.file.path
end
end
def download_returned
@document = @accepted_offer.documents.find(params[:id])
send_file @document.returned_document.file.path
end
def destroy_returned
@document = @accepted_offer.documents.find(params[:id])
if @document.returned_document?
@document.remove_returned_document!
end
@document.state = :document_downloaded
if @document.save
flash[:success] = "Document chargé"
else
flash[:success] = "Impossible de supprimer le document"
end
redirect_to :back
end
def upload_returned
if !params[:returned_document]
flash[:error] = "Vous devez sélectionner un fichier"
else
@document = @accepted_offer.documents.find(params[:id])
@document.returned_document = params[:returned_document]
if @document.save
@document.state = :document_returned
@document.save
admins = Admin.where.not(email: nil)
admins.each do |admin|
AdminMailer.customer_upload_document(admin, @document, current_customer).deliver
end
flash[:success] = "Document chargé"
else
flash[:error] = "Impossible de charger le document"
end
end
redirect_to :back
end
def check_owner
if !params[:admin]
@accepted_offer = AcceptedOffer.find(params[:accepted_offer_id])
if @accepted_offer.customer.id != current_customer.id
flash[:error] = "Vous n'avez pas la permission d'accéder à cette page"
redirect_back_or_default :root
end
end
end
end