invoicing_payments_processing 1.1.20 → 1.1.32

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: 9ebbf923439f8f516edcd98efc8f51baf0560b47
4
- data.tar.gz: fa76d9b89823901681c0488879fc0a6023c8b40c
3
+ metadata.gz: 767d1959445a933872a088246a96ce835a7aa1c1
4
+ data.tar.gz: a6d3dc28e802706eb1360daf4c5c16940e6a49c7
5
5
  SHA512:
6
- metadata.gz: 1ad3c562d56f170ce688bb4e6dd23dbfbdb8df2e89fcb3fe61fb057703778e084d5a96223a2c2aa2e6510a4208ce0d292077fadee91a5004a16a05e11d5e8519
7
- data.tar.gz: 718322b854e9fe1ecd512a8a557940c5930c19de3968472eebcda3dea30746a3ad67286f8953c58f88bf1c5214f882a53cb7c994f7e2507ea6375fa994ad5597
6
+ metadata.gz: 5ce9bd86e5bc541e6887b48c86c1086b854b0640a3e106881241a4285d0b660251c6987d9157ad803b60b865924fa65417a876f789bba45cb6475f190831011b
7
+ data.tar.gz: 06758180d0eaa6004a541fe5eb89a13b9590357acdbe1ccb45b3d198f9cfb097002ff664420dca02594672eb53133fa4d84746d6879769e83f6d0be9085d211e
@@ -331,6 +331,12 @@ module BlackStack
331
331
  s.id_client = c.id
332
332
  s.active = true
333
333
  s.save
334
+
335
+ # obtengo la factura que se creo con esta suscripcion
336
+ i = BlackStack::Invoice.where(:id=>b.item_number).first
337
+
338
+ # vinculo esta suscripcion a la factura que la genero, y a todas las facturas siguientes
339
+ i.set_subscription(s)
334
340
  end
335
341
 
336
342
  elsif (
@@ -384,22 +390,7 @@ module BlackStack
384
390
  k = BlackStack::Invoice.where(:id=>row[:id]).first
385
391
 
386
392
  # creo la factura por el reembolso
387
- i = BlackStack::Invoice.new()
388
- i.id = guid()
389
- i.id_client = c.id
390
- i.create_time = now()
391
- i.disabled_for_trial_ssm = c.disabled_for_trial_ssm
392
- i.id_buffer_paypal_notification = b.id
393
- i.status = BlackStack::Invoice::STATUS_REFUNDED
394
- i.billing_period_from = b.create_time
395
- i.billing_period_to = b.create_time
396
- i.paypal_url = nil
397
- i.disabled_for_add_remove_items = true
398
- i.save()
399
-
400
- # parseo el reeembolso - creo el registro contable
401
- i.setup_refund(payment_gross, k.id)
402
-
393
+ k.refund(payment_gross)
403
394
  end
404
395
  else
405
396
  # unknown
@@ -162,13 +162,65 @@ module BlackStack
162
162
  if from_time > to_time
163
163
  raise "From time must be earlier than To time"
164
164
  end
165
- if to_time.prev_year > from_time
166
- raise "There time frame cannot be longer than 1 year."
167
- end
165
+ #if to_time.prev_year > from_time
166
+ # raise "The time frame cannot be longer than 1 year."
167
+ #end
168
168
  to_time += 1
169
- ds = BlackStack::Movement.where(:id_client => self.id, :product_code=>product_code) if !product_code.nil?
170
- ds = BlackStack::Movement.where(:id_client => self.id) if product_code.nil?
171
- ds.where("create_time >= ? and create_time <= ?", from_time, to_time)
169
+ =begin
170
+ :id => movement.id,
171
+ :id_client => movement.id_client,
172
+ :product_code => movement.product_code,
173
+ :create_time => movement.create_time,
174
+ :type => movement.type.to_i,
175
+ :description => movement.description,
176
+ :paypal1_amount => movement.paypal1_amount.to_f,
177
+ :bonus_amount => movement.bonus_amount.to_f,
178
+ :amount => movement.amount.to_f,
179
+ :credits => movement.credits.to_f,
180
+ :profits_amount => movement.profits_amount.to_f,
181
+ :expiration_time => movement.expiration_time,
182
+ :expiration_description => movement.expiration_time.nil? ? '-' : ((movement.expiration_time - Time.now()).to_f / 60.to_f).to_i.to_time_spent
183
+ =end
184
+ q =
185
+ "SELECT " +
186
+ " m.id_client, " +
187
+ " YEAR(m.create_time) AS creation_year, " +
188
+ " MONTH(m.create_time) AS creation_month, " +
189
+ " DAY(m.create_time) AS creation_day, " +
190
+ " YEAR(m.expiration_time) AS expiration_year, " +
191
+ " MONTH(m.expiration_time) AS expiration_month, " +
192
+ " DAY(m.expiration_time) AS expiration_day, " +
193
+ " m.type, " +
194
+ " m.product_code, " +
195
+ " CAST(m.description AS VARCHAR(500)) AS description, " +
196
+ " CAST(m.expiration_description AS VARCHAR(500)) AS expiration_description, " +
197
+ " SUM(ISNULL(m.paypal1_amount,0)) AS paypal1_amount, " +
198
+ " SUM(ISNULL(m.bonus_amount,0)) AS bonus_amount, " +
199
+ " SUM(ISNULL(m.amount,0)) AS amount, " +
200
+ " SUM(ISNULL(m.credits,0)) AS credits, " +
201
+ " SUM(ISNULL(m.profits_amount,0)) AS profits_amount " +
202
+ "FROM movement m WITH (NOLOCK) " +
203
+ "WHERE m.id_client = '#{self.id}' "
204
+
205
+ q += "AND m.product_code = '#{product_code}' " if !product_code.nil?
206
+
207
+ q +=
208
+ "AND create_time >= '#{from_time.to_sql}' " +
209
+ "AND create_time <= '#{to_time.to_sql}' " +
210
+ "GROUP BY " +
211
+ " m.id_client, " +
212
+ " YEAR(m.create_time), " +
213
+ " MONTH(m.create_time), " +
214
+ " DAY(m.create_time), " +
215
+ " YEAR(m.expiration_time), " +
216
+ " MONTH(m.expiration_time), " +
217
+ " DAY(m.expiration_time), " +
218
+ " m.type, " +
219
+ " m.product_code, " +
220
+ " CAST(m.description AS VARCHAR(500)), " +
221
+ " CAST(m.expiration_description AS VARCHAR(500)) "
222
+
223
+ DB[q].all
172
224
  end
173
225
 
174
226
  #
@@ -10,10 +10,16 @@ module BlackStack
10
10
 
11
11
  many_to_one :buffer_paypal_notification, :class=>:'BlackStack::BufferPayPalNotification', :key=>:id_buffer_paypal_notification
12
12
  many_to_one :client, :class=>:'BlackStack::Client', :key=>:id_client
13
- many_to_one :paypal_subscription, :class=>:'BlackStack::PayPalSubscription', :key=>:id_paypal_subscription
13
+ #many_to_one :paypal_subscription, :class=>:'BlackStack::PayPalSubscription', :key=>:id_paypal_subscription
14
14
  many_to_one :previous, :class=>:'BlackStack::Invoice', :key=>:id_previous_invoice
15
15
  one_to_many :items, :class=>:'BlackStack::InvoiceItem', :key=>:id_invoice
16
16
 
17
+ def paypal_subscription
18
+ return nil if self.subscr_id.nil?
19
+ return nil if self.subscr_id.to_s.size == 0
20
+ return BlackStack::PayPalSubscription.where(:code=>self.subscr_id.to_s).first
21
+ end
22
+
17
23
  # compara 2 planes, y retorna TRUE si ambos pueden coexistir en una misma facutra, con un mismo enlace de PayPal
18
24
  def self.compatibility?(h, i)
19
25
  return false if h[:type]!=i[:type]
@@ -56,7 +62,19 @@ module BlackStack
56
62
  raise "Unknown Invoice Status (#{status.to_s})"
57
63
  end
58
64
  end
59
-
65
+
66
+ #
67
+ def set_subscription(s)
68
+ self.subscr_id = s.subscr_id
69
+ self.save
70
+ # aplico la mismam modificacion a todas las factuas que le siguieron a esta
71
+ BlackStack::Invoice.where(:id_previous_invoice=>self.id).all { |j|
72
+ j.set_subscription(s)
73
+ DB.disconnect
74
+ GC.start
75
+ }
76
+ end
77
+
60
78
  #
61
79
  def deserve_trial?
62
80
  return self.disabled_for_trial_ssm == false || self.disabled_for_trial_ssm == nil
@@ -234,6 +252,15 @@ module BlackStack
234
252
  def canBePaid?
235
253
  self.status == nil || self.status == BlackStack::Invoice::STATUS_UNPAID
236
254
  end
255
+
256
+ # retorna true si el estado de la factura sea NULL o UNPAID
257
+ def canBeDeleted?
258
+ if self.paypal_subscription.nil?
259
+ return self.status == nil || self.status == BlackStack::Invoice::STATUS_UNPAID
260
+ else # !self.paypal_subscription.nil?
261
+ return (self.status == nil || self.status == BlackStack::Invoice::STATUS_UNPAID) && !self.paypal_subscription.active
262
+ end # if self.paypal_subscription.nil?
263
+ end
237
264
 
238
265
  # actualiza el registro en la tabla invoice segun los parametros.
239
266
  # en este caso la factura se genera antes del pago.
@@ -286,6 +313,7 @@ module BlackStack
286
313
  end
287
314
  # marco la factura como pagada
288
315
  self.status = BlackStack::Invoice::STATUS_PAID
316
+ self.delete_time = nil
289
317
  self.save
290
318
  # expiracion de creditos de la factura anterior
291
319
  i = self.previous
@@ -313,19 +341,21 @@ module BlackStack
313
341
  # registro el pago
314
342
  BlackStack::Movement.new().parse(item, BlackStack::Movement::MOVEMENT_TYPE_ADD_PAYMENT, "Invoice Payment", payment_time, item.id).save()
315
343
  # agrego los bonos de este plan
316
- plan[:bonus_plans].each { |h|
317
- plan_bonus = BlackStack::InvoicingPaymentsProcessing.plan_descriptor(h[:item_number])
318
- raise "bonus plan not found" if plan_bonus.nil?
319
- bonus = BlackStack::InvoiceItem.new
320
- bonus.id = guid()
321
- bonus.id_invoice = self.id
322
- bonus.product_code = plan_bonus[:product_code]
323
- bonus.unit_price = 0
324
- bonus.units = plan_bonus[:credits]
325
- bonus.amount = 0
326
- bonus.item_number = plan_bonus[:item_number]
327
- BlackStack::Movement.new().parse(bonus, BlackStack::Movement::MOVEMENT_TYPE_ADD_BONUS, 'Payment Bonus', payment_time, item.id).save()
328
- }
344
+ if !plan[:bonus_plans].nil?
345
+ plan[:bonus_plans].each { |h|
346
+ plan_bonus = BlackStack::InvoicingPaymentsProcessing.plan_descriptor(h[:item_number])
347
+ raise "bonus plan not found" if plan_bonus.nil?
348
+ bonus = BlackStack::InvoiceItem.new
349
+ bonus.id = guid()
350
+ bonus.id_invoice = self.id
351
+ bonus.product_code = plan_bonus[:product_code]
352
+ bonus.unit_price = 0
353
+ bonus.units = plan_bonus[:credits] * item.number_of_packages # agrego los creditos del bono, multiplicado por la cantiad de paquetes
354
+ bonus.amount = 0
355
+ bonus.item_number = plan_bonus[:item_number]
356
+ BlackStack::Movement.new().parse(bonus, BlackStack::Movement::MOVEMENT_TYPE_ADD_BONUS, 'Payment Bonus', payment_time, item.id).save()
357
+ }
358
+ end # if !plan[:bonus_plans].nil?
329
359
  #
330
360
  DB.disconnect
331
361
  GC.start
@@ -371,62 +401,52 @@ module BlackStack
371
401
  end
372
402
  end
373
403
  end
374
-
404
+
375
405
  # configura la factura, segun el importe que pagara el cliente, la configuracion del plan en el descriptor h, y si el clietne merece un trial o no
376
- def create_item(item_number, n=1, validate_items_compatibility=true)
377
- #
378
- deserve_trial = self.deserve_trial?
379
-
380
- # busco el primer item de esta factura, si es que existe
381
- item0 = self.items.sort_by {|obj| obj.create_time}.first
382
-
383
- # numero de facturas previas a esta factura
384
- subs = BlackStack::PayPalSubscription.where(:subscr_id=>self.subscr_id).first
385
- nSubscriptionInvoices = 0 if subs.nil?
386
- nSubscriptionInvoices = subs.invoices.count if !subs.nil?
387
-
406
+ def create_item(item_number, n=1, validate_items_compatibility=true)
407
+ prev1 = self.previous
408
+ prev2 = prev1.nil? ? nil : prev1.previous
409
+
388
410
  # encuentro el descriptor del plan
389
411
  # el descriptor del plan tambien es necesario para la llamada a paypal_link
390
412
  h = self.client.plans.select { |j| j[:item_number].to_s == item_number.to_s }.first
391
413
 
392
414
  # mapeo variables
393
- c = self.client
394
415
  amount = 0.to_f
395
416
  unit_price = 0.to_f
396
417
  units = 0.to_i
397
-
398
- # decido si se trata de una suscripcion
399
- isSubscription = false
400
- isSubscription = true if h[:type] == "S"
401
-
418
+
402
419
  # le seteo la fecha de hoy
403
420
  self.billing_period_from = now()
404
-
405
- # si es una suscripcion, y
421
+ #puts
422
+ #puts
406
423
  # si el plan tiene un primer trial, y
407
- # es primer pago de esta suscripcion, entonces:
408
- # => se trata del primer pago por trial de esta suscripcion
409
- if (isSubscription && h[:trial_fee] != nil && deserve_trial)
424
+ # es la primer factura, entonces:
425
+ # => se trata del primer pago por trial
426
+ if h[:trial_fee] != nil && prev1.nil? && !self.disabled_for_trial_ssm
427
+ #puts 'a'
410
428
  units = h[:trial_credits].to_i
411
429
  unit_price = h[:trial_fee].to_f / h[:trial_credits].to_f
412
430
  billing_period_to = DB["SELECT DATEADD(#{h[:trial_period].to_s}, +#{h[:trial_units].to_s}, '#{self.billing_period_from.to_s}') AS [now]"].map(:now)[0].to_s
413
431
 
414
- # si es una suscripcion, y
415
432
  # si el plan tiene un segundo trial, y
416
- # es segundo pago de esta suscripcion, entonces:
417
- # => se trata del segundo pago por trial de esta suscripcion
418
- elsif (isSubscription && h[:trial2_fee] != nil && nSubscriptionInvoices == 1)
433
+ # es la segunda factura, entonces:
434
+ # => se trata del segundo pago por segundo trial
435
+ elsif h[:trial2_fee] != nil && !prev1.nil? && prev2.nil?
436
+ #puts 'b'
419
437
  units = h[:trial2_credits].to_i
420
438
  unit_price = h[:trial2_fee].to_f / h[:trial2_credits].to_f
421
439
  billing_period_to = DB["SELECT DATEADD(#{h[:trial2_period].to_s}, +#{h[:trial2_units].to_s}, '#{self.billing_period_from.to_s}') AS [now]"].map(:now)[0].to_s
422
440
 
423
441
  # si el plan tiene un fee, y
424
- elsif (isSubscription && h[:fee].to_f != nil)
442
+ elsif h[:fee].to_f != nil && h[:type] == BlackStack::InvoicingPaymentsProcessing::BasePlan::PAYMENT_SUBSCRIPTION
443
+ #puts 'c'
425
444
  units = n.to_i * h[:credits].to_i
426
445
  unit_price = h[:fee].to_f / h[:credits].to_f
427
446
  billing_period_to = DB["SELECT DATEADD(#{h[:period].to_s}, +#{h[:units].to_s}, '#{self.billing_period_from.to_s}') AS [now]"].map(:now)[0].to_s
428
447
 
429
- elsif (!isSubscription && h[:fee].to_f != nil)
448
+ elsif h[:fee].to_f != nil && h[:type] == BlackStack::InvoicingPaymentsProcessing::BasePlan::PAYMENT_PAY_AS_YOU_GO
449
+ #puts 'd'
430
450
  units = n.to_i * h[:credits].to_i
431
451
  unit_price = h[:fee].to_f / h[:credits].to_f
432
452
  billing_period_to = billing_period_from
@@ -435,7 +455,8 @@ module BlackStack
435
455
  raise "Plan is specified wrong"
436
456
 
437
457
  end
438
-
458
+
459
+
439
460
  #
440
461
  amount = units.to_f * unit_price.to_f
441
462
 
@@ -521,9 +542,9 @@ module BlackStack
521
542
  self.id_previous_invoice = i.id
522
543
  self.subscr_id = i.subscr_id
523
544
  self.disabled_for_add_remove_items = true
524
-
545
+
525
546
  i.items.each { |t|
526
- self.add_item(t.item_number)
547
+ self.add_item(t.item_number, t.number_of_packages)
527
548
  #
528
549
  DB.disconnect
529
550
  GC.start
@@ -657,6 +678,28 @@ module BlackStack
657
678
  DB.disconnect
658
679
  GC.start
659
680
  end # def setup_refund
660
-
681
+
682
+ def refund(amount)
683
+ c = self.client
684
+ # creo la factura por el reembolso
685
+ j = BlackStack::Invoice.new()
686
+ j.id = guid()
687
+ j.id_client = c.id
688
+ j.create_time = now()
689
+ j.disabled_for_trial_ssm = c.disabled_for_trial_ssm
690
+ j.id_buffer_paypal_notification = nil
691
+ j.status = BlackStack::Invoice::STATUS_REFUNDED
692
+ j.billing_period_from = self.billing_period_from
693
+ j.billing_period_to = self.billing_period_to
694
+ j.paypal_url = nil
695
+ j.disabled_for_add_remove_items = true
696
+ j.subscr_id = self.subscr_id
697
+ j.id_previous_invoice = self.id
698
+ j.save()
699
+
700
+ # parseo el reeembolso - creo el registro contable
701
+ j.setup_refund(amount, self.id)
702
+ end # refund
703
+
661
704
  end # class Invoice
662
705
  end # module BlackStack
@@ -7,5 +7,15 @@ module BlackStack
7
7
  BlackStack::InvoicingPaymentsProcessing::plan_descriptor(self.item_number)
8
8
  end
9
9
 
10
+ # Returns the number of plans ordered in this item
11
+ def number_of_packages()
12
+ plan = BlackStack::InvoicingPaymentsProcessing.plan_descriptor(self.item_number)
13
+ if self.amount.to_f == plan[:trial_fee].to_f || self.amount.to_f == plan[:trial2_fee].to_f
14
+ return 1.to_i
15
+ else
16
+ return (self.units.to_f / plan[:credits].to_f).to_i
17
+ end
18
+ end
19
+
10
20
  end # class LocalInvoiceItem
11
21
  end # module BlackStack
@@ -116,7 +116,7 @@ module BlackStack
116
116
  DB[
117
117
  "SELECT DISTINCT i.id " +
118
118
  "FROM invoice i WITH (NOLOCK) " +
119
- "JOIN buffer_paypal_notification b WITH (NOLOCK) ON (b.id=i.id_buffer_paypal_notification AND b.subscr_id='#{self.subscr_id.to_s}') "
119
+ "WHERE i.subscr_id='#{self.subscr_id.to_s}' "
120
120
  ].all { |row|
121
121
  a << BlackStack::Invoice.where(:id=>row[:id]).first
122
122
  # release resources
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.20
4
+ version: 1.1.32
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-08-01 00:00:00.000000000 Z
11
+ date: 2020-10-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: websocket