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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/balance.rb', line 12

def calculate(use_stat_balance=true)
  q1 = nil
  q2 = nil

  if !self.up_time.nil? || !use_stat_balance
    q1 = 
    "select cast(sum(cast(amount as numeric(18,12))) as numeric(18,6)) as amount " +
    "from movement with (nolock index(IX_movement__id_client__product_code__create_time__amount)) " +
    "where id_client='#{self.client.id}' " +
    "and product_code='#{self.product_code}' " +
	"and create_time <= '#{self.up_time.to_time.to_sql}' "

    q2 = 
    "select sum(credits) as credits " +
    "from movement with (nolock index(IX_movement__id_client__product_code__create_time__credits)) " +
    "where id_client='#{self.client.id}' " +
    "and product_code='#{self.product_code}' " +
	"and create_time <= '#{self.up_time.to_time.to_sql}' "
  else
    q1 = 
    "select cast(sum(cast(amount as numeric(18,12))) as numeric(18,6)) as amount " +
    "from stat_balance x with (nolock index(IX_movement__id_client__product_code__create_time__amount)) " +
    "where x.id_client='#{self.client.id}' " +
    "and x.product_code='#{self.product_code}' "

    q2 = 
    "select sum(credits) as credits " +
    "from stat_balance x with (nolock index(IX_movement__id_client__product_code__create_time__credits)) " +
    "where x.id_client='#{self.client.id}' " +
    "and x.product_code='#{self.product_code}' "
  end
#puts "Balance.calculate:q:#{q}:."
  row1 = DB[q1].first
  row2 = DB[q2].first
  self.amount = row1[:amount].to_f
  self.credits = row2[:credits].to_f
  # libero recursos
  DB.disconnect
  GC.start
end