using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using bnhtradeDatabaseClient; using bnhtradeDatabaseClient.Stock; using System.Configuration; using System.Transactions; namespace bnhtradeScheduledTasks { class Program { static string sqlConnectionString = ConfigurationManager.ConnectionStrings["bnhtradeDbConnString"].ConnectionString; //public static string BNHtradeDbConnectionString() //{ // return ConfigurationManager.ConnectionStrings["bnhtradeDbConnString"].ConnectionString; //} static void Main(string[] args) { //string sqlConnectionString = BNHtradeDbConnectionString(); if (args.Length == 0) { // get db connection string //string sqlConnectionString = BNHtradeDbConnectionString(); string consoleHeader = "Welcome to THE application!\n"; do { Console.Clear(); Console.WriteLine(consoleHeader); Console.WriteLine("Main Menu"); Console.WriteLine(); Console.WriteLine("<1> Amazon reports"); Console.WriteLine("<2> Stock functions"); Console.WriteLine(""); Console.WriteLine("<9> Dev functions"); Console.WriteLine(""); Console.WriteLine("<0> Exit"); Console.WriteLine(""); Console.Write("Enter an option >"); string input = Console.ReadLine(); if (input == "0") { break; } else if (input == "1") { do { Console.Clear(); Console.WriteLine(consoleHeader); Console.WriteLine("Main Menu > Amazon Reports"); Console.WriteLine(); Console.WriteLine("<1> Update all Amazon inventory and settlement data"); Console.WriteLine("<2> Update FBA Inventory Data"); Console.WriteLine("<3> Update FBA Inventory Age Data"); Console.WriteLine("<4> Update Amazon Settlement Data"); Console.WriteLine("<5> Update Fba Inventory Receipt Data"); Console.WriteLine("<6> Update Fba Sale Shipment Data"); Console.WriteLine("<7> Update Fba Return Data"); Console.WriteLine("<8> Update Fba Adustment Data"); Console.WriteLine("<9> Update Fba Removal Order Data"); Console.WriteLine(); Console.WriteLine("<0> Back"); Console.WriteLine(""); Console.Write("Enter an option >"); input = Console.ReadLine(); if (input == "0") { break; } else if (input == "1") { Console.Clear(); DownloadAll(); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "2") { Console.Clear(); var task = new AmazonReport(); task.UpdateFbaInventoryData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "3") { Console.Clear(); var task = new AmazonReport(); task.UpdateFbaInventoryAgeData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "4") { Console.Clear(); var task = new AmazonReport(); var task2 = new StockReconciliation(); task.UpdateAmazonSettlementData(sqlConnectionString); task2.WIP_ProcessAmazonSettlementData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "5") { Console.Clear(); var task = new AmazonReport(); task.UpdateFbaInventoryReceiptData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "6") { Console.Clear(); var task = new AmazonReport(); task.UpdateFbaSaleShipmentData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "7") { Console.Clear(); var task = new AmazonReport(); task.UpdateFbaReturnData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "8") { Console.Clear(); var task = new AmazonReport(); task.UpdateFbaAdustmentData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "9") { Console.Clear(); var task = new AmazonReport(); task.UpdateFbaRemovalOrderReport(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } } while (true); } else if (input == "2") { do { Console.Clear(); Console.WriteLine(consoleHeader); Console.WriteLine("Main Menu > Stock Funcions"); Console.WriteLine(); Console.WriteLine("Stock Reconciliation"); Console.WriteLine(); Console.WriteLine("<1> Update Fba Stock Import Data"); Console.WriteLine("<2> Process Fba Stock Import Data"); Console.WriteLine("<3> Reconcile stock transactions"); Console.WriteLine("<4> Run all above"); Console.WriteLine(); Console.WriteLine("<5> (temp) Update Amazon/Sku Min Max values"); Console.WriteLine(); Console.WriteLine("<0> Back"); Console.WriteLine(""); Console.Write("Enter an option >"); input = Console.ReadLine(); if (input == "0") { break; } else if (input == "1") { Console.Clear(); var task = new StockReconciliation(); try { task.UpdateFbaStockImportData(sqlConnectionString); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "2") { Console.Clear(); var task = new StockReconciliation(); try { task.ProcessFbaStockImportData(sqlConnectionString); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "3") { Console.Clear(); var task = new StockReconciliation(); var result = new StockReconciliation.ReconcileStockTransactionsResult(); try { result = task.ReconcileStockTransactions(sqlConnectionString, false); Console.WriteLine(result.ItemsCompleted + " of " + (result.ItemsCompleted + result.ItemsRemaining) + " items completed."); Console.WriteLine("Current transaction ID=" + result.StockTransactionId); Console.WriteLine(result.ProgressMessage); Console.WriteLine(""); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "4") { Console.Clear(); var task = new StockReconciliation(); try { task.UpdateFbaStockImportData(sqlConnectionString); task.ProcessFbaStockImportData(sqlConnectionString); task.ReconcileStockTransactions(sqlConnectionString, false); } catch (Exception ex) { Console.WriteLine(ex.Message.ToString()); } Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "5") { Console.Clear(); //try //{ bnhtradeDatabaseClient.Inventory.InventoryPricing.AmazonMinMaxTemp(sqlConnectionString); //} //catch (Exception ex) //{ //Console.WriteLine(ex.Message.ToString()); //} Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } } while (true); } else if (input == "9") { do { Console.Clear(); Console.WriteLine(consoleHeader); Console.WriteLine("Main Menu > Dev Funcions"); Console.WriteLine(); Console.WriteLine("<1> Test Import of Amazon Settlement Data into Account Transaction Table"); Console.WriteLine("<2> Test Stock Journal Reallocate"); Console.WriteLine("<3> test Product Update Amazon Estimate Fee"); Console.WriteLine("<4> Process Amazon Reimbursement Report (into transactiontable)"); Console.WriteLine("<5> Test Stock Table Delete"); Console.WriteLine("<6> Test Owner intro insert"); Console.WriteLine("<7> Currency exchange rate insert"); Console.WriteLine("<8> Update Amazon Inventory Table"); Console.WriteLine(); Console.WriteLine("<0> Back"); Console.WriteLine(""); Console.Write("Enter an option >"); input = Console.ReadLine(); if (input == "0") { break; } else if (input == "1") { Console.Clear(); var task = new StockReconciliation(); task.WIP_ProcessAmazonSettlementData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "2") { Console.Clear(); int result = new int(); try { result = TempStuff.TempTasks.testStockReallocate(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(result); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "3") { Console.Clear(); TempStuff.TempTasks.test_ProductUpdateAmazonEstimateFee(); Console.WriteLine("Done"); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "4") { Console.Clear(); var task = new StockReconciliation(); task.WIP_ProcessFbaReimbursementData(sqlConnectionString); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "5") { Console.Clear(); int result = new int(); try { TempStuff.TempTasks.testStockJournalDelete(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(result); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "6") { Console.Clear(); int result = 0; try { result = TempStuff.TempTasks.input6(); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine(result.ToString()); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "7") { Console.Clear(); //bool result = false; int result = 0; try { result = TempStuff.TempTasks.CurrencyExchangeInsert(); Console.WriteLine("Result: " + result.ToString()); } catch (Exception ex) { Console.WriteLine(ex.Message); } Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } else if (input == "8") { Console.Clear(); try { TempStuff.TempTasks.test_AmazonInventoryTableUpdate(); } catch (Exception ex) { Console.WriteLine(ex.Message); } //Console.WriteLine(result.ToString()); Console.WriteLine("Complete, press any key to continue..."); Console.ReadKey(); } } while (true); } } while (true); } else { if (args.Length > 1) { Console.WriteLine("Currently only one command line argument is accepted. Exiting application..."); Thread.Sleep(3000); } else { if (args[0] == "RunAll") { Console.WriteLine("Starting all reports download..."); Thread.Sleep(3000); Console.Clear(); DownloadAll(); Console.WriteLine("Finished!! Exiting application..."); Thread.Sleep(3000); } else if (args[0] == "StockReconcileUnsafe") { Console.WriteLine("Starting (unsafe) stock reconciliation..."); Console.Clear(); StockReconciliation task = new StockReconciliation(); StockReconciliation.ReconcileStockTransactionsResult result = new StockReconciliation.ReconcileStockTransactionsResult(); result = task.ReconcileStockTransactions(sqlConnectionString, false); Console.WriteLine("Progress: " + result.ProgressMessage); Console.WriteLine("Reconciled date: " + result.LastItemDateTime.ToString()); Console.WriteLine("Transactions completed: " + result.ItemsCompleted); Console.WriteLine("Transactions remaining: " + result.ItemsRemaining); if (!result.ReconciliationComplete) { Console.WriteLine("Halted at Stock Transaction ID: " + result.StockTransactionId); } Console.WriteLine("Exiting application..."); } else { Console.WriteLine("Command line parameter not recognised. Exiting application..."); Thread.Sleep(3000); } } Environment.Exit(0); } } private static void DownloadAll() { MiscFunction.EventLogInsert("Nightly scheduled tasks started."); var account = new AmazonReport(); var stock = new StockReconciliation(); bool accountUpdate = false; bool stockUpdate = false; bool accountProcess = false; bool stockProcess = false; while (true) { try { if (accountUpdate == false) { accountUpdate = true; account.UpdateAmazonSettlementData(sqlConnectionString); } if (stockUpdate == false) { stockUpdate = true; stock.UpdateFbaStockImportData(sqlConnectionString); } if (accountProcess == false) { accountProcess = true; stock.WIP_ProcessAmazonSettlementData(sqlConnectionString); } // if (stockProcess == false) { stockProcess = true; stock.ProcessFbaStockImportData(); } // ^^^^^^ best to process manually, case, fba inventory recepts, if a correction is made days later (ie -1) the already incorrect value // will have been entered in the stocktransaction table and maked as processed in the inventoryreceipt table break; } catch (Exception ex) { MiscFunction.EventLogInsert( "Exception caught running all report get method, see for further details", 1, ex.ToString() ); } } MiscFunction.EventLogInsert("Nightly scheduled tasks finished."); } } } namespace TempStuff { public class TempTasks { static string sqlConnectionString = ConfigurationManager.ConnectionStrings["bnhtradeDbConnString"].ConnectionString; public static void testStockTableDelete() { //bnhtradeDatabaseClient.Stock.StockQuery.WIP_StockTableDelete(sqlConnectionString, 15776); } public static bool testStockConsistCheck() { //return bnhtradeDatabaseClient.Stock.StockQuery.WIP_StockJournalConsistencyCheck(sqlConnectionString, 22677); return false; } public static void testStockDelete() { //bnhtradeDatabaseClient.Stock.StockQuery.WIP_StockDelete(sqlConnectionString, 15798); } public static void testStockJournalDelete() { //bnhtradeDatabaseClient.Stock.StockQuery.StockJournalDelete(sqlConnectionString, 33763); } public static int testStockReallocate() { int creditStatusId = 4; int debitStatusId = 21; DateTime entryDate = new DateTime(2099, 06, 15); //return bnhtradeDatabaseClient.Stock.StockQuery.StockReallocateByStockId(sqlConnectionString, 4, 15776, 1, debitStatusId, creditStatusId, entryDate); return 0; } public static void testPurchaseLineInsert() { DateTime entrdate = DateTime.Parse("07/09/2016 08:13:54"); //return bnhtradeDatabaseClient.Purchase.PurchaseQuery.WIP_PurchaseLineNetTransactionInsert(sqlConnectionString, 10164, "GBP", 138, 9.98m, entrdate); bnhtradeDatabaseClient.Purchase.PurchaseQuery.WIP_PurchaseLineTransactionNetUpdate(sqlConnectionString, 10164, "GBP", 138, 100000); } public static void test_ProductUpdateAmazonEstimateFee() { var list = new List<(string asin, decimal price)>(); list.Add(("B000MGVBG4", 1.99m)); bnhtradeDatabaseClient.Product.ProductQuery.ProductUpdateAmazonEstimateFee(sqlConnectionString, list); } public static void test_AmazonInventoryTableUpdate() { //bnhtradeDatabaseClient.Inventory.InventoryPricing.AmazonInventoryTableUpdate(sqlConnectionString); } public static int input6() { DateTime entrdate = DateTime.Parse("28/11/2018 08:13:54"); return bnhtradeDatabaseClient.Stock.StockCreate.WIP_StockInsertOwnerIntroduced(sqlConnectionString, 0.01m, 7, 15374, 51, 16, entrdate, 51); } public static int CurrencyExchangeInsert() { DateTime start = new DateTime(2019, 03, 01); DateTime finish = new DateTime(2019, 04, 01); return bnhtradeDatabaseClient.Account.AccountQuery.CurrencyExchangeRateInsert(sqlConnectionString, 1, "USD", 222m, start, finish, true ); } } }