Add balance checking

This commit is contained in:
Alex Sherman 2020-12-11 15:23:54 +05:00
parent 3ef6c53a8c
commit 729f39f612
5 changed files with 67 additions and 3 deletions

View file

@ -16,8 +16,12 @@ class Registrar
if domain_ids_for_bulk_renew.present?
domains = Epp::Domain.where(id: domain_ids_for_bulk_renew).to_a
task = Domains::BulkRenew::Start.run(domains: domains)
flash[:notice] = t(:bulk_renew_completed)
task = Domains::BulkRenew::Start.run(domains: domains, period_element: @period)
if task.valid?
flash[:notice] = t(:bulk_renew_completed)
else
flash[:notice] = task.errors.full_messages.join(' and ')
end
end
render file: 'registrar/bulk_change/new', locals: { active_tab: :bulk_renew }
end

View file

@ -4,9 +4,18 @@ module Domains
array :domains do
object class: Epp::Domain
end
string :period_element
def execute
period = (period_element.to_i == 0) ? 1 : period_element.to_i
unit = period_element[-1] || 'y'
task = Domains::CheckBalance::Mass.run(domains: domains,
operation: 'renew',
period: period,
unit: unit)
unless task.valid?
errors.merge!(task.errors)
end
end
end
end

View file

@ -0,0 +1,23 @@
module Domains
module CheckBalance
class Mass < ActiveInteraction::Base
array :domains do
object class: Epp::Domain
end
string :operation
integer :period
string :unit
def execute
domains.each do |domain|
compose(Domains::CheckBalance::SingleDomain,
domain: domain,
operation: 'renew',
period: period,
unit: unit)
end
end
end
end
end

View file

@ -0,0 +1,26 @@
module Domains
module CheckBalance
class SingleDomain < ActiveInteraction::Base
object :domain,
class: Epp::Domain
string :operation
integer :period
string :unit
def execute
domain_pricelist = domain.pricelist(operation, period.try(:to_i), unit)
if domain_pricelist.try(:price) # checking if price list is not found
if current_user.registrar.balance < domain_pricelist.price.amount
errors.add(:domain, I18n.t('billing_failure_credit_balance_low_for_domain', domain: domain.name))
return false
end
else
errors.add(:domain, I18n.t(:active_price_missing_for_operation_with_domain, domain: domain.name))
return false
end
true
end
end
end
end

View file

@ -588,6 +588,7 @@ en:
create_bank_transaction: 'Create bank transaction'
create_new_invoice: 'Create new invoice'
billing_failure_credit_balance_low: 'Billing failure - credit balance low'
billing_failure_credit_balance_low_for_domain: 'Billing failure - credit balance low for %{domain}'
create: 'Create'
activity_type: 'Activity type'
receipt_date_from: 'Receipt date from'
@ -602,6 +603,7 @@ en:
notes: Notes
active_price_for_this_operation_is: 'Active price for this operation is %{price}'
active_price_missing_for_this_operation: 'Active price missing for this operation!'
active_price_missing_for_operation_with_domain: 'Active price missing for operation with %{domain}'
valid_to_from: 'Valid to from'
valid_to_until: 'Valid to until'
registrant_ident: 'Registrant ident'