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)
{
if (ParameterList != null)
{
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 += " ) ";
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;
}
}
}

View File

@@ -15,12 +15,12 @@ namespace bnhtrade.Core.Logic.Account
readAccountCode = new Data.Database.Account.ReadAccountCode();
}
public List<Model.Account.AccountCode> GetAll()
public Dictionary<uint, Model.Account.Account> GetAll()
{
return readAccountCode.All();
}
public Model.Account.AccountCode ByAccountCode(int accountCode)
public Model.Account.Account ByAccountCode(int accountCode)
{
var list = ByAccountCode(new List<int> { accountCode });
if (list.Any())
@@ -33,14 +33,14 @@ namespace bnhtrade.Core.Logic.Account
}
}
public List<Model.Account.AccountCode> ByAccountCode(List<int> accountCodeList)
public List<Model.Account.Account> ByAccountCode(List<int> accountCodeList)
{
return readAccountCode.ByAccountCode(accountCodeList);
}
public Dictionary<int, Model.Account.AccountCode> ConvertToDictionary(List<Model.Account.AccountCode> accountCodeList)
public Dictionary<int, Model.Account.Account> ConvertToDictionary(List<Model.Account.Account> accountCodeList)
{
var returnDict = new Dictionary<int, Model.Account.AccountCode>();
var returnDict = new Dictionary<int, Model.Account.Account>();
if (accountCodeList == null)
{
@@ -49,9 +49,9 @@ namespace bnhtrade.Core.Logic.Account
foreach (var accountCode in accountCodeList)
{
if (!returnDict.ContainsKey(accountCode.AccountCodeId))
if (!returnDict.ContainsKey((int)accountCode.AccountCode))
{
returnDict.Add(accountCode.AccountCodeId, accountCode);
returnDict.Add((int)accountCode.AccountCode, accountCode);
}
}
return returnDict;

View File

@@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Logic.Amazon.Fba
{
public class Stock
{
Logic.Stock.StatusBalance read = new Logic.Stock.StatusBalance();
/// <summary>
/// Get a list of FBA SKUs to replenish from reversed stock. This using the local stock ledger and may not tally with Amazon
/// </summary>
/// <returns>SKU and the avaible stock in reserve</returns>
public List<Model.Stock.StatusBalance> GetReplenishmentList()
{
int fbaFulfillableStatusId = (int)Data.Database.Constants.StockStatus.FbaFulfillable;
int reserveStatusId = (int)Data.Database.Constants.StockStatus.ReservedGarageStock;
var fbaList = read.GetSkuQuantity(fbaFulfillableStatusId);
var myList = read.GetSkuQuantity(reserveStatusId);
foreach (var item in fbaList)
{
if (myList.ContainsKey(item.Key))
{
myList.Remove(item.Key);
}
}
var readStatusBalance = new Logic.Stock.StatusBalance();
var returnList = new List<Model.Stock.StatusBalance>();
foreach (var item in myList)
{
var skuStatusBalance = readStatusBalance.GetStatusBalance(item.Key, reserveStatusId, true);
returnList.Add(skuStatusBalance);
}
return returnList;
}
}
}

View File

@@ -66,7 +66,7 @@ namespace bnhtrade.Core.Logic.Sku.Price
statusTypeList.Add((int)Data.Database.Constants.StockStatusType.FbaInventoryActive);
statusTypeList.Add((int)Data.Database.Constants.StockStatusType.FbaShipment);
var fbaSkuStock = new Logic.Stock.GetStatusTypeBalance().BySku(statusTypeList);
var fbaSkuStock = new Logic.Stock.StatusTypeBalance().GetByStatusTypeId(statusTypeList);
// retrive SKU info
var getSku = new Logic.Sku.GetSkuInfo();

View File

@@ -26,29 +26,29 @@ namespace bnhtrade.Core.Logic.Stock
}
/// <summary>
/// Return the avaliable balance of a status at a specified date and time. Useful for checking availability before
/// moving stock/sku retrospectivly
/// Return the avaliable balance of a status, includes datetime that each stockNumber became avaiable.
/// Useful for checking availability before moving stock/sku retrospectivly
/// </summary>
/// <param name="sku">SKU number</param>
/// <param name="skuNumber">SKU number</param>
/// <param name="statusId">Status ID</param>
/// <param name="atDate">Date and time you would like to know the balance at</param>
/// <param name="includeDetails">True, inlcude SKU and StockStatus class. False, supply the SKU Number and StockStatusId instead</param>
/// <returns></returns>
public Model.Stock.StatusBalance GetBySku(string sku, int statusId)
public Model.Stock.StatusBalance GetStatusBalance(string skuNumber, int statusId, bool includeDetails = true)
{
if (string.IsNullOrWhiteSpace(sku))
if (string.IsNullOrWhiteSpace(skuNumber))
{
throw new Exception("SKU number is null, empty, or whitespace");
}
// get list of transactions for availale stock
var stockTransaction = new Data.Database.Stock.ReadStatusTransaction();
var transList = stockTransaction.BySku(statusId, sku);
var readStatusTransaction = new Data.Database.Stock.ReadStatusTransaction();
var statusTransaction = readStatusTransaction.BySku(statusId, skuNumber);
// create quantity list
List<int> qtyList = new List<int>();
for (int i = 0; i < transList.TransactionList.Count; i++)
for (int i = 0; i < statusTransaction.TransactionList.Count; i++)
{
qtyList.Add(transList.TransactionList[i].Quantity);
qtyList.Add(statusTransaction.TransactionList[i].Quantity);
}
// tally list
@@ -57,15 +57,15 @@ namespace bnhtrade.Core.Logic.Stock
{
if (qtyList[iCr] < 0)
{
int crStockNumber = transList.TransactionList[iCr].StockNumber;
DateTime crDate = transList.TransactionList[iCr].EntryDate;
int crStockNumber = statusTransaction.TransactionList[iCr].StockNumber;
DateTime crDate = statusTransaction.TransactionList[iCr].EntryDate;
// loop, in reverse, to find debits
for (int iDr = qtyList.Count - 1; iDr > -1; iDr--)
{
// find debits, last in first out (filter by date)
if (transList.TransactionList[iDr].StockNumber == crStockNumber
&& transList.TransactionList[iDr].EntryDate <= crDate
if (statusTransaction.TransactionList[iDr].StockNumber == crStockNumber
&& statusTransaction.TransactionList[iDr].EntryDate <= crDate
&& qtyList[iDr] > 0)
{
// credit fully assigned
@@ -87,22 +87,49 @@ namespace bnhtrade.Core.Logic.Stock
}
// build result list from tally results
var result = new Model.Stock.StatusBalance();
result.Sku = transList.Sku;
result.StockStatusId = transList.StockStatusId;
var entryList = new List<(DateTime EntryDate, int StockNumber, int Quantity)>();
for (int i = 0; i < qtyList.Count; i++)
{
if (qtyList[i] != 0)
{
result.AddBalanceTransaction(
transList.TransactionList[i].EntryDate,
transList.TransactionList[i].StockNumber,
(DateTime EntryDate, int StockNumber, int Quantity) entryItem = (
statusTransaction.TransactionList[i].EntryDate,
statusTransaction.TransactionList[i].StockNumber,
qtyList[i]);
entryList.Add(entryItem);
}
}
return result;
if (includeDetails)
{
// get the sku obj
var readSku = new Logic.Sku.GetSkuInfo();
readSku.IncludeConditionInfo = true;
readSku.IncludeProductInfo = true;
readSku.IncludeTaxCodeInfo = true;
var sku = readSku.BySkuNumber(statusTransaction.SkuNumber);
// get the status obj
var readStatus = new Data.Database.Stock.Status();
readStatus.StatusIds = new List<int> { statusTransaction.StockStatusId };
var status = readStatus.Read()[0];
return new Model.Stock.StatusBalance(status, sku, entryList);
}
else
{
return new Model.Stock.StatusBalance(statusTransaction.StockStatusId, statusTransaction.SkuNumber, entryList);
}
}
/// <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 repective balance of each</returns>
public Dictionary<string, int> GetSkuQuantity(int statusId)
{
return new Data.Database.Stock.ReadStatusBalance().ByStatusId(statusId);
}
}
}

View File

@@ -58,10 +58,10 @@ namespace bnhtrade.Core.Logic.Stock
var returnList = new List<(int StockJournalId, int Quantity)>();
// get balance of status and check for avaliable quantity
var statusBalance = new Logic.Stock.StatusBalance().GetBySku(skuNumber, creditStatusId);
var statusBalance = new Logic.Stock.StatusBalance().GetStatusBalance(skuNumber, creditStatusId);
if (statusBalance.GetAvaliableQuantity(entryDate) <= 0
|| (statusBalance.CheckAvaliableQuantity(quantity, entryDate) == false && reallocatePartialQuantity == false
|| (statusBalance.IsQuantityAvaliable(quantity, entryDate) == false && reallocatePartialQuantity == false
))
{
return returnList;
@@ -70,7 +70,7 @@ namespace bnhtrade.Core.Logic.Stock
// temp code start
// until use of stockId is designed out of application
var stockIdDictionary = new Dictionary<int, int>();
foreach (var item in statusBalance.ByDateList)
foreach (var item in statusBalance.StockList)
{
if (!stockIdDictionary.ContainsKey(item.StockNumber))
{
@@ -84,7 +84,7 @@ namespace bnhtrade.Core.Logic.Stock
//make the changes
using (TransactionScope scope = new TransactionScope())
{
foreach (var item in statusBalance.ByDateList)
foreach (var item in statusBalance.StockList)
{
if (quantity > item.Quantity)
{

View File

@@ -6,11 +6,11 @@ using System.Threading.Tasks;
namespace bnhtrade.Core.Logic.Stock
{
public class GetStatusTypeBalance
public class StatusTypeBalance
{
private Core.Data.Database.Stock.ReadStatusTypeBalance dbRead;
public GetStatusTypeBalance()
public StatusTypeBalance()
{
dbRead = new Data.Database.Stock.ReadStatusTypeBalance();
}
@@ -20,7 +20,7 @@ namespace bnhtrade.Core.Logic.Stock
/// </summary>
/// <param name="StatusTypeIdList">List of Status Type Ids</param>
/// <returns>Dictionary where SKU number is key and quantity is value</returns>
public Dictionary<string, int> BySku(List<int> StatusTypeIdList)
public Dictionary<string, int> GetByStatusTypeId(List<int> StatusTypeIdList)
{
return dbRead.ReadSku(StatusTypeIdList);
}

View File

@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
namespace bnhtrade.Core.Model.Account
{
public class Account
{
public Account(uint id, uint accountCode, string accountName, string description, string type, string basicType, int multiplier)
{
Id = id;
AccountCode = accountCode;
AccountName = accountName;
Description = description;
Type = type;
BasicType = basicType;
Multiplier = multiplier;
}
/// <summary>
/// Database record id
/// </summary>
public uint Id { get; private set; }
public uint AccountCode { get; private set; }
public string AccountName { get; private set; }
public string Description { get; private set; }
public string Type { get; private set; }
public string BasicType { get; private set; }
public int Multiplier { get; private set; }
}
}

View File

@@ -1,86 +0,0 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Model.Account
{
public class AccountCode : IValidatableObject
{
private int? accountCodeId;
[Required(), Range(1, int.MaxValue)]
public int AccountCodeId
{
get { return (int)accountCodeId; }
set { accountCodeId = value; }
}
[Required(), MaxLength(150)]
public string Title
{
get;
set;
}
[MaxLength(500)]
public string Description
{
get;
set;
}
[Required(), MaxLength(50)]
public string Type
{
get;
set;
}
[Required(), MaxLength(50)]
public string BasicType
{
get;
set;
}
public bool IsSetAccountCodeId
{
get { return accountCodeId != null; }
}
public bool IsSetTitle
{
get { return Title != null; }
}
public bool IsSetDescription
{
get { return Description != null; }
}
public bool IsSetType
{
get { return Type != null; }
}
public bool IsSetBasicType
{
get { return BasicType != null; }
}
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
{
var resultList = new List<ValidationResult>();
if (!IsSetAccountCodeId)
{
resultList.Add(new ValidationResult("Account Code is not set"));
}
return resultList;
}
}
}

View File

@@ -28,7 +28,7 @@ namespace bnhtrade.Core.Model.Account
decimal? UnitAmount { get; set; }
Model.Account.AccountCode AccountCode { get; set; }
Model.Account.Account AccountCode { get; set; }
Model.Account.TaxCodeInfo TaxCode { get; set; }
@@ -116,7 +116,7 @@ namespace bnhtrade.Core.Model.Account
public bool UnitAmountIsTaxExclusive { get; set; }
[Required()]
public Model.Account.AccountCode AccountCode
public Model.Account.Account AccountCode
{
get;
set;

View File

@@ -44,7 +44,7 @@ namespace bnhtrade.Core.Model.Account
}
[Required()]
public Model.Account.AccountCode DefaultAccountCode
public Model.Account.Account DefaultAccountCode
{
get;
set;
@@ -61,7 +61,6 @@ namespace bnhtrade.Core.Model.Account
{
var resultList = new List<ValidationResult>();
resultList.AddRange(DefaultAccountCode.Validate(validationContext));
resultList.AddRange(DefaultTaxCode.Validate(validationContext));
return resultList;

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Model.Stock
{
public class Status
{
public Status(int statusId, int? statusCode, string title, Model.Stock.StatusType type, string reference, int? foreignKeyId, bool isCreditOnly, bool isClosed, DateTime recordCreated)
{
StatusId = statusId;
StatusCode = statusCode;
StatusTitle = title;
StatusType = type;
Reference = reference;
ForeignKeyID = foreignKeyId;
IsCreditOnly = isCreditOnly;
IsClosed = isClosed;
RecordCreated = recordCreated;
}
public int StatusId { get; private set; }
public int? StatusCode { get; private set; }
public string StatusTitle { get; private set; }
public Model.Stock.StatusType StatusType { get; private set; }
public string Reference { get; private set; }
public int? ForeignKeyID { get; private set; }
public bool IsCreditOnly { get; private set; }
public bool IsClosed { get; private set; }
public DateTime RecordCreated { get; private set; }
}
}

View File

@@ -1,60 +1,150 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static bnhtrade.Core.Data.Database.Constants;
namespace bnhtrade.Core.Model.Stock
{
public class StatusBalance
{
public int StockStatusId { get; set; }
public string Sku { get; set; }
public List<ByDate> ByDateList { get; private set; } = new List<ByDate>();
public class ByDate
internal StatusBalance(int stockStatusId, string skuNumber, List<(DateTime EntryDate, int StockNumber, int Quantity)> stockList)
{
public DateTime EntryDate { get; set; }
public int StockNumber { get; set; }
public int Quantity { get; set; }
StockStatusId = stockStatusId;
SkuNumber = skuNumber;
AddStockList(stockList);
}
public void AddBalanceTransaction(DateTime entryDate, int stockNumber, int quantity)
internal StatusBalance(Model.Stock.Status status, Model.Sku.Sku sku, List<(DateTime EntryDate, int StockNumber, int Quantity)> stockList)
{
if (entryDate == new DateTime())
StockStatusId = status.StatusId;
Status = status;
SkuNumber = sku.SkuNumber;
Sku = sku;
AddStockList(stockList);
}
public int StockStatusId { get; private set; }
public Model.Stock.Status Status { get; private set; }
public bool IsSetStatus
{
get
{
return Status != null;
}
}
public string SkuNumber { get; private set; }
public Model.Sku.Sku Sku { get; private set; }
public bool IsSetSku
{
get
{
return Sku != null;
}
}
/// <summary>
/// A list of when each stock item(s) became available and the quanitity (which is not a running total).
/// </summary>
public List<Stock> StockList { get; private set; } = new List<Stock>();
public class Stock
{
public Stock(DateTime entryDate, int stockNumber, int quantity)
{
EntryDate = entryDate;
StockNumber = stockNumber;
Quantity = quantity;
}
public DateTime EntryDate { get; private set; }
public int StockNumber { get; private set; }
public int Quantity { get; private set; }
}
internal void AddDetail(Model.Stock.Status status, Model.Sku.Sku sku)
{
if (sku.SkuNumber != SkuNumber || status.StatusId != StockStatusId)
{
throw new ArgumentException("Invalid SKU or Status applied to class");
}
SkuNumber = sku.SkuNumber;
Sku = sku;
}
private void AddStockList(List<(DateTime EntryDate, int StockNumber, int Quantity)> stockList)
{
foreach (var stockItem in stockList)
{
if (stockItem.EntryDate == new DateTime())
{
throw new Exception("Entry date set to default value");
}
var item = new ByDate();
item.EntryDate = entryDate;
item.StockNumber = stockNumber;
item.Quantity = quantity;
var item = new Stock(stockItem.EntryDate, stockItem.StockNumber, stockItem.Quantity);
if (ByDateList == null) { ByDateList = new List<ByDate>(); }
if (ByDateList.Count == 0 || ByDateList.Last().EntryDate <= item.EntryDate)
if (StockList.Count == 0 || StockList.Last().EntryDate <= item.EntryDate)
{
ByDateList.Add(item);
StockList.Add(item);
}
else
{
for (int i = 0; i < ByDateList.Count; i++)
for (int i = 0; i < StockList.Count; i++)
{
if (entryDate <= ByDateList[i].EntryDate)
if (stockItem.EntryDate <= StockList[i].EntryDate)
{
i++;
ByDateList.Insert(i, item);
StockList.Insert(i, item);
}
}
}
}
}
public bool CheckAvaliableQuantity(int quantity, DateTime onDateTime)
public int GetAvaliableQuantity()
{
if (!StockList.Any())
{
return 0;
}
int qty = 0;
for (int i = 0; i < StockList.Count; i++)
{
qty += StockList[i].Quantity;
}
return qty;
}
public int GetAvaliableQuantity(DateTime onDateTime)
{
if (!StockList.Any())
{
return 0;
}
int qty = 0;
for (int i = 0; i < StockList.Count; i++)
{
if (StockList[i].EntryDate <= onDateTime)
{
qty += StockList[i].Quantity;
}
}
return qty;
}
public bool IsQuantityAvaliable(int quantity, DateTime onDateTime)
{
if (GetAvaliableQuantity(onDateTime) < quantity)
{
@@ -65,42 +155,5 @@ namespace bnhtrade.Core.Model.Stock
return true;
}
}
public int GetAvaliableQuantity()
{
if (!ByDateList.Any())
{
return 0;
}
int qty = 0;
for (int i = 0; i < ByDateList.Count; i++)
{
qty += ByDateList[i].Quantity;
}
return qty;
}
public int GetAvaliableQuantity(DateTime onDateTime)
{
if (!ByDateList.Any())
{
return 0;
}
int qty = 0;
for (int i = 0; i < ByDateList.Count; i++)
{
if (ByDateList[i].EntryDate <= onDateTime)
{
qty += ByDateList[i].Quantity;
}
}
return qty;
}
}
}

View File

@@ -10,7 +10,7 @@ namespace bnhtrade.Core.Model.Stock
{
public int StockStatusId { get; set; }
public string Sku { get; set; }
public string SkuNumber { get; set; }
public int Balance
{

View File

@@ -0,0 +1,26 @@
using bnhtrade.Core.Data.Database.Stock;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Model.Stock
{
public class StatusType
{
public StatusType(int stockStatusTypeId, string name, string foreignKeyType, string referenceType, Model.Account.Account account)
{
StockStatusTypeID = stockStatusTypeId;
StatusTypeName = name;
ForeignKeyType = foreignKeyType;
ReferenceType = referenceType;
Account = account;
}
public int StockStatusTypeID { get; private set; }
public string StatusTypeName { get; private set; }
public string ForeignKeyType { get; private set; }
public string ReferenceType { get; private set; }
public Model.Account.Account Account { get; private set; }
}
}

View File

@@ -0,0 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bnhtrade.Core.Test.Amazon
{
public class Amazon
{
public Amazon()
{
var stock = new Stock();
stock.GetReplenishmentList();
}
public class Stock
{
public void GetReplenishmentList()
{
var read = new bnhtrade.Core.Logic.Amazon.Fba.Stock();
var resut = read.GetReplenishmentList();
}
}
}
}

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using bnhtrade.Core.Data.Database.Stock;
namespace bnhtrade.Core.Test.Stock
{
@@ -11,8 +12,9 @@ namespace bnhtrade.Core.Test.Stock
public Stock()
{
// method you want to start here
ReadStockStatus();
}
public void ReadStatusBalance()
{
var result = new bnhtrade.Core.Data.Database.Stock.ReadStatusTransaction()
@@ -26,7 +28,15 @@ namespace bnhtrade.Core.Test.Stock
var atDate = new DateTime(2017, 02, 01, 21, 54, 30);
DateTime.TryParse("22/12/2017 16:35:58", out atDate);
var result = new Core.Logic.Stock.StatusBalance().GetBySku(sku, statusId);
var result = new Core.Logic.Stock.StatusBalance().GetStatusBalance(sku, statusId);
int i = 0;
}
public void StatusBalance2()
{
int statusId = 16; // Inventory, Reserved (Garage Stock)
var result = new Core.Logic.Stock.StatusBalance().GetSkuQuantity(statusId);
int i = result.Values.ToList().Sum();
int y = 0;
}
public void ReadStockId()
@@ -47,5 +57,14 @@ namespace bnhtrade.Core.Test.Stock
{
new bnhtrade.Core.Logic.Stock.SkuTransactionReconcile().UnReconcileTransaction(transactoinId);
}
public void ReadStockStatus()
{
var db = new Status();
db.StatusIds = new List<int> { 3, 4, 5, 6, 7, 11, 12, 13, 15 };
db.StatusTypeIds = new List<int> { 12, 5 };
var result = db.Read();
int i = 0;
}
}
}

View File

@@ -183,7 +183,10 @@
<Compile Include="Data\Database\Stock\InsertSkuTransactionType.cs" />
<Compile Include="Data\Database\Stock\JournalCrud.cs" />
<Compile Include="Data\Database\Stock\ReadStatusTypeBalance.cs" />
<Compile Include="Data\Database\Stock\Status.cs" />
<Compile Include="Data\Database\Stock\StatusType.cs" />
<Compile Include="Logic\Amazon\Fba\Fnsku.cs" />
<Compile Include="Logic\Amazon\Fba\Stock.cs" />
<Compile Include="Logic\Import\Amazon.cs" />
<Compile Include="Logic\Import\AmazonFbaInventoryAdjustment.cs" />
<Compile Include="Data\Database\Import\AmazonFbaInventoryAgeData.cs" />
@@ -234,7 +237,7 @@
<Compile Include="Logic\Product\AmazonEstimateFee.cs" />
<Compile Include="Logic\Product\GetProductInfo.cs" />
<Compile Include="Logic\Sku\GetSkuInfo.cs" />
<Compile Include="Logic\Stock\GetStatusTypeBalance.cs" />
<Compile Include="Logic\Stock\StatusTypeBalance.cs" />
<Compile Include="Logic\Stock\SkuTransactionImport.cs" />
<Compile Include="Logic\Stock\StatusBalance.cs" />
<Compile Include="Logic\Utilities\EasyMD5.cs" />
@@ -271,7 +274,7 @@
<Compile Include="Logic\Utilities\StringCheck.cs" />
<Compile Include="Logic\Utilities\AccountVat.cs" />
<Compile Include="Logic\Validate\SkuPriceInfo.cs" />
<Compile Include="Model\Account\AccountCode.cs" />
<Compile Include="Model\Account\Account.cs" />
<Compile Include="Model\Account\InvoiceHeader.cs" />
<Compile Include="Model\Account\Invoice.cs" />
<Compile Include="Model\Account\InvoiceLineItem.cs" />
@@ -303,8 +306,10 @@
<Compile Include="Model\Stock\SkuTransactionCreate.cs" />
<Compile Include="Model\Stock\SkuTransactionType.cs" />
<Compile Include="Model\Stock\SkuTransaction.cs" />
<Compile Include="Model\Stock\Status.cs" />
<Compile Include="Model\Stock\StatusBalance.cs" />
<Compile Include="Model\Stock\StatusTransaction.cs" />
<Compile Include="Model\Stock\StatusType.cs" />
<Compile Include="Test\Account\Account.cs" />
<Compile Include="Test\Amazon\SP-API\FulfillmentInboundV0.cs" />
<Compile Include="Test\Amazon\SP-API\Reports.cs" />
@@ -315,6 +320,7 @@
<Compile Include="Test\InboundShipmentInfo.cs" />
<Compile Include="Logic\AmazonFBAInbound\ShipmentAddByFNSKU.cs" />
<Compile Include="Test\InboundShipmentInfoSync.cs" />
<Compile Include="Test\Amazon\Amazon.cs" />
<Compile Include="Test\Logic\Export.cs" />
<Compile Include="Test\Sku\Sku.cs" />
<Compile Include="Test\SQLLoop.cs" />

View File

@@ -264,8 +264,8 @@ namespace bnhtradeScheduledTasks
Console.WriteLine("<5> Test Logic");
Console.WriteLine("<6> Test SKU");
Console.WriteLine("<7> Test Stock");
Console.WriteLine("<8> Test Amazon SP-API Report");
Console.WriteLine("<9> ");
Console.WriteLine("<8> Test Amazon SP-API");
Console.WriteLine("<9> Test Amazon");
Console.WriteLine("<a> Test SP-API Fulfilment Inbound");
Console.WriteLine();
Console.WriteLine("<0> Back");
@@ -360,6 +360,7 @@ namespace bnhtradeScheduledTasks
{
Console.Clear();
new bnhtrade.Core.Test.Amazon.Amazon();
Console.WriteLine("Nothing to do here");