The personal finance app for everyone (by everyone)
Ruby
# frozen_string_literal: true
require "digest/md5"
require "digest/sha2"
require "stringio"
class AccountStatement < ApplicationRecord
include Monetizable
DuplicateUploadError = Class.new(StandardError) do
attr_reader :statement
def initialize(statement)
@statement = statement
super("Statement file has already been uploaded")
end
end
InvalidUploadError = Class.new(StandardError)
PreparedUpload = Data.define(:content, :filename, :content_type, :byte_size, :checksum, :content_sha256)
BALANCE_FIELDS = %w[opening_balance closing_balance].freeze
MAX_FILE_SIZE = 25.megabytes
READ_CHUNK_SIZE = 1.megabyte
ALLOWED_EXTENSION_CONTENT_TYPES = {
".pdf" => %w[application/pdf],
".csv" => %w[text/csv text/plain application/csv application/vnd.ms-excel],
".xlsx" => %w[application/vnd.openxmlformats-officedocument.spreadsheetml.sheet]
}.freeze
ALLOWED_CONTENT_TYPES = ALLOWED_EXTENSION_CONTENT_TYPES.values.flatten.uniq.freeze
ACCEPTED_FILE_EXTENSIONS = ALLOWED_EXTENSION_CONTENT_TYPES.keys.freeze
belongs_to :family
belongs_to :account, optional: true
belongs_to :suggested_account, class_name: "Account", optional: true
# An import is as private as its statement, so a statement stays while any
# import carries it. The foreign key refuses the DELETE too, for an import
# committed after this check.
has_many :imports, dependent: :restrict_with_error
has_many :pdf_imports, -> { where(type: "PdfImport").ordered }, class_name: "PdfImport"
has_one_attached :original_file, dependent: :purge_later
enum :source, { manual_upload: "manual_upload" }, validate: true, default: "manual_upload"
enum :upload_status, { stored: "stored", failed: "failed" }, validate: true, default: "stored"
enum :review_status, { unmatched: "unmatched", linked: "linked", rejected: "rejected" }, validate: true, default: "unmatched", scopes: false
monetize :opening_balance, :closing_balance
before_validation :sync_file_metadata, if: -> { original_file.attached? }
before_validation :normalize_currency
before_validation :sync_review_status
before_validation :note_edited_balances
before_validation :derive_detected_balances
validates :filename, :content_type, :checksum, presence: true
validates :byte_size, presence: true, numericality: { greater_than: 0, less_than_or_equal_to: MAX_FILE_SIZE }
validates :content_type, inclusion: { in: ALLOWED_CONTENT_TYPES }
validates :content_sha256,
format: { with: /\A[0-9a-f]{64}\z/ },
uniqueness: { scope: :family_id, allow_nil: true, message: :duplicate_statement_file },
allow_nil: true
validates :parser_confidence, :match_confidence, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 1 }, allow_nil: true
validate :account_belongs_to_family
validate :suggested_account_belongs_to_family
validate :period_order
validate :currency_is_valid
validate :filename_extension_matches_content_type
validate :original_file_attached
validate :original_file_constraints, if: -> { original_file.attached? }
scope :ordered, -> { order(created_at: :desc) }
# Statements with a known period first, newest period first. A period
# counts as known only with both ends, as account_statement_period shows
# it; the rest follow in upload order, which also breaks ties.
scope :by_period, -> {
known = "account_statements.period_start_on IS NOT NULL AND account_statements.period_end_on IS NOT NULL"
order(
Arel.sql("CASE WHEN #{known} THEN 0 ELSE 1 END"),
Arel.sql("CASE WHEN #{known} THEN account_statements.period_end_on END DESC"),
Arel.sql("CASE WHEN #{known} THEN account_statements.period_start_on END DESC"),
created_at: :desc
)
}
scope :with_account, -> { where.not(account_id: nil) }
# viewable_by? in SQL, so lists filter before they paginate: a linked
# statement follows its account, an unlinked one is for statement managers.
scope :viewable_by, ->(user) {
family_statements = where(family_id: user.family_id)
linked = family_statements.where(account_id: user.accessible_accounts.select(:id))
statement_manager?(user) ? linked.or(family_statements.where(account_id: nil)) : linked
}
scope :unmatched, -> { where(account_id: nil).where(review_status: "unmatched") }
# Rejecting a suggestion leaves the statement accountless, so it stays in the inbox where it can be found and deleted.
scope :inbox, -> { where(account_id: nil).where(review_status: %w[unmatched rejected]) }
scope :for_month, ->(month) {
month_start = month.to_date.beginning_of_month
month_end = month_start.end_of_month
where("period_start_on <= ? AND period_end_on >= ?", month_end, month_start)
}
class << self
def statement_manager?(user)
user&.admin? || user&.member?
end
def create_from_upload!(family:, account:, file:)
prepared_upload = prepare_upload!(file)
create_from_prepared_upload!(family: family, account: account, prepared_upload: prepared_upload)
end
def create_from_prepared_upload!(family:, account:, prepared_upload:)
statement = nil
duplicate = duplicate_for(family, prepared_upload)
raise DuplicateUploadError, duplicate if duplicate
statement = family.account_statements.build(
account: account,
filename: prepared_upload.filename,
content_type: prepared_upload.content_type,
byte_size: prepared_upload.byte_size,
checksum: prepared_upload.checksum,
content_sha256: prepared_upload.content_sha256,
source: :manual_upload,
upload_status: :stored,
review_status: account.present? ? :linked : :unmatched,
currency: account&.currency || family.currency
)
statement.original_file.attach(
io: StringIO.new(prepared_upload.content),
filename: prepared_upload.filename,
content_type: prepared_upload.content_type
)
MetadataDetector.new(statement, content: prepared_upload.content).apply
statement.assign_account_match unless account.present?
statement.save!
statement
rescue ActiveRecord::RecordNotUnique
duplicate = duplicate_for(family, prepared_upload)
purge_original_file(statement)
if duplicate
raise DuplicateUploadError, duplicate
end
raise
rescue StandardError
purge_original_file(statement)
raise
end
def reconciliation_statuses_for(statements, account:)
statement_list = statements.to_a
balance_lookup = balance_lookup_for(account, statement_list)
statement_list.to_h do |statement|
[ statement.id, statement.reconciliation_status(balance_lookup: balance_lookup) ]
end
end
def prepare_upload!(file)
filename = file.original_filename.to_s
content = read_upload_content!(file)
byte_size = content.bytesize
raise InvalidUploadError if byte_size.zero?
content_type = detected_content_type(content:, filename:, declared_content_type: file.content_type)
raise InvalidUploadError unless allowed_upload?(filename:, content_type:)
raise InvalidUploadError if content_type == "application/pdf" && !valid_pdf_content?(content)
PreparedUpload.new(
content: content,
filename: filename,
content_type: content_type,
byte_size: byte_size,
checksum: Digest::MD5.base64digest(content),
content_sha256: Digest::SHA256.hexdigest(content)
)
end
def detected_content_type(content:, filename:, declared_content_type:)
Marcel::MimeType.for(
StringIO.new(content),
name: filename,
declared_type: declared_content_type.presence
)
end
def allowed_upload?(filename:, content_type:)
allowed_content_types_for_filename(filename).include?(content_type)
end
def allowed_content_types_for_filename(filename)
ALLOWED_EXTENSION_CONTENT_TYPES.fetch(File.extname(filename.to_s).downcase, [])
end
def valid_pdf_content?(content)
content.start_with?("%PDF-")
end
def purge_original_file(statement)
return unless statement&.original_file&.attached?
statement.original_file.purge
rescue StandardError => e
Rails.logger.warn("AccountStatement staged blob cleanup failed: #{e.class}: #{e.message}")
end
def balance_lookup_for(account, statements)
currencies = statements.map(&:statement_currency).compact.uniq
dates = statements.flat_map { |statement| [ statement.period_start_on, statement.period_end_on ] }.compact.uniq
balances = if currencies.any? && dates.any?
account.balances.where(currency: currencies, date: dates).to_a
else
[]
end
balances_by_key = balances.index_by { |balance| [ balance.date, balance.currency ] }
->(date, currency) { balances_by_key[[ date, currency ]] }
end
def read_upload_content!(file)
declared_size = declared_upload_size(file)
raise InvalidUploadError if declared_size.present? && declared_size > MAX_FILE_SIZE
content = +"".b
loop do
chunk = file.read(READ_CHUNK_SIZE)
break if chunk.nil? || chunk.empty?
content << chunk
raise InvalidUploadError if content.bytesize > MAX_FILE_SIZE
end
file.rewind if file.respond_to?(:rewind)
content
end
def declared_upload_size(file)
if file.respond_to?(:size)
file.size
elsif file.respond_to?(:length)
file.length
end
end
def duplicate_for(family, prepared_upload)
scope = family.account_statements
sha_duplicate = scope.find_by(content_sha256: prepared_upload.content_sha256) if prepared_upload.content_sha256.present?
return sha_duplicate if sha_duplicate
# Active Storage's MD5 checksum is retained only to catch legacy rows that predate content_sha256.
legacy_scope = prepared_upload.content_sha256.present? ? scope.where(content_sha256: nil) : scope
legacy_scope.find_by(checksum: prepared_upload.checksum)
end
end
def viewable_by?(user)
return false unless user&.family_id == family_id
account.present? ? account.shared_with?(user) : self.class.statement_manager?(user)
end
def manageable_by?(user)
return false unless user&.family_id == family_id
return self.class.statement_manager?(user) if account.blank?
account.permission_for(user).in?([ :owner, :full_control ]) && self.class.statement_manager?(user)
end
def link_to_account!(target_account, confidence: 1.0)
update!(
account: target_account,
suggested_account: nil,
match_confidence: confidence,
review_status: :linked,
currency: currency.presence || target_account.currency
)
end
def unlink!
transaction do
update!(
account: nil,
review_status: :unmatched,
match_confidence: nil
)
assign_account_match
save!
end
end
def reject_match!
update!(
suggested_account: nil,
match_confidence: nil,
review_status: :rejected
)
end
def assign_account_match
match = AccountMatcher.new(self).best_match
self.suggested_account = match&.account
self.match_confidence = match&.confidence
clear_invalid_suggested_account
end
def covered_months
return [] unless period_start_on.present? && period_end_on.present?
current = period_start_on.beginning_of_month
last = period_end_on.beginning_of_month
months = []
while current <= last
months << current
current = current.next_month
end
months
end
def covers_month?(month)
covered_months.include?(month.to_date.beginning_of_month)
end
def reconciliation_status(balance_lookup: nil)
checks = reconciliation_checks(balance_lookup: balance_lookup)
return "unavailable" if checks.empty?
checks.any? { |check| check[:status] == "mismatched" } ? "mismatched" : "matched"
end
def reconciliation_mismatched?(balance_lookup: nil)
reconciliation_status(balance_lookup: balance_lookup) == "mismatched"
end
def reconciliation_checks(balance_lookup: nil)
return [] unless account.present? && period_start_on.present? && period_end_on.present?
checks = []
opening_balance_record = balance_record_for(period_start_on, statement_currency, balance_lookup)
closing_balance_record = balance_record_for(period_end_on, statement_currency, balance_lookup)
if opening_balance.present? && opening_balance_record.present?
checks << reconciliation_check(
key: "opening_balance",
statement_amount: opening_balance,
ledger_amount: opening_balance_record.start_balance
)
end
if closing_balance.present? && closing_balance_record.present?
checks << reconciliation_check(
key: "closing_balance",
statement_amount: closing_balance,
ledger_amount: closing_balance_record.end_balance
)
end
if opening_balance.present? && closing_balance.present? && opening_balance_record.present? && closing_balance_record.present?
checks << reconciliation_check(
key: "period_movement",
statement_amount: closing_balance - opening_balance,
ledger_amount: closing_balance_record.end_balance - opening_balance_record.start_balance
)
end
checks
end
def statement_currency
currency.presence || account&.currency || family.currency
end
def pdf?
content_type.in?(ALLOWED_EXTENSION_CONTENT_TYPES[".pdf"])
end
def csv?
content_type.in?(ALLOWED_EXTENSION_CONTENT_TYPES[".csv"])
end
def xlsx?
content_type.in?(ALLOWED_EXTENSION_CONTENT_TYPES[".xlsx"])
end
def latest_reusable_pdf_import
pdf_imports.where.not(status: :failed).order(created_at: :desc).first
end
# Whether `amount` is the figure the statement text already gave for this
# balance in `currency`, whatever its sign, and the text marked it CR or DR:
# the detected value, signed for the account, then stands.
def detected_balance?(field, amount, currency: statement_currency)
field = field.to_s
readings = pdf_output["balance_readings"]
return false if readings.blank? || edited_balances.include?(field)
balances = TextMetadataParser.balances_for(readings, classification: account&.classification, currency: currency)
return false if balances.nil? || readings.dig(balances.source, field, "side").blank?
detected = balances.public_send(field)
detected.present? && amount.present? && detected.to_d.abs == amount.to_d.abs
end
# Records the assigned balances in `fields` as edited, so they are never
# worked out again, unless the statement text gives the same figure in the
# statement's currency. Unlike note_edited_balances, a value equal to an
# earlier derivation still counts: the currency may have changed since.
def mark_balances_edited(fields)
readings = pdf_output["balance_readings"]
return if readings.blank?
derived = TextMetadataParser.balances_for(readings, classification: account&.classification, currency: statement_currency)
edited = fields.map(&:to_s).select { |field| derived&.public_send(field) != self[field] }
merge_pdf_output("edited_balances" => (edited_balances | edited)) if edited.any?
end
private
def reconciliation_check(key:, statement_amount:, ledger_amount:)
difference = statement_amount.to_d - ledger_amount.to_d
{
key: key,
statement_amount: statement_amount.to_d,
ledger_amount: ledger_amount.to_d,
difference: difference,
status: difference.abs <= 0.01.to_d ? "matched" : "mismatched"
}
end
def balance_record_for(date, currency, balance_lookup)
return balance_lookup.call(date, currency) if balance_lookup
account.balances.find_by(date: date, currency: currency)
end
def sync_file_metadata
blob = original_file.blob
self.filename ||= blob.filename.to_s
self.content_type ||= blob.content_type
self.byte_size ||= blob.byte_size
self.checksum ||= blob.checksum
end
def normalize_currency
self.currency = currency.to_s.upcase.presence if currency.present?
end
def pdf_output
sanitized_parser_output.to_h["pdf"].to_h
end
def edited_balances
Array(pdf_output["edited_balances"])
end
def merge_pdf_output(values)
self.sanitized_parser_output = sanitized_parser_output.to_h.merge("pdf" => pdf_output.merge(values))
end
# A balance written by anyone but derive_detected_balances (the form, the
# AI extraction) is theirs from then on and is never worked out again.
# A value equal to what the derivation wrote, or would write now, is not
# an edit, so a save that fails validation and is retried, in the same
# object or through the form, keeps its balances derived.
def note_edited_balances
readings = pdf_output["balance_readings"]
return if readings.blank?
derived = TextMetadataParser.balances_for(readings, classification: account&.classification, currency: statement_currency)
edited = BALANCE_FIELDS.select do |field|
will_save_change_to_attribute?(field) &&
!(@derived_balances&.key?(field) && @derived_balances[field] == self[field]) &&
derived&.public_send(field) != self[field]
end
merge_pdf_output("edited_balances" => (edited_balances | edited)) if edited.any?
end
# The detected balances follow the account and the currency: their CR/DR
# sign and whether the summary table adds up come from the account's
# classification, and "$" is read in the statement's currency. So they are
# worked out from the stored readings on every validation, which leaves an
# unedited balance as it was unless the account or currency changed. An
# edited balance is left as it is.
def derive_detected_balances
readings = pdf_output["balance_readings"]
return if readings.blank?
balances = TextMetadataParser.balances_for(readings, classification: account&.classification, currency: statement_currency)
@derived_balances = (BALANCE_FIELDS - edited_balances).to_h do |field|
self[field] = balances&.public_send(field)
[ field, self[field] ]
end
merge_pdf_output(
"balances_source" => balances&.source,
"balances_detected" => opening_balance.present? && closing_balance.present?
)
end
def sync_review_status
return if rejected?
self.review_status = "linked" if account.present? && !linked?
self.review_status = "unmatched" if account.blank? && linked?
end
def account_belongs_to_family
return if account.nil?
return if account.family_id == family_id
errors.add(:account, :invalid)
end
def suggested_account_belongs_to_family
return if suggested_account_valid_for_family?
errors.add(:suggested_account, :invalid)
end
def clear_invalid_suggested_account
return if suggested_account_valid_for_family?
self.suggested_account = nil
self.match_confidence = nil
end
def suggested_account_valid_for_family?
suggested_account.nil? || suggested_account.family_id == family_id
end
def period_order
return if period_start_on.blank? || period_end_on.blank?
return if period_start_on <= period_end_on
errors.add(:period_end_on, :on_or_after_start)
end
def currency_is_valid
return if currency.blank?
Money::Currency.new(currency)
rescue Money::Currency::UnknownCurrencyError, ArgumentError
errors.add(:currency, :invalid)
end
def filename_extension_matches_content_type
return if filename.blank? || content_type.blank?
return if self.class.allowed_upload?(filename: filename, content_type: content_type)
errors.add(:content_type, :invalid)
end
def original_file_constraints
if original_file.byte_size.zero?
errors.add(:original_file, :blank)
elsif original_file.byte_size > MAX_FILE_SIZE
errors.add(:original_file, :too_large, max_mb: MAX_FILE_SIZE / 1.megabyte)
end
unless self.class.allowed_upload?(filename: original_file.filename.to_s, content_type: original_file.content_type)
errors.add(:original_file, :invalid_format, file_format: original_file.content_type)
end
end
def original_file_attached
errors.add(:original_file, :blank) unless original_file.attached?
end
end