Exemplo n.º 1
0
        private void CheckUnit(EnterpriseUnit unit, string propertyName, Dictionary <string, string[]> messages)
        {
            switch (propertyName)
            {
            case nameof(Orphan.CheckRelatedEnterpriseGroup):
                if (unit.EntGroupId == null)
                {
                    messages.Add(nameof(EnterpriseUnit.EntGroupId), new[] { nameof(Resource.AnalysisOrphanEnterprise) });
                }

                break;

            case nameof(Orphan.CheckEnterpriseRelatedLegalUnits):
                if (!unit.LegalUnits.Any())
                {
                    messages.Add(nameof(EnterpriseUnit.LegalUnits), new[] { nameof(Resource.AnalysisEnterpriseRelatedLegalUnits) });
                }
                break;

            case nameof(Orphan.CheckEnterpriseGroupRelatedEnterprises):
                if (!_context.EnterpriseUnits.Any(x => x.EntGroupId == unit.EntGroupId))
                {
                    messages.Add(nameof(EnterpriseUnit.LegalUnits), new[] { nameof(Resource.AnalysisEnterpriseGroupRelatedEnterprises) });
                }
                break;
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creating a legal unit with a local unit and an enterprise
        /// </summary>
        /// <param name="legalUnit"></param>
        /// <returns></returns>
        public async Task CreateLegalWithEnterpriseAndLocal(LegalUnit legalUnit)
        {
            LegalUnit      createdLegal;
            EnterpriseUnit createdEnterprise = null;
            LocalUnit      createdLocal      = null;

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    createdLegal = await CreateStatUnitAsync(legalUnit);

                    if (legalUnit.EnterpriseUnitRegId == null || legalUnit.EnterpriseUnitRegId == 0)
                    {
                        var sameStatIdEnterprise =
                            _dbContext.EnterpriseUnits.FirstOrDefault(eu => eu.StatId == legalUnit.StatId);

                        if (sameStatIdEnterprise != null)
                        {
                            await LinkEnterpriseToLegalAsync(sameStatIdEnterprise, createdLegal);
                        }
                        else
                        {
                            createdEnterprise = await CreateEnterpriseForLegalAsync(createdLegal);
                        }
                    }

                    var addressIds    = legalUnit.LocalUnits.Where(x => x.AddressId != null).Select(x => x.AddressId).ToList();
                    var addresses     = _dbContext.Address.Where(x => addressIds.Contains(x.Id)).ToList();
                    var sameAddresses = addresses.Where(x =>
                                                        x.RegionId == legalUnit.Address.RegionId &&
                                                        x.AddressPart1 == legalUnit.Address.AddressPart1 &&
                                                        x.AddressPart2 == legalUnit.Address.AddressPart2 &&
                                                        x.AddressPart3 == legalUnit.Address.AddressPart3 &&
                                                        x.Latitude == legalUnit.Address.Latitude &&
                                                        x.Longitude == legalUnit.Address.Longitude).ToList();
                    if (!sameAddresses.Any())
                    {
                        createdLocal = await CreateLocalForLegalAsync(createdLegal);
                    }

                    transaction.Commit();
                }
                catch (Exception e)
                {
                    throw new BadRequestException(nameof(Resource.SaveError), e);
                }
            }

            await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdLegal));

            if (createdLocal != null)
            {
                await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdLocal));
            }
            if (createdEnterprise != null)
            {
                await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdEnterprise));
            }
        }
Exemplo n.º 3
0
        private void ParseDecimalProp()
        {
            var unit = new EnterpriseUnit {Turnover = 0};
            const string sourceProp = "turnover";
            var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {nameof(unit.Turnover)}};
            const decimal expected = 17.17m;
            var raw = new Dictionary<string, object> {[sourceProp] = expected.ToString(CultureInfo.InvariantCulture)};

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.Equal(expected, unit.Turnover);
        }
Exemplo n.º 4
0
        private void ParseDateTimeProp()
        {
            var unit = new EnterpriseUnit {RegIdDate = DateTime.Now.AddDays(-5)};
            const string sourceProp = "created";
            var mapping = new Dictionary<string, string[]> {[sourceProp] = new[] {nameof(unit.RegIdDate)}};
            var expected = DateTime.Now.FlushSeconds();
            var raw = new Dictionary<string, object> {[sourceProp] = expected.ToString(CultureInfo.InvariantCulture)};

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.Equal(expected, unit.RegIdDate);
        }
Exemplo n.º 5
0
        private async Task LinkEnterpriseToLegalAsync(EnterpriseUnit sameStatIdEnterprise, LegalUnit legalUnit)
        {
            legalUnit.EnterpriseUnitRegId = sameStatIdEnterprise.RegId;
            _dbContext.LegalUnits.Update(legalUnit);
            await _dbContext.SaveChangesAsync();

            var legalsOfEnterprise = _dbContext.LegalUnits.Where(leu => leu.RegId == sameStatIdEnterprise.RegId)
                                     .Select(x => x.RegId).ToList();

            sameStatIdEnterprise.HistoryLegalUnitIds += string.Join(",", legalsOfEnterprise);
            _dbContext.EnterpriseUnits.Update(sameStatIdEnterprise);
            await _dbContext.SaveChangesAsync();
        }
Exemplo n.º 6
0
        private void ParseWithStringToNullableIntMapping()
        {
            var unit = new EnterpriseUnit();
            const string sourceProp = "sourceProp";
            var mapping = new Dictionary<string, string[]>
            {
                [sourceProp] = new[] {nameof(StatisticalUnit.InstSectorCodeId)}
            };

            const int expected = 42;
            var raw = new Dictionary<string, object> {[sourceProp] = "42"};

            StatUnitKeyValueParser.ParseAndMutateStatUnit(mapping, raw, unit);

            Assert.NotNull(unit.InstSectorCodeId);
            Assert.Equal(expected, unit.InstSectorCodeId.Value);
        }
Exemplo n.º 7
0
        private async Task <EnterpriseGroup> CreateGroupForEnterpriseAsync(EnterpriseUnit enterpriseUnit)
        {
            var enterpriseGroup = new EnterpriseGroup
            {
                AddressId                = enterpriseUnit.AddressId,
                ActualAddressId          = enterpriseUnit.ActualAddressId,
                HistoryEnterpriseUnitIds = enterpriseUnit.RegId.ToString()
            };

            Mapper.Map(enterpriseUnit, enterpriseGroup);
            _dbContext.EnterpriseGroups.Add(enterpriseGroup);
            await _dbContext.SaveChangesAsync();

            enterpriseUnit.EntGroupId = enterpriseGroup.RegId;
            _dbContext.EnterpriseUnits.Update(enterpriseUnit);
            await _dbContext.SaveChangesAsync();

            return(enterpriseGroup);
        }
Exemplo n.º 8
0
        private async Task <EnterpriseUnit> CreateEnterpriseForLegalAsync(LegalUnit legalUnit)
        {
            var enterpriseUnit = new EnterpriseUnit
            {
                AddressId           = legalUnit.AddressId,
                ActualAddressId     = legalUnit.ActualAddressId,
                HistoryLegalUnitIds = legalUnit.RegId.ToString()
            };

            Mapper.Map(legalUnit, enterpriseUnit);
            _dbContext.EnterpriseUnits.Add(enterpriseUnit);
            await _dbContext.SaveChangesAsync();

            legalUnit.EnterpriseUnitRegId = enterpriseUnit.RegId;
            _dbContext.LegalUnits.Update(legalUnit);
            await _dbContext.SaveChangesAsync();

            CreateActivitiesAndPersonsAndForeignParticipations(legalUnit.Activities, legalUnit.PersonsUnits, legalUnit.ForeignParticipationCountriesUnits, enterpriseUnit.RegId);
            await _dbContext.SaveChangesAsync();

            return(enterpriseUnit);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Creating an enterprise with a group of enterprises
        /// </summary>
        /// <param name="enterpriseUnit"></param>
        /// <returns></returns>
        public async Task CreateEnterpriseWithGroup(EnterpriseUnit enterpriseUnit)
        {
            EnterpriseUnit  createdUnit;
            EnterpriseGroup createdGroup = null;

            using (var transaction = _dbContext.Database.BeginTransaction())
            {
                try
                {
                    if (enterpriseUnit.EntGroupId == null || enterpriseUnit.EntGroupId == 0)
                    {
                        createdUnit = await CreateStatUnitAsync(enterpriseUnit);

                        createdGroup = await CreateGroupForEnterpriseAsync(createdUnit);

                        var sameStatIdLegalUnits = _dbContext.LegalUnits.Where(leu => leu.StatId == enterpriseUnit.StatId).ToList();
                        await LinkLegalsToEnterpriseAsync(sameStatIdLegalUnits, createdUnit);
                    }
                    else
                    {
                        createdUnit = _dbContext.EnterpriseUnits.Add(enterpriseUnit).Entity;
                        await _dbContext.SaveChangesAsync();
                    }
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    throw new BadRequestException(nameof(Resource.SaveError), e);
                }
            }

            await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdUnit));

            if (createdGroup != null)
            {
                await _elasticService.AddDocument(Mapper.Map <IStatisticalUnit, ElasticStatUnit>(createdGroup));
            }
        }
Exemplo n.º 10
0
        public static void AddStatUnits(NSCRegDbContext context)
        {
            var roleId       = context.Roles.FirstOrDefault(r => r.Name == DefaultRoleNames.Administrator)?.Id;
            var adminId      = context.UserRoles.FirstOrDefault(x => x.RoleId == roleId)?.UserId;
            var sysAdminUser = context.Users.FirstOrDefault(u => u.Id == adminId);

            context.StatisticalUnits.AddRange(new LocalUnit
            {
                Name        = "local unit 1",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address {
                    AddressPart1 = "local address 1", RegionId = 1
                },
            }, new LocalUnit
            {
                Name        = "local unit 2",
                StatId      = "OKPO2LU",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address {
                    AddressPart1 = "local address 2", RegionId = 1
                },
            });

            var le1 = new LegalUnit
            {
                Name        = "legal unit 1",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StatId      = "OKPO2LEGALU",
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address
                {
                    AddressPart1 = "legal address 1",
                    RegionId     = 1
                },
                ActivitiesUnits = new List <ActivityStatisticalUnit>
                {
                    new ActivityStatisticalUnit
                    {
                        Activity = new Activity
                        {
                            IdDate           = new DateTime(2017, 03, 17),
                            Turnover         = 2000,
                            ActivityType     = ActivityTypes.Primary,
                            UpdatedByUser    = sysAdminUser,
                            ActivityYear     = DateTime.Today.Year,
                            ActivityCategory = context.ActivityCategories.Single(v => v.Code == "11.07.9")
                        },
                    },
                    new ActivityStatisticalUnit
                    {
                        Activity =
                            new Activity
                        {
                            IdDate           = new DateTime(2017, 03, 28),
                            Turnover         = 4000,
                            ActivityType     = ActivityTypes.Secondary,
                            UpdatedByUser    = sysAdminUser,
                            ActivityYear     = 2006,
                            ActivityCategory = context.ActivityCategories.Single(v => v.Code == "91.01.9")
                        }
                    }
                },
            };

            context.StatisticalUnits.AddRange(le1, new LegalUnit
            {
                Name        = "legal unit 2",
                UserId      = sysAdminUser.Id,
                IsDeleted   = true,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address
                {
                    AddressPart1 = "legal address 2",
                    RegionId     = 1
                },
            });

            var eu1 = new EnterpriseUnit
            {
                Name        = "enterprise unit 1",
                StatId      = "OKPO1EU",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
            };

            var eu2 = new EnterpriseUnit
            {
                Name        = "enterprise unit 2",
                StatId      = "OKPO2EU",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address
                {
                    AddressPart1 = "enterprise address 2",
                    RegionId     = 1
                },
            };

            context.EnterpriseUnits.AddRange(eu1, eu2, new EnterpriseUnit
            {
                Name        = "enterprise unit 3",
                StatId      = "OKPO3EU",
                UserId      = sysAdminUser.Id,
                IsDeleted   = true,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address
                {
                    AddressPart1 = "enterprise address 2",
                    RegionId     = 1
                },
            }, new EnterpriseUnit
            {
                StatId      = "OKPO4EU",
                Name        = "enterprise unit 4",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address
                {
                    AddressPart1 = "enterprise address 2",
                    RegionId     = 1
                },
            }, new EnterpriseUnit
            {
                Name        = "enterprise unit 5",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address
                {
                    AddressPart1 = "enterprise address 2",
                    RegionId     = 1
                },
            }, new EnterpriseUnit
            {
                Name        = "enterprise unit 6",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     = new Address
                {
                    AddressPart1 = "enterprise address 2",
                    RegionId     = 1
                },
            });

            var eg1 = new EnterpriseGroup
            {
                Name        = "enterprise group 1",
                UserId      = sysAdminUser.Id,
                StatId      = "EG1",
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     =
                    new Address {
                    AddressPart1 = "ent. group address 1", RegionId = 1
                },
            };

            var eg2 = new EnterpriseGroup
            {
                Name        = "enterprise group 2",
                StatId      = "EG2",
                UserId      = sysAdminUser.Id,
                RegIdDate   = DateTime.Now,
                StartPeriod = DateTime.Now,
                EndPeriod   = DateTime.MaxValue,
                Address     =
                    new Address {
                    AddressPart1 = "ent. group address 2", RegionId = 1
                }
            };

            context.EnterpriseGroups.AddRange(eg1, eg2);

            //Links:
            eu1.EnterpriseGroup = eg1;
            le1.EnterpriseUnit  = eu1;

            context.SaveChanges();
        }
Exemplo n.º 11
0
        private async Task LinkLegalsToEnterpriseAsync(IEnumerable <LegalUnit> sameStatIdLegalUnits, EnterpriseUnit enterpriseUnit)
        {
            foreach (var legalUnit in sameStatIdLegalUnits)
            {
                legalUnit.EnterpriseUnitRegId = enterpriseUnit.RegId;
                _dbContext.LegalUnits.Update(legalUnit);
            }
            await _dbContext.SaveChangesAsync();

            var legalsOfEnterprise = _dbContext.LegalUnits.Where(leu => leu.RegId == enterpriseUnit.RegId)
                                     .Select(x => x.RegId).ToList();

            enterpriseUnit.HistoryLegalUnitIds = string.Join(",", legalsOfEnterprise);
            _dbContext.Update(enterpriseUnit);

            await _dbContext.SaveChangesAsync();
        }