mirror of
https://github.com/stokebob/bnhtrade.git
synced 2026-03-19 06:27:15 +00:00
Fix amazon settlement period and other updates
This commit is contained in:
@@ -15,10 +15,14 @@ namespace bnhtrade.Core.Data.Database.Account
|
||||
cacheLineItemByTablePk = new Dictionary<int, Model.Account.InvoiceLineItem>();
|
||||
cacheTablePkByItemCode = new Dictionary<string, int>();
|
||||
}
|
||||
|
||||
private Dictionary<int, Model.Account.InvoiceLineItem> cacheLineItemByTablePk;
|
||||
private Dictionary<string, int> cacheTablePkByItemCode;
|
||||
|
||||
public bool InsertNewOnNoMatch { get; set; } = false;
|
||||
|
||||
public string NewTypeTitle { get; } = "NEW TYPE";
|
||||
|
||||
public void CacheClear()
|
||||
{
|
||||
cacheLineItemByTablePk.Clear();
|
||||
@@ -111,7 +115,7 @@ namespace bnhtrade.Core.Data.Database.Account
|
||||
if (InsertNewOnNoMatch)
|
||||
{
|
||||
int tablePk = 0;
|
||||
returnObject.Title = "NEW TYPE";
|
||||
returnObject.Title = NewTypeTitle;
|
||||
returnObject.ItemCode = itemCode;
|
||||
returnObject.IsNewReviewRequired = true;
|
||||
|
||||
|
||||
@@ -9,39 +9,91 @@ namespace bnhtrade.Core.Data.Database.Consistency
|
||||
{
|
||||
public class ImportAmazonSettlement : Connection
|
||||
{
|
||||
public string ErrorMessage { get; set; }
|
||||
public ImportAmazonSettlement(string sqlConnectionString) : base(sqlConnectionString)
|
||||
{
|
||||
|
||||
}
|
||||
public bool RunCheck()
|
||||
|
||||
public List<string> ErrorMessage { get; private set; }
|
||||
|
||||
public bool ErrorMessageIsSet
|
||||
{
|
||||
get
|
||||
{
|
||||
if (ErrorMessage == null || !ErrorMessage.Any()) { return false; }
|
||||
else { return true; }
|
||||
}
|
||||
}
|
||||
|
||||
public bool PeriodDateGaps()
|
||||
{
|
||||
ErrorMessage = null;
|
||||
|
||||
using (var sqlConn = new SqlConnection())
|
||||
using (var sqlConn = new SqlConnection(sqlConnectionString))
|
||||
{
|
||||
var log = new Logic.Log.LogEvent();
|
||||
sqlConn.Open();
|
||||
|
||||
// get list of marketplace names
|
||||
var marketplaces = new List<string>();
|
||||
using (SqlCommand cmd = new SqlCommand(@"
|
||||
SELECT Count(tblImportAmazonSettlementReportLine.ImportAmazonSettlementReportLineID) AS CountOfID
|
||||
FROM tblImportAmazonSettlementReportLine
|
||||
INNER JOIN tblImportAmazonSettlementReport ON tblImportAmazonSettlementReportLine.ImportAmazonSettlementReportID = tblImportAmazonSettlementReport.ImportAmazonSettlementReportID
|
||||
WHERE (
|
||||
((tblImportAmazonSettlementReport.IsProcessed) = 0)
|
||||
AND ((tblImportAmazonSettlementReportLine.AccountTransactionID) IS NOT NULL)
|
||||
)
|
||||
OR (
|
||||
((tblImportAmazonSettlementReport.IsProcessed) = 0)
|
||||
AND ((tblImportAmazonSettlementReportLine.IsProcessed) = 1)
|
||||
);
|
||||
SELECT DISTINCT tblImportAmazonSettlementReport.ImportAmazonSettlementReportID
|
||||
,tblImportAmazonSettlementReport.[marketplace-name]
|
||||
,tblImportAmazonSettlementReport.[settlement-start-date]
|
||||
,tblImportAmazonSettlementReport.[settlement-end-date]
|
||||
FROM tblImportAmazonSettlementReport
|
||||
ORDER BY tblImportAmazonSettlementReport.[marketplace-name]
|
||||
,tblImportAmazonSettlementReport.[settlement-start-date]
|
||||
,tblImportAmazonSettlementReport.[settlement-end-date];
|
||||
", sqlConn))
|
||||
{
|
||||
int count = Convert.ToInt32(cmd.ExecuteScalar());
|
||||
if (count != 0)
|
||||
using(var reader = cmd.ExecuteReader())
|
||||
{
|
||||
ErrorMessage = "Error, " + count + " settlement report lines have transactionId/IsProcessed set on an unprocessed settlement";
|
||||
if (!reader.HasRows)
|
||||
{
|
||||
ErrorMessage.Add("No data found. Is the table empty?");
|
||||
return false;
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
string marketplace = "some-random-string-7posadlnvl2oaweoh3amol5o5irv8nl2aoqefl";
|
||||
DateTime endDate = DateTime.SpecifyKind(default(DateTime), DateTimeKind.Utc);
|
||||
while (reader.Read())
|
||||
{
|
||||
if (reader.IsDBNull(1))
|
||||
{
|
||||
log.LogError(
|
||||
"Action required: Enter market place name for settlelment report id " + reader.GetInt32(0) + "."
|
||||
, "Unable to process settlement data from one settlement report '" + reader.GetInt32(0) +
|
||||
"'. Report header table requires a market place name, which is not supplied in original " +
|
||||
"report from Amazon. This is useually inferred from settlement lines. " +
|
||||
"However, in this case, it was not not possible. Manual edit/entry for database table required."
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
||||
string newMarketPlace = reader.GetString(1);
|
||||
DateTime startDate = DateTime.SpecifyKind( reader.GetDateTime(2), DateTimeKind.Utc);
|
||||
|
||||
if (marketplace != newMarketPlace)
|
||||
{
|
||||
marketplace = newMarketPlace;
|
||||
i = 0;
|
||||
}
|
||||
|
||||
if (i > 0 && startDate != endDate)
|
||||
{
|
||||
log.LogError("Inconsistancy in DB data found, break in settlement period dates.",
|
||||
marketplace + " at ID=" + reader.GetInt32(0) + " start-date is not the same as the previous period end-date."
|
||||
+ Environment.NewLine + "If it's a missing settlement report and it's over 90 days ago, you may need to go to"
|
||||
+ " 'Seller Central' > 'Payments' > 'All Statements' and manually 'Request Report' for each missing settlement.");
|
||||
return false;
|
||||
}
|
||||
|
||||
endDate = DateTime.SpecifyKind(reader.GetDateTime(3), DateTimeKind.Utc);
|
||||
i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
|
||||
@@ -25,24 +25,46 @@ namespace bnhtrade.Core.Data.Database.Export
|
||||
var readItemCode = new Data.Database.Account.ReadInvoiceLineItemCode(sqlConnectionString);
|
||||
readItemCode.InsertNewOnNoMatch = true;
|
||||
bool newTypeFound = false;
|
||||
bool newTypeTitleFound = false;
|
||||
for (int i = 0; i < invoiceList.Count(); i++)
|
||||
{
|
||||
for (int j = 0; j < invoiceList[i].InvoiceLineList.Count(); j++)
|
||||
{
|
||||
var itemCodeInfo = readItemCode.ByItemCode(invoiceList[i].InvoiceLineList[j].ItemCode);
|
||||
if (itemCodeInfo.IsNewReviewRequired)
|
||||
{ newTypeFound = true; }
|
||||
if (itemCodeInfo.InvoiceLineEntryEnabled == false)
|
||||
{
|
||||
invoiceList[i].InvoiceAmount = invoiceList[i].InvoiceAmount - invoiceList[i].InvoiceLineList[j].GrossTotalAmount;
|
||||
invoiceList[i].InvoiceLineList.RemoveAt(j);
|
||||
j = j - 1;
|
||||
continue;
|
||||
}
|
||||
else if (itemCodeInfo.IsNewReviewRequired)
|
||||
{
|
||||
newTypeFound = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (itemCodeInfo.Title == readItemCode.NewTypeTitle)
|
||||
{ newTypeTitleFound = true; }
|
||||
else
|
||||
{
|
||||
invoiceList[i].InvoiceLineList[j].AccountCode = itemCodeInfo.DefaultAccountCode;
|
||||
invoiceList[i].InvoiceLineList[j].TaxCode = itemCodeInfo.DefaultTaxCode;
|
||||
invoiceList[i].InvoiceLineList[j].Description = itemCodeInfo.Description;
|
||||
invoiceList[i].InvoiceLineList[j].Description = itemCodeInfo.Title;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (newTypeFound || newTypeTitleFound)
|
||||
{
|
||||
if (newTypeFound)
|
||||
{
|
||||
log.LogError("New line ItemCode created while processing Amazon settlements. Update table to complete.");
|
||||
log.LogError("New line ItemCode found. Add item code default values and then try again.");
|
||||
}
|
||||
if (newTypeTitleFound)
|
||||
{
|
||||
log.LogError("ItemCode found with the incomplete title '" + readItemCode.NewTypeTitle + "'. Update title and then try again.");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -8,21 +8,27 @@ using System.Transactions;
|
||||
|
||||
namespace bnhtrade.Core.Logic.Export
|
||||
{
|
||||
public class AmazonSettlementData
|
||||
public class AmazonSettlement
|
||||
{
|
||||
private string sqlConnectionString;
|
||||
private Dictionary<string, Model.Account.TaxCode> taxCodeBySkuNumer;
|
||||
|
||||
public AmazonSettlementData(string sqlConnectionString)
|
||||
public AmazonSettlement(string sqlConnectionString)
|
||||
{
|
||||
this.sqlConnectionString = sqlConnectionString;
|
||||
}
|
||||
|
||||
public void ToInvoice()
|
||||
{
|
||||
var console = new UI.Console.Update();
|
||||
var log = new Logic.Log.LogEvent();
|
||||
log.LogInformation("Starting processing of Amazon settlement data into export invoice table...");
|
||||
|
||||
// check settlement reports consistancy
|
||||
var consistencyCheck = new Data.Database.Consistency.ImportAmazonSettlement(sqlConnectionString).PeriodDateGaps();
|
||||
if (consistencyCheck == false )
|
||||
{ return; }
|
||||
|
||||
// get list of unprocssed settlement reports to export
|
||||
var settlementData = new Data.Database.Import.ReadAmazonSettlement(sqlConnectionString);
|
||||
var settlementList = settlementData.AllUnprocessed();
|
||||
@@ -234,12 +240,13 @@ namespace bnhtrade.Core.Logic.Export
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
scope.Dispose();
|
||||
log.LogError(ex.Message);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Console.Write("\r");
|
||||
log.LogInformation("Finished processing of Amazon settlement data. " + invoiceList.Count() + " invoices created from "+ settlementIdList.Count() + " Amazon settlement reports.");
|
||||
log.LogInformation("\rFinished processing of Amazon settlement data. " + invoiceList.Count() + " invoices created from "+ settlementIdList.Count() + " Amazon settlement reports.");
|
||||
}
|
||||
|
||||
private string BuildLineItemCode(string skuNumber, string transactionType, string amountType, string amountDescription)
|
||||
@@ -8057,12 +8057,12 @@ namespace bnhtrade.Core
|
||||
TypeList reportList = new TypeList();
|
||||
reportList.Type.Add(mwsReportEnum);
|
||||
GetReportListRequest requestList = new GetReportListRequest();
|
||||
requestList.ReportTypeList = reportList;
|
||||
requestList.Merchant = merchantId;
|
||||
requestList.MaxCount = 100;
|
||||
requestList.ReportTypeList = reportList;
|
||||
bool? returnAcknowledged = false;
|
||||
if (returnAcknowledged == true) { requestList.Acknowledged = true; }
|
||||
else if (returnAcknowledged == false) { requestList.Acknowledged = false; }
|
||||
requestList.Acknowledged = false;
|
||||
//requestList.AvailableFromDate = new DateTime(2019, 7, 1); // default is 90 days
|
||||
//requestList.AvailableToDate = new DateTime(2020, 2, 1);
|
||||
|
||||
/*
|
||||
/ request, recive request, and then iterate through the list
|
||||
@@ -8085,15 +8085,12 @@ namespace bnhtrade.Core
|
||||
* Pass file path to function to parse into database
|
||||
*/
|
||||
|
||||
bool import = ImportReportSettlementData(sqlConnectionString, filePath);
|
||||
bool importAck = ImportReportSettlementData(sqlConnectionString, filePath);
|
||||
|
||||
// if opertion succeded, set the acknowledged property
|
||||
if (import & (returnAcknowledged == false | returnAcknowledged == null))
|
||||
if (importAck)
|
||||
{
|
||||
SetMwsReportAcknowledgement(reportId, true);
|
||||
}
|
||||
if (import == true)
|
||||
{
|
||||
reportsImported = reportsImported + 1;
|
||||
}
|
||||
}
|
||||
@@ -8634,7 +8631,7 @@ namespace bnhtrade.Core
|
||||
{
|
||||
if (newLine)
|
||||
{
|
||||
Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + consoleText);
|
||||
Console.WriteLine("\r[" + DateTime.Now.ToString("HH:mm:ss") + "] " + consoleText);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace bnhtrade.Core.Test
|
||||
}
|
||||
private void AmazonSettlement()
|
||||
{
|
||||
var instance = new Core.Logic.Export.AmazonSettlementData(sqlConnectionString);
|
||||
var instance = new Core.Logic.Export.AmazonSettlement(sqlConnectionString);
|
||||
instance.ToInvoice();
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace bnhtrade.Core.Test.Logic
|
||||
}
|
||||
public void UpdateXeroWithAmzonSettlementData()
|
||||
{
|
||||
var instance = new bnhtrade.Core.Logic.Export.AmazonSettlementData(sqlConnectionString);
|
||||
var instance = new bnhtrade.Core.Logic.Export.AmazonSettlement(sqlConnectionString);
|
||||
instance.ToInvoice();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,7 +12,7 @@ namespace bnhtrade.Core.UI.Console
|
||||
{
|
||||
if (newLine)
|
||||
{
|
||||
System.Console.WriteLine("[" + DateTime.Now.ToString("HH:mm:ss") + "] " + consoleText);
|
||||
System.Console.WriteLine("\r[" + DateTime.Now.ToString("HH:mm:ss") + "] " + consoleText);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -23,7 +23,7 @@ namespace bnhtrade.Core.UI.Console
|
||||
{
|
||||
do
|
||||
{
|
||||
System.Console.Write("\r[--------] " + consoleMessage, string.Format("{0:00}", waitSeconds));
|
||||
System.Console.Write("\r[--------] " + consoleMessage, string. Format("{0:00}", waitSeconds));
|
||||
System.Threading.Thread.Sleep(1000);
|
||||
waitSeconds = waitSeconds - 1;
|
||||
} while (waitSeconds > 0);
|
||||
|
||||
@@ -76,7 +76,7 @@
|
||||
<Compile Include="Logic\AmazonFBAInbound\UpdateDatabaseShipmentInfo.cs" />
|
||||
<Compile Include="Data\Database\Export\CreateSalesInvoice.cs" />
|
||||
<Compile Include="Logic\Export\ValidateSalesInvoice.cs" />
|
||||
<Compile Include="Logic\Export\AmazonSettlementData.cs" />
|
||||
<Compile Include="Logic\Export\AmazonSettlement.cs" />
|
||||
<Compile Include="Logic\Import\ValidateAmazonSettlement.cs" />
|
||||
<Compile Include="Logic\Validate.cs" />
|
||||
<Compile Include="Logic\Log\LogEvent.cs" />
|
||||
|
||||
@@ -107,7 +107,7 @@ namespace bnhtradeScheduledTasks
|
||||
{
|
||||
Console.Clear();
|
||||
var task = new AmazonReport();
|
||||
var task2 = new bnhtrade.Core.Logic.Export.AmazonSettlementData(sqlConnectionString);
|
||||
var task2 = new bnhtrade.Core.Logic.Export.AmazonSettlement(sqlConnectionString);
|
||||
task.UpdateAmazonSettlementData(sqlConnectionString);
|
||||
task2.ToInvoice();
|
||||
Console.WriteLine("Complete, press any key to continue...");
|
||||
@@ -469,7 +469,7 @@ namespace bnhtradeScheduledTasks
|
||||
|
||||
var account = new AmazonReport();
|
||||
var stock = new StockReconciliation();
|
||||
var export = new bnhtrade.Core.Logic.Export.AmazonSettlementData(sqlConnectionString);
|
||||
var export = new bnhtrade.Core.Logic.Export.AmazonSettlement(sqlConnectionString);
|
||||
|
||||
bool accountUpdate = false;
|
||||
bool stockUpdate = false;
|
||||
|
||||
Reference in New Issue
Block a user