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; } }