diff --git a/app/controllers/admin/accounts_controller.rb b/app/controllers/admin/accounts_controller.rb new file mode 100644 index 000000000..756bbc662 --- /dev/null +++ b/app/controllers/admin/accounts_controller.rb @@ -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 diff --git a/app/interactions/actions/account_activity_create.rb b/app/interactions/actions/account_activity_create.rb new file mode 100644 index 000000000..1bfb53baf --- /dev/null +++ b/app/interactions/actions/account_activity_create.rb @@ -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 diff --git a/app/models/ability.rb b/app/models/ability.rb index 66a8793bc..febde4e8b 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -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 diff --git a/app/models/account.rb b/app/models/account.rb index 420f43e48..fe0820888 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -1,4 +1,5 @@ class Account < ApplicationRecord + extend ToCsv include Versions belongs_to :registrar, required: true diff --git a/app/models/account_activity.rb b/app/models/account_activity.rb index 9df64209a..432d444d8 100644 --- a/app/models/account_activity.rb +++ b/app/models/account_activity.rb @@ -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 diff --git a/app/views/admin/accounts/_account.html.erb b/app/views/admin/accounts/_account.html.erb new file mode 100644 index 000000000..43a9a2c06 --- /dev/null +++ b/app/views/admin/accounts/_account.html.erb @@ -0,0 +1,7 @@ + + <%= account.id %> + <%= account.balance %> + <%= account.currency %> + <%= link_to account.registrar, admin_registrar_path(account.registrar) %> + <%= link_to(t(:edit_balance), edit_admin_account_path(account), class: 'btn btn-primary btn-xs') %> + diff --git a/app/views/admin/accounts/_form.html.erb b/app/views/admin/accounts/_form.html.erb new file mode 100644 index 000000000..01533cf20 --- /dev/null +++ b/app/views/admin/accounts/_form.html.erb @@ -0,0 +1,52 @@ +<%= form_for([:admin, @account], html: { class: 'form-horizontal', autocomplete: 'off' }) do |f| %> + <%= render 'form_errors', target: @account %> + +
+
+
+
+ <%= f.label :id, nil, class: 'required' %> +
+
+ <%= f.text_field :id, required: true, autofocus: true, + class: 'form-control', disabled: true %> +
+
+ +
+
+ <%= f.label :currency, nil, class: 'required' %> +
+
+ <%= f.text_field :currency, required: true, class: 'form-control', disabled: true %> +
+
+ +
+
+ <%= f.label :balance %> +
+
+ <%= f.text_field(:balance, class: 'form-control') %> +
+
+ +
+
+ <%= f.label :description %> +
+
+ <%= text_area_tag :description, params[:description], class: 'form-control' %> +
+
+
+
+ +
+ +
+
+ <%= button_tag t('.save_btn'), class: 'btn btn-warning' %> +
+
+<% end %> diff --git a/app/views/admin/accounts/_search_form.html.erb b/app/views/admin/accounts/_search_form.html.erb new file mode 100644 index 000000000..9a0a83521 --- /dev/null +++ b/app/views/admin/accounts/_search_form.html.erb @@ -0,0 +1,32 @@ +
+
+ <%= search_form_for @q, url: [:admin, :accounts], html: { style: 'margin-bottom: 0;' } do |f| %> +
+
+
+ <%= 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 %> +
+
+
+
+ <%= label_tag t(:results_per_page) %> + <%= text_field_tag :results_per_page, params[:results_per_page], class: 'form-control', placeholder: t(:results_per_page) %> +
+
+ +
+ + <%= 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') %> +
+
+ <% end %> +
+
diff --git a/app/views/admin/accounts/edit.html.erb b/app/views/admin/accounts/edit.html.erb new file mode 100644 index 000000000..d3b22c2b0 --- /dev/null +++ b/app/views/admin/accounts/edit.html.erb @@ -0,0 +1,5 @@ +

Editing Account

+ +<%= render 'form', account: @account %> + +<%= link_to 'Back', admin_accounts_path %> diff --git a/app/views/admin/accounts/index.html.erb b/app/views/admin/accounts/index.html.erb new file mode 100644 index 000000000..778d3d0af --- /dev/null +++ b/app/views/admin/accounts/index.html.erb @@ -0,0 +1,38 @@ +

<%= t '.header' %>

+<%= render 'search_form', search: @search %> + +
+
+
+ + + + + + + + + + + + + <%= render partial: 'account', collection: @accounts %> + +
<%= sort_link(@q, 'ID', t('.')) %><%= sort_link(@q, 'Balance') %><%= sort_link(@q, 'Currency') %><%= sort_link(@q, 'registrar_name', Registrar.model_name.human) %> + <%= t(:actions) %> +
+
+
+
+
+ +
+
+ <%= paginate @accounts %> +
+
+ +
+
diff --git a/app/views/admin/accounts/new.html.erb b/app/views/admin/accounts/new.html.erb new file mode 100644 index 000000000..8556f537a --- /dev/null +++ b/app/views/admin/accounts/new.html.erb @@ -0,0 +1,5 @@ +

New Admin Account

+ +<%= render 'form', account: @account %> + +<%= link_to 'Back', admin_accounts_path %> diff --git a/app/views/admin/accounts/show.html.erb b/app/views/admin/accounts/show.html.erb new file mode 100644 index 000000000..ef5583e83 --- /dev/null +++ b/app/views/admin/accounts/show.html.erb @@ -0,0 +1,4 @@ +

<%= notice %>

+ +<%= link_to 'Edit', edit_admin_account_path(@account) %> | +<%= link_to 'Back', admin_accounts_path %> diff --git a/app/views/admin/base/_menu.haml b/app/views/admin/base/_menu.haml index 46910afa7..c5edd4708 100644 --- a/app/views/admin/base/_menu.haml +++ b/app/views/admin/base/_menu.haml @@ -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') diff --git a/app/views/admin/registrars/index.html.erb b/app/views/admin/registrars/index.html.erb index 7095fd0ef..23f29115c 100644 --- a/app/views/admin/registrars/index.html.erb +++ b/app/views/admin/registrars/index.html.erb @@ -45,7 +45,7 @@ <%= x.reg_no %> - <%= "#{x.balance}" %> + <%= link_to "#{x.balance}", edit_admin_account_path(x.cash_account) %> <%= "#{x.test_registrar}" %> diff --git a/app/views/admin/registrars/show/_details.html.erb b/app/views/admin/registrars/show/_details.html.erb index 9ae9aac3f..28194ca0a 100644 --- a/app/views/admin/registrars/show/_details.html.erb +++ b/app/views/admin/registrars/show/_details.html.erb @@ -15,7 +15,7 @@
<%= registrar.code %>
<%= Registrar.human_attribute_name :balance %>
-
<%= number_to_currency registrar.balance %>
+
<%= link_to (number_to_currency registrar.balance), edit_admin_account_path(registrar.cash_account) %>
<%= Registrar.human_attribute_name :website %>
<%= registrar.website %>
diff --git a/config/locales/admin/accounts.en.yml b/config/locales/admin/accounts.en.yml new file mode 100644 index 000000000..8566f335e --- /dev/null +++ b/config/locales/admin/accounts.en.yml @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index 944c99367..2ef9a5b81 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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' diff --git a/config/routes.rb b/config/routes.rb index 0db226cf1..1a3b394d1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -236,6 +236,7 @@ Rails.application.routes.draw do end end + resources :accounts resources :account_activities resources :bank_statements do diff --git a/test/system/admin_area/accounts_test.rb b/test/system/admin_area/accounts_test.rb new file mode 100644 index 000000000..f07ced9c3 --- /dev/null +++ b/test/system/admin_area/accounts_test.rb @@ -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