Debit account on domain create #2741

This commit is contained in:
Martin Lensment 2015-07-02 17:08:24 +03:00
parent e2374f9702
commit 98439b896c
7 changed files with 47 additions and 8 deletions

View file

@ -21,11 +21,18 @@ class Epp::DomainsController < EppController
def create def create
authorize! :create, Epp::Domain authorize! :create, Epp::Domain
@domain = Epp::Domain.new_from_epp(params[:parsed_frame], current_user) @domain = Epp::Domain.new_from_epp(params[:parsed_frame], current_user)
@domain.valid?
if @domain.errors.any? || !@domain.save handle_errors(@domain) and return if @domain.errors.any?
handle_errors(@domain) handle_errors and return unless balance_ok?('create')
else
render_epp_response '/epp/domains/create' ActiveRecord::Base.transaction do
if @domain.save
current_user.registrar.debit!(@domain_price, "#{I18n.t('create')} #{@domain.name}")
render_epp_response '/epp/domains/create'
else
handle_errors(@domain)
end
end end
end end
@ -185,4 +192,16 @@ class Epp::DomainsController < EppController
msg: "#{I18n.t(:client_side_status_editing_error)}: status [status]" msg: "#{I18n.t(:client_side_status_editing_error)}: status [status]"
} }
end end
def balance_ok?(operation)
@domain_price = @domain.price(operation).amount
if current_user.registrar.balance < @domain_price
epp_errors << {
code: '2104',
msg: I18n.t('billing_failure_credit_balance_low')
}
return false
end
true
end
end end

View file

@ -3,7 +3,9 @@ class BankTransaction < ActiveRecord::Base
belongs_to :bank_statement belongs_to :bank_statement
has_one :account_activity has_one :account_activity
scope :unbinded, -> { where('id NOT IN (SELECT bank_transaction_id FROM account_activities)') } scope :unbinded, -> {
where('id NOT IN (SELECT bank_transaction_id FROM account_activities where bank_transaction_id IS NOT NULL)')
}
def binded? def binded?
account_activity.present? account_activity.present?

View file

@ -372,6 +372,12 @@ class Domain < ActiveRecord::Base
DomainMailer.pending_deleted(self).deliver_now DomainMailer.pending_deleted(self).deliver_now
end end
def price(operation)
zone = name.split('.').drop(1).join('.')
p = "#{self.period}year"
Pricelist.price_for(zone, operation, p)
end
### VALIDATIONS ### ### VALIDATIONS ###
def validate_nameserver_ips def validate_nameserver_ips

View file

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

View file

@ -12,6 +12,8 @@ class Registrar < ActiveRecord::Base
has_many :priv_contacts, -> { privs }, class_name: 'Contact' has_many :priv_contacts, -> { privs }, class_name: 'Contact'
has_many :white_ips, dependent: :destroy has_many :white_ips, dependent: :destroy
delegate :balance, to: :cash_account
validates :name, :reg_no, :country_code, :email, :code, presence: true validates :name, :reg_no, :country_code, :email, :code, presence: true
validates :name, :reg_no, :reference_no, :code, uniqueness: true validates :name, :reg_no, :reference_no, :code, uniqueness: true
validate :forbidden_codes validate :forbidden_codes
@ -121,6 +123,14 @@ class Registrar < ActiveRecord::Base
accounts.find_by(account_type: Account::CASH) accounts.find_by(account_type: Account::CASH)
end end
def debit!(sum, description)
cash_account.account_activities.create!(
sum: -sum,
currency: 'EUR',
description: description
)
end
def domain_transfers def domain_transfers
at = DomainTransfer.arel_table at = DomainTransfer.arel_table
DomainTransfer.where( DomainTransfer.where(

View file

@ -20,7 +20,7 @@
- if x.invoice - if x.invoice
%td= link_to(x.invoice, [:registrar, x.invoice]) %td= link_to(x.invoice, [:registrar, x.invoice])
- else - else
%td \- %td -
- c = x.sum > 0.0 ? 'text-success' : 'text-danger' - c = x.sum > 0.0 ? 'text-success' : 'text-danger'
- s = x.sum > 0.0 ? "+#{x.sum} #{x.currency}" : "#{x.sum} #{x.currency}" - s = x.sum > 0.0 ? "+#{x.sum} #{x.currency}" : "#{x.sum} #{x.currency}"
%td{class: c}= s %td{class: c}= s

View file

@ -854,3 +854,5 @@ en:
registry_zip: 'Postcode' registry_zip: 'Postcode'
registry_country_code: 'Country' registry_country_code: 'Country'
blocked_domains: 'Blocked domains' blocked_domains: 'Blocked domains'
billing_failure_credit_balance_low: 'Billing failure - credit balance low'
create: 'Create'