PI2024-23 #2
@ -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);
|
||||
|
||||
|
||||
|
@ -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
|
||||
@ -133,10 +131,18 @@ namespace FirmTracker_Server.nHibernate.Transactions
|
||||
|
||||
var product = session.Get<Product>(transactionProduct.ProductID);
|
||||
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;
|
||||
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();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user