Class: Client

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
IsActiveModelHelper
Defined in:
app/models/client.rb

Direct Known Subclasses

ClientAccounting

Instance Method Summary collapse

Methods included from IsActiveModelHelper

append_features, #initialize

Instance Method Details

#authorized_for?(options) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
74
75
# File 'app/models/client.rb', line 66

def authorized_for?(options)
  case options[:action]
    when :destroy
      [Invoice, Payment, Activity].each{ |k| return false if k.count(:all, :conditions => ['client_id = ?', id] ) > 0 }

      true
    else
      true
  end
end

#balance(force_reload = false) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'app/models/client.rb', line 29

def balance( force_reload = false )
  Money.new(
    (attribute_present? :balance_in_cents and !force_reload) ?
      read_attribute(:balance_in_cents).to_i :
      Client.find(
      :first, 
      :select => [
        'id',
        'company_name',
        '(
         IF(charges.charges_sum_in_cents IS NULL, 0,charges.charges_sum_in_cents) - 
         IF(deposits.payment_sum_in_cents IS NULL, 0, deposits.payment_sum_in_cents)
        ) AS sum_difference_in_cents'
      ].join(', '),
      :joins => [
        "LEFT JOIN(
          SELECT invoices.client_id, SUM(#{Invoice::ACTIVITY_TOTAL_SQL}) AS charges_sum_in_cents 
            FROM invoices 
            LEFT JOIN activities ON activities.invoice_id = invoices.id 
            GROUP BY invoices.client_id
        ) AS charges ON charges.client_id = clients.id",
        
        'LEFT JOIN (
          SELECT payments.client_id, SUM(payments.amount_in_cents) AS payment_sum_in_cents
            FROM payments 
            GROUP BY payments.client_id
        ) AS deposits ON deposits.client_id = clients.id '
      ].join(' '),
      :conditions => [ 'clients.id = ?', id]
      ).sum_difference_in_cents.to_i
  ) unless id.nil?
end

#ensure_not_referenced_on_destroyObject



62
63
64
# File 'app/models/client.rb', line 62

def ensure_not_referenced_on_destroy
  errors.add_to_base "Can't destroy a referenced employee" and return false unless authorized_for? {:destroy}
end

#mailing_addressObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/client.rb', line 77

def mailing_address
  ret = []
  
  %w(name address1 address2).each do |f|
    val = send(f.to_sym) and ( ret <<val if val.length )
  end

  ret << '%s%s %s' % [
    (city.nil? or city.length == 0) ? '' : "#{city}, ", 
    state, 
    zip
  ]

  ret
end

#nameObject



19
20
21
# File 'app/models/client.rb', line 19

def name
  self.company_name
end

#uninvoiced_activities_balance(force_reload = false) ⇒ Object



23
24
25
26
27
# File 'app/models/client.rb', line 23

def uninvoiced_activities_balance( force_reload = false )
  (attribute_present? :uninvoiced_activities_balance_in_cents and !force_reload) ?
    Money.new(read_attribute(:uninvoiced_activities_balance_in_cents).to_i) :
    (Activity.sum( Invoice::ACTIVITY_TOTAL_SQL, :conditions => ['client_id = ? AND is_published = ? AND invoice_id IS NULL',id, true] ) or 0.0)
end