/// <summary> /// Creates a Single Mutation /// </summary> /// <param name="account">Account affected by this mutation</param> /// <param name="previousEventNr">The Event Number of the previous Event.</param> /// <param name="counterAccount">The Counter Party Account for this mutation</param> /// <param name="mutationType">Type of mutation example Deposit of Withdrawal</param> /// <param name="entryType">Credit of Debit</param> /// <param name="prevBalance">Previous balance of the Account</param> /// <param name="amount">The Amount mutated</param> /// <param name="currency">Asset type ex USD</param> /// <param name="descr2"></param> /// <returns>Mutation object</returns> private static Mutation CreateMutation(Mutation lastAccountEvent, Mutation LastCounterAccountEvent, MutationTypes mutationType, MutationEntryTypes entryType, decimal amount, string currency, string descr2 = "", string descr3 = "") { // the amount credited will be calculated by the MutationEntryType var CreditAmount = 0m; // the amount credited will be calculated by the MutationEntryType var DebitAmount = 0m; // New balance will be calculated by the MutationEntryType Dr + and Cr - var newBalance = 0m; //this old balance is the new Balance of the previous mutation var oldBalance = 0m; // Find the Old Currency balance. If cussency is not there the balance is 0.00 foreach (Balance b in lastAccountEvent.AccountBalances) { if (b.Currency == currency) { oldBalance = b.NewBalance; } } // generate the default description (goes to descr1) var descr1 = ($"{mutationType.ToString().Replace('_', ' ')} {entryType.ToString()} {amount} {currency}."); //Calucate the balance and Debit and Credit amount switch (entryType) { case MutationEntryTypes.Cr: CreditAmount = amount; newBalance = oldBalance - amount; break; case MutationEntryTypes.Dr: DebitAmount = amount; newBalance = oldBalance + amount; break; } // Create counterAccount Information CounterAccount counterAccount = new CounterAccount { AccountHolder = LastCounterAccountEvent.Account.AccountHolder, AccountNumber = LastCounterAccountEvent.Account.AccountNumber, AccountType = LastCounterAccountEvent.Account.AccountType, PreviousEventNumber = LastCounterAccountEvent.EventNumber, EventNumber = LastCounterAccountEvent.EventNumber + 1, }; var mutation = new Mutation { MutationId = Guid.NewGuid().ToString(), Account = lastAccountEvent.Account, CounterAccount = counterAccount, PreviousEventNumber = lastAccountEvent.EventNumber, EventNumber = lastAccountEvent.EventNumber + 1, MutationType = mutationType.ToString(), Currency = currency.ToUpper(), AccountBalances = UpdateBalance(lastAccountEvent.AccountBalances, oldBalance, newBalance, currency.ToUpper()), Cr = CreditAmount, Dr = DebitAmount, Description1 = descr1, Description2 = descr2, Description3 = descr3 }; return(mutation); }
/// <summary> /// Create a new posting /// </summary> /// <param name="lastAccountEvent"></param> /// <param name="LastCounterAccountEvent"></param> /// <param name="mutationType"></param> /// <param name="entryType"></param> /// <param name="amount"></param> /// <param name="currency"></param> /// <param name="descr2"></param> /// <param name="descr3"></param> /// <returns>Posting object</returns> public static Posting CreatePosting(Mutation lastAccountEvent, Mutation LastCounterAccountEvent, MutationTypes mutationType, MutationEntryTypes entryType, decimal amount, string currency, string descr2 = "", string descr3 = "") { Posting result = new Posting(); Mutation accountEvent = CreateMutation(lastAccountEvent, LastCounterAccountEvent, mutationType, entryType, amount, currency, descr2, descr3); // Swap CR to DR and Vsa versa before creating a CounteraccountEvent. MutationEntryTypes CounterEntryType; if (entryType == MutationEntryTypes.Cr) { CounterEntryType = MutationEntryTypes.Dr; } else { CounterEntryType = MutationEntryTypes.Cr; } Mutation CounteraccountEvent = CreateMutation(LastCounterAccountEvent, lastAccountEvent, mutationType, CounterEntryType, amount, currency, descr2, descr3); // Set the mutation id's in the counterAccounts. CounteraccountEvent.CounterAccount.MutationId = accountEvent.MutationId; accountEvent.CounterAccount.MutationId = CounteraccountEvent.MutationId; result.Mutations[0] = accountEvent; result.Mutations[1] = CounteraccountEvent; return(result); }