Fix amazon settlement period and other updates

This commit is contained in:
2020-02-11 17:20:45 +00:00
committed by GitHub
parent 7e50da21e7
commit 56647c7648
10 changed files with 128 additions and 46 deletions

View File

@@ -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;

View File

@@ -9,38 +9,90 @@ 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";
return false;
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++;
}
}
}
}

View File

@@ -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
{
invoiceList[i].InvoiceLineList[j].AccountCode = itemCodeInfo.DefaultAccountCode;
invoiceList[i].InvoiceLineList[j].TaxCode = itemCodeInfo.DefaultTaxCode;
invoiceList[i].InvoiceLineList[j].Description = itemCodeInfo.Description;
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.Title;
}
}
}
}
if (newTypeFound)
if (newTypeFound || newTypeTitleFound)
{
log.LogError("New line ItemCode created while processing Amazon settlements. Update table to complete.");
if (newTypeFound)
{
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;
}