Feature: Sync MWS Shipment with Database

Various restructuring and misc. features added.
Removed reliance on ABrain Amazon MWS NuGet package, added Amazon's own C# lib
This commit is contained in:
Bobbie Hodgetts
2019-06-24 16:01:50 +01:00
committed by GitHub
parent bc44546efd
commit 116aedb897
27 changed files with 2236 additions and 289 deletions

View File

@@ -19,9 +19,12 @@ using MarketplaceWebServiceProducts.Model;
using MarketplaceWebServiceProducts;
using System.Data.SqlTypes;
using System.Security.Cryptography;
using Core.AmazonMWS;
using bnhtrade.Core.AmazonAPI;
using bnhtrade.Core.Order;
using bnhtrade.Core.Data.AmazonMWS;
using bnhtrade.Core.Logic.Sku;
namespace Core
namespace bnhtrade.Core
{
namespace Database
{
@@ -2064,7 +2067,7 @@ namespace Core
}
// get/set an skuId
int skuId = Core.Sku.SkuQuery.WIP_SkuGetSet(sqlConnectionString, productId, conditionId, accountTaxCodeId, true);
int skuId = Logic.Sku.GetSkuIdByType.Request(sqlConnectionString, productId, conditionId, accountTaxCodeId, true);
// add the entry to the stock table (minus stockJournalId)
int stockId = 0;
@@ -4232,7 +4235,8 @@ namespace Core
transactionDate = DateTime.SpecifyKind(transactionDate, DateTimeKind.Utc);
int importTableId = reader.GetInt32(index01);
string sku = reader.GetString(index03);
int skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, sku, true);
var skuData = new Data.Database.SKU.GetSKUId(sqlConnectionString);
int skuId = skuData.BySKUNumber(sku, true);
if (skuId < 1)
{
transposeSkip = transposeSkip + 1;
@@ -4428,15 +4432,16 @@ namespace Core
transactionDate = DateTime.SpecifyKind(transactionDate, DateTimeKind.Utc);
int importTableId = reader.GetInt32(index01);
string sku = reader.GetString(index03);
int skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, sku, true);
var skuData = new Data.Database.SKU.GetSKUId(sqlConnectionString);
int skuId = skuData.BySKUNumber(sku, true);
if (skuId < 1)
{ transposeSkip = transposeSkip + 1; continue; }
int quantity = reader.GetInt32(index04);
int orderChannelId = Order.OrderQuery.GetSaleChannelIdByName(sqlConnectionString, reader.GetString(index05), true);
int orderChannelId = OrderQuery.GetSaleChannelIdByName(sqlConnectionString, reader.GetString(index05), true);
string amazonOrderId = reader.GetString(index06);
int orderId = Order.OrderQuery.GetOrderIdBySaleChannelRef(sqlConnectionString, orderChannelId, amazonOrderId, true);
int orderId = OrderQuery.GetOrderIdBySaleChannelRef(sqlConnectionString, orderChannelId, amazonOrderId, true);
string amazonOrderItemId = reader.GetString(index07);
int orderItemId = Order.OrderQuery.GetOrderItemIdBySaleChannelRef(sqlConnectionString, orderId, amazonOrderItemId, true);
int orderItemId = OrderQuery.GetOrderItemIdBySaleChannelRef(sqlConnectionString, orderId, amazonOrderItemId, true);
if (quantity > 0 && transactionTypeId == 0)
{ transposeSkip = transposeSkip + 1; continue; }
@@ -4520,13 +4525,13 @@ namespace Core
string sku = reader.GetString(index04);
// get orderchannelId
int salesChannelId = Order.OrderQuery.GetSaleChannelIdByName(sqlConnectionString, salesChannel);
int salesChannelId = OrderQuery.GetSaleChannelIdByName(sqlConnectionString, salesChannel);
// get/set order ref
int orderId = Order.OrderQuery.GetOrderIdBySaleChannelRef(sqlConnectionString, salesChannelId, amazonOrderId, true);
int orderId = OrderQuery.GetOrderIdBySaleChannelRef(sqlConnectionString, salesChannelId, amazonOrderId, true);
// get/set order item ref
int orderItemId = Order.OrderQuery.GetOrderItemIdBySaleChannelRef(sqlConnectionString, orderId, amazonOrderItemId, true);
int orderItemId = OrderQuery.GetOrderItemIdBySaleChannelRef(sqlConnectionString, orderId, amazonOrderItemId, true);
//
}
Console.Write("\r");
@@ -4611,8 +4616,8 @@ namespace Core
string sku = reader.GetString(index03);
int quantity = reader.GetInt32(index04);
string reference = reader.GetString(index05);
int orderChannelId = Order.OrderQuery.GetSaleChannelIdByAmazonOrderId(sqlConnectionString, reference);
int orderId = Order.OrderQuery.GetOrderIdBySaleChannelRef(sqlConnectionString, orderChannelId, reference, true);
int orderChannelId = OrderQuery.GetSaleChannelIdByAmazonOrderId(sqlConnectionString, reference);
int orderId = OrderQuery.GetOrderIdBySaleChannelRef(sqlConnectionString, orderChannelId, reference, true);
string detail =
"Amazon Assessment: " + disposition + Environment.NewLine +
"Status: " + reader.GetString(index07) + Environment.NewLine +
@@ -4620,7 +4625,8 @@ namespace Core
if (!reader.IsDBNull(index09))
{ detail = detail + Environment.NewLine + "Customer Comments: " + reader.GetString(index09); }
int skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, sku, true);
var skuData = new Data.Database.SKU.GetSKUId(sqlConnectionString);
int skuId = skuData.BySKUNumber(sku, true);
if (skuId < 1)
{ transposeSkip = transposeSkip + 1; continue; }
@@ -4776,7 +4782,8 @@ namespace Core
if (quantity < 0) { quantity = quantity * -1; }
string reference = reader.GetString(index05);
int skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, sku, true);
var skuData = new Data.Database.SKU.GetSKUId(sqlConnectionString);
int skuId = skuData.BySKUNumber(sku, true);
if (skuId < 1) { transposeSkip = transposeSkip + 1; continue; }
using (TransactionScope scope = new TransactionScope())
@@ -5120,7 +5127,8 @@ namespace Core
string reference = reader.GetString(index03);
int quantityTotal = reader.GetInt32(index06);
int skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, reader.GetString(index05), true);
var skuData = new Data.Database.SKU.GetSKUId(sqlConnectionString);
int skuId = skuData.BySKUNumber(sku, true);
if (skuId < 1)
{
transposeSkip = transposeSkip + 1;
@@ -5251,7 +5259,10 @@ namespace Core
if (quantity == 0)
{ transactionTypeId = -1; }
else
{ skuId = Sku.SkuQuery.SkuLookupId(sqlConnectionString, sku, true); }
{
var skuData = new Data.Database.SKU.GetSKUId(sqlConnectionString);
skuId = skuData.BySKUNumber(sku, true);
}
if (skuId < 1)
{ transposeSkip = transposeSkip + 1; continue; }
@@ -5656,171 +5667,7 @@ namespace Core
}
}
namespace Sku
{
public class SkuQuery
{
public static int SkuLookupId(string sqlConnectionString, string sku, bool enableLegacy = false)
{
// if enableLegacy = true, function will attempt to lookup value by sku count
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
using (SqlCommand cmd = new SqlCommand(@"
SELECT skuSkuID FROM tblSku WHERE skuSkuNumber=@sku;
", conn))
{
cmd.Parameters.AddWithValue("@sku", sku);
object obj = cmd.ExecuteScalar();
if (!(obj == null))
{
return Convert.ToInt32(obj);
}
}
// if that didn't work, lookup buy sku count
if (sku.Length == 6 & enableLegacy == true)
{
int skucount;
bool okay = int.TryParse(sku, out skucount);
if (okay)
{
using (SqlCommand cmd = new SqlCommand(
"SELECT skuSkuID FROM tblSku WHERE skuSkuCount=@skuCount;"
, conn))
{
cmd.Parameters.AddWithValue("@skuCount", skucount);
object obj = cmd.ExecuteScalar();
if (!(obj == null))
{
return Convert.ToInt32(obj);
}
else
{
return -1;
}
}
}
}
}
return -1;
}
// used for retriving SKU that matched parameters, creates new if required, returns 0 if not found
public static int WIP_SkuGetSet(string sqlConnectionString, int productId, int conditionId, int accountTaxCodeId, bool noMatchInsertNew)
{
using (TransactionScope scope = new TransactionScope())
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
// look for existing entry
using (SqlCommand cmd = new SqlCommand(@"
SELECT
tblSku.skuSkuID
FROM
tblSku
WHERE
(((tblSku.skuProductID)=@productId) AND ((tblSku.skuSkuConditionID)=@conditionId) AND ((tblSku.AccountTaxCodeID)=@accountTaxCodeId));
", conn))
{
cmd.Parameters.AddWithValue("@productId", productId);
cmd.Parameters.AddWithValue("@conditionId", conditionId);
cmd.Parameters.AddWithValue("@accountTaxCodeId", accountTaxCodeId);
object obj = cmd.ExecuteScalar();
if (obj != null)
{
return (int)obj;
}
}
// value check insert bool
if (noMatchInsertNew == false)
{
return 0;
}
else
{
// get this far, check tax code id is a valid for SKU
using (SqlCommand cmd = new SqlCommand(@"
SELECT tblAccountTaxCode.InvoiceSales
FROM tblAccountTaxCode
WHERE (((tblAccountTaxCode.AccountTaxCodeID)=@accountTaxCodeId));
", conn))
{
cmd.Parameters.AddWithValue("@accountTaxCodeId", accountTaxCodeId);
object obj = cmd.ExecuteScalar();
if (obj == null)
{
throw new Exception("AccountTaxCodeID=" + accountTaxCodeId + " doesn't exist!");
}
else
{
bool result = (bool)obj;
if (result == false)
{
throw new Exception("AccountTaxCodeID=" + accountTaxCodeId + " is not a valid type for an SKU.");
}
}
}
// get info to create sku number
int skuCount;
int skuSuffix;
using (SqlCommand cmd = new SqlCommand("SELECT NEXT VALUE FOR SkuCountSequence;", conn))
{
skuCount = (int)cmd.ExecuteScalar();
}
using (SqlCommand cmd = new SqlCommand(@"
SELECT tblSkuCondition.scnSkuNumberSuffix
FROM tblSkuCondition
WHERE (((tblSkuCondition.scnSkuConditionID)=@conditionId));
", conn))
{
cmd.Parameters.AddWithValue("@conditionId", conditionId);
try
{
skuSuffix = (int)cmd.ExecuteScalar();
}
catch (Exception ex)
{
throw new Exception("Error retriving SKU number suffix for SkuConditionID=" + conditionId + "." +
System.Environment.NewLine + "Error Message: " + ex.Message);
}
}
string skuNumber = skuCount.ToString("D6") + "-" + skuSuffix.ToString("D2");
// insert new sku
int skuId;
using (SqlCommand cmd = new SqlCommand(@"
INSERT INTO tblSku
(skuSkuNumber, skuProductID, skuSkuConditionID, AccountTaxCodeID)
OUTPUT INSERTED.skuSkuID
VALUES
(@skuNumber, @productId, @conditionId, @accountTaxCodeId)
", conn))
{
cmd.Parameters.AddWithValue("@skuNumber", skuNumber);
cmd.Parameters.AddWithValue("@productId", productId);
cmd.Parameters.AddWithValue("@conditionId", conditionId);
cmd.Parameters.AddWithValue("@accountTaxCodeId", accountTaxCodeId);
skuId = (int)cmd.ExecuteScalar();
}
scope.Complete();
return skuId;
}
}
}
}
}
namespace Inventory
{
public class InventoryPricing
@@ -6421,24 +6268,13 @@ namespace Core
}
}
namespace AmazonMWS
namespace AmazonAPI
{
public class AmazonMwsCredential
{
public string merchantId { get { return "A3RUYNKLWWM5KW"; } }
public string marketPlaceId { get { return "A1F83G8C2ARO7P"; } } // Amazon.co.uk
public string accessKeyId { get { return "AKIAJU45WSYVINEN45UA"; } }
public string secretAccessKey { get { return "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"; } }
public string applicationName { get { return "bnhtrade Database Client"; } }
public string applicationVersion { get { return "0.1"; } }
public string serviceURL { get { return "https://mws.amazonservices.co.uk"; } }
}
public class AmazonMwsService
{
public MarketplaceWebService.MarketplaceWebService GetAmazonMwsService()
{
AmazonMwsCredential cred = new AmazonMwsCredential();
CredentialMws cred = new CredentialMws();
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
MarketplaceWebService.MarketplaceWebService service = new MarketplaceWebServiceClient(
@@ -6457,7 +6293,7 @@ namespace Core
{
public MarketplaceWebServiceProducts.MarketplaceWebServiceProducts GetAmazonMwsServiceProduct()
{
AmazonMwsCredential cred = new AmazonMwsCredential();
CredentialMws cred = new CredentialMws();
MarketplaceWebServiceProductsConfig config = new MarketplaceWebServiceProductsConfig();
config.ServiceURL = cred.serviceURL;
@@ -6478,7 +6314,7 @@ namespace Core
/// <param name="itemList"></param>
public List<FeesEstimateResult> WIP_GetProductEstimateFee( FeesEstimateRequestList requestList)
{
AmazonMwsCredential cred = new AmazonMwsCredential();
CredentialMws cred = new CredentialMws();
AmazonMwsProduct mwsProduct = new AmazonMwsProduct();
MarketplaceWebServiceProducts.MarketplaceWebServiceProducts service = mwsProduct.GetAmazonMwsServiceProduct();
@@ -6549,30 +6385,46 @@ namespace Core
}
public class AmazonReport
{
string merchantId = "A3RUYNKLWWM5KW";
string marketplaceId = "A1F83G8C2ARO7P"; // Amazon.co.uk
string accessKeyId = "AKIAJU45WSYVINEN45UA";
string secretAccessKey = "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";
string applicationName = "AmazonApiApp";
string applicationVersion = "0.1";
string serviceURL = "https://mws.amazonservices.co.uk";
public MarketplaceWebService.MarketplaceWebService GetMwsService()
private string merchantId;
//private string marketplaceId; // Amazon.co.uk
//private string accessKeyId;
//private string secretAccessKey;
//private string applicationName;
//private string applicationVersion;
//private string serviceURL;
// class constructor
public AmazonReport()
{
MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
CredentialMws cred = new CredentialMws();
MarketplaceWebService.MarketplaceWebService service = new MarketplaceWebServiceClient(
accessKeyId,
secretAccessKey,
applicationName,
applicationVersion,
config);
config.ServiceURL = serviceURL;
return service;
this.merchantId = cred.merchantId;
//this.marketplaceId = cred.marketPlaceId;
//this.accessKeyId = cred.accessKeyId;
//this.secretAccessKey = cred.secretAccessKey;
//this.applicationName = cred.applicationName;
//this.applicationVersion = cred.applicationVersion;
//this.serviceURL = cred.serviceURL;
}
//public MarketplaceWebService.MarketplaceWebService GetMwsClient()
//{
// MarketplaceWebServiceConfig config = new MarketplaceWebServiceConfig();
// MarketplaceWebService.MarketplaceWebService client = new MarketplaceWebServiceClient(
// accessKeyId,
// secretAccessKey,
// applicationName,
// applicationVersion,
// config);
// config.ServiceURL = serviceURL;
// return client;
//}
public List<ReportInfo> GetMwsReportList(GetReportListRequest requestList)
{
MarketplaceWebService.MarketplaceWebService service = GetMwsService();
var service = new Data.AmazonMWS.Service().MarketPlaceWeb;
//define the list
GetReportListResult getReportListResult = new GetReportListResult();
@@ -6613,11 +6465,12 @@ namespace Core
} while (true);
}
public string GetMwsReportById(string reportId)
{
// retrives report by reportId, saves to disk, and returns filepath
MarketplaceWebService.MarketplaceWebService service = GetMwsService();
var service = new Service().MarketPlaceWeb;
string filePath = MiscFunction.GetTempFilePath(reportId + ".txt");
@@ -6669,12 +6522,13 @@ namespace Core
} while (true);
return "";
}
public string GetMwsReportByRequest(RequestReportRequest requestReport)
{
try
{
string targetRptType = requestReport.ReportType.ToString();
MarketplaceWebService.MarketplaceWebService service = GetMwsService();
var service = new Service().MarketPlaceWeb;
string status;
RequestReportResponse requestResponse = new RequestReportResponse();
@@ -6811,6 +6665,7 @@ namespace Core
return "";
}
}
public List<string> GetMwsReportByPeriod(string mwsReportEnum, DateTime startTime, DateTime endTime, int maxReportCount = 12, int reportMaxPeriod = 30)
{
// method downloads reports and returns a list of filePaths
@@ -6902,9 +6757,10 @@ namespace Core
return reportIdList;
}
public bool SetMwsReportAcknowledgement(string reportId, bool acknowledged = true)
{
MarketplaceWebService.MarketplaceWebService service = GetMwsService();
var service = new Service().MarketPlaceWeb;
do
{
@@ -6957,6 +6813,7 @@ namespace Core
} while (true);
return false;
}
public bool ImportReportSettlementData(string sqlConnectionString, string filePath)
{
try
@@ -7224,6 +7081,7 @@ namespace Core
}
return true;
}
public bool ImportReportFbaInventoryReceipt(string sqlConnectionString, string filePath, DateTime startDate)
{
SqlConnection sqlConn;
@@ -9543,7 +9401,7 @@ namespace Core
}
else
{
Console.WriteLine("\r[" + DateTime.Now.ToString("HH:mm:ss") + "] " + consoleText);
Console.Write("\r[" + DateTime.Now.ToString("HH:mm:ss") + "] " + consoleText);
}
}
public static string GetTempFilePath(string fileName)