Class: FeideeUtils::Transaction

Inherits:
Record
  • Object
show all
Includes:
Mixins::Type
Defined in:
lib/feidee_utils/transaction.rb

Defined Under Namespace

Classes: InconsistentAmountException, InconsistentBuyerAndSellerException, InconsistentCategoryException, TransferLackBuyerOrSellerException, TransferWithCategoryException, TransfersNotPaired

Constant Summary collapse

FieldMappings =
{
  raw_created_at:         "createdTime",
  raw_modified_at:        "modifiedTime",
  raw_trade_at:           "tradeTime",
  raw_type:               "type",
  memo:                   "memo",
  buyer_account_poid:     "buyerAccountPOID",
  buyer_category_poid:    "buyerCategoryPOID",
  seller_account_poid:    "sellerAccountPOID",
  seller_category_poid:   "sellerCategoryPOID",
  raw_buyer_deduction:    "buyerMoney",
  raw_seller_addition:    "sellerMoney",
  uuid:                   "relation",
}.freeze
IgnoredFields =
[
  "creatorTradingEntityPOID",  # Foreign key to to_user.
  "modifierTradingEntityPOID", # Foreign key to to_user.
  "ffrom",                # The signature of the App.
  "photoName",            # To be added
  "photoNeedUpload",      # To be added
  "relationUnitPOID",     # Foreign key to t_tradingEntity: Merchant.
  "clientID",             # Always 0 for ealier versions, or equal to poid.
  "FSourceKey",           # WTF
].freeze

Constants included from Record::Utils

Record::Utils::AssumedTimezone

Instance Attribute Summary

Attributes included from Record::Namespaced::ClassMethods

#child_classes

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Mixins::Type

included, #type

Methods inherited from Record

generate_subclasses, #initialize

Methods included from Record::Computed::ClassMethods

#computed

Methods included from Record::Persistent::ClassMethods

#all, #column_names, #columns, #find, #find_by_id

Methods included from Record::Accessors

#last_update_time, #poid

Constructor Details

This class inherits a constructor from FeideeUtils::Record

Class Method Details

.validate_global_integrityObject



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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]. == transfers[1].
    valid &&=
      transfers[0]. == transfers[1].
    raise TransfersNotPaired.new([uuid] + transfers) unless valid
  end
end

Instance Method Details

#amountObject



168
169
170
171
# 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_deductionObject

Amount accessors



160
161
162
# File 'lib/feidee_utils/transaction.rb', line 160

def buyer_deduction
  to_bigdecimal sign_by_type(raw_buyer_deduction)
end

#category_poidObject



152
153
154
155
# 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_atObject



136
137
138
# File 'lib/feidee_utils/transaction.rb', line 136

def created_at
  timestamp_to_time(raw_created_at)
end

#has_category?Boolean

Returns:

  • (Boolean)


148
149
150
# File 'lib/feidee_utils/transaction.rb', line 148

def has_category?
  category_poid != 0
end

#is_initial_balance?Boolean

Returns:

  • (Boolean)


177
178
179
# File 'lib/feidee_utils/transaction.rb', line 177

def is_initial_balance?
  type == :positive_initial_balance or type == :negative_initial_balance
end

#is_transfer?Boolean

Returns:

  • (Boolean)


173
174
175
# File 'lib/feidee_utils/transaction.rb', line 173

def is_transfer?
  type == :transfer_buyer or type == :transfer_seller
end

#modified_atObject



140
141
142
# File 'lib/feidee_utils/transaction.rb', line 140

def modified_at
  timestamp_to_time(raw_modified_at)
end

#revised_account_poidObject



181
182
183
184
185
186
187
188
189
# File 'lib/feidee_utils/transaction.rb', line 181

def 
  if type == :transfer_buyer
    
  elsif type == :transfer_seller
    
  else
     + 
  end
end

#revised_amountObject



192
193
194
195
196
197
198
199
200
201
# File 'lib/feidee_utils/transaction.rb', line 192

def revised_amount
   = 
  if  == 
    -buyer_deduction
  elsif  == 
    seller_addition
  else
    raise "Unexpected revised account poid #{}."
  end
end

#seller_additionObject



164
165
166
# File 'lib/feidee_utils/transaction.rb', line 164

def seller_addition
  to_bigdecimal sign_by_type(raw_seller_addition)
end

#to_sObject



203
204
205
206
207
208
209
210
211
212
213
# 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 #{} to #{}"
  elsif is_initial_balance?
    "Balance of #{} set to #{revised_amount.to_f}"
  else
    "Entry of #{revised_amount.to_f} on #{} in #{category}"
  end
  str + " at #{trade_at} (Transaction #{poid})";
end

#trade_atObject



144
145
146
# File 'lib/feidee_utils/transaction.rb', line 144

def trade_at
  timestamp_to_time(raw_trade_at)
end

#validate_integrityObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/feidee_utils/transaction.rb', line 23

def validate_integrity
  if is_transfer?
    unless  != 0 and  != 0
      raise TransferLackBuyerOrSellerException,
        "Both buyer and seller should be set in a transfer. " +
        "Buyer 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 ( == 0) ^ ( == 0)
      raise InconsistentBuyerAndSellerException,
        "Exactly one of buyer and seller should be set in a non-transfer " +
        "transaction. " +
        "Buyer 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