This commit is contained in:
2025-07-10 14:33:13 +01:00
parent d4170d2b80
commit ee487db21f
11 changed files with 648 additions and 620 deletions

View File

@@ -1,6 +1,7 @@
using bnhtrade.Core.Data.Database;
using bnhtrade.Core.Data.Database.Repository.Implementation;
using bnhtrade.Core.Data.Database.UnitOfWork;
using FikaAmazonAPI.AmazonSpApiSDK.Models.FulfillmentInbound;
using System;
using System.Collections.Generic;
using System.Data.Common;
@@ -28,153 +29,75 @@ namespace bnhtrade.Core.Logic.Account
return WithUnitOfWork(uow =>
{
// insert header record
int journalId = uow.AccountJournalRepository.AccountJournalInsert(journalTypeId, entryDate, lockEntry);
int journalId = uow.AccountJournalRepository.InsertJournalHeader(journalTypeId, entryDate, lockEntry);
// insert post record
int defaultDebit;
int defaultCredit;
// ensure their are no other entries (not sure why this is needed, but it was in the old code)
int count = uow.AccountJournalRepository.CountJournalPosts(journalId);
if (count > 0)
{
throw new Exception("Unable the insert journal posts, post already present AccountJournalID=" + journalId);
}
// ensure their are no other entries
using (SqlCommand cmd = new SqlCommand(@"
SELECT
Count(tblAccountJournalPost.AccountJournalPostID) AS CountOfAccountJournalPostID
FROM
tblAccountJournalPost
WHERE
(((tblAccountJournalPost.AccountJournalID)=@AccountJournalID));
", conn))
// check defaults for debit and credit accounts
var result = uow.AccountJournalRepository.ReadJournalTypeDefaultDebitCredit(journalId);
int? defaultDebit = result.Item1;
int? defaultCredit = result.Item2;
if (defaultDebit == null)
{
if (debitAccountId == 0)
{
cmd.Parameters.AddWithValue("@AccountJournalID", journalId);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
{
throw new Exception("Unable the insert journal posts, post already present AccountJournalID=" + journalId);
}
throw new Exception("Debit Account ID required, default not set for journal type");
}
}
else
{
if (debitAccountId == 0)
{
debitAccountId = defaultDebit.Value;
}
else if (debitAccountId != defaultDebit)
{
throw new Exception("Debit Account ID supplied does not match default set for journal type");
}
//checks
using (SqlCommand cmd = new SqlCommand(@"
SELECT
tblAccountJournalType.ChartOfAccountID_Debit, tblAccountJournalType.ChartOfAccountID_Credit
FROM
tblAccountJournal
INNER JOIN tblAccountJournalType
ON tblAccountJournal.AccountJournalTypeID = tblAccountJournalType.AccountJournalTypeID
WHERE
(((tblAccountJournal.AccountJournalID)=@journalId));
", conn))
}
if (defaultCredit == null)
{
if (creditAccountId == 0)
{
cmd.Parameters.AddWithValue("@journalId", journalId);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
// debit check
if (reader.IsDBNull(0))
{
if (debitAccountId == 0)
{
throw new Exception("Debit Account ID required, default not set for journal type");
}
}
else
{
defaultDebit = reader.GetInt32(0);
if (debitAccountId == 0)
{
debitAccountId = defaultDebit;
}
else if (debitAccountId != defaultDebit)
{
throw new Exception("Debit Account ID supplied does not match default set for journal type");
}
}
// credit check
if (reader.IsDBNull(1))
{
if (creditAccountId == 0)
{
throw new Exception("Credit Account ID required, default not set for journal type");
}
}
else
{
defaultCredit = reader.GetInt32(1);
if (creditAccountId == 0)
{
creditAccountId = defaultCredit;
}
else if (creditAccountId != defaultCredit)
{
throw new Exception("Credit Account ID supplied does not match default set for journal type");
}
}
}
else
{
throw new Exception("AccountJournalID '" + journalId + "' does not exist.");
}
}
throw new Exception("Credit Account ID required, default not set for journal type");
}
// currency conversion
if (currencyCode != "GBP")
}
else
{
if (creditAccountId == 0)
{
amount = new Logic.Account.CurrencyService().CurrencyConvertToGbp(currencyCode, amount, entryDate);
creditAccountId = defaultCredit.Value;
}
// ensure decimal is rounded
amount = Math.Round(amount, 2);
// insert debit post
using (SqlCommand cmd = new SqlCommand(@"
INSERT INTO tblAccountJournalPost
(AccountJournalID, AccountChartOfID, AmountGbp)
VALUES
(@AccountJournalId, @AccountChartOfId, @AmountGbp)
", conn))
else if (creditAccountId != defaultCredit)
{
// add parameters
cmd.Parameters.AddWithValue("@AccountJournalId", journalId);
cmd.Parameters.AddWithValue("@AccountChartOfId", debitAccountId);
cmd.Parameters.AddWithValue("@AmountGbp", amount);
cmd.ExecuteNonQuery();
throw new Exception("Credit Account ID supplied does not match default set for journal type");
}
}
// insert credit post
using (SqlCommand cmd = new SqlCommand(@"
INSERT INTO tblAccountJournalPost
(AccountJournalID, AccountChartOfID, AmountGbp)
VALUES
(@AccountJournalId, @AccountChartOfId, @AmountGbp)
", conn))
{
// add parameters
cmd.Parameters.AddWithValue("@AccountJournalId", journalId);
cmd.Parameters.AddWithValue("@AccountChartOfId", creditAccountId);
cmd.Parameters.AddWithValue("@AmountGbp", (amount * -1));
cmd.ExecuteNonQuery();
}
return true;
// currency conversion
if (currencyCode != "GBP")
{
amount = new Logic.Account.CurrencyService(uow).CurrencyConvertToGbp(currencyCode, amount, entryDate);
}
// ensure decimal is rounded
amount = Math.Round(amount, 2);
// insert posts
int debitPostId = uow.AccountJournalRepository.InsertJournalPost(journalId, debitAccountId, amount);
int creditPostId = uow.AccountJournalRepository.InsertJournalPost(journalId, creditAccountId, amount * -1);
// need to add verification here to ensure the entry is correct
// finished
CommitIfOwned(uow);
return journalId;
});
@@ -182,406 +105,160 @@ namespace bnhtrade.Core.Logic.Account
public Dictionary<int, Core.Model.Account.Journal> ReadJournal(List<int> journalIdList)
{
var sqlBuilder = new SqlWhereBuilder();
throw new NotImplementedException("done, but needs testing");
//build sql query
string sql = @"
SELECT tblAccountJournal.AccountJournalID
,tblAccountJournal.AccountJournalTypeID
,tblAccountJournal.EntryDate
,tblAccountJournal.PostDate
,tblAccountJournal.LastModified
,tblAccountJournal.IsLocked
,tblAccountJournalPost.AccountJournalPostID
,tblAccountJournalPost.AccountChartOfID
,tblAccountJournalPost.AmountGbp
FROM tblAccountJournal
INNER JOIN tblAccountJournalPost ON tblAccountJournal.AccountJournalID = tblAccountJournalPost.AccountJournalID
WHERE 1 = 1 ";
var returnDict = new Dictionary<int, Core.Model.Account.Journal>();
// build the where statments
if (journalIdList.Any())
WithUnitOfWork(uow =>
{
sqlBuilder.In("tblAccountJournal.AccountJournalID", journalIdList, "AND");
}
// append where string to the sql
if (sqlBuilder.IsSetSqlWhereString)
{
sql = sql + sqlBuilder.SqlWhereString;
}
// build tuple list
var dbJournalList = new List<(
int AccountJournalId
, int AccountJournalTypeId
, DateTime EntryDate
, DateTime PostDate
, DateTime LastModified
, bool IsLocked
)>();
var dbJournalPostList = new List<(
int AccountJournalId
, int AccountJournalPostId
, int AccountChartOfId
, decimal AmountGbp
)>();
bool hasRows = false;
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.CommandText = sql;
cmd.Transaction = _transaction as SqlTransaction;
if (sqlBuilder.ParameterListIsSet)
var builderDict = uow.AccountJournalRepository.ReadJournalBuilder(journalIdList);
if (builderDict.Any())
{
sqlBuilder.AddParametersToSqlCommand(cmd);
}
// build list of journal types and accounts from the builderDict
var journalTypeIdList = new List<int>();
var accountIdList = new List<int>();
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
foreach (var journal in builderDict.Values)
{
hasRows = true;
int lastJournalId = 0;
while (reader.Read())
journalTypeIdList.Add(journal.JournalTypeId);
foreach (var post in journal.JournalBuilderPosts)
{
// read journal header
int journalId = reader.GetInt32(0);
if (journalId != lastJournalId)
{
lastJournalId = journalId;
(int AccountJournalId
, int AccountJournalTypeId
, DateTime EntryDate
, DateTime PostDate
, DateTime LastModified
, bool IsLocked
)
journal =
(journalId
, reader.GetInt32(1)
, DateTime.SpecifyKind(reader.GetDateTime(2), DateTimeKind.Utc)
, DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc)
, DateTime.SpecifyKind(reader.GetDateTime(4), DateTimeKind.Utc)
, reader.GetBoolean(5)
);
dbJournalList.Add(journal);
}
// read journal posts
(int AccountJournalId
, int AccountJournalPostId
, int AccountChartOfId
, decimal AmountGbp
)
journalPost =
(journalId
, reader.GetInt32(6)
, reader.GetInt32(7)
, reader.GetDecimal(8)
);
dbJournalPostList.Add(journalPost);
}
}
}
}
var returnList = new Dictionary<int, Core.Model.Account.Journal>();
if (hasRows)
{
// build lists to filter db results by
var journalTypeIdList = new List<int>();
var accountIdList = new List<int>();
foreach (var item in dbJournalList)
{
journalTypeIdList.Add(item.AccountJournalTypeId);
}
foreach (var item in dbJournalPostList)
{
accountIdList.Add(item.AccountChartOfId);
}
// get journalTypes from db
var journalTypeDict = new AccountJournalRepository(_connection, _transaction).ReadJournalType(journalTypeIdList);
// get accounts from db
var accountDict = new AccountCodeRepository(_connection, _transaction).ReadAccountCode(accountIdList);
// build final return dictionary
foreach (var dbJournal in dbJournalList)
{
// build posts
var newPosts = new List<Core.Model.Account.Journal.Post>();
foreach (var dbJournalPost in dbJournalPostList)
{
if (dbJournalPost.AccountJournalId == dbJournal.AccountJournalId)
{
var newPost = new Core.Model.Account.Journal.Post(
dbJournalPost.AccountJournalPostId
, accountDict[dbJournalPost.AccountChartOfId]
, dbJournalPost.AmountGbp);
newPosts.Add(newPost);
accountIdList.Add(post.AccountId);
}
}
// create the journal
var newJournal = new Core.Model.Account.Journal(
dbJournal.AccountJournalId
, journalTypeDict[dbJournal.AccountJournalTypeId]
, newPosts
, dbJournal.EntryDate
, dbJournal.PostDate
, dbJournal.LastModified
, dbJournal.IsLocked);
journalTypeIdList = journalTypeIdList.Distinct().ToList();
accountIdList = accountIdList.Distinct().ToList();
returnList.Add(dbJournal.AccountJournalId, newJournal);
// get object dictionaries
var journalTypeDict = ReadJournalType(journalTypeIdList);
var accountDict = uow.AccountCodeRepository.ReadAccountCode(accountIdList);
// build final return dictionary
foreach (var item in builderDict)
{
var journal = item.Value.Build(journalTypeDict, accountDict);
returnDict.Add(journal.JournalId, journal);
}
}
}
// all done, return the list herevar
return returnList;
});
return returnDict;
}
public DateTime ReadJournalEntryDate(int journalId)
{
if (journalId <= 0)
return WithUnitOfWork(uow =>
{
throw new ArgumentException("Invalid journal ID provided.", nameof(journalId));
}
return uow.AccountJournalRepository.ReadJournalEntryDate(journalId);
});
}
string sql = @"
SELECT tblAccountJournal.EntryDate
FROM tblAccountJournal
WHERE (((tblAccountJournal.AccountJournalID)=@accountJournalId));";
public Dictionary<int, Model.Account.JournalType> ReadJournalType(List<int> journalTypeIdList)
{
var returnDict = new Dictionary<int, Model.Account.JournalType>();
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
WithUnitOfWork(uow =>
{
cmd.CommandText = sql;
cmd.Transaction = _transaction as SqlTransaction;
// get base info from db
var dbJournalTypeList = uow.AccountJournalRepository.ReadJournalType(journalTypeIdList);
cmd.Parameters.AddWithValue("@accountJournalId", journalId);
object obj = cmd.ExecuteScalar();
if (obj == null)
// build list of account object to retrieve
var accountIdList = new List<int>();
foreach (var dbJournalType in dbJournalTypeList)
{
throw new Exception("Journal entry not found for AccountJournalID=" + journalId);
if (dbJournalType.Item3.HasValue)
{
accountIdList.Add(dbJournalType.Item3.Value);
}
if (dbJournalType.Item4.HasValue)
{
accountIdList.Add(dbJournalType.Item4.Value);
}
}
accountIdList = accountIdList.Distinct().ToList();
return DateTime.SpecifyKind((DateTime)obj, DateTimeKind.Utc);
}
// retieve account objects from db
var accountDict = uow.AccountCodeRepository.ReadAccountCode(accountIdList);
// build the return dictionary
foreach (var dbJournalType in dbJournalTypeList)
{
var journalType = new Model.Account.JournalType(
dbJournalType.Item1, // JournalTypeId
dbJournalType.Item2, // Name
dbJournalType.Item3.HasValue ? accountDict[dbJournalType.Item3.Value] : null, // DebitAccount
dbJournalType.Item4.HasValue ? accountDict[dbJournalType.Item4.Value] : null // CreditAccount
);
returnDict.Add(journalType.JournalTypeId, journalType);
}
});
return returnDict;
}
/// <summary>
/// Old code needs sorting
/// Deletes all posts associated with an entry and replaces the posts for a journal entry with two new posts
/// </summary>
public bool AccountJournalPostInsert(IUnitOfWork uow, int journalId, DateTime entryDate, string currencyCode, decimal amount, int debitAccountId = 0, int creditAccountId = 0)
/// <param name="twoPostCheck">Raise exception if journal entry has more than two associated posts</param>
internal bool AccountJournalPostReplace(int accountJournalId, string currencyCode, decimal amountGbp, int debitAccountId = 0, int creditAccountId = 0, bool twoPostCheck = true)
{
int defaultDebit;
int defaultCredit;
entryDate = DateTime.SpecifyKind(entryDate, DateTimeKind.Utc);
using (TransactionScope scope = new TransactionScope())
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
if (amountGbp <= 0)
{
conn.Open();
// ensure their are no other entries
using (SqlCommand cmd = new SqlCommand(@"
SELECT
Count(tblAccountJournalPost.AccountJournalPostID) AS CountOfAccountJournalPostID
FROM
tblAccountJournalPost
WHERE
(((tblAccountJournalPost.AccountJournalID)=@AccountJournalID));
", conn))
{
cmd.Parameters.AddWithValue("@AccountJournalID", journalId);
int count = (int)cmd.ExecuteScalar();
if (count > 0)
{
throw new Exception("Unable the insert journal posts, post already present AccountJournalID=" + journalId);
}
}
//checks
using (SqlCommand cmd = new SqlCommand(@"
SELECT
tblAccountJournalType.ChartOfAccountID_Debit, tblAccountJournalType.ChartOfAccountID_Credit
FROM
tblAccountJournal
INNER JOIN tblAccountJournalType
ON tblAccountJournal.AccountJournalTypeID = tblAccountJournalType.AccountJournalTypeID
WHERE
(((tblAccountJournal.AccountJournalID)=@journalId));
", conn))
{
cmd.Parameters.AddWithValue("@journalId", journalId);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.Read())
{
// debit check
if (reader.IsDBNull(0))
{
if (debitAccountId == 0)
{
throw new Exception("Debit Account ID required, default not set for journal type");
}
}
else
{
defaultDebit = reader.GetInt32(0);
if (debitAccountId == 0)
{
debitAccountId = defaultDebit;
}
else if (debitAccountId != defaultDebit)
{
throw new Exception("Debit Account ID supplied does not match default set for journal type");
}
}
// credit check
if (reader.IsDBNull(1))
{
if (creditAccountId == 0)
{
throw new Exception("Credit Account ID required, default not set for journal type");
}
}
else
{
defaultCredit = reader.GetInt32(1);
if (creditAccountId == 0)
{
creditAccountId = defaultCredit;
}
else if (creditAccountId != defaultCredit)
{
throw new Exception("Credit Account ID supplied does not match default set for journal type");
}
}
}
else
{
throw new Exception("AccountJournalID '" + journalId + "' does not exist.");
}
}
}
// currency conversion
if (currencyCode != "GBP")
{
amount = new Logic.Account.CurrencyService().CurrencyConvertToGbp(currencyCode, amount, entryDate);
}
// ensure decimal is rounded
amount = Math.Round(amount, 2);
// insert debit post
using (SqlCommand cmd = new SqlCommand(@"
INSERT INTO tblAccountJournalPost
(AccountJournalID, AccountChartOfID, AmountGbp)
VALUES
(@AccountJournalId, @AccountChartOfId, @AmountGbp)
", conn))
{
// add parameters
cmd.Parameters.AddWithValue("@AccountJournalId", journalId);
cmd.Parameters.AddWithValue("@AccountChartOfId", debitAccountId);
cmd.Parameters.AddWithValue("@AmountGbp", amount);
cmd.ExecuteNonQuery();
}
// insert credit post
using (SqlCommand cmd = new SqlCommand(@"
INSERT INTO tblAccountJournalPost
(AccountJournalID, AccountChartOfID, AmountGbp)
VALUES
(@AccountJournalId, @AccountChartOfId, @AmountGbp)
", conn))
{
// add parameters
cmd.Parameters.AddWithValue("@AccountJournalId", journalId);
cmd.Parameters.AddWithValue("@AccountChartOfId", creditAccountId);
cmd.Parameters.AddWithValue("@AmountGbp", (amount * -1));
cmd.ExecuteNonQuery();
}
scope.Complete();
return true;
throw new ArgumentException("Amount must be greater than zero", nameof(amountGbp));
}
if ( amountGbp.Scale > 2)
{
throw new ArgumentException("Amount must have a maximum of two decimal places", nameof(amountGbp));
}
if (debitAccountId <= 0 || creditAccountId <= 0)
{
throw new ArgumentException("Debit and Credit Account IDs must be greater than zero", nameof(debitAccountId));
}
}
public bool AccountJournalPostUpdate(int journalId, string currencyCode, decimal amount, int debitAccountId = 0, int creditAccountId = 0)
{
// retrive journal entry date
DateTime entryDate;
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
WithUnitOfWork(uow =>
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
SELECT
tblAccountJournal.EntryDate
FROM
tblAccountJournal
WHERE
(((tblAccountJournal.AccountJournalID)=@accountJournalId));";
// check if the journal entry is locked
bool? isLocked = uow.AccountJournalRepository.IsJournalLocked(accountJournalId);
if (isLocked == null)
{
throw new Exception("Journal entry does not exist for AccountJournalID=" + accountJournalId);
}
else if (isLocked.Value)
{
throw new Exception("Cannot replace posts for locked journal entry AccountJournalID=" + accountJournalId);
}
cmd.Parameters.AddWithValue("@accountJournalId", journalId);
// retrive journal entry date
DateTime entryDate = uow.AccountJournalRepository.ReadJournalEntryDate(accountJournalId);
entryDate = DateTime.SpecifyKind((DateTime)cmd.ExecuteScalar(), DateTimeKind.Utc);
}
// delete the original posts
var rowsDeleted = uow.AccountJournalRepository.DeleteJournalPostAll(accountJournalId);
if (rowsDeleted == 0)
{
throw new Exception("No posts found for AccountJournalID=" + accountJournalId);
}
if (twoPostCheck && rowsDeleted > 2)
{
throw new Exception("More than two posts found for AccountJournalID=" + accountJournalId + ", cannot replace posts with two new posts.");
}
// delete the original posts
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
DELETE FROM
tblAccountJournalPost
WHERE
(((tblAccountJournalPost.AccountJournalID)=@accountJournalId));";
//insert new posts
var rowsInserted = uow.AccountJournalRepository.InsertJournalPost(accountJournalId, debitAccountId, amountGbp);
rowsInserted += uow.AccountJournalRepository.InsertJournalPost(accountJournalId, creditAccountId, amountGbp * -1);
if (rowsInserted != 2)
{
throw new Exception("Failed to insert two posts for AccountJournalID=" + accountJournalId);
}
cmd.Parameters.AddWithValue("@accountJournalId", journalId);
// update modified date on journal
if (uow.AccountJournalRepository.UpdateJournalEntryModifiedDate(accountJournalId) == false)
{
throw new Exception("Failed to update LastModified date for AccountJournalID=" + accountJournalId);
}
cmd.ExecuteNonQuery();
}
//insert new posts
bool postResult = AccountJournalPostInsert(journalId, entryDate, currencyCode, amount, debitAccountId, creditAccountId);
// update modified date on journal
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
UPDATE
tblAccountJournal
SET
tblAccountJournal.LastModified=@utcNow
WHERE
(((tblAccountJournal.AccountJournalID)=@accountJournalId));";
cmd.Parameters.AddWithValue("@accountJournalId", journalId);
cmd.Parameters.AddWithValue("@utcNow", DateTime.UtcNow);
cmd.ExecuteNonQuery();
}
CommitIfOwned(uow);
});
return true;
}
@@ -591,42 +268,32 @@ namespace bnhtrade.Core.Logic.Account
/// </summary>
public bool DeleteJournal(int accountJournalId)
{
bool IsLocked = ReadJournalIsLocked(accountJournalId);
if (IsLocked == true)
WithUnitOfWork(uow =>
{
return false;
}
// make the delete
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.CommandText = @"
DELETE FROM tblAccountJournalPost
WHERE AccountJournalID=@accountJournalId;";
cmd.Transaction = _transaction as SqlTransaction;
cmd.Parameters.AddWithValue("@accountJournalId", accountJournalId);
int rows = cmd.ExecuteNonQuery();
if (rows == 0)
if (accountJournalId <= 0)
{
throw new Exception("Journal entry and/or entry posts do not exist for AccountJournalId=" + accountJournalId);
throw new ArgumentException("Account journal ID must be greater than zero", nameof(accountJournalId));
}
// check if the journal entry is locked
bool? isLocked = uow.AccountJournalRepository.IsJournalLocked(accountJournalId);
if (isLocked == null)
{
throw new Exception("Journal entry does not exist for AccountJournalID=" + accountJournalId);
}
else if (isLocked.Value)
{
throw new Exception("Cannot replace posts for locked journal entry AccountJournalID=" + accountJournalId);
}
}
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.CommandText = @"
DELETE FROM tblAccountJournal
WHERE AccountJournalID=@accountJournalId;";
cmd.Transaction = _transaction as SqlTransaction;
// delete posts first
uow.AccountJournalRepository.DeleteJournalPostAll(accountJournalId);
cmd.Parameters.AddWithValue("@accountJournalId", accountJournalId);
// then delete header record
uow.AccountJournalRepository.DeleteJournalHeader(accountJournalId);
cmd.ExecuteNonQuery();
}
CommitIfOwned(uow);
});
return true;
}
}

View File

@@ -194,7 +194,7 @@ namespace bnhtrade.Core.Logic.Inventory
uow.StockJournalRepository.StockJournalDelete(stockJournalId);
// delete stock table entry
count = uow.StockRepository.DeleteStockTableLine(stockId);
count = uow.StockRepository.DeleteStock(stockId);
if (count != 1)
{
throw new Exception("StockID = " + stockId + " delete failed");

View File

@@ -62,10 +62,78 @@ namespace bnhtrade.Core.Logic.Purchase
{
WithUnitOfWork(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.");
}
}
uow.PurchaseRepository.WIP_PurchaseLineTransactionNetUpdate(accountJouranlId, currencyCode, amountNet, debitAccountId);
new AccountJournalService(uow).AccountJournalPostUpdate(accountJouranlId, currencyCode, amountNet, creditAccountId);
new AccountJournalService(uow).AccountJournalPostReplace(accountJouranlId, currencyCode, amountNet, 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)