invoicing_payments_processing 1.1.56 → 1.1.62

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: bbf8d14ab09274cca1347069880dd42704276aa6
4
- data.tar.gz: fe4f4472a6e138fc6d5763bf1f3d10cfa5dfffa4
3
+ metadata.gz: 4274c11ebe44a85852da2cd471b1d2ccbd088266
4
+ data.tar.gz: b6b303e495c9ada9185cb9abc26d270debc6487f
5
5
  SHA512:
6
- metadata.gz: 0cf1c203a98b89cb889b6f3b81cda659a72ff25eef71b1c00797f7233bfff1908d119dde2bf3bf656395db258de309a96824b1eef205482f5ce7239a67f7a510
7
- data.tar.gz: beb36ff2205f605672c8905362feff35c42642f2390c12c95959a9a84bd0da3a698109928f32c0882e1f63b12420ba387e6f3fb3ad3fd6954afb8abf3db0eb66
6
+ metadata.gz: c144e34feb9b8ae90c4514a27e3fe9d042d1a50ca3ecd7577307e26dd8858c7689cf7dd8d647b8cf28193d33f51d7da8bcf057ffb29a0c9fb336de50dea5cab3
7
+ data.tar.gz: 187387a945ab22e3e38b1f35a5884b10bcddb50ba5b218b1730dce093ab67b751cb014b887c77bbd66b567515e3ac81390cd3bf6d65a4606994a5300a0862c9d
@@ -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
@@ -8,8 +8,52 @@ module BlackStack
8
8
  one_to_many :customplans, :class=>:'BlackStack::CustomPlan', :key=>:id_client
9
9
  one_to_many :movements, :class=>:'BlackStack::Movement', :key=>:id_client
10
10
 
11
+ # how many minutes ago should have updated the table stat_balance with the amount and credits of this client, for each product.
12
+ def stat_balance_delay_minutes
13
+ row = DB[
14
+ "SELECT TOP 1 m.id " +
15
+ "FROM client c WITH (NOLOCK) " +
16
+ "JOIN movement m WITH (NOLOCK INDEX(IX_movement__id_client__create_time_desc)) ON ( c.id=m.id_client AND m.create_time > ISNULL(c.last_stat_balance_update_time, '1900-01-01') ) " +
17
+ "WHERE c.id = '#{self.id}' " +
18
+ "ORDER BY m.create_time DESC "
19
+ ].first
20
+
21
+ if row.nil?
22
+ return 0
23
+ else
24
+ return DB["SELECT DATEDIFF(MI, m.create_time, GETDATE()) AS n FROM movement m WITH (NOLOCK) WHERE m.id='#{row[:id]}'"].first[:n]
25
+ end
26
+ end
27
+
28
+ # update the table stat_balance with the amount and credits of this client, for each product.
29
+ def update_stat_balance(product_code=nil)
30
+ c = self
31
+ product_descriptors = BlackStack::InvoicingPaymentsProcessing::products_descriptor.clone
32
+ product_descriptors.select! { |hprod| hprod[:code] == product_code } if !product_code.nil?
33
+ product_descriptors.each { |hprod|
34
+ row = DB[
35
+ "select isnull(sum(isnull(m.credits,0)),0) as credits, isnull(sum(isnull(m.amount,0)),0) as amount " +
36
+ # "from movement m with (nolock index(IX_movement__id_client__product_code)) " +
37
+ "from movement m with (nolock) " +
38
+ "where m.id_client='#{c.id}' " +
39
+ "and m.product_code='#{hprod[:code]}' "
40
+ ].first
41
+ credits = row[:credits]
42
+ amount = row[:amount]
43
+ row = DB["SELECT * FROM stat_balance WHERE id_client='#{c.id}' AND product_code='#{hprod[:code]}'"].first
44
+ if row.nil?
45
+ DB.execute("INSERT INTO stat_balance (id_client, product_code, amount, credits) VALUES ('#{c.id}', '#{hprod[:code]}', #{amount.to_s}, #{credits.to_s})")
46
+ else
47
+ DB.execute("UPDATE stat_balance SET amount=#{amount.to_s}, credits=#{credits.to_s} WHERE id_client='#{c.id}' AND product_code='#{hprod[:code]}'")
48
+ end
49
+ DB.execute("UPDATE client SET last_stat_balance_update_time=GETDATE() WHERE [id]='#{c.id}'")
50
+ }
51
+ end
52
+
11
53
  # 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.
12
- def consume(product_code, number_of_credits=1, description=nil)
54
+ def consume(product_code, number_of_credits=1, description=nil, datetime=nil)
55
+ dt = datetime.nil? ? now() : datetime.to_time.to_sql
56
+
13
57
  # create the consumtion
14
58
  total_credits = 0.to_f - BlackStack::Balance.new(self.id, product_code).credits.to_f
15
59
  total_amount = 0.to_f - BlackStack::Balance.new(self.id, product_code).amount.to_f
@@ -18,7 +62,7 @@ module BlackStack
18
62
  cons = BlackStack::Movement.new
19
63
  cons.id = guid()
20
64
  cons.id_client = self.id
21
- cons.create_time = now()
65
+ cons.create_time = dt
22
66
  cons.type = BlackStack::Movement::MOVEMENT_TYPE_CANCELATION
23
67
  cons.description = description.nil? ? 'Consumption' : description
24
68
  cons.paypal1_amount = 0
@@ -85,18 +85,7 @@ module BlackStack
85
85
  def allowedToAddRemoveItems?
86
86
  return (self.status == STATUS_UNPAID || self.status == nil) && (self.disabled_for_add_remove_items == false || self.disabled_for_add_remove_items == nil)
87
87
  end
88
-
89
- # envia un email transaccional con informacion de facturacion, y pasos a seguir despues del pago
90
- def notificationSubject()
91
- "We Received Your Payment"
92
- end
93
-
94
- #
95
- def notificationBody()
96
- "<p>We received your payment for a total of $#{("%.2f" % self.total()).to_s}.</p>" +
97
- "<p>You can find your invoice <a href='#{CS_HOME_PAGE}/member/invoice?iid=#{self.id.to_guid}'><b>here</b></a></p>"
98
- end
99
-
88
+
100
89
  #
101
90
  def number()
102
91
  self.id.to_guid
@@ -246,7 +235,7 @@ module BlackStack
246
235
  end
247
236
 
248
237
  # return url
249
- "#{BlackStack::InvoicingPaymentsProcessing::PAYPAL_ORDERS_URL}/cgi-bin/webscr?" + URI.encode_www_form(values)
238
+ "#{BlackStack::InvoicingPaymentsProcessing::paypal_orders_url}/cgi-bin/webscr?" + URI.encode_www_form(values)
250
239
  end
251
240
 
252
241
  # retorna true si el estado de la factura sea NULL o UNPAID
@@ -13,10 +13,19 @@ module BlackStack
13
13
 
14
14
  # static attributes
15
15
  @@paypal_business_email = "sardi.leandro.daniel@gmail.com"
16
+ @@paypal_orders_url = BlackStack::InvoicingPaymentsProcessing::PAYPAL_ORDERS_URL
17
+ @@paypal_ipn_listener = "#{BlackStack::Pampa::api_url.to_s}/api1.3/accounting/paypal/notify_new_invoice.json"
18
+
16
19
  @@products_descriptor = []
17
20
  @@plans_descriptor = []
18
-
21
+
19
22
  # getters & setters
23
+ def self.set_config(h)
24
+ @@paypal_business_email = h[:paypal_business_email]
25
+ @@paypal_orders_url = h[:paypal_orders_url]
26
+ @@paypal_ipn_listener = h[:paypal_ipn_listener]
27
+ end
28
+
20
29
  def self.set_paypal_business_email(email)
21
30
  @@paypal_business_email = email
22
31
  end # def self.set_paypal_business_email
@@ -25,10 +34,14 @@ module BlackStack
25
34
  @@paypal_business_email
26
35
  end # def self.set_paypal_business_email
27
36
 
28
- def self.paypal_ipn_listener()
29
- "#{BlackStack::Pampa::api_url.to_s}/api1.3/accounting/paypal/notify_new_invoice.json"
37
+ def self.paypal_orders_url()
38
+ @@paypal_orders_url
30
39
  end
31
40
 
41
+ def self.paypal_ipn_listener()
42
+ @@paypal_ipn_listener
43
+ end
44
+
32
45
 
33
46
  def self.set_products(h)
34
47
  @@products_descriptor = h
@@ -121,7 +121,12 @@ 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
+ registraton_time = (Time.now() + 365*24*60*60).to_time if registraton_time.nil?
125
130
  # the movment must be a payment or a bonus
126
131
  raise 'Movement must be either a payment or a bonus' if self.type != MOVEMENT_TYPE_ADD_PAYMENT && self.type != MOVEMENT_TYPE_ADD_BONUS
127
132
  #puts
@@ -131,7 +136,8 @@ module BlackStack
131
136
  self.client.movements.select { |o|
132
137
  (o.type == MOVEMENT_TYPE_ADD_PAYMENT || o.type == MOVEMENT_TYPE_ADD_BONUS) &&
133
138
  o.credits.to_f < 0 &&
134
- o.product_code.upcase == self.product_code.upcase
139
+ o.product_code.upcase == self.product_code.upcase &&
140
+ o.create_time.to_time <= registraton_time.to_time
135
141
  }.sort_by { |o| o.create_time }.each { |o|
136
142
  paid += (0.to_f - o.credits.to_f)
137
143
  break if o.id.to_guid == self.id.to_guid
@@ -140,7 +146,8 @@ module BlackStack
140
146
  # calculo los credito para este producto, desde el primer dia; incluyendo cosumo, expiraciones, ajustes.
141
147
  consumed = self.client.movements.select { |o|
142
148
  o.credits.to_f > 0 &&
143
- o.product_code.upcase == self.product_code.upcase
149
+ o.product_code.upcase == self.product_code.upcase &&
150
+ o.create_time.to_time <= registraton_time.to_time
144
151
  }.inject(0) { |sum, o| sum.to_f + o.credits.to_f }.to_f
145
152
  #puts "consumed:#{consumed.to_s}:."
146
153
  # calculo los creditos de este movimiento que voy a cancelar
@@ -184,19 +191,28 @@ module BlackStack
184
191
 
185
192
  # credits expiration
186
193
  def expire(registraton_time=nil, desc='Expiration Because Allocation is Renewed')
187
- credits_consumed = self.credits_consumed
194
+ #puts
195
+ #puts "registraton_time:#{registraton_time.to_s}:."
196
+ credits_consumed = self.credits_consumed(registraton_time)
197
+ #puts "credits_consumed:#{credits_consumed.to_s}:."
188
198
  #
189
199
  self.expiration_start_time = now()
190
200
  self.expiration_tries = self.expiration_tries.to_i + 1
191
201
  self.save
192
202
  #
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
203
+ balance = BlackStack::Balance.new(self.client.id, self.product_code, registraton_time)
204
+ balance.calculate(false)
205
+ #puts "balance.credits.to_s:#{balance.credits.to_s}:."
206
+ #puts "balance.amount.to_s:#{balance.amount.to_s}:."
207
+ total_credits = 0.to_f - balance.credits.to_f
208
+ total_amount = 0.to_f - balance.amount.to_f
195
209
  #
196
210
  credits = 0.to_i - self.credits.to_i
197
211
  #
198
212
  credits_to_expire = credits - credits_consumed.to_i #- (0.to_f - self.credits.to_f)).to_i
199
213
  amount_to_expire = total_credits.to_f == 0 ? 0 : credits_to_expire.to_f * ( total_amount.to_f / total_credits.to_f )
214
+ #puts "credits_to_expire.to_s:#{credits_to_expire.to_s}:."
215
+ #puts "amount_to_expire.to_s:#{amount_to_expire.to_s}:."
200
216
  #
201
217
  exp = BlackStack::Movement.new
202
218
  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.56
4
+ version: 1.1.62
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-06 00:00:00.000000000 Z
11
+ date: 2020-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket