app/models/client.rb in brisk-bills-0.7.0 vs app/models/client.rb in brisk-bills-0.8.1

- old
+ new

@@ -19,13 +19,15 @@ def name self.company_name end 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) + Money.new( + (attribute_present? :uninvoiced_activities_balance_in_cents and !force_reload) ? + 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] ).to_i + ) end # THis is the client's outstanding balance. This value is calculated based off the total invoices amount - total payments amount. And is # Not determined based on invoice/payment assignments def balance( force_reload = false ) @@ -60,16 +62,21 @@ ).sum_difference_in_cents.to_i ) unless id.nil? end def ensure_not_referenced_on_destroy - errors.add_to_base "Can't destroy a referenced employee" and return false unless authorized_for? {:destroy} + unless authorized_for?(:action => :delete) + errors.add_to_base "Can't destroy a referenced employee" + return false + end end def authorized_for?(options) - case options[:action] - when :destroy + return true unless options.try(:[],:action) + + case options[:action].to_sym + when :delete [Invoice, Payment, Activity].each{ |k| return false if k.count(:all, :conditions => ['client_id = ?', id] ) > 0 } true else true @@ -121,11 +128,11 @@ end # Returns an array of unsaved InvoicePayment objects, with unset invoice_ids, and 'recommended' amounts. # If the provided amount exactly equals an payment's unalloated amount, we return a InvoicePayment for the oldest such matching payment. # Otherwise, we start applying the amount to payments in ascending order by issued_date. - def recommend_payment_assignments_for(amount, verbose_inclusion = false) + def recommend_payment_assignments_for(amount) amount = amount.to_money pymnts = unassigned_payments( :all, # Using this order forces the closest-amount match to be above anything else, followed by date sorting @@ -178,6 +185,26 @@ id, 0 ]}.merge(options.reject{|k,v| k == :conditions}) ) end -end \ No newline at end of file + # This is mostly used by the batch create feature. Here, we return an array of clients, which have approved, + # unassigned activity which occurred before the specified_date + def self.find_invoiceable_clients_at(occurred_at) + find( + :all, + :select => 'DISTINCT `clients`.*', + :joins => 'LEFT JOIN `activities` ON `clients`.id = `activities`.client_id', + :conditions => [ + [ + '`activities`.occurred_on <= ?', + '`activities`.is_published = ?', + '`activities`.invoice_id IS NULL' + ].join(" AND "), + occurred_at, + true + ], + :order => ["`clients`.company_name ASC","`clients`.id ASC"].join(",") + ) + end + +end