public void SetStrategy(KCBulkProductSyncConfig config) { switch (config.SyncType) { case KCBulkProductSyncTypesConstants.Custom: if (config.DateTo.GetValueOrDefault() == default) { throw new PXException(KCMessages.DateShouldBeSet(KCConstants.DateTo)); } if (config.DateFrom.GetValueOrDefault() == default) { throw new PXException(KCMessages.DateShouldBeSet(KCConstants.DateFrom)); } if (config.DateTo < config.DateFrom) { throw new PXException(KCMessages.DateToBiggerThanDateFrom); } _strategy = new KCCustomSync(Graph, config.DateFrom.GetValueOrDefault(), config.DateTo.GetValueOrDefault()); break; case KCBulkProductSyncTypesConstants.Delta: _strategy = new KCDeltaSync(Graph); break; case KCBulkProductSyncTypesConstants.Full: _strategy = new KCFullSync(Graph); break; default: throw new ArgumentException(KCMessages.UnsupportedSyncType); } }
public static void Process(IList <KCStore> stores) { logger.SetRequestId(GetRequestId()); for (int i = 0; i < stores.Count; i++) { var tokenSource = new CancellationTokenSource(); try { ProcessStore(stores[i], tokenSource.Token); PXProcessing <KCStore> .SetInfo(i, KCMessages.BulkUploadSuccess(stores[i].SiteMasterCD)); } catch (Exception exception) { string log = KCMessages.ProcessException(stores[i].SiteMasterCD, exception.Message, exception.StackTrace); PXTrace.WriteError(exception); logger.ClearLoggingIds(); logger.Error(log); PXProcessing <KCStore> .SetError(i, new Exception(log)); } finally { tokenSource.Cancel(true); tokenSource.Dispose(); } } }
private static void Process(List <KCPriceAndInventoryMessage> messages) { logger.SetRequestId(GetRequestId()); messages = messages.OrderBy(x => x.CreatedDateTime).ToList(); bool ftp = messages.Count >= GetThresholdValue(); if (ftp && messages.Count > 0) { var tokenSource = new CancellationTokenSource(); try { var reversed = new List <KCPriceAndInventoryMessage>(messages); reversed.Reverse(); new KCMSMQDataHelper(logger.LoggerProperties).ProcessMessageFTP(reversed, tokenSource.Token); logger.ClearLoggingIds(); logger.Information(KCMessages.MSMQSyncFTP(messages.Count)); foreach (KCPriceAndInventoryMessage msg in reversed) { PXProcessing <KCPriceAndInventoryMessage> .SetInfo(messages.IndexOf(msg), $"Data Exchange for {msg.Message} has been processed successfully"); } } catch (Exception exception) { PXProcessing <KCPriceAndInventoryMessage> .SetError(new Exception(exception.Message)); logger.ClearLoggingIds(); logger.Error(exception.Message); } finally { tokenSource.Cancel(true); tokenSource.Dispose(); } } else if (messages.Count > 0) { int index = 0; try { foreach (KCPriceAndInventoryMessage msg in messages) { index = messages.IndexOf(msg); new KCMSMQDataHelper(logger.LoggerProperties).ProcessMessageAPI(msg); PXProcessing <KCPriceAndInventoryMessage> .SetInfo(index, $"Data Exchange for {msg.Message} has been processed successfully"); } } catch (Exception exception) { PXProcessing <KCPriceAndInventoryMessage> .SetError(index, new Exception(exception.Message)); logger.ClearLoggingIds(); logger.Error(exception.Message); } } }
public bool CheckBranches(KCSiteMasterMaint sitemaster, List <KCAPIOrderItem> orderItems, KCAPIOrder order) { BAccount customer = KCGeneralDataHelper.GetCustomerByCAOrder(sitemaster, order); if (customer == null) { logger.SetNonChildEntityId(order.ID.ToString()); logger.Information(KCMessages.CustomerNotFound(order.ID, order.BuyerEmailAddress)); return(false); } bool workflowPublished = new KCNamespaceReview().Test(KCConstants.NamespaceTesterPackage); if (workflowPublished) { bool validItem; foreach (KCAPIOrderItem item in orderItems) { DAC.KNSIKCInventoryItem stockItem = sitemaster.ExistingProducts.SelectSingle(item.ProductID); InventoryItem product = sitemaster.ProductByInvId.SelectSingle(stockItem.InventoryID); KCInventoryItemExt productKCExt = product.GetExtension <KCInventoryItemExt>(); validItem = false; if (product.GetExtension <InventoryItemPCExt>().UsrKNCompositeType == null) { if (productKCExt.UsrKNCPBranch != null && customer.GetExtension <KCCustomerExt>()?.UsrKNCPBranchID != null) { List <string> productBranches = productKCExt.UsrKNCPBranch.Split(',').ToList(); foreach (string branch in productBranches) { if (customer.GetExtension <KCCustomerExt>()?.UsrKNCPBranchID?.Split(',').Select(x => x.Trim()).ToList().Contains(branch) == true) { validItem = true; } } } else { validItem = true; } } else { validItem = true; } if (!validItem) { logger.SetNonChildEntityId(order.ID.ToString()); logger.Information(KCMessages.WebsitesAreNotAssociated(order.ID, product.InventoryCD, customer.AcctName)); return(false); } } } return(true); }
public bool CheckOrderItems(List <KCAPIOrderItem> orderItems, KCSiteMasterMaint exchangeMaint) { bool result = false; if (orderItems != null) { foreach (KCAPIOrderItem orderItem in orderItems) { DAC.KNSIKCInventoryItem stockItem = exchangeMaint.ExistingProducts.SelectSingle(orderItem.ProductID); if (stockItem == null) { logger.SetNonChildEntityId(orderItem.OrderID.ToString()); logger.Information(KCMessages.ProductDoesntExist(orderItem.OrderID, orderItem.ProductID)); return(false); } result = true; } } return(result); }
public bool CheckSalesAccountID(KCSiteMasterMaint sitemaster, InventoryItem product, int orderID, int CAProductId) { if (product != null && !product.StkItem.GetValueOrDefault() && (product.SalesAcctID == null || product.SalesSubID == null)) { DAC.KNSIKCInventoryItem KCChild = sitemaster.KCInventoryItem.SelectSingle(product.InventoryID); if (KCChild != null) { logger.SetNonChildEntityId(orderID.ToString()); logger.Information(KCMessages.SalesAcctIDCannotBeEmpty(orderID, KCChild.UsrKCCAID.GetValueOrDefault())); } else { logger.SetNonChildEntityId(orderID.ToString()); logger.Information(KCMessages.SalesAcctIDCannotBeEmpty(orderID, CAProductId)); } return(false); } return(true); }
public bool CheckProductStatus(KCSiteMasterMaint sitemaster, InventoryItem product, int orderID, int CAProductId) { if (product != null && product.ItemStatus != KCConstants.Active) { DAC.KNSIKCInventoryItem KCChild = sitemaster.KCInventoryItem.SelectSingle(product.InventoryID); if (KCChild != null) { logger.SetNonChildEntityId(orderID.ToString()); logger.Information(KCMessages.ProductDoesntActive(orderID, KCChild.UsrKCCAID.GetValueOrDefault())); } else { logger.SetNonChildEntityId(orderID.ToString()); logger.Information(KCMessages.ProductDoesntActive(orderID, CAProductId)); } return(false); } return(true); }
public void ExportProducts(KCStore store, List <KeyValuePair <string, InventoryItem> > productsForExport = null) { DateTime today = DateTime.Now; _store = store; KCSiteMaster connection = Graph.StoreConfig.Select().RowCast <KCSiteMaster>().First(x => x.SiteMasterCD.Equals(store.SiteMasterCD)); KCARestClient client = new KCARestClient(connection); ApiHelper = new KCInventoryItemAPIHelper(client); if (productsForExport == null) { productsForExport = GetProductsForExport(); } List <KCBulkProduct> dtos = HandleItems(productsForExport); foreach (var item in dtos) { var id = Graph.ItemByCD.SelectSingle(item.Product.Sku).InventoryID; ARSalesPrice rec = Graph.KCSalesPrice.SelectSingle("B", id); if (rec != null) { item.Product.RetailPrice = rec.SalesPrice; } } bool anyProductToExport = dtos.Count > 0; if (anyProductToExport) { string bulkFile = PrepareItemBulkFile(dtos); string productFilePath = GenerateProductUploadPath(connection); UploadFileToFTP(connection, bulkFile, productFilePath); } logger.ClearLoggingIds(); logger.Information(anyProductToExport ? KCMessages.BulkUploadSuccess(store.SiteMasterCD) : KCMessages.NoProductsToExport); }
private static void Process(IList <KCStore> stores) { LoggerProperties.RequestId = GetRequestId(); logger = new KCLoggerProvider(LoggerProperties); int index = 0; foreach (KCStore store in stores) { try { ProcessStore(store); PXProcessing <KCStore> .SetInfo(index, $"Data Exchange for {store.SiteMasterCD} has been processed successfully"); } catch (Exception exception) { string log = KCMessages.ProcessException(store.SiteMasterCD, exception.Message, exception.StackTrace); logger.ClearLoggingIds(); logger.Error(log); PXProcessing <KCStore> .SetError(index, new Exception(log)); } index++; } }
public void ExportShipments(KCStore store) { if (store.DateTo < store.DateFrom) { throw new PXException(KCMessages.DateToBiggerThanDateFrom); } if (store.DateTo.GetValueOrDefault() != default) { store.DateTo = store.DateTo.GetValueOrDefault().AddDays(1); } bool anyExported = false; KCDataExchangeMaint graph = PXGraph.CreateInstance <KCDataExchangeMaint>(); SOOrderShipmentProcess orderShipmentGraph = PXGraph.CreateInstance <SOOrderShipmentProcess>(); KCSiteMaster connection = graph.Connection.Select().RowCast <KCSiteMaster>().Where(x => x.SiteMasterCD.Equals(store.SiteMasterCD)).First(); KCARestClient client = new KCARestClient(connection); KCOrderAPIHelper helperOrder = new KCOrderAPIHelper(client); KCShipmentAPIHelper helperShipment = new KCShipmentAPIHelper(client); List <SOOrder> orders = graph.Orders.Select().RowCast <SOOrder>().Where(x => x.GetExtension <KCSOOrderExt>().UsrKCSiteName?.EndsWith("/Non-FBA") == true).ToList(); foreach (SOOrder order in orders) { IEnumerable <SOOrderShipment> orderShipments = KCGeneralDataHelper.GetOrderShipmentsByOrderNbr(graph, order.OrderNbr); if (orderShipments == null) { continue; } foreach (SOOrderShipment orderShipment in orderShipments) { if (!CheckData(orderShipment, store.DateFrom, store.DateTo)) { continue; } PXResultset <SOLine> lines = graph.OrderLines.Select(orderShipment.ShipmentNbr); SOShipment shipment = KCGeneralDataHelper.GetShipmentByShipmentNbr(orderShipmentGraph, orderShipment.ShipmentNbr); if (!CheckShippingCarrier(helperShipment, shipment)) { logger.Information(KCMessages.ShipViaDoesnotExist(shipment.ShipmentNbr)); continue; } KCSOShipmentExt shipmentKCExt = shipment?.GetExtension <KCSOShipmentExt>(); SOPackageDetail package = KCGeneralDataHelper.GetPackageByShipmentNbr(orderShipmentGraph, orderShipment.ShipmentNbr); KCSOOrderExt orderExt = order.GetExtension <KCSOOrderExt>(); KCMapShipment shipmentMapper = new KCMapShipment(); int?customerOrderNbr = Convert.ToInt32(order.CustomerOrderNbr); if (shipment != null && orderShipment.Confirmed.GetValueOrDefault() && KCGeneralDataHelper.GetExistingCAOrderById(helperOrder, customerOrderNbr) != null && shipmentKCExt?.UsrKCExported != true) { string log; KCErrorResponse response = new KCErrorResponse(); logger.SetParentAndEntityIds(order.OrderNbr, shipment.ShipmentNbr); try { response = helperShipment.MarkTheOrderAsShipped(shipmentMapper.GetAPIShipment(shipment, package, lines), customerOrderNbr); } catch (Exception ex) { log = KCMessages.CorruptedShipment(shipment.ShipmentNbr); logger.Information(log); continue; } if (response != null) { log = KCMessages.ShipmentExportFailure(shipment.ShipmentNbr, response.Error.Message); } else { shipmentKCExt.UsrKCExported = true; orderShipmentGraph.Shipments.Update(shipment); orderShipmentGraph.Save.Press(); anyExported = true; log = KCMessages.ShipmentExported(shipment.ShipmentNbr); } logger.Information(log); } } } logger.ClearLoggingIds(); logger.Information(anyExported ? KCMessages.ShipmentExportSuccess : KCMessages.NoShipmentsToExport); }
public bool CheckAddress(KCSiteMasterMaint graph, KCAPIOrder order) { Country billingCountry = graph.Countries.Select().RowCast <Country>().Where(x => x.CountryID.Equals(order.BillingCountry)).FirstOrDefault(); Country shippingCountry = graph.Countries.Select().RowCast <Country>().Where(x => x.CountryID.Equals(order.ShippingCountry)).FirstOrDefault(); if (billingCountry == null) { logger.SetNonChildEntityId(order.ID.ToString()); logger.Information(KCMessages.InvalidCountryInBillingAddress(order.ID, order.BillingCountry)); return(false); } if (shippingCountry == null) { logger.SetNonChildEntityId(order.ID.ToString()); logger.Information(KCMessages.InvalidCountryInShippingAddress(order.ID, order.ShippingCountry)); return(false); } State billingState = graph.State.SelectSingle(billingCountry.CountryID, order.BillingStateOrProvince); State shippingState = graph.State.SelectSingle(shippingCountry.CountryID, order.ShippingStateOrProvince); if (billingState == null) { logger.SetNonChildEntityId(order.ID.ToString()); logger.Information(KCMessages.InvalidStateInBillingAddress(order.ID, order.BillingStateOrProvinceName)); return(false); } if (shippingState == null) { logger.SetNonChildEntityId(order.ID.ToString()); logger.Information(KCMessages.InvalidStateInShippingAddress(order.ID, order.ShippingStateOrProvinceName)); return(false); } bool billingZip = false; if (order.BillingPostalCode != null) { billingZip = billingCountry.ZipCodeRegexp == null || Regex.IsMatch(order.BillingPostalCode, billingCountry.ZipCodeRegexp); } bool shippingZip = false; if (order.ShippingPostalCode != null) { shippingZip = shippingCountry.ZipCodeRegexp == null || Regex.IsMatch(order.ShippingPostalCode, shippingCountry.ZipCodeRegexp); } if (!shippingZip) { logger.SetNonChildEntityId(order.ID.ToString()); logger.Information(KCMessages.InvalidZipInShippingAddress(order.ID, order.ShippingPostalCode)); return(false); } if (!billingZip) { logger.SetNonChildEntityId(order.ID.ToString()); logger.Information(KCMessages.InvalidZipInBillingAddress(order.ID, order.BillingPostalCode)); return(false); } return(true); }