diff --git a/src/bnhtrade.Core/Data/Database/Account/ReadInvoiceLineItemCode.cs b/src/bnhtrade.Core/Data/Database/Account/ReadInvoiceLineItemCode.cs index bed3bc6..94708ff 100644 --- a/src/bnhtrade.Core/Data/Database/Account/ReadInvoiceLineItemCode.cs +++ b/src/bnhtrade.Core/Data/Database/Account/ReadInvoiceLineItemCode.cs @@ -15,10 +15,14 @@ namespace bnhtrade.Core.Data.Database.Account cacheLineItemByTablePk = new Dictionary(); cacheTablePkByItemCode = new Dictionary(); } + private Dictionary cacheLineItemByTablePk; private Dictionary 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; diff --git a/src/bnhtrade.Core/Data/Database/Consistency/ImportAmazonSettlement.cs b/src/bnhtrade.Core/Data/Database/Consistency/ImportAmazonSettlement.cs index 04b1274..4c48e66 100644 --- a/src/bnhtrade.Core/Data/Database/Consistency/ImportAmazonSettlement.cs +++ b/src/bnhtrade.Core/Data/Database/Consistency/ImportAmazonSettlement.cs @@ -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 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(); 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++; + } } } } diff --git a/src/bnhtrade.Core/Data/Database/Export/CreateSalesInvoice.cs b/src/bnhtrade.Core/Data/Database/Export/CreateSalesInvoice.cs index a3f56c9..88919ae 100644 --- a/src/bnhtrade.Core/Data/Database/Export/CreateSalesInvoice.cs +++ b/src/bnhtrade.Core/Data/Database/Export/CreateSalesInvoice.cs @@ -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; } diff --git a/src/bnhtrade.Core/Logic/Export/AmazonSettlementData.cs b/src/bnhtrade.Core/Logic/Export/AmazonSettlement.cs similarity index 95% rename from src/bnhtrade.Core/Logic/Export/AmazonSettlementData.cs rename to src/bnhtrade.Core/Logic/Export/AmazonSettlement.cs index 3095113..e853c2b 100644 --- a/src/bnhtrade.Core/Logic/Export/AmazonSettlementData.cs +++ b/src/bnhtrade.Core/Logic/Export/AmazonSettlement.cs @@ -8,21 +8,27 @@ using System.Transactions; namespace bnhtrade.Core.Logic.Export { - public class AmazonSettlementData + public class AmazonSettlement { private string sqlConnectionString; private Dictionary 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) diff --git a/src/bnhtrade.Core/Program.cs b/src/bnhtrade.Core/Program.cs index b78b2b2..5a4c79a 100644 --- a/src/bnhtrade.Core/Program.cs +++ b/src/bnhtrade.Core/Program.cs @@ -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 { diff --git a/src/bnhtrade.Core/Test/AutoExec.cs b/src/bnhtrade.Core/Test/AutoExec.cs index a08a2ff..cf522dd 100644 --- a/src/bnhtrade.Core/Test/AutoExec.cs +++ b/src/bnhtrade.Core/Test/AutoExec.cs @@ -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(); } diff --git a/src/bnhtrade.Core/Test/Logic/Export.cs b/src/bnhtrade.Core/Test/Logic/Export.cs index 97519fb..124b83d 100644 --- a/src/bnhtrade.Core/Test/Logic/Export.cs +++ b/src/bnhtrade.Core/Test/Logic/Export.cs @@ -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(); } } diff --git a/src/bnhtrade.Core/UI/Console/Update.cs b/src/bnhtrade.Core/UI/Console/Update.cs index 765d392..f6104ef 100644 --- a/src/bnhtrade.Core/UI/Console/Update.cs +++ b/src/bnhtrade.Core/UI/Console/Update.cs @@ -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); diff --git a/src/bnhtrade.Core/bnhtrade.Core.csproj b/src/bnhtrade.Core/bnhtrade.Core.csproj index 7b271a9..23a6b72 100644 --- a/src/bnhtrade.Core/bnhtrade.Core.csproj +++ b/src/bnhtrade.Core/bnhtrade.Core.csproj @@ -76,7 +76,7 @@ - + diff --git a/src/bnhtrade.ScheduledTasks/Program.cs b/src/bnhtrade.ScheduledTasks/Program.cs index 1da6e0e..0a3311e 100644 --- a/src/bnhtrade.ScheduledTasks/Program.cs +++ b/src/bnhtrade.ScheduledTasks/Program.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;