Refactor debit and credit registrar to include pricelist id #2741

This commit is contained in:
Martin Lensment 2015-07-13 17:39:54 +03:00
parent 9fd38f161a
commit 75a6c27221
11 changed files with 80 additions and 123 deletions

View file

@ -30,7 +30,13 @@ class Epp::DomainsController < EppController
ActiveRecord::Base.transaction do
if @domain.save # TODO: Maybe use validate: false here because we have already validated the domain?
current_user.registrar.debit!(@domain_price, "#{I18n.t('create')} #{@domain.name}", AccountActivity::CREATE)
current_user.registrar.debit!({
sum: @domain_pricelist.price.amount,
description: "#{I18n.t('create')} #{@domain.name}",
activity_type: AccountActivity::CREATE,
log_pricelist_id: @domain_pricelist.id
})
render_epp_response '/epp/domains/create'
else
handle_errors(@domain)
@ -102,7 +108,13 @@ class Epp::DomainsController < EppController
fail ActiveRecord::Rollback
end
current_user.registrar.debit!(@domain_price, "#{I18n.t('renew')} #{@domain.name}", AccountActivity::RENEW)
current_user.registrar.debit!({
sum: @domain_pricelist.price.amount,
description: "#{I18n.t('renew')} #{@domain.name}",
activity_type: AccountActivity::RENEW,
log_pricelist_id: @domain_pricelist.id
})
render_epp_response '/epp/domains/renew'
else
handle_errors(@domain)
@ -212,8 +224,8 @@ class Epp::DomainsController < EppController
end
def balance_ok?(operation, period = nil, unit = nil)
@domain_price = @domain.price(operation, period.try(:to_i), unit).amount
if current_user.registrar.balance < @domain_price
@domain_pricelist = @domain.pricelist(operation, period.try(:to_i), unit)
if current_user.registrar.balance < @domain_pricelist.price.amount
epp_errors << {
code: '2104',
msg: I18n.t('billing_failure_credit_balance_low')

View file

@ -397,7 +397,7 @@ class Domain < ActiveRecord::Base
DomainMailer.pending_deleted(self).deliver_now
end
def price(operation, period_i = nil, unit = nil)
def pricelist(operation, period_i = nil, unit = nil)
period_i ||= period
unit ||= period_unit
@ -413,7 +413,7 @@ class Domain < ActiveRecord::Base
p = "#{p}year"
end
Pricelist.price_for(zone, operation, p)
Pricelist.pricelist_for(zone, operation, p)
end
### VALIDATIONS ###

View file

@ -23,10 +23,10 @@ class Pricelist < ActiveRecord::Base
end
class << self
def price_for(zone, operation, period)
def pricelist_for(zone, operation, period)
lists = valid.where(category: zone, operation_category: operation, duration: period)
return lists.first.price if lists.count == 1
lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first.price
return lists.first if lists.count == 1
lists.where('valid_to IS NOT NULL').order(valid_from: :desc).first
end
end
end

View file

@ -123,22 +123,15 @@ class Registrar < ActiveRecord::Base
accounts.find_by(account_type: Account::CASH)
end
def debit!(sum, description, type = nil)
cash_account.account_activities.create!(
sum: -sum,
currency: 'EUR',
description: description,
activity_type: type
)
def debit!(args)
args[:sum] *= -1
args[:currency] = 'EUR'
cash_account.account_activities.create!(args)
end
def credit!(sum, description, type = nil)
cash_account.account_activities.create!(
sum: sum,
currency: 'EUR',
description: description,
activity_type: type
)
def credit!(args)
args[:currency] = 'EUR'
cash_account.account_activities.create!(args)
end
def domain_transfers