private tblInventoryImports Map(InventoryImport import) { var inventoryImport = new tblInventoryImports(); inventoryImport.id = Guid.NewGuid(); inventoryImport.ProductCode = import.ProductCode; inventoryImport.Quantity = import.Quantity; inventoryImport.Ref = DateTime.Now.ToString("yyyyMMddHHmmssfff"); inventoryImport.WarehouseCode = import.WarehouseCode; inventoryImport.ImportStatus = (int)ProcessStatus.New; inventoryImport.IM_DateCreated = DateTime.Now; inventoryImport.IM_DateLastUpdated = DateTime.Now; inventoryImport.IM_Status = (int) EntityStatus.Active; return inventoryImport; }
internal static async void PullInventory() { var fileLocation = FileUtility.GetInventoryFile("LastDateOfSync"); var fromDate = FileUtility.ReadFile(fileLocation); var newSyncDate = DateTime.Now.ToString(); bool boolSessionBegun = false; QBSessionManager sessionManager = new QBSessionManager(); try { if(string.IsNullOrEmpty(fromDate)) { var results = PullInitialInventory(); var filename = "Stockline-Alidi Kenya Limited-Initial" + DateTime.Now.ToString("yyyyMMdd"); results = results.GroupBy(n => new { n.ProductName, n.ToSiteName }) .Select(m => new InventoryImport { Balance = m.Sum(g => g.Balance), ProductName = m.Key.ProductName, ToSiteName = m.Key.ToSiteName }).ToList(); DumpExportInventoryFileAsync(results.ToCsv(), filename); var filePath = FileUtility.GetInventoryFile(filename); bool success=await SendToDistributor(filePath); if (success) DumpExportInventoryFileAsync(newSyncDate, "LastDateOfSync"); } else { List<InventoryImport> inventory = new List<InventoryImport>(); var distributorsList = new List<string>(); var inventoryPerDistributor = new Dictionary<string, List<InventoryImport>>(); var distributorsCodeDictionary = new Dictionary<string, string>(); var distributorInventoryList = new List<InventoryImport>(); var stockSiteCodMappingList = GetStockSiteCodeMapping(); IMsgSetRequest requestMsgSet; IMsgSetResponse responseMsgSet; sessionManager.OpenConnection("", _appName); sessionManager.BeginSession(qdbpath, ENOpenMode.omDontCare); boolSessionBegun = true; requestMsgSet = GetLatestMsgSetRequest(sessionManager); requestMsgSet.Attributes.OnError = ENRqOnError.roeStop; ITransferInventoryQuery transferInventoryQuery = requestMsgSet.AppendTransferInventoryQueryRq(); transferInventoryQuery.ORTransferInventoryQuery.TxnFilterNoCurrency.ORDateRangeFilter. ModifiedDateRangeFilter.FromModifiedDate.SetValue(DateTime.Parse(fromDate), false); transferInventoryQuery.ORTransferInventoryQuery.TxnFilterNoCurrency.ORDateRangeFilter. ModifiedDateRangeFilter.ToModifiedDate.SetValue(DateTime.Parse(newSyncDate), false); transferInventoryQuery.IncludeLineItems.SetValue(true); responseMsgSet = sessionManager.DoRequests(requestMsgSet); IResponse respons = responseMsgSet.ResponseList.GetAt(0); ITransferInventoryRetList lineRetList = respons.Detail as ITransferInventoryRetList; if (lineRetList != null) { var RetCount = lineRetList.Count; for (int i = 0; i < RetCount; i++) { if (lineRetList != null) { var TransferInventoryRet = lineRetList.GetAt(i); if (TransferInventoryRet != null) { var fromSite = TransferInventoryRet.FromInventorySiteRef.FullName.GetValue(); var toSite = TransferInventoryRet.ToInventorySiteRef.FullName.GetValue(); var number = TransferInventoryRet.RefNumber.GetValue(); double lineItemsCount = 0.0; if (TransferInventoryRet.TransferInventoryLineRetList != null) { var toSiteName = TransferInventoryRet.ToInventorySiteRef.FullName.GetValue(); var fromSiteName = TransferInventoryRet.FromInventorySiteRef.FullName.GetValue(); var transferInventoryLineItemsCount = TransferInventoryRet.TransferInventoryLineRetList.Count; for (int j = 0; j < transferInventoryLineItemsCount; j++) { var productFullName = TransferInventoryRet.TransferInventoryLineRetList.GetAt(j).ItemRef.FullName. GetValue(); var lastColonIndex = productFullName.LastIndexOf(":"); var productCode = TransferInventoryRet.TransferInventoryLineRetList.GetAt(j).ItemRef.FullName. GetValue().Substring(lastColonIndex + 1); var productName = GetProductName(productCode); var balance = TransferInventoryRet.TransferInventoryLineRetList.GetAt(j). QuantityTransferred.GetValue(); var distributorInventory = new InventoryImport() { ToSiteName = stockSiteCodMappingList[toSite], Balance = Convert.ToDecimal(balance), ProductName = productCode }; distributorInventoryList.Add(distributorInventory); } Console.WriteLine(@"From {0} To {1} , the Ref number is {2}, Items Transfered {3}", fromSite, toSite, number, lineItemsCount); } } } } } sessionManager.EndSession(); boolSessionBegun = false; sessionManager.CloseConnection(); var filename = "Stockline-Alidi Kenya Limited-" + DateTime.Now.ToString("yyyyMMdd"); distributorInventoryList = distributorInventoryList.GroupBy(n => new {n.ProductName, n.ToSiteName}) .Select( m => new InventoryImport { Balance = m.Sum(g => g.Balance), ProductName = m.Key.ProductName, ToSiteName = m.Key.ToSiteName }).ToList(); DumpExportInventoryFileAsync(distributorInventoryList.ToCsv(), filename); var filePath = FileUtility.GetInventoryFile(filename); bool success = await SendToDistributor(filePath); if(success) DumpExportInventoryFileAsync(newSyncDate, "LastDateOfSync"); } } catch (Exception ex) { if (boolSessionBegun) { sessionManager.EndSession(); sessionManager.CloseConnection(); } string error = (ex.Message.ToString() + "\nStack Trace: \n" + ex.StackTrace + "\nExiting the application"); Log(error); MessageBox.Show(error); } }
internal static List<InventoryImport> PullInitialInventory() { bool boolSessionBegun = false; QBSessionManager sessionManager = new QBSessionManager(); List<InventoryImport> inventory = new List<InventoryImport>(); var products=GetAllProducts(); var distributorInventoryList = new List<InventoryImport>(); var stockSiteCodMappingList = GetStockSiteCodeMapping(); try { IMsgSetRequest requestMsgSet; IMsgSetResponse responseMsgSet; sessionManager.OpenConnection("", _appName); sessionManager.BeginSession(qdbpath, ENOpenMode.omDontCare); boolSessionBegun = true; requestMsgSet = GetLatestMsgSetRequest(sessionManager); requestMsgSet.Attributes.OnError = ENRqOnError.roeStop; //ITransferInventoryQuery transferInventoryQuery = requestMsgSet.AppendTransferInventoryQueryRq(); ////Change from to the date in the lastdateofsync //var fileLocation = FileUtility.GetInventoryFile("LastDateOfSync"); //var from = FileUtility.ReadFile(fileLocation);// DateTime.Now.AddMonths(-1).ToString(); //if (string.IsNullOrWhiteSpace(from)) //{ // from = ConfigurationManager.AppSettings["lastDateOfSync"]; //} ////var fromDate = DateTime.Now.AddHours(-7).ToString(); //var newSyncDate = DateTime.Now.ToString(); //////if(from) ////transferInventoryQuery.ORTransferInventoryQuery.TxnFilterNoCurrency.ORDateRangeFilter.ModifiedDateRangeFilter.FromModifiedDate.SetValue(DateTime.Parse(from), false); ////transferInventoryQuery.ORTransferInventoryQuery.TxnFilterNoCurrency.ORDateRangeFilter.ModifiedDateRangeFilter.ToModifiedDate.SetValue(DateTime.Parse(newSyncDate), false); IItemSitesQuery itemSitesQuery = requestMsgSet.AppendItemSitesQueryRq(); var references = GetStockSiteReference(); // references.ToList().ForEach(s => itemSitesQuery.ORItemSitesQuery.ListIDList.Add(s.Key)); responseMsgSet = sessionManager.DoRequests(requestMsgSet); IResponse respons = responseMsgSet.ResponseList.GetAt(0); IItemSitesRetList lineRetList = respons.Detail as IItemSitesRetList; if (lineRetList != null) { var RetCount = lineRetList.Count; for (int i = 0; i < RetCount; i++) { if (lineRetList != null) { var TransferInventoryRet = lineRetList.GetAt(i); if (TransferInventoryRet != null) { if (TransferInventoryRet.InventorySiteRef == null || TransferInventoryRet.InventorySiteRef.FullName==null) { continue; } var siteName = TransferInventoryRet.InventorySiteRef.FullName.GetValue(); var salesman =stockSiteCodMappingList.ContainsKey(siteName)? stockSiteCodMappingList[siteName]:"Default";//GetStockSiteCode(siteName); var quantityOnHand = TransferInventoryRet.QuantityOnHand.GetValue(); var inventoryItemFullName=TransferInventoryRet.ORItemAssemblyORInventory.ItemInventoryRef.FullName.GetValue(); var product = products.FirstOrDefault(p=>p.pDesc==inventoryItemFullName); if(product!=null ) { var productCode = product.pCode; if(product.exfactory>0) { var distributorInventory = new InventoryImport() { ToSiteName = salesman, Balance = Convert.ToDecimal(quantityOnHand), ProductName = productCode }; distributorInventoryList.Add(distributorInventory); } } } } } } sessionManager.EndSession(); boolSessionBegun = false; sessionManager.CloseConnection(); //var filename = "Stockline-Alidi Kenya Limited-" + DateTime.Now.ToString("yyyyMMdd"); distributorInventoryList = distributorInventoryList.GroupBy(n => new { n.ProductName, n.ToSiteName }) .Select(m => new InventoryImport { Balance = m.Sum(g => g.Balance), ProductName = m.Key.ProductName, ToSiteName = m.Key.ToSiteName }).ToList(); //DumpExportInventoryFileAsync(distributorInventoryList.ToCsv(), filename); //DumpExportInventoryFileAsync(newSyncDate, "LastDateOfSync"); //var filePath = FileUtility.GetInventoryFile(filename); //SendToDistributor(filePath); } catch (Exception ex) { if (boolSessionBegun) { sessionManager.EndSession(); sessionManager.CloseConnection(); } string error = (ex.Message.ToString() + "\nStack Trace: \n" + ex.StackTrace + "\nExiting the application"); Log(error); MessageBox.Show(error); } return distributorInventoryList.OrderBy(n=>n.ToSiteName).ThenBy(n=>n.ProductName).Distinct().ToList(); }