Some tests for invoice binding

This commit is contained in:
Martin Lensment 2015-04-14 19:00:38 +03:00
parent e678e655da
commit 57d23976c7
19 changed files with 206 additions and 25 deletions

View file

@ -1,4 +1,12 @@
class AccountActivity < ActiveRecord::Base
belongs_to :account
belongs_to :bank_transaction
belongs_to :invoice
after_create :update_balance
def update_balance
account.balance += sum
account.save
end
end

View file

@ -3,6 +3,8 @@ class BankStatement < ActiveRecord::Base
attr_accessor :th6_file
validates :bank_code, :iban, :queried_at, presence: true
def import
import_th6_file && save
end
@ -47,4 +49,8 @@ class BankStatement < ActiveRecord::Base
self.queried_at = DateTime.strptime(row[30, 10].strip, '%y%m%d%H%M')
nil
end
def bind_with_invoices
bank_transactions.unbinded.each(&:bind_with_invoice)
end
end

View file

@ -1,3 +1,38 @@
class BankTransaction < ActiveRecord::Base
belongs_to :bank_statement
has_one :account_activity
scope :unbinded, -> { where('id NOT IN (SELECT bank_transaction_id FROM account_activities)') }
def binded?
account_activity.present?
end
# For successful binding, reference number, invoice id and sum must match with the invoice
# rubocop: disable Metrics/PerceivedComplexity
# rubocop: disable Metrics/CyclomaticComplexity
def bind_with_invoice
return if binded?
registrar = Registrar.find_by(reference_no: reference_no)
return unless registrar
match = description.match(/^[^\d]*(\d+)/)
return unless match
invoice_id = match[1].to_i
return unless invoice_id
invoice = registrar.invoices.find_by(id: invoice_id)
return unless invoice
return if invoice.sum != sum
create_account_activity(
account: registrar.cash_account,
invoice: invoice,
sum: sum,
currency: currency
)
end
# rubocop: enable Metrics/PerceivedComplexity
# rubocop: enable Metrics/CyclomaticComplexity
end

View file

@ -27,15 +27,15 @@ class Invoice < ActiveRecord::Base
invoice_items
end
def total_without_vat
items.map(&:item_total_without_vat).sum
def sum_without_vat
items.map(&:item_sum_without_vat).sum
end
def total_vat
total_without_vat * vat_prc
def vat
sum_without_vat * vat_prc
end
def total
total_without_vat + total_vat
def sum
sum_without_vat + vat
end
end

View file

@ -1,7 +1,7 @@
class InvoiceItem < ActiveRecord::Base
belongs_to :invoice
def item_total_without_vat
def item_sum_without_vat
amount * price
end
end

View file

@ -5,11 +5,13 @@ class Registrar < ActiveRecord::Base
has_many :contacts, dependent: :restrict_with_error
has_many :api_users, dependent: :restrict_with_error
has_many :messages
belongs_to :country_deprecated, foreign_key: :country_id
has_many :invoices, foreign_key: 'buyer_id'
has_many :accounts
belongs_to :country_deprecated, foreign_key: :country_id
validates :name, :reg_no, :country_code, :email, presence: true
validates :name, :reg_no, uniqueness: true
validates :name, :reg_no, :reference_no, uniqueness: true
validate :set_code, if: :new_record?
before_create :generate_iso_11649_reference_no
@ -99,6 +101,10 @@ class Registrar < ActiveRecord::Base
)
end
def cash_account
accounts.find_by(account_type: Account::CASH)
end
def domain_transfers
at = DomainTransfer.arel_table
DomainTransfer.where(