mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 22:47:15 +00:00
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:
@@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.AmazonFBAInbound
|
||||
{
|
||||
public class ShipmentSkuAddByFNSKU
|
||||
{
|
||||
public string fnsku { get; private set; }
|
||||
public int skuId { get; private set; }
|
||||
public string skuProductTitle { get; private set; }
|
||||
public string fbaCenterId { get; private set; }
|
||||
public string fbaShipmentId { get; private set; }
|
||||
public bool errorFlag { get; private set; } = false;
|
||||
public string errorMessage { get; private set; } = "";
|
||||
|
||||
// add method here
|
||||
public ShipmentSkuAddByFNSKU Request(string fnsku)
|
||||
{
|
||||
var repsonce = new ShipmentSkuAddByFNSKU();
|
||||
|
||||
// checks
|
||||
if (fnsku.Length != 10)
|
||||
{
|
||||
throw new Exception("Incorrect FNSKU supplied.");
|
||||
}
|
||||
|
||||
repsonce.fnsku = fnsku;
|
||||
|
||||
return repsonce;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,113 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Transactions;
|
||||
using bnhtrade.Core.Data;
|
||||
|
||||
namespace bnhtrade.Core.Logic.AmazonFBAInbound
|
||||
{
|
||||
public class UpdateDatabaseShipmentInfo
|
||||
{
|
||||
private string sqlConnectionString;
|
||||
private readonly string logDateTimeId = "FbaInboundShipmentNewCheck";
|
||||
public int TotalUpdated { get; private set; } = 0;
|
||||
public UpdateDatabaseShipmentInfo(string sqlConnectionString)
|
||||
{
|
||||
this.sqlConnectionString = sqlConnectionString;
|
||||
}
|
||||
public void GetNew()
|
||||
{
|
||||
TotalUpdated = 0;
|
||||
|
||||
// get time frame to check
|
||||
DateTime dateTimeBefore = new Data.AmazonMWS.CurrentDateTime().GetUtc();
|
||||
DateTime dateTimeAfter = new Data.Database.Log.DateTimeLog(sqlConnectionString).GetDateTimeUtc(logDateTimeId);
|
||||
|
||||
//var objGetUtc = new Data.AmazonMWS.CurrentDateTime();
|
||||
//DateTime dateTimeBefore = objGetUtc.GetUtc();
|
||||
|
||||
//var objDateTimeCheck = new Data.Database.Log.DateTimeLog(sqlConnectionString);
|
||||
//DateTime dateTimeAfter = objDateTimeCheck.GetDateTimeUtc(logDateTimeId);
|
||||
|
||||
//
|
||||
var shipmentRequest = new Data.AmazonMWS.FBAInbound.ListInboundShipments();
|
||||
shipmentRequest.LastUpdatedAfter = dateTimeAfter.AddDays(-14);
|
||||
shipmentRequest.LastUpdatedBefore = dateTimeBefore;
|
||||
|
||||
List<Model.AmazonFBAInbound.ShipmentInfo> shipmentInfoList = shipmentRequest.GetShipmentInfo();
|
||||
|
||||
// build list of shipments returned from mws
|
||||
var dicShipExistsInDb = new Dictionary<string, bool>();
|
||||
foreach (var item in shipmentInfoList)
|
||||
{
|
||||
dicShipExistsInDb.Add(item.AmazonShipmentId, false);
|
||||
}
|
||||
|
||||
// build list of shipmentId that do not exist in database
|
||||
int complete = 0;
|
||||
using (TransactionScope scope = new TransactionScope())
|
||||
{
|
||||
List<Model.AmazonFBAInbound.ShipmentInfo> newShipmentInfoList = null;
|
||||
if (dicShipExistsInDb.Any())
|
||||
{
|
||||
var newShipmentId = new List<string>();
|
||||
|
||||
// query db for shipment header info
|
||||
var requestHeader = new Data.Database.FBAInbound.GetShipmentHeaderInfo(sqlConnectionString);
|
||||
requestHeader.ShipmentIdList = dicShipExistsInDb.Keys.ToList();
|
||||
var resultHeader = requestHeader.Execute();
|
||||
|
||||
// compare db and mws result
|
||||
foreach (var item in resultHeader)
|
||||
{
|
||||
dicShipExistsInDb[item.AmazonShipmentId] = true;
|
||||
}
|
||||
foreach (var item in dicShipExistsInDb)
|
||||
{
|
||||
if (item.Value == false)
|
||||
{
|
||||
newShipmentId.Add(item.Key);
|
||||
}
|
||||
}
|
||||
|
||||
// query mws for new shipment info
|
||||
if (newShipmentId.Any())
|
||||
{
|
||||
shipmentRequest.ShipmentIdList = newShipmentId;
|
||||
newShipmentInfoList = shipmentRequest.GetShipmentInfo();
|
||||
|
||||
foreach (var item in newShipmentInfoList)
|
||||
{
|
||||
var shipmentItemInfoRequest = new Data.AmazonMWS.FBAInbound.ListInboundShipmentItems();
|
||||
item.ShipmentItemInfoList = shipmentItemInfoRequest.GetByAmazonShipmentId(item.AmazonShipmentId);
|
||||
}
|
||||
}
|
||||
|
||||
// write to db
|
||||
if (newShipmentInfoList != null)
|
||||
{
|
||||
foreach (var item in newShipmentInfoList)
|
||||
{
|
||||
// add the update date
|
||||
item.LastUpdatedUtc = dateTimeBefore;
|
||||
|
||||
// write to db
|
||||
var dbWrite = new Data.Database.FBAInbound.SetShipmentInfo(sqlConnectionString);
|
||||
dbWrite.Excecute(item);
|
||||
complete = complete + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// update datetime log
|
||||
new Data.Database.Log.DateTimeLog(sqlConnectionString).SetDateTimeUtc(logDateTimeId, dateTimeBefore);
|
||||
//objDateTimeCheck.SetDateTimeUtc(logDateTimeId, dateTimeBefore);
|
||||
|
||||
scope.Complete();
|
||||
}
|
||||
TotalUpdated = complete;
|
||||
}
|
||||
}
|
||||
}
|
||||
121
src/bnhtrade.Core/Logic/Sku/GetSkuIdByType.cs
Normal file
121
src/bnhtrade.Core/Logic/Sku/GetSkuIdByType.cs
Normal file
@@ -0,0 +1,121 @@
|
||||
using System;
|
||||
using System.Data.SqlClient;
|
||||
using System.Transactions;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Sku
|
||||
{
|
||||
class GetSkuIdByType
|
||||
{
|
||||
// used for retriving SKU that matched parameters, creates new if required, returns 0 if not found
|
||||
public static int Request(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user