Class: BlackStack::Balance

Inherits:
Object
  • Object
show all
Defined in:
lib/balance.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id_client, product_code, up_time = nil) ⇒ Balance

Returns a new instance of Balance.



5
6
7
8
9
10
# File 'lib/balance.rb', line 5

def initialize(id_client, product_code, up_time=nil)
  self.client = BlackStack::Client.where(:id => id_client).first
  self.product_code = product_code
self.up_time = up_time
  self.calculate()
end

Instance Attribute Details

#amountObject

Returns the value of attribute amount.



3
4
5
# File 'lib/balance.rb', line 3

def amount
  @amount
end

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/balance.rb', line 3

def client
  @client
end

#creditsObject

Returns the value of attribute credits.



3
4
5
# File 'lib/balance.rb', line 3

def credits
  @credits
end

#product_codeObject

Returns the value of attribute product_code.



3
4
5
# File 'lib/balance.rb', line 3

def product_code
  @product_code
end

#up_timeObject

Returns the value of attribute up_time.



3
4
5
# File 'lib/balance.rb', line 3

def up_time
  @up_time
end

Instance Method Details

#calculate(use_stat_balance = true) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/balance.rb', line 12

def calculate(use_stat_balance=true)
  if !self.up_time.nil? || !use_stat_balance
    q = 
    "select cast(sum(cast(amount as numeric(18,12))) as numeric(18,6)) as amount, sum(credits) as credits " +
    "from movement with (nolock) " +
    "where id_client='#{self.client.id}' " +
    "and product_code='#{self.product_code}' " +
  "and create_time <= '#{self.up_time.to_time.to_sql}' "
  else
    q = 
    "select cast(sum(cast(amount as numeric(18,12))) as numeric(18,6)) as amount, sum(credits) as credits " +
    "from stat_balance x with (nolock) " +
    "where x.id_client='#{self.client.id}' " +
    "and x.product_code='#{self.product_code}' "
  end
#puts "Balance.calculate:q:#{q}:."

  row = DB[q].first
  self.amount = row[:amount].to_f
  self.credits = row[:credits].to_f
  # libero recursos

  DB.disconnect
  GC.start
end