private static FinancialAccountRows RowsNotInVatReport(FinancialAccount account, DateTime endDateTime) { Organization organization = account.Organization; SwarmDb dbRead = SwarmDb.GetDatabaseForReading(); FinancialAccountRows rowsIntermediate = FinancialAccountRows.FromArray( SwarmDb.GetDatabaseForReading().GetAccountRowsNotInVatReport(account.Identity, endDateTime)); FinancialAccountRows rowsFinal = new FinancialAccountRows(); foreach (FinancialAccountRow row in rowsIntermediate) { // Check if this row _closes_ an _existing_ VAT report, in which case it should _not_ be included int vatReportOpenId = dbRead.GetVatReportIdFromCloseTransaction(row.FinancialTransactionId); int vatReportCloseId = dbRead.GetVatReportIdFromOpenTransaction(row.FinancialTransactionId); if (vatReportOpenId == 0 && vatReportCloseId == 0) { // This particular transaction doesn't close an existing VAT report rowsFinal.Add(row); } } return(rowsFinal); }
private static FinancialAccountRows RowsNotInVatReport(FinancialAccounts accounts, DateTime endTime) { FinancialAccountRows result = new FinancialAccountRows(); result.AddRange(accounts.SelectMany(account => RowsNotInVatReport(account, endTime))); return(result); }
public static FinancialAccountRows FromArray(BasicFinancialAccountRow[] array) { var result = new FinancialAccountRows { Capacity = (array.Length * 11 / 10) }; foreach (BasicFinancialAccountRow basic in array) { result.Add(FinancialAccountRow.FromBasic(basic)); } return(result); }
public FinancialAccountRows GetRowsFar(DateTime start, DateTime end) // selects lowerbound < x <= upperbound { BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading() .GetFinancialAccountRows(Identity, start, end, true); return(FinancialAccountRows.FromArray(basicRows)); }
public FinancialAccountRows GetRows(DateTime start, DateTime end) { BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading() .GetFinancialAccountRows(Identity, start, end, false); return(FinancialAccountRows.FromArray(basicRows)); }
public FinancialAccountRows GetLastRows(int rowCount) { BasicFinancialAccountRow[] basicRows = SwarmDb.GetDatabaseForReading() .GetLastFinancialAccountRows(Identity, rowCount); return(FinancialAccountRows.FromArray(basicRows)); }
public static VatReport Create(Organization organization, int year, int startMonth, int monthCount) { VatReport newReport = CreateDbRecord(organization, year, startMonth, monthCount); DateTime endDate = new DateTime(year, startMonth, 1).AddMonths(monthCount); FinancialAccount vatInbound = organization.FinancialAccounts.AssetsVatInboundUnreported; FinancialAccount vatOutbound = organization.FinancialAccounts.DebtsVatOutboundUnreported; FinancialAccount sales = organization.FinancialAccounts.IncomeSales; FinancialAccountRows inboundRows = RowsNotInVatReport(vatInbound, endDate); FinancialAccountRows outboundRows = RowsNotInVatReport(vatOutbound, endDate); FinancialAccountRows turnoverRows = RowsNotInVatReport(sales, endDate); Dictionary <int, bool> transactionsIncludedLookup = new Dictionary <int, bool>(); newReport.AddVatReportItemsFromAccountRows(inboundRows, transactionsIncludedLookup); newReport.AddVatReportItemsFromAccountRows(outboundRows, transactionsIncludedLookup); newReport.AddVatReportItemsFromAccountRows(turnoverRows, transactionsIncludedLookup); newReport.Release(); // Create financial TX that moves this VAT from unreported to reported Int64 differenceCents = newReport.VatInboundCents - newReport.VatOutboundCents; if (differenceCents != 0 && newReport.VatInboundCents > 0) { // if there's anything to report FinancialTransaction vatReportTransaction = FinancialTransaction.Create(organization, endDate.AddDays(4).AddHours(9), newReport.Description); if (newReport.VatInboundCents > 0) { vatReportTransaction.AddRow(organization.FinancialAccounts.AssetsVatInboundUnreported, -newReport.VatInboundCents, null); } if (newReport.VatOutboundCents > 0) { vatReportTransaction.AddRow(organization.FinancialAccounts.DebtsVatOutboundUnreported, newReport.VatOutboundCents, null); // not negative, because our number is sign-different from the bookkeeping's } if (differenceCents < 0) // outbound > inbound { vatReportTransaction.AddRow(organization.FinancialAccounts.DebtsVatOutboundReported, differenceCents, null); // debt, so negative as in our variable } else // inbound > outbound { vatReportTransaction.AddRow(organization.FinancialAccounts.AssetsVatInboundReported, differenceCents, null); // asset, so positive as in our variable } vatReportTransaction.Dependency = newReport; newReport.OpenTransaction = vatReportTransaction; } else { newReport.Open = false; // nothing to close, no tx created } return(newReport); }
private void AddVatReportItemsFromAccountRows(FinancialAccountRows rows, Dictionary <int, bool> transactionsIncludedLookup) { if (rows.Count == 0) { return; } Organization organization = rows[0].Transaction.Organization; // there is always a rows[0] because check above int vatInboundAccountId = organization.FinancialAccounts.AssetsVatInboundUnreported.Identity; int vatOutboundAccountId = organization.FinancialAccounts.DebtsVatOutboundUnreported.Identity; Dictionary <int, bool> turnoverAccountLookup = new Dictionary <int, bool>(); FinancialAccounts turnoverAccounts = FinancialAccounts.ForOrganization(organization, FinancialAccountType.Income); foreach (FinancialAccount turnoverAccount in turnoverAccounts) { turnoverAccountLookup[turnoverAccount.Identity] = true; } foreach (FinancialAccountRow accountRow in rows) { FinancialTransaction tx = accountRow.Transaction; if (tx.Dependency is VatReport) { continue; // Never include previous VAT reports in new VAT reports } if (!transactionsIncludedLookup.ContainsKey(tx.Identity)) { Int64 vatInbound = 0; Int64 vatOutbound = 0; Int64 turnOver = 0; transactionsIncludedLookup[accountRow.FinancialTransactionId] = true; FinancialTransactionRows txRows = accountRow.Transaction.Rows; foreach (FinancialTransactionRow txRow in txRows) { if (txRow.FinancialAccountId == vatInboundAccountId) { vatInbound += txRow.AmountCents; } else if (txRow.FinancialAccountId == vatOutboundAccountId) { vatOutbound += -txRow.AmountCents; // this is a negative, so converting to positive } else if (turnoverAccountLookup.ContainsKey(txRow.FinancialAccountId)) { turnOver -= txRow.AmountCents; // turnover accounts are sign reversed, so convert to positive } } // Add new row to the VAT report AddItem(tx, turnOver, vatInbound, vatOutbound); } } }