diff --git a/src/bnhtrade.Core/Data/Database/Stock/InsertSkuTransactionType.cs b/src/bnhtrade.Core/Data/Database/Stock/InsertSkuTransactionType.cs index 83d483d..4a6236f 100644 --- a/src/bnhtrade.Core/Data/Database/Stock/InsertSkuTransactionType.cs +++ b/src/bnhtrade.Core/Data/Database/Stock/InsertSkuTransactionType.cs @@ -6,23 +6,19 @@ namespace bnhtrade.Core.Data.Database.Stock { public class InsertSkuTransactionType : Connection { - /// - /// The insert command will not participate in any amibent transaction scope. Default is true. - /// - public bool SuppressTransactionScope { get; set; } = true; - /// /// Creates a new SKU Transaction Type /// /// /// + /// The insert command will not participate in any amibent transaction scope /// ID of the created record /// - 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; } diff --git a/src/bnhtrade.Core/Data/Database/Stock/ReadSkuTransactionType.cs b/src/bnhtrade.Core/Data/Database/Stock/ReadSkuTransactionType.cs index 8f4cedd..df2d71d 100644 --- a/src/bnhtrade.Core/Data/Database/Stock/ReadSkuTransactionType.cs +++ b/src/bnhtrade.Core/Data/Database/Stock/ReadSkuTransactionType.cs @@ -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 { } - /// - /// Depriciated, delete when not required by other code - /// - /// - /// - /// - public int GetTypeId(string typeCode) + private List 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(); - // 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 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(sql, param).ToList(); + if (transactionScopeSuppress) + { + using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Suppress)) + { + conn.Open(); + returnList = conn.Query(sql, param).ToList(); + } + } + else + { + conn.Open(); + returnList = conn.Query(sql, param).ToList(); + } } + + return returnList; } - public List ByTypeCode(List typeCodeList) + public List ByTypeCode(List typeCodeList, bool transactionScopeSuppress = false) { typeCodeList.RemoveAll(string.IsNullOrWhiteSpace); @@ -138,7 +81,7 @@ namespace bnhtrade.Core.Data.Database.Stock return Execute(sqlWhere, param); } - public List ByTypeName(List typeName) + public List ByTypeName(List typeName, bool transactionScopeSuppress = false) { typeName.RemoveAll(string.IsNullOrWhiteSpace); diff --git a/src/bnhtrade.Core/Logic/Stock/SkuTransactionCrud.cs b/src/bnhtrade.Core/Logic/Stock/SkuTransactionCrud.cs index 1bf36d8..4e8637d 100644 --- a/src/bnhtrade.Core/Logic/Stock/SkuTransactionCrud.cs +++ b/src/bnhtrade.Core/Logic/Stock/SkuTransactionCrud.cs @@ -126,9 +126,6 @@ namespace bnhtrade.Core.Logic.Stock /// public int Create(Model.Stock.SkuTransactionCreate skuTransaction) { - // need to add function to check if transaction type exists, if not, create a new one. - throw new NotImplementedException(); - if (skuTransaction == null) { throw new Exception(err + "Object was null"); diff --git a/src/bnhtrade.Core/Logic/Stock/SkuTransactionImport.cs b/src/bnhtrade.Core/Logic/Stock/SkuTransactionImport.cs index e41af10..1b1faef 100644 --- a/src/bnhtrade.Core/Logic/Stock/SkuTransactionImport.cs +++ b/src/bnhtrade.Core/Logic/Stock/SkuTransactionImport.cs @@ -66,33 +66,26 @@ namespace bnhtrade.Core.Logic.Stock } /// - /// Imports/Transaposes all data required for reconcilation, into the sku transaction table. + /// Imports/Transaposes all required data into sku transaction table for reconcilation. /// public void ImportAll() { - bool inventoryLedgerDetail = false; - bool reimbursement = false; + string methodName = "'Import all' data to 'Stock SKU Transactions'"; - while (true) + log.LogInformation(methodName + " started..."); + + try { - try - { - if (true) - { - if (inventoryLedgerDetail == false) { inventoryLedgerDetail = true; ImportAmazonFbaLedgerDetail(); } - if (reimbursement == false) { reimbursement = true; ImportAmazonFbaReimbursement(); } - } - - break; - } - catch (Exception ex) - { - log.LogError( - "Exception caught running Importing amazon reports in the SKU transaction table, see for further details", - ex.ToString() - ); - } + ImportAmazonFbaLedgerDetail(); + ImportAmazonFbaReimbursement(); } + catch + { + log.LogError(methodName + " did not complete."); + throw; + } + + log.LogInformation(methodName + " complete."); } /// @@ -101,6 +94,8 @@ namespace bnhtrade.Core.Logic.Stock /// public void ImportAmazonFbaReimbursement() { + string methodName = "Import 'Amazon FBA Reimbursement' into 'Stock SKU Transactions'"; + //throw new NotImplementedException("Needs testing"); /* @@ -112,9 +107,9 @@ namespace bnhtrade.Core.Logic.Stock * and also the 'Cost of goods' amounts moved to the appropreate account id */ - log.LogInformation("Starting TransposeFbaRemovalOrderReport()"); - int transposeCount = 0; - int transposeSkip = 0; + log.LogInformation(methodName + " started..."); + int importedCount = 0; + int processedCount = 0; var dbAmznReport = new Data.Database.Import.AmazonFbaReimbursement(); var dbTransType = new Logic.Stock.SkuTransactionTypeCrud(); @@ -128,6 +123,8 @@ namespace bnhtrade.Core.Logic.Stock var transTypeCodeList = new List(); foreach (var item in importList) { + processedCount++; + transTypeCodeList.Add(ConstructTransactionTypeCode(item, false)); // any that reimburse inventory, will have two entries if(item.QuantityReimbursedInventory > 0) @@ -170,7 +167,7 @@ namespace bnhtrade.Core.Logic.Stock // don't go any further until the transaction-type has been reviewed/setup if (foundNewType) { - log.LogWarning("Cannot complete ImportAmazonFbaReimbursement, new 'Stock Trnasaction Type' found. Review required/"); + log.LogWarning(methodName + " unable to complete. New 'Stock Trnasaction Type' found. Review required/"); return; } @@ -189,7 +186,7 @@ namespace bnhtrade.Core.Logic.Stock if (transType.IsNewReviewRequired) { - throw new Exception("Fail safe: Buggy code, should not get here!"); + throw new Exception(methodName + " fail safe: Buggy code, should not get here!"); } else if (transType.TransactionImportEnabled) { @@ -214,7 +211,7 @@ namespace bnhtrade.Core.Logic.Stock if (transType.IsNewReviewRequired) { - throw new Exception("Fail safe: Buggy code, should not get here!"); + throw new Exception(methodName + " fail safe: Buggy code, should not get here!"); } else if (transType.TransactionImportEnabled) { @@ -233,20 +230,26 @@ namespace bnhtrade.Core.Logic.Stock } // update the amazon report table dbAmznReport.UpdateIsProcessed(item.FbaReimbursementReportID, true, transId); - transposeCount = transposeCount + 1; + importedCount++; } // drop out of loop scope.Complete(); } Console.Write("\r"); - log.LogInformation("ProcessFbaReimbursementData() complete, " + transposeCount + " total records transposed, " + transposeSkip + " records skipped."); + + log.LogInformation( + methodName + " complete. Records transferred/processed " + importedCount + "/" + processedCount + ); } catch (Exception ex) { - log.LogError("Exception catch, aborting ProcessFbaReimbursementData(), see detailed info. " - + transposeCount + " total records completed, " + transposeSkip + " records skipped.", ex.ToString()); - } + log.LogError( + methodName + " aborted due an exception, no records where modified." + , ex.ToString() + ); + throw; + } } /// @@ -255,12 +258,11 @@ namespace bnhtrade.Core.Logic.Stock /// public void ImportAmazonFbaLedgerDetail() { - //// Done but needs testing!! - //throw new NotImplementedException("Done but needs testing!!"); + string methodName = "Import 'Amazon FBA Ledger Detail' into 'Stock SKU Transactions'"; - log.LogInformation("Starting TransposeFbaAdustmentReport()"); - int transposeCount = 0; - int transposeSkip = 0; + log.LogInformation(methodName + " started"); + int transferredCount = 0; + int processedCount = 0; using (var scope = new TransactionScope()) { @@ -275,6 +277,8 @@ namespace bnhtrade.Core.Logic.Stock var transCodeToJournalTypeId = new Dictionary(); foreach (var item in reportDict) { + processedCount++; + // test for internal Amazon stuff that we don't care about and mark as processed if (item.Value.EventType == "WhseTransfers") { @@ -340,21 +344,25 @@ namespace bnhtrade.Core.Logic.Stock } } - // check for any new types codes, and add them if ther are - var dbTransType = new Logic.Stock.SkuTransactionTypeCrud(); - var transTypeList = dbTransType.GetByTypeCode(transCodeToJournalTypeId.Keys.ToList()); - - foreach ( var transType in transTypeList) + // check for any new types codes, remove existing from list and add remaing to db + using (TransactionScope scope2 = new TransactionScope(TransactionScopeOption.Suppress)) { - if (transCodeToJournalTypeId.ContainsKey(transType.Key)) + var dbTransType = new Logic.Stock.SkuTransactionTypeCrud(); + + var transTypeList = dbTransType.GetByTypeCode(transCodeToJournalTypeId.Keys.ToList()); + + foreach (var transType in transTypeList) { - transCodeToJournalTypeId.Remove(transType.Key); + if (transCodeToJournalTypeId.ContainsKey(transType.Key)) + { + transCodeToJournalTypeId.Remove(transType.Key); + } } - } - foreach (var newItem in transCodeToJournalTypeId) - { - dbTransType.Create(newItem.Key, newItem.Value); + foreach (var newItem in transCodeToJournalTypeId) + { + dbTransType.Create(newItem.Key, newItem.Value); + } } // finally, add the transction list to the table @@ -363,33 +371,28 @@ namespace bnhtrade.Core.Logic.Stock { int id = dbTransaction.Create(item); dbImport.UpdateIsProcessed((int)item.ForeignKey, id); + transferredCount++; } scope.Complete(); log.LogInformation( - "TransposeFbaAdustmentReport() complete, " + transposeCount + " total records transposed, " + transposeSkip + " records skipped." + methodName + " complete. Records transferred/processed " + transferredCount + "/" + processedCount ); - if (transposeSkip > 0) - { - log.LogInformation( - transposeSkip + " number records skipped during TransposeFbaAdustmentReport() operation." - ); - } } catch (Exception ex) { scope.Dispose(); log.LogError( - "Exception catch, aborting TransposeFbaAdustmentReport(), see detailed info. " - + transposeCount + " total records completed, " + transposeSkip + " records skipped." + methodName + " aborted, no records modified. See additional info exception details." , ex.ToString() ); + + throw; } } - return; } } } diff --git a/src/bnhtrade.Core/Logic/Stock/SkuTransactionTypeCrud.cs b/src/bnhtrade.Core/Logic/Stock/SkuTransactionTypeCrud.cs index 7192928..62e9352 100644 --- a/src/bnhtrade.Core/Logic/Stock/SkuTransactionTypeCrud.cs +++ b/src/bnhtrade.Core/Logic/Stock/SkuTransactionTypeCrud.cs @@ -25,15 +25,15 @@ namespace bnhtrade.Core.Logic.Stock /// /// Id for new record entry /// Transaction type code already exists - public int Create(string skuTransactionTypeCode, int stockJournalTypeId) + public int Create(string skuTransactionTypeCode, int stockJournalTypeId, bool transactionScopeSuppress = false) { //check to see if type already exists - var result = dbRead.ByTypeCode(new List { skuTransactionTypeCode }); + var result = dbRead.ByTypeCode(new List { skuTransactionTypeCode }, transactionScopeSuppress); if (result.Any()) throw new InvalidOperationException("Create SKU Transaction Type failed, typecode already exists failed"); // okay to proceed - int id = new Data.Database.Stock.InsertSkuTransactionType().Create(skuTransactionTypeCode, stockJournalTypeId); + int id = new Data.Database.Stock.InsertSkuTransactionType().Create(skuTransactionTypeCode, stockJournalTypeId, transactionScopeSuppress); return id; } @@ -42,14 +42,14 @@ namespace bnhtrade.Core.Logic.Stock /// /// /// The object, or null - public Model.Stock.SkuTransactionType GetByTypeCode(string typeCode) + public Model.Stock.SkuTransactionType GetByTypeCode(string typeCode, bool transactionScopeSuppress = false) { if (string.IsNullOrWhiteSpace(typeCode)) { return null; } - var result = dbRead.ByTypeCode(new List { typeCode }); + var result = dbRead.ByTypeCode(new List { typeCode }, transactionScopeSuppress); if (result.Any()) { @@ -66,7 +66,7 @@ namespace bnhtrade.Core.Logic.Stock /// /// list of transaction type codes /// DIctionary key=transactionTypeCode, value=transactionType object - public Dictionary GetByTypeCode(List typeCodeList) + public Dictionary GetByTypeCode(List typeCodeList, bool transactionScopeSuppress = false) { var returnDict = new Dictionary(); if (!typeCodeList.Any()) @@ -74,7 +74,7 @@ namespace bnhtrade.Core.Logic.Stock return returnDict; } - var dbResult = dbRead.ByTypeCode(typeCodeList); + var dbResult = dbRead.ByTypeCode(typeCodeList, transactionScopeSuppress); foreach(var item in dbResult) {