diff --git a/Gemfile b/Gemfile index 3b45a687c..547cd6b67 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,9 @@ gem 'uuidtools', '~> 2.1.4' # For unique IDs (used by the epp gem) # for importing legacy db gem 'activerecord-import', '~> 0.7.0' # for inserting dummy data +# for generating pdf +gem 'pdfkit', '~> 0.6.2' + group :development do # dev tools gem 'spring', '~> 1.3.3' diff --git a/Gemfile.lock b/Gemfile.lock index 74de822f1..d1a2d1332 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -272,6 +272,7 @@ GEM orm_adapter (0.5.0) parser (2.2.0.3) ast (>= 1.1, < 3.0) + pdfkit (0.6.2) pg (0.18.1) phantomjs (1.9.7.1) phantomjs-binaries (1.9.2.4) @@ -521,6 +522,7 @@ DEPENDENCIES nokogiri (>= 1.6.2.2) nprogress-rails (~> 0.1.6.5) paper_trail! + pdfkit (~> 0.6.2) pg (~> 0.18.0) phantomjs (~> 1.9.7.1) phantomjs-binaries (~> 1.9.2.4) diff --git a/app/assets/images/eis-logo-black-et.png b/app/assets/images/eis-logo-black-et.png new file mode 100644 index 000000000..e65889385 Binary files /dev/null and b/app/assets/images/eis-logo-black-et.png differ diff --git a/app/assets/stylesheets/shared/general.sass b/app/assets/stylesheets/shared/general.sass index 40e1cdff2..c703c1f0b 100644 --- a/app/assets/stylesheets/shared/general.sass +++ b/app/assets/stylesheets/shared/general.sass @@ -36,3 +36,6 @@ box-sizing: border-box padding: 10px font-size: 16px + +.no-border + border: 0px !important diff --git a/app/assets/stylesheets/shared/pdf.sass b/app/assets/stylesheets/shared/pdf.sass new file mode 100644 index 000000000..1551c9285 --- /dev/null +++ b/app/assets/stylesheets/shared/pdf.sass @@ -0,0 +1,16 @@ +@import "bootstrap-sprockets" +@import "bootstrap" + +#logo + text-align: right + +.no-border + border: 0px !important + +body, html, .container + height: 100% + +#footer + position: absolute + bottom: 0 + width: 100% diff --git a/app/controllers/registrar/invoices_controller.rb b/app/controllers/registrar/invoices_controller.rb index fd9ad77da..8a8e37058 100644 --- a/app/controllers/registrar/invoices_controller.rb +++ b/app/controllers/registrar/invoices_controller.rb @@ -1,7 +1,7 @@ class Registrar::InvoicesController < RegistrarController load_and_authorize_resource - before_action :set_invoice, only: [:show] + before_action :set_invoice, only: [:show, :forward, :download_pdf] def index invoices = current_user.registrar.invoices.includes(:invoice_items, :account_activity) @@ -13,6 +13,30 @@ class Registrar::InvoicesController < RegistrarController def show end + def forward + @invoice.billing_email = @invoice.buyer.billing_email + + return unless request.post? + + @invoice.billing_email = params[:invoice][:billing_email] + + if @invoice.forward + flash[:notice] = t('invoice_forwared') + redirect_to([:registrar, @invoice]) + else + flash.now[:alert] = t('failed_to_forward_invoice') + end + end + + def download_pdf + # render 'pdf', layout: false + + kit = PDFKit.new(render_to_string('pdf', layout: false)) + pdf = kit.to_pdf + + send_data pdf, filename: "test.pdf" + end + private def set_invoice diff --git a/app/models/invoice.rb b/app/models/invoice.rb index e50c096ab..002d124d3 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -7,6 +7,9 @@ class Invoice < ActiveRecord::Base accepts_nested_attributes_for :invoice_items + attr_accessor :billing_email + validates :billing_email, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i } + validates :invoice_type, :due_date, :currency, :seller_name, :seller_iban, :buyer_name, :invoice_items, :vat_prc, presence: true @@ -38,6 +41,14 @@ class Invoice < ActiveRecord::Base Country.new(buyer_country_code) end + def forward + return false unless valid? + return false unless billing_email + + # TODO: forward invoice + true + end + def items invoice_items end diff --git a/app/views/registrar/invoices/forward.haml b/app/views/registrar/invoices/forward.haml new file mode 100644 index 000000000..e32f81628 --- /dev/null +++ b/app/views/registrar/invoices/forward.haml @@ -0,0 +1,15 @@ +- content_for :actions do + = link_to(t(:back_to_invoice), registrar_invoice_path(@invoice), class: 'btn btn-default') += render 'shared/title', name: t(:forward_invoice) + += form_for([:registrar, @invoice], url: { action: :forward }, method: :post) do |f| + .row + .col-md-4.col-md-offset-4 + = render 'shared/full_errors', object: @invoice + .form-group + = f.label :billing_email + = f.text_field :billing_email, class: 'form-control', autocomplete: 'off' + + .row + .col-md-12.text-right + = button_tag(t(:forward), class: 'btn btn-primary') diff --git a/app/views/registrar/invoices/pdf.haml b/app/views/registrar/invoices/pdf.haml new file mode 100644 index 000000000..3b7a11c37 --- /dev/null +++ b/app/views/registrar/invoices/pdf.haml @@ -0,0 +1,203 @@ +%html{lang: I18n.locale.to_s} + %head + %meta{charset: "utf-8"} + :css + .container { + margin: auto; + font-size: 12px; + } + .col-md-12 { + + } + + .col-md-6 { + width: 49%; + display: inline-block; + } + + .col-xs-4 { + width: 33%; + } + + .col-xs-2 { + width: 16%; + } + + .left { + float: left; + } + + .left { + padding-right: 5px; + } + + .right { + float: right; + } + + dt { + float: left; + width: 100px; + clear: left; + text-align: right; + overflow: hidden; + text-overflow: ellipsis; + white-space: nowrap; + font-weight: bold; + line-height: 1.42857; + } + + dd { + margin-left: 120px; + line-height: 1.42857; + } + + table { + width: 100%; + border-collapse: collapse; + } + + th { + text-align: left; + border: 0px; + border-top: 1px solid #DDD; + padding: 10px; + } + + thead th { + border-bottom: 2px solid #DDD; + border-top: 0px; + } + + td { + border-top: 1px solid #DDD; + } + + td { + padding: 10px; + } + + .no-border { + border: 0px; + } + + hr { + height: 1px; + border: 0; + color: #333; + background-color: #C4C4C4; + } + + .clear { + clear: both; + } + + .pull-down { + margin-top: 50px; + } + /%style{type:"text/css"} + / = Rails.application.assets.find_asset('shared/pdf').to_s + / = stylesheet_link_tag 'shared/pdf', media: 'all' + / = stylesheet_link_tag 'shared/pdf', media: 'all' + %body + .container + .row + .col-md-12.text-right + = image_tag('eis-logo-black-et.png') + .row + .col-md-12 + = render 'shared/title', name: t(:invoice_no, no: @invoice.id) + .row + .col-md-6.left + %h4 + Details + %hr + %dl.dl-horizontal + %dt= t('issue_date') + %dd= l(@invoice.created_at) + + %dt= t('due_date') + %dd= l(@invoice.due_date) + + %dt= t('receipt_date') + - if @invoice.binded? + %dd= l(@invoice.receipt_date) + - else + %dd{class: 'text-danger'}= t('unpaid') + + %dt= t('payment_term') + %dd= t(@invoice.payment_term) + + %dt= t('description') + - @invoice.description.prepend(' - ') if @invoice.description.present? + %dd= "#{t('invoice_no', no: @invoice.id)}#{@invoice.description}" + + %dt= t('reference_no') + %dd= @invoice.reference_no + .col-md-6.right + %h4= t('buyer') + %hr + %dl.dl-horizontal + %dt= t('name') + %dd= @invoice.buyer_name + + %dt= t('reg_no') + %dd= @invoice.buyer_reg_no + + %dt= t('address') + %dd= @invoice.buyer_address + + %dt= t('country') + %dd= @invoice.buyer_country + + %dt= t('phone') + %dd= @invoice.buyer_phone + + %dt= t('url') + %dd= @invoice.buyer_url + + %dt= t('email') + %dd= @invoice.buyer_email + + + / = render 'registrar/invoices/partials/details' + + / .col-md-6= render 'registrar/invoices/partials/buyer' + .clear + .row.pull-down + .col-md-12= render 'registrar/invoices/partials/items' + + #footer + %hr + .row + .col-md-3 + = @invoice.seller_name + %br + = @invoice.seller_address + %br + = @invoice.seller_country + %br + = "#{t('reg_no')} #{@invoice.seller_reg_no}" + %br + = "#{t('vat_no')} #{@invoice.seller_vat_no}" + + .col-md-3 + = @invoice.seller_phone + %br + = @invoice.seller_email + %br + = @invoice.seller_url + + .col-md-3.text-right + = t('bank') + %br + = t('iban') + %br + = t('swift') + + .col-md-3 + = @invoice.seller_bank + %br + = @invoice.seller_iban + %br + = @invoice.seller_swift diff --git a/app/views/registrar/invoices/show.haml b/app/views/registrar/invoices/show.haml index 332bbf2fc..6646d3d99 100644 --- a/app/views/registrar/invoices/show.haml +++ b/app/views/registrar/invoices/show.haml @@ -1,4 +1,6 @@ - content_for :actions do + = link_to(t(:download), download_pdf_registrar_invoice_path(@invoice), class: 'btn btn-default') + = link_to(t(:forward_invoice), forward_registrar_invoice_path(@invoice), class: 'btn btn-default') = link_to(t(:back), :back, class: 'btn btn-default') = render 'shared/title', name: t(:invoice_no, no: @invoice.id) diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb index 07ab380ca..d1a94d7c6 100644 --- a/config/initializers/assets.rb +++ b/config/initializers/assets.rb @@ -1 +1 @@ -Rails.application.config.assets.precompile += %w( login.css registrar-manifest.css ) +Rails.application.config.assets.precompile += %w( login.css registrar-manifest.css shared/pdf.css ) diff --git a/config/initializers/pdfkit.rb b/config/initializers/pdfkit.rb new file mode 100644 index 000000000..6c9559d98 --- /dev/null +++ b/config/initializers/pdfkit.rb @@ -0,0 +1,7 @@ +PDFKit.configure do |config| + config.wkhtmltopdf = "#{Rails.root}/vendor/bin/wkhtmltopdf" + config.default_options = { + page_size: 'A4' + # :print_media_type => true + } +end diff --git a/config/locales/en.yml b/config/locales/en.yml index 7f8aee0ff..19f8a4b89 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -723,3 +723,8 @@ en: registrar_head_title: 'EIS Registrar' admin_head_title: 'Estonian Internet Foundation' bind_manually: 'Bind manually' + forward_invoice: 'Forward invoice' + forward: 'Forward' + back_to_invoice: 'Back to invoice' + invoice_forwared: 'Invoice forwarded' + failed_to_forward_invoice: 'Failed to forward invoice' diff --git a/config/routes.rb b/config/routes.rb index 25d7b5664..aa3e96965 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -20,7 +20,11 @@ Rails.application.routes.draw do namespace :registrar do root 'polls#show' - resources :invoices + resources :invoices do + get 'download_pdf', on: :member + match 'forward', on: :member, via: [:post, :get] + end + resources :deposits resources :account_activities diff --git a/vendor/bin/wkhtmltopdf b/vendor/bin/wkhtmltopdf new file mode 100755 index 000000000..87851838d Binary files /dev/null and b/vendor/bin/wkhtmltopdf differ