Create System / Admin payment log

This commit is contained in:
Karl Erik Õunapuu 2020-02-04 18:36:06 +02:00
parent 6418924faf
commit 2645654aef
10 changed files with 122 additions and 41 deletions

View file

@ -60,7 +60,7 @@ module Admin
end
def bind_invoices
@bank_statement.bind_invoices
@bank_statement.bind_invoices(manual: true)
flash[:notice] = t('invoices_were_fully_binded') if @bank_statement.fully_binded?
flash[:warning] = t('invoices_were_partially_binded') if @bank_statement.partially_binded?

View file

@ -34,7 +34,7 @@ module Admin
end
def bind
if @bank_transaction.bind_invoice(params[:invoice_no])
if @bank_transaction.bind_invoice(params[:invoice_no], manual: true)
flash[:notice] = I18n.t('record_created')
redirect_to [:admin, @bank_transaction]
else

View file

@ -45,7 +45,7 @@ class BankStatement < ApplicationRecord
buyer_name: row[83, 35].strip,
document_no: row[118, 8].strip,
description: row[126, 140].strip,
sum: BigDecimal.new(row[268, 12].strip) / BigDecimal.new('100.0'),
sum: BigDecimal(row[268, 12].strip) / BigDecimal('100.0'),
reference_no: row[280, 35].strip
}
end
@ -80,7 +80,9 @@ class BankStatement < ApplicationRecord
status == FULLY_BINDED
end
def bind_invoices
bank_transactions.unbinded.each(&:autobind_invoice)
def bind_invoices(manual: false)
bank_transactions.unbinded.each do |transaction|
transaction.autobind_invoice(manual: manual)
end
end
end

View file

@ -13,6 +13,7 @@ class BankTransaction < ApplicationRecord
def binded_invoice
return unless binded?
account_activity.invoice
end
@ -31,28 +32,54 @@ class BankTransaction < ApplicationRecord
end
# For successful binding, reference number, invoice id and sum must match with the invoice
def autobind_invoice
def autobind_invoice(manual: false)
return if binded?
return unless registrar
return unless invoice
return unless invoice.payable?
create_activity(registrar, invoice)
channel = if manual
'admin_payment'
else
'system_payment'
end
record_system_payment(channel: channel, invoice: invoice, registrar: registrar)
end
def bind_invoice(invoice_no)
def record_system_payment(channel: nil, invoice:, registrar:)
if channel.nil?
create_activity(invoice.buyer, invoice)
return
end
payment_order = PaymentOrder.create_with_type(type: channel, invoice: invoice)
payment_order.save!
if create_activity(registrar, invoice)
payment_order.paid!
else
payment_order.failed!
payment_order.notes = 'Failed to create activity'
payment_order.save!
end
end
def bind_invoice(invoice_no, manual: false)
if binded?
errors.add(:base, I18n.t('transaction_is_already_binded'))
return
end
invoice = Invoice.find_by(number: invoice_no)
errors.add(:base, I18n.t('invoice_was_not_found')) unless invoice
validate_invoice_data(invoice)
return if errors.any?
unless invoice
errors.add(:base, I18n.t('invoice_was_not_found'))
return
end
record_system_payment(channel: (manual ? 'admin_payment' : nil), invoice: invoice,
registrar: invoice.buyer)
end
def validate_invoice_data(invoice)
if invoice.paid?
errors.add(:base, I18n.t('invoice_is_already_binded'))
return
@ -63,23 +90,19 @@ class BankTransaction < ApplicationRecord
return
end
if invoice.total != sum
errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match'))
return
end
create_activity(invoice.buyer, invoice)
errors.add(:base, I18n.t('invoice_and_transaction_sums_do_not_match')) if invoice.total != sum
end
def create_activity(registrar, invoice)
ActiveRecord::Base.transaction do
create_account_activity!(account: registrar.cash_account,
invoice: invoice,
sum: invoice.subtotal,
currency: currency,
description: description,
activity_type: AccountActivity::ADD_CREDIT)
activity = AccountActivity.new(account: registrar.cash_account,
invoice: invoice, sum: invoice.subtotal,
currency: currency, description: description,
activity_type: AccountActivity::ADD_CREDIT)
if activity.save
reset_pending_registrar_balance_reload
true
else
false
end
end

View file

@ -4,7 +4,9 @@ class PaymentOrder < ApplicationRecord
PAYMENT_INTERMEDIARIES = ENV['payments_intermediaries'].to_s.strip.split(', ').freeze
PAYMENT_BANKLINK_BANKS = ENV['payments_banks'].to_s.strip.split(', ').freeze
PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS].flatten.freeze
INTERNAL_PAYMENT_METHODS = %w[admin_payment system_payment].freeze
PAYMENT_METHODS = [PAYMENT_INTERMEDIARIES, PAYMENT_BANKLINK_BANKS,
INTERNAL_PAYMENT_METHODS].flatten.freeze
belongs_to :invoice, optional: false

View file

@ -0,0 +1,9 @@
module PaymentOrders
class AdminPayment < PaymentOrder
CONFIG_NAMESPACE = 'admin_payment'.freeze
def self.config_namespace_name
CONFIG_NAMESPACE
end
end
end

View file

@ -0,0 +1,9 @@
module PaymentOrders
class SystemPayment < PaymentOrder
CONFIG_NAMESPACE = 'system_payment'.freeze
def self.config_namespace_name
CONFIG_NAMESPACE
end
end
end

View file

@ -93,20 +93,6 @@ class BankLinkTest < ActiveSupport::TestCase
assert_equal(expected_response, @new_bank_link.form_fields)
end
def test_correct_channel_is_assigned
swed_link = PaymentOrder.create_with_type(type: 'swed', invoice: @invoice)
assert_equal swed_link.channel, 'Swed'
assert_equal swed_link.class.config_namespace_name, 'swed'
seb_link = PaymentOrder.create_with_type(type: 'seb', invoice: @invoice)
assert_equal seb_link.channel, 'Seb'
assert_equal seb_link.class.config_namespace_name, 'seb'
lhv_link = PaymentOrder.create_with_type(type: 'lhv', invoice: @invoice)
assert_equal lhv_link.channel, 'Lhv'
assert_equal lhv_link.class.config_namespace_name, 'lhv'
end
def test_valid_success_response_from_intermediary?
assert(@completed_bank_link.valid_response_from_intermediary?)
end

View file

@ -43,6 +43,32 @@ class PaymentOrdersTest < ActiveSupport::TestCase
end
end
def test_correct_channel_is_assigned
everypay_channel = PaymentOrder.create_with_type(type: 'every_pay', invoice: @invoice)
assert_equal everypay_channel.channel, 'EveryPay'
assert_equal everypay_channel.class.config_namespace_name, 'every_pay'
swed_channel = PaymentOrder.create_with_type(type: 'swed', invoice: @invoice)
assert_equal swed_channel.channel, 'Swed'
assert_equal swed_channel.class.config_namespace_name, 'swed'
seb_channel = PaymentOrder.create_with_type(type: 'seb', invoice: @invoice)
assert_equal seb_channel.channel, 'Seb'
assert_equal seb_channel.class.config_namespace_name, 'seb'
lhv_channel = PaymentOrder.create_with_type(type: 'lhv', invoice: @invoice)
assert_equal lhv_channel.channel, 'Lhv'
assert_equal lhv_channel.class.config_namespace_name, 'lhv'
admin_channel = PaymentOrder.create_with_type(type: 'admin_payment', invoice: @invoice)
assert_equal admin_channel.channel, 'AdminPayment'
assert_equal admin_channel.class.config_namespace_name, 'admin_payment'
system_channel = PaymentOrder.create_with_type(type: 'system_payment', invoice: @invoice)
assert_equal system_channel.channel, 'SystemPayment'
assert_equal system_channel.class.config_namespace_name, 'system_payment'
end
def test_can_not_create_order_for_paid_invoice
invoice = invoices(:one)
payment_order = PaymentOrder.create_with_type(type: 'every_pay', invoice: invoice)

View file

@ -58,6 +58,30 @@ class ProcessPaymentsTaskTest < ActiveSupport::TestCase
assert @invoice.paid?
end
def test_attaches_paid_payment_order_to_invoice
assert @invoice.unpaid?
capture_io { run_task }
@invoice.reload
payment_order = @invoice.payment_orders.last
assert_equal 'PaymentOrders::SystemPayment', payment_order.type
assert payment_order.paid?
end
def test_attaches_failed_payment_order_to_invoice
assert @invoice.unpaid?
account = accounts(:cash)
account.update!(registrar: registrars(:goodnames))
capture_io { run_task }
@invoice.reload
payment_order = @invoice.payment_orders.last
assert_equal 'PaymentOrders::SystemPayment', payment_order.type
assert payment_order.failed?
end
def test_output
assert_output "Transactions processed: 1\n" do
run_task