/// <summary> /// Creates GST File data. /// </summary> public GSTAuditFile Create(GAFPeriod gafPeriod, DateTime creationDate) { if (gafPeriod == null) { throw new ArgumentNullException(nameof(gafPeriod)); } var validationResult = _gafValidator.ValidateTaxYearStructure(_gafRepository.GetTaxPeriodByKey(gafPeriod.BranchID, gafPeriod.TaxAgencyID, gafPeriod.TaxPeriodID)); validationResult.RaiseIfHasError(); var documentIds = _gafRepository.GetReportedDocuments(gafPeriod.BranchID, gafPeriod.TaxAgencyID, gafPeriod.TaxPeriodID) .ToArray(); if (!documentIds.Any()) { return(null); } #region Initialization var glDocumentIds = documentIds.Where(documentId => _glTaxDocumentTypes.Contains(documentId.DocType)) .ToArray(); var documentGroups = SplitToDocumentGroups(documentIds.Except(glDocumentIds)); if (glDocumentIds.Any()) { var glDocumentGroup = BuildGLDocumentGroup(glDocumentIds); documentGroups.Add(glDocumentGroup); } var documentProcessingQueue = new GAFDocumentProcessingQueue(documentGroups); var gafDataStringBuilder = new StringBuilder(); _purchaseGAFDataStringBuilder = new StringBuilder(); _supplyGAFDataStringBuilder = new StringBuilder(); var footerRecord = new FooterRecord(); var gstAuditFile = new GSTAuditFile(); #endregion #region Processing SetGAFVersion(gstAuditFile, gafPeriod); var companyRecord = CreateCompanyRecord(gafPeriod, gstAuditFile, creationDate); _gafRecordWriter.WriteCompanyRecord(companyRecord, gafDataStringBuilder); #region Documents var documentGroup = documentProcessingQueue.GetNextDocumentGroup(); while (documentGroup != null) { CreateGAFDataByDocumentGroup(documentGroup, gafPeriod, footerRecord); _gafRepository.ClearCaches(); documentGroup = documentProcessingQueue.GetNextDocumentGroup(); } gafDataStringBuilder.Append(_purchaseGAFDataStringBuilder); gafDataStringBuilder.Append(_supplyGAFDataStringBuilder); #endregion #region Ledger var ledgerRecords = _glgafLedgerRecordsCreator.CreateLedgerRecords(gafPeriod); AddAmountsAndCountToFooterRecord(footerRecord, ledgerRecords); foreach (var ledgerRecord in ledgerRecords) { _gafRecordWriter.WriteLedgerRecord(ledgerRecord, gafDataStringBuilder); } #endregion _gafRecordWriter.WriteFooterRecord(footerRecord, gafDataStringBuilder); #endregion gstAuditFile.Data = gafDataStringBuilder.ToString(); var branch = _gafRepository.GetBranchByID(gafPeriod.BranchID); gstAuditFile.FileName = string.Concat(branch.BranchCD.Trim(), "_", string.Format(GafVersionTemplate, gstAuditFile.MajorVersion, GafVersionDelimiterForFileName, gstAuditFile.MinorVersion), ".txt"); return(gstAuditFile); }
/// <summary> /// Creates L record elements. Exports GL transactions and account balances. /// </summary> public IList <LedgerRecord> CreateLedgerRecords(GAFPeriod gafPeriod) { var taxPeriod = _gafRepository.GetTaxPeriodByKey(gafPeriod.BranchID, gafPeriod.TaxAgencyID, gafPeriod.TaxPeriodID); var finPeriods = _gafRepository.GetFinPeriodsInInterval(taxPeriod.StartDate, taxPeriod.EndDate) .ToList(); var finYears = finPeriods.Select(finPeriod => finPeriod.FinYear) .Distinct(); var lastYearPeriod = finYears.Select(finYear => _gafRepository.FindLastYearNotAdjustmentPeriod(finYear)) .SingleOrDefault(); if (lastYearPeriod != null && finPeriods.Select(finPeriod => finPeriod.FinPeriodID).Contains(lastYearPeriod.FinPeriodID)) { finPeriods.AddRange(_gafRepository.GetAdjustmentFinPeriods(lastYearPeriod.FinYear)); } var records = new List <LedgerRecord>(); var branch = _gafRepository.GetBranchByID(gafPeriod.BranchID); var minFinPeriod = finPeriods.First(); var accountsWithGLHistoryData = _gafRepository.GetAccountsWithDataToCalcBeginBalancesExcludingYTDNetIncAcc(branch.BranchID, branch.LedgerID, minFinPeriod.FinPeriodID); foreach (var accountWithGLHistoryData in accountsWithGLHistoryData) { var account = (Account)accountWithGLHistoryData; var glHistoryForLastActivityPeriod = (AH)accountWithGLHistoryData; var glHistoryForSelectedPeriod = (GLHistory)accountWithGLHistoryData; decimal?beginBalanceForPeriod = 0m; if (glHistoryForLastActivityPeriod.FinPeriodID != null) { var selectedPeriodPtdCreditTotal = glHistoryForSelectedPeriod.FinPtdCredit ?? 0m; var selectedPeriodPtdDebitTotal = glHistoryForSelectedPeriod.FinPtdDebit ?? 0m; var selectedPeriodPtdSaldo = AccountRules.CalcSaldo(account.Type, selectedPeriodPtdDebitTotal, selectedPeriodPtdCreditTotal); beginBalanceForPeriod = GetBalanceSign(account.Type) * (glHistoryForLastActivityPeriod.FinYtdBalance - selectedPeriodPtdSaldo); } var balanceRecord = BuildLedgerRecordForBalance(account, minFinPeriod.StartDate, beginBalanceForPeriod.Value); records.Add(balanceRecord); foreach (var finPeriod in finPeriods) { var glTrans = _gafRepository.GetPostedGLTrans(branch.BranchID, branch.LedgerID, account.AccountID, finPeriod.FinPeriodID); foreach (var glTran in glTrans) { var record = BuildLedgerRecordForTran(account, glTran, records.LastOrDefault()); records.Add(record); } _gafRepository.ClearCaches(); } } return(records); }