mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 06:27:15 +00:00
Migration from Amazon MWS to Selling Partner API
This commit is contained in:
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