mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 14:37:16 +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
|
// Create
|
||||||
//
|
//
|
||||||
@@ -77,14 +95,20 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
|||||||
// Read
|
// 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
|
List<int> stockJournalIds = null, List<int> stockIds = null, List<string> stockNumbers = null
|
||||||
, DateTime? minEntryDate = null, DateTime? maxEntryDate = null, List<int> stockStatusIds = null)
|
, DateTime? minEntryDate = null, DateTime? maxEntryDate = null, List<int> stockStatusIds = null)
|
||||||
{
|
{
|
||||||
var returnDict = new Dictionary<int, Model.Stock.StockJournalBuilder>();
|
var returnDict = new Dictionary<int, Model.Stock.StockJournalBuilder>();
|
||||||
var sqlWhere = new SqlWhereBuilder();
|
var sqlWhere = new SqlWhereBuilder();
|
||||||
|
|
||||||
string sql = @"
|
string lockClause = "";
|
||||||
|
if (lockRecords)
|
||||||
|
{
|
||||||
|
lockClause = " WITH (UPDLOCK, HOLDLOCK)";
|
||||||
|
}
|
||||||
|
|
||||||
|
string sql = $@"
|
||||||
SELECT tblStockJournal.StockJournalID,
|
SELECT tblStockJournal.StockJournalID,
|
||||||
tblStockJournal.StockJournalTypeID,
|
tblStockJournal.StockJournalTypeID,
|
||||||
tblStockJournal.StockID,
|
tblStockJournal.StockID,
|
||||||
@@ -97,9 +121,9 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
|||||||
tblStockJournalPost.StockJournalPostID,
|
tblStockJournalPost.StockJournalPostID,
|
||||||
tblStockJournalPost.StockStatusID,
|
tblStockJournalPost.StockStatusID,
|
||||||
tblStockJournalPost.Quantity,
|
tblStockJournalPost.Quantity,
|
||||||
FROM tblStockJournal
|
FROM tblStockJournal{lockClause}
|
||||||
LEFT OUTER JOIN tblStock ON tblStockJournal.StockID = tblStock.StockID
|
LEFT OUTER JOIN tblStock{lockClause} ON tblStockJournal.StockID = tblStock.StockID
|
||||||
LEFT OUTER JOIN tblStockJournalPost ON tblStockJournal.StockJournalID = tblStockJournalPost.StockJournalID
|
LEFT OUTER JOIN tblStockJournalPost{lockClause} ON tblStockJournal.StockJournalID = tblStockJournalPost.StockJournalID
|
||||||
WHERE 1=1 ";
|
WHERE 1=1 ";
|
||||||
|
|
||||||
// build where clause based on provided filters
|
// build where clause based on provided filters
|
||||||
@@ -116,7 +140,8 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
|||||||
}
|
}
|
||||||
if (stockNumbers != null && stockNumbers.Any())
|
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;
|
noFilter = false;
|
||||||
}
|
}
|
||||||
if (minEntryDate.HasValue)
|
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)
|
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)>();
|
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())
|
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)
|
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
|
||||||
{
|
{
|
||||||
@@ -507,9 +478,7 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
|
|||||||
tblStockJournal.StockID=@stockId
|
tblStockJournal.StockID=@stockId
|
||||||
AND tblStockJournalPost.Quantity>0 ";
|
AND tblStockJournalPost.Quantity>0 ";
|
||||||
|
|
||||||
sqlWhere.In("tblStockJournalPost.StockStatusID", stockStatusIdList, "AND");
|
stringSql += sqlWhere.InClause("tblStockJournalPost.StockStatusID", stockStatusIdList, "AND") + ";";
|
||||||
|
|
||||||
stringSql = stringSql + sqlWhere.SqlWhereString;
|
|
||||||
|
|
||||||
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
|
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 InsertStockJournalHeader(int stockId, int journalTypeId, DateTime entryDate, bool isLocked);
|
||||||
int InsertStockJournalPost(int stockJournalId, int stockStatusId, int quantity);
|
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);
|
Dictionary<int, Model.Stock.StockJournalBuilder> ReadStockJournal(bool lockRecords = false,
|
||||||
List<int> ReadStockJournalIdByStatus(int stockId, List<int> stockStatusIds = null);
|
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);
|
Dictionary<int, (int id, string title, int? stockStatusIdDebit, int? stockStatusIdCredit)> ReadStockJournalType(List<int> stockJournalTypeIdList);
|
||||||
(int, DateTime) ReadJournalStockIdAndEntryDate(int stockJournalId);
|
(int, DateTime) ReadJournalStockIdAndEntryDate(int stockJournalId);
|
||||||
int ReadJournalTypeIdByStockId(int stockId);
|
int ReadJournalTypeIdByStockId(int stockId);
|
||||||
|
|||||||
@@ -18,51 +18,56 @@ namespace bnhtrade.Core.Logic.Inventory
|
|||||||
internal StockJournalService(IUnitOfWork unitOfWork) : base(unitOfWork) { }
|
internal StockJournalService(IUnitOfWork unitOfWork) : base(unitOfWork) { }
|
||||||
|
|
||||||
public Dictionary<int, Model.Stock.StockJournal> ReadStockJournal(List<int> stockJournalIds)
|
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)
|
if (stockJournalIds == null || stockJournalIds.Count == 0)
|
||||||
{
|
{
|
||||||
throw new ArgumentException("Stock journal IDs cannot be null or empty.", nameof(stockJournalIds));
|
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(lockJournalEntries, stockJournalIds);
|
||||||
|
|
||||||
|
// get the stock status for the journal posts
|
||||||
|
var statusIds = new List<int>();
|
||||||
|
foreach (var journal in builderList.Values)
|
||||||
{
|
{
|
||||||
// get the stock journal entries from the repository
|
foreach (var post in journal.StockJournalBuilderPosts)
|
||||||
var builderList = uow.StockJournalRepository.ReadStockJournal(stockJournalIds);
|
|
||||||
|
|
||||||
// get the stock status for the journal posts
|
|
||||||
var statusIds = new List<int>();
|
|
||||||
foreach (var journal in builderList.Values)
|
|
||||||
{
|
{
|
||||||
foreach (var post in journal.StockJournalBuilderPosts)
|
if (!statusIds.Contains(post.StatusId))
|
||||||
{
|
{
|
||||||
if (!statusIds.Contains(post.StatusId))
|
statusIds.Add(post.StatusId);
|
||||||
{
|
|
||||||
statusIds.Add(post.StatusId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var statusDict = new StockStatusService(uow).GetStatus(statusIds);
|
}
|
||||||
|
var statusDict = new StockStatusService(uow).GetStatus(statusIds);
|
||||||
|
|
||||||
// get the stock journal types for the journal entries
|
// get the stock journal types for the journal entries
|
||||||
var journalTypeIds = new List<int>();
|
var journalTypeIds = new List<int>();
|
||||||
foreach (var journal in builderList.Values)
|
foreach (var journal in builderList.Values)
|
||||||
|
{
|
||||||
|
if (!journalTypeIds.Contains(journal.StockJournalTypeId))
|
||||||
{
|
{
|
||||||
if (!journalTypeIds.Contains(journal.StockJournalTypeId))
|
journalTypeIds.Add(journal.StockJournalTypeId);
|
||||||
{
|
|
||||||
journalTypeIds.Add(journal.StockJournalTypeId);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
var journalTypeDict = ReadStockJournalType(uow, journalTypeIds);
|
}
|
||||||
|
var journalTypeDict = ReadStockJournalType(uow, journalTypeIds);
|
||||||
|
|
||||||
// build the stock journal objects and return them in a dictionary
|
// build the stock journal objects and return them in a dictionary
|
||||||
var returnDict = new Dictionary<int, Model.Stock.StockJournal>();
|
var returnDict = new Dictionary<int, Model.Stock.StockJournal>();
|
||||||
foreach (var journal in builderList.Values)
|
foreach (var journal in builderList.Values)
|
||||||
{
|
{
|
||||||
returnDict.Add(journal.StockJournalId, journal.Build(journalTypeDict, statusDict));
|
returnDict.Add(journal.StockJournalId, journal.Build(journalTypeDict, statusDict));
|
||||||
}
|
}
|
||||||
|
|
||||||
return returnDict;
|
return returnDict;
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Dictionary<int, Model.Stock.StockJournalType> ReadStockJournalType(List<int> stockJournalTypeIds = null)
|
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)
|
private bool StockJournalDelete(IUnitOfWork uow, int stockJournalId)
|
||||||
{
|
{
|
||||||
// get journal entry
|
// 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)
|
if (stockJournalDict.Count == 0)
|
||||||
{
|
{
|
||||||
throw new Exception("StockJournalID=" + stockJournalId + " does not exist!");
|
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
|
// get all journal entries for stockId and debit status combination where entryDate >= stockJournal.EntryDate
|
||||||
var journalList = uow.StockJournalRepository.ReadStockJournal(
|
var journalList = uow.StockJournalRepository.ReadStockJournal(
|
||||||
uow.StockJournalRepository.ReadStockJournalIdByStatus(stockJournal.StockId, debitStatusIds)
|
true, null, new List<int> { stockJournal.StockId }, null, stockJournal.EntryDate, null, debitStatusIds
|
||||||
, new List<int> { stockJournal.StockId }
|
|
||||||
, null
|
|
||||||
, stockJournal.EntryDate
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// check no credits for stockId & debit status combination have been made since delete entry
|
// 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
|
// 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)
|
// (unless the status is enabled to do so)
|
||||||
// set empty list or statusIdEffected to null to check entier stock entries for consistency
|
// 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)
|
if (statusIdsEffected == null)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user