Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion apps/hellgate/src/hellgate.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
genlib,
fault_detector_proto,
hg_proto,
shumpune_proto,
shumaich_proto,
cowboy,
how_are_you, % must be after ranch and before any woody usage
woody,
Expand Down
145 changes: 88 additions & 57 deletions apps/hellgate/src/hg_accounting.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,33 @@
-module(hg_accounting).

-export([get_account/1]).
-export([get_account/2]).
-export([get_balance/1]).
-export([get_balance/2]).
-export([create_account/1]).
-export([create_account/2]).
% creating is lazy with shumaich
% -export([create_account/1]).
% -export([create_account/2]).
-export([collect_account_map/6]).
-export([collect_merchant_account_map/2]).
-export([collect_provider_account_map/3]).
-export([collect_system_account_map/5]).
-export([collect_external_account_map/4]).

-export([hold/2]).
-export([hold/3]).

-export([plan/2]).
-export([plan/3]).

-export([commit/2]).
-export([commit/3]).

-export([rollback/2]).
-export([rollback/3]).

-include_lib("damsel/include/dmsl_payment_processing_thrift.hrl").
-include_lib("damsel/include/dmsl_domain_thrift.hrl").
-include_lib("shumpune_proto/include/shumpune_shumpune_thrift.hrl").
-include_lib("shumpune_proto/include/shumaich_shumaich_thrift.hrl").

-type amount() :: dmsl_domain_thrift:'Amount'().
-type currency_code() :: dmsl_domain_thrift:'CurrencySymbolicCode'().
Expand All @@ -34,7 +43,7 @@
-type batch_id() :: dmsl_accounter_thrift:'BatchID'().
-type final_cash_flow() :: dmsl_domain_thrift:'FinalCashFlow'().
-type batch() :: {batch_id(), final_cash_flow()}.
-type clock() :: shumpune_shumpune_thrift:'Clock'().
-type clock() :: shumaich_shumaich_thrift:'Clock'().

-type payment() :: dmsl_domain_thrift:'InvoicePayment'().
-type shop() :: dmsl_domain_thrift:'Shop'().
Expand All @@ -59,67 +68,68 @@

-spec get_account(account_id()) ->
account().

get_account(AccountID) ->
case call_accounter('GetAccountByID', [AccountID]) of
get_account(AccountID, {latest, #shumaich_LatestClock{}}).

-spec get_account(account_id(), clock()) ->
account().
get_account(AccountID, Clock) ->
case call_accounter('GetAccountByID', [AccountID, Clock]) of
{ok, Result} ->
construct_account(AccountID, Result);
{exception, #shumpune_AccountNotFound{}} ->
{exception, #shumaich_AccountNotFound{}} ->
hg_woody_wrapper:raise(#payproc_AccountNotFound{})
end.

-spec get_balance(account_id()) ->
balance().

get_balance(AccountID) ->
get_balance(AccountID, {latest, #shumpune_LatestClock{}}).
get_balance(AccountID, {latest, #shumaich_LatestClock{}}).

-spec get_balance(account_id(), clock()) ->
balance().

get_balance(AccountID, Clock) ->
case call_accounter('GetBalanceByID', [AccountID, Clock]) of
{ok, Result} ->
construct_balance(AccountID, Result);
{exception, #shumpune_AccountNotFound{}} ->
{exception, #shumaich_AccountNotFound{}} ->
hg_woody_wrapper:raise(#payproc_AccountNotFound{})
end.

-spec create_account(currency_code()) ->
account_id().
% DEPRECATED

create_account(CurrencyCode) ->
create_account(CurrencyCode, undefined).
% -spec create_account(currency_code()) ->
% account_id().
% create_account(CurrencyCode) ->
% create_account(CurrencyCode, undefined).

-spec create_account(currency_code(), binary() | undefined) ->
account_id().
% -spec create_account(currency_code(), binary() | undefined) ->
% account_id().
% create_account(CurrencyCode, Description) ->
% case call_accounter('CreateAccount', [construct_prototype(CurrencyCode, Description)]) of
% {ok, Result} ->
% Result;
% {exception, Exception} ->
% error({accounting, Exception}) % FIXME
% end.

create_account(CurrencyCode, Description) ->
case call_accounter('CreateAccount', [construct_prototype(CurrencyCode, Description)]) of
{ok, Result} ->
Result;
{exception, Exception} ->
error({accounting, Exception}) % FIXME
end.
% DEPRECATED

-spec collect_account_map(payment(), shop(), payment_institution(), provider(), varset(), revision()) -> map().

collect_account_map(Payment, Shop, PaymentInstitution, Provider, VS, Revision) ->
Map0 = collect_merchant_account_map(Shop, #{}),
Map1 = collect_provider_account_map(Payment, Provider, Map0),
Map2 = collect_system_account_map(Payment, PaymentInstitution, VS, Revision, Map1),
collect_external_account_map(Payment, VS, Revision, Map2).

-spec collect_merchant_account_map(shop(), map()) -> map().

collect_merchant_account_map(#domain_Shop{account = MerchantAccount}, Acc) ->
Acc#{
{merchant, settlement} => MerchantAccount#domain_ShopAccount.settlement,
{merchant, guarantee } => MerchantAccount#domain_ShopAccount.guarantee
}.

-spec collect_provider_account_map(payment(), provider(), map()) -> map().

collect_provider_account_map(Payment, #domain_Provider{accounts = ProviderAccounts}, Acc) ->
Currency = get_currency(get_payment_cost(Payment)),
ProviderAccount = hg_payment_institution:choose_provider_account(Currency, ProviderAccounts),
Expand All @@ -128,7 +138,6 @@ collect_provider_account_map(Payment, #domain_Provider{accounts = ProviderAccoun
}.

-spec collect_system_account_map(payment(), payment_institution(), varset(), revision(), map()) -> map().

collect_system_account_map(Payment, PaymentInstitution, VS, Revision, Acc) ->
Currency = get_currency(get_payment_cost(Payment)),
SystemAccount = hg_payment_institution:get_system_account(Currency, VS, Revision, PaymentInstitution),
Expand All @@ -138,7 +147,6 @@ collect_system_account_map(Payment, PaymentInstitution, VS, Revision, Acc) ->
}.

-spec collect_external_account_map(payment(), varset(), revision(), map()) -> map().

collect_external_account_map(Payment, VS, Revision, Acc) ->
Currency = get_currency(get_payment_cost(Payment)),
case hg_payment_institution:choose_external_account(Currency, VS, Revision) of
Expand All @@ -151,67 +159,83 @@ collect_external_account_map(Payment, VS, Revision, Acc) ->
Acc
end.

construct_prototype(CurrencyCode, Description) ->
#shumpune_AccountPrototype{
currency_sym_code = CurrencyCode,
description = Description
}.
% construct_prototype(CurrencyCode, Description) ->
% #shumaich_AccountPrototype{
% currency_sym_code = CurrencyCode,
% description = Description
% }.

%%
-spec plan(plan_id(), [batch()]) ->
clock().
plan(PlanID, Batches) ->
plan(PlanID, Batches, {latest, #shumaich_LatestClock{}}).

plan(_PlanID, []) ->
-spec plan(plan_id(), [batch()], clock()) ->
clock().
plan(_PlanID, [], _Clock) ->
error(badarg);
plan(_PlanID, Batches) when not is_list(Batches) ->
plan(_PlanID, Batches, _Clock) when not is_list(Batches) ->
error(badarg);
plan(PlanID, Batches) ->
plan(PlanID, Batches, Clock) ->
lists:foldl(
fun (Batch, _) -> hold(PlanID, Batch) end,
fun (Batch, _) -> hold(PlanID, Batch, Clock) end,
undefined,
Batches
).

-spec hold(plan_id(), batch()) ->
clock().

hold(PlanID, Batch) ->
do('Hold', construct_plan_change(PlanID, Batch)).
hold(PlanID, Batch, {latest, #shumaich_LatestClock{}}).

-spec commit(plan_id(), [batch()]) ->
-spec hold(plan_id(), batch(), clock()) ->
clock().
hold(PlanID, Batch, Clock) ->
do('Hold', construct_plan_change(PlanID, Batch), Clock).

-spec commit(plan_id(), [batch()]) ->
clock().
commit(PlanID, Batches) ->
do('CommitPlan', construct_plan(PlanID, Batches)).
commit(PlanID, Batches, {latest, #shumaich_LatestClock{}}).

-spec rollback(plan_id(), [batch()]) ->
-spec commit(plan_id(), [batch()], clock()) ->
clock().
commit(PlanID, Batches, Clock) ->
do('CommitPlan', construct_plan(PlanID, Batches), Clock).

-spec rollback(plan_id(), [batch()]) ->
clock().
rollback(PlanID, Batches) ->
do('RollbackPlan', construct_plan(PlanID, Batches)).
rollback(PlanID, Batches, {latest, #shumaich_LatestClock{}}).

do(Op, Plan) ->
case call_accounter(Op, [Plan]) of
-spec rollback(plan_id(), [batch()], clock()) ->
clock().
rollback(PlanID, Batches, Clock) ->
do('RollbackPlan', construct_plan(PlanID, Batches), Clock).

do(Op, Plan, Clock) ->
case call_accounter(Op, [Plan, Clock]) of
{ok, Clock} ->
Clock;
{exception, Exception} ->
error({accounting, Exception}) % FIXME
end.

construct_plan_change(PlanID, {BatchID, Cashflow}) ->
#shumpune_PostingPlanChange{
#shumaich_PostingPlanChange{
id = PlanID,
batch = #shumpune_PostingBatch{
batch = #shumaich_PostingBatch{
id = BatchID,
postings = collect_postings(Cashflow)
}
}.

construct_plan(PlanID, Batches) ->
#shumpune_PostingPlan{
#shumaich_PostingPlan{
id = PlanID,
batch_list = [
#shumpune_PostingBatch{
#shumaich_PostingBatch{
id = BatchID,
postings = collect_postings(Cashflow)
}
Expand All @@ -220,11 +244,15 @@ construct_plan(PlanID, Batches) ->

collect_postings(Cashflow) ->
[
#shumpune_Posting{
from_id = Source,
to_id = Destination,
#shumaich_Posting{
% NEW
from_account = construct_posting_account(Source, CurrencyCode),
to_account = construct_posting_account(Destination, CurrencyCode),
% OLD
% from_id = Source,
% to_id = Destination,
amount = Amount,
currency_sym_code = CurrencyCode,
currency_symbolic_code = CurrencyCode,
description = construct_posting_description(Details)
}
|| #domain_FinalCashFlowPosting{
Expand All @@ -238,6 +266,9 @@ collect_postings(Cashflow) ->
} <- Cashflow
].

construct_posting_account(ID, Currency) ->
#shumaich_Account{id = integer_to_binary(ID), currency_symbolic_code = Currency}.

construct_posting_description(Details) when is_binary(Details) ->
Details;
construct_posting_description(undefined) ->
Expand All @@ -247,8 +278,8 @@ construct_posting_description(undefined) ->

construct_account(
AccountID,
#shumpune_Account{
currency_sym_code = CurrencyCode
#shumaich_Account{
currency_symbolic_code = CurrencyCode
}
) ->
#{
Expand All @@ -258,7 +289,7 @@ construct_account(

construct_balance(
AccountID,
#shumpune_Balance{
#shumaich_Balance{
own_amount = OwnAmount,
min_available_amount = MinAvailableAmount,
max_available_amount = MaxAvailableAmount
Expand Down
25 changes: 14 additions & 11 deletions apps/hellgate/test/hg_ct_fixture.erl
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,9 @@ construct_contract_template(Ref, TermsRef, ValidSince, ValidUntil) ->
construct_provider_account_set(Currencies) ->
ok = hg_context:save(hg_context:create()),
AccountSet = lists:foldl(
fun (Cur = ?cur(Code), Acc) ->
Acc#{Cur => ?prvacc(hg_accounting:create_account(Code))}
fun (Cur = ?cur(_Code), Acc) ->
% WARNING ADHOC FOR TESTING ONLY
Acc#{Cur => ?prvacc(rand:uniform(1000))} %?prvacc(hg_accounting:create_account(Code))}
end,
#{},
Currencies
Expand All @@ -218,17 +219,18 @@ construct_system_account_set(Ref) ->

construct_system_account_set(Ref, Name, ?cur(CurrencyCode)) ->
ok = hg_context:save(hg_context:create()),
SettlementAccountID = hg_accounting:create_account(CurrencyCode),
SubagentAccountID = hg_accounting:create_account(CurrencyCode),
% SettlementAccountID = hg_accounting:create_account(CurrencyCode),
% SubagentAccountID = hg_accounting:create_account(CurrencyCode),
hg_context:cleanup(),
{system_account_set, #domain_SystemAccountSetObject{
ref = Ref,
data = #domain_SystemAccountSet{
name = Name,
description = Name,
accounts = #{?cur(CurrencyCode) => #domain_SystemAccount{
settlement = SettlementAccountID,
subagent = SubagentAccountID
% WARNING ADHOC FOR TESTING ONLY
settlement = rand:uniform(1000),
subagent = rand:uniform(1000)
}}
}
}}.
Expand All @@ -242,19 +244,20 @@ construct_external_account_set(Ref) ->
-spec construct_external_account_set(external_account_set(), name(), currency()) ->
{system_account_set, dmsl_domain_thrift:'ExternalAccountSetObject'()}.

construct_external_account_set(Ref, Name, ?cur(CurrencyCode)) ->
construct_external_account_set(Ref, Name, ?cur(_CurrencyCode)) ->
ok = hg_context:save(hg_context:create()),
AccountID1 = hg_accounting:create_account(CurrencyCode),
AccountID2 = hg_accounting:create_account(CurrencyCode),
% AccountID1 = hg_accounting:create_account(CurrencyCode),
% AccountID2 = hg_accounting:create_account(CurrencyCode),
hg_context:cleanup(),
{external_account_set, #domain_ExternalAccountSetObject{
ref = Ref,
data = #domain_ExternalAccountSet{
name = Name,
description = Name,
accounts = #{?cur(<<"RUB">>) => #domain_ExternalAccount{
income = AccountID1,
outcome = AccountID2
% WARNING ADHOC FOR TESTING ONLY
income = rand:uniform(1000),
outcome = rand:uniform(1000)
}}
}
}}.
Expand Down
2 changes: 1 addition & 1 deletion apps/hg_proto/src/hg_proto.erl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ get_service(proxy_inspector) ->
get_service(proxy_host_provider) ->
{dmsl_proxy_provider_thrift, 'ProviderProxyHost'};
get_service(accounter) ->
{shumpune_shumpune_thrift, 'Accounter'};
{shumaich_shumaich_thrift, 'Accounter'};
get_service(automaton) ->
{mg_proto_state_processing_thrift, 'Automaton'};
get_service(processor) ->
Expand Down
2 changes: 1 addition & 1 deletion apps/party_management/src/party_management.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
stdlib,
genlib,
pm_proto,
shumpune_proto,
shumaich_proto,
cowboy,
how_are_you, % must be after ranch and before any woody usage
woody,
Expand Down
Loading