Skip to content

Commit

Permalink
Fix balances shown with zero in AnaliticoDeCuentas
Browse files Browse the repository at this point in the history
  • Loading branch information
efrain-jerv committed Aug 12, 2021
1 parent f009e54 commit e0215d3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 18 deletions.
3 changes: 1 addition & 2 deletions BalanceEngine.Tests/Balances/TrialBalanceUseCasesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void Should_Build_A_Traditional_Consolidated_Trial_Balance() {
TrialBalanceCommand command = new TrialBalanceCommand();

command.AccountsChartUID = "b2328e67-3f2e-45b9-b1f6-93ef6292204e";
command.BalancesType = BalanceEngine.BalancesType.AllAccounts;
command.BalancesType = BalanceEngine.BalancesType.WithMovements;
command.ConsolidateBalancesToTargetCurrency = false;
command.TrialBalanceType = BalanceEngine.TrialBalanceType.AnaliticoDeCuentas;
command.FromAccount = "1503";
Expand All @@ -57,7 +57,6 @@ public void Should_Build_A_Traditional_Consolidated_Trial_Balance() {
command.InitialPeriod.ValuateToCurrrencyUID = "01";
command.InitialPeriod.ExchangeRateDate = new DateTime(2021, 04, 30);
command.InitialPeriod.ExchangeRateTypeUID = "5923136d-8533-4975-81b9-c8ec3bf18dea";

TrialBalanceDto trialBalance = _usecases.BuildTrialBalance(command);

Assert.NotNull(trialBalance);
Expand Down
1 change: 1 addition & 0 deletions BalanceEngine/Domain/SpecialCases/AnaliticoDeCuentas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ internal TrialBalance Build() {
var twoColumnsHelper = new TwoCurrenciesBalanceHelper(_command);

FixedList<TrialBalanceEntry> postingEntries = helper.GetTrialBalanceEntries(_command.InitialPeriod);

postingEntries = helper.ValuateToExchangeRate(postingEntries, _command.InitialPeriod);

postingEntries = helper.RoundTrialBalanceEntries(postingEntries);
Expand Down
1 change: 1 addition & 0 deletions BalanceEngine/Domain/TrialBalanceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ internal FixedList<TrialBalanceEntry> ValuateToExchangeRate(FixedList<TrialBalan
FixedList<ExchangeRate> exchageRates = ExchangeRate.GetList(exchangeRateType, commandPeriod.ExchangeRateDate);

foreach (var entry in entries.Where(a => a.Currency.Code != "01")) {

var exchangeRate = exchageRates.FirstOrDefault(a => a.FromCurrency.Code == commandPeriod.ValuateToCurrrencyUID &&
a.ToCurrency.Code == entry.Currency.Code);

Expand Down
38 changes: 22 additions & 16 deletions BalanceEngine/Domain/TwoCurrenciesBalanceHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,18 +200,21 @@ internal FixedList<TwoCurrenciesBalanceEntry> MergeAccountsIntoTwoColumns(List<T
var summaryEntries = new EmpiriaHashTable<TwoCurrenciesBalanceEntry>();

foreach (var entry in trialBalance) {
string hash = $"{entry.Account.Number}||{entry.Sector.Code}||{targetCurrency.Id}||{entry.Ledger.Id}||{entry.Account.DebtorCreditor}";
Currency currentCurrency = entry.Currency;

if (entry.Currency.Equals(targetCurrency)) {
GenerateOrIncreaseTwoCurrenciesBalanceEntry(summaryEntries, entry, hash,
targetCurrency, currentCurrency);
} else if (summaryEntries.ContainsKey(hash)) {
SumTwoCurrenciesBalanceEntry(summaryEntries[hash], entry, targetCurrency, currentCurrency);
} else {
entry.Currency = targetCurrency;
GenerateOrIncreaseTwoCurrenciesBalanceEntry(summaryEntries, entry, hash,
targetCurrency, currentCurrency);
if (entry.CurrentBalance != 0) {
string hash = $"{entry.Account.Number}||{entry.Sector.Code}||{targetCurrency.Id}||" +
$"{entry.Ledger.Id}||{entry.Account.DebtorCreditor}";
Currency currentCurrency = entry.Currency;

if (entry.Currency.Equals(targetCurrency)) {
GenerateOrIncreaseTwoCurrenciesBalanceEntry(summaryEntries, entry, hash,
currentCurrency);
} else if (summaryEntries.ContainsKey(hash)) {
SumTwoCurrenciesBalanceEntry(summaryEntries[hash], entry, currentCurrency);
} else {
entry.Currency = targetCurrency;
GenerateOrIncreaseTwoCurrenciesBalanceEntry(summaryEntries, entry, hash,
currentCurrency);
}
}
}

Expand Down Expand Up @@ -254,23 +257,26 @@ private void GenerateListSummaryGroupEntries(FixedList<TwoCurrenciesBalanceEntry

private void GenerateOrIncreaseTwoCurrenciesBalanceEntry(
EmpiriaHashTable<TwoCurrenciesBalanceEntry> summaryEntries,
TrialBalanceEntry entry, string hash, Currency targetCurrency,
TrialBalanceEntry entry, string hash,
Currency currentCurrency) {
var targetCurrency = Currency.Parse(_command.InitialPeriod.ValuateToCurrrencyUID);

TwoCurrenciesBalanceEntry summaryEntry;
summaryEntries.TryGetValue(hash, out summaryEntry);

if (summaryEntry == null) {
summaryEntries.Insert(hash, entry.MapToTwoColumnsBalanceEntry());
SumTwoCurrenciesBalanceEntry(summaryEntries[hash], entry, targetCurrency, currentCurrency);
SumTwoCurrenciesBalanceEntry(summaryEntries[hash], entry, currentCurrency);
} else {
SumTwoCurrenciesBalanceEntry(summaryEntry, entry, targetCurrency, currentCurrency);
SumTwoCurrenciesBalanceEntry(summaryEntry, entry, currentCurrency);
}
}


private void SumTwoCurrenciesBalanceEntry(TwoCurrenciesBalanceEntry twoCurrenciesEntry,
TrialBalanceEntry entry,
Currency targetCurrency, Currency currentCurrency) {
Currency currentCurrency) {
var targetCurrency = Currency.Parse(_command.InitialPeriod.ValuateToCurrrencyUID);

if (currentCurrency != targetCurrency && entry.Currency.Code != "44" && currentCurrency.Code != "44") {
twoCurrenciesEntry.ForeignBalance += entry.CurrentBalance;
Expand Down

0 comments on commit e0215d3

Please sign in to comment.