mirror of
https://github.com/internetee/registry.git
synced 2025-07-24 11:38:30 +02:00
refactoring: separate methods from bank_transaction model to concern
This commit is contained in:
parent
28d1560555
commit
1294a05870
3 changed files with 54 additions and 20 deletions
|
@ -1,5 +1,6 @@
|
|||
class BankTransaction < ApplicationRecord
|
||||
include Versions
|
||||
include TransactionPaidInvoices
|
||||
belongs_to :bank_statement
|
||||
has_one :account_activity
|
||||
|
||||
|
@ -17,24 +18,6 @@ class BankTransaction < ApplicationRecord
|
|||
account_activity.invoice
|
||||
end
|
||||
|
||||
def invoice
|
||||
return unless registrar
|
||||
|
||||
@invoice ||= registrar.invoices
|
||||
.order(created_at: :asc)
|
||||
.unpaid
|
||||
.non_cancelled
|
||||
.find_by(total: sum)
|
||||
end
|
||||
|
||||
def non_canceled?
|
||||
paid_invoices = registrar.invoices
|
||||
.order(created_at: :asc)
|
||||
.non_cancelled
|
||||
.where(total: sum)
|
||||
return true if paid_invoices.any?(&:paid?)
|
||||
end
|
||||
|
||||
def registrar
|
||||
@registrar ||= Invoice.find_by(reference_no: parsed_ref_number)&.buyer
|
||||
end
|
||||
|
|
37
app/models/concerns/transaction_paid_invoices.rb
Normal file
37
app/models/concerns/transaction_paid_invoices.rb
Normal file
|
@ -0,0 +1,37 @@
|
|||
module TransactionPaidInvoices
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
def invoice
|
||||
return unless registrar
|
||||
|
||||
@invoice ||= registrar.invoices
|
||||
.order(created_at: :asc)
|
||||
.unpaid
|
||||
.non_cancelled
|
||||
.find_by(total: sum)
|
||||
end
|
||||
|
||||
def non_canceled?
|
||||
paid_invoices = registrar.invoices
|
||||
.order(created_at: :asc)
|
||||
.non_cancelled
|
||||
.where(total: sum)
|
||||
paid_invoices.any? do |invoice|
|
||||
return true if invoice.paid? && fresh_admin_paid_invoice(invoice)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fresh_admin_paid_invoice(invoice)
|
||||
check_for_date_paid_invoice(invoice) && does_invoice_created_by_admin?(invoice)
|
||||
end
|
||||
|
||||
def check_for_date_paid_invoice(invoice)
|
||||
invoice.account_activity.created_at > Time.zone.today - 2.days
|
||||
end
|
||||
|
||||
def does_invoice_created_by_admin?(invoice)
|
||||
invoice.account_activity.creator_str&.include? 'Admin'
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue