mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-23 08:17:15 +00:00
pull master into branch
* Migrated projects to dotnet8 migrated all projects over to .net8 incomplete feature for gui shipments * Amazon inventory ledger testing and implementation Tested what I can until more data for the Amazon Ledger Detail table comes in * amazon settlement amounts now set to tax inclusive when exporting to invoice * Some updates to the COM lib to attempt to get it to work on .net 8. Unfinished, porting all Access functions over to vs instead * feature exchange rate update automation Automated downloading exchange rates from HMRC and updating the database. Added function call to the console and form applications. Also added a form to show the console output in form application.
This commit is contained in:
@@ -162,7 +162,7 @@ namespace bnhtrade.Core.Data.Database.Account
|
||||
// currency conversion
|
||||
if (currencyCode != "GBP")
|
||||
{
|
||||
amount = new Data.Database.Account.Currency().CurrencyConvertToGbp(currencyCode, amount, entryDate);
|
||||
amount = new Logic.Account.Currency().CurrencyConvertToGbp(currencyCode, amount, entryDate);
|
||||
}
|
||||
|
||||
// ensure decimal is rounded
|
||||
|
||||
@@ -1,26 +1,25 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Collections;
|
||||
using FikaAmazonAPI.AmazonSpApiSDK.Models.FulfillmentOutbound;
|
||||
using System.Data;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Account
|
||||
{
|
||||
internal class Currency : Connection
|
||||
{
|
||||
public decimal CurrencyConvertToGbp(string currencyCode, decimal amount, DateTime conversionDate)
|
||||
/// <summary>
|
||||
/// Returns excahnge rate, in decimal format, for a given currency and datetime
|
||||
/// </summary>
|
||||
/// <param name="currencyCode">currency code</param>
|
||||
/// <param name="date">dat and time</param>
|
||||
/// <returns></returns>
|
||||
public decimal? ReadExchangeRate(string currencyCode, DateTime date)
|
||||
{
|
||||
if (currencyCode == "GBP" || amount == 0M)
|
||||
{
|
||||
return amount;
|
||||
}
|
||||
|
||||
if (currencyCode.Length != 3)
|
||||
{
|
||||
throw new Exception("Invalid currency code '" + currencyCode + "'");
|
||||
}
|
||||
|
||||
using (SqlConnection sqlConn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
sqlConn.Open();
|
||||
@@ -32,145 +31,210 @@ namespace bnhtrade.Core.Data.Database.Account
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
cmd.Parameters.AddWithValue("@conversionDate", conversionDate);
|
||||
cmd.Parameters.AddWithValue("@conversionDate", date);
|
||||
|
||||
object result = cmd.ExecuteScalar();
|
||||
if (result != null)
|
||||
{
|
||||
return amount / Convert.ToDecimal(result);
|
||||
}
|
||||
}
|
||||
|
||||
// return reason for no record found
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT CurrencyUnitsPerGBP
|
||||
FROM tblAccountExchangeRate
|
||||
WHERE CurrencyCode=@currencyCode
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
|
||||
object result = cmd.ExecuteScalar();
|
||||
if (result == null)
|
||||
{
|
||||
throw new Exception("Currency code '" + currencyCode + "' does not exist in Exchange Rate table");
|
||||
return Convert.ToDecimal(result);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Date range for " + currencyCode + " " + conversionDate.ToShortDateString() + " " +
|
||||
conversionDate.ToLongTimeString() + "' does not exist in Exchange Rate table");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int CurrencyExchangeRateInsert(int exchangeRateSource, string currencyCode,
|
||||
decimal currencyUnitsPerGbp, DateTime periodStart, DateTime periodEnd, bool checkOverride = false)
|
||||
public List<Model.Account.CurrencyExchangeRate> ReadExchangeRate(List<Model.Account.CurrencyCode> currencyCodeList = null, DateTime date = default(DateTime))
|
||||
{
|
||||
throw new NotImplementedException("Complete, but untested");
|
||||
|
||||
var returnList = new List<Model.Account.CurrencyExchangeRate>();
|
||||
|
||||
string sql = @"
|
||||
SELECT AccountEchangeRateID, ExchangeRateSource, CurrencyCode, CurrencyUnitsPerGBP, StartDate, EndDate
|
||||
FROM tblAccountExchangeRate
|
||||
WHERE 1=1 ";
|
||||
|
||||
if (date != default(DateTime))
|
||||
{
|
||||
sql = sql + " AND (@dateTime >= StartDate AND @dateTime < endDate) ";
|
||||
}
|
||||
|
||||
var sqlWhere = new Data.Database.SqlWhereBuilder();
|
||||
|
||||
// create string list
|
||||
List<string> stringList = new List<string>();
|
||||
if (currencyCodeList != null)
|
||||
{
|
||||
stringList = currencyCodeList.ConvertAll(f => f.ToString());
|
||||
if (stringList.Any())
|
||||
{
|
||||
sqlWhere.In("CurrencyCode", stringList, "AND");
|
||||
}
|
||||
}
|
||||
|
||||
sql = sql + sqlWhere.SqlWhereString;
|
||||
|
||||
using (SqlConnection sqlConn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
sqlConn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(sql))
|
||||
{
|
||||
if (date != default(DateTime))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@dateTime", date);
|
||||
}
|
||||
if (stringList.Any())
|
||||
{
|
||||
sqlWhere.AddParametersToSqlCommand(cmd);
|
||||
}
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
int id = reader.GetInt32(0);
|
||||
int exchangeRateSource = reader.GetInt32(1);
|
||||
string currencyCodeString = reader.GetString(2);
|
||||
decimal currencyUnitsPerGBP = reader.GetDecimal(3);
|
||||
DateTime startDate = DateTime.SpecifyKind( reader.GetDateTime(4), DateTimeKind.Utc);
|
||||
DateTime endDate = DateTime.SpecifyKind(reader.GetDateTime(5), DateTimeKind.Utc);
|
||||
|
||||
|
||||
// convert string to enum
|
||||
if (Enum.TryParse(currencyCodeString, out Model.Account.CurrencyCode currencyCode) == false)
|
||||
{
|
||||
throw new Exception("Failed converting database string to enum");
|
||||
}
|
||||
|
||||
var item = new Model.Account.CurrencyExchangeRate(
|
||||
currencyCode
|
||||
, exchangeRateSource
|
||||
, startDate
|
||||
, endDate
|
||||
);
|
||||
|
||||
returnList.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
public List<Model.Account.CurrencyExchangeRate> ReadExchangeRateLatest(List<Model.Account.CurrencyCode> currencyCodeList = null)
|
||||
{
|
||||
var returnList = new List<Model.Account.CurrencyExchangeRate>();
|
||||
|
||||
string sql = @"
|
||||
SELECT t1.AccountExchangeRateID, t1.ExchangeRateSource, t1.CurrencyCode, t1.CurrencyUnitsPerGBP, t1.StartDate, t1.EndDate,
|
||||
t2.maxdate
|
||||
FROM tblAccountExchangeRate AS t1
|
||||
INNER JOIN
|
||||
(SELECT max(StartDate) AS maxdate,
|
||||
CurrencyCode
|
||||
FROM tblAccountExchangeRate
|
||||
GROUP BY CurrencyCode
|
||||
";
|
||||
|
||||
// add any filters
|
||||
var sqlWhere = new Data.Database.SqlWhereBuilder();
|
||||
var codeStringList = new List<string>();
|
||||
if (currencyCodeList != null && currencyCodeList.Any() == true)
|
||||
{
|
||||
// convert to string list
|
||||
foreach ( var currencyCode in currencyCodeList)
|
||||
{
|
||||
codeStringList.Add(currencyCode.ToString());
|
||||
}
|
||||
|
||||
// add to where statement
|
||||
sqlWhere.In("CurrencyCode", codeStringList, "HAVING");
|
||||
sql = sql + sqlWhere.SqlWhereString;
|
||||
}
|
||||
|
||||
sql = sql + @" ) AS t2
|
||||
ON t1.CurrencyCode = t2.CurrencyCode
|
||||
AND t1.StartDate = t2.maxdate
|
||||
ORDER BY t1.CurrencyCode;";
|
||||
|
||||
//query db
|
||||
using (SqlConnection sqlConn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
sqlConn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(sql, sqlConn))
|
||||
{
|
||||
if (codeStringList.Any())
|
||||
{
|
||||
sqlWhere.AddParametersToSqlCommand(cmd);
|
||||
}
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
while (reader.Read())
|
||||
{
|
||||
int id = reader.GetInt32(0);
|
||||
int exchangeRateSource = reader.GetInt32(1);
|
||||
string currencyCodeString = reader.GetString(2);
|
||||
decimal currencyUnitsPerGBP = reader.GetDecimal(3);
|
||||
DateTime startDate = DateTime.SpecifyKind(reader.GetDateTime(4), DateTimeKind.Utc);
|
||||
DateTime endDate = DateTime.SpecifyKind(reader.GetDateTime(5), DateTimeKind.Utc);
|
||||
|
||||
|
||||
// convert string to enum
|
||||
if (Enum.TryParse(currencyCodeString, out Model.Account.CurrencyCode currencyCode) == false)
|
||||
{
|
||||
throw new Exception("Failed converting database string to enum");
|
||||
}
|
||||
|
||||
var item = new Model.Account.CurrencyExchangeRate(
|
||||
currencyCode
|
||||
, exchangeRateSource
|
||||
, startDate
|
||||
, endDate
|
||||
);
|
||||
|
||||
returnList.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return returnList;
|
||||
}
|
||||
|
||||
public int InsertExchangeRate(int exchangeRateSource, Model.Account.CurrencyCode currencyCode,
|
||||
decimal currencyUnitsPerGbp, DateTime periodStartUtc, DateTime periodEnd, bool checkOverride = false)
|
||||
{
|
||||
// checks
|
||||
if (periodStartUtc.Kind != DateTimeKind.Utc || periodEnd.Kind != DateTimeKind.Utc)
|
||||
{
|
||||
throw new FormatException("Currency date time kind must be UTC");
|
||||
}
|
||||
|
||||
currencyUnitsPerGbp = decimal.Round(currencyUnitsPerGbp, 4);
|
||||
periodStart = DateTime.SpecifyKind(periodStart, DateTimeKind.Utc);
|
||||
periodEnd = DateTime.SpecifyKind(periodEnd, DateTimeKind.Utc);
|
||||
|
||||
// CHECKS
|
||||
// HMRC source only
|
||||
if (exchangeRateSource != 1)
|
||||
{
|
||||
throw new Exception("Function does not currently accept exchange rates from sources other than HMRC");
|
||||
}
|
||||
// currency code upper case only
|
||||
currencyCode = currencyCode.ToUpper();
|
||||
if (currencyCode.Length != 3)
|
||||
{
|
||||
throw new Exception("Invalid currency code '" + currencyCode + "'");
|
||||
}
|
||||
|
||||
if (periodEnd <= periodStart)
|
||||
if (periodEnd <= periodStartUtc)
|
||||
{
|
||||
throw new Exception("Invalid date period.");
|
||||
}
|
||||
|
||||
if (checkOverride == false && (periodEnd - periodStart).Days > 31)
|
||||
if (checkOverride == false && (periodEnd - periodStartUtc).Days > 31)
|
||||
{
|
||||
throw new Exception("Date period is greater than 31 days.");
|
||||
}
|
||||
|
||||
// retirve previous data
|
||||
// make the insert
|
||||
DateTime? periodEndLast = null;
|
||||
using (SqlConnection sqlConn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
sqlConn.Open();
|
||||
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT Max(tblAccountExchangeRate.EndDate) AS MaxOfEndDate
|
||||
FROM tblAccountExchangeRate
|
||||
WHERE (((tblAccountExchangeRate.CurrencyCode) = @currencyCode))
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
|
||||
object obj = cmd.ExecuteScalar();
|
||||
|
||||
// currency code not existing
|
||||
if (obj == DBNull.Value && checkOverride == false)
|
||||
{
|
||||
throw new Exception("Currency code '" + currencyCode + "' does not exist in table");
|
||||
}
|
||||
// currency code exists
|
||||
else
|
||||
{
|
||||
periodEndLast = DateTime.SpecifyKind(Convert.ToDateTime(obj), DateTimeKind.Utc);
|
||||
|
||||
if (periodStart != periodEndLast)
|
||||
{
|
||||
throw new Exception("Invalid period start date -- must equal previous period end-date.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// retrive previous exchange rate and check
|
||||
decimal currencyUnitsPerGbpLast = 0;
|
||||
if (periodEndLast != null)
|
||||
{
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT tblAccountExchangeRate.AccountExchangeRateID, tblAccountExchangeRate.CurrencyUnitsPerGBP
|
||||
FROM tblAccountExchangeRate
|
||||
WHERE (tblAccountExchangeRate.EndDate = @periodEndLast)
|
||||
AND (CurrencyCode = @currencyCode);
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@periodEndLast", periodEndLast);
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
|
||||
using (var reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
currencyUnitsPerGbpLast = reader.GetDecimal(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Error that shouldn't happen! Check code @ 5129f3e6-2f7e-4883-bc73-b317d8fa4050");
|
||||
}
|
||||
// error if >1 line
|
||||
if (reader.Read())
|
||||
{
|
||||
string errText = "Multiple lines in currency exchange table for '" + currencyCode + "' where [EndDate]=" + periodEndLast.ToString();
|
||||
new Logic.Log.LogEvent().LogError(errText);
|
||||
throw new Exception(errText);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// check difference between current and previous exchange rates isn't too great
|
||||
if (checkOverride == false &&
|
||||
(currencyUnitsPerGbpLast > (currencyUnitsPerGbp * 1.05m) || currencyUnitsPerGbpLast < (currencyUnitsPerGbp * 0.95m))
|
||||
)
|
||||
{
|
||||
throw new Exception("Difference between supplied and previous exchange rates is greater than 5%");
|
||||
}
|
||||
|
||||
// MAKE THE INSERT
|
||||
int recordId = 0;
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
INSERT INTO tblAccountExchangeRate (ExchangeRateSource, CurrencyCode, CurrencyUnitsPerGBP, StartDate, EndDate)
|
||||
@@ -179,9 +243,9 @@ namespace bnhtrade.Core.Data.Database.Account
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@exchangeRateSource", exchangeRateSource);
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode);
|
||||
cmd.Parameters.AddWithValue("@currencyCode", currencyCode.ToString());
|
||||
cmd.Parameters.AddWithValue("@currencyUnitsPerGbp", currencyUnitsPerGbp);
|
||||
cmd.Parameters.AddWithValue("@periodStart", periodStart);
|
||||
cmd.Parameters.AddWithValue("@periodStart", periodStartUtc);
|
||||
cmd.Parameters.AddWithValue("@periodEnd", periodEnd);
|
||||
|
||||
recordId = (int)cmd.ExecuteScalar();
|
||||
|
||||
@@ -8,14 +8,22 @@ namespace bnhtrade.Core.Data.Database
|
||||
{
|
||||
public static class Constants
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the date bnhtrade started trading, UK time (datetime kind unspecified).
|
||||
/// </summary>
|
||||
/// <returns>The UK date and time the business started (datetime kind unspecified)</returns>
|
||||
public static DateTime GetBusinessStartUk()
|
||||
{
|
||||
return new Logic.Utilities.DateTime().ConvertUtcToUk(GetBusinessStartUtc());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the date bnhtrade started trading.
|
||||
/// Gets the date bnhtrade started trading, as UTC time
|
||||
/// </summary>
|
||||
/// <returns>Date and time</returns>
|
||||
/// <returns>The UTC date and time the business started (datetime kind UTC)</returns>
|
||||
public static DateTime GetBusinessStartUtc()
|
||||
{
|
||||
DateTime businessStart = new DateTime(2014, 09, 01);
|
||||
DateTime businessStart = new DateTime(2014, 08, 31, 23, 00, 00); // 2014-09-01 uk date time
|
||||
return DateTime.SpecifyKind(businessStart, DateTimeKind.Utc);
|
||||
}
|
||||
|
||||
|
||||
@@ -354,8 +354,8 @@ namespace bnhtrade.Core.Data.Database.Import
|
||||
item.Asin = reader.GetString(8);
|
||||
item.Condition = reader.GetString(9);
|
||||
item.CurrencyUnit = reader.GetString(10);
|
||||
item.AmountPerUnit = reader.GetInt32(11);
|
||||
item.AmountTotal = reader.GetInt32(12);
|
||||
item.AmountPerUnit = reader.GetDecimal(11);
|
||||
item.AmountTotal = reader.GetDecimal(12);
|
||||
item.QuantityReimbursedCash = reader.GetInt32(13);
|
||||
item.QuantityReimbursedInventory = reader.GetInt32(14);
|
||||
item.QuantityReimbursedTotal = reader.GetInt32(15);
|
||||
|
||||
@@ -146,16 +146,16 @@ namespace bnhtrade.Core.Data.Database.Import
|
||||
sqlCommand.Parameters.AddWithValue("@settlementId", settlementRef);
|
||||
}
|
||||
|
||||
var parseDateTime = new Core.Logic.Utilities.DateTimeParse();
|
||||
var parseDateTime = new Core.Logic.Utilities.DateTime();
|
||||
|
||||
if (indexSettlementStartDate == -1 || items[indexSettlementStartDate].Length == 0) { sqlCommand.Parameters.AddWithValue("@settlementStartDate", DBNull.Value); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@settlementStartDate", parseDateTime.ParseMwsReportDateTime(items[indexSettlementStartDate])); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@settlementStartDate", parseDateTime.ParseIsoDateTimeString(items[indexSettlementStartDate])); }
|
||||
|
||||
if (indexSettlementEndDate == -1 || items[indexSettlementEndDate].Length == 0) { sqlCommand.Parameters.AddWithValue("@settlementEndDate", DBNull.Value); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@settlementEndDate", parseDateTime.ParseMwsReportDateTime(items[indexSettlementEndDate])); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@settlementEndDate", parseDateTime.ParseIsoDateTimeString(items[indexSettlementEndDate])); }
|
||||
|
||||
if (indexDepositDate == -1 || items[indexDepositDate].Length == 0) { sqlCommand.Parameters.AddWithValue("@depositDate", DBNull.Value); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@depositDate", parseDateTime.ParseMwsReportDateTime(items[indexDepositDate])); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@depositDate", parseDateTime.ParseIsoDateTimeString(items[indexDepositDate])); }
|
||||
|
||||
if (indexTotalAmount == -1 || items[indexTotalAmount].Length == 0) { sqlCommand.Parameters.AddWithValue("@totalAmount", DBNull.Value); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@settlementotalAmounttId", settlementAmount); }
|
||||
@@ -245,7 +245,7 @@ namespace bnhtrade.Core.Data.Database.Import
|
||||
else { sqlCommand.Parameters.AddWithValue("@FulfillmentRef", items[indexFulfillmentId]); }
|
||||
|
||||
if (indexPostedDateTime == -1 || items[indexPostedDateTime].Length == 0) { sqlCommand.Parameters.AddWithValue("@PostedDateTimeUTC", DBNull.Value); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@PostedDateTimeUTC", new Logic.Utilities.DateTimeParse().ParseMwsReportDateTime(items[indexPostedDateTime])); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@PostedDateTimeUTC", new Logic.Utilities.DateTime().ParseIsoDateTimeString(items[indexPostedDateTime])); }
|
||||
|
||||
if (indexOrderItemCode == -1 || items[indexOrderItemCode].Length == 0) { sqlCommand.Parameters.AddWithValue("@OrderItemCode", DBNull.Value); }
|
||||
else { sqlCommand.Parameters.AddWithValue("@OrderItemCode", long.Parse(items[indexOrderItemCode])); }
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data.SqlClient;
|
||||
using Microsoft.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
@@ -53,7 +54,19 @@ namespace bnhtrade.Core.Data.Database
|
||||
ParameterList = new Dictionary<string, object>();
|
||||
}
|
||||
|
||||
public void AddParametersToSqlCommand(SqlCommand cmd)
|
||||
// delete this once all references use the new Microsoft.Data.SqlClient
|
||||
public void AddParametersToSqlCommand(System.Data.SqlClient.SqlCommand cmd)
|
||||
{
|
||||
if (ParameterList != null)
|
||||
{
|
||||
foreach (var item in ParameterList)
|
||||
{
|
||||
cmd.Parameters.AddWithValue(item.Key, item.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void AddParametersToSqlCommand(Microsoft.Data.SqlClient.SqlCommand cmd)
|
||||
{
|
||||
if (ParameterList != null)
|
||||
{
|
||||
|
||||
@@ -6,23 +6,19 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
{
|
||||
public class InsertSkuTransactionType : Connection
|
||||
{
|
||||
/// <summary>
|
||||
/// The insert command will not participate in any amibent transaction scope. Default is true.
|
||||
/// </summary>
|
||||
public bool SuppressTransactionScope { get; set; } = true;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new SKU Transaction Type
|
||||
/// </summary>
|
||||
/// <param name="stockJournalTypeId"></param>
|
||||
/// <param name="skuTransactionCode"></param>
|
||||
/// <param name="transactionScopeSuppress">The insert command will not participate in any amibent transaction scope</param>
|
||||
/// <returns>ID of the created record</returns>
|
||||
/// <exception cref="Exception"></exception>
|
||||
public int Create(string skuTransactionCode, int stockJournalTypeId)
|
||||
public int Create(string skuTransactionCode, int stockJournalTypeId, bool transactionScopeSuppress = false)
|
||||
{
|
||||
int id;
|
||||
var scopeOption = new TransactionScopeOption();
|
||||
if (SuppressTransactionScope)
|
||||
if (transactionScopeSuppress)
|
||||
{
|
||||
scopeOption = TransactionScopeOption.Suppress;
|
||||
}
|
||||
@@ -32,12 +28,13 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
}
|
||||
|
||||
using (TransactionScope scope = new TransactionScope(scopeOption))
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
// insert new and retrive new value
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
// insert new and retrive new value
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
INSERT INTO tblStockSkuTransactionType
|
||||
( TypeName, StockJournalTypeID, TypeCode )
|
||||
OUTPUT
|
||||
@@ -45,19 +42,20 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
VALUES
|
||||
( @typeName, @stockJournalTypeId, @typeCode );
|
||||
", conn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@typeName", skuTransactionCode);
|
||||
cmd.Parameters.AddWithValue("@typeCode", skuTransactionCode);
|
||||
cmd.Parameters.AddWithValue("@stockJournalTypeId", stockJournalTypeId);
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@typeName", skuTransactionCode);
|
||||
cmd.Parameters.AddWithValue("@stockJournalTypeId", stockJournalTypeId);
|
||||
cmd.Parameters.AddWithValue("@typeCode", skuTransactionCode);
|
||||
|
||||
object obj = cmd.ExecuteScalar();
|
||||
object obj = cmd.ExecuteScalar();
|
||||
|
||||
if (obj == null || obj == DBNull.Value)
|
||||
throw new Exception("tblStockSkuTransactionType insert operation returned null");
|
||||
if (obj == null || obj == DBNull.Value)
|
||||
throw new Exception("tblStockSkuTransactionType insert operation returned null");
|
||||
|
||||
id = (int)obj;
|
||||
id = (int)obj;
|
||||
}
|
||||
scope.Complete();
|
||||
}
|
||||
scope.Complete();
|
||||
}
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -4,7 +4,9 @@ using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
using Dapper;
|
||||
using static System.Formats.Asn1.AsnWriter;
|
||||
|
||||
namespace bnhtrade.Core.Data.Database.Stock
|
||||
{
|
||||
@@ -14,81 +16,10 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Depriciated, delete when not required by other code
|
||||
/// </summary>
|
||||
/// <param name="sqlConnectionString"></param>
|
||||
/// <param name="typeCode"></param>
|
||||
/// <returns></returns>
|
||||
public int GetTypeId(string typeCode)
|
||||
private List<Model.Stock.SkuTransactionType> Execute(string sqlWhere, DynamicParameters param, bool transactionScopeSuppress = false)
|
||||
{
|
||||
/* GetStockTransactionTypeId return meanings
|
||||
* >0 use as the TypeId when inserting transaction
|
||||
* 0 Skip transpose, type doesn't exist or is new (not reviewed yet)
|
||||
* -1 Type import/transpose is disabled, IsProcessed=TRUE StockTransactionID=NULL */
|
||||
var returnList = new List<Model.Stock.SkuTransactionType>();
|
||||
|
||||
// old optional parameters
|
||||
// , bool onNewReturnId = false, bool onNewDisableInsert = false
|
||||
|
||||
if (typeCode.Length == 0)
|
||||
{
|
||||
throw new Exception("Empty match string passed to method");
|
||||
}
|
||||
using (SqlConnection sqlConn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
sqlConn.Open();
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT
|
||||
StockSkuTransactionTypeID,
|
||||
IsNewReviewRequired,
|
||||
TransactionImportEnabled
|
||||
FROM
|
||||
tblStockSkuTransactionType
|
||||
WHERE
|
||||
TypeCode=@typeCode;
|
||||
", sqlConn))
|
||||
{
|
||||
cmd.Parameters.AddWithValue("@typeCode", typeCode);
|
||||
|
||||
using (SqlDataReader reader = cmd.ExecuteReader())
|
||||
{
|
||||
if (reader.Read())
|
||||
{
|
||||
int transactionTypeId = reader.GetInt32(0);
|
||||
bool isNew = reader.GetBoolean(1);
|
||||
bool importEnabled = reader.GetBoolean(2);
|
||||
|
||||
if (isNew == true)
|
||||
{
|
||||
// return 0 and 'skip' item
|
||||
return 0;
|
||||
}
|
||||
else if (importEnabled == false)
|
||||
{
|
||||
// mark IsProcessed=true and leave transactionId=null
|
||||
return -1;
|
||||
}
|
||||
else if (transactionTypeId > 0)
|
||||
{
|
||||
|
||||
return transactionTypeId;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("Sku TransactionTypeId lookup method failed, is one of the 'enabled' boolean on table set to null?");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private List<Model.Stock.SkuTransactionType> Execute(string sqlWhere, DynamicParameters param)
|
||||
{
|
||||
string sql = @"
|
||||
SELECT tblStockSkuTransactionType.StockSkuTransactionTypeID AS TypeId
|
||||
,tblStockSkuTransactionType.TypeName
|
||||
@@ -117,13 +48,25 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
|
||||
using (SqlConnection conn = new SqlConnection(SqlConnectionString))
|
||||
{
|
||||
conn.Open();
|
||||
|
||||
return conn.Query<Model.Stock.SkuTransactionType>(sql, param).ToList();
|
||||
if (transactionScopeSuppress)
|
||||
{
|
||||
using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress))
|
||||
{
|
||||
conn.Open();
|
||||
returnList = conn.Query<Model.Stock.SkuTransactionType>(sql, param).ToList();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
conn.Open();
|
||||
returnList = conn.Query<Model.Stock.SkuTransactionType>(sql, param).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
return returnList;
|
||||
}
|
||||
|
||||
public List<Model.Stock.SkuTransactionType> ByTypeCode(List<string> typeCodeList)
|
||||
public List<Model.Stock.SkuTransactionType> ByTypeCode(List<string> typeCodeList, bool transactionScopeSuppress = false)
|
||||
{
|
||||
typeCodeList.RemoveAll(string.IsNullOrWhiteSpace);
|
||||
|
||||
@@ -138,7 +81,7 @@ namespace bnhtrade.Core.Data.Database.Stock
|
||||
return Execute(sqlWhere, param);
|
||||
}
|
||||
|
||||
public List<Model.Stock.SkuTransactionType> ByTypeName(List<string> typeName)
|
||||
public List<Model.Stock.SkuTransactionType> ByTypeName(List<string> typeName, bool transactionScopeSuppress = false)
|
||||
{
|
||||
typeName.RemoveAll(string.IsNullOrWhiteSpace);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user