Added invoice export function and started implementation of unitofwork pattern (#43)

* complete read invoices from db

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* updated nuget package spapi

* WIP

* wip, now test

* wip, jut need to fix tax inclusive line amounts not supported

* wip

* wip, before I f everything up

* no, it complies now, this is the one before I f everything up

* wip

* wip

* wip, logic ready for testing

* wip it builds!!!!

* wip tested, working, need to complete the gui section

* wip

* wip

* wip - created export invoice data delete, time for testing

* wip testing phase

* wip - delete function fully tested and working

* wip on to sorting out the issue with settlement invoices not tallying

* wip

* wip

* wip

* wip

* wip before I complete change the ReadInvoiceLineItem sections

* that appears to have worked, on with the main quest

* no it's doesn't work, saving before i remove the confusing cache system (just use a dictionary!!)

* wipping picadilli

* wip

* wip

* implemented uow on inovice export, now for testing

* wip

* wip all tested do invoice currency convertion fearure

* wip

* pretty much done so long as xero accepts the exported invoices

* Complete!
This commit is contained in:
Bobbie Hodgetts
2025-06-26 23:29:22 +01:00
committed by GitHub
parent 8bbf885a48
commit 29f9fae508
82 changed files with 4606 additions and 2837 deletions

View File

@@ -1,70 +1,97 @@
using bnhtrade.Core.Data.Amazon.Report;
using bnhtrade.Core.Data.Database.UnitOfWork;
using System;
using System.Linq;
namespace bnhtrade.Core.Logic.Import
{
public class AmazonSettlement
{
private readonly IUnitOfWork _providedUnitOfWork = null;
private readonly bool _ownsUnitOfWork = false;
private Data.Amazon.Report.SettlementReport amazonReport;
private Logic.Log.LogEvent log = new Log.LogEvent();
public AmazonSettlement()
{
amazonReport = new Data.Amazon.Report.SettlementReport();
_ownsUnitOfWork = true;
}
internal AmazonSettlement(IUnitOfWork unitOfWork)
{
_providedUnitOfWork = unitOfWork ?? throw new ArgumentNullException(nameof(unitOfWork));
_ownsUnitOfWork = false;
}
public void SyncDatabase()
{
string operation = "Import Amazon Settlement Reports";
log.LogInformation("Started '" + operation + "' operation.");
// get avaiable reports from amazon api
var spapiReportIdList = amazonReport.ListAvaliableReports();
int reportCount = spapiReportIdList.Count();
if (reportCount == 0)
IUnitOfWork currentUow = null;
if (_ownsUnitOfWork)
{
log.LogInformation("Exiting '" + operation + "' operation. No settlement reports availble on Amazon SP-API.");
return;
currentUow = new UnitOfWork();
}
else
{
currentUow = _providedUnitOfWork;
}
// query db and remove reports that have already been imported
var dbReportList = new Data.Database.Import.AmazonSettlementHeaderRead().BySpapiReportId(spapiReportIdList);
foreach (var dbReport in dbReportList)
using (currentUow != null && _ownsUnitOfWork ? currentUow : null)
{
if (dbReport.SpapiReportIdIsSet)
// get avaiable reports from amazon api
var spapiReportIdList = amazonReport.ListAvaliableReports();
int reportCount = spapiReportIdList.Count();
if (reportCount == 0)
{
for (int i = 0; i < spapiReportIdList.Count; i++)
log.LogInformation("Exiting '" + operation + "' operation. No settlement reports availble on Amazon SP-API.");
return;
}
// query db and remove reports that have already been imported
var dbReportList = currentUow.ImportAmazonRepository.ReadAmazonSettlementHeaderInfoBySpapiReportId(spapiReportIdList);
foreach (var dbReport in dbReportList)
{
if (dbReport.SpapiReportIdIsSet)
{
if (spapiReportIdList[i] == dbReport.SpapiReportId)
for (int i = 0; i < spapiReportIdList.Count; i++)
{
spapiReportIdList.RemoveAt(i);
i--;
break;
if (spapiReportIdList[i] == dbReport.SpapiReportId)
{
spapiReportIdList.RemoveAt(i);
i--;
break;
}
}
}
}
}
if (!spapiReportIdList.Any())
{
log.LogInformation("Exiting '" + operation + "' operation. No new reports to import (" + reportCount + " avaibale).");
if (!spapiReportIdList.Any())
{
log.LogInformation("Exiting '" + operation + "' operation. No new reports to import (" + reportCount + " avaibale).");
return;
}
// import into database
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.GetReportFile(spapiReportIdList[i]);
bool ack = currentUow.ImportAmazonRepository.CreateAmazonSettlements(filePath, spapiReportIdList[i]);
log.LogInformation("Settlment Report imported (ReportID:" + spapiReportIdList[i] + ").");
}
if (_ownsUnitOfWork)
{
currentUow.Commit();
}
return;
}
// import into database
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.GetReportFile(spapiReportIdList[i]);
bool ack = dbInsert.ByFlatFile(filePath, spapiReportIdList[i]);
log.LogInformation("Settlment Report imported (ReportID:" + spapiReportIdList[i] + ").");
}
return;
}
}
}