public async Task <IActionResult> PostInvoice([FromBody] List <Invoice> invoices) { // Verify whether any invoice was sent to the server if (invoices.Any()) { // Create new empty list to saved success invoices that will be processed to the server List <Invoice> successInvoices = new List <Invoice>(); // Create new empty list to save invoices that could not be processed to the server List <Object> errorInvoices = new List <Object>(); // Iterate through all invoices sent foreach (var invoice in invoices) { // Verify whether invoice number is not duplicated if (_context.Invoices.Any(x => x.InvoiceNumber == invoice.InvoiceNumber)) { // Add order to error list if invoice number already exists errorInvoices.Add(new { invoice, error = $"Invoice number {invoice.InvoiceNumber} already exists" }); // Continue with the next iteration continue; } // Verify whether order number exists if (!_context.Orders.Any(x => x.OrderNumber == invoice.OrderNumber)) { // Add order to error list if invoice number already exists errorInvoices.Add(new { invoice, error = $"Order number {invoice.OrderNumber} for invoice {invoice.InvoiceNumber} does not exist" }); // Continue with the next iteration continue; } // Get invoice items List <InvoiceItem> items = invoice.Items; // Verify whether the invoice has items if (items == null || !items.Any()) { // Add invoice to error list if not any item was specified errorInvoices.Add(new { invoice, error = "Items can not be empty" }); // Continue with the next iteration continue; } // Add success invoice to the list successInvoices.Add(invoice); } // Create new empty list to save invoices that will be sent to the server List <Object> savedInvoices = new List <Object>(); // Verify whether success invoices list is not empty if (successInvoices.Any()) { // Create a new synchronization record SynchronizationRecord synchronizationRecord = new SynchronizationRecord { Route = successInvoices.FirstOrDefault().Route, TypeOfCatalogId = "8", // Invoice TypeOfVisitId = "2", // Delivery FileName = "api.v1.invoices.postinvoices", SynchronizationDate = DateTime.Now, RecordsRead = successInvoices.Count, RecordsSynchronized1 = successInvoices.Count, RecordsSynchronized2 = successInvoices.Count, ProcessType = Enumerations.ProcessType.Upload }; // Add synchronization record to the context _context.SynchronizationRecords.Add(synchronizationRecord); // Save synchronization record to the database await _context.SaveChangesAsync(); // Iterate through success invoices foreach (var invoice in successInvoices) { // Set default values to invoice instance invoice.SynchronizationDate = DateTime.Now; invoice.IsInvoiceFinished = false; invoice.IsInvoiceCanceled = false; invoice.SynchronizationId = synchronizationRecord.Id; // Add invoice to the context _context.Invoices.Add(invoice); // Get order by order number Order order = _context .Orders .Where(x => x.OrderNumber == invoice.OrderNumber) .FirstOrDefault(); // Verify whether order is not null if (order != null) { // Set is order finished to true order.IsOrderFinished = true; // Add order to modified state _context.Entry(order).State = EntityState.Modified; } // Save invoice changes await _context.SaveChangesAsync(); // Add saved invoice to the list savedInvoices.Add(new { invoice }); } // Get max invoice number int lastInvoiceSequence = invoices .Select(x => Int32.Parse(x.InvoiceNumber.Split("-")[1])) .Max(); // Get last invoice number string lastInvoiceNumber = invoices .Where(x => x.InvoiceNumber == $"{invoices.First().Route}-{lastInvoiceSequence.ToString()}") .Select(x => x.InvoiceNumber) .FirstOrDefault(); // Get current consecutive invoice number ConsecutiveInvoice consecutiveInvoice = _context .ConsecutiveInvoices .Where(x => x.Route == invoices.First().Route) .Where(x => x.DocumentTypeId == "1") .FirstOrDefault(); // Verify whether last invoice number from invoices list is greater than last consecutive invoice number if (lastInvoiceSequence > consecutiveInvoice.SequenceNumber) { consecutiveInvoice.SequenceNumber = lastInvoiceSequence; } else { consecutiveInvoice.SequenceNumber += 1; } consecutiveInvoice.LastInvoiceNumber = lastInvoiceNumber; consecutiveInvoice.UpdatedAt = DateTime.Now; // Update consecutive invoice _context.Entry(consecutiveInvoice).State = EntityState.Modified; // Save changes to database await _context.SaveChangesAsync(); } return(StatusCode(201, new { InvoicesWithoutErrors = savedInvoices, InvoicesWithErrors = errorInvoices })); } else { return(BadRequest("Invoices can not be empty")); } }
public async Task <IActionResult> PostPaymentReceipt([FromBody] List <PaymentReceipt> paymentReceipts) { // Verify whether any invoice was sent to the server if (paymentReceipts.Any()) { // Create new empty list to saved success receipts that will be processed to the server List <PaymentReceipt> successReceipts = new List <PaymentReceipt>(); // Create new empty list to save receipts that could not be processed to the server List <Object> errorReceipts = new List <Object>(); // Iterate through all receipts sent foreach (var receipt in paymentReceipts) { // Verify whether receipt number is not duplicated if (_context.PaymentReceipts.Any(x => x.ReceiptNumber == receipt.ReceiptNumber)) { // Add order to error list if receipt number already exists errorReceipts.Add(new { receipt, error = $"Receipt number {receipt.ReceiptNumber} already exists" }); // Continue with the next iteration continue; } // Get receipt items List <PaymentReceiptItem> items = receipt.Items; // Verify whether the receipt has items if (items == null || !items.Any()) { // Add receipt to error list if not any item was specified errorReceipts.Add(new { receipt, error = "Items can not be empty" }); // Continue with the next iteration continue; } // Add success invoice to the list successReceipts.Add(receipt); } // Create new empty list to save receipts that will be sent to the server List <Object> savedReceipts = new List <Object>(); // Verify whether success receipts list is not empty if (successReceipts.Any()) { // Create a new synchronization record SynchronizationRecord synchronizationRecord = new SynchronizationRecord { Route = successReceipts.FirstOrDefault().Route, TypeOfCatalogId = "7", // Receipt TypeOfVisitId = "2", // Delivery FileName = "api.v1.paymentreceipts.postpaymentreceipts", SynchronizationDate = DateTime.Now, RecordsRead = successReceipts.Count, RecordsSynchronized1 = successReceipts.Count, RecordsSynchronized2 = successReceipts.Count, ProcessType = Enumerations.ProcessType.Upload }; // Add synchronization record to the context _context.SynchronizationRecords.Add(synchronizationRecord); // Save synchronization record to the database await _context.SaveChangesAsync(); // Iterate through success receipts foreach (var receipt in successReceipts) { // Set default values to receipt instance receipt.SynchronizationDate = DateTime.Now; receipt.SynchronizationId = synchronizationRecord.Id; // Add receipt to the context _context.PaymentReceipts.Add(receipt); // Save invoice changes await _context.SaveChangesAsync(); // Add saved receipt to the list savedReceipts.Add(new { receipt }); } // Get max receipt sequence int lastReceiptSequence = paymentReceipts .Select(x => Int32.Parse(x.ReceiptNumber.Split("-")[1])) .Max(); // Get last receipt number string lastReceiptNumber = paymentReceipts .Where(x => x.ReceiptNumber == $"{paymentReceipts.First().Route}-{lastReceiptSequence.ToString()}") .Select(x => x.ReceiptNumber) .FirstOrDefault(); // Get current consecutive receipt number ConsecutiveInvoice consecutiveInvoice = _context .ConsecutiveInvoices .Where(x => x.Route == paymentReceipts.First().Route) .Where(x => x.DocumentTypeId == "2") .FirstOrDefault(); // Verify whether last receipt number from receipts list is greater than last consecutive receipt number if (lastReceiptSequence > consecutiveInvoice.SequenceNumber) { consecutiveInvoice.SequenceNumber = lastReceiptSequence; } else { consecutiveInvoice.SequenceNumber += 1; } consecutiveInvoice.LastInvoiceNumber = lastReceiptNumber; consecutiveInvoice.UpdatedAt = DateTime.Now; // Update consecutive invoice _context.Entry(consecutiveInvoice).State = EntityState.Modified; // Save changes to database await _context.SaveChangesAsync(); } return(StatusCode(201, new { ReceiptsWithoutErrors = savedReceipts, ReceiptsWithErrors = errorReceipts })); } else { return(BadRequest("Payment receipts can not be empty")); } }