Exemple #1
0
        /// <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);
        }