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

Don't use Rails.application.assets #106

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ rvm:
sudo: false
cache: bundler
before_script:
- sh -e /etc/init.d/xvfb start
- export DISPLAY=:99.0
- bundle exec rake test_app
script:
- bundle exec rspec spec
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
source 'https://rubygems.org'

gem 'spree', github: 'spree/spree'
gem 'spree_auth_devise', github: 'spree/spree_auth_devise'
gem 'spree', github: 'spree/spree', branch: "3-0-stable"
gem 'spree_auth_devise', github: 'spree/spree_auth_devise', branch: "3-0-stable"

gemspec
47 changes: 34 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Enjoy! Now allow to generate invoices with sequential numbers.

1. Set the logo path preference to include your store / company logo.

```ruby
Spree::PrintInvoice::Config.set(logo_path: '/path/to/public/images/company-logo.png')
```
```ruby
Spree::PrintInvoice::Config.set(logo_path: '/path/to/public/images/company-logo.png')
```

2. Add your own own footer texts to the locale. The current footer works with `:footer_left1` , `:footer_left2` and `:footer_right1`, `:footer_right2` where the 1 version is on the left in bold, and the 2 version the "value" on the right.

Expand All @@ -37,26 +37,47 @@ Spree::PrintInvoice::Config.set(logo_path: '/path/to/public/images/company-logo.

5. Many european countries requires numeric and sequential invoices numbers. To use invoices sequential number fill the specific field in "General Settings" or by setting:

```ruby
Spree::PrintInvoice::Config.set(next_number: [1|'your current next invoice number'])
```
```ruby
Spree::PrintInvoice::Config.set(next_number: [1|'your current next invoice number'])
```

The next invoice number will be the one that you specified. You will able to increase it in any moment, for example, to re-sync invoices number if you are making invoices also in other programs for the same business name.
The next invoice number will be the one that you specified. You will able to increase it in any moment, for example, to re-sync invoices number if you are making invoices also in other programs for the same business name.

6. Enable packaging slips, by setting:

```ruby
Spree::PrintInvoice::Config.set(print_buttons: 'invoice,packaging_slip') # comma separated list
```
```ruby
Spree::PrintInvoice::Config.set(print_buttons: 'invoice,packaging_slip') # comma separated list
```

Use above feature for your own template if you want. For each button_name, define `button_name_print` text in your locale.
Use above feature for your own template if you want. For each button_name, define `button_name_print` text in your locale.

7. Set page/document options with:

```ruby
Spree::PrintInvoice::Config.set(prawn_options: { page_layout: :landscape, page_size: 'A4', margin: [50, 100, 150, 200] })
```ruby
Spree::PrintInvoice::Config.set(prawn_options: { page_layout: :landscape, page_size: 'A4', margin: [50, 100, 150, 200] })
```

8. Enable PDF storage feature

PDF files can be stored to disk. This is very handy, if you want to send these files as email attachment.

```ruby
Spree::PrintInvoice::Config.set(store_pdf: true) # Default: false
Spree::PrintInvoice::Config.set(storage_path: 'pdfs/orders') # Default: tmp/order_prints
```

*) Inside the `storage_path` a folder for each template will be created. Files will be saved with order number respectively invoice number as file name.

## Customize templates

In order to customize the build in invoice and packaging slip templates you need to copy them into your app:

```sh
$ bundle exec rails g spree_print_invoice:templates
```

You can then customize them at `app/views/spree/admin/orders/invoice.pdf.prawn` and `app/views/spree/admin/orders/packaging_slip.pdf.prawn`.

---

## Contributing
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$ ->
storage_path_field = $('#storage_path')

$('#store_pdf').click ->
storage_path_field.prop('disabled', !$(this).prop('checked'))
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
//= require spree/backend
//= require ./print_invoice_settings
20 changes: 11 additions & 9 deletions app/controllers/spree/admin/orders_controller_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,25 @@ module Admin

def show
load_order

respond_with(@order) do |format|
format.pdf do
template = params[:template] || 'invoice'
update_sequential_number_for(@order) if template == 'invoice'
render layout: false, template: "spree/admin/orders/#{template}.pdf.prawn"
@order.update_invoice_number!

send_data @order.pdf_file(pdf_template_name),
type: 'application/pdf', disposition: 'inline'
end
end
end

private

def update_sequential_number_for(order)
return unless Spree::PrintInvoice::Config.use_sequential_number?
return unless order.invoice_number.present?
order.invoice_number = Spree::PrintInvoice::Config.increase_invoice_number
order.invoice_date = Date.today
order.save!
def pdf_template_name
pdf_template_name = params[:template] || 'invoice'
if !Spree::PrintInvoice::Config.print_templates.include?(pdf_template_name)
raise Spree::PrintInvoice::UnsupportedTemplateError.new(pdf_template_name)
end
pdf_template_name
end
end
end
Expand Down
99 changes: 99 additions & 0 deletions app/models/spree/order_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
Spree::Order.class_eval do

# Updates +invoice_number+ without calling ActiveRecord callbacks
#
# Only updates if number is not already present and if
# +Spree::PrintInvoice::Config.next_number+ is set and greater than zero.
#
# Also sets +invoice_date+ to current date.
#
def update_invoice_number!
return unless Spree::PrintInvoice::Config.use_sequential_number?
return if invoice_number.present?

update_columns(
invoice_number: Spree::PrintInvoice::Config.increase_invoice_number,
invoice_date: Date.today
)
end

# Returns the given template as pdf binary suitable for Rails send_data
#
# If the file is already present it returns this
# else it generates a new file, stores and returns this.
#
# You can disable the pdf file generation with setting
#
# Spree::PrintInvoice::Config.store_pdf to false
#
def pdf_file(template)
if Spree::PrintInvoice::Config.store_pdf
send_or_create_pdf(template)
else
render_pdf(template)
end
end

# = The PDF filename
#
# Tries to take invoice_number attribute.
# If this is not present it takes the order number.
#
def pdf_filename
invoice_number.present? ? invoice_number : number
end

# = PDF file path for template name
#
def pdf_file_path(template)
Rails.root.join(pdf_storage_path(template), "#{pdf_filename}.pdf")
end

# = PDF storage folder path for given template name
#
# Configure the storage path with +Spree::PrintInvoice::Config.storage_path+
#
# Each template type gets it own pluralized folder inside
# of +Spree::PrintInvoice::Config.storage_path+
#
# == Example:
#
# pdf_storage_path('invoice') => "tmp/pdf_prints/invoices"
#
# Creates the folder if it's not present yet.
#
def pdf_storage_path(template)
storage_path = Rails.root.join(Spree::PrintInvoice::Config.storage_path, template.pluralize)
FileUtils.mkdir_p(storage_path)
storage_path
end

# Renders the prawn template for give template name in context of ActionView.
#
# Prawn templates need to be placed in +app/views/spree/admin/orders/+ folder.
#
# Assigns +@order+ instance variable
#
def render_pdf(template)
ActionView::Base.new(
ActionController::Base.view_paths,
{order: self}
).render(template: "spree/admin/orders/#{template}.pdf.prawn")
end

private

# Sends stored pdf for given template from disk.
#
# Renders and stores it if it's not yet present.
#
def send_or_create_pdf(template)
file_path = pdf_file_path(template)

unless File.exist?(file_path)
File.open(file_path, "wb") { |f| f.puts render_pdf(template) }
end

IO.binread(file_path)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
virtual_path: 'spree/admin/shared/sub_menu/_configuration',
name: 'print_invoice_admin_configurations_menu',
insert_bottom: '[data-hook="admin_configurations_sidebar_menu"]',
text: '<%= configurations_sidebar_menu_item Spree.t(:settings, scope: :print_invoice), edit_admin_print_invoice_settings_path %>'
text: '<%= configurations_sidebar_menu_item Spree.t(:settings, scope: :print_invoice), spree.edit_admin_print_invoice_settings_path %>'
)
11 changes: 6 additions & 5 deletions app/views/spree/admin/orders/invoice.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ define_grid(columns: 5, rows: 8, gutter: 10)

# HEADER
repeat(:all) do
im = Rails.application.assets.find_asset(Spree::PrintInvoice::Config[:logo_path])
if File.exist? im
image im, vposition: :top, height: 40, scale: Spree::PrintInvoice::Config[:logo_scale]
im = asset_path(Spree::PrintInvoice::Config[:logo_path])

if im && File.exist?(im.pathname)
image im.filename, vposition: :top, height: 40, scale: Spree::PrintInvoice::Config[:logo_scale]
end

grid([0,3], [0,4]).bounding_box do
Expand Down Expand Up @@ -67,8 +68,8 @@ grid([1,0], [6,4]).bounding_box do

@order.line_items.each do |item|
row = [
item.variant.product.sku,
item.variant.product.name,
item.variant.sku,
item.variant.name,
item.variant.options_text,
item.single_display_amount.to_s,
item.quantity,
Expand Down
11 changes: 6 additions & 5 deletions app/views/spree/admin/orders/packaging_slip.pdf.prawn
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ define_grid(columns: 5, rows: 8, gutter: 10)

# HEADER
repeat(:all) do
im = Rails.application.assets.find_asset(Spree::PrintInvoice::Config[:logo_path])
if File.exist? im
im = asset_path(Spree::PrintInvoice::Config[:logo_path])

if im && File.exist?(im.pathname)
image im, vposition: :top, height: 40, scale: Spree::PrintInvoice::Config[:logo_scale]
end

Expand All @@ -19,7 +20,7 @@ repeat(:all) do

text Spree.t(:invoice_number, number: @order.invoice_number, scope: :print_invoice), align: :right
move_down 2
text "#{Spree.t(:invoice_date)} #{I18n.l @order.invoice_date}", align: :right
text "#{Spree.t(:invoice_date, scope: :print_invoice)} #{I18n.l @order.invoice_date}", align: :right

else

Expand Down Expand Up @@ -76,8 +77,8 @@ grid([1,0], [6,4]).bounding_box do

@order.line_items.each do |item|
row = [
item.variant.product.sku,
item.variant.product.name,
item.variant.sku,
item.variant.name,
item.variant.options_text,
item.quantity
]
Expand Down
14 changes: 14 additions & 0 deletions app/views/spree/admin/print_invoice_settings/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@
<%= label_tag :logo_scale, Spree.t(:logo_scale, scope: [:print_invoice, :preferences]) %>
<%= number_field_tag(:logo_scale, Spree::PrintInvoice::Config[:logo_scale], in: 1...101, class: 'form-control') %>
</div>

<div class="form-group">
<div class="checkbox">
<%= label_tag :store_pdf do %>
<%= preference_field_tag(:store_pdf, Spree::PrintInvoice::Config[:store_pdf], type: :boolean) %>
<%= Spree.t(:store_pdf, scope: [:print_invoice, :preferences]) %>
<% end %>
</div>
</div>

<div class="form-group">
<%= label_tag :storage_path, Spree.t(:storage_path, scope: [:print_invoice, :preferences]) %>
<%= preference_field_tag(:storage_path, Spree::PrintInvoice::Config[:storage_path], class: 'form-control', disabled: !Spree::PrintInvoice::Config[:store_pdf]) %>
</div>
</fieldset>
</div>

Expand Down
42 changes: 22 additions & 20 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,39 @@ de:
print_invoice:
buttons:
invoice: Rechnung drucken
packaging_slip: Warenbegleitschein drucken
invoice: Kundenrechnung
packaging_slip: Lieferschein drucken
invoice: Rechnung
item_name: Artikelnamen oder SKU
of: aus
via: über
via: per
on_date: Bestelldatum
packaging_slip: Packzettel drucken
packaging_slip: Lieferschein
page: Seite
payment_via: "%{gateway} Online-Zahlung #%{number} auf %{date}"
payment_via: "%{gateway} Zahlung #%{number} per %{date}"
preferences:
next_number: Rechnung drucken nächste Zahl
use_footer: Verwenden Fußzeile
use_page_numbers: Verwenden Sie die Seitenzahlen
anomaly_message: Bestellen Anomalie Nachricht
footer_left: Linke Fußzeile Inhalt
next_number: nächste Rechnungsnummer
use_footer: Fußzeile verwenden
use_page_numbers: Seitenzahlen anzeigen
anomaly_message: Bestellanomalie-Hinweis
footer_left: Inhalt der linken Fußzeile
footer_left_info: Z.B. Ihr Firmenname und Adresse.
footer_right: Rechts Fußzeile Inhalt
footer_right: Inhalt der rechten Fußzeile
footer_right_info: Z.B. Ihre Telefon-und E-Mail-Adresse.
page_layout: Seitenlayout
page_size: Seitengröße
print_buttons: Drucktasten
print_buttons_info: Durch Kommata getrennte Liste.
logo_path: Rechnung drucken logo Weg
return_message: Bestellrückmeldung
print_buttons: Anzuzeigende Buttons
print_buttons_info: Durch Kommas getrennte Liste.
logo_path: Dateipfad zum Logo
return_message: Hinweis bei Rückgaben
font_face: Schriftart
font_size: Schriftgröße
logo_scale: Logo Maßstab
settings: Rechnung drucken Einstellungen
unprocessed: Unverarbeitet
logo_scale: Logo-Skalierung
store_pdf: PDF-Dateien speichern
storage_path: Speicherort für PDF-Dateien
settings: Rechnungsdruck-Einstellungen
unprocessed: offene
vat: MwSt. nicht anwendbar
pdf_legend: PDF
invoice_slip_legend: Rechnung / Slip
pdf_legend: PDF-Grundeinstellungen
invoice_slip_legend: PDF-Inhaltseinstellungen
invoice_number: 'Rechnungsnummer %{number}'
invoice_date: Rechnungsdatum
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ en:
font_face: Font Face
font_size: Font Size
logo_scale: Logo Scale
store_pdf: Store PDF files
storage_path: PDF storage path
settings: Print Invoice Settings
unprocessed: Unprocessed
vat: VAT not applicable
Expand Down
3 changes: 2 additions & 1 deletion config/locales/es.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ es:
return_message: Mensaje de vuelta del pedido
font_face: Tipo de letra
font_size: Tamaño de letra
logo_scale: Eescala de Logo
store_pdf: Store PDF files
storage_path: PDF storage path
settings: Configuración de impresión de facturas
unprocessed: No procesado
vat: IVA no aplicable
Expand Down
Loading