Cancel invoice
This commit is contained in:
		@@ -191,20 +191,25 @@ public class InvoiceController : Controller {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    [HttpPost("Cancel/{id}"), Authorize(Roles = "Client, Administrateur")]
 | 
					    [HttpPost("Cancel/{id}"), Authorize(Roles = "Client, Administrateur")]
 | 
				
			||||||
    public async Task<ActionResult<InvoiceModel>> Cancel(int id) {
 | 
					    public ActionResult<InvoiceModel> Cancel(int id) {
 | 
				
			||||||
        InvoiceModel inv;
 | 
					        InvoiceModel inv;
 | 
				
			||||||
 | 
					        List<ProductModel> prods;
 | 
				
			||||||
        IList<string> roles;
 | 
					        IList<string> roles;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try { // Trouver la commande.
 | 
					        try { // Trouver la commande.
 | 
				
			||||||
            inv = _context.Invoices.Where(x => x.Id == id)
 | 
					            inv = _context.Invoices.Where(x => x.Id == id)
 | 
				
			||||||
                                   .Include("Product").First();
 | 
					                                   .Include(x => x.Products).ThenInclude(x => x.Product).First();
 | 
				
			||||||
        } catch (Exception e) {
 | 
					        } catch (Exception e) {
 | 
				
			||||||
            _logger.LogError(8, e.Message);
 | 
					            _logger.LogError(8, e.Message);
 | 
				
			||||||
            return BadRequest();
 | 
					            return BadRequest();
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
 | 
					        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) {
 | 
					        } catch (Exception e) {
 | 
				
			||||||
            _logger.LogError(10, e.Message);
 | 
					            _logger.LogError(10, e.Message);
 | 
				
			||||||
            roles = new List<string>();
 | 
					            roles = new List<string>();
 | 
				
			||||||
@@ -225,13 +230,21 @@ public class InvoiceController : Controller {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        inv.Status = InvoiceModel.InStates.Cancelled;
 | 
					        inv.Status = InvoiceModel.InStates.Cancelled;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        foreach (var prod in inv.Products) { // Revert l'inventaire.
 | 
					        try {
 | 
				
			||||||
            if (prod.Product.Quantity == 0)
 | 
					            prods = _context.Products.Where(x => inv.Products.Select(x => x.Product.Id).Contains(x.Id)).ToList();
 | 
				
			||||||
                if (prod.Product.Status == ProductModel.States.Discontinued)
 | 
					            foreach (var prod in inv.Products) { // Revert de quantités dans l'inventaire.
 | 
				
			||||||
                    prod.Product.Status = ProductModel.States.Clearance;
 | 
					                ProductModel inventProd = prods.First(x => x.Id == prod.Product.Id);
 | 
				
			||||||
                else prod.Product.Status = ProductModel.States.Available;
 | 
					                inventProd.Quantity += prod.Quantity;
 | 
				
			||||||
            prod.Product.Quantity = prod.Product.Quantity + prod.Quantity;
 | 
					                inventProd.Status = inventProd.Status == ProductModel.States.Discontinued ?
 | 
				
			||||||
            prod.Product.Sales -= prod.Quantity;
 | 
					                                                         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 {
 | 
					        try {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -9,6 +9,7 @@ public class InventoryContext : IdentityDbContext<InventoryUser> {
 | 
				
			|||||||
    public DbSet<ProductModel> Products { get; set; }
 | 
					    public DbSet<ProductModel> Products { get; set; }
 | 
				
			||||||
    public DbSet<AddressModel> Addresses { get; set; }
 | 
					    public DbSet<AddressModel> Addresses { get; set; }
 | 
				
			||||||
    public DbSet<InvoiceModel> Invoices { get; set; }
 | 
					    public DbSet<InvoiceModel> Invoices { get; set; }
 | 
				
			||||||
 | 
					    //public DbSet<InvoiceModel.ProductInvoice> ProductInvoice { get; set; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public InventoryContext(DbContextOptions<InventoryContext> options) : base(options) { }
 | 
					    public InventoryContext(DbContextOptions<InventoryContext> options) : base(options) { }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user