ruuts / invoice_numbers

Create sequences of uninterrupted invoice numbers
4 stars 6 forks source link

== 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:

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