Class: BlackStack::Invoice
- Inherits:
-
Object
- Object
- BlackStack::Invoice
- Defined in:
- lib/invoice.rb
Constant Summary collapse
- STATUS_UNPAID =
0
- STATUS_PAID =
1
- STATUS_REFUNDED =
2
- PARAMS_FLOAT_MULTIPLICATION_FACTOR =
10000
Class Method Summary collapse
-
.compatibility?(h, i) ⇒ Boolean
compara 2 planes, y retorna TRUE si ambos pueden coexistir en una misma facutra, con un mismo enlace de PayPal.
-
.statusColor(status) ⇒ Object
retorna el valor del color HTML para un estado.
-
.statusDescription(status) ⇒ Object
retorna un string con el nombre descriptivo del estado.
-
.statuses ⇒ Object
retorna un array con la lista de estados posibles de una factura.
Instance Method Summary collapse
-
#add_item(item_number, n = 1) ⇒ Object
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.
- #allowedToAddRemoveItems? ⇒ Boolean
- #billingPeriodFromDesc ⇒ Object
- #billingPeriodToDesc ⇒ Object
-
#canBeDeleted? ⇒ Boolean
retorna true si el estado de la factura sea NULL o UNPAID.
-
#canBePaid? ⇒ Boolean
retorna true si el estado de la factura sea NULL o UNPAID.
-
#check_create_item(item_number, validate_items_compatibility = true, amount = nil) ⇒ Object
Verify if I can add this item_number to this invoice.
-
#create_item(item_number, n = 1, validate_items_compatibility = true) ⇒ Object
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.
- #dateDesc ⇒ Object
- #deserve_trial? ⇒ Boolean
- #dueDateDesc ⇒ Object
-
#getPaid(payment_time = nil) ⇒ Object
Los registros en la table de movimientos se registraran con la fecha del parametro sql_payment_datetime.
-
#next(i) ⇒ Object
actualiza el registro en la tabla invoice como las siguiente factura.
- #number ⇒ Object
-
#parse(b) ⇒ Object
actualiza el registro en la tabla invoice segun un registro en la tabla buffer_paypal_notification.
-
#paypal_link ⇒ Object
TODO: refactor me.
- #paypal_subscription ⇒ Object
- #plan_payment_description(h) ⇒ Object
-
#refund(amount) ⇒ Object
def setup_refund.
-
#remove_item(item_id) ⇒ Object
add_item.
- #set_subscription(s) ⇒ Object
-
#setup ⇒ Object
actualiza el registro en la tabla invoice segun los parametros.
-
#setup_refund(payment_gross, id_invoice) ⇒ Object
Genera lis items de la factura de reembolso.
- #total ⇒ Object
- #totalDesc ⇒ Object
Class Method Details
.compatibility?(h, i) ⇒ Boolean
compara 2 planes, y retorna TRUE si ambos pueden coexistir en una misma facutra, con un mismo enlace de PayPal
24 25 26 27 28 29 30 31 32 33 |
# File 'lib/invoice.rb', line 24 def self.compatibility?(h, i) return false if h[:type]!=i[:type] return false if h[:type]=='S' && h[:type]==i[:type] && h[:period]!=i[:period] return false if h[:type]=='S' && h[:type]==i[:type] && h[:units]!=i[:units] return false if h[:type]=='S' && h[:type]==i[:type] && h[:trial_period]!=i[:trial_period] return false if h[:type]=='S' && h[:type]==i[:type] && h[:trial_units]!=i[:trial_units] return false if h[:type]=='S' && h[:type]==i[:type] && h[:trial2_period]!=i[:trial2_period] return false if h[:type]=='S' && h[:type]==i[:type] && h[:trial2_units]!=i[:trial2_units] true end |
.statusColor(status) ⇒ Object
retorna el valor del color HTML para un estado
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/invoice.rb', line 54 def self.statusColor(status) if status == STATUS_UNPAID || status == nil return "red" elsif status == STATUS_PAID return "green" elsif status == STATUS_REFUNDED return "brown" else raise "Unknown Invoice Status (#{status.to_s})" end end |
.statusDescription(status) ⇒ Object
retorna un string con el nombre descriptivo del estado
41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/invoice.rb', line 41 def self.statusDescription(status) if status == STATUS_UNPAID || status == nil return "UNPAID" elsif status == STATUS_PAID return "PAID" elsif status == STATUS_REFUNDED return "REFUNDED" else raise "Unknown Invoice Status (#{status.to_s})" end end |
.statuses ⇒ Object
retorna un array con la lista de estados posibles de una factura
36 37 38 |
# File 'lib/invoice.rb', line 36 def self.statuses() [STATUS_UNPAID, STATUS_PAID, STATUS_REFUNDED] end |
Instance Method Details
#add_item(item_number, n = 1) ⇒ Object
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
500 501 502 503 504 505 506 507 508 509 510 |
# File 'lib/invoice.rb', line 500 def add_item(item_number, n=1) # creo el item item1 = self.create_item(item_number, n) item1.save() # agrega el item al array de la factura self.items << item1 # reconfiguro la factura self.setup() end |
#allowedToAddRemoveItems? ⇒ Boolean
85 86 87 |
# File 'lib/invoice.rb', line 85 def allowedToAddRemoveItems? return (self.status == STATUS_UNPAID || self.status == nil) && (self.disabled_for_add_remove_items == false || self.disabled_for_add_remove_items == nil) end |
#billingPeriodFromDesc ⇒ Object
105 106 107 |
# File 'lib/invoice.rb', line 105 def billingPeriodFromDesc() Date.strptime(self.billing_period_from.to_s, '%Y-%m-%d').strftime('%b %d, %Y') end |
#billingPeriodToDesc ⇒ Object
110 111 112 |
# File 'lib/invoice.rb', line 110 def billingPeriodToDesc() Date.strptime(self.billing_period_to.to_s, '%Y-%m-%d').strftime('%b %d, %Y') end |
#canBeDeleted? ⇒ Boolean
retorna true si el estado de la factura sea NULL o UNPAID
255 256 257 258 259 260 261 |
# File 'lib/invoice.rb', line 255 def canBeDeleted? if self.paypal_subscription.nil? return self.status == nil || self.status == BlackStack::Invoice::STATUS_UNPAID else # !self.paypal_subscription.nil? return (self.status == nil || self.status == BlackStack::Invoice::STATUS_UNPAID) && !self.paypal_subscription.active end # if self.paypal_subscription.nil? end |
#canBePaid? ⇒ Boolean
retorna true si el estado de la factura sea NULL o UNPAID
250 251 252 |
# File 'lib/invoice.rb', line 250 def canBePaid? self.status == nil || self.status == BlackStack::Invoice::STATUS_UNPAID end |
#check_create_item(item_number, validate_items_compatibility = true, amount = nil) ⇒ Object
Verify if I can add this item_number to this invoice. Otherwise, it raise an exception.
Si el atributo ‘amount’ ademas es distinto a nil, se filtran items por ese monto.
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/invoice.rb', line 373 def check_create_item(item_number, validate_items_compatibility=true, amount=nil) # busco el primer item de esta factura, si es que existe item0 = self.items.sort_by {|obj| obj.create_time}.first # encuentro el descriptor del plan # el descriptor del plan tambien es necesario para la llamada a paypal_link h = self.client.plans.select { |j| j[:item_number].to_s == item_number.to_s }.first if h == nil raise "Unknown item_number" end # returna true si el plan es una oferta one-time, # => y ya existe una item de factura asignado a # => este plan, con un importe igual al del trial1 # => o trial2. if h[:one_time_offer] == true if self.client.has_item(h[:item_number], amount) && h[:fee].to_f != amount.to_f raise "The plan is a one-time offer and you already have it included in an existing invoice" end end # si la factura ya tiene un item if !item0.nil? plan_descriptor = self.client.plans.select { |j| j[:item_number].to_s == item0.item_number.to_s }.first if plan_descriptor.nil? raise "Plan '#{item0.item_number.to_s}' not found" end # valido que los items sean compatibles if !BlackStack::Invoice.compatibility?(h, plan_descriptor) && validate_items_compatibility==true raise "Incompatible Items" end end end |
#create_item(item_number, n = 1, validate_items_compatibility = true) ⇒ Object
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
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 |
# File 'lib/invoice.rb', line 409 def create_item(item_number, n=1, validate_items_compatibility=true) prev1 = self.previous prev2 = prev1.nil? ? nil : prev1.previous # encuentro el descriptor del plan # el descriptor del plan tambien es necesario para la llamada a paypal_link h = self.client.plans.select { |j| j[:item_number].to_s == item_number.to_s }.first # mapeo variables amount = 0.to_f unit_price = 0.to_f units = 0.to_i # le seteo la fecha de hoy self.billing_period_from = now() #puts #puts # si el plan tiene un primer trial, y # es la primer factura, entonces: # => se trata del primer pago por trial if h[:trial_fee] != nil && prev1.nil? && !self.disabled_for_trial_ssm #puts 'a' units = h[:trial_credits].to_i unit_price = h[:trial_fee].to_f / h[:trial_credits].to_f 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 # si el plan tiene un segundo trial, y # es la segunda factura, entonces: # => se trata del segundo pago por segundo trial elsif h[:trial2_fee] != nil && !prev1.nil? && prev2.nil? #puts 'b' units = h[:trial2_credits].to_i unit_price = h[:trial2_fee].to_f / h[:trial2_credits].to_f 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 # si el plan tiene un fee, y elsif h[:fee].to_f != nil && h[:type] == BlackStack::InvoicingPaymentsProcessing::BasePlan::PAYMENT_SUBSCRIPTION #puts 'c' units = n.to_i * h[:credits].to_i unit_price = h[:fee].to_f / h[:credits].to_f 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 elsif h[:fee].to_f != nil && h[:type] == BlackStack::InvoicingPaymentsProcessing::BasePlan::PAYMENT_PAY_AS_YOU_GO #puts 'd' units = n.to_i * h[:credits].to_i unit_price = h[:fee].to_f / h[:credits].to_f billing_period_to = billing_period_from else # se hace un prorrateo raise "Plan is specified wrong" end # amount = units.to_f * unit_price.to_f # valido si puedo agregar este item self.check_create_item(item_number, validate_items_compatibility, amount) # cuardo la factura en la base de datos self.billing_period_to = billing_period_to self.save() # creo el item por el producto LGB2 item1 = BlackStack::InvoiceItem.new() item1.id = guid() item1.id_invoice = self.id item1.product_code = h[:product_code] item1.unit_price = unit_price.to_f item1.units = units.to_i item1.amount = amount.to_f item1.item_number = h[:item_number] item1.description = h[:name] item1.detail = plan_payment_description(h) # return item1 end |
#dateDesc ⇒ Object
95 96 97 |
# File 'lib/invoice.rb', line 95 def dateDesc() self.create_time.strftime('%b %d, %Y') end |
#deserve_trial? ⇒ Boolean
80 81 82 |
# File 'lib/invoice.rb', line 80 def deserve_trial? return self.disabled_for_trial_ssm == false || self.disabled_for_trial_ssm == nil end |
#dueDateDesc ⇒ Object
100 101 102 |
# File 'lib/invoice.rb', line 100 def dueDateDesc() Date.strptime(self.billing_period_from.to_s, '%Y-%m-%d').strftime('%b %d, %Y') end |
#getPaid(payment_time = nil) ⇒ Object
Los registros en la table de movimientos se registraran con la fecha del parametro sql_payment_datetime. Las fechas de expiracion de los movimientos se calculan seguin la fecha del pago.
sql_payment_datetime: Fecha-hora del pago. Por defecto es la fecha-hora actual.
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
# File 'lib/invoice.rb', line 306 def getPaid(payment_time=nil) payment_time = Time.now() if payment_time.nil? if self.canBePaid? == false raise "Method BlackStack::Invoice::getPaid requires the current status is nil or unpaid." end # marco la factura como pagada self.status = BlackStack::Invoice::STATUS_PAID self.delete_time = nil if self.previous.nil? # si es la primer factura de una suscripcion, o no es una suscripcion diff = payment_time.to_time - self.billing_period_from.to_time self.billing_period_from = payment_time self.billing_period_to = self.billing_period_to.to_time + diff end self.save # expiracion de creditos de la factura anterior i = self.previous if !i.nil? InvoiceItem.where(:id_invoice=>i.id).all { |item| # BlackStack::Movement.where(:id_invoice_item => item.id).all { |mov| # mov.expire(self.billing_period_from, "Expiration of <a href='/settings/record?rid=#{mov.id.to_guid}'>record:#{mov.id.to_guid}</a> because subscription renewal.") if mov.expiration_on_next_payment == true # DB.disconnect GC.start } # BlackStack::Movement.where(:id_invoice_item => item.id).all { |mov| # DB.disconnect GC.start } # InvoiceItem.where(:id_invoice=>i.id).all { |item| end # registro los asientos contables InvoiceItem.where(:id_invoice=>self.id).all { |item| # obtengo descriptor del plan plan = BlackStack::InvoicingPaymentsProcessing.plan_descriptor(item.item_number) # obtengo descriptor del producto prod = BlackStack::InvoicingPaymentsProcessing.product_descriptor(plan[:product_code]) # registro el pago BlackStack::Movement.new().parse(item, BlackStack::Movement::MOVEMENT_TYPE_ADD_PAYMENT, "Payment of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.", payment_time, item.id).save() # agrego los bonos de este plan if !plan[:bonus_plans].nil? plan[:bonus_plans].each { |h| plan_bonus = BlackStack::InvoicingPaymentsProcessing.plan_descriptor(h[:item_number]) raise "bonus plan not found" if plan_bonus.nil? bonus = BlackStack::InvoiceItem.new bonus.id = guid() bonus.id_invoice = self.id bonus.product_code = plan_bonus[:product_code] bonus.unit_price = 0 bonus.units = plan_bonus[:credits] * item.number_of_packages # agrego los creditos del bono, multiplicado por la cantiad de paquetes bonus.amount = 0 bonus.item_number = plan_bonus[:item_number] BlackStack::Movement.new().parse(bonus, BlackStack::Movement::MOVEMENT_TYPE_ADD_BONUS, "Bonus from the <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.", payment_time, item.id).save() } end # if !plan[:bonus_plans].nil? # DB.disconnect GC.start } end |
#next(i) ⇒ Object
actualiza el registro en la tabla invoice como las siguiente factura. en este caso la factura se genera antes del pago. crea uno o mas registros en la tabla invoice_item.
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 |
# File 'lib/invoice.rb', line 520 def next(i) # b = i.buffer_paypal_notification # if b == nil # raise "Method BlackStack::Invoice::next requires the previous invoice (i) is linked to a record in the table buffer_paypal_notification." # end id_client = i.id_client c = BlackStack::Client.where(:id=>id_client).first if c==nil raise "Client not found" end h = BlackStack::InvoicingPaymentsProcessing::plans_descriptor.select { |obj| obj[:item_number].to_s == i.items.first.item_number.to_s }.first if h==nil raise "Plan not found" end return_path = h[:return_path] if (self.id == nil) self.id = guid() end self.create_time = now() self.id_client = c.id self.id_buffer_paypal_notification = nil self.id_previous_invoice = i.id self.subscr_id = i.subscr_id self.disabled_for_add_remove_items = true i.items.each { |t| self.add_item(t.item_number, t.number_of_packages) # DB.disconnect GC.start } self.billing_period_from = i.billing_period_to self.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 self.paypal_url = self.paypal_link self.paypal_url = nil if h[:type] == "S" # si se trata de una suscripcion, entonces esta factura se pagara automaticamente self.automatic_billing = 1 if h[:type] == "S" # si se trata de una suscripcion, entonces esta factura se pagara automaticamente self.save end |
#number ⇒ Object
90 91 92 |
# File 'lib/invoice.rb', line 90 def number() self.id.to_guid end |
#parse(b) ⇒ Object
actualiza el registro en la tabla invoice segun un registro en la tabla buffer_paypal_notification. en este caso la factura se genera despues del pago. crea uno o mas registros en la tabla invoice_item.
569 570 571 572 573 574 575 576 577 578 579 |
# File 'lib/invoice.rb', line 569 def parse(b) item_number = b.item_number payment_gross = b.payment_gross.to_f billing_from = b.create_time #isSubscription = b.isSubscription? c = b.get_client if (c==nil) raise "Client not found" end self.setup(item_number, c.id, payment_gross, billing_from) end |
#paypal_link ⇒ Object
TODO: refactor me
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/invoice.rb', line 132 def paypal_link() item = self.items.first if item.nil? raise "Invoice has no items" end plan_descriptor = self.client.plans.select { |j| j[:item_number].to_s == item.item_number.to_s }.first if plan_descriptor.nil? raise "Plan not found" end product_descriptor = BlackStack::InvoicingPaymentsProcessing::products_descriptor.select { |j| j[:code].to_s == plan_descriptor[:product_code].to_s }.first if product_descriptor.nil? raise "Product not found" end item_name = "" n = 0 self.items.each { |t| h = BlackStack::InvoicingPaymentsProcessing::plans_descriptor.select { |obj| obj[:item_number].to_s == t.item_number.to_s }.first item_name += '; ' if n>0 item_name += h[:name] if item_name.size >= 65 item_name += "..." break end n+=1 } return_path = product_descriptor[:return_path] id_invoice = self.id id_client = self.client.id allow_trials = self.deserve_trial? bIsSubscription = false bIsSubscription = true if plan_descriptor[:type]=="S" # generating the invoice number invoice_id = "#{id_client.to_guid}.#{id_invoice.to_guid}" values = {} # common parameters for all the situations values[:business] = BlackStack::InvoicingPaymentsProcessing::paypal_business_email values[:lc] = "en_US" if bIsSubscription values[:cmd] = "_xclick-subscriptions" else values[:cmd] = "_xclick" end values[:upload] = 1 values[:no_shipping] = 1 values[:return] = BlackStack::Netting::add_param(return_path, "track_object_id", id_invoice.to_guid) values[:return_url] = BlackStack::Netting::add_param(return_path, "track_object_id", id_invoice.to_guid) values[:rm] = 1 values[:notify_url] = BlackStack::InvoicingPaymentsProcessing::paypal_ipn_listener values[:invoice] = id_invoice values[:item_name] = item_name values[:item_number] = id_invoice.to_s values[:src] = '1' # si es una suscripcion if (bIsSubscription) trial1 = allow_trials && plan_descriptor[:trial_fee]!=nil && plan_descriptor[:trial_period]!=nil && plan_descriptor[:trial_units]!=nil trial2 = allow_trials && plan_descriptor[:trial2_fee]!=nil && plan_descriptor[:trial2_period]!=nil && plan_descriptor[:trial2_units]!=nil values[:a3] = 0 self.items.each { |i| puts puts puts "i.id.to_s:#{i.id.to_s}:." puts "i.id_invoice.to_s:#{i.id_invoice.to_s}:." puts "i.units.to_i.to_s:#{i.units.to_i.to_s}:." puts "i.plan_descriptor[:trial_credits].to_i.to_s:#{i.plan_descriptor[:trial_credits].to_i.to_s}:." puts puts if trial1 && i.units.to_i!=i.plan_descriptor[:trial_credits].to_i raise 'Cannot order more than 1 package and trial in the same invoice' elsif trial1 values[:a3] += i.plan_descriptor[:fee].to_f else # !trial1 values[:a3] += ( i.units.to_f * ( i.plan_descriptor[:fee].to_f / i.plan_descriptor[:credits].to_f ).to_f ) end } values[:p3] = plan_descriptor[:units] # every 1 values[:t3] = plan_descriptor[:period] # per month # si tiene un primer periodo de prueba if trial1 values[:a1] = 0 # $1 fee self.items.each { |i| values[:a1] += i.plan_descriptor[:trial_fee].to_i } values[:p1] = plan_descriptor[:trial_units] # 15 values[:t1] = plan_descriptor[:trial_period] # days # si tiene un segundo periodo de prueba if trial2 values[:a2] = 0 # $50 fee self.items.each { |i| values[:a2] += i.plan_descriptor[:trial2_fee].to_i } values[:p2] = plan_descriptor[:trial2_units] # first 1 values[:t2] = plan_descriptor[:trial2_period] # month end end # sino, entonces es un pago por unica vez else values[:amount] = 0 self.items.each { |i| values[:amount] += i.amount.to_f } end # return url "#{BlackStack::InvoicingPaymentsProcessing::paypal_orders_url}/cgi-bin/webscr?" + URI.encode_www_form(values) end |
#paypal_subscription ⇒ Object
17 18 19 20 21 |
# File 'lib/invoice.rb', line 17 def paypal_subscription return nil if self.subscr_id.nil? return nil if self.subscr_id.to_s.size == 0 return BlackStack::PayPalSubscription.where(:subscr_id=>self.subscr_id.to_s).first end |
#plan_payment_description(h) ⇒ Object
489 490 491 492 493 494 495 496 497 |
# File 'lib/invoice.rb', line 489 def plan_payment_description(h) ret = "" ret += "$#{h[:trial_fee]} trial. " if self.deserve_trial? && !h[:trial_fee].nil? ret += "$#{h[:trial2_fee]} one-time price. " if self.deserve_trial? && !h[:trial2_fee].nil? ret += "$#{h[:fee]}/#{h[:period]}. " if h[:units].to_i <= 1 ret += "$#{h[:fee]}/#{h[:units]}#{h[:period]}. " if h[:units].to_i > 1 ret += "#{h[:credits]} credits. " if h[:credits].to_i > 1 ret end |
#refund(amount) ⇒ Object
def setup_refund
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
# File 'lib/invoice.rb', line 743 def refund(amount) c = self.client # creo la factura por el reembolso j = BlackStack::Invoice.new() j.id = guid() j.id_client = c.id j.create_time = now() j.disabled_for_trial_ssm = c.disabled_for_trial_ssm j.id_buffer_paypal_notification = nil j.status = BlackStack::Invoice::STATUS_REFUNDED j.billing_period_from = self.billing_period_from j.billing_period_to = self.billing_period_to j.paypal_url = nil j.disabled_for_add_remove_items = true j.subscr_id = self.subscr_id j.id_previous_invoice = self.id j.save() # parseo el reeembolso - creo el registro contable j.setup_refund(amount, self.id) end |
#remove_item(item_id) ⇒ Object
add_item
512 513 514 515 |
# File 'lib/invoice.rb', line 512 def remove_item(item_id) DB.execute("DELETE invoice_item WHERE id_invoice='#{self.id}' AND [id]='#{item_id}'") self.setup end |
#set_subscription(s) ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/invoice.rb', line 67 def set_subscription(s) self.subscr_id = s.subscr_id self.delete_time = nil self.save # aplico la mismam modificacion a todas las factuas que le siguieron a esta BlackStack::Invoice.where(:id_previous_invoice=>self.id).all { |j| j.set_subscription(s) DB.disconnect GC.start } end |
#setup ⇒ Object
actualiza el registro en la tabla invoice segun los parametros. en este caso la factura se genera antes del pago. genera el enlace de paypal.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 |
# File 'lib/invoice.rb', line 266 def setup() # busco el primer item de esta factura item = self.items.sort_by {|obj| obj.create_time}.first if item == nil raise "Invoice has no items." end h = self.client.plans.select { |j| j[:item_number].to_s == item.item_number.to_s }.first if h == nil raise "Unknown item_number." end # return_path = h[:return_path] c = BlackStack::Client.where(:id=>id_client).first if (c==nil) raise "Client not found" end if (self.id == nil) self.id = guid() end self.create_time = now() self.id_client = c.id self.id_buffer_paypal_notification = nil self.paypal_url = self.paypal_link # self.save() end |
#setup_refund(payment_gross, id_invoice) ⇒ Object
Genera lis items de la factura de reembolso. payment_gross: es el tital reembolsado id_invoice: es el ID de la factura que se estĂ¡ reembolsando
Si el monto del reembolso es mayor al total de la factua, entocnes se levanta una excepcion
Si existe un item, y solo uno, con importe igual al reembolso, entonces se aplica todo el reembolso a ese item. Y termina la funcion. Si existen mas de un item con igual importe que el reembolso, entonces se levanta una excepcion.
Si el monto del reembolso es igual al total de la factura, se hace un reembolso total de todos los items. Y termina la funcion. Si la factura tiene un solo item, entonces se calcula un reembolso parcial. Sino, entonces se levanta una excepcion.
TODO: Hacerlo transaccional
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 |
# File 'lib/invoice.rb', line 595 def setup_refund(payment_gross, id_invoice) # cargo la factura i = BlackStack::Invoice.where(:id=>id_invoice).first raise "Invoice not found (#{id_invoice})" if i.nil? # obtengo el total de la factura total = i.total.to_f # Si existe un item, y solo uno, con importe igual al reembolso, entonces se aplica todo el reembolso a ese item. Y termina la funcion. # Si existen mas de un item con igual importe que el reembolso, entonces se levanta una excepcion. matched_items = i.items.select { |o| o.amount.to_f == -payment_gross.to_f } # if total < -payment_gross raise "The refund is higher than the invoice amount (invoice #{id_invoice}, #{total.to_s}, #{payment_gross.to_s})" # Si el monto del reembolso es igual al total de la factura, se hace un reembolso total de todos los items. Y termina la funcion. # Si el monto de la factura es distinto al moneto del reembolso, entonces se levanta una excepcion. elsif total == -payment_gross i.items.each { |u| h = BlackStack::InvoicingPaymentsProcessing::plans_descriptor.select { |obj| obj[:item_number] == u.item_number }.first raise "Plan not found" if h.nil? item1 = BlackStack::InvoiceItem.new() item1.id = guid() item1.id_invoice = self.id item1.unit_price = u.unit_price.to_f item1.units = -u.units item1.amount = -u.amount.to_f item1.product_code = u.product_code.to_s item1.item_number = u.item_number.to_s item1.detail = u.detail.to_s item1.description = u.description.to_s item1.save() # hago el reembolso de este item # si el balance quedo en negativo, entonces aplico otro ajuste BlackStack::Movement.new().parse(item1, BlackStack::Movement::MOVEMENT_TYPE_REFUND_BALANCE, "Full Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.").save() net_amount = 0.to_f - BlackStack::Balance.new(self.client.id, u.product_code.to_s).amount.to_f net_credits = 0.to_f - BlackStack::Balance.new(self.client.id, u.product_code.to_s).credits.to_f if net_amount <= 0 && net_credits < 0 adjust = self.client.adjustment(u.product_code.to_s, net_amount, net_credits, "Adjustment for Negative Balance After Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.") adjust.id_invoice_item = item1.id adjust.save end # if net_amount < 0 # hago el reembolso de cada bono de este item # si el balance quedo en negativo, entonces aplico otro ajuste if !h[:bonus_plans].nil? h[:bonus_plans].each { |bonus| i = BlackStack::InvoicingPaymentsProcessing::plans_descriptor.select { |obj| obj[:item_number] == bonus[:item_number] }.first j = BlackStack::InvoicingPaymentsProcessing::products_descriptor.select { |obj| obj[:code] == i[:product_code] }.first item2 = BlackStack::InvoiceItem.new() item2.id = guid() item2.id_invoice = self.id item2.unit_price = 0 item2.units = -i[:credits] item2.amount = 0 item2.product_code = i[:product_code].to_s item2.item_number = i[:item_number].to_s item2.detail = 'Bonus Refund' item2.description = j[:description].to_s item2.save() BlackStack::Movement.new().parse(item2, BlackStack::Movement::MOVEMENT_TYPE_REFUND_BALANCE, "Bonus Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.").save() net_amount = 0.to_f - BlackStack::Balance.new(self.client.id, i[:product_code].to_s).amount.to_f net_credits = 0.to_f - BlackStack::Balance.new(self.client.id, i[:product_code].to_s).credits.to_f if net_amount <= 0 && net_credits < 0 adjust = self.client.adjustment(i[:product_code].to_s, net_amount, net_credits, "Adjustment for Negative Balance After Bonus Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.") adjust.id_invoice_item = item1.id adjust.save end # if net_amount < 0 } end # if !h[:bonus_plans].nil? # release resources DB.disconnect GC.start } # i.items.each { |u| # reembolso parcial de una factura con un unico item elsif i.items.size == 1 # and we know that: total < -payment_gross, so it is a partial refund t = i.items.first # amount = -payment_gross.to_f unit_price = t.amount.to_f / t.units.to_f float_units = (amount / unit_price.to_f) units = float_units.round.to_i # h = BlackStack::InvoicingPaymentsProcessing::plans_descriptor.select { |obj| obj[:item_number] == t.item_number }.first raise "Plan not found" if h.nil? item1 = BlackStack::InvoiceItem.new() item1.id = guid() item1.id_invoice = self.id item1.unit_price = unit_price.to_f item1.units = -units item1.amount = -amount.to_f item1.product_code = t.product_code.to_s item1.item_number = t.item_number.to_s item1.detail = t.detail.to_s item1.description = t.description.to_s item1.save() BlackStack::Movement.new().parse(item1, BlackStack::Movement::MOVEMENT_TYPE_REFUND_BALANCE, "Partial Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.").save() # agrego un ajuste por el redondeo a una cantidad entera de creditos if float_units.to_f != units.to_f adjustment_amount = unit_price.to_f * (units.to_f - float_units.to_f) adjust = self.client.adjustment(t.product_code.to_s, adjustment_amount, 0, "Adjustment for Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.", BlackStack::Movement::MOVEMENT_TYPE_REFUND_ADJUSTMENT) adjust.id_invoice_item = item1.id adjust.save end # si el balance quedo en negativo, entonces aplico otro ajuste net_amount = 0.to_f - BlackStack::Balance.new(self.client.id, t.product_code.to_s).amount.to_f net_credits = 0.to_f - BlackStack::Balance.new(self.client.id, t.product_code.to_s).credits.to_f if net_amount < 0 && net_credits < 0 adjust = self.client.adjustment(t.product_code.to_s, net_amount, net_credits, "Adjustment for Negative Balance After Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.") adjust.id_invoice_item = item1.id adjust.save end # if net_amount < 0 # recalculo todos los consumos y expiraciones - CANCELADO - Debe hacerse offline # => self.client.recalculate(t.product_code.to_s) # si el cliente se quedo sin saldo luego del reembolso parcial if net_amount <= 0 # hago el reembolso de cada bono de este item # si el balance quedo en negativo, entonces aplico otro ajuste h[:bonus_plans].each { |bonus| i = BlackStack::InvoicingPaymentsProcessing::plans_descriptor.select { |obj| obj[:item_number] == bonus[:item_number] }.first j = BlackStack::InvoicingPaymentsProcessing::products_descriptor.select { |obj| obj[:code] == i[:product_code] }.first item2 = BlackStack::InvoiceItem.new() item2.id = guid() item2.id_invoice = self.id item2.unit_price = 0 item2.units = -i[:credits] item2.amount = 0 item2.product_code = i[:product_code].to_s item2.item_number = i[:item_number].to_s item2.detail = 'Bonus Refund' item2.description = j[:description].to_s item2.save() BlackStack::Movement.new().parse(item2, BlackStack::Movement::MOVEMENT_TYPE_REFUND_BALANCE, "Bonus Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.").save() net_amount = 0.to_f - BlackStack::Balance.new(self.client.id, i[:product_code].to_s).amount.to_f net_credits = 0.to_f - BlackStack::Balance.new(self.client.id, i[:product_code].to_s).credits.to_f if net_amount <= 0 && net_credits < 0 adjust = self.client.adjustment(i[:product_code].to_s, net_amount, net_credits, "Adjustment for Negative Balance After Bonus Refund of <a href='/settings/invoice?iid=#{self.id.to_guid}'>invoice:#{self.id.to_guid}</a>.") adjust.id_invoice_item = item1.id adjust.save end # if net_amount < 0 } end else # we know that: i.items.size > 1 && total < -payment_gross raise "Refund amount is not matching with the invoice total (#{total.to_s}) and the invoice has more than 1 item." end # release resources DB.disconnect GC.start end |
#total ⇒ Object
115 116 117 118 119 120 121 122 123 124 |
# File 'lib/invoice.rb', line 115 def total() ret = 0 self.items.each { |item| ret += item.amount.to_f # libero recursos DB.disconnect GC.start } ret end |
#totalDesc ⇒ Object
127 128 129 |
# File 'lib/invoice.rb', line 127 def totalDesc() ("%.2f" % self.total.to_s) end |