From cdcc6b666db961362e9fc1e261b00ddae6a41e47 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Mon, 14 Nov 2022 07:19:33 -0800 Subject: [PATCH] Cancel invoice --- .../Controllers/InvoiceController.cs | 33 +++++++++++++------ .../Data/InventoryContext.cs | 1 + 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs index e35881a..7977f6a 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InvoiceController.cs @@ -191,20 +191,25 @@ public class InvoiceController : Controller { } [HttpPost("Cancel/{id}"), Authorize(Roles = "Client, Administrateur")] - public async Task> Cancel(int id) { + public ActionResult Cancel(int id) { InvoiceModel inv; + List prods; IList roles; try { // Trouver la commande. inv = _context.Invoices.Where(x => x.Id == id) - .Include("Product").First(); + .Include(x => x.Products).ThenInclude(x => x.Product).First(); } catch (Exception e) { _logger.LogError(8, e.Message); return BadRequest(); } try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver. - roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User)); + var user = _userMan.GetUserAsync(_signInMan.Context.User); + user.Wait(); + var rolecall = _userMan.GetRolesAsync(user.Result); + rolecall.Wait(); + roles = rolecall.Result; } catch (Exception e) { _logger.LogError(10, e.Message); roles = new List(); @@ -225,13 +230,21 @@ public class InvoiceController : Controller { inv.Status = InvoiceModel.InStates.Cancelled; - foreach (var prod in inv.Products) { // Revert l'inventaire. - if (prod.Product.Quantity == 0) - if (prod.Product.Status == ProductModel.States.Discontinued) - prod.Product.Status = ProductModel.States.Clearance; - else prod.Product.Status = ProductModel.States.Available; - prod.Product.Quantity = prod.Product.Quantity + prod.Quantity; - prod.Product.Sales -= prod.Quantity; + try { + prods = _context.Products.Where(x => inv.Products.Select(x => x.Product.Id).Contains(x.Id)).ToList(); + foreach (var prod in inv.Products) { // Revert de quantités dans l'inventaire. + ProductModel inventProd = prods.First(x => x.Id == prod.Product.Id); + inventProd.Quantity += prod.Quantity; + inventProd.Status = inventProd.Status == ProductModel.States.Discontinued ? + ProductModel.States.Clearance : + ProductModel.States.Available; + if (inventProd.Sales - prod.Quantity < 0) + inventProd.Sales = 0; + else inventProd.Sales -= prod.Quantity; + } + } catch (Exception e) { + _logger.LogError(8, e.Message); + return BadRequest(); } try { diff --git a/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs b/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs index 6316b5c..007d364 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Data/InventoryContext.cs @@ -9,6 +9,7 @@ public class InventoryContext : IdentityDbContext { public DbSet Products { get; set; } public DbSet Addresses { get; set; } public DbSet Invoices { get; set; } + //public DbSet ProductInvoice { get; set; } public InventoryContext(DbContextOptions options) : base(options) { }