Merge pull request #2126 from internetee/714-registrar-credit-changing-functionality

Admin: the functionality to change registrar credit balance
This commit is contained in:
Timo Võhmar 2021-08-25 14:09:47 +03:00 committed by GitHub
commit c1c9304377
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
19 changed files with 286 additions and 6 deletions

View file

@ -0,0 +1,30 @@
module Admin
class AccountsController < BaseController
load_and_authorize_resource
def index
@q = Account.includes(:registrar).search(params[:q])
@accounts = @q.result.page(params[:page])
@accounts = @accounts.per(params[:results_per_page]) if paginate?
render_by_format('admin/accounts/index', 'accounts')
end
def show; end
def edit; end
def update
action = Actions::AccountActivityCreate.new(@account,
params[:account][:balance],
params[:description],
AccountActivity::UPDATE_CREDIT)
if action.call
redirect_to admin_accounts_path, notice: t('.updated')
else
flash[:alert] = t('invalid_balance')
render 'edit'
end
end
end
end

View file

@ -0,0 +1,47 @@
module Actions
class AccountActivityCreate
def initialize(account, new_balance, description, type)
@account = account
@new_balance = new_balance
@description = description
@type = type
end
def call
validate_new_balance
return false if @error
calc_sum
create_activity
commit
end
def calc_sum
@sum = @new_balance.to_f - @account.balance
end
def validate_new_balance
if @new_balance.blank?
@error = true
else
begin
!Float(@new_balance).nil?
rescue StandardError
@error = true
end
end
end
def create_activity
@activity = AccountActivity.new(account: @account,
sum: @sum,
currency: @account.currency,
description: @description,
activity_type: @type)
end
def commit
@activity.save!
end
end
end

View file

@ -101,6 +101,7 @@ class Ability
can :manage, BankTransaction
can :manage, Invoice
can :manage, WhiteIp
can :manage, Account
can :manage, AccountActivity
can :manage, Dispute
can :read, ApiLog::EppLog

View file

@ -1,4 +1,5 @@
class Account < ApplicationRecord
extend ToCsv
include Versions
belongs_to :registrar, required: true

View file

@ -5,9 +5,10 @@ class AccountActivity < ApplicationRecord
belongs_to :invoice
belongs_to :price, class_name: 'Billing::Price'
CREATE = 'create'
RENEW = 'renew'
ADD_CREDIT = 'add_credit'
CREATE = 'create'.freeze
RENEW = 'renew'.freeze
ADD_CREDIT = 'add_credit'.freeze
UPDATE_CREDIT = 'update_credit'.freeze
after_create :update_balance
def update_balance
@ -20,7 +21,7 @@ class AccountActivity < ApplicationRecord
class << self
def types_for_select
[CREATE, RENEW, ADD_CREDIT].map { |x| [I18n.t(x), x] }
[CREATE, RENEW, ADD_CREDIT, UPDATE_CREDIT].map { |x| [I18n.t(x), x] }
end
def to_csv

View file

@ -0,0 +1,7 @@
<tr>
<td><%= account.id %></td>
<td><%= account.balance %></td>
<td><%= account.currency %></td>
<td><%= link_to account.registrar, admin_registrar_path(account.registrar) %></td>
<td><%= link_to(t(:edit_balance), edit_admin_account_path(account), class: 'btn btn-primary btn-xs') %></td>
</tr>

View file

@ -0,0 +1,52 @@
<%= form_for([:admin, @account], html: { class: 'form-horizontal', autocomplete: 'off' }) do |f| %>
<%= render 'form_errors', target: @account %>
<div class="row">
<div class="col-md-8">
<div class="form-group">
<div class="col-md-4 control-label">
<%= f.label :id, nil, class: 'required' %>
</div>
<div class="col-md-7">
<%= f.text_field :id, required: true, autofocus: true,
class: 'form-control', disabled: true %>
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
<%= f.label :currency, nil, class: 'required' %>
</div>
<div class="col-md-7">
<%= f.text_field :currency, required: true, class: 'form-control', disabled: true %>
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
<%= f.label :balance %>
</div>
<div class="col-md-7">
<%= f.text_field(:balance, class: 'form-control') %>
</div>
</div>
<div class="form-group">
<div class="col-md-4 control-label">
<%= f.label :description %>
</div>
<div class="col-md-7">
<%= text_area_tag :description, params[:description], class: 'form-control' %>
</div>
</div>
</div>
</div>
<hr>
<div class="row">
<div class="col-md-8 text-right">
<%= button_tag t('.save_btn'), class: 'btn btn-warning' %>
</div>
</div>
<% end %>

View file

@ -0,0 +1,32 @@
<div class="row">
<div class="col-md-12">
<%= search_form_for @q, url: [:admin, :accounts], html: { style: 'margin-bottom: 0;' } do |f| %>
<div class="row">
<div class="col-md-3">
<div class="form-group">
<%= f.label t(:registrar_name) %>
<%= f.select :registrar_id_in, Registrar.all.map { |x| [x, x.id] }, {}, class: 'form-control js-combobox', placeholder: t(:choose), multiple: true %>
</div>
</div>
<div class="col-md-3">
<div class="form-group">
<%= label_tag t(:results_per_page) %>
<%= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) %>
</div>
</div>
<div class="col-md-6 text-right" style="padding-top: 25px;">
<button class="btn btn-default search">
&nbsp;
<span class="glyphicon glyphicon-search"></span>
&nbsp;
</button>
<%= button_tag t('.download_csv_btn'),
formaction: admin_accounts_path(format: 'csv'),
class: 'btn btn-default' %>
<%= link_to(t('.reset_btn'), admin_accounts_path, class: 'btn btn-default') %>
</div>
</div>
<% end %>
</div>
</div>

View file

@ -0,0 +1,5 @@
<h1>Editing Account</h1>
<%= render 'form', account: @account %>
<%= link_to 'Back', admin_accounts_path %>

View file

@ -0,0 +1,38 @@
<h1><%= t '.header' %></h1>
<%= render 'search_form', search: @search %>
<div class="row">
<div class="col-md-12">
<div class="table-responsive">
<table class="table table-hover table-bordered table-condensed">
<thead>
<tr>
<th class="col-xs-2"><%= sort_link(@q, 'ID', t('.')) %></th>
<th class="col-xs-2"><%= sort_link(@q, 'Balance') %></th>
<th class="col-xs-2"><%= sort_link(@q, 'Currency') %></th>
<th class="col-xs-2"><%= sort_link(@q, 'registrar_name', Registrar.model_name.human) %></th>
<th class="col-xs-2">
<%= t(:actions) %>
</th>
</tr>
</thead>
<tbody>
<%= render partial: 'account', collection: @accounts %>
</tbody>
</table>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6">
<%= paginate @accounts %>
</div>
<div class="col-md-6 text-right">
<div class="pagination">
<%= t(:result_count, count: @q.result.count) %>
</div>
</div>
</div>

View file

@ -0,0 +1,5 @@
<h1>New Admin Account</h1>
<%= render 'form', account: @account %>
<%= link_to 'Back', admin_accounts_path %>

View file

@ -0,0 +1,4 @@
<p id="notice"><%= notice %></p>
<%= link_to 'Edit', edit_admin_account_path(@account) %> |
<%= link_to 'Back', admin_accounts_path %>

View file

@ -21,6 +21,7 @@
%li= link_to t('.prices'), admin_prices_path
%li= link_to t(:bank_statements), admin_bank_statements_path
%li= link_to t(:invoices), admin_invoices_path
%li= link_to t(:accounts), admin_accounts_path
%li= link_to t(:account_activities), admin_account_activities_path(created_after: 'today')
%li.divider
%li.dropdown-header= t('.archive')

View file

@ -45,7 +45,7 @@
<%= x.reg_no %>
</td>
<td>
<%= "#{x.balance}" %>
<%= link_to "#{x.balance}", edit_admin_account_path(x.cash_account) %>
</td>
<td>
<%= "#{x.test_registrar}" %>

View file

@ -15,7 +15,7 @@
<dd><%= registrar.code %></dd>
<dt><%= Registrar.human_attribute_name :balance %></dt>
<dd><%= number_to_currency registrar.balance %></dd>
<dd><%= link_to (number_to_currency registrar.balance), edit_admin_account_path(registrar.cash_account) %></dd>
<dt><%= Registrar.human_attribute_name :website %></dt>
<dd><%= registrar.website %></dd>

View file

@ -0,0 +1,21 @@
en:
edit_balance: Edit balance
invalid_balance: The balance must be a number
admin:
accounts:
index:
header: Accounts
update:
updated: Account has been successfully updated
form:
update_btn: Update account
reset_btn: Reset
save_btn: Save
search_form:
registrar_name: Registrar
reset_btn: Reset
download_csv_btn: CSV

View file

@ -358,6 +358,7 @@ en:
contact_org_error: 'Parameter value policy error. Org must be blank'
contact_fax_error: 'Parameter value policy error. Fax must be blank'
invoices: 'Invoices'
accounts: 'Accounts'
no_such_user: 'No such user'
phone_no: 'Phone number'
confirmation_sms_was_sent_to_your_phone_verification_code_is: 'Confirmation sms was sent to your phone. Verification code is %{code}.'
@ -602,6 +603,7 @@ en:
receipt_date_from: 'Receipt date from'
receipt_date_until: 'Receipt date until'
add_credit: 'Add credit'
update_credit: 'Update credit'
invalid_yaml: 'Invalid YAML'
reserved_pw: 'Reserved pw'
no_transfers_found: 'No transfers found'

View file

@ -236,6 +236,7 @@ Rails.application.routes.draw do
end
end
resources :accounts
resources :account_activities
resources :bank_statements do

View file

@ -0,0 +1,32 @@
require 'application_system_test_case'
class AdminAccountsSystemTest < ApplicationSystemTestCase
setup do
sign_in users(:admin)
@account = registrars(:bestnames).cash_account
end
def test_download_accounts
now = Time.zone.parse('2010-07-05 08:00')
travel_to now
get admin_accounts_path(format: :csv)
assert_response :ok
assert_equal 'text/csv; charset=utf-8', response.headers['Content-Type']
assert_equal %(attachment; filename="accounts_#{Time.zone.now.to_formatted_s(:number)}.csv"; filename*=UTF-8''accounts_#{Time.zone.now.to_formatted_s(:number)}.csv),
response.headers['Content-Disposition']
assert_not_empty response.body
end
def test_change_account_balance
visit edit_admin_account_path(@account)
assert_button 'Save'
assert_field 'Balance'
fill_in 'Balance', with: '234'
click_on 'Save'
assert_text 'Account has been successfully updated'
assert_text '234'
end
end