Replace ordergroup.account_updated with non database attr.

This commit is contained in:
benni 2012-12-30 15:31:37 +01:00
parent a489079334
commit 28aec7e4d6
3 changed files with 14 additions and 4 deletions

View File

@ -4,7 +4,6 @@
#
# Ordergroup have the following attributes, in addition to Group
# * account_balance (decimal)
# * account_updated (datetime)
class Ordergroup < Group
APPLE_MONTH_AGO = 6 # How many month back we will count tasks and orders sum
@ -49,7 +48,6 @@ class Ordergroup < Group
t = FinancialTransaction.new(:ordergroup => self, :amount => amount, :note => note, :user => user)
t.save!
self.account_balance = financial_transactions.sum('amount')
self.account_updated = t.created_on
save!
# Notify only when order group had a positive balance before the last transaction:
if t.amount < 0 && self.account_balance < 0 && self.account_balance - t.amount >= 0
@ -95,6 +93,10 @@ class Ordergroup < Group
stats = Ordergroup.pluck(:stats)
stats.sum {|s| s[:jobs_size].to_f } / stats.sum {|s| s[:orders_sum].to_f }
end
def account_updated
financial_transactions.last.try(:created_on) || created_on
end
private

View File

@ -0,0 +1,9 @@
class RemoveAccountUpdatedFromOrdergroups < ActiveRecord::Migration
def up
remove_column :groups, :account_updated
end
def down
add_column :groups, :account_updated, :datetime
end
end

View File

@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
ActiveRecord::Schema.define(:version => 20121216180646) do
ActiveRecord::Schema.define(:version => 20121230142516) do
create_table "article_categories", :force => true do |t|
t.string "name", :default => "", :null => false
@ -137,7 +137,6 @@ ActiveRecord::Schema.define(:version => 20121216180646) do
t.string "name", :default => "", :null => false
t.string "description"
t.decimal "account_balance", :precision => 8, :scale => 2, :default => 0.0, :null => false
t.datetime "account_updated"
t.datetime "created_on", :null => false
t.boolean "role_admin", :default => false, :null => false
t.boolean "role_suppliers", :default => false, :null => false