2024-05-06 21:25:10 +02:00
using FirmTracker_Server.nHibernate.Products ;
2024-05-05 22:08:27 +02:00
using NHibernate ;
using NHibernate.Linq ;
2024-05-06 21:25:10 +02:00
using System ;
using System.Collections.Generic ;
using System.Linq ;
2024-05-16 00:42:06 +02:00
using System.Transactions ;
2024-05-05 22:08:27 +02:00
namespace FirmTracker_Server.nHibernate.Transactions
{
public class TransactionCRUD
{
public void AddTransaction ( Transaction transaction )
{
using ( var session = SessionFactory . OpenSession ( ) )
using ( var sessionTransaction = session . BeginTransaction ( ) )
{
try
{
2024-05-06 21:25:10 +02:00
foreach ( var transactionProduct in transaction . TransactionProducts )
{
2024-06-11 23:54:59 +02:00
var product = session . Get < Product > ( transactionProduct . ProductID ) ;
if ( product . Type ! = 0 )
{
transaction . TotalPrice + = ( ( transactionProduct . Quantity * product . Price ) * ( ( 1 - ( transaction . Discount / 100 ) ) ) ) ;
}
else
{
transaction . TotalPrice + = ( product . Price ) * ( ( 1 - ( transaction . Discount / 100 ) ) ) ;
}
2024-06-12 14:03:49 +02:00
transactionProduct . TransactionId = transaction . Id ;
2024-05-06 21:25:10 +02:00
session . Save ( transactionProduct ) ;
}
2024-06-12 14:03:49 +02:00
transaction . TotalPrice = Math . Round ( transaction . TotalPrice , 2 , MidpointRounding . AwayFromZero ) ;
2024-05-05 22:08:27 +02:00
session . Save ( transaction ) ;
2024-05-06 21:25:10 +02:00
// Decrease product quantities based on transaction
foreach ( var transactionProduct in transaction . TransactionProducts )
{
2024-05-16 00:42:06 +02:00
var product = session . Get < Product > ( transactionProduct . ProductID ) ;
2024-05-06 21:25:10 +02:00
if ( product . Type ! = 0 )
{
product . Availability - = transactionProduct . Quantity ;
session . Update ( product ) ;
}
}
2024-05-05 22:08:27 +02:00
sessionTransaction . Commit ( ) ;
}
catch
{
sessionTransaction . Rollback ( ) ;
throw ;
}
}
}
2024-05-06 21:25:10 +02:00
//usage of HQL
2024-05-16 00:42:06 +02:00
public Transaction2 GetTransaction ( int transactionId )
2024-05-05 22:08:27 +02:00
{
using ( var session = SessionFactory . OpenSession ( ) )
{
2024-05-06 21:25:10 +02:00
var query = session . CreateQuery ( @ "
2024-05-16 00:42:06 +02:00
SELECT t
FROM Transaction2 t
LEFT JOIN FETCH t . TransactionProducts tp
LEFT JOIN FETCH tp . Product
2024-05-06 21:25:10 +02:00
WHERE t . Id = : transactionId
");
query . SetParameter ( "transactionId" , transactionId ) ;
2024-05-16 00:42:06 +02:00
var transaction = query . UniqueResult < Transaction2 > ( ) ;
2024-05-05 22:08:27 +02:00
return transaction ;
}
}
2024-05-06 21:25:10 +02:00
2024-05-16 00:42:06 +02:00
2024-05-05 22:08:27 +02:00
public void UpdateTransaction ( Transaction transaction )
{
using ( var session = SessionFactory . OpenSession ( ) )
2024-05-16 00:42:06 +02:00
using ( var sessionTransaction = session . BeginTransaction ( ) )
2024-05-05 22:08:27 +02:00
{
try
{
2024-06-13 15:25:07 +02:00
2024-06-13 16:36:44 +02:00
var oldTransaction = session . Get < Transaction > ( transaction . Id ) ;
2024-06-13 15:25:07 +02:00
foreach ( var tp in oldTransaction . TransactionProducts )
{
var product = session . Get < Product > ( tp . ProductID ) ;
if ( product . Type ! = 0 )
{
product . Availability + = tp . Quantity ;
}
session . Update ( product ) ;
}
session . Flush ( ) ;
session . Clear ( ) ;
2024-06-13 16:36:44 +02:00
transaction . TotalPrice = 0 ;
2024-05-16 00:42:06 +02:00
foreach ( var transactionProduct in transaction . TransactionProducts )
{
2024-06-13 15:25:07 +02:00
var product = session . Get < Product > ( transactionProduct . ProductID ) ;
2024-06-11 23:54:59 +02:00
if ( product . Type ! = 0 )
{
transaction . TotalPrice + = ( ( transactionProduct . Quantity * product . Price ) * ( ( 1 - ( transaction . Discount / 100 ) ) ) ) ;
}
else
{
transaction . TotalPrice + = ( product . Price ) * ( ( 1 - ( transaction . Discount / 100 ) ) ) ;
2024-06-13 15:25:07 +02:00
}
2024-05-16 00:42:06 +02:00
transactionProduct . TransactionId = transaction . Id ;
2024-06-13 15:25:07 +02:00
session . SaveOrUpdate ( transactionProduct ) ;
2024-05-16 00:42:06 +02:00
}
2024-06-13 16:36:44 +02:00
transaction . TotalPrice = Math . Round ( transaction . TotalPrice , 2 , MidpointRounding . AwayFromZero ) ;
2024-05-05 22:08:27 +02:00
session . Update ( transaction ) ;
2024-05-16 00:42:06 +02:00
// Decrease product quantities based on transaction
foreach ( var transactionProduct in transaction . TransactionProducts )
{
var product = session . Get < Product > ( transactionProduct . ProductID ) ;
if ( product . Type ! = 0 )
{
2024-06-13 16:36:44 +02:00
if ( transactionProduct . Quantity > product . Availability )
{
2024-06-14 13:46:20 +02:00
throw new InvalidOperationException ( $"Nie można dodać {product.Name} do transakcji. Dostępność: {product.Availability}, Zażądano: {transactionProduct.Quantity}" ) ;
2024-06-13 16:36:44 +02:00
}
else
{
product . Availability - = transactionProduct . Quantity ;
session . Update ( product ) ;
}
2024-05-16 00:42:06 +02:00
}
}
sessionTransaction . Commit ( ) ;
}
catch
{
sessionTransaction . Rollback ( ) ;
throw ;
}
}
}
public void UpdateTransactionProduct ( TransactionProduct transactionProduct )
{
using ( var session = SessionFactory . OpenSession ( ) )
using ( var t = session . BeginTransaction ( ) )
{
try
{
session . Update ( transactionProduct ) ;
2024-05-05 22:08:27 +02:00
t . Commit ( ) ;
}
catch
{
t . Rollback ( ) ;
throw ;
}
}
}
public void DeleteTransaction ( int transactionId )
{
using ( var session = SessionFactory . OpenSession ( ) )
using ( var t = session . BeginTransaction ( ) )
{
try
{
2024-05-06 21:25:10 +02:00
var transaction = session . Get < Transaction > ( transactionId ) ;
2024-05-05 22:08:27 +02:00
if ( transaction ! = null )
{
2024-06-13 16:36:44 +02:00
foreach ( var transactionProduct in transaction . TransactionProducts )
{
var product = session . Get < Product > ( transactionProduct . ProductID ) ;
if ( product . Type ! = 0 )
{
product . Availability + = transactionProduct . Quantity ;
session . Update ( product ) ;
}
}
2024-05-05 22:08:27 +02:00
session . Delete ( transaction ) ;
t . Commit ( ) ;
}
}
catch
{
t . Rollback ( ) ;
throw ;
}
}
}
2024-05-06 21:25:10 +02:00
public void AddTransactionProductToTransaction ( int transactionId , TransactionProduct transactionProduct )
{
using ( var session = SessionFactory . OpenSession ( ) )
using ( var transaction = session . BeginTransaction ( ) )
{
try
{
var transactionToUpdate = session . Get < Transaction > ( transactionId ) ;
if ( transactionToUpdate ! = null )
{
2024-06-11 23:54:59 +02:00
var product = session . Get < Product > ( transactionProduct . ProductID ) ;
if ( product . Type ! = 0 )
{
transactionToUpdate . TotalPrice + = ( ( transactionProduct . Quantity * product . Price ) * ( ( 1 - ( transactionToUpdate . Discount / 100 ) ) ) ) ;
}
else
{
transactionToUpdate . TotalPrice + = ( product . Price ) * ( ( 1 - ( transactionToUpdate . Discount / 100 ) ) ) ;
}
2024-05-16 00:42:06 +02:00
transactionProduct . TransactionId = transactionToUpdate . Id ;
2024-05-06 21:25:10 +02:00
session . Save ( transactionProduct ) ;
2024-06-11 23:54:59 +02:00
2024-06-12 14:03:49 +02:00
transactionToUpdate . TotalPrice = Math . Round ( transactionToUpdate . TotalPrice , 2 , MidpointRounding . AwayFromZero ) ;
2024-06-11 23:54:59 +02:00
session . Update ( transactionToUpdate ) ;
//var product = session.Get<Product>(transactionProduct.ProductID);
2024-05-16 00:42:06 +02:00
if ( product . Type ! = 0 )
{
product . Availability - = transactionProduct . Quantity ;
session . Update ( product ) ;
}
2024-06-12 14:03:49 +02:00
transaction . Commit ( ) ;
2024-05-06 21:25:10 +02:00
}
else
{
throw new Exception ( "Transaction not found." ) ;
}
}
catch ( Exception ex )
{
transaction . Rollback ( ) ;
throw ex ;
}
}
}
2024-05-05 22:08:27 +02:00
2024-05-16 00:42:06 +02:00
public IList < Transaction2 > GetAllTransactions ( )
2024-05-05 22:08:27 +02:00
{
using ( var session = SessionFactory . OpenSession ( ) )
{
2024-05-16 00:42:06 +02:00
var transactions = session . Query < Transaction2 > ( )
. FetchMany ( t = > t . TransactionProducts )
. ThenFetch ( tp = > tp . Product )
2024-05-05 22:08:27 +02:00
. ToList ( ) ;
2024-05-06 21:25:10 +02:00
2024-05-05 22:08:27 +02:00
return transactions ;
}
}
2024-05-06 21:25:10 +02:00
}
2024-05-05 22:08:27 +02:00
}