Cancel invoice
This commit is contained in:
		| @@ -191,20 +191,25 @@ public class InvoiceController : Controller { | ||||
|     } | ||||
|  | ||||
|     [HttpPost("Cancel/{id}"), Authorize(Roles = "Client, Administrateur")] | ||||
|     public async Task<ActionResult<InvoiceModel>> Cancel(int id) { | ||||
|     public ActionResult<InvoiceModel> Cancel(int id) { | ||||
|         InvoiceModel inv; | ||||
|         List<ProductModel> prods; | ||||
|         IList<string> 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<string>(); | ||||
| @@ -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 { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user