예제 #1
0
        public ImportReportModel LoadData(string file)
        {
            var result = _importService.ImportDataRow(file);

            if (result == null)
            {
                throw new NullReferenceException(nameof(result));
            }

            var importReport = new ImportReportModel();

            foreach (var item in result)
            {
                var configuration = _configuration.Load();
                if (configuration == null)
                {
                    throw new NullReferenceException(nameof(configuration));
                }

                var documentsToAdd    = new List <PaymentDocumentEntity>();
                var documentsToUpdate = new List <PaymentDocumentEntity>();

                foreach (var detailsImport in item.PaymentDetailsImports)
                {
                    try
                    {
                        var personalAccount = ValidPersonalAccount(detailsImport.PersonalAccount, configuration.AccountLength);
                        var documentDate    = ValidDateTime(item.DocumentData);

                        var account = _accountService.GetItem(x => x.Account == personalAccount);
                        if (account == null)
                        {
                            _report.Write(personalAccount.ToString(), FileType.Html);
                            importReport.CorruptedRecords++;
                            continue;
                        }

                        var paymentDocuments = account.PaymentDocuments.Where(x => x.PaymentDate == documentDate).ToList();
                        if (paymentDocuments.Count() != 0)
                        {
                            documentsToUpdate
                            .AddRange(paymentDocuments
                                      .Where(entity => entity.Accrued != detailsImport.Accrued ||
                                             entity.Received != detailsImport.Received &&
                                             entity.IsDeleted == false));
                            continue;
                        }

                        if (account.PaymentDocuments == null)
                        {
                            account.PaymentDocuments = new List <PaymentDocumentEntity>();
                        }

                        var paymentDocument = new PaymentDocumentEntity
                        {
                            AccountId   = account.Id,
                            Accrued     = detailsImport.Accrued,
                            Received    = detailsImport.Received,
                            PaymentDate = documentDate,
                        };

                        documentsToAdd.Add(paymentDocument);
                    }
                    catch (Exception e)
                    {
                        _report.Write(detailsImport.PersonalAccount.ToString(), FileType.Html);

                        _report.WriteException(e.Message, FileType.Html);

                        continue;
                    }
                }

                if (documentsToAdd.Count != 0)
                {
                    _documentService.Add(documentsToAdd);
                    importReport.Add += documentsToAdd.Count;
                }

                if (documentsToUpdate.Count != 0)
                {
                    _documentService.Update(documentsToUpdate);
                    importReport.Updates += documentsToUpdate.Count;
                }
            }
            return(importReport);
        }
예제 #2
0
        public ImportReportModel LoadData(string file)
        {
            var configuration = _configuration.Load();

            var import = _import.ImportDataRow(file);

            var importReport = new ImportReportModel();

            foreach (var districtsGroup in import.GroupBy(x => x.District.Name))
            {
                var firstDistrict = districtsGroup.FirstOrDefault();

                if (firstDistrict == null)
                {
                    continue;
                }

                var district = AddOrCreate(firstDistrict.District);

                foreach (var addressGroup in districtsGroup.GroupBy(x => x.Address.Name))
                {
                    var firstAddress = addressGroup.FirstOrDefault();
                    if (firstAddress == null)
                    {
                        continue;
                    }

                    var street = AddOrCreate(firstAddress.Address, district.Id);

                    var accountsToUpdate = new List <AccountEntity>();
                    var accountsToAdd    = new List <AccountEntity>();

                    foreach (var dataRow in addressGroup)
                    {
                        try
                        {
                            var account = _accountService.GetItem(x => x.Account == dataRow.Account.PersonalAccount && !x.IsDeleted);

                            if (account != null)
                            {
                                account.IsArchive    = account.IsEmpty && account.IsEmptyAgain && string.IsNullOrWhiteSpace(dataRow.Account.ServiceProviderCode);
                                account.IsEmptyAgain = string.IsNullOrWhiteSpace(dataRow.Account.ServiceProviderCode) &&
                                                       account.IsEmpty;

                                account.IsEmpty = string.IsNullOrWhiteSpace(dataRow.Account.ServiceProviderCode);
                                account.IsNew   = false;

                                accountsToUpdate.Add(account);
                                continue;
                            }

                            account = new AccountEntity
                            {
                                AccountCreationDate = ConvertAccrualMonth(dataRow.Account.AccrualMonth),
                                Account             = dataRow.Account.PersonalAccount,
                                AccountType         = ConvertAccountType(dataRow.Account.AccountType, configuration.MunicipalMark),
                                IsEmpty             = string.IsNullOrWhiteSpace(dataRow.Account.ServiceProviderCode),
                                IsNew = true
                            };

                            if (street.Locations == null)
                            {
                                street.Locations = new List <LocationEntity>();
                            }

                            var location = new LocationEntity
                            {
                                HouseNumber     = dataRow.LocationImport.HouseNumber,
                                BuildingCorpus  = dataRow.LocationImport.BuildingNumber,
                                ApartmentNumber = dataRow.LocationImport.ApartmentNumber,
                                StreetId        = street.Id,
                            };
                            street.Locations.Add(location);

                            account.StreetId = street.Id;
                            account.Location = location;

                            accountsToAdd.Add(account);
                        }
                        catch (Exception e)
                        {
                            _brokenRecordsReport.WriteException(e.Message, FileType.Excel);
                        }
                    }
                    if (accountsToAdd.Count != 0)
                    {
                        _accountService.Add(accountsToAdd);
                        importReport.Add += accountsToAdd.Count;
                    }
                    else
                    {
                        _accountService.Update(accountsToUpdate);
                        importReport.Updates += accountsToUpdate.Count;
                    }
                }
            }
            return(importReport);
        }