Skip to content

quarkstudio/invoice_numbers

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Invoice number generation for ActiveRecord or Mongoid

The problem this gem tries to solve is that for bookkeeping, an uninterrupted sequence of invoice numbers is needed. There are, however, situations in which the database’s auto incrementing id field won’t do. For example:

  • Orders for a webshop should only get an invoice number if the order is actually paid.

  • The order in which record are made is different from the order in which invoices are created, for example in case of a reservations system.

This gem makes this stupid simple:

# as soon as order is paid, invoice_nr will be set
class Order < ActiveRecord::Base
    has_invoice_number :invoice_nr, assign_if: ->(order) { order.paid? }
end

# manually assign an invoice number when the user finishes the reservation
class Reservation < ActiveRecord::Base
    has_invoice_number :invoice_nr

    def finish
        assign_invoice_number
        save
    end
end

# both funky order and boring order will share the same sequence of invoice numbers
class FunkyOrder < ActiveRecord::Base
    has_invoice_number :invoice_nr, assign_if: ->(order) { order.paid? }, invoice_number_sequence: :order
end
class BoringOrder < ActiveRecord::Base
    has_invoice_number :invoice_nr, assign_if: ->(order) { order.paid? }, invoice_number_sequence: :order
end

MongoDB is also supported through Mongoid, so if you don’t use ActiveRecord, you can just do

class Order

include Mongoid::Document include InvoiceNumbers::InvoiceNumbers field :invoice_nr, type: Integer

    has_invoice_number :invoice_nr, assign_if: ->(order) { order.paid? }
end

About

Create sequences of uninterrupted invoice numbers

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%