PI2024-23 #2

Merged
s464958 merged 21 commits from PI2024-23 into master 2024-06-14 14:47:25 +02:00
2 changed files with 28 additions and 26 deletions
Showing only changes of commit 3b32e30d00 - Show all commits

View File

@ -60,22 +60,13 @@ namespace FirmTracker_Server.Controllers
int type = _productCRUD.GetProductType(product.ProductID);
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}");
}
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)
{
return BadRequest(new {
Message = ioe.Message,
ErrorCode = "Availability"
});
return BadRequest(ioe.Message);
}
catch (Exception ex)
{
@ -134,10 +122,9 @@ namespace FirmTracker_Server.Controllers
{
product.TransactionId = transaction.Id;
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);

View File

@ -87,7 +87,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
try
{
var oldTransaction = session.Load<Transaction>(transaction.Id);
var oldTransaction = session.Get<Transaction>(transaction.Id);
foreach (var tp in oldTransaction.TransactionProducts)
{
var product = session.Get<Product>(tp.ProductID);
@ -96,17 +96,14 @@ namespace FirmTracker_Server.nHibernate.Transactions
if (product.Type != 0)
{
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.Flush();
session.Clear();
transaction.TotalPrice = 0;
foreach (var transactionProduct in transaction.TransactionProducts)
{
var product = session.Get<Product>(transactionProduct.ProductID);
@ -125,6 +122,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
transactionProduct.TransactionId = transaction.Id;
session.SaveOrUpdate(transactionProduct);
}
transaction.TotalPrice = Math.Round(transaction.TotalPrice, 2, MidpointRounding.AwayFromZero);
session.Update(transaction);
// Decrease product quantities based on transaction
@ -134,8 +132,16 @@ namespace FirmTracker_Server.nHibernate.Transactions
var product = session.Get<Product>(transactionProduct.ProductID);
if (product.Type != 0)
{
product.Availability -= transactionProduct.Quantity;
session.Update(product);
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;
session.Update(product);
}
}
}
sessionTransaction.Commit();
@ -175,6 +181,15 @@ namespace FirmTracker_Server.nHibernate.Transactions
var transaction = session.Get<Transaction>(transactionId);
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);
t.Commit();
}