mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 06:27:15 +00:00
wip
This commit is contained in:
@@ -21,6 +21,24 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
||||
{
|
||||
}
|
||||
|
||||
private List<string> ConvertStockNumbers(List<int> stockNumbers)
|
||||
{
|
||||
if (stockNumbers == null || stockNumbers.Count == 0)
|
||||
{
|
||||
return new List<string>();
|
||||
}
|
||||
return stockNumbers.Select(sn => "STK#" + sn.ToString("D6")).ToList();
|
||||
}
|
||||
|
||||
private List<int> ConvertStockNumbers(List<string> stockNumbers)
|
||||
{
|
||||
if (stockNumbers == null || stockNumbers.Count == 0)
|
||||
{
|
||||
return new List<int>();
|
||||
}
|
||||
return stockNumbers.Select(sn => int.Parse(sn.Substring(4))).ToList();
|
||||
}
|
||||
|
||||
//
|
||||
// Create
|
||||
//
|
||||
@@ -77,14 +95,20 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
||||
// Read
|
||||
//
|
||||
|
||||
public Dictionary<int, Model.Stock.StockJournalBuilder> ReadStockJournal(
|
||||
public Dictionary<int, Model.Stock.StockJournalBuilder> ReadStockJournal(bool lockRecords = false,
|
||||
List<int> stockJournalIds = null, List<int> stockIds = null, List<string> stockNumbers = null
|
||||
, DateTime? minEntryDate = null, DateTime? maxEntryDate = null, List<int> stockStatusIds = null)
|
||||
{
|
||||
var returnDict = new Dictionary<int, Model.Stock.StockJournalBuilder>();
|
||||
var sqlWhere = new SqlWhereBuilder();
|
||||
|
||||
string sql = @"
|
||||
string lockClause = "";
|
||||
if (lockRecords)
|
||||
{
|
||||
lockClause = " WITH (UPDLOCK, HOLDLOCK)";
|
||||
}
|
||||
|
||||
string sql = $@"
|
||||
SELECT tblStockJournal.StockJournalID,
|
||||
tblStockJournal.StockJournalTypeID,
|
||||
tblStockJournal.StockID,
|
||||
@@ -97,9 +121,9 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
||||
tblStockJournalPost.StockJournalPostID,
|
||||
tblStockJournalPost.StockStatusID,
|
||||
tblStockJournalPost.Quantity,
|
||||
FROM tblStockJournal
|
||||
LEFT OUTER JOIN tblStock ON tblStockJournal.StockID = tblStock.StockID
|
||||
LEFT OUTER JOIN tblStockJournalPost ON tblStockJournal.StockJournalID = tblStockJournalPost.StockJournalID
|
||||
FROM tblStockJournal{lockClause}
|
||||
LEFT OUTER JOIN tblStock{lockClause} ON tblStockJournal.StockID = tblStock.StockID
|
||||
LEFT OUTER JOIN tblStockJournalPost{lockClause} ON tblStockJournal.StockJournalID = tblStockJournalPost.StockJournalID
|
||||
WHERE 1=1 ";
|
||||
|
||||
// build where clause based on provided filters
|
||||
@@ -116,7 +140,8 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
||||
}
|
||||
if (stockNumbers != null && stockNumbers.Any())
|
||||
{
|
||||
sql += sqlWhere.InClause("tblStock.StockNumber", stockNumbers, "AND");
|
||||
List<int> stockNumbersInt = ConvertStockNumbers(stockNumbers);
|
||||
sql += sqlWhere.InClause("tblStock.StockNumber", stockNumbersInt, "AND");
|
||||
noFilter = false;
|
||||
}
|
||||
if (minEntryDate.HasValue)
|
||||
@@ -192,60 +217,6 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
||||
}
|
||||
}
|
||||
|
||||
public List<int> ReadStockJournalIdByStatus(int stockId, List<int> stockStatusIds = null)
|
||||
{
|
||||
if (stockId <= 0)
|
||||
{
|
||||
throw new ArgumentException("Stock ID must be greater than zero.", nameof(stockId));
|
||||
}
|
||||
if (stockStatusIds != null && stockStatusIds.Any() && stockStatusIds.Any(id => id <= 0))
|
||||
{
|
||||
throw new ArgumentException("Stock status IDs must be greater than zero.", nameof(stockStatusIds));
|
||||
}
|
||||
|
||||
var returnDict = new Dictionary<int, Model.Stock.StockJournalBuilder>();
|
||||
var sqlWhere = new SqlWhereBuilder();
|
||||
|
||||
string sql = @"
|
||||
SELECT tblStockJournal.StockJournalID,
|
||||
FROM tblStockJournal
|
||||
LEFT OUTER JOIN
|
||||
tblStockJournalPost
|
||||
ON tblStockJournal.StockJournalID = tblStockJournalPost.StockJournalID
|
||||
WHERE 1=1 ";
|
||||
|
||||
sqlWhere.IsEqualTo("tblStockJournal.StockID", stockId, "AND");
|
||||
|
||||
if (stockStatusIds != null && stockStatusIds.Any())
|
||||
{
|
||||
sqlWhere.In("StockStatusID", stockStatusIds, "AND");
|
||||
}
|
||||
|
||||
sql += sqlWhere.SqlWhereString + ";";
|
||||
|
||||
var idList = new List<int>();
|
||||
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
|
||||
{
|
||||
cmd.Transaction = _transaction as SqlTransaction;
|
||||
cmd.CommandText = sql;
|
||||
|
||||
if (sqlWhere.ParameterListIsSet)
|
||||
{
|
||||
sqlWhere.AddParametersToSqlCommand(cmd);
|
||||
}
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
idList.Add(reader.GetInt32(0));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return idList;
|
||||
}
|
||||
|
||||
public Dictionary<int, (int id, string title, int? stockStatusIdDebit, int? stockStatusIdCredit)> ReadStockJournalType(List<int> stockJournalTypeIdList)
|
||||
{
|
||||
var returnDict = new Dictionary<int, (int id, string title, int? stockStatusIdDebit, int? stockStatusIdCredit)>();
|
||||
@@ -258,10 +229,10 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
||||
|
||||
if (stockJournalTypeIdList != null && stockJournalTypeIdList.Any())
|
||||
{
|
||||
sqlWhere.In("StockJournalTypeID", stockJournalTypeIdList, "AND");
|
||||
sql += sqlWhere.InClause("StockJournalTypeID", stockJournalTypeIdList, "AND");
|
||||
}
|
||||
|
||||
sql += sqlWhere.SqlWhereString + ";";
|
||||
sql += ";";
|
||||
|
||||
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
|
||||
{
|
||||
@@ -507,9 +478,7 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
||||
tblStockJournal.StockID=@stockId
|
||||
AND tblStockJournalPost.Quantity>0 ";
|
||||
|
||||
sqlWhere.In("tblStockJournalPost.StockStatusID", stockStatusIdList, "AND");
|
||||
|
||||
stringSql = stringSql + sqlWhere.SqlWhereString;
|
||||
stringSql += sqlWhere.InClause("tblStockJournalPost.StockStatusID", stockStatusIdList, "AND") + ";";
|
||||
|
||||
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
|
||||
{
|
||||
|
||||
@@ -10,8 +10,9 @@ namespace bnhtrade.Core.Data.Database.Repository.Interface
|
||||
{
|
||||
int InsertStockJournalHeader(int stockId, int journalTypeId, DateTime entryDate, bool isLocked);
|
||||
int InsertStockJournalPost(int stockJournalId, int stockStatusId, int quantity);
|
||||
Dictionary<int, Model.Stock.StockJournalBuilder> ReadStockJournal(List<int> stockJournalIds = null, List<int> stockIds = null, List<int> stockNumbers = null, DateTime? minEntryDate = null, DateTime? maxEntryDate = null);
|
||||
List<int> ReadStockJournalIdByStatus(int stockId, List<int> stockStatusIds = null);
|
||||
Dictionary<int, Model.Stock.StockJournalBuilder> ReadStockJournal(bool lockRecords = false,
|
||||
List<int> stockJournalIds = null, List<int> stockIds = null, List<string> stockNumbers = null
|
||||
, DateTime? minEntryDate = null, DateTime? maxEntryDate = null, List<int> stockStatusIds = null);
|
||||
Dictionary<int, (int id, string title, int? stockStatusIdDebit, int? stockStatusIdCredit)> ReadStockJournalType(List<int> stockJournalTypeIdList);
|
||||
(int, DateTime) ReadJournalStockIdAndEntryDate(int stockJournalId);
|
||||
int ReadJournalTypeIdByStockId(int stockId);
|
||||
|
||||
@@ -18,16 +18,22 @@ namespace bnhtrade.Core.Logic.Inventory
|
||||
internal StockJournalService(IUnitOfWork unitOfWork) : base(unitOfWork) { }
|
||||
|
||||
public Dictionary<int, Model.Stock.StockJournal> ReadStockJournal(List<int> stockJournalIds)
|
||||
{
|
||||
return WithUnitOfWork(uow =>
|
||||
{
|
||||
return ReadStockJournal(uow, false, stockJournalIds);
|
||||
});
|
||||
}
|
||||
|
||||
internal Dictionary<int, Model.Stock.StockJournal> ReadStockJournal(IUnitOfWork uow, bool lockJournalEntries, List<int> stockJournalIds)
|
||||
{
|
||||
if (stockJournalIds == null || stockJournalIds.Count == 0)
|
||||
{
|
||||
throw new ArgumentException("Stock journal IDs cannot be null or empty.", nameof(stockJournalIds));
|
||||
}
|
||||
|
||||
return WithUnitOfWork(uow =>
|
||||
{
|
||||
// get the stock journal entries from the repository
|
||||
var builderList = uow.StockJournalRepository.ReadStockJournal(stockJournalIds);
|
||||
var builderList = uow.StockJournalRepository.ReadStockJournal(lockJournalEntries, stockJournalIds);
|
||||
|
||||
// get the stock status for the journal posts
|
||||
var statusIds = new List<int>();
|
||||
@@ -62,7 +68,6 @@ namespace bnhtrade.Core.Logic.Inventory
|
||||
}
|
||||
|
||||
return returnDict;
|
||||
});
|
||||
}
|
||||
|
||||
public Dictionary<int, Model.Stock.StockJournalType> ReadStockJournalType(List<int> stockJournalTypeIds = null)
|
||||
@@ -324,7 +329,7 @@ namespace bnhtrade.Core.Logic.Inventory
|
||||
private bool StockJournalDelete(IUnitOfWork uow, int stockJournalId)
|
||||
{
|
||||
// get journal entry
|
||||
var stockJournalDict = uow.StockJournalRepository.ReadStockJournal(new List<int> { stockJournalId });
|
||||
var stockJournalDict = uow.StockJournalRepository.ReadStockJournal(true, new List<int> { stockJournalId });
|
||||
if (stockJournalDict.Count == 0)
|
||||
{
|
||||
throw new Exception("StockJournalID=" + stockJournalId + " does not exist!");
|
||||
@@ -343,10 +348,7 @@ namespace bnhtrade.Core.Logic.Inventory
|
||||
|
||||
// get all journal entries for stockId and debit status combination where entryDate >= stockJournal.EntryDate
|
||||
var journalList = uow.StockJournalRepository.ReadStockJournal(
|
||||
uow.StockJournalRepository.ReadStockJournalIdByStatus(stockJournal.StockId, debitStatusIds)
|
||||
, new List<int> { stockJournal.StockId }
|
||||
, null
|
||||
, stockJournal.EntryDate
|
||||
true, null, new List<int> { stockJournal.StockId }, null, stockJournal.EntryDate, null, debitStatusIds
|
||||
);
|
||||
|
||||
// check no credits for stockId & debit status combination have been made since delete entry
|
||||
@@ -386,7 +388,7 @@ namespace bnhtrade.Core.Logic.Inventory
|
||||
// can be used before commiting an sql insert, update or delete to the stock journal to ensure a status does not fall below 0
|
||||
// (unless the status is enabled to do so)
|
||||
// set empty list or statusIdEffected to null to check entier stock entries for consistency
|
||||
public bool WIP_StockJournalConsistencyCheck(IUnitOfWork uow, int stockId, List<int> statusIdsEffected = null)
|
||||
internal bool WIP_StockJournalConsistencyCheck(int stockId, List<int> statusIdsEffected = null)
|
||||
{
|
||||
if (statusIdsEffected == null)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user