Exemplo n.º 1
0
        public async Task DeleteAsync(int id)
        {
            try
            {
                var includeQuery = new QuerySet();
                includeQuery.Include.Fields = new string[] { "order" };
                var depositInvoice = await _depositInvoiceDataProvider.GetByIdAsync(id, includeQuery);

                if (depositInvoice.BookingDate != null)
                {
                    var message = $"Deposit-invoice {id} cannot be deleted because it has already been transferred to the accounting system.";
                    _logger.LogError(message);
                    throw new InvalidOperationException(message);
                }

                try
                {
                    var invoice = await _invoiceDataProvider.GetByOrderIdAsync(depositInvoice.Order.Id);

                    var message = $"Deposit-invoice {id} cannot be deleted because invoice {invoice.Id} is attached to it.";
                    _logger.LogError(message);
                    throw new InvalidOperationException(message);
                }
                catch (EntityNotFoundException)
                {
                    await _documentGenerationManager.DeleteDepositInvoiceDocumentAsync(id);

                    await _documentGenerationManager.DeleteCertificateTemplateForDepositInvoiceAsync(id);

                    await _documentGenerationManager.DeleteCertificateForDepositInvoiceAsync(id);

                    await _depositInvoiceDataProvider.DeleteByIdAsync(id);
                }
            }
            catch (EntityNotFoundException)
            {
                // Deposit invoice not found. Nothing should happen.
            }
        }