From 2b8867fab80c843dbc2f960e65f571984c858a46 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 3 Nov 2015 16:29:02 +0100 Subject: [PATCH 1/3] Make the invoice number formattable In order to satisfy jurisdictions where invoice numbers have to conform specific formal requirements (number of digits, prefix, whatever) you can now define a `Spree::PrintInvoice::NumberFormattter` class which initializes with a integer, and implements a `to_s` method returning a formatted number. --- .../spree/printables/invoice/base_view.rb | 14 +++++++++- .../printables/invoice/base_view_spec.rb | 27 ++++++++++++++++--- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/app/models/spree/printables/invoice/base_view.rb b/app/models/spree/printables/invoice/base_view.rb index d01215c4..65738d08 100644 --- a/app/models/spree/printables/invoice/base_view.rb +++ b/app/models/spree/printables/invoice/base_view.rb @@ -54,7 +54,7 @@ def shipping_methods def number if use_sequential_number? - Spree::PrintInvoice::Config.next_number + formatted_number else printable.number end @@ -66,6 +66,18 @@ def after_save_actions private + def formatted_number + if (Object.const_get('::Spree::PrintInvoice::NumberFormatter') rescue false) + ::Spree::PrintInvoice::NumberFormatter.new(next_number).to_s + else + next_number + end + end + + def next_number + Spree::PrintInvoice::Config.next_number + end + def increase_invoice_number! Spree::PrintInvoice::Config.increase_invoice_number! end diff --git a/spec/models/spree/printables/invoice/base_view_spec.rb b/spec/models/spree/printables/invoice/base_view_spec.rb index 3d62d7c3..fdc2c902 100644 --- a/spec/models/spree/printables/invoice/base_view_spec.rb +++ b/spec/models/spree/printables/invoice/base_view_spec.rb @@ -68,14 +68,17 @@ end describe '#number' do + before do + allow(Spree::PrintInvoice::Config).to receive(:next_number) { 77 } + end + context 'when using sequential numbers' do before do allow(Spree::PrintInvoice::Config).to receive(:use_sequential_number?) { true } end - it 'calls next_number on Spree::PrintInvoice::Config' do - expect(Spree::PrintInvoice::Config).to receive(:next_number) - base_view.number + it 'returns the next number without additional formatting' do + expect(base_view.number).to eq(77) end end @@ -89,5 +92,23 @@ base_view.number end end + + context 'when using a NumberFormatter' do + before do + class Spree::PrintInvoice::NumberFormatter + def initialize(number) + @number = number + end + + def to_s + "MY-NICE-INVOICE-#{@number}" + end + end + end + + it 'returns a nicely formatted number' do + expect(base_view.number).to eq("MY-NICE-INVOICE-77") + end + end end end From 19e4da667282c4c9c701498da7784ffc89c53f26 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 3 Nov 2015 23:06:10 +0100 Subject: [PATCH 2/3] Fix BookkeepingDocumentsController spec Somehow, it worked before without a user being created. This adds the necessary before block. Also, replace the FactoryGirl.create call with a cheaper FactoryGirl.build. --- .../spree/admin/bookkeeping_documents_controller_spec.rb | 6 ++++++ .../spree/admin/print_invoice_settings_controller_spec.rb | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/spec/controllers/spree/admin/bookkeeping_documents_controller_spec.rb b/spec/controllers/spree/admin/bookkeeping_documents_controller_spec.rb index 057fbb33..b0d43f50 100644 --- a/spec/controllers/spree/admin/bookkeeping_documents_controller_spec.rb +++ b/spec/controllers/spree/admin/bookkeeping_documents_controller_spec.rb @@ -1,6 +1,12 @@ RSpec.describe Spree::Admin::BookkeepingDocumentsController, type: :controller do stub_authorization! + before do + reset_spree_preferences + user = build(:admin_user) + allow(controller).to receive(:try_spree_current_user).and_return(user) + end + describe '#show as :pdf' do context 'an order invoice' do let!(:order) { create(:invoiceable_order) } diff --git a/spec/controllers/spree/admin/print_invoice_settings_controller_spec.rb b/spec/controllers/spree/admin/print_invoice_settings_controller_spec.rb index a49fc701..7296db29 100644 --- a/spec/controllers/spree/admin/print_invoice_settings_controller_spec.rb +++ b/spec/controllers/spree/admin/print_invoice_settings_controller_spec.rb @@ -3,7 +3,7 @@ before do reset_spree_preferences - user = create(:admin_user) + user = build(:admin_user) allow(controller).to receive(:try_spree_current_user).and_return(user) end From 18122be61053c50adaa012d1ccf3f82f6fd7a5b5 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Tue, 3 Nov 2015 23:55:13 +0100 Subject: [PATCH 3/3] Update README.md --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 192eef4b..6796e864 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![Build Status](https://travis-ci.org/spree-contrib/spree_print_invoice.svg?branch=master)](https://travis-ci.org/spree-contrib/spree_print_invoice) [![Code Climate](https://codeclimate.com/github/spree-contrib/spree_print_invoice/badges/gpa.svg)](https://codeclimate.com/github/spree-contrib/spree_print_invoice) -This extension provides a model `Spree::BookkeepingDocument`, which generates PDFs from any Spree Object with the help of View objects that translate between different object structures and PDF templates. It stores a "number" string as well as first name, last name, email, and amount with each document for convenient searching in the backend. +This extension provides a model `Spree::BookkeepingDocument`, which generates PDFs from any Spree Object with the help of View objects that translate between different object structures and PDF templates. It stores a `number` string as well as first `name`, `last name`, `email`, and `amount` with each document for convenient searching in the backend. The Gem contains an example implementation for Invoices for `Spree::Orders`. The basic structure looks like this: @@ -13,6 +13,20 @@ The Gem contains an example implementation for Invoices for `Spree::Orders`. The In the `Spree::Admin::OrdersController#edit` view, you'll find an additional button `Documents`, where all printable documents will be listed. Additionally, you can find all available Documents in a "Documents" tab in the main menu. +There's also the option of adding a custom `Spree::PrintInvoice::NumberFormatter` class. If that class is present, it will be used to generate a custom string from the next invoice number, complete with (for example) a prefix and padding. Here's what we did: + +``` +class Spree::PrintInvoice::NumberFormatter + # Generates a number that starts with INV- and pads the invoice number to ten digits + def initialize(number) + @number = number + end + + def to_s + "INV-#{'%.10d' % number}" + end +end +``` ## Installation