Skip to content

Commit

Permalink
Fix/addctx (#7)
Browse files Browse the repository at this point in the history
* Added context to IsCurrencyExist

* Make account number to be optional, if its empty, it will be assigned, if its not it will be used as it is.

* Many major changes
  • Loading branch information
newm4n authored Jul 18, 2021
1 parent 469f1e1 commit cd2003b
Show file tree
Hide file tree
Showing 7 changed files with 347 additions and 343 deletions.
22 changes: 13 additions & 9 deletions Accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ func (acc *Accounting) GetUniqueIDGenerator() UniqueIDGenerator {
return acc.uniqueIDGenerator
}

func (acc *Accounting) CreateNewAccount(context context.Context, name, description, coa string, currency string, alignment TransactionType, creator string) (Account, error) {
func (acc *Accounting) CreateNewAccount(context context.Context, accountNumber, name, description, coa string, currency string, alignment Alignment, creator string) (Account, error) {
account := acc.GetAccountManager().NewAccount(context).
SetName(name).SetDescription(description).SetCOA(coa).
SetCurrency(currency).SetBaseTransactionType(alignment).
SetAccountNumber(acc.GetUniqueIDGenerator().NewUniqueID()).
SetCurrency(currency).SetAlignment(alignment).
SetCreateBy(creator).SetCreateTime(time.Now())
if len(accountNumber) == 0 {
account.SetAccountNumber(acc.GetUniqueIDGenerator().NewUniqueID())
} else {
account.SetAccountNumber(accountNumber)
}
err := acc.GetAccountManager().PersistAccount(context, account)
if err != nil {
return nil, err
Expand All @@ -52,7 +56,7 @@ func (acc *Accounting) CreateNewAccount(context context.Context, name, descripti
type TransactionInfo struct {
AccountNumber string
Description string
TxType TransactionType
TxType Alignment
Amount int64
}

Expand All @@ -65,11 +69,11 @@ func (acc *Accounting) CreateNewJournal(context context.Context, description str

transacs := make([]Transaction, 0)

// make sure all transactions have accounts of the same Currency
// make sure all Transactions have accounts of the same Currency
for _, txinfo := range transactions {
newTransaction := acc.GetTransactionManager().NewTransaction(context).SetCreateBy(creator).SetCreateTime(time.Now()).
SetDescription(txinfo.Description).SetAccountNumber(txinfo.AccountNumber).SetAmount(txinfo.Amount).
SetTransactionTime(time.Now()).SetTransactionType(txinfo.TxType).SetTransactionID(acc.GetUniqueIDGenerator().NewUniqueID())
SetTransactionTime(time.Now()).SetAlignment(txinfo.TxType).SetTransactionID(acc.GetUniqueIDGenerator().NewUniqueID())

transacs = append(transacs, newTransaction)
}
Expand All @@ -95,16 +99,16 @@ func (acc *Accounting) CreateReversal(context context.Context, description strin

transacs := make([]Transaction, 0)

// make sure all transactions have accounts of the same Currency
// make sure all Transactions have accounts of the same Currency
for _, txinfo := range reversed.GetTransactions() {
tx := DEBIT
if txinfo.GetTransactionType() == DEBIT {
if txinfo.GetAlignment() == DEBIT {
tx = CREDIT
}

newTransaction := acc.GetTransactionManager().NewTransaction(context).SetCreateBy(creator).SetCreateTime(time.Now()).
SetDescription(fmt.Sprintf("%s - reversed", txinfo.GetDescription())).SetAccountNumber(txinfo.GetAccountNumber()).
SetTransactionTime(time.Now()).SetTransactionType(tx).SetTransactionID(acc.GetUniqueIDGenerator().NewUniqueID())
SetTransactionTime(time.Now()).SetAlignment(tx).SetTransactionID(acc.GetUniqueIDGenerator().NewUniqueID())

transacs = append(transacs, newTransaction)
}
Expand Down
8 changes: 4 additions & 4 deletions Accounting_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestAccounting_CreateNewAccount(t *testing.T) {
},
}

account, err := acc.CreateNewAccount(ctx, "Test Account", "Gold base test user account", "1.1", "GOLD", CREDIT, "aCreator")
account, err := acc.CreateNewAccount(ctx, "", "Test Account", "Gold base test user account", "1.1", "GOLD", CREDIT, "aCreator")
if err != nil {
t.Error(err.Error())
t.FailNow()
Expand Down Expand Up @@ -70,19 +70,19 @@ func TestAccounting_CreateNewJournal(t *testing.T) {
},
}

goldLoan, err := acc.CreateNewAccount(ctx, "Gold Loan", "Gold base loan reserve", "1.1", "GOLD", DEBIT, "aCreator")
goldLoan, err := acc.CreateNewAccount(ctx, "", "Gold Loan", "Gold base loan reserve", "1.1", "GOLD", DEBIT, "aCreator")
if err != nil {
t.Error(err.Error())
t.FailNow()
}

alphaCreditor, err := acc.CreateNewAccount(ctx, "Gold Creditor Alpha", "Gold base debitor alpha", "2.1", "GOLD", CREDIT, "aCreator")
alphaCreditor, err := acc.CreateNewAccount(ctx, "", "Gold Creditor Alpha", "Gold base debitor alpha", "2.1", "GOLD", CREDIT, "aCreator")
if err != nil {
t.Error(err.Error())
t.FailNow()
}

betaDebitor, err := acc.CreateNewAccount(ctx, "Gold Debitor Alpha", "Gold base creditor beta", "3.1", "GOLD", DEBIT, "aCreator")
betaDebitor, err := acc.CreateNewAccount(ctx, "", "Gold Debitor Alpha", "Gold base creditor beta", "3.1", "GOLD", DEBIT, "aCreator")
if err != nil {
t.Error(err.Error())
t.FailNow()
Expand Down
Loading

0 comments on commit cd2003b

Please sign in to comment.