This commit is contained in:
Bobbie Hodgetts
2024-11-06 17:12:34 +00:00
parent a865abb788
commit a0c669f1d4
4 changed files with 61 additions and 24 deletions

View File

@@ -86,6 +86,25 @@ namespace bnhtrade.Core.Data.Amazon.Report
ReportProcessingStatus = ProcessingStatus.NULL;
}
/// <summary>
/// Return a list of report that are currently available filtered parameters set in class
/// </summary>
/// <returns>report list</returns>
protected IList<FikaAmazonAPI.AmazonSpApiSDK.Models.Reports.Report> ListAvailableReports()
{
var parameters = new ParameterReportList();
parameters.reportTypes = new List<ReportTypes>() { reportType };
parameters.marketplaceIds.Add(amznConn.GetCurrentMarketplace.ID);
// request from amazon
return amznConn.Reports.GetReports(parameters);
}
/// <summary>
/// For reports that require a start and end period to report over.
/// </summary>
@@ -169,6 +188,22 @@ namespace bnhtrade.Core.Data.Amazon.Report
}
}
DownloadReport(reportId);
}
protected void DownloadByReportId(string reportId)
{
Init();
DownloadReport(reportId);
}
private void DownloadReport(string reportId)
{
if (report == null)
{
WaitWhileProcessing(reportId);
}
// test for processing status
if (report.ProcessingStatus == FikaAmazonAPI.AmazonSpApiSDK.Models.Reports.Report.ProcessingStatusEnum.DONE)
{
@@ -246,12 +281,19 @@ namespace bnhtrade.Core.Data.Amazon.Report
}
// save to file
string reportFilePath = Config.GetTempFileDirectoryPath() + @"\SP-API-Reports\ " + report.ReportType.ToString() + " reportId_" + reportId + ".txt";
string dirPath = Config.GetTempFileDirectoryPath() + @"\SP-API-Reports\";
System.IO.Directory.CreateDirectory(dirPath);
string reportFilePath = dirPath + report.ReportType.ToString() + " reportId_" + reportId + ".txt";
System.IO.File.WriteAllText(reportFilePath, reportString);
log.LogInformation("Amazon report #" + reportId + " sucessfully saved to disk.");
this.ReportFilePath = reportFilePath;
}
/// <summary>
/// If a duplicate report has been recently requested, Amazon may return 'FATAL'. This method tests for, and attempts to retrive the duplicate report that is causing the error.
/// </summary>

View File

@@ -1,6 +1,7 @@
using bnhtrade.Core.Data.Amazon.SellingPartnerAPI;
using FikaAmazonAPI;
using FikaAmazonAPI.Parameter.Report;
using FikaAmazonAPI.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -10,11 +11,9 @@ using static FikaAmazonAPI.Utils.Constants;
namespace bnhtrade.Core.Data.Amazon.Report
{
public class SettlementReport
public class SettlementReport : ReportLogic
{
private AmazonConnection amznConn = new SpApiConnection().Connection;
public SettlementReport ()
public SettlementReport () : base(ReportTypes.GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2)
{
}
@@ -25,28 +24,22 @@ namespace bnhtrade.Core.Data.Amazon.Report
public List<string> ListAvaliableReports()
{
UI.Console.WriteLine("Requesting list of avaliable settlement reports form Amazon SP-API");
var reportList = ListAvailableReports();
// set parameters
var parameters = new ParameterReportList();
parameters.reportTypes = new List<ReportTypes>() { ReportTypes.GET_V2_SETTLEMENT_REPORT_DATA_FLAT_FILE_V2};
parameters.marketplaceIds.Add(amznConn.GetCurrentMarketplace.ID);
// request from amazon
var result = amznConn.Reports.GetReports(parameters);
var returnList = new List<string>();
foreach (var report in result)
var reportIdList = new List<string>();
foreach (var report in reportList)
{
returnList.Add(report.ReportId);
reportIdList.Add(report.ReportId);
}
UI.Console.WriteLine("{0} Settlement reports avaible on Amazon SP-API");
return returnList;
return reportIdList;
}
public string GetFile(string reportId)
public string GetReportFile(string reportId)
{
return amznConn.Reports.GetReportFile(reportId);
DownloadByReportId(reportId);
return ReportFilePath;
}
}
}

View File

@@ -104,7 +104,7 @@ namespace bnhtrade.Core.Data.Database.Export
cmd.Parameters.AddWithValue("@invoiceID", invoiceId);
cmd.Parameters.AddWithValue("@itemCode", invoiceList[i].InvoiceLineList[j].ItemCode);
cmd.Parameters.AddWithValue("@netAmount", invoiceList[i].InvoiceLineList[j].UnitAmount);
cmd.Parameters.AddWithValue("@accountCode", invoiceList[i].InvoiceLineList[j].AccountCode.AccountCode);
cmd.Parameters.AddWithValue("@accountCode", (int)invoiceList[i].InvoiceLineList[j].AccountCode.AccountCode);
cmd.Parameters.AddWithValue("@taxAmount", invoiceList[i].InvoiceLineList[j].TaxAmount);
cmd.Parameters.AddWithValue("@taxCode", invoiceList[i].InvoiceLineList[j].TaxCode.TaxCode);

View File

@@ -1,4 +1,5 @@
using System.Linq;
using bnhtrade.Core.Data.Amazon.Report;
using System.Linq;
namespace bnhtrade.Core.Logic.Import
{
@@ -10,6 +11,7 @@ namespace bnhtrade.Core.Logic.Import
public AmazonSettlement()
{
amazonReport = new Data.Amazon.Report.SettlementReport();
}
public void SyncDatabase()
@@ -53,12 +55,12 @@ namespace bnhtrade.Core.Logic.Import
}
// import into database
var dbImport = new Data.Database.Import.AmazonSettlementInsert();
var dbInsert = 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]);
var filePath = amazonReport.GetReportFile(spapiReportIdList[i]);
bool ack = dbInsert.ByFlatFile(filePath, spapiReportIdList[i]);
log.LogInformation("Settlment Report imported (ReportID:" + spapiReportIdList[i] + ").");
}