Skip to content

Commit

Permalink
Fixing by adding getter to accounting (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
newm4n authored Jul 7, 2021
1 parent 2bc6948 commit 8c565a2
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions Accounting.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,26 @@ type Accounting struct {
uniqueIDGenerator UniqueIDGenerator
}

func (acc *Accounting) GetAccountManager() AccountManager {
return acc.accountManager
}
func (acc *Accounting) GetTransactionManager() TransactionManager {
return acc.transactionManager
}
func (acc *Accounting) GetJournalManager() JournalManager {
return acc.journalManager
}
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) {
account := acc.accountManager.NewAccount(context).
account := acc.GetAccountManager().NewAccount(context).
SetName(name).SetDescription(description).SetCOA(coa).
SetCurrency(currency).SetBaseTransactionType(alignment).
SetAccountNumber(acc.uniqueIDGenerator.NewUniqueID()).
SetAccountNumber(acc.GetUniqueIDGenerator().NewUniqueID()).
SetCreateBy(creator).SetCreateTime(time.Now())
err := acc.accountManager.PersistAccount(context, account)
err := acc.GetAccountManager().PersistAccount(context, account)
if err != nil {
return nil, err
}
Expand All @@ -44,30 +57,30 @@ type TransactionInfo struct {
}

func (acc *Accounting) CreateNewJournal(context context.Context, description string, transactions []TransactionInfo, creator string) (Journal, error) {
journal := acc.journalManager.NewJournal(context).SetDescription(description)
journal := acc.GetJournalManager().NewJournal(context).SetDescription(description)

journal.SetJournalID(acc.uniqueIDGenerator.NewUniqueID()).SetCreateBy(creator).
journal.SetJournalID(acc.GetUniqueIDGenerator().NewUniqueID()).SetCreateBy(creator).
SetCreateTime(time.Now()).SetJournalingTime(time.Now()).
SetReversal(false).SetReversedJournal(nil)

transacs := make([]Transaction, 0)

// make sure all transactions have accounts of the same Currency
for _, txinfo := range transactions {
newTransaction := acc.transactionManager.NewTransaction(context).SetCreateBy(creator).SetCreateTime(time.Now()).
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.uniqueIDGenerator.NewUniqueID())
SetTransactionTime(time.Now()).SetTransactionType(txinfo.TxType).SetTransactionID(acc.GetUniqueIDGenerator().NewUniqueID())

transacs = append(transacs, newTransaction)
}

journal.SetTransactions(transacs)

err := acc.journalManager.PersistJournal(context, journal)
err := acc.GetJournalManager().PersistJournal(context, journal)
if err != nil {
err = acc.journalManager.CommitJournal(context, journal)
err = acc.GetJournalManager().CommitJournal(context, journal)
if err != nil {
err = acc.journalManager.CancelJournal(context, journal)
err = acc.GetJournalManager().CancelJournal(context, journal)
return nil, err
}
return nil, err
Expand All @@ -76,8 +89,8 @@ func (acc *Accounting) CreateNewJournal(context context.Context, description str
}

func (acc *Accounting) CreateReversal(context context.Context, description string, reversed Journal, creator string) (Journal, error) {
journal := acc.journalManager.NewJournal(context).SetDescription(description)
journal.SetJournalID(acc.uniqueIDGenerator.NewUniqueID()).SetCreateBy(creator).SetCreateTime(time.Now()).SetJournalingTime(time.Now()).
journal := acc.GetJournalManager().NewJournal(context).SetDescription(description)
journal.SetJournalID(acc.GetUniqueIDGenerator().NewUniqueID()).SetCreateBy(creator).SetCreateTime(time.Now()).SetJournalingTime(time.Now()).
SetReversal(true).SetReversedJournal(reversed)

transacs := make([]Transaction, 0)
Expand All @@ -89,20 +102,20 @@ func (acc *Accounting) CreateReversal(context context.Context, description strin
tx = CREDIT
}

newTransaction := acc.transactionManager.NewTransaction(context).SetCreateBy(creator).SetCreateTime(time.Now()).
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.uniqueIDGenerator.NewUniqueID())
SetTransactionTime(time.Now()).SetTransactionType(tx).SetTransactionID(acc.GetUniqueIDGenerator().NewUniqueID())

transacs = append(transacs, newTransaction)
}

journal.SetTransactions(transacs)

err := acc.journalManager.PersistJournal(context, journal)
err := acc.GetJournalManager().PersistJournal(context, journal)
if err != nil {
err = acc.journalManager.CommitJournal(context, journal)
err = acc.GetJournalManager().CommitJournal(context, journal)
if err != nil {
err = acc.journalManager.CancelJournal(context, journal)
err = acc.GetJournalManager().CancelJournal(context, journal)
return nil, err
}
return nil, err
Expand Down

0 comments on commit 8c565a2

Please sign in to comment.