invoicing_payments_processing 1.1.59 → 1.1.64

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 18728dea987541ecbbed7da0945b9699d258d626
4
- data.tar.gz: 324265ee99c3bd89c570527250b9d5b6b7a805fd
3
+ metadata.gz: 504ea9259de82ef1ffc339b33a7fca1a4273b6a9
4
+ data.tar.gz: be390eb676c4cd0eeb5398681e6a6d02d2bcd272
5
5
  SHA512:
6
- metadata.gz: 7d430dad7ca8f9dabc486352d1a169b56ef994bf515784d851b70e0a556baa495b52060d3905affc26c98a0f4ee3e9be0f630c7ba48c0e616a791181fb223116
7
- data.tar.gz: 58b118d7c14d2ad05c4025996fc8b56f94e17624d00fe087998b648486e261468e60582c6b8dd85145d8b0f327317f3f9be14b53afeaa040ee951a88a6222152
6
+ metadata.gz: 49240d883710395e708b20b382f95eb3b9c1fc1c3a1f87abcfc2afb9cd6b0088c74fcc0413dd23213b07fbbdf10987681725b19f265f532e08b741f91cfc7997
7
+ data.tar.gz: 9d6a4fbbae677537d13699075debbebf4e5ffe54b2dba6fbc7b51c5f80c9dbb7ee6d2086c1e3d3e163a7d18a474e04e56b0bc3a51c62b6c000c66bee840a0322
@@ -9,14 +9,14 @@ module BlackStack
9
9
  self.calculate()
10
10
  end
11
11
 
12
- def calculate()
13
- if !self.up_time.nil?
12
+ def calculate(use_stat_balance=true)
13
+ if !self.up_time.nil? || !use_stat_balance
14
14
  q =
15
15
  "select cast(sum(cast(amount as numeric(18,12))) as numeric(18,6)) as amount, sum(credits) as credits " +
16
16
  "from movement with (nolock) " +
17
17
  "where id_client='#{self.client.id}' " +
18
18
  "and product_code='#{self.product_code}' " +
19
- "and create_time < '#{self.up_time.to_time.to_sql}' "
19
+ "and create_time <= '#{self.up_time.to_time.to_sql}' "
20
20
  else
21
21
  q =
22
22
  "select cast(sum(cast(amount as numeric(18,12))) as numeric(18,6)) as amount, sum(credits) as credits " +
@@ -24,7 +24,7 @@ module BlackStack
24
24
  "where x.id_client='#{self.client.id}' " +
25
25
  "and x.product_code='#{self.product_code}' "
26
26
  end
27
-
27
+ #puts "Balance.calculate:q:#{q}:."
28
28
  row = DB[q].first
29
29
  self.amount = row[:amount].to_f
30
30
  self.credits = row[:credits].to_f
@@ -6,7 +6,26 @@ module BlackStack
6
6
  class Client < Sequel::Model(:client)
7
7
  one_to_many :paypal_subscriptions, :class=>:'BlackStack::PayPalSubscription', :key=>:id_client
8
8
  one_to_many :customplans, :class=>:'BlackStack::CustomPlan', :key=>:id_client
9
- one_to_many :movements, :class=>:'BlackStack::Movement', :key=>:id_client
9
+
10
+ # This method replace the line:
11
+ # one_to_many :movements, :class=>:'BlackStack::Movement', :key=>:id_client
12
+ #
13
+ # Because when you have a large number of records in the table movement, for a client,
14
+ # then the call to this attribute client.movements can take too much time and generates
15
+ # a query timeout exception.
16
+ #
17
+ # The call to this method may take too much time, but ti won't raise a query timeout.
18
+ #
19
+ def movements
20
+ ret = []
21
+ BlackStack::Movement.where(:id_client=>self.id).each { |o|
22
+ ret << o
23
+ print '.'
24
+ GC.start
25
+ DB.disconnect
26
+ }
27
+ ret
28
+ end
10
29
 
11
30
  # how many minutes ago should have updated the table stat_balance with the amount and credits of this client, for each product.
12
31
  def stat_balance_delay_minutes
@@ -28,7 +47,7 @@ module BlackStack
28
47
  # update the table stat_balance with the amount and credits of this client, for each product.
29
48
  def update_stat_balance(product_code=nil)
30
49
  c = self
31
- product_descriptors = BlackStack::InvoicingPaymentsProcessing::products_descriptor
50
+ product_descriptors = BlackStack::InvoicingPaymentsProcessing::products_descriptor.clone
32
51
  product_descriptors.select! { |hprod| hprod[:code] == product_code } if !product_code.nil?
33
52
  product_descriptors.each { |hprod|
34
53
  row = DB[
@@ -51,7 +70,9 @@ module BlackStack
51
70
  end
52
71
 
53
72
  # crea/actualiza un registro en la tabla movment, reduciendo la cantidad de creditos y saldo que tiene el cliente, para el producto indicado en product_code.
54
- def consume(product_code, number_of_credits=1, description=nil)
73
+ def consume(product_code, number_of_credits=1, description=nil, datetime=nil)
74
+ dt = datetime.nil? ? now() : datetime.to_time.to_sql
75
+
55
76
  # create the consumtion
56
77
  total_credits = 0.to_f - BlackStack::Balance.new(self.id, product_code).credits.to_f
57
78
  total_amount = 0.to_f - BlackStack::Balance.new(self.id, product_code).amount.to_f
@@ -60,7 +81,7 @@ module BlackStack
60
81
  cons = BlackStack::Movement.new
61
82
  cons.id = guid()
62
83
  cons.id_client = self.id
63
- cons.create_time = now()
84
+ cons.create_time = dt
64
85
  cons.type = BlackStack::Movement::MOVEMENT_TYPE_CANCELATION
65
86
  cons.description = description.nil? ? 'Consumption' : description
66
87
  cons.paypal1_amount = 0
@@ -135,7 +156,7 @@ module BlackStack
135
156
  credits_paid = 0
136
157
 
137
158
  #total_credits = 0.to_f - BlackStack::Balance.new(self.id, product_code).credits.to_f
138
- #total_amount = 0.to_f - BlackStack::Balance.new(self.id, product_code).amount.to_f
159
+ #total_amount = 0.to_f - BlackStack::Balance.new(self.id, product_code).amount.to_f
139
160
 
140
161
  self.movements.select { |o|
141
162
  o.product_code.upcase == product_code.upcase
@@ -121,28 +121,66 @@ module BlackStack
121
121
 
122
122
  # Returns the number of credits assigned in the movement that have been consumed.
123
123
  # The movment must be a payment or a bonus
124
- def credits_consumed()
124
+ #
125
+ # registraton_time: consider only movements before this time. If it is nil, this method will consider all the movements.
126
+ #
127
+ def credits_consumed(registraton_time=nil)
128
+ # le agrego 365 dias a la fecha actual, para abarcar todas las fechas ocurridas hasta hoy seguro
129
+ if registraton_time.nil?
130
+ registraton_time = (Time.now() + 365*24*60*60)
131
+ else
132
+ registraton_time = registraton_time.to_time if registraton_time.class != Time
133
+ end
134
+ # move time to the first second of the next day.
135
+ # example: '2020-11-12 15:49:43 -0300' will be converted to '2020-11-13 00:00:00 -0300'
136
+ registraton_time = (Date.strptime(registraton_time.strftime("%Y-%m-%d"), "%Y-%m-%d").to_time + 24*60*60)
125
137
  # the movment must be a payment or a bonus
126
138
  raise 'Movement must be either a payment or a bonus' if self.type != MOVEMENT_TYPE_ADD_PAYMENT && self.type != MOVEMENT_TYPE_ADD_BONUS
127
139
  #puts
128
140
  #puts "product_code:#{self.product_code}:."
141
+
129
142
  # itero los pagos y bonos hechos por este mismo producto, desde el primer dia hasta este movimiento.
143
+ =begin
130
144
  paid = 0
131
145
  self.client.movements.select { |o|
132
146
  (o.type == MOVEMENT_TYPE_ADD_PAYMENT || o.type == MOVEMENT_TYPE_ADD_BONUS) &&
133
147
  o.credits.to_f < 0 &&
134
- o.product_code.upcase == self.product_code.upcase
148
+ o.product_code.upcase == self.product_code.upcase &&
149
+ o.create_time.to_time < registraton_time.to_time
135
150
  }.sort_by { |o| o.create_time }.each { |o|
136
151
  paid += (0.to_f - o.credits.to_f)
137
152
  break if o.id.to_guid == self.id.to_guid
138
153
  }
154
+ =end
155
+ paid = 0 - DB[
156
+ "select ISNULL(SUM(ISNULL(m.credits,0)),0) AS n " +
157
+ "from movement m with (nolock) " +
158
+ "where isnull(m.type, #{BlackStack::Movement::MOVEMENT_TYPE_ADD_PAYMENT.to_s}) in (#{BlackStack::Movement::MOVEMENT_TYPE_ADD_PAYMENT.to_s}, #{BlackStack::Movement::MOVEMENT_TYPE_ADD_BONUS.to_s}) " +
159
+ "and m.id_client='#{self.client.id.to_guid}' " +
160
+ "and isnull(m.credits,0) < 0 " +
161
+ "and upper(isnull(m.product_code, '')) = '#{self.product_code.upcase}' " +
162
+ "and m.create_time < '#{registraton_time.to_time.strftime('%Y-%m-%d')}' "
163
+ ].first[:n]
139
164
  #puts "paid:#{paid.to_s}:."
165
+
166
+ =begin
140
167
  # calculo los credito para este producto, desde el primer dia; incluyendo cosumo, expiraciones, ajustes.
141
168
  consumed = self.client.movements.select { |o|
142
169
  o.credits.to_f > 0 &&
143
- o.product_code.upcase == self.product_code.upcase
170
+ o.product_code.upcase == self.product_code.upcase &&
171
+ o.create_time.to_time < registraton_time.to_time
144
172
  }.inject(0) { |sum, o| sum.to_f + o.credits.to_f }.to_f
145
- #puts "consumed:#{consumed.to_s}:."
173
+ =end
174
+ consumed = DB[
175
+ "select ISNULL(SUM(ISNULL(m.credits,0)),0) AS n " +
176
+ "from movement m with (nolock) " +
177
+ "where m.id_client='#{self.client.id.to_guid}' " +
178
+ "and isnull(m.credits,0) > 0 " +
179
+ "and upper(isnull(m.product_code, '')) = '#{self.product_code.upcase}' " +
180
+ "and m.create_time < '#{registraton_time.to_time.strftime('%Y-%m-%d')}' "
181
+ ].first[:n]
182
+ #puts "consumed:#{consumed.to_s}:."
183
+
146
184
  # calculo los creditos de este movimiento que voy a cancelar
147
185
  credits = 0.to_f - self.credits.to_f
148
186
  #puts "credits:#{credits.to_s}:."
@@ -184,19 +222,28 @@ module BlackStack
184
222
 
185
223
  # credits expiration
186
224
  def expire(registraton_time=nil, desc='Expiration Because Allocation is Renewed')
187
- credits_consumed = self.credits_consumed
225
+ #puts
226
+ #puts "registraton_time:#{registraton_time.to_s}:."
227
+ credits_consumed = self.credits_consumed(registraton_time)
228
+ #puts "credits_consumed:#{credits_consumed.to_s}:."
188
229
  #
189
230
  self.expiration_start_time = now()
190
231
  self.expiration_tries = self.expiration_tries.to_i + 1
191
232
  self.save
192
233
  #
193
- total_credits = 0.to_f - BlackStack::Balance.new(self.client.id, self.product_code, registraton_time).credits.to_f
194
- total_amount = 0.to_f - BlackStack::Balance.new(self.client.id, self.product_code, registraton_time).amount.to_f
234
+ balance = BlackStack::Balance.new(self.client.id, self.product_code, registraton_time)
235
+ balance.calculate(false)
236
+ #puts "balance.credits.to_s:#{balance.credits.to_s}:."
237
+ #puts "balance.amount.to_s:#{balance.amount.to_s}:."
238
+ total_credits = 0.to_f - balance.credits.to_f
239
+ total_amount = 0.to_f - balance.amount.to_f
195
240
  #
196
241
  credits = 0.to_i - self.credits.to_i
197
242
  #
198
243
  credits_to_expire = credits - credits_consumed.to_i #- (0.to_f - self.credits.to_f)).to_i
199
244
  amount_to_expire = total_credits.to_f == 0 ? 0 : credits_to_expire.to_f * ( total_amount.to_f / total_credits.to_f )
245
+ #puts "credits_to_expire.to_s:#{credits_to_expire.to_s}:."
246
+ #puts "amount_to_expire.to_s:#{amount_to_expire.to_s}:."
200
247
  #
201
248
  exp = BlackStack::Movement.new
202
249
  exp.id = guid()
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: invoicing_payments_processing
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.59
4
+ version: 1.1.64
5
5
  platform: ruby
6
6
  authors:
7
7
  - Leandro Daniel Sardi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-11-11 00:00:00.000000000 Z
11
+ date: 2020-11-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket