Some refactor of InitialBalance
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using Swarmops.Basic.Types.Financial;
|
||||
using Swarmops.Common;
|
||||
using Swarmops.Common.Enums;
|
||||
@@ -187,6 +188,75 @@ namespace Swarmops.Logic.Financial
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public Money InitialBalanceCents
|
||||
{
|
||||
set
|
||||
{
|
||||
try
|
||||
{
|
||||
const string initialBalanceTransactionTitle = "Initial Balances";
|
||||
|
||||
Int64 currentInitialBalanceCents = this.GetDeltaCents(new DateTime(1900, 1, 1),
|
||||
new DateTime(this.Organization.FirstFiscalYear, 1, 1));
|
||||
|
||||
// Find or create "Initial Balances" transaction
|
||||
|
||||
FinancialAccountRows testRows = FinancialAccountRows.ForOrganization(this.Organization,
|
||||
new DateTime(1900, 1, 1), new DateTime(this.Organization.FirstFiscalYear, 1, 1));
|
||||
|
||||
FinancialTransaction initialBalancesTransaction = null;
|
||||
|
||||
foreach (FinancialAccountRow row in testRows)
|
||||
{
|
||||
if (row.Transaction.Description == initialBalanceTransactionTitle)
|
||||
{
|
||||
initialBalancesTransaction = row.Transaction;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (initialBalancesTransaction == null)
|
||||
{
|
||||
// create transaction
|
||||
|
||||
initialBalancesTransaction = FinancialTransaction.Create(this.Organization,
|
||||
new DateTime(this.Organization.FirstFiscalYear - 1, 12, 31),
|
||||
initialBalanceTransactionTitle);
|
||||
}
|
||||
|
||||
// reconstructing the R-value now that we have the valuation date, which the setter didn't
|
||||
|
||||
Money setValue = new Money(value.Cents, value.Currency, initialBalancesTransaction.DateTime);
|
||||
Int64 deltaCents = setValue.ToCurrency(Organization.Currency).Cents - currentInitialBalanceCents;
|
||||
|
||||
Dictionary<int, Int64> recalcBase = initialBalancesTransaction.GetRecalculationBase();
|
||||
int equityAccountId = this.Organization.FinancialAccounts.DebtsEquity.Identity;
|
||||
|
||||
if (!recalcBase.ContainsKey(this.Identity))
|
||||
{
|
||||
recalcBase[this.Identity] = 0;
|
||||
}
|
||||
if (!recalcBase.ContainsKey(equityAccountId))
|
||||
{
|
||||
recalcBase[equityAccountId] = 0;
|
||||
}
|
||||
|
||||
recalcBase[this.Identity] += deltaCents;
|
||||
recalcBase[equityAccountId] -= deltaCents;
|
||||
initialBalancesTransaction.RecalculateTransaction(recalcBase, null);
|
||||
}
|
||||
catch (Exception weirdException)
|
||||
{
|
||||
SupportFunctions.LogException("FinancialAccount-SetInitialBalance", weirdException);
|
||||
|
||||
throw;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public ExternalBankDataProfile ExternalBankDataProfile
|
||||
{
|
||||
get
|
||||
|
||||
@@ -321,12 +321,12 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
|
||||
{
|
||||
try
|
||||
{
|
||||
const string initialBalanceTransactionTitle = "Initial Balances";
|
||||
|
||||
AuthenticationData authData = GetAuthenticationDataAndCulture();
|
||||
FinancialAccount account = FinancialAccount.FromIdentity (accountId);
|
||||
FinancialAccount account = FinancialAccount.FromIdentity(accountId);
|
||||
|
||||
if (!PrepareAccountChange (account, authData, false) || authData.CurrentOrganization.Parameters.FiscalBooksClosedUntilYear >= authData.CurrentOrganization.FirstFiscalYear)
|
||||
if (!PrepareAccountChange(account, authData, false) ||
|
||||
authData.CurrentOrganization.Parameters.FiscalBooksClosedUntilYear >=
|
||||
authData.CurrentOrganization.FirstFiscalYear)
|
||||
{
|
||||
return new ChangeAccountDataResult
|
||||
{
|
||||
@@ -336,61 +336,17 @@ namespace Swarmops.Frontend.Pages.v5.Ledgers
|
||||
|
||||
Int64 desiredInitialBalanceCents = Formatting.ParseDoubleStringAsCents(newInitialBalanceString);
|
||||
|
||||
Int64 currentInitialBalanceCents = account.GetDeltaCents (new DateTime (1900, 1, 1),
|
||||
new DateTime (authData.CurrentOrganization.FirstFiscalYear, 1, 1));
|
||||
account.InitialBalanceCents = new Money(desiredInitialBalanceCents, account.Organization.Currency); // only avaialable for presentation currencies
|
||||
|
||||
Int64 deltaCents = desiredInitialBalanceCents - currentInitialBalanceCents;
|
||||
|
||||
// Find or create "Initial Balances" transaction
|
||||
|
||||
FinancialAccountRows testRows = FinancialAccountRows.ForOrganization (authData.CurrentOrganization,
|
||||
new DateTime (1900, 1, 1), new DateTime (authData.CurrentOrganization.FirstFiscalYear, 1, 1));
|
||||
|
||||
FinancialTransaction initialBalancesTransaction = null;
|
||||
|
||||
foreach (FinancialAccountRow row in testRows)
|
||||
{
|
||||
if (row.Transaction.Description == initialBalanceTransactionTitle)
|
||||
{
|
||||
initialBalancesTransaction = row.Transaction;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (initialBalancesTransaction == null)
|
||||
{
|
||||
// create transaction
|
||||
|
||||
initialBalancesTransaction = FinancialTransaction.Create (authData.CurrentOrganization.Identity,
|
||||
new DateTime (authData.CurrentOrganization.FirstFiscalYear - 1, 12, 31), initialBalanceTransactionTitle);
|
||||
}
|
||||
|
||||
Dictionary<int, Int64> recalcBase = initialBalancesTransaction.GetRecalculationBase();
|
||||
int equityAccountId = authData.CurrentOrganization.FinancialAccounts.DebtsEquity.Identity;
|
||||
|
||||
if (!recalcBase.ContainsKey (accountId))
|
||||
{
|
||||
recalcBase[accountId] = 0;
|
||||
}
|
||||
if (!recalcBase.ContainsKey (equityAccountId))
|
||||
{
|
||||
recalcBase[equityAccountId] = 0;
|
||||
}
|
||||
|
||||
recalcBase[accountId] += deltaCents;
|
||||
recalcBase[equityAccountId] -= deltaCents;
|
||||
initialBalancesTransaction.RecalculateTransaction (recalcBase, authData.CurrentUser);
|
||||
return new ChangeAccountDataResult
|
||||
{
|
||||
Result = ChangeAccountDataOperationsResult.Changed,
|
||||
NewData = (desiredInitialBalanceCents / 100.0).ToString("N2", CultureInfo.CurrentCulture)
|
||||
};
|
||||
}
|
||||
catch (Exception weirdException)
|
||||
catch (Exception exception)
|
||||
{
|
||||
SupportFunctions.LogException ("AccountPlan-SetInitBalance", weirdException);
|
||||
|
||||
throw;
|
||||
throw new Exception("AccountPlan-SetInitialBalance", exception);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -296,6 +296,9 @@ namespace Swarmops.Site.Pages.Ledgers
|
||||
html = String.Format (Resources.Pages.Ledgers.UploadBankFiles_ResultsGood,
|
||||
resultDetail.TransactionsImported, resultDetail.DuplicateTransactions,
|
||||
resultDetail.EarliestTransaction, resultDetail.LatestTransaction);
|
||||
|
||||
// if (resultDetail.)
|
||||
|
||||
break;
|
||||
case ImportResultsCategory.Questionable:
|
||||
html = String.Format (Resources.Pages.Ledgers.UploadBankFiles_ResultsQuestionable,
|
||||
@@ -606,6 +609,7 @@ namespace Swarmops.Site.Pages.Ledgers
|
||||
FinancialAccount autoDepositAccount = args.Organization.FinancialAccounts.IncomeDonations;
|
||||
int autoDepositLimit = 0; // Disabled; TODO: this.CurrentOrganization.Parameters.AutoDonationLimit;
|
||||
|
||||
bool autosetInitialBalance = false;
|
||||
ImportResults result = new ImportResults();
|
||||
int count = 0;
|
||||
int progressUpdateInterval = import.Records.Length/40;
|
||||
@@ -623,6 +627,13 @@ namespace Swarmops.Site.Pages.Ledgers
|
||||
accountCurrency = organizationCurrency;
|
||||
}
|
||||
|
||||
FinancialAccountRows existingRows = assetAccount.GetRows(Constants.DateTimeLow, Constants.DateTimeHigh); // gets all
|
||||
if (existingRows.Count == 0)
|
||||
{
|
||||
autosetInitialBalance = true;
|
||||
}
|
||||
|
||||
|
||||
foreach (ExternalBankDataRecord row in import.Records)
|
||||
{
|
||||
// Update progress.
|
||||
@@ -739,7 +750,18 @@ namespace Swarmops.Site.Pages.Ledgers
|
||||
|
||||
// Import complete. Return true if the bookkeeping account matches the bank data.
|
||||
|
||||
Int64 databaseAccountBalanceCents = assetAccount.BalanceTotalCents;
|
||||
Int64 databaseAccountBalanceCents;
|
||||
|
||||
if (accountCurrency.Identity == organizationCurrency.Identity)
|
||||
{
|
||||
databaseAccountBalanceCents = assetAccount.BalanceTotalCents;
|
||||
}
|
||||
else
|
||||
{
|
||||
// foreign-currency account
|
||||
databaseAccountBalanceCents = assetAccount.ForeignBalanceTotalCents.Cents;
|
||||
}
|
||||
|
||||
|
||||
// Subtract any transactions made after the most recent imported transaction.
|
||||
// This is necessary in case of Paypal and others which continuously feed the
|
||||
@@ -762,6 +784,19 @@ namespace Swarmops.Site.Pages.Ledgers
|
||||
result.AccountBalanceMatchesBank = false;
|
||||
result.BalanceMismatchCents = (databaseAccountBalanceCents - beyondEofCents) -
|
||||
import.LatestAccountBalanceCents;
|
||||
|
||||
if (autosetInitialBalance)
|
||||
{
|
||||
if (accountCurrency.Identity == organizationCurrency.Identity) // ordinary presentation currency
|
||||
{
|
||||
// assetAccount.
|
||||
}
|
||||
else
|
||||
{
|
||||
// non-presentation currency
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.CurrencyCode = args.Organization.Currency.Code;
|
||||
|
||||
Reference in New Issue
Block a user