invoicing_payments_processing 1.1.60 → 1.1.65

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa3e73d17b05d75e69025d7364460ee6045a46cb
4
- data.tar.gz: 2579bdc5324a9288e41fa92c9f2f84048f0c27f2
3
+ metadata.gz: a6c823ecb08c6b06064463c50e00f8a392d31db4
4
+ data.tar.gz: 019aad76d9430a5e5ca28a960de182dd03956e3a
5
5
  SHA512:
6
- metadata.gz: e3138f3c9a4721c504eba4376923717cf50af2382204020150d10fc480c3a6e536e6c09d7d56457d999aac9bcecf4b42388f2b686d91b5eb502291a4cbfed494
7
- data.tar.gz: e3ef838af52b803608c1349a3e31b682afefb846716570e7fe38ee8070f73f96aaeecbf665b7b8d8641ae405b41859d60ccd43d1624c585ed5d2f6baa50e9074
6
+ metadata.gz: c82e69dc914a4ccf5a84dcaa55ebedb065aef47405bb99b83c9546f6ced35d95b12fe88c0efc608a49c4c721225555282952baa2126b0a1d55df47b912bcfafa
7
+ data.tar.gz: ec65bbcc0118e99f08299bea1e71b3a663bbd91068a02c2cd14b87db5e91c7ae6de0db5ce9fe7b4dfcbf45c0265dc916ee27958601126ad031623d499035a94b
@@ -16,7 +16,7 @@ module BlackStack
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,30 @@ 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
+ i = 0
21
+ ret = []
22
+ BlackStack::Movement.where(:id_client=>self.id).each { |o|
23
+ ret << o
24
+ i += 1
25
+ if i == 1000
26
+ i = 0
27
+ GC.start
28
+ DB.disconnect
29
+ end
30
+ }
31
+ ret
32
+ end
10
33
 
11
34
  # how many minutes ago should have updated the table stat_balance with the amount and credits of this client, for each product.
12
35
  def stat_balance_delay_minutes
@@ -51,7 +74,9 @@ module BlackStack
51
74
  end
52
75
 
53
76
  # 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)
77
+ def consume(product_code, number_of_credits=1, description=nil, datetime=nil)
78
+ dt = datetime.nil? ? now() : datetime.to_time.to_sql
79
+
55
80
  # create the consumtion
56
81
  total_credits = 0.to_f - BlackStack::Balance.new(self.id, product_code).credits.to_f
57
82
  total_amount = 0.to_f - BlackStack::Balance.new(self.id, product_code).amount.to_f
@@ -60,7 +85,7 @@ module BlackStack
60
85
  cons = BlackStack::Movement.new
61
86
  cons.id = guid()
62
87
  cons.id_client = self.id
63
- cons.create_time = now()
88
+ cons.create_time = dt
64
89
  cons.type = BlackStack::Movement::MOVEMENT_TYPE_CANCELATION
65
90
  cons.description = description.nil? ? 'Consumption' : description
66
91
  cons.paypal1_amount = 0
@@ -135,7 +160,7 @@ module BlackStack
135
160
  credits_paid = 0
136
161
 
137
162
  #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
163
+ #total_amount = 0.to_f - BlackStack::Balance.new(self.id, product_code).amount.to_f
139
164
 
140
165
  self.movements.select { |o|
141
166
  o.product_code.upcase == product_code.upcase
@@ -121,28 +121,68 @@ 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
+ "and m.id <> '#{self.id.to_guid}' "
164
+ ].first[:n]
139
165
  #puts "paid:#{paid.to_s}:."
166
+
167
+ =begin
140
168
  # calculo los credito para este producto, desde el primer dia; incluyendo cosumo, expiraciones, ajustes.
141
169
  consumed = self.client.movements.select { |o|
142
170
  o.credits.to_f > 0 &&
143
- o.product_code.upcase == self.product_code.upcase
171
+ o.product_code.upcase == self.product_code.upcase &&
172
+ o.create_time.to_time < registraton_time.to_time
144
173
  }.inject(0) { |sum, o| sum.to_f + o.credits.to_f }.to_f
145
- #puts "consumed:#{consumed.to_s}:."
174
+ =end
175
+ consumed = DB[
176
+ "select ISNULL(SUM(ISNULL(m.credits,0)),0) AS n " +
177
+ "from movement m with (nolock) " +
178
+ "where m.id_client='#{self.client.id.to_guid}' " +
179
+ "and isnull(m.credits,0) > 0 " +
180
+ "and upper(isnull(m.product_code, '')) = '#{self.product_code.upcase}' " +
181
+ "and m.create_time < '#{registraton_time.to_time.strftime('%Y-%m-%d')}' " +
182
+ "and m.id <> '#{self.id.to_guid}' "
183
+ ].first[:n]
184
+ #puts "consumed:#{consumed.to_s}:."
185
+
146
186
  # calculo los creditos de este movimiento que voy a cancelar
147
187
  credits = 0.to_f - self.credits.to_f
148
188
  #puts "credits:#{credits.to_s}:."
@@ -184,7 +224,10 @@ module BlackStack
184
224
 
185
225
  # credits expiration
186
226
  def expire(registraton_time=nil, desc='Expiration Because Allocation is Renewed')
187
- credits_consumed = self.credits_consumed
227
+ #puts
228
+ #puts "registraton_time:#{registraton_time.to_s}:."
229
+ credits_consumed = self.credits_consumed(registraton_time)
230
+ #puts "credits_consumed:#{credits_consumed.to_s}:."
188
231
  #
189
232
  self.expiration_start_time = now()
190
233
  self.expiration_tries = self.expiration_tries.to_i + 1
@@ -192,6 +235,8 @@ module BlackStack
192
235
  #
193
236
  balance = BlackStack::Balance.new(self.client.id, self.product_code, registraton_time)
194
237
  balance.calculate(false)
238
+ #puts "balance.credits.to_s:#{balance.credits.to_s}:."
239
+ #puts "balance.amount.to_s:#{balance.amount.to_s}:."
195
240
  total_credits = 0.to_f - balance.credits.to_f
196
241
  total_amount = 0.to_f - balance.amount.to_f
197
242
  #
@@ -199,6 +244,8 @@ module BlackStack
199
244
  #
200
245
  credits_to_expire = credits - credits_consumed.to_i #- (0.to_f - self.credits.to_f)).to_i
201
246
  amount_to_expire = total_credits.to_f == 0 ? 0 : credits_to_expire.to_f * ( total_amount.to_f / total_credits.to_f )
247
+ #puts "credits_to_expire.to_s:#{credits_to_expire.to_s}:."
248
+ #puts "amount_to_expire.to_s:#{amount_to_expire.to_s}:."
202
249
  #
203
250
  exp = BlackStack::Movement.new
204
251
  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.60
4
+ version: 1.1.65
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