Esempio n. 1
0
        protected virtual async Task <IEnumerable <T> > SearchAsync <T>(string company, EntitySearchRequest searchRequest, string gridCode, string viewName, bool skipPaginationLimit = false)
        {
            var grid = await _gridQueries.GetGrid(gridCode, company);

            if (grid == null)
            {
                throw new AtlasTechnicalException($"No grid configuration found for {gridCode}.");
            }

            if (!skipPaginationLimit && grid.MaxNumberOfRecords.HasValue && grid.MaxNumberOfRecords < searchRequest.Limit)
            {
                searchRequest.Limit = grid.MaxNumberOfRecords;
            }

            var dynamicQueryDefinition = _mapper.Map <DynamicQueryDefinition>(searchRequest);

            var columnConfiguration = _mapper.Map <List <ColumnConfiguration> >(grid.Columns);

            var companyDate = await _systemDateTimeService.GetCompanyDate(company);

            var userDepartments = (await _userService.GetUserByIdAsync(_identityService.GetUserAtlasId(), false)).Permissions
                                  .FirstOrDefault(permission => permission.CompanyId == company)
                                  ?.Departments.Select(department => department.DepartmentId).ToList();

            var dataVersionId = searchRequest.DataVersionId ?? await _systemDateTimeService.GetCompanyCurrentDataVersionId(company);

            var buildQueryResult = DynamicQueryBuilder.BuildQuery(company, _identityService.GetUserName(), dynamicQueryDefinition, viewName, columnConfiguration, companyDate, dataVersionId, userDepartments);

            var results = await ExecuteDynamicQueryAsync <T>(buildQueryResult.Sql, buildQueryResult.Parameters);

            return(results.ToList());
        }
Esempio n. 2
0
        private async Task <int?> GetCompanyCurrentDataVersionId(string companyId)
        {
            // Retrieve data from cache
            string cacheKey    = $"Company_{companyId}_CurrentDataVersionId";
            var    cachedValue = await _distributedCache.GetStringAsync(cacheKey);

            if (cachedValue != null)
            {
                var cachedDataVersionId = JsonConvert.DeserializeObject <int>(cachedValue);

                return(cachedDataVersionId);
            }

            var dataVersionId = await _systemDateTimeService.GetCompanyCurrentDataVersionId(companyId);

            if (dataVersionId != null)
            {
                // Cache the current DataVersionId of the company as it will never change.
                await _distributedCache.SetStringAsync(cacheKey, JsonConvert.SerializeObject(dataVersionId));
            }

            return(dataVersionId);
        }
        private async Task <List <SectionReference> > CreateIntercoContractOnCreateContract(CreatePhysicalFixedPricedContractCommand request,
                                                                                            Section physicalFixedPricedContract, List <SectionReference> references, List <SectionReference> sectionReferences)
        {
            // Create Interco Trade if available
            if (request.IsInterco)
            {
                string counterpartyCode = (physicalFixedPricedContract.Type == ContractType.Purchase) ?
                                          physicalFixedPricedContract.SellerCode : physicalFixedPricedContract.BuyerCode;
                var counterparty = await _masterDataService.GetAllByCounterpartyIdAsync(request.CompanyId, counterpartyCode);

                if (counterparty != null && counterparty.FirstOrDefault() != null &&
                    counterparty.FirstOrDefault().CompanyId != request.CompanyId &&
                    counterparty.FirstOrDefault().IsCounterpartyGroupAccount)
                {
                    var dataVersionId = await _systemDateTimeService.GetCompanyCurrentDataVersionId(request.IntercoCompanyId);

                    var intercoPhysicalFixedPricedContract = _mapper.Map <Section>(request);
                    intercoPhysicalFixedPricedContract.CompanyId     = physicalFixedPricedContract.IntercoCompanyId;
                    intercoPhysicalFixedPricedContract.TraderId      = physicalFixedPricedContract.IntercoTraderId;
                    intercoPhysicalFixedPricedContract.DepartmentId  = physicalFixedPricedContract.IntercoDepartmentId;
                    intercoPhysicalFixedPricedContract.Type          = (physicalFixedPricedContract.Type == ContractType.Purchase) ? ContractType.Sale : ContractType.Purchase;
                    intercoPhysicalFixedPricedContract.DataVersionId = dataVersionId;
                    intercoPhysicalFixedPricedContract.ContractDate  = DateTime.UtcNow;

                    intercoPhysicalFixedPricedContract.Costs = physicalFixedPricedContract.Costs.Where(r => r.SupplierCode == counterpartyCode);

                    // At least one contract has to be always created
                    intercoPhysicalFixedPricedContract.NumberOfContract = 1;

                    var intercoReferences = (await _tradeRepository.CreatePhysicalContractAsImageAsync(intercoPhysicalFixedPricedContract)).ToList();

                    foreach (var intercoItem in intercoReferences)
                    {
                        if (intercoItem.SectionId > 0 && intercoPhysicalFixedPricedContract.Costs.Any())
                        {
                            foreach (var intercoCostItem in intercoPhysicalFixedPricedContract.Costs)
                            {
                                intercoCostItem.CompanyId       = intercoPhysicalFixedPricedContract.CompanyId;
                                intercoCostItem.SectionId       = intercoItem.SectionId;
                                intercoCostItem.DataVersionId   = dataVersionId;
                                intercoCostItem.CostDirectionId = (intercoCostItem.CostDirectionId == 1) ? 2 : 1;
                            }

                            // check the business rule for PriceUnitId.
                            await _costRepository.AddCostsAsync(intercoPhysicalFixedPricedContract.Costs, intercoPhysicalFixedPricedContract.CompanyId, dataVersionId);
                        }
                    }

                    // Update Reference & Memo Details
                    var sourceDataVersionId = await _systemDateTimeService.GetCompanyCurrentDataVersionId(physicalFixedPricedContract.CompanyId);

                    await UpdateReferenceAndInternalMemo(sourceDataVersionId,
                                                         references.FirstOrDefault().PhysicalContractId,
                                                         request.IntercoCompanyId.ToUpper() + "/" + intercoReferences.FirstOrDefault().ContractLabel,
                                                         dataVersionId,
                                                         intercoReferences.FirstOrDefault().PhysicalContractId,
                                                         request.CompanyId.ToUpper() + "/" + references.FirstOrDefault().ContractLabel,
                                                         request.Type);

                    sectionReferences.AddRange(intercoReferences);
                }
                else
                {
                    throw new AtlasBusinessException($"Invalid Counterparty for Interco Contract.");
                }
            }
            return(sectionReferences);
        }