Module: Invoicing::LedgerItem::ActMethods

Defined in:
lib/invoicing/ledger_item.rb

Instance Method Summary collapse

Instance Method Details

#acts_as_credit_note(options = {}) ⇒ Object

Synonym for acts_as_ledger_item :subtype => :credit_note. All options other than :subtype are passed on to acts_as_ledger_item. You should apply acts_as_credit_note only to a model which is a subclass of an acts_as_ledger_item type.


393
394
395
# File 'lib/invoicing/ledger_item.rb', line 393

def acts_as_credit_note(options={})
  acts_as_ledger_item(options.clone.update({:subtype => :credit_note}))
end

#acts_as_invoice(options = {}) ⇒ Object

Synonym for acts_as_ledger_item :subtype => :invoice. All options other than :subtype are passed on to acts_as_ledger_item. You should apply acts_as_invoice only to a model which is a subclass of an acts_as_ledger_item type.


386
387
388
# File 'lib/invoicing/ledger_item.rb', line 386

def acts_as_invoice(options={})
  acts_as_ledger_item(options.clone.update({:subtype => :invoice}))
end

#acts_as_ledger_item(*args) ⇒ Object

Declares that the current class is a model for ledger items (i.e. invoices, credit notes and payment notes).

This method accepts a hash of options, all of which are optional:

:subtype

One of :invoice, :credit_note or :payment.

Also, the name of any attribute or method required by LedgerItem (as documented on the LedgerItem module) may be used as an option, with the value being the name under which that particular method or attribute can be found. This allows you to use names other than the defaults. For example, if your database column storing the invoice value is called gross_amount instead of total_amount:

acts_as_ledger_item :total_amount => :gross_amount

303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/invoicing/ledger_item.rb', line 303

def acts_as_ledger_item(*args)
  Invoicing::ClassInfo.acts_as(Invoicing::LedgerItem, self, args)
  
  info = ledger_item_class_info
  return unless info.previous_info.nil? # Called for the first time?
  
  before_validation :calculate_total_amount
  
  # Set the 'amount' columns to act as currency values
  acts_as_currency_value(info.method(:total_amount), info.method(:tax_amount),
    :currency => info.method(:currency), :value_for_formatting => :value_for_formatting)
  
  extend Invoicing::FindSubclasses
  include Invoicing::LedgerItem::RenderHTML
  include Invoicing::LedgerItem::RenderUBL
  
  # Dynamically created named scopes
  named_scope :sent_by, lambda{ |sender_id|
    { :conditions => {info.method(:sender_id) => sender_id} }
  }
  
  named_scope :received_by, lambda{ |recipient_id|
    { :conditions => {info.method(:recipient_id) => recipient_id} }
  }
  
  named_scope :sent_or_received_by, lambda{ |sender_or_recipient_id|
    sender_col = connection.quote_column_name(info.method(:sender_id))
    recipient_col = connection.quote_column_name(info.method(:recipient_id))
    { :conditions => ["#{sender_col} = ? OR #{recipient_col} = ?",
                      sender_or_recipient_id, sender_or_recipient_id] }
  }
  
  named_scope :in_effect, :conditions => {info.method(:status) => ['closed', 'cleared']}
  
  named_scope :open_or_pending, :conditions => {info.method(:status) => ['open', 'pending']}
  
  named_scope :due_at, lambda{ |date|
    due_date = connection.quote_column_name(info.method(:due_date))
    {:conditions => ["#{due_date} <= ? OR #{due_date} IS NULL", date]}
  }
  
  named_scope :sorted, lambda{|column|
    column = ledger_item_class_info.method(column).to_s
    if column_names.include?(column)
      {:order => "#{connection.quote_column_name(column)}, #{connection.quote_column_name(primary_key)}"}
    else
      {:order => connection.quote_column_name(primary_key)}
    end
  }
  
  named_scope :exclude_empty_invoices, lambda{
    line_items_assoc_id = info.method(:line_items).to_sym
    line_items_refl = reflections[line_items_assoc_id]
    line_items_table = line_items_refl.quoted_table_name
    
    # e.g. `ledger_items`.`id`
    ledger_items_id = quoted_table_name + "." + connection.quote_column_name(primary_key)
    
    # e.g. `line_items`.`id`
    line_items_id = line_items_table + "." +
      connection.quote_column_name(line_items_refl.klass.primary_key)
    
    # e.g. `line_items`.`ledger_item_id`
    ledger_item_foreign_key = line_items_table + "." + connection.quote_column_name(
      line_items_refl.klass.send(:line_item_class_info).method(:ledger_item_id))
    
    payment_classes = select_matching_subclasses(:is_payment, true).map{|c| c.name}
    is_payment_class = merge_conditions({info.method(:type) => payment_classes})
    
    subquery = construct_finder_sql(
      :select => "#{quoted_table_name}.*, COUNT(#{line_items_id}) AS number_of_line_items",
      :joins => "LEFT JOIN #{line_items_table} ON #{ledger_item_foreign_key} = #{ledger_items_id}",
      :group => Invoicing::ConnectionAdapterExt.group_by_all_columns(self)
    )
      
    {:from => "(#{subquery}) AS #{quoted_table_name}",
     :conditions => "number_of_line_items > 0 OR #{is_payment_class}"}
  }
end

#acts_as_payment(options = {}) ⇒ Object

Synonym for acts_as_ledger_item :subtype => :payment. All options other than :subtype are passed on to acts_as_ledger_item. You should apply acts_as_payment only to a model which is a subclass of an acts_as_ledger_item type.


400
401
402
# File 'lib/invoicing/ledger_item.rb', line 400

def acts_as_payment(options={})
  acts_as_ledger_item(options.clone.update({:subtype => :payment}))
end