mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 06:27:15 +00:00
Stock SKU transaction reconciliation
This commit is contained in:
@@ -34,7 +34,11 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
OUTPUT INSERTED.StockSkuTransactionID
|
||||
VALUES (
|
||||
@transactionDate
|
||||
,@stockSkuTransactionTypeID
|
||||
,(
|
||||
SELECT StockSkuTransactionTypeID
|
||||
FROM tblStockSkuTransactionType
|
||||
WHERE TypeCode = @skuTransactionTypeCode
|
||||
)
|
||||
,@foreignKey
|
||||
,@reference
|
||||
,@Detail
|
||||
@@ -50,7 +54,7 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@transactionDate", skuTransaction.TransactionDate.ToUniversalTime());
|
||||
cmd.Parameters.AddWithValue("@stockSkuTransactionTypeID", skuTransaction.SkuTransactionTypeId);
|
||||
cmd.Parameters.AddWithValue("@skuTransactionTypeCode", skuTransaction.SkuTransactionTypeCode);
|
||||
if (!skuTransaction.IsSetForeignKey) { cmd.Parameters.AddWithValue("@foreignKey", DBNull.Value); }
|
||||
else { cmd.Parameters.AddWithValue("@foreignKey", skuTransaction.ForeignKey); }
|
||||
if (!skuTransaction.IsSetReference) { cmd.Parameters.AddWithValue("@reference", DBNull.Value); }
|
||||
|
||||
@@ -10,11 +10,45 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
{
|
||||
public class ReadSkuTransaction : Connection
|
||||
{
|
||||
private Data.Database.WhereBuilder whereBuilder = new WhereBuilder();
|
||||
|
||||
public ReadSkuTransaction(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initialise result filters
|
||||
/// </summary>
|
||||
public void Init()
|
||||
{
|
||||
whereBuilder = new WhereBuilder();
|
||||
IsReconciled = null;
|
||||
StockTransactionTypeName = null;
|
||||
StockTransactionTypeCode = null;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read result filter
|
||||
/// </summary>
|
||||
public bool? IsReconciled { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Read result filter
|
||||
/// </summary>
|
||||
public List<string> StockTransactionTypeName { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Read result filter
|
||||
/// </summary>
|
||||
public List<string> StockTransactionTypeCode { get; set; }
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Retrive a 'Stock Journal ID' for a reconciled SKU transaction
|
||||
/// </summary>
|
||||
/// <param name="skuTransactionId">Stock SKU Transaction ID</param>
|
||||
/// <returns>Stock Journal ID</returns>
|
||||
public int? GetJournalId(int skuTransactionId)
|
||||
{
|
||||
using (var conn = new SqlConnection(sqlConnectionString))
|
||||
@@ -41,24 +75,19 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
}
|
||||
}
|
||||
|
||||
public List<Model.Stock.SkuTransaction> GetUnreconciled()
|
||||
|
||||
/// <summary>
|
||||
/// Populates model class from filtered database results
|
||||
/// </summary>
|
||||
/// <returns>List of SkuTransaction</returns>
|
||||
public List<Model.Stock.SkuTransaction> Read()
|
||||
{
|
||||
// order by stocktransId desc = Amazon reports are listed in datetime ASC order, some reports have date only,
|
||||
// however I have reason to believe these are in time order.
|
||||
// Adding this means they at least get processed in the correct order (maybe)!
|
||||
string sqlWhere = @"
|
||||
WHERE tblStockSkuTransaction.IsProcessed = 0
|
||||
AND (
|
||||
tblStockSkuTransactionType.StockJournalEntryEnabled = 1
|
||||
OR tblStockSkuTransactionType.IsNewReviewRequired = 1
|
||||
) ";
|
||||
|
||||
return Read(sqlWhere, new DynamicParameters());
|
||||
}
|
||||
|
||||
private List<Model.Stock.SkuTransaction> Read(string sqlWhere, DynamicParameters parameters)
|
||||
{
|
||||
var resultList = new List<Model.Stock.SkuTransaction>();
|
||||
var parameters = new DynamicParameters();
|
||||
|
||||
string sql = @"
|
||||
SELECT tblStockSkuTransaction.StockSkuTransactionID AS SkuTransactionId
|
||||
@@ -70,14 +99,43 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
,tblStockSkuTransaction.Quantity
|
||||
,tblStockSkuTransaction.IsProcessed
|
||||
,tblStockSkuTransaction.StockJournalID AS StockJournalId
|
||||
,tblStockSkuTransactionType.TypeTitle AS SkuTransactionTypeName
|
||||
,tblStockSkuTransactionType.TypeName AS SkuTransactionTypeName
|
||||
,tblSku.skuSkuNumber AS SkuNumber
|
||||
,tblStockSkuTransaction.SkuID
|
||||
FROM tblStockSkuTransaction
|
||||
INNER JOIN tblStockSkuTransactionType ON tblStockSkuTransaction.StockSkuTransactionTypeID = tblStockSkuTransactionType.StockSkuTransactionTypeID
|
||||
INNER JOIN tblSku ON tblStockSkuTransaction.SkuID = tblSku.skuSkuID ";
|
||||
|
||||
sql += sqlWhere;
|
||||
INNER JOIN tblSku ON tblStockSkuTransaction.SkuID = tblSku.skuSkuID
|
||||
WHERE 1=1 ";
|
||||
|
||||
if (IsReconciled != null)
|
||||
{
|
||||
sql += @"
|
||||
AND tblStockSkuTransaction.IsProcessed =";
|
||||
if (IsReconciled.GetValueOrDefault())
|
||||
{
|
||||
sql += " 1 ";
|
||||
}
|
||||
else
|
||||
{
|
||||
sql += " 0 ";
|
||||
}
|
||||
}
|
||||
|
||||
if (StockTransactionTypeName != null && StockTransactionTypeName.Any())
|
||||
{
|
||||
parameters.Add("@stockTransactionTypeName", StockTransactionTypeName);
|
||||
|
||||
sql += @"
|
||||
AND tblStockSkuTransactionType.TypeName IN @stockTransactionTypeName ";
|
||||
}
|
||||
|
||||
if (StockTransactionTypeCode != null && StockTransactionTypeCode.Any())
|
||||
{
|
||||
parameters.Add("@stockTransactionTypeCode", StockTransactionTypeCode);
|
||||
|
||||
sql += @"
|
||||
AND tblStockSkuTransactionType.TypeCode IN @stockTransactionTypeCode ";
|
||||
}
|
||||
|
||||
sql += @"
|
||||
ORDER BY tblStockSkuTransaction.TransactionDate ASC, tblStockSkuTransaction.StockSkuTransactionID DESC;";
|
||||
|
||||
@@ -54,15 +54,11 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
int index01 = reader.GetOrdinal("StockSkuTransactionTypeID");
|
||||
int index02 = reader.GetOrdinal("IsNewReviewRequired");
|
||||
int index03 = reader.GetOrdinal("TransactionImportEnabled");
|
||||
int transactionTypeId = reader.GetInt32(0);
|
||||
bool isNew = reader.GetBoolean(1);
|
||||
bool importEnabled = reader.GetBoolean(2);
|
||||
|
||||
int transactionTypeId = reader.GetInt32(index01);
|
||||
bool isNew = reader.GetBoolean(index02);
|
||||
bool? importEnabled = reader[index03] as bool? ?? null; // column can be null
|
||||
|
||||
if (isNew == true || importEnabled == null)
|
||||
if (isNew == true)
|
||||
{
|
||||
// return 0 and 'skip' item
|
||||
return 0;
|
||||
|
||||
@@ -148,14 +148,18 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
|
||||
public void Update(Model.Stock.SkuTransaction skuTransaction)
|
||||
{
|
||||
using (var conn = new SqlConnection())
|
||||
using (var conn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
UPDATE tblStockSkuTransaction
|
||||
SET TransactionDate = @transactionDate
|
||||
,StockSkuTransactionTypeID = @stockSkuTransactionTypeID
|
||||
,StockSkuTransactionTypeID = (
|
||||
SELECT StockSkuTransactionTypeID
|
||||
FROM tblStockSkuTransactionType
|
||||
WHERE TypeCode = @skuTransactionTypeCode
|
||||
)
|
||||
,ForeignKey = @foreignKey
|
||||
,Reference = @reference
|
||||
,Detail = @Detail
|
||||
@@ -171,7 +175,7 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@transactionDate", skuTransaction.TransactionDate.ToUniversalTime());
|
||||
cmd.Parameters.AddWithValue("@stockSkuTransactionTypeID", skuTransaction.SkuTransactionTypeId);
|
||||
cmd.Parameters.AddWithValue("@skuTransactionTypeCode", skuTransaction.SkuTransactionTypeCode);
|
||||
if (!skuTransaction.IsSetForeignKey) { cmd.Parameters.AddWithValue("@foreignKey", DBNull.Value); }
|
||||
else { cmd.Parameters.AddWithValue("@foreignKey", skuTransaction.ForeignKey); }
|
||||
if (!skuTransaction.IsSetReference) { cmd.Parameters.AddWithValue("@reference", DBNull.Value); }
|
||||
|
||||
Reference in New Issue
Block a user