From c978bbfc376fb64a60b8416d5f40f786ee7a971e Mon Sep 17 00:00:00 2001 From: Nicolas Bally Date: Tue, 20 Sep 2016 22:33:03 +0200 Subject: [PATCH] pdfs --- Gemfile | 3 + Gemfile.lock | 2 + app/controllers/admin/documents_controller.rb | 18 +++- app/controllers/application_controller.rb | 10 ++- .../public/documents_controller.rb | 82 ++++++++++++++++--- app/models/document.rb | 2 + .../admin/accepted_offers/show.html.haml | 8 +- app/views/admin/customers/_form.html.haml | 2 + app/views/admin/documents/_document.html.haml | 0 app/views/admin/documents/_form.html.haml | 14 ++++ app/views/admin/documents/edit.js.erb | 1 + app/views/admin/documents/update.js.erb | 3 + .../public/documents/particulars.html.haml | 28 +++++++ config/deploy.rb | 5 +- config/initializers/wicked_pdf.rb | 4 + config/routes.rb | 11 ++- ...0920185645_add_particulars_to_documents.rb | 7 ++ db/schema.rb | 4 +- 18 files changed, 181 insertions(+), 23 deletions(-) create mode 100644 app/views/admin/documents/_document.html.haml create mode 100755 app/views/admin/documents/_form.html.haml create mode 100755 app/views/admin/documents/edit.js.erb create mode 100755 app/views/admin/documents/update.js.erb create mode 100644 app/views/public/documents/particulars.html.haml create mode 100644 db/migrate/20160920185645_add_particulars_to_documents.rb diff --git a/Gemfile b/Gemfile index 24504fa..d19b77c 100755 --- a/Gemfile +++ b/Gemfile @@ -73,3 +73,6 @@ gem "paranoia", "~> 2.0" gem 'workflow', '~> 1.2.0' gem 'sidekiq' + + +gem "posix-spawn" \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 91d7bbd..342955c 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -132,6 +132,7 @@ GEM mini_portile (~> 0.6.0) paranoia (2.1.4) activerecord (~> 4.0) + posix-spawn (0.3.11) rack (1.6.0) rack-test (0.6.3) rack (>= 1.0) @@ -247,6 +248,7 @@ DEPENDENCIES mysql2 net-ssh (~> 2.7.0) paranoia (~> 2.0) + posix-spawn rails (= 4.2.0) rmagick rvm-capistrano (= 1.4.1) diff --git a/app/controllers/admin/documents_controller.rb b/app/controllers/admin/documents_controller.rb index c2dde86..154ff13 100755 --- a/app/controllers/admin/documents_controller.rb +++ b/app/controllers/admin/documents_controller.rb @@ -1,7 +1,23 @@ class Admin::DocumentsController < ApplicationController layout "admin" before_filter :auth_admin - before_filter :load_offer + before_filter :load_offer, :except => [:edit, :update] + + def edit + @document = Document.find(params[:id]) + end + + def update + @document = Document.find(params[:id]) + + if @document.update_attributes(params.require(:document).permit!) + + else + render :action => "edit" + end + + + end def create diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 7e37a4d..84f6f28 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -5,7 +5,7 @@ class ApplicationController < ActionController::Base def check_enabled - if !@current_customer.account_validated? + if !(!@current_customer and current_admin and params[:admin]) or (@current_customer and !@current_customer.account_validated?) redirect_to public_my_account_path end end @@ -13,12 +13,14 @@ class ApplicationController < ActionController::Base def auth_customer session[:devise_id] = params[:d] if params[:d] - if !current_customer + if !current_customer and !(current_admin and params[:admin]) session[:before_auth_url] = request.url redirect_to new_public_customers_auth_path(:p => params[:p], :for_annonce => (true if params[:controller] == "public/annonces")) else - @current_customer.last_activity = DateTime.now - @current_customer.save + if !(current_admin and params[:admin]) + @current_customer.last_activity = DateTime.now + @current_customer.save + end end end diff --git a/app/controllers/public/documents_controller.rb b/app/controllers/public/documents_controller.rb index 403fae0..dc59024 100644 --- a/app/controllers/public/documents_controller.rb +++ b/app/controllers/public/documents_controller.rb @@ -13,17 +13,73 @@ class Public::DocumentsController < ApplicationController end def download - @document = @accepted_offer.documents.find(params[:id]) - if @document.state == 'document_available' - @document.state = :document_downloaded - @document.save + 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 + admins = Admin.where.not(email: nil) + admins.each do |admin| + AdminMailer.customer_download_document(admin, @document, current_customer).deliver + end end end - send_file @document.document.file.path + 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 @@ -73,11 +129,13 @@ class Public::DocumentsController < ApplicationController end def check_owner - @accepted_offer = AcceptedOffer.find(params[:accepted_offer_id]) + 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 + 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 diff --git a/app/models/document.rb b/app/models/document.rb index a71fb01..16b8102 100755 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -2,6 +2,8 @@ class Document < ActiveRecord::Base include Workflow belongs_to :accepted_offer + + has_one :customer, :through => :accepted_offer mount_uploader :document, DocumentUploader mount_uploader :returned_document, DocumentUploader diff --git a/app/views/admin/accepted_offers/show.html.haml b/app/views/admin/accepted_offers/show.html.haml index 9c64d20..eb26b8d 100755 --- a/app/views/admin/accepted_offers/show.html.haml +++ b/app/views/admin/accepted_offers/show.html.haml @@ -45,7 +45,7 @@ = file_field_tag :document = submit_tag("Charger" , class:"btn btn-primary") -else - =link_to i(:"download"), admin_offer_accepted_offer_document_download_path(@offer, @accepted_offer, document),title: "Télécharger le document" + =link_to i(:"download"), download_public_document_path(document, :admin => true),title: "Télécharger le document" -if !document.document_verified? =link_to i(:"remove"), admin_offer_accepted_offer_document_delete_path(@offer, @accepted_offer, document), title: "Supprimer le fichier chargé", :data => {:confirm => 'Voulez-vous vraiment supprimer le fichier chargé ?'} @@ -58,7 +58,7 @@ = file_field_tag :returned_document = submit_tag("Charger manuellement" , class:"btn btn-primary") -else - =link_to i(:"download"), admin_offer_accepted_offer_document_download_returned_path(@offer, @accepted_offer, document),title: "Télécharger le document" + =link_to i(:"download"), admin_offer_accepted_offer_document_download_returned_path(@offer, @accepted_offer, document, :admin => true),title: "Télécharger le document" -if !document.document_verified? =link_to i(:"remove"), admin_offer_accepted_offer_document_delete_returned_path(@offer, @accepted_offer, document), title: "Supprimer le fichier chargé", :data => {:confirm => 'Voulez-vous vraiment supprimer le fichier chargé ?'} -if document.document_returned? @@ -71,7 +71,9 @@ %td{style:"text-align:right"} -if !document.document_verified? =link_to i(:"check"), admin_offer_accepted_offer_document_force_verified_path(@offer, @accepted_offer, document), title: "Marquer ce document comme vérifié", :data => {:confirm => 'Voulez-vous vraiment marquer ce document comme vérifié ?'} - + + =link_to i(:"pencil"), edit_admin_document_path(document), :remote => true + =link_to i(:"trash"), admin_offer_accepted_offer_document_destroy_path(@offer, @accepted_offer, document), title: "Supprimer le document", :data => {:confirm => 'Voulez-vous vraiment supprimer ce document ?'} -if @accepted_offer.waiting_documents? && @accepted_offer.documents.where(state: :document_verified).count == @accepted_offer.documents.count diff --git a/app/views/admin/customers/_form.html.haml b/app/views/admin/customers/_form.html.haml index 755f66d..ee817d2 100755 --- a/app/views/admin/customers/_form.html.haml +++ b/app/views/admin/customers/_form.html.haml @@ -31,6 +31,8 @@ =f.input :need_2, :label => "2ième besoin : ",:as => :select, :collection => [ "Materiel professionnel", "Matériel informatique", "Achat de véhicules", "Energie"] =f.input :need_3, :label => "3ième besoin : ", :placeholder => "Autre, précisez ",:rows => 5, :input_html => {:style => "height:100px;"} + =f.input :particulars_text, :label => "En-tête contrats : ",:rows => 5, :input_html => {:style => "height:100px;"} + diff --git a/app/views/admin/documents/_document.html.haml b/app/views/admin/documents/_document.html.haml new file mode 100644 index 0000000..e69de29 diff --git a/app/views/admin/documents/_form.html.haml b/app/views/admin/documents/_form.html.haml new file mode 100755 index 0000000..31440ab --- /dev/null +++ b/app/views/admin/documents/_form.html.haml @@ -0,0 +1,14 @@ + + + +=semantic_form_for [:admin, @document], :remote => true do |f| + .content + + + = f.inputs do + + = f.input :particulars, :label => "Avec impression coordonnées ?" + + + .actions= f.submit "Sauvegarder", :class => "btn btn-primary" + diff --git a/app/views/admin/documents/edit.js.erb b/app/views/admin/documents/edit.js.erb new file mode 100755 index 0000000..6c8f015 --- /dev/null +++ b/app/views/admin/documents/edit.js.erb @@ -0,0 +1 @@ +show_pane_hover("<%= escape_javascript(render(:partial => "form"))%>",700,900); \ No newline at end of file diff --git a/app/views/admin/documents/update.js.erb b/app/views/admin/documents/update.js.erb new file mode 100755 index 0000000..679dba7 --- /dev/null +++ b/app/views/admin/documents/update.js.erb @@ -0,0 +1,3 @@ +//$('#annonce_cat_index').replaceWith("<%= escape_javascript(render(@document)) %>"); + +close_pane_hover(); diff --git a/app/views/public/documents/particulars.html.haml b/app/views/public/documents/particulars.html.haml new file mode 100644 index 0000000..7bb3b36 --- /dev/null +++ b/app/views/public/documents/particulars.html.haml @@ -0,0 +1,28 @@ +!!! +%html + %head + + %meta{:"http-equiv" => "Content-Type", :content=>"text/html; charset=UTF-8"} + %meta{:name=>"viewport", :content=>"width=device-width,initial-scale=1"} + :css + #div{ + position:absolute;top:9.3cm;left:1.4cm;right:1.4cm;border:1px solid red;font-family:serif; + + } + p{ + margin:0; + } + + + + + + + + + + %body + + + #div + =simple_format @document.customer.particulars_text \ No newline at end of file diff --git a/config/deploy.rb b/config/deploy.rb index be8ef75..4fdf3a8 100755 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -71,7 +71,10 @@ set :application, 'negos_app' run "ln -s #{deploy_to}shared/storage/public #{current_path}/public/public_medias" - + + run "ln -s #{deploy_to}shared/pdf #{current_path}/pdf" + + run "ln -s #{deploy_to}shared/production.sqlite3 #{current_path}/db/production.sqlite3" sudo "cp #{current_path}/config/unicorn_init_d /etc/init.d/#{application}" diff --git a/config/initializers/wicked_pdf.rb b/config/initializers/wicked_pdf.rb index e69de29..fa3ea05 100755 --- a/config/initializers/wicked_pdf.rb +++ b/config/initializers/wicked_pdf.rb @@ -0,0 +1,4 @@ + +WickedPdf.config = { + :exe_path => "/usr/local/bin/wkhtmltopdf", +} diff --git a/config/routes.rb b/config/routes.rb index f1133fe..4d1f930 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,13 @@ Rails.application.routes.draw do get 'p/:mlm_token' => "public/customers#parrainage", :as => :mlm_token namespace :public do - + resources :documents do + member do + get :download + end + end + + resources :customer_favs resources :customer_ribs do @@ -224,6 +230,9 @@ Rails.application.routes.draw do end namespace :admin do + + resources :documents + resources :domains resources :annonces do diff --git a/db/migrate/20160920185645_add_particulars_to_documents.rb b/db/migrate/20160920185645_add_particulars_to_documents.rb new file mode 100644 index 0000000..8f1c135 --- /dev/null +++ b/db/migrate/20160920185645_add_particulars_to_documents.rb @@ -0,0 +1,7 @@ +class AddParticularsToDocuments < ActiveRecord::Migration + def change + add_column :documents, :particulars, :boolean + + add_column :customers, :particulars_text, :text + end +end diff --git a/db/schema.rb b/db/schema.rb index c181a0d..e583399 100755 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160530213935) do +ActiveRecord::Schema.define(version: 20160920185645) do create_table "accepted_offers", force: :cascade do |t| t.datetime "created_at", null: false @@ -224,6 +224,7 @@ ActiveRecord::Schema.define(version: 20160530213935) do t.float "latitude", limit: 24 t.float "longitude", limit: 24 t.datetime "last_activity" + t.text "particulars_text", limit: 65535 end create_table "data_files", force: :cascade do |t| @@ -246,6 +247,7 @@ ActiveRecord::Schema.define(version: 20160530213935) do t.string "returned_document", limit: 255 t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.boolean "particulars", limit: 1 end create_table "domain_customers", force: :cascade do |t|