Class: FeideeUtils::Transaction
Defined Under Namespace
Classes: InconsistentAmountException, InconsistentBuyerAndSellerException, InconsistentCategoryException, ModifiedTransaction, 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",
"modifierTradingEntityPOID",
"ffrom",
"photoName",
"photoNeedUpload",
"relationUnitPOID",
"clientID",
"FSourceKey",
].freeze
Instance Attribute Summary
Attributes inherited from Record
#field, #field_type
#child_classes
Class Method Summary
collapse
Instance Method Summary
collapse
included, #type
Methods inherited from Record
#initialize
#define_accessors, #define_entity_accessor
#collect_subclass, #generate_namespaced_record_classes
#all, #find, #find_by_id, #genereate_names
#last_update_time, #poid
Class Method Details
.validate_global_integrity ⇒ Object
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
|
# File 'lib/feidee_utils/transaction.rb', line 67
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
|
Instance Method Details
#amount ⇒ Object
158
159
160
161
|
# File 'lib/feidee_utils/transaction.rb', line 158
def amount
(buyer_deduction + seller_addition) / 2
end
|
#buyer_deduction ⇒ Object
150
151
152
|
# File 'lib/feidee_utils/transaction.rb', line 150
def buyer_deduction
to_bigdecimal sign_by_type(raw_buyer_deduction)
end
|
#category_poid ⇒ Object
142
143
144
145
|
# File 'lib/feidee_utils/transaction.rb', line 142
def category_poid
buyer_category_poid + seller_category_poid
end
|
#created_at ⇒ Object
126
127
128
|
# File 'lib/feidee_utils/transaction.rb', line 126
def created_at
timestamp_to_time(raw_created_at)
end
|
#has_category? ⇒ Boolean
138
139
140
|
# File 'lib/feidee_utils/transaction.rb', line 138
def has_category?
category_poid != 0
end
|
#is_initial_balance? ⇒ Boolean
167
168
169
|
# File 'lib/feidee_utils/transaction.rb', line 167
def is_initial_balance?
type == :positive_initial_balance or type == :negative_initial_balance
end
|
#is_transfer? ⇒ Boolean
163
164
165
|
# File 'lib/feidee_utils/transaction.rb', line 163
def is_transfer?
type == :transfer_buyer or type == :transfer_seller
end
|
#modified_at ⇒ Object
130
131
132
|
# File 'lib/feidee_utils/transaction.rb', line 130
def modified_at
timestamp_to_time(raw_modified_at)
end
|
#revised_account_poid ⇒ Object
171
172
173
174
175
176
177
178
179
|
# File 'lib/feidee_utils/transaction.rb', line 171
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 ⇒ Object
182
183
184
185
186
187
188
189
190
191
|
# File 'lib/feidee_utils/transaction.rb', line 182
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 ⇒ Object
154
155
156
|
# File 'lib/feidee_utils/transaction.rb', line 154
def seller_addition
to_bigdecimal sign_by_type(raw_seller_addition)
end
|
#trade_at ⇒ Object
134
135
136
|
# File 'lib/feidee_utils/transaction.rb', line 134
def trade_at
timestamp_to_time(raw_trade_at)
end
|
#validate_integrity ⇒ Object
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
|
# 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
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
|