mirror of
https://github.com/internetee/registry.git
synced 2025-08-01 07:26:22 +02:00
refactoring
This commit is contained in:
parent
9856a2a5c0
commit
fdf70359d1
6 changed files with 72 additions and 65 deletions
|
@ -5,7 +5,7 @@ class EisBilling::DirectoResponseController < EisBilling::BaseController
|
||||||
@month = params.fetch(:month, false)
|
@month = params.fetch(:month, false)
|
||||||
|
|
||||||
process_directo_response(xml_data, response)
|
process_directo_response(xml_data, response)
|
||||||
render status: 200, json: { messege: 'Should return new directo number', status: :ok }
|
render status: :ok, json: { messege: 'Should return new directo number' }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -2,13 +2,13 @@ class EisBilling::EInvoiceResponseController < EisBilling::BaseController
|
||||||
def update
|
def update
|
||||||
invoice_number = params[:invoice_number]
|
invoice_number = params[:invoice_number]
|
||||||
|
|
||||||
set_e_invoice_sent_at(invoice_number)
|
mark_e_invoice_sent_at(invoice_number)
|
||||||
render status: :ok, json: { messege: 'Response received' }
|
render status: :ok, json: { message: 'Response received' }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def set_e_invoice_sent_at(invoice_number)
|
def mark_e_invoice_sent_at(invoice_number)
|
||||||
invoice = Invoice.find_by(number: invoice_number)
|
invoice = Invoice.find_by(number: invoice_number)
|
||||||
invoice.update(e_invoice_sent_at: Time.zone.now)
|
invoice.update(e_invoice_sent_at: Time.zone.now)
|
||||||
end
|
end
|
||||||
|
|
|
@ -19,21 +19,27 @@ module EisBilling
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
bank_statement.save!
|
bank_statement.save!
|
||||||
|
transaction = create_transaction(incoming_transaction: incoming_transaction, bank_statement: bank_statement)
|
||||||
transaction_attributes = { sum: incoming_transaction['amount'],
|
|
||||||
currency: incoming_transaction['currency'],
|
|
||||||
paid_at: incoming_transaction['date'],
|
|
||||||
reference_no: incoming_transaction['payment_reference_number'],
|
|
||||||
description: incoming_transaction['payment_description'] }
|
|
||||||
transaction = bank_statement.bank_transactions.create!(transaction_attributes)
|
|
||||||
|
|
||||||
next if transaction.registrar.blank?
|
next if transaction.registrar.blank?
|
||||||
|
|
||||||
unless transaction.non_canceled?
|
create_invoice_if_missing(transaction) unless transaction.non_canceled?
|
||||||
Invoice.create_from_transaction!(transaction) unless transaction.autobindable?
|
|
||||||
transaction.autobind_invoice
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_invoice_if_missing(transaction)
|
||||||
|
Invoice.create_from_transaction!(transaction) unless transaction.autobindable?
|
||||||
|
transaction.autobind_invoice
|
||||||
|
end
|
||||||
|
|
||||||
|
def create_transaction(incoming_transaction:, bank_statement:)
|
||||||
|
transaction_attributes = { sum: incoming_transaction['amount'],
|
||||||
|
currency: incoming_transaction['currency'],
|
||||||
|
paid_at: incoming_transaction['date'],
|
||||||
|
reference_no: incoming_transaction['payment_reference_number'],
|
||||||
|
description: incoming_transaction['payment_description'] }
|
||||||
|
|
||||||
|
bank_statement.bank_transactions.create!(transaction_attributes)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -3,17 +3,10 @@ module EisBilling
|
||||||
TYPE = 'PaymentOrders::EveryPay'.freeze
|
TYPE = 'PaymentOrders::EveryPay'.freeze
|
||||||
|
|
||||||
def update
|
def update
|
||||||
invoice_number = params[:order_reference]
|
|
||||||
paid_at = params[:transaction_time]
|
|
||||||
sum = params[:standing_amount]
|
|
||||||
everypay_response = params
|
|
||||||
|
|
||||||
payment_status = define_payment_status(params[:payment_state])
|
payment_status = define_payment_status(params[:payment_state])
|
||||||
|
invoice = Invoice.find_by(number: params[:order_reference])
|
||||||
invoice = Invoice.find_by(number: invoice_number)
|
bank = create_bank_transfer(invoice: invoice, sum: params[:standing_amount], paid_at: params[:transaction_time])
|
||||||
|
create_payment_order(invoice: invoice, everypay_response: params, payment_status: payment_status)
|
||||||
bank = create_bank_transfer(invoice: invoice, sum: sum, paid_at: paid_at)
|
|
||||||
create_payment_order(invoice: invoice, everypay_response: everypay_response, payment_status: payment_status)
|
|
||||||
|
|
||||||
registrar = invoice.buyer
|
registrar = invoice.buyer
|
||||||
bank.create_activity(registrar, invoice)
|
bank.create_activity(registrar, invoice)
|
||||||
|
@ -37,9 +30,6 @@ module EisBilling
|
||||||
payment.status = payment_status
|
payment.status = payment_status
|
||||||
payment.save
|
payment.save
|
||||||
|
|
||||||
logger.info '++++ PAYMENT ORDER ERRORS ? ++++'
|
|
||||||
logger.info payment.errors
|
|
||||||
|
|
||||||
payment
|
payment
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -54,9 +44,6 @@ module EisBilling
|
||||||
bank.buyer_name = invoice.buyer_name
|
bank.buyer_name = invoice.buyer_name
|
||||||
bank.save
|
bank.save
|
||||||
|
|
||||||
logger.info '++++ BANK TRANSACTION ERRORS ? ++++'
|
|
||||||
logger.info bank.errors
|
|
||||||
|
|
||||||
bank
|
bank
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -40,41 +40,55 @@ class Invoice < ApplicationRecord
|
||||||
|
|
||||||
attribute :vat_rate, ::Type::VatRate.new
|
attribute :vat_rate, ::Type::VatRate.new
|
||||||
|
|
||||||
|
def validate_invoice_number(result)
|
||||||
|
response = JSON.parse(result.body)
|
||||||
|
|
||||||
|
billing_restrictions_issue if response['code'] == '403'
|
||||||
|
billing_out_of_range_issue if response['error'] == 'out of range'
|
||||||
|
end
|
||||||
|
|
||||||
|
def billing_restrictions_issue
|
||||||
|
errors.add(:base, I18n.t('cannot get access'))
|
||||||
|
logger.error('PROBLEM WITH TOKEN')
|
||||||
|
throw(:abort)
|
||||||
|
end
|
||||||
|
|
||||||
|
def billing_out_of_range_issue
|
||||||
|
errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
|
||||||
|
logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
|
||||||
|
throw(:abort)
|
||||||
|
end
|
||||||
|
|
||||||
|
def invoice_number_from_billing
|
||||||
|
result = EisBilling::GetInvoiceNumber.send_invoice
|
||||||
|
validate_invoice_number(result)
|
||||||
|
|
||||||
|
self.number = JSON.parse(result.body)['invoice_number'].to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
def generate_invoice_number_legacy
|
||||||
|
last_no = Invoice.all
|
||||||
|
.where(number: Setting.invoice_number_min.to_i...Setting.invoice_number_max.to_i)
|
||||||
|
.order(number: :desc)
|
||||||
|
.limit(1)
|
||||||
|
.pick(:number)
|
||||||
|
|
||||||
|
if last_no && last_no >= Setting.invoice_number_min.to_i
|
||||||
|
self.number = last_no + 1
|
||||||
|
else
|
||||||
|
self.number = Setting.invoice_number_min.to_i
|
||||||
|
end
|
||||||
|
|
||||||
|
return if number <= Setting.invoice_number_max.to_i
|
||||||
|
|
||||||
|
billing_out_of_range_issue
|
||||||
|
end
|
||||||
|
|
||||||
def set_invoice_number
|
def set_invoice_number
|
||||||
if Feature.billing_system_integrated?
|
if Feature.billing_system_integrated?
|
||||||
result = EisBilling::GetInvoiceNumber.send_invoice
|
invoice_number_from_billing
|
||||||
|
|
||||||
if JSON.parse(result.body)['code'] == '403'
|
|
||||||
errors.add(:base, I18n.t('cannot get access'))
|
|
||||||
logger.error('PROBLEM WITH TOKEN')
|
|
||||||
throw(:abort)
|
|
||||||
end
|
|
||||||
|
|
||||||
if JSON.parse(result.body)['error'] == 'out of range'
|
|
||||||
errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
|
|
||||||
logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
|
|
||||||
throw(:abort)
|
|
||||||
end
|
|
||||||
|
|
||||||
self.number = JSON.parse(result.body)['invoice_number'].to_i
|
|
||||||
else
|
else
|
||||||
last_no = Invoice.all
|
generate_invoice_number_legacy
|
||||||
.where(number: Setting.invoice_number_min.to_i...Setting.invoice_number_max.to_i)
|
|
||||||
.order(number: :desc)
|
|
||||||
.limit(1)
|
|
||||||
.pick(:number)
|
|
||||||
|
|
||||||
if last_no && last_no >= Setting.invoice_number_min.to_i
|
|
||||||
self.number = last_no + 1
|
|
||||||
else
|
|
||||||
self.number = Setting.invoice_number_min.to_i
|
|
||||||
end
|
|
||||||
|
|
||||||
return if number <= Setting.invoice_number_max.to_i
|
|
||||||
|
|
||||||
errors.add(:base, I18n.t('failed_to_generate_invoice_invoice_number_limit_reached'))
|
|
||||||
logger.error('INVOICE NUMBER LIMIT REACHED, COULD NOT GENERATE INVOICE')
|
|
||||||
throw(:abort)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -18,8 +18,8 @@ module EisBilling
|
||||||
data[:order_reference] = invoice.number
|
data[:order_reference] = invoice.number
|
||||||
data[:customer_name] = invoice.buyer_name
|
data[:customer_name] = invoice.buyer_name
|
||||||
data[:customer_email] = invoice.buyer_email
|
data[:customer_email] = invoice.buyer_email
|
||||||
data[:custom_field_1] = invoice.description
|
data[:custom_field1] = invoice.description
|
||||||
data[:custom_field_2] = INITIATOR
|
data[:custom_field2] = INITIATOR
|
||||||
data[:invoice_number] = invoice.number
|
data[:invoice_number] = invoice.number
|
||||||
|
|
||||||
data
|
data
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue