Class: Uomi::Invoice
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Uomi::Invoice
- Includes:
- Workflow
- Defined in:
- lib/uomi/invoice.rb
Direct Known Subclasses
Class Method Summary collapse
- .draft ⇒ Object
- .for_payment_reference(reference) ⇒ Object
- .issued ⇒ Object
- .owing ⇒ Object
- .settled ⇒ Object
- .voided ⇒ Object
Instance Method Summary collapse
- #add_credit_transaction(params) ⇒ Object
- #add_debit_transaction(params) ⇒ Object
- #add_line_item(params) ⇒ Object
- #add_payment_reference(params) ⇒ Object
- #adjust(&block) ⇒ Object
- #annul_remaining_amount! ⇒ Object
- #balance_zero? ⇒ Boolean
- #calculate_balance ⇒ Object
- #calculate_totals ⇒ Object
- #create_initial_transaction! ⇒ Object
- #credit_notes ⇒ Object
- #credit_transactions ⇒ Object
- #debit_transactions ⇒ Object
- #decorate_with(decorations) ⇒ Object
- #default_numbering_prefix ⇒ Object
- #due(due_date) ⇒ Object
- #due_date_past? ⇒ Boolean
- #from(sellerable) ⇒ Object
- #issue(&block) ⇒ Object
- #line_item(cost_item) ⇒ Object
- #mark_items_invoiced! ⇒ Object
- #mark_items_uninvoiced! ⇒ Object
- #net_total ⇒ Object
- #numbered(invoice_number) ⇒ Object
- #overdue? ⇒ Boolean
- #owing? ⇒ Boolean
- #payment_reference(reference) ⇒ Object
- #remove_line_item(item) ⇒ Object
- #remove_payment_reference(payment_reference) ⇒ Object
- #set_invoice_number! ⇒ Object
- #should_settle? ⇒ Boolean
- #to(buyerable) ⇒ Object
- #void ⇒ Object
Class Method Details
.draft ⇒ Object
146 147 148 |
# File 'lib/uomi/invoice.rb', line 146 def self.draft where(workflow_state: "draft") end |
.for_payment_reference(reference) ⇒ Object
166 167 168 |
# File 'lib/uomi/invoice.rb', line 166 def self.for_payment_reference(reference) PaymentReference.where(reference: reference).map(&:invoice) end |
.issued ⇒ Object
142 143 144 |
# File 'lib/uomi/invoice.rb', line 142 def self.issued where(workflow_state: "issued") end |
.owing ⇒ Object
138 139 140 |
# File 'lib/uomi/invoice.rb', line 138 def self.owing where("balance < ?", 0) end |
.settled ⇒ Object
150 151 152 |
# File 'lib/uomi/invoice.rb', line 150 def self.settled where(workflow_state: "settled") end |
.voided ⇒ Object
154 155 156 |
# File 'lib/uomi/invoice.rb', line 154 def self.voided where(workflow_state: "voided") end |
Instance Method Details
#add_credit_transaction(params) ⇒ Object
64 65 66 |
# File 'lib/uomi/invoice.rb', line 64 def add_credit_transaction(params) self.transactions << CreditTransaction.new(params) end |
#add_debit_transaction(params) ⇒ Object
60 61 62 |
# File 'lib/uomi/invoice.rb', line 60 def add_debit_transaction(params) self.transactions << DebitTransaction.new(params) end |
#add_line_item(params) ⇒ Object
52 53 54 |
# File 'lib/uomi/invoice.rb', line 52 def add_line_item(params) self.line_items << LineItem.new(params) end |
#add_payment_reference(params) ⇒ Object
158 159 160 |
# File 'lib/uomi/invoice.rb', line 158 def add_payment_reference(params) self.payment_references << PaymentReference.new(params) end |
#adjust(&block) ⇒ Object
229 230 231 232 233 234 |
# File 'lib/uomi/invoice.rb', line 229 def adjust(&block) adjustment = InvoiceAdjustment.new(self) adjustment.instance_eval(&block) adjustment.persist! adjustment.invoice end |
#annul_remaining_amount! ⇒ Object
73 74 75 |
# File 'lib/uomi/invoice.rb', line 73 def annul_remaining_amount! add_credit_transaction amount: balance.abs end |
#balance_zero? ⇒ Boolean
122 123 124 |
# File 'lib/uomi/invoice.rb', line 122 def balance_zero? balance == 0 end |
#calculate_balance ⇒ Object
107 108 109 110 111 112 |
# File 'lib/uomi/invoice.rb', line 107 def calculate_balance bal = (0 - debit_transactions.sum(&:amount)) + credit_transactions.sum(&:amount) self.balance = bal settle! if should_settle? unsettle! if (bal < 0 && settled?) end |
#calculate_totals ⇒ Object
68 69 70 71 |
# File 'lib/uomi/invoice.rb', line 68 def calculate_totals self.total = line_items.inject(0) {|res, item| res + item.amount.to_i} self.tax = line_items.inject(0) {|res, item| res + item.tax.to_i} end |
#create_initial_transaction! ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/uomi/invoice.rb', line 77 def create_initial_transaction! if total < 0 add_credit_transaction amount: total else add_debit_transaction amount: total end end |
#credit_notes ⇒ Object
85 86 87 |
# File 'lib/uomi/invoice.rb', line 85 def credit_notes CreditNoteInvoice.where(invoice_id: id).map(&:credit_note) end |
#credit_transactions ⇒ Object
103 104 105 |
# File 'lib/uomi/invoice.rb', line 103 def credit_transactions transactions.select{|t| t.is_a? CreditTransaction} end |
#debit_transactions ⇒ Object
99 100 101 |
# File 'lib/uomi/invoice.rb', line 99 def debit_transactions transactions.select{|t| t.is_a? DebitTransaction} end |
#decorate_with(decorations) ⇒ Object
217 218 219 220 221 222 223 |
# File 'lib/uomi/invoice.rb', line 217 def decorate_with(decorations) if self.invoice_decorator self.invoice_decorator.data = decorations else self.invoice_decorator = InvoiceDecorator.new(data: decorations) end end |
#default_numbering_prefix ⇒ Object
89 90 91 |
# File 'lib/uomi/invoice.rb', line 89 def default_numbering_prefix "INV" end |
#due(due_date) ⇒ Object
205 206 207 |
# File 'lib/uomi/invoice.rb', line 205 def due(due_date) self.due_date = due_date end |
#due_date_past? ⇒ Boolean
130 131 132 |
# File 'lib/uomi/invoice.rb', line 130 def due_date_past? due_date.to_date < Date.today end |
#from(sellerable) ⇒ Object
213 214 215 |
# File 'lib/uomi/invoice.rb', line 213 def from(sellerable) self.seller = Seller.for(sellerable) end |
#issue(&block) ⇒ Object
37 38 39 40 41 42 43 |
# File 'lib/uomi/invoice.rb', line 37 def issue(&block) self.issued_at = Time.now instance_eval(&block) if block_given? create_initial_transaction! mark_items_invoiced! self end |
#line_item(cost_item) ⇒ Object
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/uomi/invoice.rb', line 182 def line_item(cost_item) if cost_item.is_a? Hash add_line_item( amount: cost_item[:amount] || 0, tax: cost_item[:tax] || 0, description: cost_item[:description] || 'Line Item', line_item_type_id: cost_item[:line_item_type_id] ) else add_line_item( invoiceable: cost_item, amount: cost_item.amount || 0, tax: cost_item.tax || 0, description: cost_item.description || 'Line Item', line_item_type_id: cost_item.respond_to?(:line_item_type_id) ? cost_item.line_item_type_id : 0 ) end end |
#mark_items_invoiced! ⇒ Object
170 171 172 173 174 |
# File 'lib/uomi/invoice.rb', line 170 def mark_items_invoiced! line_items.map(&:invoiceable).compact.each do |item| item.mark_invoiced(self) if item.respond_to?(:mark_invoiced) end end |
#mark_items_uninvoiced! ⇒ Object
176 177 178 179 180 |
# File 'lib/uomi/invoice.rb', line 176 def mark_items_uninvoiced! line_items.map(&:invoiceable).compact.each do |item| item.mark_uninvoiced(self) if item.respond_to?(:mark_uninvoiced) end end |
#net_total ⇒ Object
118 119 120 |
# File 'lib/uomi/invoice.rb', line 118 def net_total total - tax end |
#numbered(invoice_number) ⇒ Object
225 226 227 |
# File 'lib/uomi/invoice.rb', line 225 def numbered(invoice_number) self.invoice_number = invoice_number end |
#overdue? ⇒ Boolean
134 135 136 |
# File 'lib/uomi/invoice.rb', line 134 def overdue? owing? and due_date_past? end |
#owing? ⇒ Boolean
126 127 128 |
# File 'lib/uomi/invoice.rb', line 126 def owing? balance < 0 end |
#payment_reference(reference) ⇒ Object
201 202 203 |
# File 'lib/uomi/invoice.rb', line 201 def payment_reference(reference) add_payment_reference(reference: reference) end |
#remove_line_item(item) ⇒ Object
56 57 58 |
# File 'lib/uomi/invoice.rb', line 56 def remove_line_item(item) line_items.delete(item) end |
#remove_payment_reference(payment_reference) ⇒ Object
162 163 164 |
# File 'lib/uomi/invoice.rb', line 162 def remove_payment_reference(payment_reference) payment_references.delete(payment_reference) end |
#set_invoice_number! ⇒ Object
93 94 95 96 97 |
# File 'lib/uomi/invoice.rb', line 93 def set_invoice_number! self.invoice_number ||= "#{default_numbering_prefix}#{id}" self.invoice_number.gsub!("{id}", "#{id}") save! end |
#should_settle? ⇒ Boolean
114 115 116 |
# File 'lib/uomi/invoice.rb', line 114 def should_settle? issued? && balance_zero? end |
#to(buyerable) ⇒ Object
209 210 211 |
# File 'lib/uomi/invoice.rb', line 209 def to(buyerable) self.buyer = Buyer.for(buyerable) end |
#void ⇒ Object
45 46 47 48 49 50 |
# File 'lib/uomi/invoice.rb', line 45 def void raise CannotVoidDocumentException, "Cannot void a document that has a transaction recorded against it!" if transactions.many? annul_remaining_amount! unless self.draft? mark_items_uninvoiced! self end |