Method: Client#unpaid_invoices

Defined in:
app/models/client.rb

#unpaid_invoices(how_many = :all, options = {}) ⇒ Object

Returns all the client’s invoices for which the allocated payments is less than the invoice amount. Perhaps this should be a has_many, But since we’re using the find_with_totals that would get complicated…



160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/models/client.rb', line 160

def unpaid_invoices( how_many = :all, options = {} )
  Invoice.find_with_totals(
    how_many, 
    {:conditions => [
      [
      'client_id = ?',
      'is_published = ?',
      'IF(activities_total.total_in_cents IS NULL, 0,activities_total.total_in_cents) - '+
      'IF(invoices_total.total_in_cents IS NULL, 0,invoices_total.total_in_cents) > ?'
      ].join(' AND '),
      id, true, 0
    ]}.merge(options.reject{|k,v| k == :conditions})
  )
end