public async Task <IEnumerable <SectionReference> > Handle(CreatePhysicalFixedPricedContractCommand request, CancellationToken cancellationToken) { List <SectionReference> sectionReferences = new List <SectionReference>(); _unitOfWork.BeginTransaction(); try { if (request.ContractReference?.Length > 0) { request.ContractReference = request.ContractReference.PadLeft(7, '0'); } var companyDate = await _systemDateTimeService.GetCompanyDate(request.CompanyId); if (request.ContractDate.Date > companyDate.Date) { throw new AtlasBusinessException($"Contract date cannot be in the future. Contract date: {request.ContractDate.Date}. Company date: {companyDate}."); } if (request.DataVersionId != null) { var freezeDate = await _freezeRepository.GetFreezeNotClosedAsync(request.CompanyId, request.DataVersionId.Value); if (freezeDate == null) { throw new AtlasBusinessException($"Contracts cannot be created in a freeze if the month is closed."); } if (request.ContractReference == null) { throw new AtlasBusinessException($"Contract reference is mandatory in a frozen database."); } } await PhysicalContractCommonRules.ValidatePhysicalContract(request, _identityService, _masterDataService, _userService, _systemDateTimeService); var user = _identityService.GetUserName(); request.CreatedBy = user; var physicalFixedPricedContract = _mapper.Map <Section>(request); var company = await _masterDataService.GetCompanyByIdAsync(request.CompanyId); if (company.IsProvinceEnable) { physicalFixedPricedContract.ProvinceId = company.DefaultProvinceId; physicalFixedPricedContract.BranchId = company.DefaultBranchId; } else { physicalFixedPricedContract.ProvinceId = null; physicalFixedPricedContract.BranchId = null; } // At least one contract has to be always created physicalFixedPricedContract.NumberOfContract = (request.NumberOfContracts > 0) ? request.NumberOfContracts : 1; physicalFixedPricedContract.IsInterco = request.IsInterco; if (!string.IsNullOrWhiteSpace(physicalFixedPricedContract.OtherReference)) { physicalFixedPricedContract.EstimatedMaturityDate = null; } physicalFixedPricedContract.EstimatedMaturityDate = await CalculateEstimatedMaturityDate(physicalFixedPricedContract, request.CompanyId); var references = (await _tradeRepository.CreatePhysicalContractAsImageAsync(physicalFixedPricedContract)).ToList(); sectionReferences.AddRange(references); foreach (var item in references) { if (item.SectionId > 0 && item.Quantity > 0 && physicalFixedPricedContract.Costs.Any()) { foreach (var costItem in physicalFixedPricedContract.Costs) { costItem.CompanyId = request.CompanyId; costItem.SectionId = item.SectionId; costItem.DataVersionId = item.DataVersionId; } // check the business rule for PriceUnitId. await _costRepository.AddCostsAsync(physicalFixedPricedContract.Costs, request.CompanyId, request.DataVersionId); } // request.ChildSections would not be null if only it is Imaging the trade and we have selected Image Splits/tranche option as well if (request.ChildSections != null) { if (item.SectionId > 0 && request.ChildSections.Any()) { CreateSplitCommand splitRequest = new CreateSplitCommand(); splitRequest.ChildSections = request.ChildSections; splitRequest.ChildSections.ToList().ForEach(section => section.OriginalQuantity = 0); splitRequest.CompanyId = request.CompanyId; splitRequest.OriginalQuantity = request.Quantity; splitRequest.Quantity = request.Quantity; var sectionTrancheContract = _mapper.Map <SectionDeprecated>(splitRequest); sectionTrancheContract.SectionId = item.SectionId; sectionTrancheContract.ContractLabel = item.ContractLabel; foreach (var splitTrancheItem in sectionTrancheContract.ChildSections) { splitTrancheItem.SectionId = 0; splitTrancheItem.ContractId = item.PhysicalContractId; splitTrancheItem.SectionOriginId = (int)item.SectionId; splitTrancheItem.EstimatedMaturityDate = physicalFixedPricedContract.EstimatedMaturityDate; } bool isTradeImage = true; var splitTranchReferences = await _tradeRepository.CreateTrancheSplitAsync(sectionTrancheContract, request.CompanyId, isTradeImage); // Inserting cost while creating an Image of a trade,if available foreach (var childItem in splitTranchReferences) { if (request.ChildSections.First().Costs != null) { var childCosts = request.ChildSections.First().Costs; foreach (var costItem in childCosts) { costItem.CompanyId = request.CompanyId; costItem.SectionId = childItem.SectionId; costItem.DataVersionId = childItem.DataVersionId; } await _costRepository.AddCostsAsync(childCosts, request.CompanyId, request.DataVersionId); } } } } if (request.IsInterco) { sectionReferences = await CreateIntercoContractOnCreateContract(request, physicalFixedPricedContract, references, sectionReferences); } if (request.DataVersionId != null) { // this might be incorrect. This is due to the code that is written above where you can create multiple contracts in your Current db with Image await InsertFreezeRecalcProcessQueue(references[0].SectionId, request.DataVersionId, request.CompanyId); } } _unitOfWork.Commit(); _logger.LogInformation("New physical contract with id {Atlas_ContractLabel} created.", references[0].ContractLabel); return(sectionReferences); } catch { _unitOfWork.Rollback(); throw; } }