Amazon inventory ledger testing and implementation

Tested what I can until more data for the Amazon Ledger Detail table comes in
This commit is contained in:
Bobbie Hodgetts
2024-11-21 17:50:18 +00:00
committed by GitHub
parent 7a12b49b44
commit c8689e3bba
10 changed files with 134 additions and 184 deletions
@@ -39,10 +39,10 @@ namespace bnhtrade.Core.Logic.Stock
private string ConstructTransactionTypeCode(Model.Import.AmazonFbaInventoryLedgerDetail record)
{
if (string.IsNullOrEmpty(record.Reason))
{
return null;
}
//if (string.IsNullOrEmpty(record.Reason))
//{
// return null;
//}
string transactionCode = "<AmazonReport><GET_LEDGER_DETAIL_VIEW_DATA><" + record.EventType + ">";
@@ -66,33 +66,26 @@ namespace bnhtrade.Core.Logic.Stock
}
/// <summary>
/// Imports/Transaposes all data required for reconcilation, into the sku transaction table.
/// Imports/Transaposes all required data into sku transaction table for reconcilation.
/// </summary>
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.");
}
/// <summary>
@@ -101,7 +94,9 @@ namespace bnhtrade.Core.Logic.Stock
/// <exception cref="NotImplementedException"></exception>
public void ImportAmazonFbaReimbursement()
{
throw new NotImplementedException("Needs testing");
string methodName = "Import 'Amazon FBA Reimbursement' into 'Stock SKU Transactions'";
//throw new NotImplementedException("Needs testing");
/*
* Not to be used for stock reconciliation! A single stock item can have multiple reimburesements aginst it.
@@ -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<string>();
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;
}
}
/// <summary>
@@ -255,12 +258,11 @@ namespace bnhtrade.Core.Logic.Stock
/// <exception cref="NotImplementedException"></exception>
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<string, int>();
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;
}
}
}