przywracanie dostępności po delete transakcji, korekcja ceny po updacie i obsługa update transakcji ponad dostępność produktu

This commit is contained in:
Kamil Ryżek 2024-06-13 16:36:44 +02:00
parent 06f22951c9
commit 3b32e30d00
2 changed files with 28 additions and 26 deletions

View File

@ -60,22 +60,13 @@ namespace FirmTracker_Server.Controllers
int type = _productCRUD.GetProductType(product.ProductID); int type = _productCRUD.GetProductType(product.ProductID);
if (type == 1) if (type == 1)
{ {
int availability = _productCRUD.GetProductAvailability(product.ProductID); var prod = _productCRUD.GetProduct(product.ProductID);
if (product.Quantity > availability) if (product.Quantity > prod.Availability)
{ {
throw new InvalidOperationException($"Can't add product {product.ProductID} to transaction. Available: {availability}, Desired: {product.Quantity}"); throw new InvalidOperationException($"Can't add product {prod.Name} to transaction. Available: {prod.Availability}, Desired: {product.Quantity}");
//return BadRequest($"Can't add product {product.ProductID} to transaction. Available: {availability}, Desired: {product.Quantity}"); //return BadRequest($"Can't add product {product.ProductID} to transaction. Available: {availability}, Desired: {product.Quantity}");
} }
else
{
//transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
}
}
else
{
//transaction.TotalPrice += (price * ((1 - (transaction.Discount / 100))));
} }
} }
@ -95,10 +86,7 @@ namespace FirmTracker_Server.Controllers
} }
catch (InvalidOperationException ioe) catch (InvalidOperationException ioe)
{ {
return BadRequest(new { return BadRequest(ioe.Message);
Message = ioe.Message,
ErrorCode = "Availability"
});
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -134,10 +122,9 @@ namespace FirmTracker_Server.Controllers
{ {
product.TransactionId = transaction.Id; product.TransactionId = transaction.Id;
decimal price = _productCRUD.GetProductPrice(product.ProductID); decimal price = _productCRUD.GetProductPrice(product.ProductID);
//transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
} }
transaction.TotalPrice = Math.Round(transaction.TotalPrice, 2, MidpointRounding.AwayFromZero);
_transactionCRUD.UpdateTransaction(transaction); _transactionCRUD.UpdateTransaction(transaction);

View File

@ -87,7 +87,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
try try
{ {
var oldTransaction = session.Load<Transaction>(transaction.Id); var oldTransaction = session.Get<Transaction>(transaction.Id);
foreach (var tp in oldTransaction.TransactionProducts) foreach (var tp in oldTransaction.TransactionProducts)
{ {
var product = session.Get<Product>(tp.ProductID); var product = session.Get<Product>(tp.ProductID);
@ -96,17 +96,14 @@ namespace FirmTracker_Server.nHibernate.Transactions
if (product.Type != 0) if (product.Type != 0)
{ {
product.Availability += tp.Quantity; product.Availability += tp.Quantity;
transaction.TotalPrice -= ((tp.Quantity * product.Price) * ((1 - (transaction.Discount / 100))));
}
else
{
transaction.TotalPrice -= (product.Price) * ((1 - (transaction.Discount / 100)));
} }
session.Update(product); session.Update(product);
} }
session.Flush(); session.Flush();
session.Clear(); session.Clear();
transaction.TotalPrice = 0;
foreach (var transactionProduct in transaction.TransactionProducts) foreach (var transactionProduct in transaction.TransactionProducts)
{ {
var product = session.Get<Product>(transactionProduct.ProductID); var product = session.Get<Product>(transactionProduct.ProductID);
@ -125,6 +122,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
transactionProduct.TransactionId = transaction.Id; transactionProduct.TransactionId = transaction.Id;
session.SaveOrUpdate(transactionProduct); session.SaveOrUpdate(transactionProduct);
} }
transaction.TotalPrice = Math.Round(transaction.TotalPrice, 2, MidpointRounding.AwayFromZero);
session.Update(transaction); session.Update(transaction);
// Decrease product quantities based on transaction // Decrease product quantities based on transaction
@ -133,10 +131,18 @@ namespace FirmTracker_Server.nHibernate.Transactions
var product = session.Get<Product>(transactionProduct.ProductID); var product = session.Get<Product>(transactionProduct.ProductID);
if (product.Type != 0) if (product.Type != 0)
{
if (transactionProduct.Quantity > product.Availability)
{
throw new InvalidOperationException($"Can't add product {product.Name} to transaction. Available: {product.Availability}, Desired: {transactionProduct.Quantity}");
}
else
{ {
product.Availability -= transactionProduct.Quantity; product.Availability -= transactionProduct.Quantity;
session.Update(product); session.Update(product);
} }
}
} }
sessionTransaction.Commit(); sessionTransaction.Commit();
} }
@ -175,6 +181,15 @@ namespace FirmTracker_Server.nHibernate.Transactions
var transaction = session.Get<Transaction>(transactionId); var transaction = session.Get<Transaction>(transactionId);
if (transaction != null) if (transaction != null)
{ {
foreach (var transactionProduct in transaction.TransactionProducts)
{
var product = session.Get<Product>(transactionProduct.ProductID);
if (product.Type != 0)
{
product.Availability += transactionProduct.Quantity;
session.Update(product);
}
}
session.Delete(transaction); session.Delete(transaction);
t.Commit(); t.Commit();
} }