improvement to settlement report marketplace inference

This commit is contained in:
2026-04-29 19:16:33 +01:00
parent db8eafe571
commit f921e79671
@@ -369,7 +369,7 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
// //
/// <summary> /// <summary>
/// Update the settlement report marketplace name by settlement Id (not table id) /// Update the settlement report marketplace name by settlement Id (not row id)
/// </summary> /// </summary>
/// <param name="settlementId">Settlement id (not table/record id) of the settlement to update</param> /// <param name="settlementId">Settlement id (not table/record id) of the settlement to update</param>
/// <param name="marketPlaceName">marketplace name</param> /// <param name="marketPlaceName">marketplace name</param>
@@ -433,13 +433,13 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
/// Takes a Settlement Report flat file from Amazon and imports it into the database. /// Takes a Settlement Report flat file from Amazon and imports it into the database.
/// </summary> /// </summary>
/// <param name="filePath">path to the Amazon report flat file</param> /// <param name="filePath">path to the Amazon report flat file</param>
/// <param name="reportId">The unique Amazon SP-API report id (not settlement id)</param> /// <param name="spapiReportId">The unique Amazon SP-API report id (not settlement id)</param>
/// <returns></returns> /// <returns></returns>
public bool CreateAmazonSettlements(string filePath, string reportId) public bool CreateAmazonSettlements(string filePath, string spapiReportId)
{ {
int settlementReportId = 0; int settlementReportId = 0;
string settlementRef = ""; string settlementRef = null;
bool marketPlaceUpdated = false; string marketplaceName = null;
decimal settlementAmount = 0m; decimal settlementAmount = 0m;
int lineNumber = 2; int lineNumber = 2;
int lineSkip = 0; int lineSkip = 0;
@@ -479,17 +479,17 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
int indexQuantityPurchased = Array.IndexOf(headers, "quantity-purchased"); int indexQuantityPurchased = Array.IndexOf(headers, "quantity-purchased");
int indexPromotionId = Array.IndexOf(headers, "promotion-id"); int indexPromotionId = Array.IndexOf(headers, "promotion-id");
string currency = ""; string currency = null;
string fileRow; string fileRow;
while ((fileRow = reader.ReadLine()) != null) while ((fileRow = reader.ReadLine()) != null)
{ {
Console.Write("\rParsing record: " + lineNumber); Console.Write("\rParsing record: " + lineNumber);
//split line into array //split line into array
string[] items = fileRow.Split('\t'); string[] rowArray = fileRow.Split('\t');
if (items.Length != columnCount) if (rowArray.Length != columnCount)
{ {
// skip line // skip line, check settlement total at the end
lineSkip = lineSkip + 1; lineSkip = lineSkip + 1;
_log.LogWarning( _log.LogWarning(
"Line #" + lineNumber + " skipped due to no enough element in row.", "Line #" + lineNumber + " skipped due to no enough element in row.",
@@ -498,28 +498,25 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
} }
else if (lineNumber == 2) else if (lineNumber == 2)
{ {
// check if settlement has already been imported settlementRef = rowArray[indexSettlementId];
currency = rowArray[indexCurrency];
settlementAmount = decimal.Parse(rowArray[indexTotalAmount].Replace(",", "."));
// check if settlement has already been imported
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand) using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{ {
cmd.CommandText = "SELECT COUNT(*) FROM tblImportAmazonSettlementReport WHERE [settlement-id]=@settlementId;"; cmd.CommandText = "SELECT COUNT(*) FROM tblImportAmazonSettlementReport WHERE [settlement-id]=@settlementId;";
cmd.Transaction = _transaction as SqlTransaction; cmd.Transaction = _transaction as SqlTransaction;
cmd.Parameters.AddWithValue("@settlementId", items[indexSettlementId]); cmd.Parameters.AddWithValue("@settlementId", rowArray[indexSettlementId]);
int recordCount = (int)cmd.ExecuteScalar(); int recordCount = (int)cmd.ExecuteScalar();
if (recordCount > 0) if (recordCount > 0)
{ {
SetSpapiReportId(items[indexSettlementId], reportId); SetSpapiReportId(rowArray[indexSettlementId], spapiReportId);
_log.LogInformation("Settlement report already imported, skipping..."); _log.LogInformation("Settlement report already imported, skipping...");
return true; return true;
} }
} }
//set currencyId
//currencyId = GeneralQueries.GetCurrencyId(items[5]);
//set currency
currency = items[indexCurrency];
settlementAmount = decimal.Parse(items[indexTotalAmount].Replace(",", "."));
// insert // insert
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand) using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
@@ -543,61 +540,38 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
,@depositDate ,@depositDate
,@settlementotalAmounttId ,@settlementotalAmounttId
,@currency ,@currency
,@reportId ,@spapiReportId
);"; );";
// add parameters
if (indexSettlementId == -1 || items[indexSettlementId].Length == 0) { cmd.Parameters.AddWithValue("@settlementId", DBNull.Value); }
else
{
settlementRef = items[indexSettlementId];
cmd.Parameters.AddWithValue("@settlementId", settlementRef);
}
var parseDateTime = new Core.Logic.Utilities.DateTime(); var parseDateTime = new Core.Logic.Utilities.DateTime();
if (indexSettlementStartDate == -1 || items[indexSettlementStartDate].Length == 0) { cmd.Parameters.AddWithValue("@settlementStartDate", DBNull.Value); } // add parameters
else { cmd.Parameters.AddWithValue("@settlementStartDate", parseDateTime.ParseIsoDateTimeString(items[indexSettlementStartDate])); } cmd.Parameters.AddWithValue("@settlementId", rowArray[indexSettlementId]);
cmd.Parameters.AddWithValue("@settlementStartDate", parseDateTime.ParseIsoDateTimeString(rowArray[indexSettlementStartDate]));
if (indexSettlementEndDate == -1 || items[indexSettlementEndDate].Length == 0) { cmd.Parameters.AddWithValue("@settlementEndDate", DBNull.Value); } cmd.Parameters.AddWithValue("@settlementEndDate", parseDateTime.ParseIsoDateTimeString(rowArray[indexSettlementEndDate]));
else { cmd.Parameters.AddWithValue("@settlementEndDate", parseDateTime.ParseIsoDateTimeString(items[indexSettlementEndDate])); } cmd.Parameters.AddWithValue("@depositDate", parseDateTime.ParseIsoDateTimeString(rowArray[indexDepositDate]));
cmd.Parameters.AddWithValue("@settlementotalAmounttId", settlementAmount);
if (indexDepositDate == -1 || items[indexDepositDate].Length == 0) { cmd.Parameters.AddWithValue("@depositDate", DBNull.Value); } if (string.IsNullOrWhiteSpace(spapiReportId)) { cmd.Parameters.AddWithValue("@spapiReportId", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@depositDate", parseDateTime.ParseIsoDateTimeString(items[indexDepositDate])); } else { cmd.Parameters.AddWithValue("@spapiReportId", spapiReportId); }
if (indexTotalAmount == -1 || items[indexTotalAmount].Length == 0) { cmd.Parameters.AddWithValue("@totalAmount", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@settlementotalAmounttId", settlementAmount); }
if (string.IsNullOrWhiteSpace(reportId)) { cmd.Parameters.AddWithValue("@reportId", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@reportId", reportId); }
cmd.Parameters.AddWithValue("@currency", currency); cmd.Parameters.AddWithValue("@currency", currency);
//if (currencyId == -1) { sqlCommand.Parameters.AddWithValue("@currencyId", DBNull.Value); }
//else { sqlCommand.Parameters.AddWithValue("@currencyId", currencyId); }
//execute and retrive id //execute and retrive id
settlementReportId = (int)cmd.ExecuteScalar(); settlementReportId = (int)cmd.ExecuteScalar();
} }
} }
else else
{ {
//update market place name in main table, if required // attempt to retrieve marketplace name for header table
if (marketPlaceUpdated == false && settlementReportId > 0 && items[indexMarketplaceName].Length > 1) if (rowArray[indexMarketplaceName].Length > 1 && settlementReportId > 0)
{ {
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand) if (marketplaceName == null)
{ {
cmd.Transaction = _transaction as SqlTransaction; marketplaceName = rowArray[indexMarketplaceName];
cmd.CommandText = @" }
UPDATE tblImportAmazonSettlementReport else if (marketplaceName != rowArray[indexMarketplaceName])
SET [marketplace-name]=@MarketplaceName {
WHERE ImportAmazonSettlementReportID=@ImportAmazonSettlementReportID;"; _log.LogError("Marketplace name '" + rowArray[indexMarketplaceName] + "' on line " + lineNumber +
cmd.Parameters.AddWithValue("@MarketplaceName", items[indexMarketplaceName]); " is different from marketplace name '" + marketplaceName + "' on previous line. This shouldn't be possible!");
cmd.Parameters.AddWithValue("@ImportAmazonSettlementReportID", settlementReportId);
cmd.ExecuteNonQuery();
marketPlaceUpdated = true;
} }
} }
@@ -620,67 +594,95 @@ namespace bnhtrade.Core.Data.Database.Repository.Implementation
cmd.Parameters.AddWithValue("@currency", currency); cmd.Parameters.AddWithValue("@currency", currency);
if (indexTransactionType == -1 || items[indexTransactionType].Length == 0) { cmd.Parameters.AddWithValue("@TransactionType", DBNull.Value); } if (indexTransactionType == -1 || rowArray[indexTransactionType].Length == 0) { cmd.Parameters.AddWithValue("@TransactionType", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@TransactionType", items[indexTransactionType]); } else { cmd.Parameters.AddWithValue("@TransactionType", rowArray[indexTransactionType]); }
if (indexOrderId == -1 || items[indexOrderId].Length == 0) { cmd.Parameters.AddWithValue("@orderRef", DBNull.Value); } if (indexOrderId == -1 || rowArray[indexOrderId].Length == 0) { cmd.Parameters.AddWithValue("@orderRef", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@orderRef", items[indexOrderId]); } else { cmd.Parameters.AddWithValue("@orderRef", rowArray[indexOrderId]); }
if (indexMerchantOrderId == -1 || items[indexMerchantOrderId].Length == 0) { cmd.Parameters.AddWithValue("@merchantOrderRef", DBNull.Value); } if (indexMerchantOrderId == -1 || rowArray[indexMerchantOrderId].Length == 0) { cmd.Parameters.AddWithValue("@merchantOrderRef", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@merchantOrderRef", items[indexMerchantOrderId]); } else { cmd.Parameters.AddWithValue("@merchantOrderRef", rowArray[indexMerchantOrderId]); }
if (indexAdjustmentId == -1 || items[indexAdjustmentId].Length == 0) { cmd.Parameters.AddWithValue("@AdjustmentRef", DBNull.Value); } if (indexAdjustmentId == -1 || rowArray[indexAdjustmentId].Length == 0) { cmd.Parameters.AddWithValue("@AdjustmentRef", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@AdjustmentRef", items[indexAdjustmentId]); } else { cmd.Parameters.AddWithValue("@AdjustmentRef", rowArray[indexAdjustmentId]); }
if (indexShipmentId == -1 || items[indexShipmentId].Length == 0) { cmd.Parameters.AddWithValue("@ShipmentRef", DBNull.Value); } if (indexShipmentId == -1 || rowArray[indexShipmentId].Length == 0) { cmd.Parameters.AddWithValue("@ShipmentRef", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@ShipmentRef", items[indexShipmentId]); } else { cmd.Parameters.AddWithValue("@ShipmentRef", rowArray[indexShipmentId]); }
if (indexMarketplaceName == -1 || items[indexMarketplaceName].Length == 0) { cmd.Parameters.AddWithValue("@MarketplaceName", DBNull.Value); } if (indexMarketplaceName == -1 || rowArray[indexMarketplaceName].Length == 0) { cmd.Parameters.AddWithValue("@MarketplaceName", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@MarketplaceName", items[indexMarketplaceName]); } else { cmd.Parameters.AddWithValue("@MarketplaceName", rowArray[indexMarketplaceName]); }
if (indexAmountType == -1 || items[indexAmountType].Length == 0) { cmd.Parameters.AddWithValue("@AmountType", DBNull.Value); } if (indexAmountType == -1 || rowArray[indexAmountType].Length == 0) { cmd.Parameters.AddWithValue("@AmountType", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@AmountType", items[indexAmountType]); } else { cmd.Parameters.AddWithValue("@AmountType", rowArray[indexAmountType]); }
if (indexAmountDescription == -1 || items[indexAmountDescription].Length == 0) { cmd.Parameters.AddWithValue("@AmountDescription", DBNull.Value); } if (indexAmountDescription == -1 || rowArray[indexAmountDescription].Length == 0) { cmd.Parameters.AddWithValue("@AmountDescription", DBNull.Value); }
else else
{ {
string amountDescription = items[indexAmountDescription]; string amountDescription = rowArray[indexAmountDescription];
if (amountDescription.Length > 100) { amountDescription = amountDescription.Substring(0, 100); } if (amountDescription.Length > 100) { amountDescription = amountDescription.Substring(0, 100); }
cmd.Parameters.AddWithValue("@AmountDescription", amountDescription); cmd.Parameters.AddWithValue("@AmountDescription", amountDescription);
} }
if (indexAmount == -1 || items[indexAmount].Length == 0) { cmd.Parameters.AddWithValue("@Amount", DBNull.Value); } if (indexAmount == -1 || rowArray[indexAmount].Length == 0) { cmd.Parameters.AddWithValue("@Amount", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@Amount", decimal.Parse(items[indexAmount].Replace(",", "."))); } else { cmd.Parameters.AddWithValue("@Amount", decimal.Parse(rowArray[indexAmount].Replace(",", "."))); }
if (indexFulfillmentId == -1 || items[indexFulfillmentId].Length == 0) { cmd.Parameters.AddWithValue("@FulfillmentRef", DBNull.Value); } if (indexFulfillmentId == -1 || rowArray[indexFulfillmentId].Length == 0) { cmd.Parameters.AddWithValue("@FulfillmentRef", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@FulfillmentRef", items[indexFulfillmentId]); } else { cmd.Parameters.AddWithValue("@FulfillmentRef", rowArray[indexFulfillmentId]); }
if (indexPostedDateTime == -1 || items[indexPostedDateTime].Length == 0) { cmd.Parameters.AddWithValue("@PostedDateTimeUTC", DBNull.Value); } if (indexPostedDateTime == -1 || rowArray[indexPostedDateTime].Length == 0) { cmd.Parameters.AddWithValue("@PostedDateTimeUTC", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@PostedDateTimeUTC", new Logic.Utilities.DateTime().ParseIsoDateTimeString(items[indexPostedDateTime])); } else { cmd.Parameters.AddWithValue("@PostedDateTimeUTC", new Logic.Utilities.DateTime().ParseIsoDateTimeString(rowArray[indexPostedDateTime])); }
if (indexOrderItemCode == -1 || items[indexOrderItemCode].Length == 0) { cmd.Parameters.AddWithValue("@OrderItemCode", DBNull.Value); } if (indexOrderItemCode == -1 || rowArray[indexOrderItemCode].Length == 0) { cmd.Parameters.AddWithValue("@OrderItemCode", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@OrderItemCode", long.Parse(items[indexOrderItemCode])); } else { cmd.Parameters.AddWithValue("@OrderItemCode", long.Parse(rowArray[indexOrderItemCode])); }
if (indexMerchantOrderItemId == -1 || items[indexMerchantOrderItemId].Length == 0) { cmd.Parameters.AddWithValue("@MerchantOrderItemRef", DBNull.Value); } if (indexMerchantOrderItemId == -1 || rowArray[indexMerchantOrderItemId].Length == 0) { cmd.Parameters.AddWithValue("@MerchantOrderItemRef", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@MerchantOrderItemRef", long.Parse(items[indexMerchantOrderItemId])); } else { cmd.Parameters.AddWithValue("@MerchantOrderItemRef", long.Parse(rowArray[indexMerchantOrderItemId])); }
if (indexMerchantAdjustmentItemId == -1 || items[indexMerchantAdjustmentItemId].Length == 0) { cmd.Parameters.AddWithValue("@MerchantAdjustmentItemRef", DBNull.Value); } if (indexMerchantAdjustmentItemId == -1 || rowArray[indexMerchantAdjustmentItemId].Length == 0) { cmd.Parameters.AddWithValue("@MerchantAdjustmentItemRef", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@MerchantAdjustmentItemRef", items[indexMerchantAdjustmentItemId]); } else { cmd.Parameters.AddWithValue("@MerchantAdjustmentItemRef", rowArray[indexMerchantAdjustmentItemId]); }
if (indexSku == -1 || items[indexSku].Length == 0) { cmd.Parameters.AddWithValue("@SkuNumber", DBNull.Value); } if (indexSku == -1 || rowArray[indexSku].Length == 0) { cmd.Parameters.AddWithValue("@SkuNumber", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@SkuNumber", items[indexSku]); } else { cmd.Parameters.AddWithValue("@SkuNumber", rowArray[indexSku]); }
if (indexQuantityPurchased == -1 || items[indexQuantityPurchased].Length == 0) { cmd.Parameters.AddWithValue("@QuantityPurchased", DBNull.Value); } if (indexQuantityPurchased == -1 || rowArray[indexQuantityPurchased].Length == 0) { cmd.Parameters.AddWithValue("@QuantityPurchased", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@QuantityPurchased", int.Parse(items[indexQuantityPurchased])); } else { cmd.Parameters.AddWithValue("@QuantityPurchased", int.Parse(rowArray[indexQuantityPurchased])); }
if (indexPromotionId == -1 || items[indexPromotionId].Length == 0) { cmd.Parameters.AddWithValue("@PromotionRef", DBNull.Value); } if (indexPromotionId == -1 || rowArray[indexPromotionId].Length == 0) { cmd.Parameters.AddWithValue("@PromotionRef", DBNull.Value); }
else { cmd.Parameters.AddWithValue("@PromotionRef", items[indexPromotionId]); } else { cmd.Parameters.AddWithValue("@PromotionRef", rowArray[indexPromotionId]); }
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
} }
lineNumber = lineNumber + 1; lineNumber = lineNumber + 1;
} }
// end of filestream reading
// if we haven't gotten the marketplace name from the line items, attempt to infer from currency
if (marketplaceName == null && settlementReportId > 0)
{
if (currency == "GBP")
{
marketplaceName = MarketPlaceEnum.AmazonUK.GetMarketplaceUrl();
marketplaceName = char.ToUpper(marketplaceName[0]) + marketplaceName.Substring(1);
}
}
// finally, if we have it, add marketplace name to main table
if (marketplaceName != null && settlementReportId > 0)
{
using (SqlCommand cmd = _connection.CreateCommand() as SqlCommand)
{
cmd.Transaction = _transaction as SqlTransaction;
cmd.CommandText = @"
UPDATE tblImportAmazonSettlementReport
SET [marketplace-name]=@MarketplaceName
WHERE ImportAmazonSettlementReportID=@ImportAmazonSettlementReportID;";
cmd.Parameters.AddWithValue("@MarketplaceName", marketplaceName);
cmd.Parameters.AddWithValue("@ImportAmazonSettlementReportID", settlementReportId);
cmd.ExecuteNonQuery();
}
}
} }
//final check - settlement amount matches sum of inserted settlement lines //final check - settlement amount matches sum of inserted settlement lines