Feature: stock replenishment

This commit is contained in:
Bobbie Hodgetts
2024-05-09 13:23:33 +01:00
committed by GitHub
parent 91ef9acc78
commit 270eebca9a
30 changed files with 721 additions and 249 deletions

View File

@@ -10,19 +10,29 @@ namespace bnhtrade.Core.Data.Database.Account
public class ReadAccountCode : Connection
{
private Data.Database.SqlWhereBuilder sqlWhere = new SqlWhereBuilder();
private List<Model.Account.AccountCode> resultList;
private List<Model.Account.Account> resultList;
public ReadAccountCode()
{
}
public List<Model.Account.AccountCode> All()
/// <summary>
/// Gets the full chart of accounts
/// </summary>
/// <returns>Dictionary where the database record id is the key</returns>
public Dictionary<uint, Model.Account.Account> All()
{
Innit();
return Execute(null, null);
var list = Execute(null, null);
var dictionary = new Dictionary<uint, Model.Account.Account>();
foreach (var item in list)
{
dictionary.Add(item.Id, item);
}
return dictionary;
}
public List<Model.Account.AccountCode> ByAccountCode(List<int> accountCodeList)
public List<Model.Account.Account> ByAccountCode(List<int> accountCodeList)
{
Innit();
@@ -35,7 +45,7 @@ namespace bnhtrade.Core.Data.Database.Account
return Execute(sqlWhere.SqlWhereString, sqlWhere.ParameterList);
}
private List<Model.Account.AccountCode> Execute(string sqlWhere, Dictionary<string, object> parameters)
private List<Model.Account.Account> Execute(string sqlWhere, Dictionary<string, object> parameters)
{
//build sql query
string sqlString = @"
@@ -45,6 +55,7 @@ namespace bnhtrade.Core.Data.Database.Account
,tblAccountChartOf.Description
,tblAccountChartOfType.AccountChartOfType
,tblAccountChartOfType.BasicType
,tblAccountChartOfType.Multiplier
FROM tblAccountChartOf
INNER JOIN tblAccountChartOfType ON tblAccountChartOf.AccountChartOfTypeID = tblAccountChartOfType.AccountChartOfTypeID
" + sqlWhere;
@@ -69,19 +80,20 @@ namespace bnhtrade.Core.Data.Database.Account
{
while (reader.Read())
{
var result = new Model.Account.AccountCode();
int tablePk = reader.GetInt32(0);
result.AccountCodeId = reader.GetInt32(1);
result.Title = reader.GetString(2);
if (!reader.IsDBNull(3)) { result.Description = reader.GetString(3); }
result.Type = reader.GetString(4);
result.BasicType = reader.GetString(5);
uint tablePk = (uint)reader.GetInt32(0);
uint accountCode = (uint)reader.GetInt32(1);
string title = reader.GetString(2);
string description = null;
if (!reader.IsDBNull(3)) { description = reader.GetString(3); }
string type = reader.GetString(4);
string basicType = reader.GetString(5);
int multiplier = reader.GetInt32(6);
var result = new Model.Account.Account(tablePk, accountCode, title, description, type, basicType, multiplier);
resultList.Add(result);
}
}
return resultList;
}
}
@@ -90,7 +102,7 @@ namespace bnhtrade.Core.Data.Database.Account
private void Innit()
{
resultList = new List<Model.Account.AccountCode>();
resultList = new List<Model.Account.Account>();
}
}
}

View File

@@ -94,7 +94,7 @@ namespace bnhtrade.Core.Data.Database.Account
taxcodeList = taxcodeList.Distinct().ToList();
whereBuilder.Innit();
whereBuilder.Init();
whereBuilder.In("TaxCode", taxcodeList, "WHERE");
return Execute(whereBuilder.SqlWhereString, whereBuilder.ParameterList);
@@ -111,7 +111,7 @@ namespace bnhtrade.Core.Data.Database.Account
taxcodeIdList = taxcodeIdList.Distinct().ToList();
whereBuilder.Innit();
whereBuilder.Init();
whereBuilder.In("AccountTaxCodeID", taxcodeIdList, "WHERE");
return Execute(whereBuilder.SqlWhereString, whereBuilder.ParameterList);
@@ -144,7 +144,7 @@ namespace bnhtrade.Core.Data.Database.Account
FROM tblSku
INNER JOIN tblAccountTaxCode ON tblSku.AccountTaxCodeID = tblAccountTaxCode.AccountTaxCodeID ";
whereBuilder.Innit();
whereBuilder.Init();
whereBuilder.In("tblSku.skuSkuNumber", skuNumberList, "WHERE");
sql += whereBuilder.SqlWhereString;
@@ -196,7 +196,7 @@ namespace bnhtrade.Core.Data.Database.Account
LEFT OUTER JOIN tblAccountTaxCode ON tblAccountInvoiceLineItem.AccountTaxCodeID_Default = tblAccountTaxCode.AccountTaxCodeID
";
whereBuilder.Innit();
whereBuilder.Init();
whereBuilder.In("tblAccountInvoiceLineItem.ItemCode", lineItemCode, "WHERE");
sql += whereBuilder.SqlWhereString;

View File

@@ -59,6 +59,17 @@ namespace bnhtrade.Core.Data.Database
SkuReconciliationFbaReimbursement = 11,
}
/// <summary>
/// Returns the sql table ID for a stock status
/// </summary>
public enum StockStatus
{
FbaFulfillable = 15,
FbaSold = 12,
ReservedGarageStock = 16,
Reserved = 11,
}
/// <summary>
/// Returns the sql table ID for a stock status type
/// </summary>

View File

@@ -104,7 +104,7 @@ namespace bnhtrade.Core.Data.Database.Export
cmd.Parameters.AddWithValue("@invoiceID", invoiceId);
cmd.Parameters.AddWithValue("@itemCode", invoiceList[i].InvoiceLineList[j].ItemCode);
cmd.Parameters.AddWithValue("@netAmount", invoiceList[i].InvoiceLineList[j].UnitAmount);
cmd.Parameters.AddWithValue("@accountCode", invoiceList[i].InvoiceLineList[j].AccountCode.AccountCodeId);
cmd.Parameters.AddWithValue("@accountCode", invoiceList[i].InvoiceLineList[j].AccountCode.AccountCode);
cmd.Parameters.AddWithValue("@taxAmount", invoiceList[i].InvoiceLineList[j].TaxAmount);
cmd.Parameters.AddWithValue("@taxCode", invoiceList[i].InvoiceLineList[j].TaxCode.TaxCode);

View File

@@ -43,7 +43,7 @@ namespace bnhtrade.Core.Data.Database.Import
INNER JOIN tblImportAmazonSettlementReportLine ON tblImportAmazonSettlementReport.ImportAmazonSettlementReportID = tblImportAmazonSettlementReportLine.ImportAmazonSettlementReportID
WHERE ";
whereBuilder.Innit();
whereBuilder.Init();
whereBuilder.In("tblImportAmazonSettlementReport.[settlement-id]", settlementIdList);
sql += whereBuilder.SqlWhereString;

View File

@@ -120,7 +120,7 @@ namespace bnhtrade.Core.Data.Database.Product
// add nullable items to model
if (!reader.IsDBNull(2))
item.TitleSuffix = reader.GetString(2);
if (!reader.IsDBNull(3))
if (!reader.IsDBNull(4))
item.ProductCategorySubID = reader.GetInt32(4);
if (!reader.IsDBNull(5))
item.ReleaseDate = DateTime.SpecifyKind(reader.GetDateTime(5), DateTimeKind.Utc);
@@ -187,11 +187,11 @@ namespace bnhtrade.Core.Data.Database.Product
";
var sqlWhereBuilder = new Database.SqlWhereBuilder();
sqlWhereBuilder.Innit();
sqlWhereBuilder.Init();
sqlWhereBuilder.In("tblSku.skuSkuNumber", skuNumberList);
sql += sqlWhereBuilder.SqlWhereString;
using (SqlConnection conn = new SqlConnection())
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
conn.Open();

View File

@@ -34,7 +34,7 @@ namespace bnhtrade.Core.Data.Database.Sku
FROM tblSku
WHERE ";
sqlWhere.Innit();
sqlWhere.Init();
sqlWhere.In("skuSkuNumber", skuNumberList);
sqlString += sqlWhere.SqlWhereString;

View File

@@ -7,44 +7,71 @@ using System.Threading.Tasks;
namespace bnhtrade.Core.Data.Database
{
/// <summary>
/// Step 1: Call the methods for each where clause you want to create. This can be done multiple times to create an sql where string. Pay attention
/// to the prefixes that you'll require between each where clause, as each time a method is called the sql statement will be appended to the previous sql
/// string.
/// Step 2: Appened the created sql string to your sql statement, NB the WHERE statemet is not included by default.
/// Step 3: Once you've created your sql command object, add the parameters to it using the method contained within this class.
/// STep 4: exceute your sql commend.
/// </summary>
public class SqlWhereBuilder
{
private int parameterCount;
private int parameterCount = 0;
public SqlWhereBuilder()
{
Innit();
Init();
}
public string SqlWhereString { get; private set; }
public bool IsSetSqlWhereString
{
get
{
if (SqlWhereString == null || string.IsNullOrEmpty(SqlWhereString))
{
return false;
}
else
{
return true;
}
}
}
public Dictionary<string, object> ParameterList { get; private set; }
public void Innit()
/// <summary>
/// Initialises the class
/// </summary>
public void Init()
{
SqlWhereString = null;
ParameterList = new Dictionary<string, object>();
parameterCount = 0;
SqlWhereString = "";
ParameterList = new Dictionary<string, object>();
}
public void AddParametersToSqlCommand(SqlCommand cmd)
{
foreach (var item in ParameterList)
if (ParameterList != null)
{
cmd.Parameters.AddWithValue(item.Key, item.Value);
foreach (var item in ParameterList)
{
cmd.Parameters.AddWithValue(item.Key, item.Value);
}
}
}
/// <summary>
/// Used to create a string for an SQL where condition 'In' statement
/// Append an 'In' statement and parameter list to the class properties
/// </summary>
/// <param name="columnReference">Name of the column to used to for the condition statement</param>
/// <param name="orValueList">List of values to test in condition statement</param>
/// <param name="wherePrefix">Optional string to prefix to the returned result</param>
/// <param name="wherePrefix">Optional prefix that gets added to the sql string result</param>
public void In(string columnReference, List<string> orValueList, string wherePrefix = null)
{
Innit();
if (orValueList == null || !orValueList.Any())
{
return;
@@ -62,7 +89,6 @@ namespace bnhtrade.Core.Data.Database
sqlWhere += " " + columnReference + " IN ( ";
var paramters = new Dictionary<string, object>();
int listCount = distinctList.Count();
for (int i = 0; i < listCount; i++, parameterCount++)
{
@@ -73,20 +99,19 @@ namespace bnhtrade.Core.Data.Database
string param = "@parameter" + parameterCount;
sqlWhere += param;
paramters.Add(param, distinctList[i]);
ParameterList.Add(param, distinctList[i]);
}
sqlWhere += " )";
sqlWhere += " ) ";
SqlWhereString = sqlWhere;
ParameterList = paramters;
SqlWhereString = SqlWhereString + sqlWhere;
}
/// <summary>
/// Used to create a string for an SQL where condition 'In' statement
/// Append an 'In' statement and parameter list to the class properties
/// </summary>
/// <param name="columnReference">Name of the column to used to for the condition statement</param>
/// <param name="orValueList">List of values to test in condition statement</param>
/// <param name="wherePrefix">Optional string to prefix to the returned result</param>
/// <param name="wherePrefix">Optional prefix that gets added to the sql string result</param>
public void In(string columnReference, List<int> orValueList, string wherePrefix = null)
{
var stringList = new List<string>();

View File

@@ -119,5 +119,54 @@ namespace bnhtrade.Core.Data.Database.Stock
}
return statusBalance;
}
/// <summary>
/// Get a list of SKUs avaliable against a specified statusId
/// </summary>
/// <param name="statusId">The stock status id</param>
/// <returns>Dictionary of SKUs and the balance of each</returns>
public Dictionary<string, int> ByStatusId(int statusId)
{
var returnList = new Dictionary<string, int>();
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(@"
SELECT sub.*
FROM (
SELECT tblSku.skuSkuID
,tblSku.skuSkuNumber
,SUM(tblStockJournalPost.Quantity) AS Balance
FROM tblStockJournal
INNER JOIN tblStockJournalPost ON tblStockJournal.StockJournalID = tblStockJournalPost.StockJournalID
INNER JOIN tblStock ON tblStockJournal.StockID = tblStock.StockID
INNER JOIN tblSku ON tblStock.SkuID = tblSku.skuSkuID
WHERE (tblStockJournalPost.StockStatusID = @statusId)
GROUP BY tblSku.skuSkuID
,tblSku.skuSkuNumber
) sub
WHERE sub.Balance <> 0
", conn))
{
// add parameters
cmd.Parameters.AddWithValue("@statusId", statusId);
// execute
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
string sku = reader.GetString(1);
int qty = reader.GetInt32(2);
returnList.Add(sku, qty);
}
}
}
}
return returnList;
}
}
}

View File

@@ -112,7 +112,7 @@ namespace bnhtrade.Core.Data.Database.Stock
foreach (var item in dbresults)
{
result.Sku = item.SkuNumber;
result.SkuNumber = item.SkuNumber;
result.AddBalanceTransaction(item.EntryDate, item.StockNumber, item.Quantity);
}
}
@@ -125,15 +125,15 @@ namespace bnhtrade.Core.Data.Database.Stock
/// Gets list of stock status debits and credits. Includes stock numbers with balance greater than or less than zero.
/// </summary>
/// <param name="stockStatusId">Required, filters results by stock status Id</param>
/// <param name="sku">Required, filters results by sku number</param>
/// <param name="skuNumber">Required, filters results by sku number</param>
/// <returns>Tuple list is assending order</returns>
public Model.Stock.StatusTransaction BySku(int stockStatusId, string sku)
public Model.Stock.StatusTransaction BySku(int stockStatusId, string skuNumber)
{
var result = new Model.Stock.StatusTransaction();
result.Sku = sku;
result.SkuNumber = skuNumber;
result.StockStatusId = stockStatusId;
if (string.IsNullOrWhiteSpace(sku))
if (string.IsNullOrWhiteSpace(skuNumber))
{
return result;
}
@@ -176,7 +176,7 @@ namespace bnhtrade.Core.Data.Database.Stock
{
var param = new Dapper.DynamicParameters();
param.Add("@stockStatusId", stockStatusId);
param.Add("@sku", sku);
param.Add("@sku", skuNumber);
var dbList = conn.Query(strSQL, param);

View File

@@ -0,0 +1,115 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static bnhtrade.Core.Data.Database.Constants;
namespace bnhtrade.Core.Data.Database.Stock
{
internal class Status : Connection
{
private bnhtrade.Core.Data.Database.SqlWhereBuilder sqlBuilder;
public List<int> StatusIds { get; set; }
public List<int> StatusTypeIds { get; set; }
public Status ()
{
Init();
}
public void Init()
{
sqlBuilder = new SqlWhereBuilder();
StatusIds = new List<int>();
StatusTypeIds = new List<int>();
}
public List<Model.Stock.Status> Read()
{
var returnList = new List<Model.Stock.Status>();
sqlBuilder.Init();
//build sql query
string sql = @"
SELECT [StockStatusID]
,[StatusCode]
,[StockStatus]
,[StockStatusTypeID]
,[Reference]
,[ForeignKeyID]
,[IsCreditOnly]
,[IsClosed]
,[RecordCreated]
FROM [e2A].[dbo].[tblStockStatus]
WHERE 1=1 ";
// build the where statments
if (StatusIds.Any())
{
sqlBuilder.In("StockStatusID", StatusIds, "AND");
}
if (StatusTypeIds.Any())
{
sqlBuilder.In("StockStatusTypeID", StatusTypeIds, "AND");
}
// append where string to the sql
if (sqlBuilder.IsSetSqlWhereString)
{
sql = sql + sqlBuilder.SqlWhereString;
}
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(sql, conn))
{
sqlBuilder.AddParametersToSqlCommand(cmd);
using (SqlDataReader reader = cmd.ExecuteReader())
{
if (reader.HasRows)
{
var typeDict = new Data.Database.Stock.StatusType().Read();
while (reader.Read())
{
int statusId = reader.GetInt32(0);
int? statusCode = null;
if (!reader.IsDBNull(1)) { statusCode = reader.GetInt32(1); }
string stockStatus = reader.GetString(2);
int typeId = reader.GetInt32(3);
string reference = null;
if (!reader.IsDBNull(4)) { reference = reader.GetString(4); }
int? foreignKeyId = null;
if (!reader.IsDBNull(5)) { foreignKeyId = reader.GetInt32(5); }
bool isCreditOnly = reader.GetBoolean(6);
bool isClosed = reader.GetBoolean(7);
DateTime recordCreated = DateTime.SpecifyKind(reader.GetDateTime(8), DateTimeKind.Utc);
var newItem = new Model.Stock.Status(statusId
, statusCode
, stockStatus
, typeDict[typeId]
, reference
, foreignKeyId
, isCreditOnly
, isClosed
, recordCreated
);
returnList.Add(newItem);
}
}
}
}
}
return returnList;
}
}
}

View File

@@ -0,0 +1,63 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Data.Database.Stock
{
public class StatusType : Connection
{
public StatusType()
{
}
/// <summary>
/// Reads all Status Types from database
/// </summary>
/// <returns></returns>
public Dictionary<int, Model.Stock.StatusType> Read()
{
var returnDict = new Dictionary<int, Model.Stock.StatusType>();
// get all account info before we start
var accountDict = new Data.Database.Account.ReadAccountCode().All();
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(@"
SELECT [StockStatusTypeID]
,[StatusTypeName]
,[ForeignKeyType]
,[ReferenceType]
,[AccountChartOfID]
FROM [e2A].[dbo].[tblStockStatusType]
", conn))
{
using (SqlDataReader reader = cmd.ExecuteReader())
{
var accountIdDict = new Dictionary<int, int>();
while (reader.Read())
{
int statusTypeId = reader.GetInt32(0);
string name = reader.GetString(1);
string foreignKey = null;
if (!reader.IsDBNull(2)) { foreignKey = reader.GetString(2); }
string reference = null;
if (!reader.IsDBNull(3)) { reference = reader.GetString(3); }
uint accountId = (uint)reader.GetInt32(4);
var statusType = new Model.Stock.StatusType(statusTypeId, name, foreignKey, reference, accountDict[accountId]);
returnDict.Add(statusTypeId, statusType);
}
}
}
}
return returnDict;
}
}
}