This commit is contained in:
2025-07-10 16:49:39 +01:00
parent ee487db21f
commit 97b945e0cb
6 changed files with 110 additions and 134 deletions

View File

@@ -1,4 +1,5 @@
using bnhtrade.Core.Data.Database.Repository.Interface;
using bnhtrade.Core.Model.Account;
using Microsoft.Data.SqlClient;
using System;
using System.Collections.Generic;
@@ -23,6 +24,44 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
{
}
public int DeletePurchaseLineTransaction(int accountJournalId)
{
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
DELETE FROM tblPurchaseLineTransaction
WHERE AccountJournalID=@accountJournalId;";
cmd.Parameters.AddWithValue("@accountJournalID", accountJournalId);
return cmd.ExecuteNonQuery();
}
}
public void InsertPurchaseLineTransaction(int accountJournalId, int purchaseLineId)
{
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
INSERT INTO
tblPurchaseLineTransaction
( PurchaseLineID, AccountJournalID )
VALUES
( @purchaseLineID, @accountJournalID );";
cmd.Parameters.AddWithValue("@purchaseLineID", purchaseLineId);
cmd.Parameters.AddWithValue("@accountJournalID", accountJournalId);
int count = cmd.ExecuteNonQuery();
if (count != 1)
{
throw new Exception("Failed to insert record to tblPurchaseLineTransaction table");
}
}
}
public void WIP_PurchaseLineTransactionNetUpdate(int accountJouranlId, string currencyCode, decimal amountNet, int debitAccountId)
{

View File

@@ -80,6 +80,41 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
}
}
public int? ReadStockIdByAccountJournalId(int accountJournalId)
{
if (accountJournalId <= 0)
{
throw new ArgumentException("Account Journal ID must be greater than zero", nameof(accountJournalId));
}
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
SELECT StockID
FROM tblStock
WHERE AccountJournalID = @accountJournalId;
";
cmd.Parameters.AddWithValue("@accountJournalId", accountJournalId);
object obj = cmd.ExecuteScalar();
// unique index on tblStock.AccountJournalID ensures that this will return at most one row
if (obj == null)
{
throw new Exception("AccountJournalID=" + accountJournalId + " does not exist.");
}
else if (obj == DBNull.Value)
{
return null; // no stock entry found
}
else
{
return (int)obj;
}
}
}
public int? ReadStockJournalId(int stockId)
{
if (stockId <= 0)

View File

@@ -8,6 +8,8 @@ namespace bnhtrade.Core.Data.Database.Repository.Interface
{
internal interface IPurchaseRepository
{
int DeletePurchaseLineTransaction(int accountJournalId);
void InsertPurchaseLineTransaction(int accountJournalId, int purchaseLineId);
void WIP_PurchaseLineTransactionNetUpdate(int accountJouranlId, string currencyCode, decimal amountNet, int debitAccountId);
}
}

View File

@@ -10,6 +10,7 @@ namespace bnhtrade.Core.Data.Database.Repository.Interface
{
int InsertNewStock(int skuId, int accountJournalId);
int CountStockTableRecords(List<int> accountJournalId = null);
int? ReadStockIdByAccountJournalId(int accountJournalId);
int? ReadAccountJournalId(int stockId);
int? ReadStockJournalId(int stockId);
int UpdateAccountJournalId(int stockId, int? accountJournalID);

View File

@@ -24,7 +24,7 @@ namespace bnhtrade.Core.Logic.Account
}
public int JournalInsert(int journalTypeId, DateTime entryDate, string currencyCode,
decimal amount, int debitAccountId = 0, int creditAccountId = 0, bool lockEntry = false)
decimal amount, int? debitAccountId = null, int? creditAccountId = null, bool lockEntry = false)
{
return WithUnitOfWork(uow =>
{
@@ -45,18 +45,18 @@ namespace bnhtrade.Core.Logic.Account
if (defaultDebit == null)
{
if (debitAccountId == 0)
if (debitAccountId == null)
{
throw new Exception("Debit Account ID required, default not set for journal type");
}
}
else
{
if (debitAccountId == 0)
if (debitAccountId == null)
{
debitAccountId = defaultDebit.Value;
}
else if (debitAccountId != defaultDebit)
else if (debitAccountId.Value != defaultDebit)
{
throw new Exception("Debit Account ID supplied does not match default set for journal type");
}
@@ -65,18 +65,18 @@ namespace bnhtrade.Core.Logic.Account
if (defaultCredit == null)
{
if (creditAccountId == 0)
if (creditAccountId == null)
{
throw new Exception("Credit Account ID required, default not set for journal type");
}
}
else
{
if (creditAccountId == 0)
if (creditAccountId == null)
{
creditAccountId = defaultCredit.Value;
}
else if (creditAccountId != defaultCredit)
else if (creditAccountId.Value != defaultCredit)
{
throw new Exception("Credit Account ID supplied does not match default set for journal type");
}
@@ -92,8 +92,8 @@ namespace bnhtrade.Core.Logic.Account
amount = Math.Round(amount, 2);
// insert posts
int debitPostId = uow.AccountJournalRepository.InsertJournalPost(journalId, debitAccountId, amount);
int creditPostId = uow.AccountJournalRepository.InsertJournalPost(journalId, creditAccountId, amount * -1);
int debitPostId = uow.AccountJournalRepository.InsertJournalPost(journalId, debitAccountId.Value, amount);
int creditPostId = uow.AccountJournalRepository.InsertJournalPost(journalId, creditAccountId.Value, amount * -1);
// need to add verification here to ensure the entry is correct

View File

@@ -23,39 +23,20 @@ namespace bnhtrade.Core.Logic.Purchase
internal PurchaseService(IUnitOfWork unitOfWork) : base(unitOfWork) { }
public void WIP_PurchaseLineTransactionNetInsert(int purchaseLineId, string currencyCode, decimal amountNet, DateTime entryDate, int debitAccountId = 0)
public void WIP_PurchaseLineTransactionNetInsert(int purchaseLineId, string currencyCode, decimal amountNet, DateTime entryDate, int? debitAccountId = null)
{
// default to 'Inventory, Receivable/Processing'
if (debitAccountId < 1)
if (debitAccountId == null)
{
debitAccountId = defaultAccountId;
}
// create account journal entry
int journalId = new Data.Database.Repository.Implementation.AccountJournalRepository(_connection, _transaction).
AccountJournalInsert(accountJournalTypeIdNet, entryDate, currencyCode, amountNet, debitAccountId);
// add transaction to purchase line transaction table
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
WithUnitOfWork(uow =>
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
INSERT INTO
tblPurchaseLineTransaction
( PurchaseLineID, AccountJournalID )
VALUES
( @purchaseLineID, @accountJournalID );";
cmd.Parameters.AddWithValue("@purchaseLineID", purchaseLineId);
cmd.Parameters.AddWithValue("@accountJournalID", journalId);
int count = cmd.ExecuteNonQuery();
if (count != 1)
{
throw new Exception("Failed to insert record to tblPurchaseLineTransaction table");
}
}
int journalId = new AccountJournalService(uow).JournalInsert(accountJournalTypeIdNet, entryDate, currencyCode, amountNet, debitAccountId.Value);
uow.PurchaseRepository.InsertPurchaseLineTransaction(journalId, purchaseLineId);
CommitIfOwned(uow);
});
}
public void WIP_PurchaseLineTransactionNetUpdate(int accountJouranlId, string currencyCode, decimal amountNet, int debitAccountId)
@@ -65,126 +46,44 @@ namespace bnhtrade.Core.Logic.Purchase
// stock accountId check
if (debitAccountId == 86)
{
int count = 0;
int? stockId = uow.StockRepository.ReadStockIdByAccountJournalId(accountJouranlId);
using (SqlCommand cmd = new SqlCommand(@"
SELECT Count(tblStock.StockID) AS CountOfStockID
FROM tblStock
WHERE (((tblStock.AccountJournalID)=@accountJouranlId));
", conn))
{
cmd.Parameters.AddWithValue("@accountJouranlId", accountJouranlId);
count = (int)cmd.ExecuteScalar();
}
if (count == 0)
if (stockId.HasValue == false)
{
throw new Exception("Add account journal entry to stock before attempting this operation.");
}
else if (count > 1)
{
throw new Exception("Houston we have a problem! An account journal entry is assigned to " + count + " stock lines.");
}
}
uow.PurchaseRepository.WIP_PurchaseLineTransactionNetUpdate(accountJouranlId, currencyCode, amountNet, debitAccountId);
new AccountJournalService(uow).AccountJournalPostReplace(accountJouranlId, currencyCode, amountNet, creditAccountId);
// make the update
new AccountJournalService(uow).AccountJournalPostReplace(accountJouranlId, currencyCode, amountNet, debitAccountId, creditAccountId);
CommitIfOwned(uow);
});
// stock accountId check
if (debitAccountId == 86)
{
int count = 0;
using (SqlCommand cmd = new SqlCommand(@"
SELECT Count(tblStock.StockID) AS CountOfStockID
FROM tblStock
WHERE (((tblStock.AccountJournalID)=@accountJouranlId));
", conn))
{
cmd.Parameters.AddWithValue("@accountJouranlId", accountJouranlId);
count = (int)cmd.ExecuteScalar();
}
if (count == 0)
{
throw new Exception("Add account journal entry to stock before attempting this operation.");
}
else if (count > 1)
{
throw new Exception("Houston we have a problem! An account journal entry is assigned to " + count + " stock lines.");
}
}
// make the update
bool result = new Data.Database.Account.UpdateJournal().AccountJournalPostUpdate(accountJouranlId, currencyCode, amountNet, debitAccountId, creditAccountId);
}
public void WIP_PurchaseLineTransactionDelete(int purchaseLineId, int accountJournalId)
{
// check accountJournalId does not exist in stock table
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
WithUnitOfWork(uow =>
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
SELECT StockID
FROM tblStock
WHERE AccountJournalID=@accountJournalId;";
cmd.Parameters.AddWithValue("@accountJournalId", accountJournalId);
using (var reader = cmd.ExecuteReader())
// check accountJournalId does not exist in stock table
int? stockId = uow.StockRepository.ReadStockIdByAccountJournalId(accountJournalId);
if (stockId.HasValue)
{
if (reader.Read())
{
if (reader.Read())
{
throw new Exception("Integrity check failure! AccountJournalID=" + accountJournalId + " exists multiple time in stock table!");
}
else
{
throw new Exception("Delete stock first before proceeding, AccountJournalID=" + accountJournalId);
}
}
throw new Exception("Delete stock first before proceeding, AccountJournalID=" + accountJournalId);
}
}
// delete line in purchase line transaction table
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
DELETE FROM tblPurchaseLineTransaction
WHERE AccountJournalID=@accountJournalId;";
// delete line in purchase line transaction table
int deleted = uow.PurchaseRepository.DeletePurchaseLineTransaction(accountJournalId);
cmd.Parameters.AddWithValue("@accountJournalId", accountJournalId);
int count = cmd.ExecuteNonQuery();
if (count != 1)
if (deleted != 1)
{
throw new Exception("Operation cancelled, failed to delete entry in tblPurchaseLineTransaction WHERE AccountJournalID=" + accountJournalId);
}
}
// delete account journal entry
new AccountJournalService(uow).DeleteJournal(accountJournalId);
// delete account journal entry
new AccountJournalService(uow).DeleteJournal(accountJournalId);
CommitIfOwned(uow);
});
}
}
}