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

Make the invoice number formattable #99

Open
wants to merge 3 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
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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

Expand Down
14 changes: 13 additions & 1 deletion app/models/spree/printables/invoice/base_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def shipping_methods

def number
if use_sequential_number?
Spree::PrintInvoice::Config.next_number
formatted_number
else
printable.number
end
Expand All @@ -66,6 +66,18 @@ def after_save_actions

private

def formatted_number
if (Object.const_get('::Spree::PrintInvoice::NumberFormatter') rescue false)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid using rescue in its modifier form.

Copy link

@mkoentopf mkoentopf Sep 1, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or better use defined?(::Spree::PrintInvoice::NumberFormatter). So you do not need a rescue.

::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
Expand Down
Original file line number Diff line number Diff line change
@@ -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) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
27 changes: 24 additions & 3 deletions spec/models/spree/printables/invoice/base_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer single-quoted strings when you don't need string interpolation or special symbols.

end
end
end
end