class FeideeUtils::Transaction

Constants

FieldMappings
IgnoredFields

Public Class Methods

validate_global_integrity() click to toggle source
# File lib/feidee_utils/transaction.rb, line 75
def self.validate_global_integrity
  uuids_map = all.inject({}) do |uuids, transaction|
    if transaction.is_transfer?
      uuid = transaction.uuid
      uuids[uuid] ||= [nil, nil]
      uuids[uuid][transaction.raw_type - 2] = transaction
    end
    uuids
  end

  uuids_map.each do |uuid, transfers|
    valid = true
    valid &&= transfers[0] != nil
    valid &&= transfers[1] != nil
    valid &&=
      transfers[0].buyer_account_poid == transfers[1].buyer_account_poid
    valid &&=
      transfers[0].seller_account_poid == transfers[1].seller_account_poid
    raise TransfersNotPaired.new([uuid] + transfers) unless valid
  end
end

Public Instance Methods

amount() click to toggle source
# File lib/feidee_utils/transaction.rb, line 168
def amount
  # Buyer deduction is always equal to seller addition.
  (buyer_deduction + seller_addition) / 2
end
buyer_deduction() click to toggle source

Amount accessors

# File lib/feidee_utils/transaction.rb, line 160
def buyer_deduction
  to_bigdecimal sign_by_type(raw_buyer_deduction)
end
category_poid() click to toggle source
# File lib/feidee_utils/transaction.rb, line 152
def category_poid
  # At least one of those two must be 0.
  buyer_category_poid + seller_category_poid
end
created_at() click to toggle source
# File lib/feidee_utils/transaction.rb, line 136
def created_at
  timestamp_to_time(raw_created_at)
end
has_category?() click to toggle source
# File lib/feidee_utils/transaction.rb, line 148
def has_category?
  category_poid != 0
end
is_initial_balance?() click to toggle source
# File lib/feidee_utils/transaction.rb, line 177
def is_initial_balance?
  type == :positive_initial_balance or type == :negative_initial_balance
end
is_transfer?() click to toggle source
# File lib/feidee_utils/transaction.rb, line 173
def is_transfer?
  type == :transfer_buyer or type == :transfer_seller
end
modified_at() click to toggle source
# File lib/feidee_utils/transaction.rb, line 140
def modified_at
  timestamp_to_time(raw_modified_at)
end
revised_account_poid() click to toggle source
# File lib/feidee_utils/transaction.rb, line 181
def revised_account_poid
  if type == :transfer_buyer
    buyer_account_poid
  elsif type == :transfer_seller
    seller_account_poid
  else
    buyer_account_poid + seller_account_poid
  end
end
revised_amount() click to toggle source
# File lib/feidee_utils/transaction.rb, line 192
def revised_amount
  account_poid = revised_account_poid
  if account_poid == buyer_account_poid
    -buyer_deduction
  elsif account_poid == seller_account_poid
    seller_addition
  else
    raise "Unexpected revised account poid #{account_poid}."
  end
end
seller_addition() click to toggle source
# File lib/feidee_utils/transaction.rb, line 164
def seller_addition
  to_bigdecimal sign_by_type(raw_seller_addition)
end
to_s() click to toggle source
# File lib/feidee_utils/transaction.rb, line 203
def to_s
  str = if is_transfer?
    (type == :transfer_buyer ? "Buyer" : "Seller") +
    " transfer #{amount.to_f} from #{buyer_account} to #{seller_account}"
  elsif is_initial_balance?
    "Balance of #{revised_account} set to #{revised_amount.to_f}"
  else
    "Entry of #{revised_amount.to_f} on #{revised_account} in #{category}"
  end
  str + " at #{trade_at} (Transaction #{poid})";
end
trade_at() click to toggle source
# File lib/feidee_utils/transaction.rb, line 144
def trade_at
  timestamp_to_time(raw_trade_at)
end
validate_integrity() click to toggle source
# File lib/feidee_utils/transaction.rb, line 23
def validate_integrity
  if is_transfer?
    unless buyer_account_poid != 0 and seller_account_poid != 0
      raise TransferLackBuyerOrSellerException,
        "Both buyer and seller should be set in a transfer. " +
        "Buyer account POID: #{buyer_account_poid}. " +
        "Seller account POID: #{seller_account_poid}.\n" +
        inspect
    end
    unless buyer_category_poid == 0 and seller_category_poid == 0
      raise TransferWithCategoryException,
        "Neither buyer or seller category should be set in a transfer. " +
        "Buyer category POID: #{buyer_category_poid}. " +
        "Seller category POID: #{seller_category_poid}.\n" +
        inspect
    end
  else
    unless (buyer_account_poid == 0) ^ (seller_account_poid == 0)
      raise InconsistentBuyerAndSellerException,
        "Exactly one of buyer and seller should be set in a non-transfer " +
        "transaction. " +
        "Buyer account POID: #{buyer_account_poid}. " +
        "Seller account POID: #{seller_account_poid}.\n" +
        inspect
    end

    # We could enforce that category is set to the matching party (buyer or
    # seller) of account. However the implementation could handle all
    # situations, as long as only one of them is set. Thus no extra check is
    # done here.
    unless buyer_category_poid == 0 or seller_category_poid == 0
      raise InconsistentCategoryException,
        "Only one of buyer and seller category should be set in a " +
        "non-transfer transaction. " +
        "Buyer category POID: #{buyer_category_poid}. " +
        "Seller category POID: #{seller_category_poid}.\n" +
        inspect
    end
  end

  unless raw_buyer_deduction == raw_seller_addition
    raise InconsistentAmountException,
      "Buyer and seller should have the same amount set. " +
      "Buyer deduction: #{buyer_deduction}. "+
      "Seller_addition: #{seller_addition}.\n" +
    inspect
  end
end

Private Instance Methods

sign_by_type(num) click to toggle source
# File lib/feidee_utils/transaction.rb, line 216
def sign_by_type num
  # This is awkward. For transactions of type positive_initial_balance, the
  # buyer is always 0, but the seller have to **add** the amount shown as
  # sellerMoney, which is consistent with other type of transactions.
  #
  # Whereas for transactions of type negative_initial_balance (9),  the
  # buyer is always 0, and the seller will have to **deduct** the amount
  # shown as sellerMoney from balance. Here the sign of
  # negative_initial_balance is reversed to reflect this awkwardness.
  raw_type == 9 ? -num : num
end