public async Task <Unit> Handle(DeallocateSectionCommand request, CancellationToken cancellationToken) { _unitOfWork.BeginTransaction(); try { if (request.DataVersionId != null) { var freezeDate = await _freezeRepository.GetFreezeNotClosedAsync(request.Company, request.DataVersionId.Value); if (freezeDate == null) { throw new AtlasBusinessException($"Contracts cannot be updated in a freeze if the month is closed."); } } await _tradeAllocationRepository.DeallocateAsync(request.SectionId, request.ReInstateTrafficDetails, request.Company, request.DataVersionId); _unitOfWork.Commit(); return(Unit.Value); } catch { _unitOfWork.Rollback(); throw; } }
public async Task <long> Handle(AllocateSectionCommand request, CancellationToken cancellationToken) { _unitOfWork.BeginTransaction(); try { var allocationOperation = _mapper.Map <AllocationOperation>(request); if (request.DataVersionId != null) { var freezeDate = await _freezeRepository.GetFreezeNotClosedAsync(request.Company, request.DataVersionId.Value); if (freezeDate == null) { throw new AtlasBusinessException($"Contracts cannot be allocated in a freeze if the month is closed."); } } var response = await _tradeAllocationRepository.AllocateAsync(allocationOperation); if (request.DataVersionId != null) { await InsertFreezeRecalcProcessQueue(request.SectionId, request.DataVersionId, request.Company); await InsertFreezeRecalcProcessQueue(request.AllocatedSectionId, request.DataVersionId, request.Company); } _unitOfWork.Commit(); _logger.LogInformation("Section {Atlas_SectionId} allocated with section {Atlas_AllocatedSectionId}.", request.SectionId, request.AllocatedSectionId); return(response); } catch { _unitOfWork.Rollback(); throw; } }
public async Task <Unit> Handle(UpdateSectionTrafficCommand request, CancellationToken cancellationToken) { _unitOfWork.BeginTransaction(); try { var sectionTraffic = ConvertToSectionTraffic(request); var authorizationResult = await _authorizationService.AuthorizeAsync(_identityService.GetUser(), sectionTraffic, Policies.CharterPolicy); var vesselNamePolicyResult = await _authorizationService.AuthorizeAsync(_identityService.GetUser(), sectionTraffic, Policies.VesselNamePolicy); var blDatePolicyResult = await _authorizationService.AuthorizeAsync(_identityService.GetUser(), sectionTraffic, Policies.BlDatePolicy); var blReferencePolicyResult = await _authorizationService.AuthorizeAsync(_identityService.GetUser(), sectionTraffic, Policies.BlReferencePolicy); var quantityForTrafficPolicyResult = await _authorizationService.AuthorizeAsync(_identityService.GetUser(), sectionTraffic, Policies.QuantityForTrafficPolicy); if (authorizationResult.Succeeded || vesselNamePolicyResult.Succeeded || blDatePolicyResult.Succeeded || blReferencePolicyResult.Succeeded || quantityForTrafficPolicyResult.Succeeded) { if (request.DataVersionId != null) { var freezeDate = await _freezeRepository.GetFreezeNotClosedAsync(request.CompanyId, request.DataVersionId.Value); if (freezeDate == null) { throw new AtlasBusinessException($"Contracts cannot be updated in a freeze if the month is closed."); } } var companyDate = await _systemDateTimeService.GetCompanyDate(request.CompanyId); if (request.BLDate != null && request.BLDate.Value.Date > companyDate.Date) { throw new AtlasBusinessException($"BL Date cannot be in the future. Bl Date: {request.BLDate.Value.Date}. Company date: {companyDate}."); } await _charterRepository.UpdateSectionTrafficDetailsAsync(sectionTraffic, request.CompanyId, request.DataVersionId); if (request.DataVersionId != null) { await InsertFreezeRecalcProcessQueue(request.SectionId, request.DataVersionId, request.CompanyId); } _unitOfWork.Commit(); _logger.LogInformation("Section Traffic with id {Atlas_SectionId} updated.", request.SectionId); } else { throw new AtlasSecurityException("One or more privileges are required to perform this action."); } return(Unit.Value); } catch { _unitOfWork.Rollback(); throw; } }
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; } }
public async Task <Unit> Handle(UpdateInvoiceMarkingCommand request, CancellationToken cancellationToken) { _unitOfWork.BeginTransaction(); try { if (request.DataVersionId != null) { var freezeDate = await _freezeRepository.GetFreezeNotClosedAsync(request.Company, request.DataVersionId.Value); if (freezeDate == null) { throw new AtlasBusinessException($"Contracts cannot be updated in a freeze if the month is closed."); } } List <InvoiceMarking> newInvoices = new List <InvoiceMarking>(); var invoiceStatus = request.InvoiceStatusId; if (request.Invoices.ToList().Count == 0 && invoiceStatus != null && request.SectionId != null) // Dorine { // Even if there is no invoice marking, the invoicing status can be changed from the trade page await _invoiceRepository.UpdateInvoicingStatusAsync((int)request.SectionId, (int)invoiceStatus, request.Company, request.DataVersionId); } else { foreach (var item in request.Invoices) { if (item.Quantity > 0 && item.CostId == null) { item.InvoiceAmount = item.Quantity * item.Price; } item.CompanyId = request.Company; if (item.InvoiceMarkingId != null) { await _invoiceRepository.UpdateInvoiceMarkingAsync(item, invoiceStatus, request.Company); } else { newInvoices.Add(item); } } if (newInvoices.Count > 0) { await _invoiceRepository.AddInvoiceMarkingAsync(newInvoices, invoiceStatus, request.Company); } } _unitOfWork.Commit(); return(Unit.Value); } catch (Exception) { _unitOfWork.Rollback(); throw; } }