Cancel invoice

This commit is contained in:
MarcEricMartel 2022-11-14 07:19:33 -08:00
parent 94567c2395
commit cdcc6b666d
2 changed files with 24 additions and 10 deletions

View File

@ -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 {

View File

@ -9,6 +9,7 @@ public class InventoryContext : IdentityDbContext<InventoryUser> {
public DbSet<ProductModel> Products { get; set; }
public DbSet<AddressModel> Addresses { get; set; }
public DbSet<InvoiceModel> Invoices { get; set; }
//public DbSet<InvoiceModel.ProductInvoice> ProductInvoice { get; set; }
public InventoryContext(DbContextOptions<InventoryContext> options) : base(options) { }