Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into refactor-contact-ar…
Browse files Browse the repository at this point in the history
…chivation
  • Loading branch information
karlerikounapuu committed Sep 16, 2020
2 parents 5330743 + 09e9521 commit db77147
Show file tree
Hide file tree
Showing 47 changed files with 452 additions and 289 deletions.
23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,26 @@
15.09.2020
* Fixed e-invoice sending issue with QUE [#1683](https://github.com/internetee/registry/pull/1683)

14.09.2020
* Restored version logging for registry prices [#980](https://github.com/internetee/registry/pull/980)

11.09.2020
* Registrars can now top up their credit accounts without generating invoice in advance [#1101](https://github.com/internetee/registry/issues/1101)
* Fixed typo in admin settings [#371](https://github.com/internetee/registry/issues/371)

10.09.2020
* New registrar ref nr are now always created 7 digits long [#1679](https://github.com/internetee/registry/pull/1679)

08.09.2020
* Removed bank statement import option [#1674](https://github.com/internetee/registry/pull/1674)
* Fixed error with reference nr not being found in the transaction [#1677](https://github.com/internetee/registry/issues/1677)

04.09.2020
* Removed reduntant domains.registered_at db column [#1445](https://github.com/internetee/registry/pull/1445)
* Certificate revocation lists are now hanlded outside of the application code [#1662](https://github.com/internetee/registry/pull/1662)
* Monthly invoices are sent one by one to elliminate reply delay from accounting system [#1671](https://github.com/internetee/registry/pull/1671)
* Fixed poll request ip whitelist issue [#1672](https://github.com/internetee/registry/pull/1672)

03.09.2020
* Refactored session timeout management [#711](https://github.com/internetee/registry/issues/711)
* Improved error handling for epp requests without proper session [#1276](https://github.com/internetee/registry/pull/1276)
Expand Down
13 changes: 7 additions & 6 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ GIT

GIT
remote: https://github.com/internetee/directo.git
revision: 8ff8a382d004ffb85722a6a7a68a020bd4d7159b
revision: e4ba54f601d1815fd8782a196788730d47861e86
branch: master
specs:
directo (1.0.1)
Expand All @@ -18,7 +18,7 @@ GIT

GIT
remote: https://github.com/internetee/e_invoice.git
revision: b374ffd7be77b559b30c7a0210dc0df5ac3ed723
revision: 5f8d0029bf1affdbf2bd6e3d1ce87d34066add4d
branch: master
specs:
e_invoice (0.1.0)
Expand Down Expand Up @@ -165,7 +165,7 @@ GEM
coffee-script-source
execjs
coffee-script-source (1.12.2)
concurrent-ruby (1.1.6)
concurrent-ruby (1.1.7)
countries (3.0.1)
i18n_data (~> 0.10.0)
sixarm_ruby_unaccent (~> 1.1)
Expand Down Expand Up @@ -238,10 +238,10 @@ GEM
http-cookie (1.0.3)
domain_name (~> 0.5)
httpclient (2.8.3)
httpi (2.4.4)
httpi (2.4.5)
rack
socksify
i18n (1.8.3)
i18n (1.8.5)
concurrent-ruby (~> 1.0)
i18n_data (0.10.0)
isikukood (0.1.2)
Expand Down Expand Up @@ -467,7 +467,8 @@ GEM
i18n
warden (1.2.8)
rack (>= 2.0.6)
wasabi (3.5.0)
wasabi (3.6.1)
addressable
httpi (~> 2.0)
nokogiri (>= 1.4.2)
webdrivers (4.4.1)
Expand Down
25 changes: 2 additions & 23 deletions app/controllers/admin/bank_statements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Admin
class BankStatementsController < BaseController
load_and_authorize_resource

before_action :set_bank_statement, only: [:show, :download_import_file, :bind_invoices]
before_action :set_bank_statement, only: %i[show bind_invoices]

def index
@q = BankStatement.search(params[:q])
Expand Down Expand Up @@ -43,22 +43,6 @@ def create
end
end

def import
@bank_statement = BankStatement.new
end

def create_from_import
@bank_statement = BankStatement.new(bank_statement_params)

if @bank_statement.import
flash[:notice] = I18n.t('record_created')
redirect_to [:admin, @bank_statement]
else
flash.now[:alert] = I18n.t('failed_to_create_record')
render 'new'
end
end

def bind_invoices
@bank_statement.bind_invoices(manual: true)

Expand All @@ -69,19 +53,14 @@ def bind_invoices
redirect_to [:admin, @bank_statement]
end

def download_import_file
filename = @bank_statement.import_file_path.split('/').last
send_data File.open(@bank_statement.import_file_path, 'r').read, filename: filename
end

private

def set_bank_statement
@bank_statement = BankStatement.find(params[:id])
end

def bank_statement_params
params.require(:bank_statement).permit(:th6_file, :bank_code, :iban, bank_transactions_attributes: [
params.require(:bank_statement).permit(:bank_code, :iban, bank_transactions_attributes: [
:description, :sum, :currency, :reference_no, :paid_at
])
end
Expand Down
26 changes: 13 additions & 13 deletions app/jobs/directo_invoice_forward_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ class DirectoInvoiceForwardJob < Que::Job
def run(monthly: false, dry: false)
@dry = dry
(@month = Time.zone.now - 1.month) if monthly
api_url = ENV['directo_invoice_url']
sales_agent = Setting.directo_sales_agent
payment_term = Setting.directo_receipt_payment_term
@prepayment_product_id = Setting.directo_receipt_product_name

@client = DirectoApi::Client.new(api_url, sales_agent, payment_term)
@client = new_directo_client
monthly ? send_monthly_invoices : send_receipts
end

def new_directo_client
DirectoApi::Client.new(ENV['directo_invoice_url'], Setting.directo_sales_agent,
Setting.directo_receipt_payment_term)
end

def send_receipts
unsent_invoices = Invoice.where(in_directo: false).non_cancelled

Expand All @@ -28,19 +29,18 @@ def send_receipts

def send_monthly_invoices
Registrar.where.not(test_registrar: true).find_each do |registrar|
fetch_monthly_summary(registrar: registrar)
end

return unless @client.invoices.count.positive?
next unless registrar.cash_account

sync_with_directo
@client = new_directo_client
send_invoice_for_registrar(registrar)
end
end

def fetch_monthly_summary(registrar:)
return unless registrar.cash_account

def send_invoice_for_registrar(registrar)
summary = registrar.monthly_summary(month: @month)
@client.invoices.add_with_schema(invoice: summary, schema: 'summary') unless summary.nil?

sync_with_directo if @client.invoices.count.positive?
end

def assign_monthly_numbers
Expand Down
10 changes: 5 additions & 5 deletions app/jobs/send_e_invoice_job.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class SendEInvoiceJob < Que::Job
def run(invoice_id)
invoice = run_condition(Invoice.find_by(id: invoice_id))
def run(invoice_id, payable = true)
invoice = run_condition(Invoice.find_by(id: invoice_id), payable: payable)

invoice.to_e_invoice.deliver
invoice.to_e_invoice(payable: payable).deliver
ActiveRecord::Base.transaction do
invoice.update(e_invoice_sent_at: Time.zone.now)
log_success(invoice)
Expand All @@ -15,9 +15,9 @@ def run(invoice_id)

private

def run_condition(invoice)
def run_condition(invoice, payable: true)
destroy unless invoice
destroy if invoice.do_not_send_e_invoice?
destroy if invoice.do_not_send_e_invoice? && payable
invoice
end

Expand Down
4 changes: 3 additions & 1 deletion app/models/ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,14 @@ def super # Registrar/api_user dynamic role

def epp # Registrar/api_user dynamic role
if @user.registrar.api_ip_white?(@ip)
can :manage, :poll
can :manage, Depp::Contact
can :manage, :xml_console
can :manage, Depp::Domain
end

# Poll
can :manage, :poll

# REPP
can(:manage, :repp)

Expand Down
61 changes: 4 additions & 57 deletions app/models/bank_statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,70 +4,17 @@ class BankStatement < ApplicationRecord

accepts_nested_attributes_for :bank_transactions

attr_accessor :th6_file

validates :bank_code, :iban, presence: true

FULLY_BINDED = 'fully_binded'
PARTIALLY_BINDED = 'partially_binded'
NOT_BINDED = 'not_binded'

def import
import_th6_file && save
end

def import_th6_file
return false unless th6_file

th6_file.open.each_line do |row|
bt_params = parse_th6_row(row)
next unless bt_params
bank_transactions.build(bt_params)
end

prepare_dir
self.import_file_path = "#{ENV['bank_statement_import_dir']}/#{Time.zone.now.to_formatted_s(:number)}.txt"
File.open(import_file_path, 'w') { |f| f.write(th6_file.open.read) }
end

def prepare_dir
dirname = ENV['bank_statement_import_dir']
FileUtils.mkdir_p(dirname) unless File.directory?(dirname)
end

def parse_th6_row(row)
return parse_th6_header(row) if row[4, 3].strip == '000'
return if row[4, 3].strip == '999' # skip footer
return unless row[4, 1].strip == '1' # import only transactions
return unless row[266, 2].strip == 'C' # import only Credit transactions

{
paid_at: DateTime.strptime(row[5, 8].strip, '%Y%m%d'),
bank_reference: row[5, 16].strip,
iban: row[25, 20].strip,
currency: row[45, 3].strip,
buyer_bank_code: row[48, 3].strip,
buyer_iban: row[51, 32].strip,
buyer_name: row[83, 35].strip,
document_no: row[118, 8].strip,
description: row[126, 140].strip,
sum: BigDecimal(row[268, 12].strip) / BigDecimal('100.0'),
reference_no: row[280, 35].strip
}
end

def parse_th6_header(row)
self.bank_code = row[7, 3].strip
self.iban = row[10, 20].strip
self.queried_at = DateTime.strptime(row[30, 10].strip, '%y%m%d%H%M')
nil
end
FULLY_BINDED = 'fully_binded'.freeze
PARTIALLY_BINDED = 'partially_binded'.freeze
NOT_BINDED = 'not_binded'.freeze

# TODO: Cache this to database so it can be used for searching
def status
if bank_transactions.unbinded.count == bank_transactions.count
NOT_BINDED
elsif bank_transactions.unbinded.count == 0
elsif bank_transactions.unbinded.count.zero?
FULLY_BINDED
else
PARTIALLY_BINDED
Expand Down
46 changes: 24 additions & 22 deletions app/models/bank_transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,18 @@ def registrar
@registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer
end

def autobindable?
!binded? && registrar && invoice.payable? ? true : false
rescue NoMethodError
false
end

# For successful binding, reference number, invoice id and sum must match with the invoice
def autobind_invoice(manual: false)
return if binded?
return unless registrar
return unless invoice
return unless invoice.payable?

channel = if manual
'admin_payment'
else
'system_payment'
end
create_internal_payment_record(channel: channel, invoice: invoice,
registrar: registrar)
return unless autobindable?

channel = manual ? 'admin_payment' : 'system_payment'
create_internal_payment_record(channel: channel, invoice: invoice, registrar: registrar)
end

def create_internal_payment_record(channel: nil, invoice:, registrar:)
Expand Down Expand Up @@ -93,12 +91,11 @@ def validate_invoice_data(invoice)
end

def create_activity(registrar, invoice)
activity = AccountActivity.new(
account: registrar.cash_account, bank_transaction: self,
invoice: invoice, sum: invoice.subtotal,
currency: currency, description: description,
activity_type: AccountActivity::ADD_CREDIT
)
activity = AccountActivity.new(account: registrar.cash_account, bank_transaction: self,
invoice: invoice, sum: invoice.subtotal,
currency: currency, description: description,
activity_type: AccountActivity::ADD_CREDIT)

if activity.save
reset_pending_registrar_balance_reload
true
Expand All @@ -107,6 +104,10 @@ def create_activity(registrar, invoice)
end
end

def parsed_ref_number
reference_no || ref_number_from_description
end

private

def reset_pending_registrar_balance_reload
Expand All @@ -116,11 +117,12 @@ def reset_pending_registrar_balance_reload
registrar.save!
end

def parsed_ref_number
reference_no || ref_number_from_description
def ref_number_from_description
matches = description.to_s.scan(Billing::ReferenceNo::MULTI_REGEXP).flatten
matches.detect { |m| break m if m.length == 7 || valid_ref_no?(m) }
end

def ref_number_from_description
/(\d{7})/.match(description)[0]
def valid_ref_no?(match)
return true if Billing::ReferenceNo.valid?(match) && Registrar.find_by(reference_no: match)
end
end
1 change: 1 addition & 0 deletions app/models/billing/price.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Billing
class Price < ApplicationRecord
include Concerns::Billing::Price::Expirable
include Versions

belongs_to :zone, class_name: 'DNS::Zone', required: true
has_many :account_activities
Expand Down
8 changes: 7 additions & 1 deletion app/models/billing/reference_no.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
module Billing
class ReferenceNo
REGEXP = /\A\d{2,20}\z/
REGEXP = /\A\d{2,20}\z/.freeze
MULTI_REGEXP = /(\d{2,20})/.freeze

def self.generate
base = Base.generate
"#{base}#{base.check_digit}"
end

def self.valid?(ref)
base = Base.new(ref.to_s[0...-1])
ref.to_s == "#{base}#{base.check_digit}"
end
end
end
Loading

0 comments on commit db77147

Please sign in to comment.