mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-21 15:27:15 +00:00
Migration from Amazon MWS to Selling Partner API
This commit is contained in:
38
src/bnhtrade.Core/Logic/Import/AmazonFbaCustomerReturn.cs
Normal file
38
src/bnhtrade.Core/Logic/Import/AmazonFbaCustomerReturn.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonFbaCustomerReturn
|
||||
{
|
||||
public void SyncDatabaseWithAmazon()
|
||||
{
|
||||
/*
|
||||
* flat file lines/rows maybe unique
|
||||
* Overlap reports by 3 days.
|
||||
* To disambiguate duplicate row, I will use combination of date, orderid, fnsku, quantity, LPN#, disposition, and reason.
|
||||
*/
|
||||
|
||||
// get most recent record from db
|
||||
var db = new Data.Database.Import.AmazonFbaCustomerReturn();
|
||||
var startDate = db.ReadRecentDate().AddDays(-14);
|
||||
|
||||
var getFile = new Data.Amazon.Report.FbaCustomerReturn();
|
||||
getFile.GetReport(startDate, DateTime.UtcNow);
|
||||
|
||||
if (!getFile.ReportFilePathIsSet)
|
||||
{
|
||||
MiscFunction.EventLogInsert(
|
||||
"Something went wrong retriving report from Amazon SP API. Operation cancelled.",
|
||||
2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
db.UpdateByFlatFile(getFile.ReportFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
src/bnhtrade.Core/Logic/Import/AmazonFbaInventory.cs
Normal file
28
src/bnhtrade.Core/Logic/Import/AmazonFbaInventory.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonFbaInventory
|
||||
{
|
||||
public void SyncDatabaseWithAmazon()
|
||||
{
|
||||
var getFile = new Data.Amazon.Report.FbaInventory();
|
||||
getFile.GetReport();
|
||||
|
||||
if (!getFile.ReportFilePathIsSet)
|
||||
{
|
||||
MiscFunction.EventLogInsert(
|
||||
"Something went wrong retriving report from Amazon SP API. Operation cancelled.",
|
||||
2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
new Data.Database.Import.AmazonFbaInventoryData().InsertByFlatFile(getFile.ReportFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonFbaInventoryAdjustment
|
||||
{
|
||||
public void SyncDatabaseWithAmazon()
|
||||
{
|
||||
// get most recent record from db
|
||||
var db = new Data.Database.Import.AmazonFbaInventoryAdjustment();
|
||||
var startDate = db.ReadRecentDate().AddDays(-3);
|
||||
|
||||
var getFile = new Data.Amazon.Report.FbaInventoryAdustment();
|
||||
getFile.GetReport(startDate, DateTime.UtcNow);
|
||||
|
||||
if (!getFile.ReportFilePathIsSet)
|
||||
{
|
||||
MiscFunction.EventLogInsert(
|
||||
"Something went wrong retriving report from Amazon SP API. Operation cancelled.",
|
||||
2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
db.InsertByFlatFile(getFile.ReportFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
28
src/bnhtrade.Core/Logic/Import/AmazonFbaInventoryAge.cs
Normal file
28
src/bnhtrade.Core/Logic/Import/AmazonFbaInventoryAge.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonFbaInventoryAge
|
||||
{
|
||||
public void SyncDatabaseWithAmazon()
|
||||
{
|
||||
var getFile = new Data.Amazon.Report.FbaInventoryAge();
|
||||
getFile.GetReport();
|
||||
|
||||
if (!getFile.ReportFilePathIsSet)
|
||||
{
|
||||
MiscFunction.EventLogInsert(
|
||||
"Something went wrong retriving report from Amazon SP API. Operation cancelled.",
|
||||
2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
new Data.Database.Import.AmazonFbaInventoryAgeData().InsertByFlatFile(getFile.ReportFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/bnhtrade.Core/Logic/Import/AmazonFbaInventoryReceipt.cs
Normal file
32
src/bnhtrade.Core/Logic/Import/AmazonFbaInventoryReceipt.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonFbaInventoryReceipt
|
||||
{
|
||||
public void SyncDatabaseWithAmazon()
|
||||
{
|
||||
// get most recent record from db
|
||||
var db = new Data.Database.Import.AmazonFbaInventoryReceipt();
|
||||
var startDate = db.ReadRecentDate().AddSeconds(1);
|
||||
|
||||
var getReport = new Data.Amazon.Report.FbaInventoryReceipt();
|
||||
getReport.GetReport(startDate, DateTime.UtcNow);
|
||||
|
||||
if (!getReport.ReportFilePathIsSet)
|
||||
{
|
||||
MiscFunction.EventLogInsert(
|
||||
"Something went wrong retriving report from Amazon SP API. Operation cancelled.",
|
||||
2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
db.InsertByFlatFile(getReport.ReportFilePath, startDate);
|
||||
}
|
||||
}
|
||||
}
|
||||
32
src/bnhtrade.Core/Logic/Import/AmazonFbaReimbursement.cs
Normal file
32
src/bnhtrade.Core/Logic/Import/AmazonFbaReimbursement.cs
Normal file
@@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonFbaReimbursement
|
||||
{
|
||||
public void SyncDatabaseWithAmazon()
|
||||
{
|
||||
// get most recent record from db
|
||||
var db = new Data.Database.Import.AmazonFbaReimbursement();
|
||||
var startDate = db.ReadRecentDate().AddDays(-3);
|
||||
|
||||
var getFile = new Data.Amazon.Report.FbaReimbursement();
|
||||
getFile.GetReport(startDate, DateTime.UtcNow);
|
||||
|
||||
if (!getFile.ReportFilePathIsSet)
|
||||
{
|
||||
MiscFunction.EventLogInsert(
|
||||
"Something went wrong retriving report from Amazon SP API. Operation cancelled.",
|
||||
2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
db.InsertByFlatFile(getFile.ReportFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
39
src/bnhtrade.Core/Logic/Import/AmazonFbaRemovalOrder.cs
Normal file
39
src/bnhtrade.Core/Logic/Import/AmazonFbaRemovalOrder.cs
Normal file
@@ -0,0 +1,39 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonFbaRemovalOrder
|
||||
{
|
||||
public void SyncDatabaseWithAmazon()
|
||||
{
|
||||
/*
|
||||
* flat file lines/rows are unique
|
||||
* FROM MWS GUIDANCE
|
||||
* https://docs.developer.amazonservices.com/en_UK/fba_guide/FBAGuide_TipsShipmentsReport.html
|
||||
* Overlap report by 3 days. To disambiguate duplicate shipment reports, use the ShipmentItemId (not the OrderId or the OrderItemId)
|
||||
*/
|
||||
|
||||
// get most recent record from db
|
||||
var db = new Data.Database.Import.AmazonFbaRemovalOrder();
|
||||
var startDate = db.ReadRecentDate().AddDays(-30);
|
||||
|
||||
var getFile = new Data.Amazon.Report.FbaRemovalOrder();
|
||||
getFile.GetReport(startDate, DateTime.UtcNow);
|
||||
|
||||
if (!getFile.ReportFilePathIsSet)
|
||||
{
|
||||
MiscFunction.EventLogInsert(
|
||||
"Something went wrong retriving report from Amazon SP API. Operation cancelled.",
|
||||
2
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
db.InsertByFlatFile(getFile.ReportFilePath);
|
||||
}
|
||||
}
|
||||
}
|
||||
72
src/bnhtrade.Core/Logic/Import/AmazonFbaSaleShipment.cs
Normal file
72
src/bnhtrade.Core/Logic/Import/AmazonFbaSaleShipment.cs
Normal file
@@ -0,0 +1,72 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonFbaSaleShipment
|
||||
{
|
||||
private int maxTimePeriodDay = 30;
|
||||
public void SyncDatabaseWithAmazon()
|
||||
{
|
||||
/*
|
||||
* flat file lines/rows are unique
|
||||
* FROM MWS GUIDANCE
|
||||
* https://docs.developer.amazonservices.com/en_UK/fba_guide/FBAGuide_TipsShipmentsReport.html
|
||||
* Overlap report by 3 days. To disambiguate duplicate shipment reports, use the ShipmentItemId (not the OrderId or the OrderItemId)
|
||||
*/
|
||||
|
||||
// get most recent record from db
|
||||
var db = new Data.Database.Import.AmazonFbaSaleShipment();
|
||||
var dataSart = db.ReadRecentDate().AddDays(-3);
|
||||
var dataEnd = DateTime.UtcNow;
|
||||
|
||||
// error check
|
||||
if (dataSart >= dataEnd)
|
||||
throw new Exception("Data start time is equal or greater than the end time.");
|
||||
|
||||
bool doLoop = true;
|
||||
var requestStart = dataSart;
|
||||
var requestEnd = dataSart.AddDays(maxTimePeriodDay);
|
||||
if (requestEnd > dataEnd)
|
||||
requestEnd = dataEnd;
|
||||
|
||||
do
|
||||
{
|
||||
var getFile = new Data.Amazon.Report.FbaSaleShipment();
|
||||
getFile.GetReport(requestStart, requestEnd);
|
||||
|
||||
if (getFile.ReportDoneNoData)
|
||||
{
|
||||
// continue
|
||||
}
|
||||
else if (getFile.ReportFilePathIsSet)
|
||||
{
|
||||
db.InsertByFlatFile(getFile.ReportFilePath);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("no report returned");
|
||||
}
|
||||
|
||||
// increment requests times and test for end
|
||||
if (requestEnd == dataEnd)
|
||||
{
|
||||
doLoop = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
requestStart = requestEnd;
|
||||
requestEnd = requestStart.AddDays(maxTimePeriodDay);
|
||||
if (requestEnd > dataEnd)
|
||||
requestEnd = dataEnd;
|
||||
if (requestStart == requestEnd)
|
||||
doLoop = false;
|
||||
}
|
||||
}
|
||||
while (doLoop);
|
||||
}
|
||||
}
|
||||
}
|
||||
67
src/bnhtrade.Core/Logic/Import/AmazonSettlement.cs
Normal file
67
src/bnhtrade.Core/Logic/Import/AmazonSettlement.cs
Normal file
@@ -0,0 +1,67 @@
|
||||
using System.Linq;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Import
|
||||
{
|
||||
public class AmazonSettlement
|
||||
{
|
||||
Data.Amazon.Report.SettlementReport amazonReport;
|
||||
|
||||
public AmazonSettlement(Model.Credentials.AmazonSPAPI spapiCredentials)
|
||||
{
|
||||
amazonReport = new Data.Amazon.Report.SettlementReport();
|
||||
}
|
||||
|
||||
public void SyncDatabase(string sqlConnectionString)
|
||||
{
|
||||
string operation = "Import Amazon Settlement Reports";
|
||||
|
||||
MiscFunction.EventLogInsert("Started '" + operation + "' operation.");
|
||||
|
||||
// get avaiable reports from amazon api
|
||||
var spapiReportIdList = amazonReport.ListAvaliableReports();
|
||||
int reportCount = spapiReportIdList.Count();
|
||||
|
||||
if (reportCount == 0)
|
||||
{
|
||||
MiscFunction.EventLogInsert("Exiting '" + operation + "' operation. No settlement reports availble on Amazon SP-API.");
|
||||
return;
|
||||
}
|
||||
|
||||
// query db and remove reports that have already been imported
|
||||
var dbReportList = new Data.Database.Import.AmazonSettlementHeaderRead().BySpapiReportId(spapiReportIdList);
|
||||
foreach (var dbReport in dbReportList)
|
||||
{
|
||||
if (dbReport.SpapiReportIdIsSet)
|
||||
{
|
||||
for (int i = 0; i < spapiReportIdList.Count; i++)
|
||||
{
|
||||
if (spapiReportIdList[i] == dbReport.SpapiReportId)
|
||||
{
|
||||
spapiReportIdList.RemoveAt(i);
|
||||
i--;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!spapiReportIdList.Any())
|
||||
{
|
||||
MiscFunction.EventLogInsert("Exiting '" + operation + "' operation. No new reports to import (" + reportCount + " avaibale).");
|
||||
return;
|
||||
}
|
||||
|
||||
// import into database
|
||||
var dbImport = new Data.Database.Import.AmazonSettlementInsert();
|
||||
for (int i = 0; i < spapiReportIdList.Count(); i++)
|
||||
{
|
||||
UI.Console.WriteLine("Importing settlement report " + (i + 1) + " of " + spapiReportIdList.Count() + " (ReportID:" + spapiReportIdList[i] + ").");
|
||||
var filePath = amazonReport.GetFile(spapiReportIdList[i]);
|
||||
bool ack = dbImport.ByFlatFile(filePath, spapiReportIdList[i]);
|
||||
MiscFunction.EventLogInsert("Settlment Report imported (ReportID:" + spapiReportIdList[i] + ").");
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user