diff --git a/app/controllers/epp/domains_controller.rb b/app/controllers/epp/domains_controller.rb index a3977e8e2..3bb8f50e1 100644 --- a/app/controllers/epp/domains_controller.rb +++ b/app/controllers/epp/domains_controller.rb @@ -21,11 +21,18 @@ class Epp::DomainsController < EppController def create authorize! :create, Epp::Domain @domain = Epp::Domain.new_from_epp(params[:parsed_frame], current_user) + @domain.valid? - if @domain.errors.any? || !@domain.save - handle_errors(@domain) - else - render_epp_response '/epp/domains/create' + handle_errors(@domain) and return if @domain.errors.any? + handle_errors and return unless balance_ok?('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 @@ -185,4 +192,16 @@ class Epp::DomainsController < EppController msg: "#{I18n.t(:client_side_status_editing_error)}: status [status]" } 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 diff --git a/app/models/bank_transaction.rb b/app/models/bank_transaction.rb index 1a2ec5568..a5c8da94c 100644 --- a/app/models/bank_transaction.rb +++ b/app/models/bank_transaction.rb @@ -3,7 +3,9 @@ class BankTransaction < ActiveRecord::Base belongs_to :bank_statement 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? account_activity.present? diff --git a/app/models/domain.rb b/app/models/domain.rb index 2fa8e338e..6203fe2f4 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -372,6 +372,12 @@ class Domain < ActiveRecord::Base DomainMailer.pending_deleted(self).deliver_now end + def price(operation) + zone = name.split('.').drop(1).join('.') + p = "#{self.period}year" + Pricelist.price_for(zone, operation, p) + end + ### VALIDATIONS ### def validate_nameserver_ips diff --git a/app/models/pricelist.rb b/app/models/pricelist.rb index 1c47c1b83..4bd1847cd 100644 --- a/app/models/pricelist.rb +++ b/app/models/pricelist.rb @@ -23,8 +23,8 @@ class Pricelist < ActiveRecord::Base end class << self - def price_for(category, operation, duration) - lists = valid.where(category: category, operation_category: operation, duration: duration) + def price_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 end diff --git a/app/models/registrar.rb b/app/models/registrar.rb index 1feb2caf4..e2a3f7e4d 100644 --- a/app/models/registrar.rb +++ b/app/models/registrar.rb @@ -12,6 +12,8 @@ class Registrar < ActiveRecord::Base has_many :priv_contacts, -> { privs }, class_name: 'Contact' 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, :reference_no, :code, uniqueness: true validate :forbidden_codes @@ -121,6 +123,14 @@ class Registrar < ActiveRecord::Base accounts.find_by(account_type: Account::CASH) end + def debit!(sum, description) + cash_account.account_activities.create!( + sum: -sum, + currency: 'EUR', + description: description + ) + end + def domain_transfers at = DomainTransfer.arel_table DomainTransfer.where( diff --git a/app/views/registrar/account_activities/index.haml b/app/views/registrar/account_activities/index.haml index 45783d727..0efd0ab20 100644 --- a/app/views/registrar/account_activities/index.haml +++ b/app/views/registrar/account_activities/index.haml @@ -20,7 +20,7 @@ - if x.invoice %td= link_to(x.invoice, [:registrar, x.invoice]) - else - %td \- + %td - - c = x.sum > 0.0 ? 'text-success' : 'text-danger' - s = x.sum > 0.0 ? "+#{x.sum} #{x.currency}" : "#{x.sum} #{x.currency}" %td{class: c}= s diff --git a/config/locales/en.yml b/config/locales/en.yml index 679bb44a0..0faa3b0ef 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -854,3 +854,5 @@ en: registry_zip: 'Postcode' registry_country_code: 'Country' blocked_domains: 'Blocked domains' + billing_failure_credit_balance_low: 'Billing failure - credit balance low' + create: 'Create'